GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_02.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 4 78 5.1%
Functions: 1 5 20.0%
Branches: 2 94 2.1%

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 "assert_loop.h"
21 #include "unique_shmem.h"
22 #include "test_process.h"
23
24 #include "apis/dlb.h"
25 #include "support/mask_utils.h"
26 #include "LB_comm/shmem.h"
27
28 #include <assert.h>
29 #include <pthread.h>
30 #include <stdbool.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/wait.h>
34 #include <unistd.h>
35
36 /* Test --lewi-color */
37
38 struct data {
39 bool initialized;
40 pthread_barrier_t barrier;
41 };
42
43 ✗ static shmem_handler_t* open_shmem(void **shdata) {
44 ✗ return shmem_init(shdata,
45 ✗ &(const shmem_props_t) {
46 .size = sizeof(struct data),
47 .name = "test",
48 .key = SHMEM_KEY,
49 });
50 }
51
52 ✗ void init_shmem(shmem_handler_t *handler, struct data *shdata, int barrier_size) {
53 ✗ shmem_lock(handler);
54 {
55 ✗ if (!shdata->initialized) {
56 pthread_barrierattr_t attr;
57 ✗ assert( pthread_barrierattr_init(&attr) == 0 );
58 ✗ assert( pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED) == 0 );
59 ✗ assert( pthread_barrier_init(&shdata->barrier, &attr, barrier_size) == 0 );
60 ✗ assert( pthread_barrierattr_destroy(&attr) == 0 );
61 ✗ shdata->initialized = true;
62 }
63 }
64 ✗ shmem_unlock(handler);
65 }
66
67
68 static cpu_set_t process_mask;
69
70 ✗ static void cb_enable_cpu(int cpuid, void *arg) {
71 ✗ CPU_SET(cpuid, &process_mask);
72 }
73
74 ✗ static void cb_disable_cpu(int cpuid, void *arg) {
75 ✗ CPU_CLR(cpuid, &process_mask);
76 }
77
78 1 int main(int argc, char *argv[]) {
79
80 /* This test needs at least 4 real CPUs */
81 cpu_set_t parent_process_mask;
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( sched_getaffinity(0, sizeof(cpu_set_t), &parent_process_mask) == 0 );
83
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (CPU_COUNT(&parent_process_mask) < 4 ) {
84 1 return 0;
85 }
86
87 /* Set up masks for child processes:
88 * Children 0 and 2: [0,1] or first two valid CPUs
89 * Children 1 and 3: [2,3] or second two valid CPUs
90 */
91 cpu_set_t child_0_2_mask, child_1_3_mask;
92 ✗ CPU_ZERO(&child_0_2_mask);
93 ✗ CPU_ZERO(&child_1_3_mask);
94 ✗ int cpuid = mu_get_first_cpu(&parent_process_mask);
95 ✗ CPU_SET(cpuid, &child_0_2_mask); // [0]
96 ✗ cpuid = mu_get_next_cpu(&parent_process_mask, cpuid);
97 ✗ CPU_SET(cpuid, &child_0_2_mask); // [1]
98 ✗ cpuid = mu_get_next_cpu(&parent_process_mask, cpuid);
99 ✗ CPU_SET(cpuid, &child_1_3_mask); // [2]
100 ✗ cpuid = mu_get_next_cpu(&parent_process_mask, cpuid);
101 ✗ CPU_SET(cpuid, &child_1_3_mask); // [3]
102
103 /* Test 4 processes, 2 CPUs each, in a 4 CPU system (oversubscription) */
104 {
105 enum { NUM_PROCS = 4 };
106 struct data *shdata;
107 shmem_handler_t *handler;
108
109 // Create 4 children
110 ✗ for (int child_num = 0; child_num < NUM_PROCS; ++child_num) {
111 ✗ pid_t pid = fork();
112 ✗ assert( pid >= 0 );
113 ✗ if (pid == 0) {
114
115 // Initialize DLB
116 // 0: [0,1] and 1: [2,3], --lewi-color=1
117 // 2: [0,1] and 3: [2,3], --lewi-color=2
118 ✗ memcpy(&process_mask,
119 ✗ child_num % 2 == 0 ? &child_0_2_mask : &child_1_3_mask,
120 sizeof(cpu_set_t));
121
122 ✗ pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &process_mask);
123
124 char options[64];
125 ✗ snprintf(options, 64, "--lewi --lewi-color=%1d --shm-key=%s",
126 child_num < 2 ? 1 : 2, SHMEM_KEY);
127 ✗ assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
128 ✗ assert( DLB_CallbackSet(dlb_callback_enable_cpu,
129 (dlb_callback_t)cb_enable_cpu, NULL) == DLB_SUCCESS);
130 ✗ assert( DLB_CallbackSet(dlb_callback_disable_cpu,
131 (dlb_callback_t)cb_disable_cpu, NULL) == DLB_SUCCESS);
132
133 // Initialize 'test' shmem and perform a barrier
134 ✗ handler = open_shmem((void**)&shdata);
135 ✗ init_shmem(handler, shdata, NUM_PROCS);
136 ✗ int error = pthread_barrier_wait(&shdata->barrier);
137 ✗ assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
138
139 // Child 0 lends all CPUs: [0,1]
140 ✗ if (child_num == 0) {
141 ✗ assert( DLB_Lend() == DLB_SUCCESS );
142 ✗ CPU_ZERO(&process_mask);
143 }
144
145 // Barrier
146 ✗ error = pthread_barrier_wait(&shdata->barrier);
147 ✗ assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
148
149 // Child 1 may acquire CPUs 0 and 1
150 ✗ if (child_num == 1) {
151 ✗ assert( DLB_Borrow() == DLB_SUCCESS );
152 ✗ assert( CPU_COUNT(&process_mask) == 4 );
153 ✗ assert( mu_is_subset(&child_0_2_mask, &process_mask) );
154 ✗ assert( mu_is_subset(&child_1_3_mask, &process_mask) );
155 }
156
157 // Barrier
158 ✗ error = pthread_barrier_wait(&shdata->barrier);
159 ✗ assert(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
160
161 // Children 2 and 3 are not affected, they still have their own mask
162 ✗ if (child_num == 2) {
163 ✗ assert( CPU_COUNT(&process_mask) == 2 );
164 ✗ assert( mu_is_subset(&child_0_2_mask, &process_mask) );
165 }
166 ✗ if (child_num == 3) {
167 ✗ assert( CPU_COUNT(&process_mask) == 2 );
168 ✗ assert( mu_is_subset(&child_1_3_mask, &process_mask) );
169 }
170
171 // All 4 processes may invoke the same barrier
172 ✗ assert( DLB_Barrier() == DLB_SUCCESS );
173
174 // Print shmem from one child of each color:
175 ✗ if (child_num % 2 == 0) {
176 ✗ DLB_PrintShmem(0, DLB_COLOR_ALWAYS);
177 }
178
179 ✗ assert( DLB_Barrier() == DLB_SUCCESS );
180
181 // Finalize DLB
182 ✗ assert( DLB_Finalize() == DLB_SUCCESS );
183 ✗ shmem_finalize(handler, NULL);
184
185 // We need to call _exit so that children don't call assert_shmem destructors
186 ✗ dlb_test__exit(EXIT_SUCCESS);
187 }
188 }
189
190 // Wait for all child processes
191 int wstatus;
192 ✗ while(wait(&wstatus) > 0) {
193 ✗ if (!WIFEXITED(wstatus))
194 ✗ exit(EXIT_FAILURE);
195 ✗ int rc = WEXITSTATUS(wstatus);
196 ✗ if (rc != 0) {
197 ✗ printf("Child return status: %d\n", rc);
198 ✗ exit(EXIT_FAILURE);
199 }
200 }
201 }
202
203 ✗ return 0;
204 }
205