GCC Code Coverage Report


Directory: ./
File: tests/unit/03_policies/lewi_mask_smt_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 138 139 99.3%
Functions: 6 6 100.0%
Branches: 102 194 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 "apis/dlb_errors.h"
23 #include "LB_core/spd.h"
24 #include "LB_comm/shmem_async.h"
25 #include "LB_comm/shmem_procinfo.h"
26 #include "LB_comm/shmem_cpuinfo.h"
27 #include "LB_policies/lewi_mask.h"
28 #include "support/debug.h"
29 #include "support/mask_utils.h"
30 #include "LB_numThreads/numThreads.h"
31
32 #include <assert.h>
33 #include <string.h>
34
35 #define assert_expected(str, cpu_set) \
36 do { \
37 cpu_set_t expected; \
38 mu_parse_mask(str, &expected); \
39 assert( CPU_EQUAL(&expected, cpu_set) ); \
40 } while(0)
41
42
43 static subprocess_descriptor_t spd1;
44 static subprocess_descriptor_t spd2;
45 static cpu_set_t sp1_mask;
46 static cpu_set_t sp2_mask;
47 static interaction_mode_t mode;
48
49 /* Subprocess 1 callbacks */
50 28 static void sp1_cb_enable_cpu(int cpuid, void *arg) {
51
1/2
✓ Branch 0 taken 28 times.
✗ Branch 1 not taken.
28 CPU_SET(cpuid, &sp1_mask);
52 28 }
53
54 4 static void sp1_cb_disable_cpu(int cpuid, void *arg) {
55
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 CPU_CLR(cpuid, &sp1_mask);
56 4 }
57
58 /* Subprocess 2 callbacks */
59 24 static void sp2_cb_enable_cpu(int cpuid, void *arg) {
60
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 CPU_SET(cpuid, &sp2_mask);
61 24 }
62
63 12 static void sp2_cb_disable_cpu(int cpuid, void *arg) {
64
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 CPU_CLR(cpuid, &sp2_mask);
65 12 }
66
67 4 static void wait_for_async_completion(void) {
68
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if (mode == MODE_ASYNC) {
69 4 shmem_async_wait_for_completion(spd1.id);
70 4 shmem_async_wait_for_completion(spd2.id);
71 4 __sync_synchronize();
72 }
73 4 }
74
75 2 int main(int argc, char *arg[]) {
76
77 // Simulate a SMT architecture
78 enum { SYS_NUM_CPUS = 16 };
79 enum { SYS_NUM_CORES = 4 };
80 enum { SYS_NUM_NODES = 1 };
81 2 mu_testing_set_sys(SYS_NUM_CPUS, SYS_NUM_CORES, SYS_NUM_NODES);
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( mu_system_has_smt() );
83
84 // Options
85 2 char options[64] = "--verbose=shmem --shm-key=";
86 2 strcat(options, SHMEM_KEY);
87
88 // Subprocess 1 init
89 2 spd1.id = 111;
90 2 options_init(&spd1.options, options);
91 2 debug_init(&spd1.options);
92 2 mu_parse_mask("0-7", &sp1_mask);
93 2 memcpy(&spd1.process_mask, &sp1_mask, sizeof(cpu_set_t));
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_procinfo__init(spd1.id, 0, &spd1.process_mask, NULL, spd1.options.shm_key,
95 spd1.options.shm_size_multiplier) == DLB_SUCCESS );
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_cpuinfo__init(spd1.id, 0, &spd1.process_mask, spd1.options.shm_key,
97 spd1.options.lewi_color) == DLB_SUCCESS );
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( pm_callback_set(&spd1.pm, dlb_callback_enable_cpu,
99 (dlb_callback_t)sp1_cb_enable_cpu, NULL) == DLB_SUCCESS );
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( pm_callback_set(&spd1.pm, dlb_callback_disable_cpu,
101 (dlb_callback_t)sp1_cb_disable_cpu, NULL) == DLB_SUCCESS );
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Init(&spd1) == DLB_SUCCESS );
103
104 // Subprocess 2 init
105 2 spd2.id = 222;
106 2 options_init(&spd2.options, options);
107 2 debug_init(&spd2.options);
108 2 mu_parse_mask("8-15", &sp2_mask);
109 2 memcpy(&spd2.process_mask, &sp2_mask, sizeof(cpu_set_t));
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_procinfo__init(spd2.id, 0, &spd2.process_mask, NULL, spd2.options.shm_key,
111 spd2.options.shm_size_multiplier) == DLB_SUCCESS );
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_cpuinfo__init(spd2.id, 0, &spd2.process_mask, spd2.options.shm_key,
113 spd2.options.lewi_color) == DLB_SUCCESS );
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( pm_callback_set(&spd2.pm, dlb_callback_enable_cpu,
115 (dlb_callback_t)sp2_cb_enable_cpu, NULL) == DLB_SUCCESS );
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( pm_callback_set(&spd2.pm, dlb_callback_disable_cpu,
117 (dlb_callback_t)sp2_cb_disable_cpu, NULL) == DLB_SUCCESS );
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Init(&spd2) == DLB_SUCCESS );
119
120 // Get interaction mode
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( spd1.options.mode == spd2.options.mode );
122 2 mode = spd1.options.mode;
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_ASYNC) {
124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_async_init(spd2.id, &spd2.pm, &spd2.process_mask,
125 spd2.options.shm_key, spd2.options.shm_size_multiplier) == DLB_SUCCESS );
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_async_init(spd1.id, &spd1.pm, &spd1.process_mask,
127 spd1.options.shm_key, spd1.options.shm_size_multiplier) == DLB_SUCCESS );
128 }
129
130 // Get pointers to the shmem auxiliary cpu sets
131 2 const cpu_set_t *free_cpus = shmem_cpuinfo_testing__get_free_cpu_set();
132 2 const cpu_set_t *occupied_cores = shmem_cpuinfo_testing__get_occupied_core_set();
133
134 /* Lend CPUs in core, they cannot be borrowed until all core is idle */
135 {
136 // Subprocess 1 lends CPU 3
137
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_CLR(3, &sp1_mask);
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpu(&spd1, 3) == DLB_SUCCESS );
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("3", free_cpus);
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
141
142 // Subprocess 2 can't borrow
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_BorrowCpus(&spd2, 1) == DLB_NOUPDT );
144
145 // Subprocess 1 lends CPU 2
146
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_CLR(2, &sp1_mask);
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpu(&spd1, 2) == DLB_SUCCESS );
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_BorrowCpus(&spd2, 1) == DLB_NOUPDT );
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("2-3", free_cpus);
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
151
152 // Subprocess 1 lends CPU 1
153
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_CLR(1, &sp1_mask);
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpu(&spd1, 1) == DLB_SUCCESS );
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_BorrowCpus(&spd2, 1) == DLB_NOUPDT );
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("1-3", free_cpus);
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
158
159 // Subprocess 1 lends CPU 0
160
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_CLR(0, &sp1_mask);
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpu(&spd1, 0) == DLB_SUCCESS );
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3", free_cpus);
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
164
165 // Subprocess 2 can now borrow a CPU (the entire core is borrowed)
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_BorrowCpus(&spd2, 1) == DLB_SUCCESS );
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3,8-15", &sp2_mask);
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(free_cpus) == 0 );
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3", occupied_cores);
170
171 // Subprocess 2 lends again and acquires a CPU (the entire core is acquired)
172 cpu_set_t cpus_to_lend;
173 2 mu_parse_mask("0-3", &cpus_to_lend);
174 2 mu_subtract(&sp2_mask, &sp2_mask, &cpus_to_lend);
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpuMask(&spd2, &cpus_to_lend) == DLB_SUCCESS );
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("8-15", &sp2_mask);
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3", free_cpus);
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_AcquireCpus(&spd2, 1) == DLB_SUCCESS );
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3,8-15", &sp2_mask);
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(free_cpus) == 0 );
182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-3", occupied_cores);
183
184 // Subprocess 1 reclaims CPU 2
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_ReclaimCpu(&spd1, 2) == DLB_NOTED );
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("2,4-7", &sp1_mask);
187
188
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_POLLING) {
189 // Subprocess 2 must return all CPUS in core
190
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
191
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert_expected("0-3", occupied_cores);
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_Return(&spd2) == DLB_SUCCESS );
193 } else {
194 1 wait_for_async_completion();
195 }
196
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( CPU_EQUAL(&sp2_mask, &spd2.process_mask) );
197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-1,3", free_cpus);
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
199
200 // Subprocess 1 can acquire its initial mask
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Reclaim(&spd1) == DLB_SUCCESS );
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert_expected("0-7", &sp1_mask);
203
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(free_cpus) == 0 );
204
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(occupied_cores) == 0 );
205 }
206
207 /* Test asynchronous borrow */
208
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_ASYNC)
209 {
210 // Subprocess 1 asks for the maximum number of CPUs
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_AcquireCpus(&spd1, DLB_MAX_CPUS) == DLB_NOTED );
212
213 // Subprocess 2 lends core [12-15]
214 cpu_set_t cpus_to_lend;
215 1 mu_parse_mask("12-15", &cpus_to_lend);
216 1 mu_subtract(&sp2_mask, &sp2_mask, &cpus_to_lend);
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_LendCpuMask(&spd2, &cpus_to_lend) == DLB_SUCCESS );
218
219 1 wait_for_async_completion();
220
221 // Subprocess 1 should have its mask updated
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert_expected("0-7,12-15", &sp1_mask);
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(free_cpus) == 0 );
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert_expected("12-15", occupied_cores);
225
226 // Subprocess 2 reclaims all
227
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_Reclaim(&spd2) == DLB_NOTED );
228
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert_expected("8-15", &sp2_mask);
229
230 1 wait_for_async_completion();
231
232 // Subprocess 1 should have its initial mask
233
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert_expected("0-7", &sp1_mask);
234
235 // Remove requests
236
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_AcquireCpus(&spd1, DLB_DELETE_REQUESTS) == DLB_SUCCESS );
237 }
238
239 /* Test blocking calls */
240 {
241 // Subprocesses 1 enters into a blocking call
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(&sp1_mask) == 8 );
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_IntoBlockingCall(&spd1) == DLB_SUCCESS );
244
245 // No callback is invoked when entering a blocking call
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(&sp1_mask) == 8 );
247
248 // Subprocess 1 lends all
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_LendCpuMask(&spd1, &sp1_mask) == DLB_SUCCESS );
250 2 CPU_ZERO(&sp1_mask);
251
252 // Subprocess 1 exits the blocking call
253
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_POLLING) {
254
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_OutOfBlockingCall(&spd1) == DLB_SUCCESS );
255
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (mode == MODE_ASYNC) {
256
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_OutOfBlockingCall(&spd1) == DLB_NOTED );
257 1 wait_for_async_completion();
258 }
259
260 // Note: lewi_mask_OutOfBlockingCall() queries the process's actual CPU
261 // affinity mask to determine available CPUs. On systems with less than
262 // 8 CPUs, the affinity mask naturally contains fewer entries, so the
263 // CPU-reclaim check would produce false negatives. Skip the check in
264 // that case and force a Reclaim of the full mask.
265 2 int nproc_onln = sysconf(_SC_NPROCESSORS_ONLN);
266
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (nproc_onln >= 8) {
267 ✗ assert( CPU_COUNT(&sp1_mask) == 8 );
268 } else {
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Reclaim(&spd1) == DLB_SUCCESS );
270 }
271
272 // Subprocess 1 reclaims all
273
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Reclaim(&spd1) == DLB_NOUPDT );
274
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( CPU_COUNT(&sp1_mask) == 8 );
275 }
276
277 // Finalize subprocess 1
278
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Finalize(&spd1) == DLB_SUCCESS );
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_cpuinfo__finalize(spd1.id, spd1.options.shm_key, spd1.options.lewi_color)
280 == DLB_SUCCESS );
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_procinfo__finalize(spd1.id, false, spd1.options.shm_key,
282 spd1.options.shm_size_multiplier) == DLB_SUCCESS );
283
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_ASYNC) {
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_async_finalize(spd1.id) == DLB_SUCCESS );
285 }
286
287 // Finalize subprocess 2
288
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( lewi_mask_Finalize(&spd2) == DLB_SUCCESS );
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_cpuinfo__finalize(spd2.id, spd2.options.shm_key, spd2.options.lewi_color)
290 == DLB_SUCCESS );
291
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_procinfo__finalize(spd2.id, false, spd2.options.shm_key,
292 spd2.options.shm_size_multiplier) == DLB_SUCCESS );
293
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (mode == MODE_ASYNC) {
294
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_async_finalize(spd2.id) == DLB_SUCCESS );
295 }
296
297 2 mu_finalize();
298
299 2 return 0;
300 }
301