GCC Code Coverage Report


Directory: ./
File: tests/unit/02_shmem/shmem_size_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 69 70 98.6%
Functions: 3 3 100.0%
Branches: 35 58 60.3%

Line Branch Exec Source
1 /*********************************************************************************/
2 /* Copyright 2009-2024 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 // Test eachs of shared memory capacity with default size and --shm-size-multiplier
21
22 #include "extra_tests.h"
23 #include "unique_shmem.h"
24 #include "test_process.h"
25
26 #include "LB_comm/shmem.h"
27 #include "LB_comm/shmem_async.h"
28 #include "LB_comm/shmem_barrier.h"
29 #include "LB_comm/shmem_lewi_async.h"
30 #include "LB_comm/shmem_procinfo.h"
31 #include "LB_comm/shmem_talp.h"
32 #include "LB_core/spd.h"
33 #include "apis/dlb_errors.h"
34 #include "support/debug.h"
35 #include "support/mask_utils.h"
36 #include "support/options.h"
37
38 #include <assert.h>
39 #include <pthread.h>
40 #include <stdlib.h>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <unistd.h>
44
45
46 struct data {
47 pthread_barrier_t barrier;
48 };
49
50 /* Every child process calls this function to test the full occupancy
51 * of the shared memory. Two barriers are needed, in between the parent
52 * process will try to initialize DLB and will get an error */
53 35 void init_dlb_wait_and_finalize(const cpu_set_t *system_mask, options_t *options) {
54
55 // Attach to the test shared memory
56 struct data *shdata;
57 35 shmem_handler_t *handler = shmem_init((void**)&shdata,
58 35 &(const shmem_props_t) {
59 .size = sizeof(struct data),
60 .name = "test",
61 35 .key = options->shm_key,
62 });
63
64 // All children initialize DLB shared memories
65 35 subprocess_descriptor_t child_spd = { .id = getpid() };
66 35 spd_enter_dlb(&child_spd);
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_async_init(child_spd.id, NULL, system_mask, options->shm_key,
68 options->shm_size_multiplier) == DLB_SUCCESS );
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_barrier__init(options->shm_key, options->shm_size_multiplier)
70 == DLB_SUCCESS );
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_lewi_async__init(child_spd.id, 1, options->shm_key,
72 options->shm_size_multiplier) == DLB_SUCCESS );
73
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_procinfo__init_with_cpu_sharing(child_spd.id, 0, system_mask,
74 options->shm_key, options->shm_size_multiplier) == DLB_SUCCESS );
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_talp__init(options->shm_key, options->shm_size_multiplier)
76 == DLB_SUCCESS );
77
78 /* Barrier 1 */
79 35 pthread_barrier_wait(&shdata->barrier);
80
81 /* Barrier 2 */
82 35 pthread_barrier_wait(&shdata->barrier);
83
84 // All children finalize DLB shared memories
85 unsigned int new_ncpus, nreqs;
86 lewi_request_t requests;
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_async_finalize(child_spd.id) == DLB_SUCCESS );
88 35 shmem_barrier__finalize(options->shm_key, options->shm_size_multiplier);
89 35 shmem_lewi_async__finalize(child_spd.id, &new_ncpus, &requests, &nreqs, 0);
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_procinfo__finalize(child_spd.id, false, options->shm_key,
91 options->shm_size_multiplier) == DLB_SUCCESS );
92
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
35 assert( shmem_talp__finalize(child_spd.id) == DLB_SUCCESS );
93
94 // Finalize test shared memory
95 35 shmem_finalize(handler, NULL);
96 35 }
97
98
99 6 int test_shm_multiplier(int size_multiplier, bool try_different_multiplier) {
100
101 cpu_set_t system_mask;
102 6 mu_get_system_mask(&system_mask);
103 6 int system_size = mu_get_system_size();
104
105 6 int num_children = system_size * size_multiplier;
106 6 int num_participants = num_children + 1;
107
108 /* Specific case: we don't want to fill the shared memories. Instead, the
109 * parent process with try to initialize them with a different multiplier */
110
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 5 times.
6 if (try_different_multiplier) {
111 1 --num_children;
112 1 --num_participants;
113 }
114
115 /* Options and parent spd */
116 enum { DLB_ARGS_SIZE = 80 };
117 char dlb_args[DLB_ARGS_SIZE];
118 6 snprintf(dlb_args, DLB_ARGS_SIZE, "--shm-size-multiplier=%d --shm-key=%s",
119 size_multiplier, SHMEM_KEY);
120 options_t options;
121 6 options_init(&options, dlb_args);
122 6 debug_init(&options);
123 6 subprocess_descriptor_t spd = { .id = getpid() };
124 6 spd_enter_dlb(&spd);
125
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 assert( options.shm_size_multiplier == size_multiplier );
127
128 /* Parent process creates shmem and initializes barrier */
129 struct data *shdata;
130 6 shmem_handler_t *handler = shmem_init((void**)&shdata,
131 6 &(const shmem_props_t) {
132 .size = sizeof(struct data),
133 .name = "test",
134 .key = options.shm_key,
135 });
136 pthread_barrierattr_t attr;
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 assert( pthread_barrierattr_init(&attr) == 0 );
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 assert( pthread_barrier_init(&shdata->barrier, &attr, num_participants) == 0 );
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 assert( pthread_barrierattr_destroy(&attr) == 0 );
141
142
143 /* Create as many child processes as CPUs in the system */
144
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 6 times.
41 for (int child = 0; child < num_children; ++child) {
145 35 pid_t pid = fork();
146
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 70 times.
70 assert( pid >= 0 );
147
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 35 times.
70 if (pid == 0) {
148
149 /* Everybody initialize DLB, wait for the parent, then finalize */
150 35 init_dlb_wait_and_finalize(&system_mask, &options);
151
152 /* We need to call _exit so that children don't call assert_shmem
153 */
154 35 dlb_test__exit(EXIT_SUCCESS);
155 ✗ break;
156 }
157 }
158
159 /* Barrier 1 */
160 6 pthread_barrier_wait(&shdata->barrier);
161
162
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (!try_different_multiplier) {
163 /* With the shared memories full, the parent process cannot attach to any of them:
164 * (shared memories for Barrier and TALP do not reserve any space during
165 * init, so they cannot cause a DLB_ERR_NOMEM error) */
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 assert( shmem_async_init(spd.id, NULL, &system_mask, options.shm_key,
167 options.shm_size_multiplier) == DLB_ERR_NOMEM );
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 assert( shmem_lewi_async__init(spd.id, 1, options.shm_key,
169 options.shm_size_multiplier) == DLB_ERR_NOMEM );
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 assert( shmem_procinfo__init_with_cpu_sharing(spd.id, 0, &system_mask,
171 options.shm_key, options.shm_size_multiplier) == DLB_ERR_NOMEM );
172 } else {
173 /* Specific case: shared memories are not full, check error if using a
174 * different multiplier */
175
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_async_init(spd.id, NULL, &system_mask, options.shm_key,
176 options.shm_size_multiplier * 2) == DLB_ERR_INIT );
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_lewi_async__init(spd.id, 1, options.shm_key,
178 options.shm_size_multiplier * 2) == DLB_ERR_INIT );
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__init_with_cpu_sharing(spd.id, 0, &system_mask,
180 options.shm_key, options.shm_size_multiplier * 2) == DLB_ERR_INIT );
181 }
182
183 /* Barrier 2 */
184 6 pthread_barrier_wait(&shdata->barrier);
185
186 // Finalize everything
187
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
6 assert( pthread_barrier_destroy(&shdata->barrier) == 0 );
188 6 shmem_finalize(handler, NULL);
189
190 // Wait for all child processes
191 int wstatus;
192
2/2
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 6 times.
41 while(wait(&wstatus) > 0) {
193
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
35 if (!WIFEXITED(wstatus)) return -1;
194 }
195
196 6 return 0;
197 }
198
199 1 int main(int argc, char *argv[]) {
200
201 1 int error = 0;
202
203 /* The following tests may create a lot of child processes in large
204 * systems. Avoid unless option is specified. */
205
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (DLB_EXTRA_TESTS) {
206
207 /* Test --shm-size-multiplier flag */
208
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 for (int size_multiplier = 1; size_multiplier <= 5; ++size_multiplier) {
209 5 fprintf(stderr, "Testing with size multiplier: %d\n", size_multiplier);
210 5 error += test_shm_multiplier(size_multiplier, false);
211 }
212
213 /* Test error when different values of --shm-size-multiplier */
214 1 fprintf(stderr, "Testing with different size multiplier\n");
215 1 error += test_shm_multiplier(3, true);
216 }
217
218 1 return error;
219 }
220