GCC Code Coverage Report


Directory: ./
File: tests/unit/05_api/api_00.c
Date: 2026-11-30 15:03:27
Exec Total Coverage
Lines: 99 108 91.7%
Functions: 2 4 50.0%
Branches: 79 158 50.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 "test_process.h"
21 #include "unique_shmem.h"
22
23 #include "apis/dlb.h"
24 #include "support/mask_utils.h"
25
26 #include <assert.h>
27 #include <sched.h>
28 #include <string.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
31
32 static cpu_set_t process_mask;
33
34 ✗ static void cb_enable_cpu(int cpuid, void *arg) {
35 ✗ CPU_SET(cpuid, &process_mask);
36 }
37
38 ✗ static void cb_disable_cpu(int cpuid, void *arg) {
39 ✗ CPU_CLR(cpuid, &process_mask);
40 ✗ DLB_LendCpu(cpuid);
41 }
42
43 /* Callback to count how many times has been called */
44 static int ntimes = 0;
45 1 static void cb_count(int cpuid, void *arg) {
46 1 ++ntimes;
47 1 }
48
49 1 int main( int argc, char **argv ) {
50 1 CPU_ZERO(&process_mask);
51
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &process_mask);
52
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(1, &process_mask);
53
54 1 char options[64] = "--no-lewi --barrier=no --shm-key=";
55 1 strcat(options, SHMEM_KEY);
56
57
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
58
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackSet(dlb_callback_disable_cpu,
59 (dlb_callback_t)cb_disable_cpu, NULL) == DLB_SUCCESS);
60
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackSet(dlb_callback_enable_cpu,
61 (dlb_callback_t)cb_enable_cpu, NULL) == DLB_SUCCESS);
62 dlb_callback_t cb;
63 void *arg;
64
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackGet(dlb_callback_enable_cpu, &cb, &arg) == DLB_SUCCESS );
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_enable_cpu );
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == NULL );
67
68 // Basic enable-disable test
69
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Disable() == DLB_SUCCESS );
70
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Lend() == DLB_ERR_NOLEWI );
71
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Enable() == DLB_SUCCESS );
72
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_SetMaxParallelism(1) == DLB_ERR_NOLEWI );
73
74 // Lend
75
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Lend() == DLB_ERR_NOLEWI );
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_LendCpu(0) == DLB_ERR_NOLEWI );
77
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_LendCpus(1) == DLB_ERR_NOLEWI );
78
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_LendCpuMask(&process_mask) == DLB_ERR_NOLEWI );
79
80 // Reclaim
81
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Reclaim() == DLB_ERR_NOLEWI );
82
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_ReclaimCpu(0) == DLB_ERR_NOLEWI );
83
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_ReclaimCpus(1) == DLB_ERR_NOLEWI );
84
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_ReclaimCpuMask(&process_mask) == DLB_ERR_NOLEWI );
85
86 // Acquire
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_AcquireCpu(0) == DLB_ERR_NOLEWI );
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_AcquireCpus(1) == DLB_ERR_NOLEWI );
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_AcquireCpuMask(&process_mask) == DLB_ERR_NOLEWI );
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_AcquireCpusInMask(1, &process_mask) == DLB_ERR_NOLEWI );
91
92 // Borrow
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Borrow() == DLB_ERR_NOLEWI );
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BorrowCpu(1) == DLB_ERR_NOLEWI );
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BorrowCpus(1) == DLB_ERR_NOLEWI );
96
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BorrowCpuMask(&process_mask) == DLB_ERR_NOLEWI );
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BorrowCpusInMask(1, &process_mask) == DLB_ERR_NOLEWI );
98
99 // Return
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Return() == DLB_ERR_NOLEWI );
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_ReturnCpu(0) == DLB_ERR_NOLEWI );
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_ReturnCpuMask(&process_mask) == DLB_ERR_NOLEWI );
103
104 // Barrier
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Barrier() == DLB_ERR_NOCOMP );
106
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BarrierAttach() == DLB_ERR_NOCOMP );
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_BarrierDetach() == DLB_ERR_NOCOMP );
108
109 // Misc
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CheckCpuAvailability(0) == DLB_ERR_NOLEWI );
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PollDROM(NULL, NULL) == DLB_ERR_NOCOMP );
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_SetVariable("--drom", "1") == DLB_ERR_PERM );
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_SetVariable("--debug-opts", "foo") == DLB_SUCCESS );
114 char value[32];
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_GetVariable("--drom", value) == DLB_SUCCESS );
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PrintVariables(0) == DLB_SUCCESS );
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PrintShmem(0, DLB_COLOR_AUTO) == DLB_SUCCESS );
118
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Strerror(0) != NULL );
119
120
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
121
122 // Check init again
123 1 CPU_ZERO(&process_mask);
124
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &process_mask);
125
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(1, &process_mask);
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_ERR_INIT );
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
129
130 // Change options to enable lewi
131 1 snprintf(options, 64, "--lewi --shm-key=%s", SHMEM_KEY);
132
133 // Call DLB_PrintShmem with full node
134 1 sched_getaffinity(0, sizeof(cpu_set_t), &process_mask);
135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
136 1 DLB_LendCpu(0);
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PrintShmem(4, DLB_COLOR_AUTO) == DLB_SUCCESS );
138
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
139
140 // Call DLB_PrintShmem with different sizes
141 1 mu_testing_set_sys_size(64);
142 1 CPU_ZERO(&process_mask);
143 int i;
144
3/4
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 64 times.
✓ Branch 3 taken 1 times.
65 for(i=0; i<64; ++i) CPU_SET(i, &process_mask);
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
146 1 DLB_LendCpu(0);
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_PrintShmem(0, DLB_COLOR_AUTO) == DLB_SUCCESS );
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
149
150 // Use different options to enable lewi and respect-cpuset=no
151 char options2[128];
152 1 snprintf(options2, 128, "--lewi --lewi-respect-cpuset=no --shm-key=%s", SHMEM_KEY);
153
154 // Test that callbacks are never invoked on Finalize
155 1 mu_parse_mask("0-3", &process_mask);
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options2) == DLB_SUCCESS );
157 1 ntimes = 0;
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackSet(dlb_callback_disable_cpu,
159 (dlb_callback_t)cb_count, NULL) == DLB_SUCCESS);
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_CallbackSet(dlb_callback_enable_cpu,
161 (dlb_callback_t)cb_count, NULL) == DLB_SUCCESS);
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_AcquireCpu(4) == DLB_SUCCESS );
163
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( ntimes == 1 );
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( ntimes == 1 );
166
167 // Test multiple finalizes with all modules enabled
168 char options3[128];
169 1 snprintf(options3, 128, "--lewi --drom --talp --async --shm-key=%s", SHMEM_KEY);
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options3) == DLB_SUCCESS );
171
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
172
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_NOUPDT );
173
174 // Test that forked process cannot finalize DLB not any other function that
175 // requires DLB initialization
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Init(0, &process_mask, options) == DLB_SUCCESS );
177 1 pid_t pid = fork();
178
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( pid >= 0 );
179
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (pid == 0) {
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (DLB_Enable() != DLB_ERR_NOINIT) {
181 ✗ fprintf(stderr, "ERROR: Child process received unexpected return from DLB_Enable\n");
182 ✗ dlb_test__exit(1);
183 }
184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (DLB_Finalize() != DLB_NOUPDT) {
185 ✗ fprintf(stderr, "ERROR: Child process received unexpected return from DLB_Finalize\n");
186 ✗ dlb_test__exit(1);
187 }
188 1 dlb_test__exit(0);
189 }
190 int status;
191 1 waitpid(pid, &status, 0);
192
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_Finalize() == DLB_SUCCESS );
193
194 // Test version
195 int major, minor, patch;
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( DLB_GetVersion(&major, &minor, &patch) == DLB_SUCCESS );
197
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( major == (DLB_VERSION >> 16 & 0xff) );
198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( minor == (DLB_VERSION >> 8 & 0xff) );
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( patch == (DLB_VERSION & 0xff) );
200
201 1 mu_finalize();
202
203 1 return 0;
204 }
205