GCC Code Coverage Report


Directory: ./
File: tests/unit/03_policies/lewi_mask_02.c
Date: 2026-12-01 10:07:21
Exec Total Coverage
Lines: 62 64 96.9%
Functions: 7 8 87.5%
Branches: 27 56 48.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 "apis/dlb_errors.h"
23 #include "LB_core/spd.h"
24 #include "LB_policies/lewi_mask.h"
25 #include "LB_comm/shmem_procinfo.h"
26 #include "LB_comm/shmem_cpuinfo.h"
27 #include "LB_comm/shmem_async.h"
28 #include "LB_numThreads/numThreads.h"
29 #include "support/mask_utils.h"
30 #include "support/debug.h"
31
32 #include <sched.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <string.h>
36 #include <stdlib.h>
37
38
39 /* Test race conditions between lewi requests and async helpers */
40
41 static subprocess_descriptor_t spd1;
42 static subprocess_descriptor_t spd2;
43 static cpu_set_t sp1_mask;
44 static cpu_set_t sp2_mask;
45
46 2 static void wait_for_async_completion(const subprocess_descriptor_t *spd) {
47 2 shmem_async_wait_for_completion(spd->id);
48 2 __sync_synchronize();
49 2 }
50
51 /* Subprocess 1 callbacks */
52 1 static void sp1_cb_enable_cpu(int cpuid, void *arg) {
53
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_SET(cpuid, &sp1_mask);
54 1 }
55
56 ✗ static void sp1_cb_disable_cpu(int cpuid, void *arg) {
57 ✗ CPU_CLR(cpuid, &sp1_mask);
58 }
59
60 /* Subprocess 2 callbacks */
61 2 static void sp2_cb_enable_cpu(int cpuid, void *arg) {
62
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CPU_SET(cpuid, &sp2_mask);
63 2 }
64
65 1 static void sp2_cb_disable_cpu(int cpuid, void *arg) {
66
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_CLR(cpuid, &sp2_mask);
67 1 }
68
69 4 static void init_subprocess(subprocess_descriptor_t *spd, cpu_set_t *sp_mask,
70 const cpu_set_t *process_mask, dlb_callback_t cb_enable, dlb_callback_t cb_disable) {
71 static int id = 100;
72 4 ++id;
73
74 // Initialize subprocess mask
75 4 memcpy(sp_mask, process_mask, sizeof(cpu_set_t));
76
77 // Options
78 4 char options[64] = "--verbose=shmem --mode=async --shm-key=";
79 4 strcat(options, SHMEM_KEY);
80
81 // Subprocess init
82 4 spd->id = id;
83 4 options_init(&spd->options, options);
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 assert( spd->options.mode == MODE_ASYNC );
85 4 debug_init(&spd->options);
86 4 memcpy(&spd->process_mask, sp_mask, sizeof(cpu_set_t));
87
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_procinfo__init(spd->id, 0, &spd->process_mask, NULL, spd->options.shm_key,
88 spd->options.shm_size_multiplier) == DLB_SUCCESS );
89
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_cpuinfo__init(spd->id, 0, &spd->process_mask, spd->options.shm_key,
90 spd->options.lewi_color) == DLB_SUCCESS );
91
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_async_init(spd->id, &spd->pm, &spd->process_mask,
92 spd->options.shm_key, spd->options.shm_size_multiplier) == DLB_SUCCESS );
93
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( pm_callback_set(&spd->pm, dlb_callback_enable_cpu, cb_enable, NULL) == DLB_SUCCESS );
94
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( pm_callback_set(&spd->pm, dlb_callback_disable_cpu, cb_disable, NULL) == DLB_SUCCESS );
95
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( lewi_mask_Init(spd) == DLB_SUCCESS );
96 4 }
97
98 4 static void finalize_subprocess(subprocess_descriptor_t *spd) {
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( lewi_mask_Finalize(spd) == DLB_SUCCESS );
100
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_cpuinfo__finalize(spd->id, spd->options.shm_key, spd->options.lewi_color)
101 == DLB_SUCCESS );
102
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_procinfo__finalize(spd->id, false, spd->options.shm_key,
103 spd->options.shm_size_multiplier) == DLB_SUCCESS );
104
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 assert( shmem_async_finalize(spd->id) == DLB_SUCCESS );
105 4 }
106
107 1 int main( int argc, char **argv ) {
108 // This test needs at least room for 4 CPUs
109 enum { SYS_SIZE = 4 };
110 1 mu_init();
111 1 mu_testing_set_sys_size(SYS_SIZE);
112
113 // Initialize constant masks for fast reference
114 1 const cpu_set_t sp1_process_mask = {.__bits={0x3}}; /* [0011] */
115 1 const cpu_set_t sp2_process_mask = {.__bits={0xc}}; /* [1100] */
116
117 // Sp1 finalizes with pending requests
118 {
119 // Init
120 1 init_subprocess(&spd1, &sp1_mask, &sp1_process_mask,
121 (dlb_callback_t)sp1_cb_enable_cpu, (dlb_callback_t)sp1_cb_disable_cpu);
122 1 init_subprocess(&spd2, &sp2_mask, &sp2_process_mask,
123 (dlb_callback_t)sp2_cb_enable_cpu, (dlb_callback_t)sp2_cb_disable_cpu);
124
125 // Subprocess 1 wants to acquire CPU 3
126
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_AcquireCpu(&spd1, 3) == DLB_NOTED );
127
128 // Subprocess 1 finalizes
129 1 finalize_subprocess(&spd1);
130
131 // Subprocess 2 no longer needs CPU 3
132
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_CLR(3, &sp2_mask);
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_LendCpu(&spd2, 3) == DLB_SUCCESS );
134
135 // Subprocess 2 finalizes
136 1 finalize_subprocess(&spd2);
137 }
138
139 // Sp1 finalizes with reclaimed CPUs
140 {
141 // Init
142 1 init_subprocess(&spd1, &sp1_mask, &sp1_process_mask,
143 (dlb_callback_t)sp1_cb_enable_cpu, (dlb_callback_t)sp1_cb_disable_cpu);
144 1 init_subprocess(&spd2, &sp2_mask, &sp2_process_mask,
145 (dlb_callback_t)sp2_cb_enable_cpu, (dlb_callback_t)sp2_cb_disable_cpu);
146
147 // Subprocess 1 lends CPU 1
148
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CPU_CLR(1, &sp1_mask);
149
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_LendCpu(&spd1, 1) == DLB_SUCCESS );
150
151 // Subprocess 2 wants to acquire CPU 1
152
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_AcquireCpu(&spd2, 1) == DLB_SUCCESS );
153 1 wait_for_async_completion(&spd2);
154
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 assert( CPU_ISSET(1, &sp2_mask) );
155
156 // Subprocess 1 reclaims CPU 1 and finalizes immediately
157 1 finalize_subprocess(&spd1);
158
159 // Subprocess 2 gets CPU 1 removed and can only add new request
160 1 wait_for_async_completion(&spd2);
161
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
1 assert( !CPU_ISSET(1, &sp2_mask) );
162
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 assert( lewi_mask_AcquireCpu(&spd2, 1) == DLB_NOTED );
163
164 // Subprocess 2 finalizes
165 1 finalize_subprocess(&spd2);
166 }
167
168 1 mu_finalize();
169
170 1 return 0;
171 }
172