GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/barrier_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 144 157 91.7%
Functions: 2 2 100.0%
Branches: 70 130 53.8%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2022 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 "extra_tests.h"
21 #include "unique_shmem.h"
22 #include "test_process.h"
23
24 #include "LB_comm/shmem_barrier.h"
25 #include "LB_comm/shmem.h"
26 #include "LB_core/spd.h"
27 #include "support/mask_utils.h"
28 #include "support/options.h"
29 #include "support/debug.h"
30 #include "apis/dlb_errors.h"
31
32 #include <assert.h>
33 #include <pthread.h>
34 #include <stdio.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <unistd.h>
38
39
40 /* Test node barrier */
41
42 struct data {
43 pthread_barrier_t barrier;
44 };
45
46 2 void* barrier_fn(void *arg) {
47 2 barrier_t *barrier = arg;
48 2 shmem_barrier__barrier(barrier);
49 2 return NULL;
50 }
51
52 1 int main(int argc, char **argv) {
53
54 enum { SHMEM_SIZE_MULTIPLIER = 1 };
55
56 1 const char *barrier_name = "barrier";
57 1 bool lewi = false;
58
59 /* Test barrier with one process */
60 {
61 options_t options;
62 1 options_init(&options, NULL);
63 1 debug_init(&options);
64 1 spd_enter_dlb(NULL);
65 1 thread_spd->id = getpid();
66
67 1 printf("Testing barrier with one process\n");
68 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__register(NULL, lewi) == NULL );
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__find(NULL) == NULL );
71 1 barrier_t *barrier = shmem_barrier__register(barrier_name, lewi);
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier != NULL );
73 1 shmem_barrier__barrier(barrier);
74 1 shmem_barrier__print_info(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == 2 );
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == 3 );
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == 2 );
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == 1 );
79 1 shmem_barrier__barrier(barrier);
80
81 /* Perform a barrier with two threads of the same process*/
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == 2 );
83 pthread_t thread1, thread2;
84 1 pthread_create(&thread1, NULL, barrier_fn, barrier);
85 1 pthread_create(&thread2, NULL, barrier_fn, barrier);
86 1 pthread_join(thread1, NULL);
87 1 pthread_join(thread2, NULL);
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == 1 );
89
90 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
91 }
92
93 /* Test that multiple initialize or finalize does not cause errors */
94 {
95 options_t options;
96 1 options_init(&options, NULL);
97 1 debug_init(&options);
98 1 spd_enter_dlb(NULL);
99 1 thread_spd->id = getpid();
100
101 1 printf("Testing multiple init/finalize\n");
102 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
103 1 barrier_t *barrier = shmem_barrier__register(barrier_name, lewi);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier != NULL );
105 1 barrier_t *barrier_copy = shmem_barrier__find(barrier_name);
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier == barrier_copy);
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == 2 );
108 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == 3 );
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__exists() );
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == 2 );
112 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( !shmem_barrier__exists() );
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == DLB_ERR_NOSHMEM );
115 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
116 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
117 1 barrier = shmem_barrier__register(barrier_name, lewi);
118
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier != NULL );
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__exists() );
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == 0 );
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == DLB_ERR_PERM );
122
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__attach(barrier) == DLB_ERR_PERM );
123 1 shmem_barrier__barrier(barrier); /* no effect */
124
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__find(NULL) == NULL );
125 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) == DLB_ERR_NOSHMEM );
127 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(NULL) == DLB_ERR_UNKNOWN );
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( !shmem_barrier__exists() );
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__find(NULL) == NULL );
131 }
132
133 /* Test barrier with N processes */
134
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (DLB_EXTRA_TESTS)
135 {
136 1 printf("Testing barrier with N processes\n");
137 int ncpus;
138 struct data *shdata;
139 shmem_handler_t *handler;
140
141 // Master process creates shmem and initializes barrier
142 1 ncpus = mu_get_system_size();
143 1 handler = shmem_init((void**)&shdata,
144 1 &(const shmem_props_t) {
145 .size = sizeof(struct data),
146 .name = "test",
147 .key = SHMEM_KEY,
148 });
149 pthread_barrierattr_t attr;
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, ncpus-1) == 0 );
153
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
154
155 // Create a child process per CPU-1
156 int child;
157
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for(child=1; child<ncpus; ++child) {
158 1 pid_t pid = fork();
159
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( pid >= 0 );
160
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
161 // All children initialize DLB
162 options_t options;
163 1 options_init(&options, NULL);
164 1 debug_init(&options);
165 1 spd_enter_dlb(NULL);
166 1 thread_spd->id = getpid();
167 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
168 1 barrier_t *barrier = shmem_barrier__register(barrier_name, lewi);
169
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier != NULL );
170
171 // Attach to the "test" shared memory and synchronize
172 1 handler = shmem_init((void**)&shdata,
173 1 &(const shmem_props_t) {
174 .size = sizeof(struct data),
175 .name = "test",
176 .key = SHMEM_KEY,
177 });
178 1 int error = pthread_barrier_wait(&shdata->barrier);
179
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
180
181 // All processes do multiple DLB_Barriers once they have initialized
182 1 printf("Child %d successfully initialized\n", child);
183 1 shmem_barrier__barrier(barrier);
184 1 shmem_barrier__barrier(barrier);
185 1 shmem_barrier__barrier(barrier);
186 1 printf("Child %d completed all barriers\n", child);
187
188 // Only one process prints shmem info
189
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (child == 1) {
190 1 shmem_barrier__print_info(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
191 }
192
193 // Finalize both shared memories
194
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) >= 0 );
195 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
196 1 shmem_finalize(handler, NULL);
197
198 // We need to call _exit so that children don't call assert_shmem destructors
199 1 dlb_test__exit(EXIT_SUCCESS);
200 ✗ break;
201 }
202 }
203
204 // Wait for all child processes
205 int wstatus;
206
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
208 ✗ exit(EXIT_FAILURE);
209 1 int rc = WEXITSTATUS(wstatus);
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
211 ✗ printf("Child return status: %d\n", rc);
212 ✗ exit(EXIT_FAILURE);
213 }
214 }
215
216 // Master process finalizes everything
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
218 1 shmem_finalize(handler, NULL);
219 }
220
221 /* Test barrier with N processes where 1 of them detaches from the barrier */
222
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (DLB_EXTRA_TESTS)
223 {
224 int ncpus;
225 struct data *shdata;
226 shmem_handler_t *handler;
227
228 // Master process creates shmem and initializes barrier
229 1 ncpus = mu_get_system_size();
230 1 handler = shmem_init((void**)&shdata,
231 1 &(const shmem_props_t) {
232 .size = sizeof(struct data),
233 .name = "test",
234 .key = SHMEM_KEY,
235 });
236 pthread_barrierattr_t attr;
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, ncpus-1) == 0 );
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
241
242 // Create a child process per CPU-1
243 int child;
244
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for(child=1; child<ncpus; ++child) {
245 1 pid_t pid = fork();
246
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( pid >= 0 );
247
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
248 // All children initialize DLB
249 options_t options;
250 1 options_init(&options, NULL);
251 1 debug_init(&options);
252 1 spd_enter_dlb(NULL);
253 1 thread_spd->id = getpid();
254 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
255 1 barrier_t *barrier = shmem_barrier__register(barrier_name, lewi);
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barrier != NULL );
257
258 // Child 1 does DLB_BarrierDetach
259
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (child == 1) {
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__detach(barrier) >= 0 );
261 }
262
263 // Attach to the "test" shared memory and synchronize
264 1 handler = shmem_init((void**)&shdata,
265 1 &(const shmem_props_t) {
266 .size = sizeof(struct data),
267 .name = "test",
268 .key = SHMEM_KEY,
269 });
270 1 int error = pthread_barrier_wait(&shdata->barrier);
271
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
272
273
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (child != 1) {
274 // All other processes do multiple DLB_Barriers once they have initialized
275 ✗ shmem_barrier__barrier(barrier);
276 ✗ shmem_barrier__barrier(barrier);
277 ✗ shmem_barrier__barrier(barrier);
278 }
279
280 // Only one process prints shmem info
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (child == 2) {
282 ✗ shmem_barrier__print_info(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
283 }
284
285 // Finalize both shared memories
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (child != 1) {
287 ✗ assert( shmem_barrier__detach(barrier) >= 0 );
288 }
289 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
290 1 shmem_finalize(handler, NULL);
291
292 // We need to call _exit so that children don't call assert_shmem destructors
293 1 dlb_test__exit(EXIT_SUCCESS);
294 ✗ break;
295 }
296 }
297
298 // Wait for all child processes
299 int wstatus;
300
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
2 while(wait(&wstatus) > 0) {
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!WIFEXITED(wstatus))
302 ✗ exit(EXIT_FAILURE);
303 1 int rc = WEXITSTATUS(wstatus);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (rc != 0) {
305 ✗ printf("Child return status: %d\n", rc);
306 ✗ exit(EXIT_FAILURE);
307 }
308 }
309
310 // Master process finalizes everything
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
312 1 shmem_finalize(handler, NULL);
313 }
314
315 1 return EXIT_SUCCESS;
316 }
317