GCC Code Coverage Report


Directory: src/
File: src/LB_policies/lewi.c
Date: 2025-11-21 10:34:40
Exec Total Coverage
Lines: 72 85 84.7%
Functions: 12 13 92.3%
Branches: 29 46 63.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
21 #include "LB_policies/lewi.h"
22
23 #include "LB_core/spd.h"
24 #include "apis/dlb_errors.h"
25 #include "LB_comm/comm_lend_light.h"
26 #include "LB_numThreads/numThreads.h"
27 #include "support/mask_utils.h"
28 #include "support/options.h"
29 #include "support/debug.h"
30 #include "support/types.h"
31
32 #include <sched.h>
33 #include <limits.h>
34
35
36 static int default_cpus;
37 static int myCPUS = 0;
38 static int enabled = 0;
39 static int single = 0;
40 static int max_parallelism = 0;
41 static void setThreads_Lend_light(const pm_interface_t *pm, int numThreads);
42
43 /******* Main Functions LeWI Balancing Policy ********/
44
45 28 int lewi_Init(subprocess_descriptor_t *spd) {
46
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 27 times.
28 verbose(VB_MICROLB, "LeWI Init");
47
48 28 default_cpus = spd->lewi_ncpus;
49
50 28 setThreads_Lend_light(&spd->pm, default_cpus);
51
52 28 info0("Default cpus per process: %d", default_cpus);
53
54 28 bool greedy = spd->options.lewi_greedy;
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (greedy) {
56 info0("Policy mode GREEDY");
57 }
58
59 //Initialize shared memory
60 28 ConfigShMem(default_cpus, greedy, spd->options.shm_key);
61
62
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 28 times.
28 if (spd->options.lewi_warmup) {
63 setThreads_Lend_light(&spd->pm, mu_get_system_size());
64 setThreads_Lend_light(&spd->pm, default_cpus);
65 }
66
67 28 enabled = 1;
68
69 28 return DLB_SUCCESS;
70 }
71
72 28 int lewi_Finalize(subprocess_descriptor_t *spd) {
73 28 finalize_comm();
74 28 return DLB_SUCCESS;
75 }
76
77 1 int lewi_EnableDLB(const subprocess_descriptor_t *spd) {
78 1 single = 0;
79 1 enabled = 1;
80 1 return DLB_SUCCESS;
81 }
82
83 1 int lewi_DisableDLB(const subprocess_descriptor_t *spd) {
84
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (enabled && !single) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 verbose(VB_MICROLB, "ResetDLB");
86 1 acquireCpus(myCPUS);
87 1 setThreads_Lend_light(&spd->pm, default_cpus);
88 }
89 1 enabled = 0;
90 1 return DLB_SUCCESS;
91 }
92
93 2 int lewi_SetMaxParallelism(const subprocess_descriptor_t *spd, int max) {
94
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (myCPUS>max){
95 releaseCpus(myCPUS-max);
96 setThreads_Lend_light(&spd->pm, max);
97 }
98 2 max_parallelism = max;
99 2 return DLB_SUCCESS;
100 }
101
102 int lewi_UnsetMaxParallelism(const subprocess_descriptor_t *spd) {
103 max_parallelism = 0;
104 return DLB_SUCCESS;
105 }
106
107 9 int lewi_IntoBlockingCall(const subprocess_descriptor_t *spd) {
108
109
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (enabled) {
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if ( spd->options.lewi_keep_cpu_on_blocking_call ) {
111 /* 1CPU */
112 verbose(VB_MICROLB, "LENDING %d cpus", myCPUS-1);
113 releaseCpus(myCPUS-1);
114 setThreads_Lend_light(&spd->pm, 1);
115 } else {
116 /* BLOCK */
117
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 verbose(VB_MICROLB, "LENDING %d cpus", myCPUS);
118 9 releaseCpus(myCPUS);
119 9 setThreads_Lend_light(&spd->pm, 0);
120 }
121 }
122 9 return DLB_SUCCESS;
123 }
124
125 9 int lewi_OutOfBlockingCall(const subprocess_descriptor_t *spd) {
126
127
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (enabled) {
128 int cpus;
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (single) {
130 cpus = acquireCpus(1);
131 } else {
132 9 cpus = acquireCpus(myCPUS);
133 }
134 9 setThreads_Lend_light(&spd->pm, cpus);
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
9 verbose(VB_MICROLB, "ACQUIRING %d cpus", cpus);
136 }
137 9 return DLB_SUCCESS;
138 }
139
140 4 int lewi_Lend(const subprocess_descriptor_t *spd) {
141
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 verbose(VB_MICROLB, "LENDING %d cpus", myCPUS-1);
142 4 releaseCpus(myCPUS-1);
143 4 setThreads_Lend_light(&spd->pm, 1);
144 4 return DLB_SUCCESS;
145 }
146
147 2 int lewi_Reclaim(const subprocess_descriptor_t *spd) {
148 2 int cpus = acquireCpus(myCPUS);
149 2 setThreads_Lend_light(&spd->pm, cpus);
150
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 verbose(VB_MICROLB, "ACQUIRING %d cpus", cpus);
151 2 return DLB_SUCCESS;
152 }
153
154 2 int lewi_Borrow(const subprocess_descriptor_t *spd) {
155 2 return lewi_BorrowCpus(spd, INT_MAX);
156 }
157
158 10 int lewi_BorrowCpus(const subprocess_descriptor_t *spd, int maxResources) {
159 10 int error = DLB_NOUPDT;
160
2/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
20 if (enabled && !single) {
161 20 int max_resources = max_parallelism > 0 ?
162
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
10 min_int(maxResources, max_parallelism - myCPUS) : maxResources;
163 10 int cpus = checkIdleCpus(myCPUS, max_resources);
164
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (myCPUS!=cpus) {
165
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 verbose(VB_MICROLB, "Using %d cpus", cpus);
166 5 setThreads_Lend_light(&spd->pm, cpus);
167 5 error = DLB_SUCCESS;
168 }
169 } else {
170 error = DLB_ERR_DISBLD;
171 }
172 10 return error;
173 }
174
175 /******* Auxiliar Functions LeWI Balancing Policy ********/
176 58 static void setThreads_Lend_light(const pm_interface_t *pm, int numThreads) {
177
178
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 26 times.
58 if (myCPUS!=numThreads) {
179
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 18 times.
32 verbose(VB_MICROLB, "Using %d cpus", numThreads);
180 32 update_threads(pm, numThreads);
181 32 myCPUS=numThreads;
182 }
183 58 }
184