GCC Code Coverage Report


Directory: ./
File: tests/unit/01_pm/omptm_role_shift_00.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 304 305 99.7%
Functions: 1 1 100.0%
Branches: 214 418 51.2%

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_role_shift.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/atomic.h"
31 #include "support/mask_utils.h"
32
33 #include <assert.h>
34 #include <sched.h>
35 #include <stdbool.h>
36 #include <string.h>
37
38 /* array_cpuid_t */
39 #define ARRAY_T cpuid_t
40 #include "support/array_template.h"
41
42 /* array_cpuinfo_task_t */
43 #define ARRAY_T cpuinfo_task_t
44 #define ARRAY_KEY_T pid_t
45 #include "support/array_template.h"
46
47 typedef enum CPUStatus {
48 OWN = 0,
49 UNKNOWN = 1 << 0,
50 LENT = 1 << 1,
51 BORROWED = 1 << 2
52 } cpu_status_t;
53
54 typedef struct DLB_ALIGN_CACHE CPU_Data {
55 _Atomic(cpu_status_t) ownership;
56 bool fa;
57 } cpu_data_t;
58
59 enum {
60 PARALLEL_UNSET,
61 PARALLEL_LEVEL_1,
62 };
63
64 // This test needs at least room for 8 CPUs
65 enum { SYS_SIZE = 8 };
66
67 static int thread_id = 0;
68 3 int __kmp_get_thread_id(void) {
69 3 return thread_id;
70 }
71
72 /*int __kmp_get_num_free_agent_threads(void) {
73 return SYS_SIZE-1;
74 }*/
75 4 int __kmp_get_num_threads_role(ompt_role_t r){
76
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 if(r == OMP_ROLE_FREE_AGENT){
77 4 return atoi(getenv("KMP_FREE_AGENT_NUM_THREADS"));
78 }
79 ✗ return -1;
80 }
81
82 static bool free_agent_threads_status[SYS_SIZE];
83 /*void __kmp_set_free_agent_thread_active_status(
84 unsigned int thread_num, bool active) {
85 free_agent_threads_status[thread_num] = active;
86 }*/
87 14 void __kmp_set_thread_roles2(int tid, ompt_role_t r){
88
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 if(tid == SYS_SIZE) tid = thread_id;
89
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (r == OMP_ROLE_FREE_AGENT){
90 7 free_agent_threads_status[tid] = true;
91 }
92 else {
93 7 free_agent_threads_status[tid] = false;
94 }
95 14 }
96
97
98 1 int main (int argc, char *argv[]) {
99 1 mu_init();
100 1 mu_testing_set_sys_size(SYS_SIZE);
101
102 1 char options[64] = "--lewi --shm-key=";
103 1 strcat(options, SHMEM_KEY);
104
105 /* P1 needs pre-init + init */
106 1 char p1_options[64] = "--preinit-pid=111 ";
107 1 strcat(p1_options, options);
108 1 printf("p1_options: %s\n", p1_options);
109 1 printf("options: %s\n", options);
110 cpu_set_t p1_mask;
111 1 mu_parse_mask("0,1,4,5", &p1_mask);
112 1 spd_enter_dlb(NULL);
113 1 thread_spd->dlb_initialized = true;
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( PreInitialize(thread_spd, &p1_mask, p1_options) == DLB_SUCCESS );
115 1 pid_t p1_pid = 111;
116
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)
117 == DLB_SUCCESS );
118
119 /* P2 is fake and does nothing with OMPT, just init shmems */
120 cpu_set_t p2_mask;
121 1 mu_parse_mask("2,3,6,7", &p2_mask);
122 1 pid_t p2_pid = 222;
123
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,
124 thread_spd->options.lewi_color) == DLB_SUCCESS );
125
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,
126 thread_spd->options.shm_size_multiplier) == DLB_SUCCESS );
127
128 // Setup dummy priority CPUs
129 array_cpuid_t cpus_priority_array;
130 1 array_cpuid_t_init(&cpus_priority_array, SYS_SIZE);
131
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);
132
133 array_cpuinfo_task_t tasks;
134 1 array_cpuinfo_task_t_init(&tasks, SYS_SIZE);
135
136 1 int64_t last_borrow = 0;
137
138 /* P1 inits free agents module with OMP_NUM_THREADS=1 and *
139 * KMP_NUM_THREADS=8 */
140 {
141 1 setenv("OMP_NUM_THREADS", "1", 1);
142 1 setenv("KMP_FREE_AGENT_NUM_THREADS", "8", 1);
143 1 omptm_role_shift__init(p1_pid, &thread_spd->options);
144
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_free_agents() == 8 );
146
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_registered_threads() == 8 );
147
148 1 omptm_role_shift__finalize();
149 }
150 /* P1 inits free agents module with OMP_NUM_THREADS=8 and *
151 * KMP_NUM_THREADS=8 */
152 {
153 1 setenv("OMP_NUM_THREADS", "8", 1);
154 1 setenv("KMP_FREE_AGENT_NUM_THREADS", "8", 1);
155 1 omptm_role_shift__init(p1_pid, &thread_spd->options);
156
157
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_free_agents() == 8 );
158
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_registered_threads() == 8 );
159
160 1 omptm_role_shift__finalize();
161 }
162 /* P1 inits free agents module with OMP_NUM_THREADS=8 and *
163 * KMP_NUM_THREADS=4 */
164 {
165 1 setenv("OMP_NUM_THREADS", "8", 1);
166 1 setenv("KMP_FREE_AGENT_NUM_THREADS", "4", 1);
167 1 omptm_role_shift__init(p1_pid, &thread_spd->options);
168
169
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_free_agents() == 4 );
170
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_num_registered_threads() == 8 );
171
172 1 omptm_role_shift__finalize();
173 }
174 // Initialize thread status array to false. They have not been created yet.
175
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i=0; i<SYS_SIZE; ++i) {
176 8 free_agent_threads_status[i] = false;
177 }
178
179 // Assume OMP_NUM_THREADS=1 for the rest of the test
180 1 setenv("OMP_NUM_THREADS", "1", 1);
181 1 setenv("KMP_FREE_AGENT_NUM_THREADS", "4", 1);
182 1 omptm_role_shift__init(p1_pid, &thread_spd->options);
183
184 1 int *cpu_by_id = omptm_role_shift_testing__get_cpu_by_id_ptr();
185 1 cpu_data_t *cpu_data = omptm_role_shift_testing__get_cpu_data_ptr();
186
187 /* Emulate initial free agent threads creation based on env variables*/
188 {
189
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 for (int i=1; i<=3; ++i) {
190 3 thread_id = i;
191 3 omptm_role_shift__thread_begin(ompt_thread_other);
192 3 free_agent_threads_status[i] = true;
193 }
194 /* When using the OpenMP runtime properly, each thread will call the callback and
195 execute the pthread_getaffinity_np and obtain the unique CPU where the free agents
196 are binded. Since there's no runtime here, we modify the variables here */
197 1 cpu_by_id[1] = 1;
198 1 cpu_data[1].fa = true;
199 1 cpu_by_id[2] = 4;
200 1 cpu_data[4].fa = true;
201 1 cpu_by_id[3] = 5;
202 1 cpu_data[5].fa = true;
203
204
205 /* check all CPU ids */
206
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[0] == 0 );
207
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[1] == 1 );
208
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[2] == 4 );
209
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[3] == 5 );
210
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[4] == -1 );
211
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[5] == -1 );
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[6] == -1 );
213
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( cpu_by_id[7] == -1 );
214
215 /* check all ids from CPU */
216
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(0) == 0 );
217
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(1) == 1 );
218
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(2) == -1 );
219
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(3) == -1 );
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(4) == 2 );
221
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(5) == 3 );
222
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(6) == -1 );
223
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(7) == -1 );
224 }
225
226 /* Test the task_schedule function when there is no parallel region involved */
227 {
228
229 /* Execute 4 pending tasks. There should not be any pending task after that.
230 CPUs [2,3,6,7] should be inactive */
231 1 omptm_role_shift_testing__set_pending_tasks(4);
232 1 omptm_role_shift__task_switch();
233 1 omptm_role_shift__task_switch();
234 1 omptm_role_shift__task_switch();
235 1 omptm_role_shift__task_switch();
236
237
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_pending_tasks() == 0 );
238
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(0) == 0 );
239
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(1) == 1 );
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(2) == -1 );
241
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(3) == -1 );
242
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(4) == 2 );
243
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(5) == 3 );
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(6) == -1 );
245
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(7) == -1 );
246
247 /* The four tasks end their execution, one by each thread.
248 The process should retain the CPUs from the free agent threads
249 since it's the owner and the lewi policy is not aggressive */
250 1 omptm_role_shift_testing__set_global_tid(3);
251 1 omptm_role_shift__task_complete();
252 1 omptm_role_shift_testing__set_global_tid(2);
253 1 omptm_role_shift__task_complete();
254 1 omptm_role_shift_testing__set_global_tid(1);
255 1 omptm_role_shift__task_complete();
256 1 omptm_role_shift_testing__set_global_tid(0);
257 1 omptm_role_shift__task_complete();
258
259
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(0) == 0 );
260
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(1) == 1 );
261
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(2) == -1 );
262
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(3) == -1 );
263
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(4) == 2 );
264
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(5) == 3 );
265
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(6) == -1 );
266
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(7) == -1 );
267 }
268
269 /* Test the task_create callback. Creates 4 tasks, ensure that they
270 are counted, and check that no CPU is acquired. */
271 {
272 1 omptm_role_shift_testing__set_pending_tasks(0);
273
274 1 omptm_role_shift__task_create();
275
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_pending_tasks() == 1);
276 1 omptm_role_shift__task_create();
277
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_pending_tasks() == 2);
278 1 omptm_role_shift__task_create();
279
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_pending_tasks() == 3);
280 1 omptm_role_shift__task_create();
281
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_pending_tasks() == 4);
282
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(0) == 0 );
284
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(1) == 1 );
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(2) == -1 );
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(3) == -1 );
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(4) == 2 );
288
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(5) == 3 );
289
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(6) == -1 );
290
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( omptm_role_shift_testing__get_id_from_cpu(7) == -1 );
291
292 1 omptm_role_shift_testing__set_pending_tasks(0);
293 }
294
295 /* Check the parallel_begin and parallel_end callbacks. First, check that the
296 variables are updated correctly without nesting, and then under nested parallelism. */
297 {
298 1 omptool_parallel_data_t omptool_parallel_data = {
299 .level = 1,
300 .requested_parallelism = 4,
301 };
302 1 omptm_role_shift__parallel_begin(&omptool_parallel_data);
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__in_parallel());
304
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_current_parallel_size() == 4);
305
306 1 omptm_role_shift__parallel_end(&omptool_parallel_data);
307
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(!omptm_role_shift_testing__in_parallel());
308
309 //Testing a nested parallel
310 1 omptm_role_shift__parallel_begin(&omptool_parallel_data);
311
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__in_parallel());
312
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_current_parallel_size() == 4);
313
314 1 omptool_parallel_data_t omptool_parallel_data_level2 = {
315 .level = 2,
316 .requested_parallelism = 2,
317 };
318 1 omptm_role_shift__parallel_begin(&omptool_parallel_data_level2);
319
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__in_parallel());
320 // nested parallel is actually ignored, size is of level 1
321
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_current_parallel_size() == 4);
322
323 1 omptm_role_shift__parallel_end(&omptool_parallel_data_level2);
324
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__in_parallel());
325
326 1 omptm_role_shift__parallel_end(&omptool_parallel_data);
327
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(!omptm_role_shift_testing__in_parallel());
328 }
329
330 //Check basic actions of thread_role_shift callback
331 {
332 1 int global_tid = 2;
333 1 omptm_role_shift_testing__set_global_tid(global_tid);
334
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_by_id[global_tid] == 4);
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[4].ownership == OWN);
336
337 //FA -> NONE transition, nothing should happen
338 1 omptm_role_shift__thread_role_shift(NULL,
339 OMP_ROLE_FREE_AGENT, OMP_ROLE_NONE);
340
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_by_id[global_tid] == 4);
341
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[4].ownership == OWN);
342
343 //NONE -> FA transition. Nothing should happen because the process owns the CPU
344 1 omptm_role_shift_testing__set_pending_tasks(0);
345 1 omptm_role_shift__thread_role_shift(NULL,
346 OMP_ROLE_NONE, OMP_ROLE_FREE_AGENT);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_by_id[global_tid] == 4);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[4].ownership == OWN);
349 }
350
351 /* Test IntoBlockingCall and OutOfBlockingCall */
352 {
353 /* P1 is outside a parallel region, and invokes IntoBlockingCall */
354 1 omptm_role_shift_testing__set_global_tid(0);
355 1 omptm_role_shift__IntoBlockingCall();
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[1]);
357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[2]);
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[3]);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[0].ownership == LENT);
360
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[1].ownership == LENT);
361
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[4].ownership == LENT);
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[5].ownership == LENT);
363
364 /* P2 asks for all the CPUs, it should success (CPU 0 is not lent) */
365 //TODO: Not sure about CPU 0
366
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__borrow_ncpus_from_cpu_subset(p2_pid, NULL,
367 &cpus_priority_array, LEWI_AFFINITY_AUTO, 0,
368 &last_borrow, &tasks) == DLB_SUCCESS );
369
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 4 );
370
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
371 && tasks.items[0].cpuid == 0
372 && tasks.items[0].action == ENABLE_CPU );
373
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
374 && tasks.items[1].cpuid == 1
375 && tasks.items[1].action == ENABLE_CPU );
376
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
377 && tasks.items[2].cpuid == 4
378 && tasks.items[2].action == ENABLE_CPU );
379
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 == p2_pid
380 && tasks.items[3].cpuid == 5
381 && tasks.items[3].action == ENABLE_CPU );
382 1 array_cpuinfo_task_t_clear(&tasks);
383
384 /* P2 returns P1's CPUs */
385
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 );
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 0 );
387
388 /* P1 invokes OutOfBlockingCall */
389 1 omptm_role_shift__OutOfBlockingCall();
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[1]);
391
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[2]);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[3]);
393
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[0].ownership == OWN);
394
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[1].ownership == OWN);
395
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[4].ownership == OWN);
396
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[5].ownership == OWN);
397 }
398
399 /* P2 lends all CPUs. P1 creates tasks and executes them. The CPUs should be used */
400 {
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == UNKNOWN);
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == UNKNOWN);
404
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == UNKNOWN);
405
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 );
406 1 omptm_role_shift_testing__set_pending_tasks(0);
407
408 //Create two tasks. Free agents should be activated in CPUs 2 and 3
409 1 thread_id = 4;
410 1 omptm_role_shift__task_create();
411
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
412
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
413
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == UNKNOWN);
414
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == UNKNOWN);
415
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
416
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[5]);
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[6]);
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[7]);
419
420 1 thread_id = 5;
421 1 omptm_role_shift__task_create();
422
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
423
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == BORROWED);
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == UNKNOWN);
425
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == UNKNOWN);
426
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
427
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[5]);
428
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[6]);
429
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[7]);
430
431
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert(omptm_role_shift_testing__get_pending_tasks() == 2);
432 1 omptm_role_shift_testing__set_pending_tasks(10);
433
434 //Schdeule 2 tasks. Two free agents should be created in CPUs 6 and 7
435
436 1 thread_id = 6;
437 1 omptm_role_shift__task_switch();
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == BORROWED);
440
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == BORROWED);
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
442
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[5]);
443
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[6]);
444
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[7]);
445
446 1 thread_id = 7;
447 1 omptm_role_shift__task_switch();
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
449
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == BORROWED);
450
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == BORROWED);
451
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == BORROWED);
452
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[5]);
454
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[6]);
455
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[7]);
456
457 //Thread 5 finishes a task and there are some pending tasks.
458 //The status of the threads should not change.
459 1 omptm_role_shift_testing__set_global_tid(5);
460 //Update CPU data manually since thread_begin is not really called for this thread
461 1 cpu_data[cpu_by_id[4]].fa = true;
462 1 cpu_data[cpu_by_id[5]].fa = true;
463 1 cpu_data[cpu_by_id[6]].fa = true;
464 1 cpu_data[cpu_by_id[7]].fa = true;
465 1 omptm_role_shift__task_complete();
466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
467
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == BORROWED);
468
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == BORROWED);
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == BORROWED);
470
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
471
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[5]);
472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[6]);
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[7]);
474
475 1 omptm_role_shift_testing__set_pending_tasks(0);
476 //A task is finished an there are no pending tasks.
477 //The CPU should be lend to DLB and the state updated
478 1 omptm_role_shift__task_complete();
479
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == BORROWED);
480
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == BORROWED);
482
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == BORROWED);
483
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[4]);
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[5]);
485
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[6]);
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[7]);
487
488 //P2 reclaims its process mask. CPU 3 can be immediately reclaimed
489
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__reclaim_all(p2_pid, &tasks) == DLB_NOTED );
490
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert( tasks.count == 7 );
491
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 == p1_pid
492 && tasks.items[0].cpuid == 2
493 && tasks.items[0].action == DISABLE_CPU );
494
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
495 && tasks.items[1].cpuid == 2
496 && tasks.items[1].action == ENABLE_CPU );
497
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
498 && tasks.items[2].cpuid == 3
499 && tasks.items[2].action == ENABLE_CPU );
500
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
501 && tasks.items[3].cpuid == 6
502 && tasks.items[3].action == DISABLE_CPU );
503
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
504 && tasks.items[4].cpuid == 6
505 && tasks.items[4].action == ENABLE_CPU );
506
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
507 && tasks.items[5].cpuid == 7
508 && tasks.items[5].action == DISABLE_CPU );
509
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
510 && tasks.items[6].cpuid == 7
511 && tasks.items[6].action == ENABLE_CPU );
512 1 array_cpuinfo_task_t_clear(&tasks);
513
514 //Threads 4, 6, and 7 (Free agents) finish a task.
515 //The CPUs should be disabled and the threads change their role
516 1 omptm_role_shift_testing__set_global_tid(4);
517 1 omptm_role_shift__task_complete();
518
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == UNKNOWN);
519
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == BORROWED);
521
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == BORROWED);
522
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[4]);
523
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[5]);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[6]);
525
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[7]);
526
527 1 omptm_role_shift_testing__set_global_tid(6);
528 1 omptm_role_shift__task_complete();
529
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == UNKNOWN);
530
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == UNKNOWN);
532
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == BORROWED);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[4]);
534
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[5]);
535
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[6]);
536
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(free_agent_threads_status[7]);
537
538 1 omptm_role_shift_testing__set_global_tid(7);
539 1 omptm_role_shift__task_complete();
540
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[2].ownership == UNKNOWN);
541
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[3].ownership == UNKNOWN);
542
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[6].ownership == UNKNOWN);
543
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(cpu_data[7].ownership == UNKNOWN);
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[4]);
545
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[5]);
546
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[6]);
547
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 assert(!free_agent_threads_status[7]);
548 }
549
550 /* Finalize */
551
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( Finish(thread_spd) == DLB_SUCCESS );
552 1 omptm_role_shift__finalize();
553
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_cpuinfo__finalize(p2_pid, thread_spd->options.shm_key,
554 thread_spd->options.lewi_color) == DLB_SUCCESS );
555
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( shmem_procinfo__finalize(p2_pid, false, thread_spd->options.shm_key,
556 thread_spd->options.shm_size_multiplier) == DLB_SUCCESS );
557
558 1 mu_finalize();
559
560 1 return 0;
561 }
562