GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_drom_02.c
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 201 215 93.5%
Functions: 3 3 100.0%
Branches: 157 264 59.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 <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <assert.h>
34 #include <pthread.h>
35 #include <sys/wait.h>
36
37
38 /* DROM tests of a single process */
39
40 struct data {
41 pthread_barrier_t barrier;
42 int initialized;
43 };
44
45 9 static shmem_handler_t* open_shmem(void **shdata) {
46 18 return shmem_init(shdata,
47 9 &(const shmem_props_t) {
48 .size = sizeof(struct data),
49 .name = "test",
50 .key = SHMEM_KEY,
51 });
52 }
53
54 2 void cb_set_process_mask(const cpu_set_t *mask, void *arg) {
55 2 cpu_set_t *current_mask = arg;
56 2 memcpy(current_mask, mask, sizeof(cpu_set_t));
57 2 }
58
59 1 int main(int argc, char **argv) {
60 /* Options */
61 1 char options[64] = "--shm-key=";
62 1 strcat(options, SHMEM_KEY);
63
64 1 mu_init();
65
66 /* Modify DLB_ARGS to include options */
67 1 dlb_setenv("DLB_ARGS", options, NULL, ENV_APPEND);
68
69 1 pid_t pid = getpid();
70 cpu_set_t process_mask;
71 1 sched_getaffinity(0, sizeof(cpu_set_t), &process_mask);
72
73 1 dlb_drom_flags_t no_flags = DLB_DROM_FLAGS_NONE;
74
75 /* Tests with an empty mask */
76 {
77 /* Save a copy of DLB_ARGS for further tests, since DROM_Attach adds the pre-init flag */
78 1 char *dlb_args_copy = strdup(getenv("DLB_ARGS"));
79
80 /* Preinitialize with an empty mask */
81 cpu_set_t empty_mask;
82 1 CPU_ZERO(&empty_mask);
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Attach() == DLB_SUCCESS );
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_PreInit(pid, &empty_mask, DLB_STEAL_CPUS, NULL) == DLB_SUCCESS );
85
86 /* DLB_Init */
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &empty_mask, NULL) == DLB_SUCCESS );
88
89 /* --drom is automatically set */
90 char value[16];
91
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_GetVariable("--drom", value) == DLB_SUCCESS );
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( strcmp(value, "yes") == 0 );
93
94 /* Process mask is still empty */
95 cpu_set_t mask;
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(pid, &mask, no_flags) == DLB_SUCCESS );
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&mask) == 0 );
98
99 /* Simulate set process mask from an external process */
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(pid, &process_mask, no_flags) == DLB_SUCCESS );
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PollDROM(NULL, &mask) == DLB_NOUPDT ); /* own process, already applied */
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(pid, &mask, no_flags) == DLB_SUCCESS );
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&mask, &process_mask) );
104
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Detach() == DLB_SUCCESS );
107
108 /* Restore DLB_ARGS */
109 1 setenv("DLB_ARGS", dlb_args_copy, 1);
110 1 free(dlb_args_copy);
111 }
112
113 /* Test mask setter and getter from own process */
114 {
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, "--drom") == DLB_SUCCESS );
116
117 /* Get mask */
118 cpu_set_t mask;
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(0, &mask, no_flags) == DLB_SUCCESS );
120
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&mask, &process_mask) );
121
122 /* Set a new mask with 1 less CPU */
123 int i;
124
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 for (i=0; i<CPU_SETSIZE; ++i) {
125
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 if (CPU_ISSET(i, &mask)) {
126
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_CLR(i, &mask);
127 1 break;
128 }
129 }
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(0, &mask, no_flags) == DLB_SUCCESS );
131 cpu_set_t new_mask;
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PollDROM(NULL, &new_mask) == DLB_NOUPDT );
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(0, &new_mask, no_flags) == DLB_SUCCESS );
134
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&mask, &new_mask) );
135
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 assert( CPU_COUNT(&new_mask) + 1 == CPU_COUNT(&process_mask) );
136
137 /* Set again the original mask with DLB_NO_SYNC */
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(0, &process_mask, DLB_NO_SYNC) == DLB_SUCCESS );
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PollDROM(NULL, &new_mask) == DLB_SUCCESS );
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&process_mask, &new_mask) );
141
142
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
143 }
144
145 /* Test setprocessmask with SYNC_NOW */
146 {
147 cpu_set_t current_mask, new_mask1, new_mask2;
148 enum { SYS_SIZE = 4 };
149 1 mu_testing_set_sys_size(SYS_SIZE);
150 1 mu_parse_mask("0-3", &process_mask);
151 1 memcpy(&current_mask, &process_mask, sizeof(cpu_set_t));
152
153 /* Fork process */
154 1 pid = fork();
155
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pid >= 0) {
156 /* Both parent and child execute concurrently */
157 int error;
158 struct data *shdata;
159 2 shmem_handler_t *handler = open_shmem((void**)&shdata);
160 2 shmem_lock(handler);
161 {
162
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!shdata->initialized) {
163 pthread_barrierattr_t attr;
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, 2) == 0 );
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
168 1 shdata->initialized = 1;
169 }
170 }
171 2 shmem_unlock(handler);
172
173 /* Child initializes DLB */
174
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, "--drom") == DLB_SUCCESS );
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackSet(dlb_callback_set_process_mask,
177 (dlb_callback_t)cb_set_process_mask, &current_mask) == DLB_SUCCESS );
178 }
179
180 /* Both processes synchronize */
181 2 error = pthread_barrier_wait(&shdata->barrier);
182
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
183
184 2 mu_parse_mask("0-2", &new_mask1);
185
186 /* Parent process sets mask */
187
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid > 0) {
188 /* Set mask and update with poll drom */
189
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(pid, &new_mask1, no_flags) == DLB_SUCCESS );
190
191 /* A second update is not yet allowed */
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(pid, &new_mask1, no_flags) == DLB_ERR_PDIRTY );
193 }
194
195 /* Both processes synchronize */
196 2 error = pthread_barrier_wait(&shdata->barrier);
197
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
198
199 /* Child process polls */
200
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&process_mask, &current_mask) );
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PollDROM_Update() == DLB_SUCCESS );
203
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( !CPU_EQUAL(&process_mask, &current_mask) );
204
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&new_mask1, &current_mask) );
205 }
206
207 /* Both processes synchronize */
208 2 error = pthread_barrier_wait(&shdata->barrier);
209
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
210
211 2 mu_parse_mask("0-3", &new_mask2);
212
213 /* Parent process sets mask with SYNC_NOW flag */
214
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid > 0) {
215
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(pid, &new_mask2, DLB_SYNC_NOW) == DLB_SUCCESS );
216 }
217
218 /* Child process loops until new mask is set */
219
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
220 int err;
221
2/2
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
2 while ((err = DLB_PollDROM_Update()) == DLB_NOUPDT) { usleep(1000); }
222
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( err == DLB_SUCCESS );
223
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( !CPU_EQUAL(&new_mask1, &current_mask) );
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&new_mask2, &current_mask) );
225 }
226
227 /* Both processes synchronize */
228 2 error = pthread_barrier_wait(&shdata->barrier);
229
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
230
231 /* Finalize shmem */
232 2 shmem_lock(handler);
233 {
234
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (shdata->initialized) {
235
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
236 1 shdata->initialized = 0;
237 }
238 }
239 2 shmem_unlock(handler);
240 2 shmem_finalize(handler, NULL);
241
242 /* Child finalizes DLB and exits */
243
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
245 1 dlb_test__exit(EXIT_SUCCESS);
246 }
247 }
248
249 /* Wait for child process */
250 int wstatus;
251
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
252
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
253 ✗ exit(EXIT_FAILURE);
254 1 int rc = WEXITSTATUS(wstatus);
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
256 ✗ printf("Child return status: %d\n", rc);
257 ✗ exit(EXIT_FAILURE);
258 }
259 }
260 }
261
262 /* Test mask getter and setter without init */
263 {
264 cpu_set_t mask;
265 enum { SYS_SIZE = 4 };
266 1 mu_testing_set_sys_size(SYS_SIZE);
267
268 /* Test that error is NOPROC instead of NOSHMEM */
269
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(0, &mask, no_flags) == DLB_ERR_NOPROC );
270
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(0, &mask, no_flags) == DLB_ERR_NOPROC );
271
272 /* Fork process */
273 1 pid = fork();
274
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (pid >= 0) {
275 /* Both parent and child execute concurrently */
276 int error;
277 struct data *shdata;
278 2 shmem_handler_t *handler = open_shmem((void**)&shdata);
279 2 shmem_lock(handler);
280 {
281
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (!shdata->initialized) {
282 pthread_barrierattr_t attr;
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, 2) == 0 );
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
287 1 shdata->initialized = 1;
288 }
289 }
290 2 shmem_unlock(handler);
291
292 /* Child initializes DLB */
293
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
294 1 mu_parse_mask("0-3", &process_mask);
295
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, "--drom") == DLB_SUCCESS );
296 }
297
298 /* Both processes synchronize */
299 2 error = pthread_barrier_wait(&shdata->barrier);
300
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
301
302 /* Parent process gets the mask without DROM_Attach */
303
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid != 0) {
304 cpu_set_t child_mask, expected_child_mask;
305 1 mu_parse_mask("0-3", &expected_child_mask);
306
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(pid, &child_mask, no_flags) == DLB_SUCCESS );
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&child_mask, &expected_child_mask) );
308 }
309
310 /* Parent process sets a new mask without DROM_Attach */
311
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid != 0) {
312 cpu_set_t new_mask;
313 1 mu_parse_mask("0-2", &new_mask);
314
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_SetProcessMask(pid, &new_mask, no_flags) == DLB_SUCCESS );
315 }
316
317 /* Both processes synchronize */
318 2 error = pthread_barrier_wait(&shdata->barrier);
319
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
320
321 /* Child process updates new mask */
322
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
323 cpu_set_t new_mask, expected_new_mask;
324 1 mu_parse_mask("0-2", &expected_new_mask);
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_GetProcessMask(0, &new_mask, no_flags) == DLB_NOTED );
326
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( CPU_EQUAL(&new_mask, &expected_new_mask) );
327 }
328
329 /* Finalize shmem */
330 2 shmem_lock(handler);
331 {
332
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (shdata->initialized) {
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
334 1 shdata->initialized = 0;
335 }
336 }
337 2 shmem_unlock(handler);
338 2 shmem_finalize(handler, NULL);
339
340 /* Child finalizes DLB and exits */
341
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
342
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
343 1 dlb_test__exit(EXIT_SUCCESS);
344 }
345 }
346
347 /* Wait for child process */
348 int wstatus;
349
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
350
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
351 ✗ exit(EXIT_FAILURE);
352 1 int rc = WEXITSTATUS(wstatus);
353
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
354 ✗ printf("Child return status: %d\n", rc);
355 ✗ exit(EXIT_FAILURE);
356 }
357 }
358 }
359
360 /* Test child processes inheriting parent preinit */
361 {
362 cpu_set_t mask;
363 enum { SYS_SIZE = 4 };
364 1 mu_testing_set_sys_size(SYS_SIZE);
365
366 /* Parent process preinitializes */
367 1 mu_parse_mask("0-3", &mask);
368
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Attach() == DLB_SUCCESS );
369
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_PreInit(pid, &mask, no_flags, NULL) == DLB_SUCCESS );
370
371 /* Fork processes */
372 pid_t child_pid[SYS_SIZE];
373 int i;
374
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for(i=0; i<SYS_SIZE; ++i) {
375 4 child_pid[i] = fork();
376
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
8 if (child_pid[i] == 0) {
377 /* Child initializes test shmem */
378 int error;
379 struct data *shdata;
380 4 shmem_handler_t *handler = open_shmem((void**)&shdata);
381 4 shmem_lock(handler);
382 {
383
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (!shdata->initialized) {
384 pthread_barrierattr_t attr;
385
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
387
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, SYS_SIZE+1) == 0 );
388
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
389 1 shdata->initialized = 1;
390 }
391 }
392 4 shmem_unlock(handler);
393
394 /* Initialize DLB */
395 4 CPU_ZERO(&process_mask);
396
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 CPU_SET(i, &process_mask);
397 /* assert( DLB_Init(0, &process_mask, "--drom") == DLB_NOTED ); */
398 4 int err = DLB_Init(0, &process_mask, "--drom");
399 4 fprintf(stderr, "DLB_Init err: %d\n", err);
400
401 /* Synchronize: all children are initialized */
402 4 error = pthread_barrier_wait(&shdata->barrier);
403
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);
404
405 /* Parent process checks everything is correct */
406
407 /* Synchronize: all children will finalize */
408 4 error = pthread_barrier_wait(&shdata->barrier);
409
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);
410
411 /* Finalize shmem */
412 4 shmem_finalize(handler, NULL);
413
414 /* Finalizes DLB and exit */
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( DLB_Finalize() == DLB_SUCCESS );
416 4 dlb_test__exit(EXIT_SUCCESS);
417 }
418 }
419
420 /* Parent process initializes test shmem */
421 int error;
422 struct data *shdata;
423 1 shmem_handler_t *handler = open_shmem((void**)&shdata);
424 1 shmem_lock(handler);
425 {
426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!shdata->initialized) {
427 pthread_barrierattr_t attr;
428 ✗ assert( pthread_barrierattr_init(&attr) == 0 );
429 ✗ assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
430 ✗ assert( pthread_barrier_init(&shdata->barrier, &attr, SYS_SIZE+1) == 0 );
431 ✗ assert( pthread_barrierattr_destroy(&attr) == 0 );
432 ✗ shdata->initialized = 1;
433 }
434 }
435 1 shmem_unlock(handler);
436
437 /* Synchronize: all children are initialized */
438 1 error = pthread_barrier_wait(&shdata->barrier);
439
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
440
441 /* Parent process checks everything is correct */
442
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for(i=0; i<SYS_SIZE; ++i) {
443 cpu_set_t child_mask, expected_child_mask;
444 4 CPU_ZERO(&expected_child_mask);
445
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 CPU_SET(i, &expected_child_mask);
446
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( DLB_DROM_GetProcessMask(child_pid[i], &child_mask, no_flags)
447 == DLB_SUCCESS );
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert( CPU_EQUAL(&child_mask, &expected_child_mask) );
449 }
450
451 /* Synchronize: all children will finalize */
452 1 error = pthread_barrier_wait(&shdata->barrier);
453
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
454
455 /* Finalize shmem */
456 1 shmem_lock(handler);
457 {
458
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (shdata->initialized) {
459
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
460 1 shdata->initialized = 0;
461 }
462 }
463 1 shmem_unlock(handler);
464 1 shmem_finalize(handler, NULL);
465
466 /* Wait for all child processes */
467 int wstatus;
468
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 1 times.
5 while(wait(&wstatus) > 0) {
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!WIFEXITED(wstatus))
470 ✗ exit(EXIT_FAILURE);
471 4 int rc = WEXITSTATUS(wstatus);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (rc != 0) {
473 ✗ printf("Child return status: %d\n", rc);
474 ✗ exit(EXIT_FAILURE);
475 }
476 }
477
478
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_DROM_Detach() == DLB_SUCCESS );
479 }
480
481 1 mu_finalize();
482
483 1 return 0;
484 }
485