GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/shmem_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 78 79 98.7%
Functions: 1 1 100.0%
Branches: 32 50 64.0%

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 "extra_tests.h"
21 #include "unique_shmem.h"
22 #include "test_process.h"
23
24 #include "LB_comm/shmem.h"
25 #include "support/atomic.h"
26 #include "support/mask_utils.h"
27 #include "support/mytime.h"
28
29 #include <assert.h>
30 #include <unistd.h>
31 #include <pthread.h>
32 #include <sys/wait.h>
33
34 /* Fill node with processes, children will join the shared memory and will test the lock mechanism */
35
36 struct data {
37 pthread_barrier_t barrier;
38 int foo;
39 };
40
41 1 int main(int argc, char **argv) {
42
43 1 mu_init();
44 1 int system_size = mu_get_system_size();
45
46 /* Ensure the system size for this test is exactly 4 if necessary:
47 * - If the actual system size is smaller than 4, fake it by setting it to 4.
48 * - If the actual system size is larger than 4 and EXTRA_TESTS are disabled,
49 * reduce it to 4 to avoid issues with too many forks on large systems.
50 */
51 enum { MINIMUM_SYS_SIZE = 4 };
52
1/6
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
1 if (system_size < MINIMUM_SYS_SIZE || (!DLB_EXTRA_TESTS && system_size > MINIMUM_SYS_SIZE)) {
53 1 system_size = MINIMUM_SYS_SIZE;
54 1 mu_testing_set_sys_size(MINIMUM_SYS_SIZE);
55 }
56
57 struct data *shdata;
58 shmem_handler_t *handler;
59
60 // Master process creates shmem and initializes barrier
61 1 handler = shmem_init((void**)&shdata,
62 1 &(const shmem_props_t) {
63 .size = sizeof(struct data),
64 .name = "test",
65 .key = SHMEM_KEY,
66 });
67
68 // check shdata is aligned to DLB_CACHE_LINE
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( (intptr_t)shdata % DLB_CACHE_LINE == 0 );
70
71 1 fprintf(stdout, "shmem name: %s\n", get_shm_filename(handler));
72 pthread_barrierattr_t attr;
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_init(&attr) == 0 );
74
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_init(&shdata->barrier, &attr, system_size) == 0 );
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrierattr_destroy(&attr) == 0 );
77 1 shdata->foo = system_size;
78
79 // Create a child process per CPU-1
80
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for(int child = 1; child < system_size; ++child) {
81 3 pid_t pid = fork();
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 assert( pid >= 0 );
83
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (pid == 0) {
84 // Each child will attach to the shared memory
85 3 handler = shmem_init((void**)&shdata,
86 3 &(const shmem_props_t) {
87 .size = sizeof(struct data),
88 .name = "test",
89 .key = SHMEM_KEY,
90 });
91
92 // check shdata is aligned to DLB_CACHE_LINE
93
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 assert( (intptr_t)shdata % DLB_CACHE_LINE == 0 );
94
95 3 pthread_barrier_wait(&shdata->barrier); // Barrier 1
96
97 // Everyone performs a locked decrement
98 3 shmem_lock(handler);
99 3 shdata->foo--;
100 3 shmem_unlock(handler);
101 3 pthread_barrier_wait(&shdata->barrier); // Barrier 2
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 assert(shdata->foo == 1);
103 3 pthread_barrier_wait(&shdata->barrier); // Barrier 3
104
105 // Everyone performs a locked increment (using lock maintenance)
106 3 shmem_lock_maintenance(handler);
107 3 shdata->foo++;
108 3 shmem_unlock_maintenance(handler);
109 3 pthread_barrier_wait(&shdata->barrier); // Barrier 4
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 assert(shdata->foo == system_size);
111 3 pthread_barrier_wait(&shdata->barrier); // Barrier 5
112
113 // Test that shmem cannot be put in BUSY while in MAINTENANCE
114
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (child == 1) {
115 // PRE-B6: Child 1 sets the maintenance mode
116 1 shmem_lock_maintenance(handler);
117 1 pthread_barrier_wait(&shdata->barrier); // Barrier 6
118 // PRE-B7: Child 1 sets some value
119 1 shdata->foo = 42;
120 // PRE-B7: Child 1 unsets the maintenance mode
121 1 shmem_unlock_maintenance(handler);
122 1 pthread_barrier_wait(&shdata->barrier); // Barrier 7
123
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (child == 2) {
124 1 pthread_barrier_wait(&shdata->barrier); // Barrier 6
125 // PRE-B7: Child 2 will read the value in BUSY mode
126 1 shmem_acquire_busy(handler);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(shdata->foo == 42);
128 1 shmem_release_busy(handler);
129 1 pthread_barrier_wait(&shdata->barrier); // Barrier 7
130 } else {
131 1 pthread_barrier_wait(&shdata->barrier); // Barrier 6
132 1 pthread_barrier_wait(&shdata->barrier); // Barrier 7
133 }
134
135 // Test that shmem cannot be put in MAINTENANCE while in BUSY
136
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (child == 1) {
137 1 pthread_barrier_wait(&shdata->barrier); // Barrier 8
138 // PRE-B9: Child 1 wants to acquire in maintenance mode
139 1 shmem_lock_maintenance(handler);
140 // PRE-B9: Child 1 will read the value in maintenance mode
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(shdata->foo == 42*42);
142 1 shmem_unlock_maintenance(handler);
143 1 pthread_barrier_wait(&shdata->barrier); // Barrier 9
144
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (child == 2) {
145 // PRE-B8: Child 2 enters in busy mode
146 1 shmem_acquire_busy(handler);
147 1 pthread_barrier_wait(&shdata->barrier); // Barrier 8
148 // PRE-B9: Child 2 sets some value in busy mode
149 1 shdata->foo = 42*42;
150 // PRE-B9: Child 2 leaves busy mode
151 1 shmem_release_busy(handler);
152 1 pthread_barrier_wait(&shdata->barrier); // Barrier 9
153 } else {
154 1 pthread_barrier_wait(&shdata->barrier); // Barrier 8
155 1 pthread_barrier_wait(&shdata->barrier); // Barrier 9
156 }
157
158 // Test that BUSY mode does not cause synchronization
159 3 shmem_acquire_busy(handler);
160 // Everybody synchronizes inside
161 3 pthread_barrier_wait(&shdata->barrier); // Barrier 10
162
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (child == 1) {
163 // Only one child deactivates busy mode
164 1 shmem_release_busy(handler);
165 }
166 3 pthread_barrier_wait(&shdata->barrier); // Barrier 11
167
168 3 shmem_finalize(handler, NULL);
169
170 // We need to call _exit so that children don't call assert_shmem destructors
171 3 dlb_test__exit(EXIT_SUCCESS);
172 ✗ break;
173 }
174 }
175
176 // Master process must do as many barriers as children do
177 int i;
178
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 1 times.
12 for (i=0; i<11; ++i) {
179 11 pthread_barrier_wait(&shdata->barrier);
180 }
181
182 // Finalize everything
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
184 1 shmem_finalize(handler, NULL);
185
186 // Wait for all child processes
187 int wstatus;
188
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1 times.
4 while(wait(&wstatus) > 0) {
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (!WIFEXITED(wstatus)) return -1;
190 }
191
192 1 mu_finalize();
193
194 1 return 0;
195 }
196