GCC Code Coverage Report


Directory: ./
File: tests/unit/01_pm/pm_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 111 111 100.0%
Functions: 10 10 100.0%
Branches: 83 164 50.6%

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 #include "LB_numThreads/numThreads.h"
21 #include "apis/dlb_errors.h"
22 #include "support/mask_utils.h"
23
24 #include <sched.h>
25 #include <string.h>
26 #include <assert.h>
27
28 typedef struct MyObject {
29 int n;
30 } object_t;
31
32 static int nthreads = 0;
33 static cpu_set_t process_mask;
34
35 static object_t cb_set_num_threads_arg = { .n = 1 };
36 4 static void cb_set_num_threads(int num_threads, void *arg) {
37
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert( ((object_t*)arg)->n == cb_set_num_threads_arg.n );
38 4 nthreads = num_threads;
39 4 }
40
41 static object_t cb_set_active_mask_arg = { .n = 2 };
42 1 static void cb_set_active_mask(const cpu_set_t *mask, void *arg) {
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( ((object_t*)arg)->n == cb_set_active_mask_arg.n );
44 1 }
45
46 static object_t cb_set_process_mask_arg = { .n = 3 };
47 2 static void cb_set_process_mask(const cpu_set_t *mask, void *arg) {
48
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( ((object_t*)arg)->n == cb_set_process_mask_arg.n );
49 2 memcpy(&process_mask, mask, sizeof(cpu_set_t));
50 2 }
51
52 static object_t cb_add_active_mask_arg = { .n = 4 };
53 1 static void cb_add_active_mask(const cpu_set_t *mask, void *arg) {
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( ((object_t*)arg)->n == cb_add_active_mask_arg.n );
55 1 }
56
57 static object_t cb_add_process_mask_arg = { .n = 5 };
58 1 static void cb_add_process_mask(const cpu_set_t *mask, void *arg) {
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( ((object_t*)arg)->n == cb_add_process_mask_arg.n );
60 1 }
61
62 static object_t cb_enable_cpu_arg = { .n = 6 };
63 2 static void cb_enable_cpu(int cpuid, void *arg) {
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( ((object_t*)arg)->n == cb_enable_cpu_arg.n );
65
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_SET(cpuid, &process_mask);
66 2 }
67
68 static object_t cb_disable_cpu_arg = { .n = 7 };
69 2 static void cb_disable_cpu(int cpuid, void *arg) {
70
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( ((object_t*)arg)->n == cb_disable_cpu_arg.n );
71
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_CLR(cpuid, &process_mask);
72 2 }
73
74 static object_t cb_enable_cpu_set_arg = { .n = 8 };
75 2 static void cb_enable_cpu_set(const cpu_set_t *cpu_set, void *arg) {
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( ((object_t*)arg)->n == cb_enable_cpu_set_arg.n );
77
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 2 times.
34 CPU_OR(&process_mask, &process_mask, cpu_set);
78 2 }
79
80 static object_t cb_disable_cpu_set_arg = { .n = 9 };
81 2 static void cb_disable_cpu_set(const cpu_set_t *cpu_set, void *arg) {
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 assert( ((object_t*)arg)->n == cb_disable_cpu_set_arg.n );
83 2 mu_subtract(&process_mask, &process_mask, cpu_set);
84 2 }
85
86 1 int main( int argc, char **argv ) {
87 cpu_set_t mask;
88 1 CPU_ZERO(&mask);
89 1 CPU_ZERO(&process_mask);
90 pm_interface_t pm;
91 1 pm_init(&pm);
92
93 // Call callbacks without initialization and check DLB_ERR_NOCBK
94 /* update_threads may have been initialized with omp_set_num_threads
95 assert( update_threads(&pm, 2) == DLB_ERR_NOCBK );
96 */
97
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( set_mask(&pm, &mask) == DLB_ERR_NOCBK );
98
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( set_process_mask(&pm, &mask) == DLB_ERR_NOCBK );
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( add_mask(&pm, &mask) == DLB_ERR_NOCBK );
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( add_process_mask(&pm, &mask) == DLB_ERR_NOCBK );
101
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( enable_cpu(&pm, 0) == DLB_ERR_NOCBK );
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( disable_cpu(&pm, 0) == DLB_ERR_NOCBK );
103
104 // Set callbacks
105
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_set_num_threads,
106 (dlb_callback_t)cb_set_num_threads, &cb_set_num_threads_arg) == DLB_SUCCESS );
107
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_set_active_mask,
108 (dlb_callback_t)cb_set_active_mask, &cb_set_active_mask_arg) == DLB_SUCCESS );
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_set_process_mask,
110 (dlb_callback_t)cb_set_process_mask, &cb_set_process_mask_arg) == DLB_SUCCESS );
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_add_active_mask,
112 (dlb_callback_t)cb_add_active_mask, &cb_add_active_mask_arg) == DLB_SUCCESS );
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_add_process_mask,
114 (dlb_callback_t)cb_add_process_mask, &cb_add_process_mask_arg) == DLB_SUCCESS );
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_enable_cpu,
116 (dlb_callback_t)cb_enable_cpu, &cb_enable_cpu_arg) == DLB_SUCCESS );
117
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_disable_cpu,
118 (dlb_callback_t)cb_disable_cpu, &cb_disable_cpu_arg) == DLB_SUCCESS );
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_enable_cpu_set,
120 (dlb_callback_t)cb_enable_cpu_set, &cb_enable_cpu_set_arg) == DLB_SUCCESS );
121
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, dlb_callback_disable_cpu_set,
122 (dlb_callback_t)cb_disable_cpu_set, &cb_disable_cpu_set_arg) == DLB_SUCCESS );
123
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_set(&pm, (dlb_callbacks_t)42, (dlb_callback_t)main, NULL) == DLB_ERR_NOCBK );
124
125 // Get callbacks
126 dlb_callback_t cb;
127 void *arg;
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_set_num_threads, &cb, &arg) == DLB_SUCCESS );
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_set_num_threads );
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_set_num_threads_arg );
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_set_active_mask, &cb, &arg) == DLB_SUCCESS );
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_set_active_mask );
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_set_active_mask_arg );
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_set_process_mask, &cb, &arg) == DLB_SUCCESS );
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_set_process_mask );
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_set_process_mask_arg );
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_add_active_mask, &cb, &arg) == DLB_SUCCESS );
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_add_active_mask );
139
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_add_active_mask_arg );
140
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_add_process_mask, &cb, &arg) == DLB_SUCCESS );
141
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_add_process_mask );
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_add_process_mask_arg );
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_enable_cpu, &cb, &arg) == DLB_SUCCESS );
144
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_enable_cpu );
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_enable_cpu_arg );
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_disable_cpu, &cb, &arg) == DLB_SUCCESS );
147
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_disable_cpu );
148
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_disable_cpu_arg );
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_enable_cpu_set, &cb, &arg) == DLB_SUCCESS );
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_enable_cpu_set );
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_enable_cpu_set_arg );
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, dlb_callback_disable_cpu_set, &cb, &arg) == DLB_SUCCESS );
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cb == (dlb_callback_t)cb_disable_cpu_set );
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( arg == &cb_disable_cpu_set_arg );
155
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( pm_callback_get(&pm, (dlb_callbacks_t)42, &cb, &arg) == DLB_ERR_NOCBK );
156
157 // Call callback and check DLB_SUCCESS
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( update_threads(&pm, 0) == DLB_SUCCESS );
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( update_threads(&pm, 2) == DLB_SUCCESS );
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( update_threads(&pm, 42) == DLB_SUCCESS );
161
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( set_mask(&pm, &mask) == DLB_SUCCESS );
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( set_process_mask(&pm, &mask) == DLB_SUCCESS );
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( add_mask(&pm, &mask) == DLB_SUCCESS );
164
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( add_process_mask(&pm, &mask) == DLB_SUCCESS );
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( enable_cpu(&pm, 0) == DLB_SUCCESS );
166
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( disable_cpu(&pm, 0) == DLB_SUCCESS );
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( enable_cpu_set(&pm, &mask) == DLB_SUCCESS );
168
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( disable_cpu_set(&pm, &mask) == DLB_SUCCESS );
169
170 // Call callback and check that the parameter is correct
171 1 update_threads(&pm, 1);
172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(nthreads == 1);
173 1 CPU_ZERO(&mask);
174
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(0, &mask);
175
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(1, &mask);
176
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( set_process_mask(&pm, &mask) == DLB_SUCCESS );
177
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&process_mask) == 2 );
178
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( enable_cpu(&pm, 2) == DLB_SUCCESS );
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&process_mask) == 3 );
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( disable_cpu(&pm, 2) == DLB_SUCCESS );
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&process_mask) == 2 );
182 1 CPU_ZERO(&mask);
183
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(2, &mask);
184
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(3, &mask);
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( enable_cpu_set(&pm, &mask) == DLB_SUCCESS );
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&process_mask) == 4 );
187
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( disable_cpu_set(&pm, &mask) == DLB_SUCCESS );
188
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( CPU_COUNT(&process_mask) == 2 );
189
190 1 return 0;
191 }
192