GCC Code Coverage Report


Directory: ./
File: tests/unit/01_pm/omptm_free_agents_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 265 265 100.0%
Functions: 1 1 100.0%
Branches: 202 346 58.4%

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 "unique_shmem.h"
21
22 #include "LB_numThreads/omptm_free_agents.h"
23 #include "LB_numThreads/omptool.h"
24 #include "LB_numThreads/omp-tools.h"
25 #include "LB_core/DLB_kernel.h"
26 #include "LB_core/spd.h"
27 #include "LB_comm/shmem_cpuinfo.h"
28 #include "LB_comm/shmem_procinfo.h"
29 #include "apis/dlb_errors.h"
30 #include "support/mask_utils.h"
31
32 #include <assert.h>
33 #include <sched.h>
34 #include <stdbool.h>
35 #include <string.h>
36
37 /* array_cpuid_t */
38 #define ARRAY_T cpuid_t
39 #include "support/array_template.h"
40
41 /* array_cpuinfo_task_t */
42 #define ARRAY_T cpuinfo_task_t
43 #define ARRAY_KEY_T pid_t
44 #include "support/array_template.h"
45
46 // This test needs at least room for 8 CPUs
47 enum { SYS_SIZE = 8 };
48
49 static int free_agent_id = 0;
50 7 int __kmp_get_free_agent_id(void) {
51 7 return free_agent_id;
52 }
53
54 4 int __kmp_get_num_free_agent_threads(void) {
55 4 return SYS_SIZE-1;
56 }
57
58 static bool free_agent_threads_status[SYS_SIZE-1];
59 30 void __kmp_set_free_agent_thread_active_status(
60 unsigned int thread_num, bool active) {
61 30 free_agent_threads_status[thread_num] = active;
62 30 }
63
64
65 1 int main (int argc, char *argv[]) {
66 1 mu_init();
67 1 mu_testing_set_sys_size(SYS_SIZE);
68
69 1 char options[64] = "--lewi --shm-key=";
70 1 strcat(options, SHMEM_KEY);
71
72 /* P1 needs pre-init + init */
73 1 char p1_options[64] = "--preinit-pid=111 ";
74 1 strcat(p1_options, options);
75 cpu_set_t p1_mask;
76 1 mu_parse_mask("0,1,4,5", &p1_mask);
77 1 spd_enter_dlb(NULL);
78 1 thread_spd->dlb_initialized = true;
79
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( PreInitialize(thread_spd, &p1_mask, p1_options) == DLB_SUCCESS );
80 1 pid_t p1_pid = 111;
81
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 assert( Initialize(thread_spd, p1_pid, CPU_COUNT(&p1_mask), &p1_mask, p1_options)
82 == DLB_SUCCESS );
83
84 /* P2 is fake and does nothing with OMPT, just init shmems */
85 cpu_set_t p2_mask;
86 1 mu_parse_mask("2,3,6,7", &p2_mask);
87 1 pid_t p2_pid = 222;
88
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__init(p2_pid, 0, &p2_mask, thread_spd->options.shm_key,
89 thread_spd->options.lewi_color) == DLB_SUCCESS );
90
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__init(p2_pid, 0, &p2_mask, NULL, thread_spd->options.shm_key,
91 thread_spd->options.shm_size_multiplier) == DLB_SUCCESS );
92
93 // Setup dummy priority CPUs
94 array_cpuid_t cpus_priority_array;
95 1 array_cpuid_t_init(&cpus_priority_array, SYS_SIZE);
96
2/2
✓ Branch 1 taken 8 times.
✓ Branch 2 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) array_cpuid_t_push(&cpus_priority_array, i);
97
98 array_cpuinfo_task_t tasks;
99 1 array_cpuinfo_task_t_init(&tasks, SYS_SIZE);
100
101 1 int64_t last_borrow = 0;
102
103 /* P1 inits free agents module with OMP_NUM_THREADS=1 */
104 {
105 1 setenv("OMP_NUM_THREADS", "1", 1);
106 1 omptm_free_agents__init(p1_pid, &thread_spd->options);
107
108 /* Free agents should bind in this order: [1,4,5], [2,3,6,7] */
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(0) == 1 );
110
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(1) == 4 );
111
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(2) == 5 );
112
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(3) == 2 );
113
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(4) == 3 );
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(5) == 6 );
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(6) == 7 );
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(7) == -1 );
117
118 1 omptm_free_agents__finalize();
119 }
120
121 /* P1 inits free agents module with OMP_NUM_THREADS=2 */
122 {
123 1 setenv("OMP_NUM_THREADS", "2", 1);
124 1 omptm_free_agents__init(p1_pid, &thread_spd->options);
125
126 /* Free agents should bind in this order: [4,5], [2,3,6,7], [1] */
127
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(0) == 4 );
128
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(1) == 5 );
129
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(2) == 2 );
130
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(3) == 3 );
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(4) == 6 );
132
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(5) == 7 );
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(6) == 1 );
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(7) == -1 );
135
136 1 omptm_free_agents__finalize();
137 }
138
139 /* P1 inits free agents module with OMP_NUM_THREADS=4 */
140 {
141 1 setenv("OMP_NUM_THREADS", "4", 1);
142 1 omptm_free_agents__init(p1_pid, &thread_spd->options);
143
144 /* Free agents should bind in this order: [2,3,6,7], [1,4,5] */
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(0) == 2 );
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(1) == 3 );
147
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(2) == 6 );
148
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(3) == 7 );
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(4) == 1 );
150
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(5) == 4 );
151
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(6) == 5 );
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_binding(7) == -1 );
153
154 1 omptm_free_agents__finalize();
155 }
156
157 // Initialize thread status array to true just to make sure
158 // the thread_begin function disables all the free agent threads
159
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i=0; i<SYS_SIZE-1; ++i) {
160 7 free_agent_threads_status[i] = true;
161 }
162
163 // Assume OMP_NUM_THREADS=4 for the rest of the test
164 1 setenv("OMP_NUM_THREADS", "4", 1);
165 1 omptm_free_agents__init(p1_pid, &thread_spd->options);
166
167 /* Emulate free agent threads creation */
168 {
169
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i=0; i<SYS_SIZE-1; ++i) {
170 7 free_agent_id = i;
171 7 omptm_free_agents__thread_begin(ompt_thread_other);
172 }
173
174 /* All free agents are disabled */
175
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i=0; i<SYS_SIZE-1; ++i) {
176
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 assert( free_agent_threads_status[i] == false );
177 }
178
179 /* check all CPU ids */
180
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(0) == -1 );
181
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(1) == 4 );
182
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(2) == 0 );
183
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(3) == 1 );
184
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(4) == 5 );
185
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(5) == 6 );
186
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(6) == 2 );
187
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_id_by_cpuid(7) == 3 );
188
189 /* check all free agent ids */
190
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i=0; i<SYS_SIZE-1; ++i) {
191
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 7 times.
7 assert( omptm_free_agents_testing__get_free_agent_cpuid_by_id(i)
192 == omptm_free_agents_testing__get_free_agent_binding(i) );
193 }
194
195 /* check ordered CPU list */
196
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(0) == 1 );
197
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(1) == 4 );
198
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(2) == 5 );
199
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(3) == 2 );
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(4) == 3 );
201
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(5) == 6 );
202
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_free_agent_cpu(6) == 7 );
203 }
204
205 /* Test tasks out of parallel, then reclaim CPUs from same process (for parallel)
206 * and later from other processes */
207 {
208 /* Free agents assigned to CPUs [1,4,5] should take priority,
209 * since they belong to the process mask and there is not an
210 * active parallel region */
211
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( !omptm_free_agents_testing__in_parallel() );
212
213 /* Acquire CPU 1 */
214 1 omptm_free_agents_testing__acquire_one_free_agent();
215 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(1);
216
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
217
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
218
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 if (i != 1) {
219 7 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(i);
220
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
7 if (free_agent_id != -1) {
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 assert( free_agent_threads_status[free_agent_id] == false );
222 }
223 }
224 }
225
226 /* Acquire CPU 4 */
227 1 omptm_free_agents_testing__acquire_one_free_agent();
228 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(4);
229
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
230
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
231
4/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
8 if (i != 1 && i != 4) {
232 6 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(i);
233
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (free_agent_id != -1) {
234
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 assert( free_agent_threads_status[free_agent_id] == false );
235 }
236 }
237 }
238
239 /* acquire CPU 5 */
240 1 omptm_free_agents_testing__acquire_one_free_agent();
241 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(5);
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
243
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
244
6/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
8 if (i != 1 && i != 4 && i != 5) {
245 5 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(i);
246
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (free_agent_id != -1) {
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert( free_agent_threads_status[free_agent_id] == false );
248 }
249 }
250 }
251
252 /* another acquire should do nothing, since CPUs 2,3,6,7 are still guested by P2 */
253 1 omptm_free_agents_testing__acquire_one_free_agent();
254
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
255
6/6
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
8 if (i != 1 && i != 4 && i != 5) {
256 5 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(i);
257
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (free_agent_id != -1) {
258
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert( free_agent_threads_status[free_agent_id] == false );
259 }
260 }
261 }
262
263 /* P2 lends everything */
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(p2_pid, &p2_mask, &tasks) == DLB_SUCCESS );
265
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
266
267 /* acquire CPU 2 */
268 1 omptm_free_agents_testing__acquire_one_free_agent();
269 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(2);
270
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
271
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
272
8/8
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 4 times.
✓ Branch 7 taken 1 times.
8 if (i != 1 && i != 4 && i != 5 && i != 2) {
273 4 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(i);
274
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 if (free_agent_id != -1) {
275
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 assert( free_agent_threads_status[free_agent_id] == false );
276 }
277 }
278 }
279
280 /* acquire CPUs 3,6,7 */
281 1 omptm_free_agents_testing__acquire_one_free_agent();
282 1 omptm_free_agents_testing__acquire_one_free_agent();
283 1 omptm_free_agents_testing__acquire_one_free_agent();
284
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 1 times.
8 for (int i=0; i<SYS_SIZE-1; ++i) {
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7 times.
7 assert( free_agent_threads_status[i] == true );
286 }
287
288 /* P1 starts a parallel region with 4 threads */
289 1 omptool_parallel_data_t omptool_parallel_data = {
290 .level = 1,
291 .requested_parallelism = 4,
292 };
293 1 omptm_free_agents__parallel_begin(&omptool_parallel_data);
294 1 omptm_free_agents_testing__set_worker_binding(0);
295 1 omptm_free_agents__into_parallel_function(&omptool_parallel_data, 0);
296 1 omptm_free_agents_testing__set_worker_binding(1);
297 1 omptm_free_agents__into_parallel_function(&omptool_parallel_data, 1);
298 1 omptm_free_agents_testing__set_worker_binding(4);
299 1 omptm_free_agents__into_parallel_function(&omptool_parallel_data, 2);
300 1 omptm_free_agents_testing__set_worker_binding(5);
301 1 omptm_free_agents__into_parallel_function(&omptool_parallel_data, 3);
302
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_in_parallel(0) );
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_in_parallel(1) );
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_in_parallel(4) );
305
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_in_parallel(5) );
306
307 /* free agents in CPUs 1,4,5 should be disabled */
308 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(1);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
310 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(4);
311
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
312 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(5);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
314
315 /* free agents in CPUs 2,3,6,7 should not */
316
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 4 );
317 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(2);
318
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
319 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(3);
320
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
321 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(6);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
323 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(7);
324
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
325
326 /* free agent in CPU 2 finishes a task, let's assume there are some pending tasks */
327 1 omptm_free_agents_testing__set_pending_tasks(10);
328
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 4 );
329 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(2);
330 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
331 1 omptm_free_agents__task_complete();
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == true );
333
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 4 );
334
335 /* same, but now without pending tasks */
336 1 omptm_free_agents_testing__set_pending_tasks(0);
337 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(2);
338 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
339 1 omptm_free_agents__task_complete();
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
341
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 3 );
342
343 /* P2 reclaims its process mask, CPU 2 can be immediately acquired */
344
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_all(p2_pid, &tasks) == DLB_NOTED );
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 7 );
346
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == p2_pid
347 && tasks.items[0].cpuid == 2
348 && tasks.items[0].action == ENABLE_CPU );
349
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[1].pid == p1_pid
350 && tasks.items[1].cpuid == 3
351 && tasks.items[1].action == DISABLE_CPU );
352
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[2].pid == p2_pid
353 && tasks.items[2].cpuid == 3
354 && tasks.items[2].action == ENABLE_CPU );
355
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[3].pid == p1_pid
356 && tasks.items[3].cpuid == 6
357 && tasks.items[3].action == DISABLE_CPU );
358
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[4].pid == p2_pid
359 && tasks.items[4].cpuid == 6
360 && tasks.items[4].action == ENABLE_CPU );
361
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[5].pid == p1_pid
362 && tasks.items[5].cpuid == 7
363 && tasks.items[5].action == DISABLE_CPU );
364
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[6].pid == p2_pid
365 && tasks.items[6].cpuid == 7
366 && tasks.items[6].action == ENABLE_CPU );
367 1 array_cpuinfo_task_t_clear(&tasks);
368
369 /* Free agents finish their tasks, they should notice their CPU is reclaimed */
370 1 omptm_free_agents_testing__set_pending_tasks(10);
371 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(3);
372 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
373 1 omptm_free_agents__task_complete();
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
375 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(6);
376 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
377 1 omptm_free_agents__task_complete();
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
379 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(7);
380 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
381 1 omptm_free_agents__task_complete();
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
383
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
384
385 /* P1 starts a nested parallel region, it should not affect anything */
386 1 omptool_parallel_data_t omptool_parallel_data_level2 = {
387 .level = 2,
388 .requested_parallelism = 1,
389 };
390 1 omptm_free_agents__parallel_begin(&omptool_parallel_data_level2);
391 1 omptm_free_agents__into_parallel_function(&omptool_parallel_data_level2, 0);
392
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
393 1 omptm_free_agents_testing__acquire_one_free_agent();
394
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
395
396 /* P1 finalizes nested parallel, nothing... */
397 1 omptm_free_agents__parallel_end(&omptool_parallel_data_level2);
398
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__in_parallel() );
399
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
400 1 omptm_free_agents_testing__acquire_one_free_agent();
401
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
402
403 /* P1 finalizes level 1 parallel */
404 1 omptm_free_agents__parallel_end(&omptool_parallel_data);
405
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( !omptm_free_agents_testing__in_parallel() );
406
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
407
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(1) );
408
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(4) );
409
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(5) );
410 1 omptm_free_agents_testing__acquire_one_free_agent();
411 1 omptm_free_agents_testing__acquire_one_free_agent();
412 1 omptm_free_agents_testing__acquire_one_free_agent();
413
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 3 );
414
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_free_agent_enabled(1) );
415
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_free_agent_enabled(4) );
416
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_free_agent_enabled(5) );
417
418 /* No more tasks, disable free agents */
419 1 omptm_free_agents_testing__set_pending_tasks(0);
420 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(1);
421 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
422 1 omptm_free_agents__task_complete();
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
424
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 2 );
425 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(4);
426 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
427 1 omptm_free_agents__task_complete();
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
429
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 1 );
430 1 free_agent_id = omptm_free_agents_testing__get_free_agent_id_by_cpuid(5);
431 1 omptm_free_agents_testing__set_free_agent_id(free_agent_id);
432 1 omptm_free_agents__task_complete();
433
434
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( free_agent_threads_status[free_agent_id] == false );
435
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
436
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(1) );
437
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(4) );
438
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__check_cpu_idle(5) );
439 }
440
441 /* Test IntoBlockingCall and OutOfBlockingCall: easy, no reclaim */
442 {
443 /* P1 is outside a parallel region, and invokes IntoBlockingCall */
444 1 omptm_free_agents__IntoBlockingCall();
445
446 /* P2 asks for all the CPUs, it should success (CPU 0 is not lent) */
447
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__borrow_ncpus_from_cpu_subset(p2_pid, NULL,
448 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0,
449 &last_borrow, &tasks) == DLB_SUCCESS );
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 3 );
451
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[0].pid == p2_pid
452 && tasks.items[0].cpuid == 1
453 && tasks.items[0].action == ENABLE_CPU );
454
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[1].pid == p2_pid
455 && tasks.items[1].cpuid == 4
456 && tasks.items[1].action == ENABLE_CPU );
457
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 assert( tasks.items[2].pid == p2_pid
458 && tasks.items[2].cpuid == 5
459 && tasks.items[2].action == ENABLE_CPU );
460 1 array_cpuinfo_task_t_clear(&tasks);
461
462 /* P2 returns P1's CPUs */
463
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__lend_cpu_mask(p2_pid, &p1_mask, &tasks) == DLB_SUCCESS );
464
465 /* P1 invokes OutOfBlockingCall */
466
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
467 1 omptm_free_agents__OutOfBlockingCall();
468
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
469
470 /* Free agents can be enabled on CPUs 1,4,5 */
471 1 omptm_free_agents_testing__set_pending_tasks(10);
472
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 0 );
473 1 omptm_free_agents_testing__acquire_one_free_agent();
474 1 omptm_free_agents_testing__acquire_one_free_agent();
475 1 omptm_free_agents_testing__acquire_one_free_agent();
476
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_free_agents_testing__get_num_enabled_free_agents() == 3 );
477 }
478
479 /* Finalize */
480
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( Finish(thread_spd) == DLB_SUCCESS );
481 1 omptm_free_agents__finalize();
482
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(p2_pid, thread_spd->options.shm_key,
483 thread_spd->options.lewi_color) == DLB_SUCCESS );
484
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__finalize(p2_pid, false, thread_spd->options.shm_key,
485 thread_spd->options.shm_size_multiplier) == DLB_SUCCESS );
486
487 1 mu_finalize();
488
489 1 return 0;
490 }
491