GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_01.c
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 149 161 92.5%
Functions: 1 1 100.0%
Branches: 158 212 74.5%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2021 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 #include "test_process.h"
22
23 #include "apis/dlb.h"
24 #include "apis/dlb_drom.h"
25 #include "support/env.h"
26 #include "support/mask_utils.h"
27 #include "LB_comm/shmem.h"
28
29 #include <sched.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <assert.h>
34 #include <pthread.h>
35 #include <sys/wait.h>
36
37 /* Test DLB_PreInit */
38
39 struct data {
40 pthread_barrier_t barrier;
41 int initialized;
42 };
43
44 1 int main(int argc, char **argv) {
45 /* Options */
46 1 char options[64] = "--shm-key=";
47 1 strcat(options, SHMEM_KEY);
48
49 /* Modify DLB_ARGS to include options */
50 1 dlb_setenv("DLB_ARGS", options, NULL, ENV_APPEND);
51
52 /* Single process */
53 {
54 /* PreInit with full process mask */
55 cpu_set_t process_mask;
56 1 sched_getaffinity(0, sizeof(cpu_set_t), &process_mask);
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PreInit(&process_mask, NULL) == DLB_SUCCESS );
58
59 /* Init after binding master thread */
60 cpu_set_t current_mask;
61 1 CPU_ZERO(&current_mask);
62
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 CPU_SET(sched_getcpu(), &current_mask);
63 1 sched_setaffinity(0, sizeof(cpu_set_t), &current_mask);
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &current_mask, NULL) == DLB_SUCCESS );
65
66 /* A second Init should return error */
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &current_mask, NULL) == DLB_ERR_INIT );
68
69 /* Check current mask */
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Attach() == DLB_SUCCESS );
71
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(getpid(), &process_mask, DLB_DROM_FLAGS_NONE)
72 == DLB_SUCCESS );
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Detach() == DLB_SUCCESS );
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&process_mask, &current_mask) );
75
76 /* Finalize */
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
78
79 /* A second invocation may be needed to clean up CPUs not being inherited */
80
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
81 }
82
83 /* Test two processes preregistering and two inheriting */
84 {
85 enum { SYS_SIZE = 4 };
86 1 mu_init();
87 1 mu_testing_set_sys_size(SYS_SIZE);
88
89 /* This process forks to have two parent processes simulating a dlb_run */
90 1 pid_t parent2_pid = fork();
91 2 bool parent1 = parent2_pid > 0;
92 2 bool parent2 = parent2_pid == 0;
93
94 /* parent1 preinializes [0-1], parent2 preinializes [2-3] */
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( mu_get_system_size() == 4 );
96 cpu_set_t process_mask;
97
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (parent1) {
98 1 mu_parse_mask("0-1", &process_mask);
99 }
100
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (parent2) {
101 1 mu_parse_mask("2-3", &process_mask);
102 }
103
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_PreInit(&process_mask, NULL) == DLB_SUCCESS );
104
105 /* Both parents fork again into child1 and child2 */
106 2 pid_t child_pid = fork();
107
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 bool child1 = parent1 && child_pid == 0;
108
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 bool child2 = parent2 && child_pid == 0;
109
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 parent1 = parent1 && !child1;
110
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 parent2 = parent2 && !child2;
111
112 /* All processes attach to a helper shared memory and synchronize */
113 int error;
114 struct data *shdata;
115 4 shmem_handler_t *handler = shmem_init((void**)&shdata,
116 4 &(const shmem_props_t) {
117 .size = sizeof(struct data),
118 .name = "test",
119 .key = SHMEM_KEY,
120 });
121 4 shmem_lock(handler);
122 {
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (!shdata->initialized) {
124 pthread_barrierattr_t attr;
125
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, 4) == 0 );
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
129 1 shdata->initialized = 1;
130 }
131 }
132 4 shmem_unlock(handler);
133 4 error = pthread_barrier_wait(&shdata->barrier);
134
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
135
136 /* Both children initialize */
137
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (child1 || child2) {
138 2 CPU_ZERO(&process_mask);
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_Init(0, &process_mask, NULL) == DLB_SUCCESS );
140 }
141 4 error = pthread_barrier_wait(&shdata->barrier);
142
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
143
144 /* Both children finalize */
145
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (child1 || child2) {
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_Finalize() == DLB_SUCCESS );
147 }
148 4 error = pthread_barrier_wait(&shdata->barrier);
149
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
150
151 /* Both parents finalize */
152
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (parent1 || parent2) {
153
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_Finalize() == DLB_SUCCESS );
154 }
155 4 error = pthread_barrier_wait(&shdata->barrier);
156
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
157
158 /* Both children finalize shared memory and exit */
159
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (child1 || child2) {
160 2 shmem_finalize(handler, NULL);
161 2 dlb_test__exit(EXIT_SUCCESS);
162 }
163
164 /* parent2 waits for its child, then finalizes shared memory and exits */
165
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (parent2) {
166 int wstatus;
167
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
168
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
169 ✗ exit(EXIT_FAILURE);
170 1 int rc = WEXITSTATUS(wstatus);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
172 ✗ printf("Child return status: %d\n", rc);
173 ✗ exit(EXIT_FAILURE);
174 }
175 }
176 1 shmem_finalize(handler, NULL);
177 1 dlb_test__exit(EXIT_SUCCESS);
178 }
179
180 /* Wait for all child processes */
181 int wstatus;
182
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while(wait(&wstatus) > 0) {
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!WIFEXITED(wstatus))
184 ✗ exit(EXIT_FAILURE);
185 2 int rc = WEXITSTATUS(wstatus);
186
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (rc != 0) {
187 ✗ printf("Child return status: %d\n", rc);
188 ✗ exit(EXIT_FAILURE);
189 }
190 }
191
192 /* Finalize helper shared memory */
193 1 shmem_lock(handler);
194 {
195
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (shdata->initialized) {
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
197 1 shdata->initialized = 0;
198 }
199 }
200 1 shmem_unlock(handler);
201 1 shmem_finalize(handler, NULL);
202 }
203
204 /* Test two processes preregistering and two inheriting (finalization out of order) */
205 {
206 enum { SYS_SIZE = 4 };
207 1 mu_init();
208 1 mu_testing_set_sys_size(SYS_SIZE);
209
210 /* This process forks to have two parent processes simulating a dlb_run */
211 1 pid_t parent2_pid = fork();
212 2 bool parent1 = parent2_pid > 0;
213 2 bool parent2 = parent2_pid == 0;
214
215 /* parent1 preinializes [0-1], parent2 preinializes [2-3] */
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( mu_get_system_size() == 4 );
217 cpu_set_t process_mask;
218
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (parent1) {
219 1 mu_parse_mask("0-1", &process_mask);
220 }
221
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 else if (parent2) {
222 1 mu_parse_mask("2-3", &process_mask);
223 }
224
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_PreInit(&process_mask, NULL) == DLB_SUCCESS );
225
226 /* Both parents fork again into child1 and child2 */
227 2 pid_t child_pid = fork();
228
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 bool child1 = parent1 && child_pid == 0;
229
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 bool child2 = parent2 && child_pid == 0;
230
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 parent1 = parent1 && !child1;
231
4/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
4 parent2 = parent2 && !child2;
232
233 /* All processes attach to a helper shared memory and synchronize */
234 int error;
235 struct data *shdata;
236 4 shmem_handler_t *handler = shmem_init((void**)&shdata,
237 4 &(const shmem_props_t) {
238 .size = sizeof(struct data),
239 .name = "test",
240 .key = SHMEM_KEY,
241 });
242 4 shmem_lock(handler);
243 {
244
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (!shdata->initialized) {
245 pthread_barrierattr_t attr;
246
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
247
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
248
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, 4) == 0 );
249
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
250 1 shdata->initialized = 1;
251 }
252 }
253 4 shmem_unlock(handler);
254 4 error = pthread_barrier_wait(&shdata->barrier);
255
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
256
257 /* Both children initialize */
258
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (child1 || child2) {
259 2 CPU_ZERO(&process_mask);
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( DLB_Init(0, &process_mask, NULL) == DLB_SUCCESS );
261 }
262 4 error = pthread_barrier_wait(&shdata->barrier);
263
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
264
265 /* Only child2 and parent2 finalize */
266
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (child2) {
267
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
268 }
269 4 error = pthread_barrier_wait(&shdata->barrier);
270
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
271
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (parent2) {
272
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
273 }
274 4 error = pthread_barrier_wait(&shdata->barrier);
275
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
276
277 /* child1 and parent1 finalize */
278
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (child1) {
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
280 }
281 4 error = pthread_barrier_wait(&shdata->barrier);
282
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
283
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (parent1) {
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
285 }
286 4 error = pthread_barrier_wait(&shdata->barrier);
287
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
4 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
288
289 /* Both children finalize shared memory and exit */
290
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 2 times.
4 if (child1 || child2) {
291 2 shmem_finalize(handler, NULL);
292 2 dlb_test__exit(EXIT_SUCCESS);
293 }
294
295 /* parent2 waits for its child, then finalizes shared memory and exits */
296
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (parent2) {
297 int wstatus;
298
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
300 ✗ exit(EXIT_FAILURE);
301 1 int rc = WEXITSTATUS(wstatus);
302
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
303 ✗ printf("Child return status: %d\n", rc);
304 ✗ exit(EXIT_FAILURE);
305 }
306 }
307 1 shmem_finalize(handler, NULL);
308 1 dlb_test__exit(EXIT_SUCCESS);
309 }
310
311 /* Wait for all child processes */
312 int wstatus;
313
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 1 times.
3 while(wait(&wstatus) > 0) {
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!WIFEXITED(wstatus))
315 ✗ exit(EXIT_FAILURE);
316 2 int rc = WEXITSTATUS(wstatus);
317
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (rc != 0) {
318 ✗ printf("Child return status: %d\n", rc);
319 ✗ exit(EXIT_FAILURE);
320 }
321 }
322
323 /* Finalize helper shared memory */
324 1 shmem_lock(handler);
325 {
326
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (shdata->initialized) {
327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
328 1 shdata->initialized = 0;
329 }
330 }
331 1 shmem_unlock(handler);
332 1 shmem_finalize(handler, NULL);
333 }
334
335 1 mu_finalize();
336
337 1 return 0;
338 }
339