GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/cpuinfo_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 182 183 99.5%
Functions: 1 1 100.0%
Branches: 179 340 52.6%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2024 Barcelona Supercomputing Center */
3 /* */
4 /* This file is part of the DLB library. */
5 /* */
6 /* DLB is free software: you can redistribute it and/or modify */
7 /* it under the terms of the GNU Lesser General Public License as published by */
8 /* the Free Software Foundation, either version 3 of the License, or */
9 /* (at your option) any later version. */
10 /* */
11 /* DLB is distributed in the hope that it will be useful, */
12 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
14 /* GNU Lesser General Public License for more details. */
15 /* */
16 /* You should have received a copy of the GNU Lesser General Public License */
17 /* along with DLB. If not, see <https://www.gnu.org/licenses/>. */
18 /*********************************************************************************/
19
20 #include "unique_shmem.h"
21
22 #include "LB_comm/shmem.h"
23 #include "LB_comm/shmem_cpuinfo.h"
24 #include "apis/dlb_errors.h"
25 #include "support/mask_utils.h"
26 #include "support/types.h"
27
28 #include <sched.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <assert.h>
35
36 /* array_cpuid_t */
37 #define ARRAY_T cpuid_t
38 #include "support/array_template.h"
39
40 /* array_cpuinfo_task_t */
41 #define ARRAY_T cpuinfo_task_t
42 #define ARRAY_KEY_T pid_t
43 #include "support/array_template.h"
44
45 // Basic checks with 1 sub-process
46
47 1 int main( int argc, char **argv ) {
48 1 pid_t pid = getpid();
49 cpu_set_t process_mask, mask;
50 1 sched_getaffinity(0, sizeof(cpu_set_t), &process_mask);
51 1 int mycpu = sched_getcpu();
52 1 printf("mycpu: %d\n", mycpu);
53 1 int system_size = mu_get_system_size();
54 array_cpuinfo_task_t tasks;
55 1 array_cpuinfo_task_t_init(&tasks, system_size);
56 array_cpuid_t cpus_priority_array;
57 1 array_cpuid_t_init(&cpus_priority_array, system_size);
58 1 int64_t last_borrow = 0;
59 1 int requested_ncpus = 0;
60
61 // FIXME: this test assumes that the system's mask is 0..system_size
62 // skip otherwise
63
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (CPU_COUNT(&process_mask) != system_size) {
64 ✗ return 0;
65 }
66
67 // Setup dummy priority CPUs
68
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
69
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 if (CPU_ISSET(i, &process_mask)) {
70 2 array_cpuid_t_push(&cpus_priority_array, i);
71 }
72 }
73
74 // Init
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, 0, &process_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
76
77 // Get pointers to the shmem auxiliary cpu sets
78 1 const cpu_set_t *free_cpus = shmem_cpuinfo_testing__get_free_cpu_set();
79 1 const cpu_set_t *occupied_cores = shmem_cpuinfo_testing__get_occupied_core_set();
80
81 /* Tests using mycpu */
82
83 // Lend CPU
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
86
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 assert( CPU_COUNT(free_cpus) == 1 && CPU_ISSET(mycpu, free_cpus) );
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
88
89 // Reclaim CPU
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
91
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 1 );
92
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == pid
93 && tasks.items[0].cpuid == mycpu
94 && tasks.items[0].action == ENABLE_CPU );
95 1 array_cpuinfo_task_t_clear(&tasks);
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
98
99 // Lend CPU
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
102
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 assert( CPU_COUNT(free_cpus) == 1 && CPU_ISSET(mycpu, free_cpus) );
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
104
105 // Reclaim CPUs
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpus(pid, 1, &tasks) == DLB_SUCCESS );
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 1 );
108
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == pid
109 && tasks.items[0].cpuid == mycpu
110 && tasks.items[0].action == ENABLE_CPU );
111 1 array_cpuinfo_task_t_clear(&tasks);
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
114
115 // Lend CPU
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
117
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
118
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 assert( CPU_COUNT(free_cpus) == 1 && CPU_ISSET(mycpu, free_cpus) );
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
120
121 // Acquire CPU
122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__acquire_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 1 );
124
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == pid
125 && tasks.items[0].cpuid == mycpu
126 && tasks.items[0].action == ENABLE_CPU );
127 1 array_cpuinfo_task_t_clear(&tasks);
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__acquire_cpu(pid, mycpu, &tasks) == DLB_NOUPDT );
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
132
133 // Lend CPU
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu(pid, mycpu, &tasks) == DLB_SUCCESS );
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
136
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
1 assert( CPU_COUNT(free_cpus) == 1 && CPU_ISSET(mycpu, free_cpus) );
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
138
139 // Borrow CPUs
140 1 requested_ncpus = 1;
141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__borrow_ncpus_from_cpu_subset(pid, &requested_ncpus,
142 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0 /* max_parallelism */,
143 &last_borrow, &tasks)
144 == DLB_SUCCESS );
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 1 );
146
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == pid
147 && tasks.items[0].cpuid == mycpu
148 && tasks.items[0].action == ENABLE_CPU );
149 1 array_cpuinfo_task_t_clear(&tasks);
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
152
153 /* Tests using masks */
154
155 // Lend mask
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &process_mask, &tasks) == DLB_SUCCESS );
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
158
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(free_cpus, &process_mask) );
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
160
161 // Reclaim mask
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu_mask(pid, &process_mask, &tasks) >= 0 );
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)system_size );
164
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
165
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 assert( tasks.items[i].pid == pid
166 && tasks.items[i].cpuid == i
167 && tasks.items[i].action == ENABLE_CPU );
168 }
169 1 array_cpuinfo_task_t_clear(&tasks);
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
172
173 // Lend mask
174
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &process_mask, &tasks) == DLB_SUCCESS );
175
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(free_cpus, &process_mask) );
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
178
179 // Reclaim all (all CPUs in system)
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_all(pid, &tasks) >= 0 );
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)system_size );
182
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
183
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 assert( tasks.items[i].pid == pid
184 && tasks.items[i].cpuid == i
185 && tasks.items[i].action == ENABLE_CPU );
186 }
187 1 array_cpuinfo_task_t_clear(&tasks);
188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
190
191 // Lend mask
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &process_mask, &tasks) == DLB_SUCCESS );
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(free_cpus, &process_mask) );
195
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
196
197 // Acquire mask
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__acquire_ncpus_from_cpu_subset(pid, NULL /* requested_ncpus */,
199 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0 /* max_parallelism */,
200 &last_borrow, &tasks) >= 0 );
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)system_size );
202
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
203
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 assert( tasks.items[i].pid == pid
204 /* && tasks.items[i].cpuid == i */ // this depends on the HW
205 && tasks.items[i].action == ENABLE_CPU );
206 }
207 1 array_cpuinfo_task_t_clear(&tasks);
208
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
209
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
210
211 // Lend mask
212
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &process_mask, &tasks) == DLB_SUCCESS );
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
214
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(free_cpus, &process_mask) );
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
216
217 // Borrow all
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__borrow_ncpus_from_cpu_subset(pid, NULL /* requested_ncpus */,
219 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0 /* max_parallelism */,
220 &last_borrow, &tasks) == DLB_SUCCESS );
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)system_size );
222
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
223
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 assert( tasks.items[i].pid == pid
224 /* && tasks.items[i].cpuid == i */ // this depends on the HW
225 && tasks.items[i].action == ENABLE_CPU );
226 }
227 1 array_cpuinfo_task_t_clear(&tasks);
228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
229
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
230
231 // Return
232
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__return_all(pid, &tasks) == DLB_NOUPDT );
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__return_cpu(pid, mycpu, &tasks) == DLB_NOUPDT );
235
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
236
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__return_cpu_mask(pid, &process_mask, &tasks) == DLB_NOUPDT );
237
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
238
239 // MaxParallelism
240
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int max=1; max<system_size; ++max) {
241 // Set up 'max' CPUs
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__update_max_parallelism(pid, max, &tasks) == DLB_SUCCESS );
243
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)(system_size - max) );
244
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (unsigned i=0; i<tasks.count; ++i) {
245
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[i].pid == pid
246 && tasks.items[i].cpuid == i+max
247 && tasks.items[i].action == DISABLE_CPU );
248 }
249 1 array_cpuinfo_task_t_clear(&tasks);
250 // Acquire all CPUs again
251
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__acquire_ncpus_from_cpu_subset(pid, NULL /* requested_ncpus */,
252 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0 /* max_parallelism */,
253 &last_borrow, &tasks) == DLB_SUCCESS );
254 1 array_cpuinfo_task_t_clear(&tasks);
255 }
256
257 // Check errors with a nonexistent CPU
258
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__acquire_cpu(pid, system_size, &tasks) == DLB_ERR_PERM );
259
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu(pid, system_size, &tasks) == DLB_ERR_PERM );
261
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
262 1 CPU_ZERO(&mask);
263
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(system_size, &mask);
264 // mask is not checked beyond max_cpus
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu_mask(pid, &mask, &tasks) == DLB_NOUPDT );
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
267
268 // Deregister process and check that no action is needed
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__deregister(pid, &tasks) == DLB_SUCCESS );
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
271
272 // Finalize
273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
274
275 /* Test inherit CPUs from preinit pid */
276 {
277 1 pid_t preinit_pid = pid+1;
278
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo_ext__init(SHMEM_KEY, 0) == DLB_SUCCESS );
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo_ext__preinit(preinit_pid, &process_mask, DLB_DROM_FLAGS_NONE)
280 == DLB_SUCCESS );
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo_ext__finalize() == DLB_SUCCESS );
282
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, preinit_pid, &process_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
284
285 // Lend mask
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &process_mask, &tasks) == DLB_SUCCESS );
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
288
289 // Reclaim mask
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu_mask(pid, &process_mask, &tasks) >= 0 );
291
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == (unsigned)system_size );
292
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i=0; i<system_size; ++i) {
293
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 assert( tasks.items[i].pid == pid
294 && tasks.items[i].cpuid == i
295 && tasks.items[i].action == ENABLE_CPU );
296 }
297 1 array_cpuinfo_task_t_clear(&tasks);
298
299
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
300 }
301
302 /* Test registering with empty mask */
303 {
304 1 CPU_ZERO(&process_mask);
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, 0, &process_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
306 1 shmem_cpuinfo__print_info(SHMEM_KEY, 0, 0, DLB_COLOR_AUTO);
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
308 }
309
310 // The following tests use a fixed system size of 4. We don't need cpus_priority_array
311 // anymore, but we need to reallocate tasks in case previous system size is smaller.
312 1 array_cpuid_t_destroy(&cpus_priority_array);
313 1 array_cpuinfo_task_t_destroy(&tasks);
314 enum { SYS_SIZE = 4 };
315 1 mu_testing_set_sys_size(SYS_SIZE);
316 1 array_cpuinfo_task_t_init(&tasks, SYS_SIZE);
317
318 /* Test with masks with more bits than system size */
319 {
320 1 mu_parse_mask("0-3", &process_mask);
321
322 // Trying with a process mask with way more CPUs than system:
323 cpu_set_t faulty_mask;
324 1 memset(&faulty_mask, 0xFF, sizeof(cpu_set_t));
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, 0, &process_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
326 1 free_cpus = shmem_cpuinfo_testing__get_free_cpu_set();
327 1 occupied_cores = shmem_cpuinfo_testing__get_occupied_core_set();
328
329 // Lend mask
330
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(pid, &faulty_mask, &tasks) == DLB_SUCCESS );
331
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(free_cpus, &process_mask) );
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
334
335 // Reclaim mask
336
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_cpu_mask(pid, &faulty_mask, &tasks) >= 0 );
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == SYS_SIZE );
338
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (int i = 0; i < SYS_SIZE; ++i) {
339
3/6
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 assert( tasks.items[i].pid == pid
340 && tasks.items[i].cpuid == i
341 && tasks.items[i].action == ENABLE_CPU );
342 }
343 1 array_cpuinfo_task_t_clear(&tasks);
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
345
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
346
347
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
348 }
349
350 /* Tests with disabled CPUs */
351 {
352 1 CPU_ZERO(&process_mask);
353
354 // Init with empty mask
355
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(pid, 0, &process_mask, SHMEM_KEY, 0) == DLB_SUCCESS );
356 1 free_cpus = shmem_cpuinfo_testing__get_free_cpu_set();
357 1 occupied_cores = shmem_cpuinfo_testing__get_occupied_core_set();
358
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
360
361 // Lend system mask
362 cpu_set_t system_mask;
363 1 mu_get_system_mask(&system_mask);
364 1 shmem_cpuinfo__lend_cpu_mask(pid, &system_mask, &tasks);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
367
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(occupied_cores) == 0 );
368
369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(pid, SHMEM_KEY, 0) == DLB_SUCCESS );
370 }
371
372 1 array_cpuinfo_task_t_destroy(&tasks);
373
374 1 mu_finalize();
375
376 1 return 0;
377 }
378