GCC Code Coverage Report


Directory: ./
File: tests/unit/04_core/node_barrier_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 80 91 87.9%
Functions: 2 2 100.0%
Branches: 40 92 43.5%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2023 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_core/node_barrier.h"
23 #include "LB_core/spd.h"
24 #include "LB_comm/shmem_barrier.h"
25 #include "apis/dlb_errors.h"
26 #include "support/atomic.h"
27 #include "support/options.h"
28
29 #include <assert.h>
30 #include <pthread.h>
31 #include <stdint.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35
36 /* Test node_barrier module */
37
38 struct pthread_args {
39 subprocess_descriptor_t *spd;
40 barrier_t *barrier;
41 };
42
43 2 void* barrier_fn(void *arg) {
44 2 struct pthread_args *pargs = arg;
45
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( node_barrier(pargs->spd, pargs->barrier) == DLB_SUCCESS );
46 2 return NULL;
47 }
48
49 1 int main(int argc, char *argv[]) {
50
51 1 char options[64] = "--barrier --shm-key=";
52 1 strcat(options, SHMEM_KEY);
53
54 enum { SHMEM_SIZE_MULTIPLIER = 1 };
55
56 1 subprocess_descriptor_t spd1 = {.id = 111};
57 1 options_init(&spd1.options, options);
58 1 subprocess_descriptor_t spd2 = {.id = 222};
59 1 options_init(&spd2.options, options);
60 1 shmem_barrier__init(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
61 1 bool lewi = false;
62
63 /* Register barriers */
64 {
65 /* Initialize module and default barrier */
66 1 node_barrier_init(&spd1);
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, NULL) == DLB_SUCCESS );
68
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_attach(&spd1, NULL) == DLB_ERR_PERM );
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 1 );
70
71 enum { BARRIER_MAX_LEN = 32 };
72 char barrier_name[BARRIER_MAX_LEN];
73 1 int max_barriers = shmem_barrier__get_max_barriers();
74 1 int max_named_barriers = max_barriers - 1;
75
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( max_barriers > 0 );
76
77
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (max_named_barriers > 0) {
78 1 barrier_t **barriers = malloc(sizeof(barrier_t*) * max_named_barriers);
79
80 /* register barrier */
81 1 sprintf(barrier_name, "Barrier 0");
82 1 barriers[0] = node_barrier_register(&spd1, barrier_name, lewi);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barriers[0] != NULL );
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 2 );
85 1 barrier_t *barrier_copy = node_barrier_register(&spd1, barrier_name, lewi);
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barriers[0] == barrier_copy );
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 2 );
88 // make sure two registers don't increase participants
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, barriers[0]) == DLB_SUCCESS );
90
91 /* attach and detach */
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_attach(&spd1, barriers[0]) == DLB_ERR_PERM );
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_attach(&spd1, barriers[0]) == DLB_ERR_PERM );
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd1, barriers[0]) == 0 );
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd1, barriers[0]) == DLB_ERR_PERM );
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 1 );
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, barriers[0]) == DLB_NOUPDT );
98
99
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (int i = 0; i < max_named_barriers; ++i) {
100 1 snprintf(barrier_name, BARRIER_MAX_LEN, "Barrier %d", i);
101 1 barriers[i] = node_barrier_register(&spd1, barrier_name, lewi);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barriers[i] != NULL );
103 1 barrier_t *named_barrier_copy = node_barrier_register(&spd1, barrier_name, lewi);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( barriers[i] == named_barrier_copy );
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, barriers[i]) == DLB_SUCCESS );
106 }
107 }
108
109 1 node_barrier_finalize(&spd1);
110
111 /* Make sure node_barrier_finalize removes all barriers from shared memory */
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__find(NULL) == NULL );
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__find("default") == NULL );
114
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (int i = 0; i < max_barriers; ++i) {
115 2 snprintf(barrier_name, BARRIER_MAX_LEN, "Barrier %d", i);
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 assert( shmem_barrier__find(barrier_name) == NULL );
117 }
118 }
119
120 /* Multiple spds */
121 {
122 1 node_barrier_init(&spd1);
123 1 node_barrier_init(&spd2);
124
125 1 int max_barriers = shmem_barrier__get_max_barriers();
126 1 barrier_t *barrier_spd1 = NULL;
127 1 barrier_t *barrier_spd2 = NULL;
128
129
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (max_barriers >= 2) {
130 /* Two subprocesses register a named barrier */
131 1 const char *test_barrier_name = "Test barrier";
132 1 barrier_spd1 = node_barrier_register(&spd1, test_barrier_name, lewi);
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 2 );
134 1 barrier_spd2 = node_barrier_register(&spd2, test_barrier_name, lewi);
135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_barrier__get_num_barriers() == 2 );
136
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd1, barrier_spd1) == 1 );
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_attach(&spd1, barrier_spd1) == 2 );
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd2, barrier_spd2) == 1 );
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_attach(&spd2, barrier_spd2) == 2 );
140
141 /* Perform a barrier with two threads of the same process*/
142 pthread_t thread1, thread2;
143 1 struct pthread_args pargs1 = {.spd = &spd1, .barrier = barrier_spd1 };
144 1 pthread_create(&thread1, NULL, barrier_fn, &pargs1);
145 1 struct pthread_args pargs2 = {.spd = &spd2, .barrier = barrier_spd2 };
146 1 pthread_create(&thread2, NULL, barrier_fn, &pargs2);
147 1 pthread_join(thread1, NULL);
148 1 pthread_join(thread2, NULL);
149 }
150
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (max_barriers >= 3) {
152 /* Another named barrier */
153 ✗ const char *test_barrier2_name = "Test barrier 2";
154 ✗ barrier_t *barrier2_spd1 = node_barrier_register(&spd1, test_barrier2_name, lewi);
155 ✗ assert( barrier2_spd1 != NULL );
156 ✗ assert( shmem_barrier__get_num_barriers() == 3 );
157 ✗ barrier_t *barrier2_spd2 = node_barrier_register(&spd2, test_barrier2_name, lewi);
158 ✗ assert( barrier2_spd2 != NULL );
159 ✗ assert( shmem_barrier__get_num_barriers() == 3 );
160 ✗ assert( node_barrier_detach(&spd1, barrier2_spd1) == 1 );
161 ✗ assert( node_barrier_attach(&spd1, barrier2_spd1) == 2 );
162 ✗ assert( node_barrier_detach(&spd1, barrier2_spd1) == 1 );
163 ✗ assert( node_barrier_detach(&spd2, barrier2_spd2) == 0 );
164 }
165
166 /* node_barrier on detached barriers */
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd1, NULL) == 1 );
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, NULL) == DLB_NOUPDT );
169
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (max_barriers >= 2) {
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier_detach(&spd1, barrier_spd1) == 1 );
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( node_barrier(&spd1, barrier_spd1) == DLB_NOUPDT );
172 }
173
174 1 node_barrier_finalize(&spd1);
175 1 node_barrier_finalize(&spd2);
176 }
177
178 1 shmem_barrier__finalize(SHMEM_KEY, SHMEM_SIZE_MULTIPLIER);
179
180 1 return 0;
181 }
182