Debian Codegraph @

src/shared/cpu-set-util.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <syslog.h>
5 #include <unistd.h>
6
7 #include "alloc-util.h"
8 #include "bitfield.h"
9 #include "cgroup-util.h"
10 #include "cpu-set-util.h"
11 #include "extract-word.h"
12 #include "log.h"
13 #include "parse-util.h"
14 #include "string-util.h"
15
16 /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */
17 #define CPU_SET_MAX_NCPU 8192
18
19 char* cpu_set_to_string(const CPUSet *c) {
20 _cleanup_free_ char *str = NULL;
21
22 assert(c);
23
24 for (size_t i = 0; i < c->allocated * 8; i++) {
25 if (!CPU_ISSET_S(i, c->allocated, c->set))
26 continue;
27
28 if (strextendf_with_separator(&str, " ", "%zu", i) < 0)
29 return NULL;
30 }
31
32 return TAKE_PTR(str) ?: strdup("");
33 }
34
35 static int add_range(char **str, size_t start, size_t end) {
36 assert(str);
37 assert(start <= end);
38
39 if (start == end)
40 return strextendf_with_separator(str, " ", "%zu", start);
41
42 return strextendf_with_separator(str, " ", "%zu-%zu", start, end);
43 }
44
45 char* cpu_set_to_range_string(const CPUSet *c) {
46 _cleanup_free_ char *str = NULL;
47 size_t start = 0, end;
48 bool in_range = false;
49
50 assert(c);
51
52 for (size_t i = 0; i < c->allocated * 8; i++) {
53 if (CPU_ISSET_S(i, c->allocated, c->set)) {
54 if (in_range)
55 end++;
56 else {
57 start = end = i;
58 in_range = true;
59 }
60 continue;
61 }
62
63 if (in_range && add_range(&str, start, end) < 0)
64 return NULL;
65
66 in_range = false;
67 }
68
69 if (in_range && add_range(&str, start, end) < 0)
70 return NULL;
71
72 return TAKE_PTR(str) ?: strdup("");
73 }
74
75 char* cpu_set_to_mask_string(const CPUSet *c) {
76 _cleanup_free_ char *str = NULL;
77 bool found_nonzero = false;
78 int r;
79
80 assert(c);
81
82 /* Return CPU set in hexadecimal bitmap mask, e.g.
83 * CPU 0 -> "1"
84 * CPU 1 -> "2"
85 * CPU 0,1 -> "3"
86 * CPU 0-3 -> "f"
87 * CPU 0-7 -> "ff"
88 * CPU 4-7 -> "f0"
89 * CPU 7 -> "80"
90 * None -> "0"
91 *
92 * When there are more than 32 CPUs, separate every 32 CPUs by comma, e.g.
93 * CPU 0-47 -> "ffff,ffffffff"
94 * CPU 0-63 -> "ffffffff,ffffffff"
95 * CPU 0-71 -> "ff,ffffffff,ffffffff" */
96
97 for (size_t i = c->allocated * 8; i > 0; ) {
98 uint32_t m = 0;
99
100 for (int j = (i % 32 ?: 32) - 1; j >= 0; j--) {
101 --i;
102 if (CPU_ISSET_S(i, c->allocated, c->set))
103 SET_BIT(m, j);
104 }
105
106 if (!found_nonzero) {
107 if (m == 0)
108 continue;
109
110 r = strextendf_with_separator(&str, ",", "%" PRIx32, m);
111 } else
112 r = strextendf_with_separator(&str, ",", "%08" PRIx32, m);
113 if (r < 0)
114 return NULL;
115
116 found_nonzero = true;
117 }
118
119 return TAKE_PTR(str) ?: strdup("0");
120 }
121
122 void cpu_set_done(CPUSet *c) {
123 assert(c);
124
125 if (c->set)
126 CPU_FREE(c->set);
127
128 *c = (CPUSet) {};
129 }
130
131 int cpu_set_realloc(CPUSet *c, size_t n) {
132 assert(c);
133
134 if (n > CPU_SET_MAX_NCPU)
135 return -ERANGE;
136
137 n = CPU_ALLOC_SIZE(n);
138 if (n <= c->allocated) {
139 assert(c->set || n == 0);
140 return 0;
141 }
142
143 if (!GREEDY_REALLOC0(c->set, DIV_ROUND_UP(n, sizeof(cpu_set_t))))
144 return -ENOMEM;
145
146 c->allocated = n;
147 return 0;
148 }
149
150 int cpu_set_add(CPUSet *c, size_t i) {
151 int r;
152
153 assert(c);
154
155 /* cpu_set_realloc() has similar check, but for avoiding overflow. */
156 if (i >= CPU_SET_MAX_NCPU)
157 return -ERANGE;
158
159 r = cpu_set_realloc(c, i + 1);
160 if (r < 0)
161 return r;
162
163 /* Silence static analyzers */
164 assert(i / CHAR_BIT < c->allocated);
165 CPU_SET_S(i, c->allocated, c->set);
166 return 0;
167 }
168
169 int cpu_set_add_set(CPUSet *c, const CPUSet *src) {
170 int r;
171
172 assert(c);
173 assert(src);
174
175 r = cpu_set_realloc(c, src->allocated * 8);
176 if (r < 0)
177 return r;
178
179 for (size_t i = 0; i < src->allocated * 8; i++)
180 if (CPU_ISSET_S(i, src->allocated, src->set))
181 CPU_SET_S(i, c->allocated, c->set);
182
183 return 1;
184 }
185
186 int cpu_set_add_range(CPUSet *c, size_t start, size_t end) {
187 int r;
188
189 assert(c);
190 assert(start <= end);
191
192 /* cpu_set_realloc() has similar check, but for avoiding overflow. */
193 if (end >= CPU_SET_MAX_NCPU)
194 return -ERANGE;
195
196 r = cpu_set_realloc(c, end + 1);
197 if (r < 0)
198 return r;
199
200 /* Silence static analyzers */
201 assert(end / CHAR_BIT < c->allocated);
202 for (size_t i = start; i <= end; i++)
203 CPU_SET_S(i, c->allocated, c->set);
204
205 return 0;
206 }
207
208 int cpu_set_add_all(CPUSet *c) {
209 assert(c);
210
211 long m = sysconf(_SC_NPROCESSORS_ONLN);
212 if (m < 0)
213 return -errno;
214 if (m == 0)
215 return -ENXIO;
216
217 return cpu_set_add_range(c, 0, m - 1);
218 }
219
220 int config_parse_cpu_set(
221 const char *unit,
222 const char *filename,
223 unsigned line,
224 const char *section,
225 unsigned section_line,
226 const char *lvalue,
227 int ltype, /* 0 when used as conf parser, 1 when used as usual parser */
228 const char *rvalue,
229 void *data,
230 void *userdata) {
231
232 CPUSet *c = ASSERT_PTR(data);
233 int r, level = ltype ? LOG_DEBUG : LOG_ERR;
234 bool critical = ltype;
235
236 assert(critical || lvalue);
237
238 if (isempty(rvalue)) {
239 cpu_set_done(c);
240 return 1;
241 }
242
243 _cleanup_(cpu_set_done) CPUSet cpuset = {};
244 for (const char *p = rvalue;;) {
245 _cleanup_free_ char *word = NULL;
246
247 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_UNQUOTE);
248 if (r == -ENOMEM)
249 return log_oom_full(level);
250 if (r < 0) {
251 if (critical)
252 return log_debug_errno(r, "Failed to parse CPU set: %s", rvalue);
253
254 log_syntax(unit, LOG_WARNING, filename, line, r, "Failed to parse %s= setting, ignoring assignment: %s",
255 lvalue, rvalue);
256 return 0;
257 }
258 if (r == 0)
259 break;
260
261 unsigned lower, upper;
262 r = parse_range(word, &lower, &upper);
263 if (r < 0) {
264 if (critical)
265 return log_debug_errno(r, "Failed to parse CPU range: %s", word);
266
267 log_syntax(unit, LOG_WARNING, filename, line, r,
268 "Failed to parse CPU range, ignoring assignment: %s", word);
269 continue;
270 }
271
272 if (lower > upper) {
273 if (critical)
274 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
275 "Invalid CPU range (%u > %u): %s",
276 lower, upper, word);
277
278 log_syntax(unit, LOG_WARNING, filename, line, r,
279 "Invalid CPU range (%u > %u), ignoring assignment: %s",
280 lower, upper, word);
281 continue;
282 }
283
284 r = cpu_set_add_range(&cpuset, lower, upper);
285 if (r == -ENOMEM)
286 return log_oom_full(level);
287 if (r < 0) {
288 if (critical)
289 return log_debug_errno(r, "Failed to set CPU(s) '%s': %m", word);
290
291 log_syntax(unit, LOG_WARNING, filename, line, r,
292 "Failed to set CPU(s), ignoring assignment: %s", word);
293 }
294 }
295
296 if (!c->set) {
297 *c = TAKE_STRUCT(cpuset);
298 return 1;
299 }
300
301 r = cpu_set_add_set(c, &cpuset);
302 if (r == -ENOMEM)
303 return log_oom_full(level);
304 assert(r >= 0);
305
306 return 1;
307 }
308
309 int parse_cpu_set(const char *s, CPUSet *ret) {
310 _cleanup_(cpu_set_done) CPUSet c = {};
311 int r;
312
313 assert(s);
314 assert(ret);
315
316 r = config_parse_cpu_set(
317 /* unit= */ NULL,
318 /* filename= */ NULL,
319 /* line= */ 0,
320 /* section= */ NULL,
321 /* section_line= */ 0,
322 /* lvalue= */ NULL,
323 /* ltype= */ 1,
324 /* rvalue= */ s,
325 /* data= */ &c,
326 /* userdata= */ NULL);
327 if (r < 0)
328 return r;
329
330 *ret = TAKE_STRUCT(c);
331 return 0;
332 }
333
334 int cpus_in_affinity_mask(void) {
335 size_t n = 16;
336 int r;
337
338 for (;;) {
339 cpu_set_t *c;
340
341 c = CPU_ALLOC(n);
342 if (!c)
343 return -ENOMEM;
344
345 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
346 int k;
347
348 k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
349 CPU_FREE(c);
350
351 if (k <= 0)
352 return -EINVAL;
353
354 return k;
355 }
356
357 r = -errno;
358 CPU_FREE(c);
359
360 if (r != -EINVAL)
361 return r;
362 if (n > SIZE_MAX/2)
363 return -ENOMEM;
364 n *= 2;
365 }
366 }
367
368 int cpu_set_to_dbus(const CPUSet *c, uint8_t **ret, size_t *ret_size) {
369 assert(c);
370 assert(ret);
371 assert(ret_size);
372
373 uint8_t *buf = new0(uint8_t, c->allocated);
374 if (!buf)
375 return -ENOMEM;
376
377 for (size_t i = 0; i < c->allocated * 8; i++)
378 if (CPU_ISSET_S(i, c->allocated, c->set))
379 SET_BIT(buf[i / 8], i % 8);
380
381 *ret = buf;
382 *ret_size = c->allocated;
383 return 0;
384 }
385
386 int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *ret) {
387 _cleanup_(cpu_set_done) CPUSet c = {};
388 int r;
389
390 assert(bits || size == 0);
391 assert(ret);
392
393 r = cpu_set_realloc(&c, size * 8);
394 if (r < 0)
395 return r;
396
397 for (size_t i = 0; i < size * 8; i++)
398 if (BIT_SET(bits[i / 8], i % 8))
399 CPU_SET_S(i, c.allocated, c.set);
400
401 *ret = TAKE_STRUCT(c);
402 return 0;
403 }
404
405 static int cgroup_cpus_effective(unsigned *ret) {
406 int r;
407
408 assert(ret);
409
410 _cleanup_free_ char *root = NULL;
411 r = cg_get_root_path(&root);
412 if (r < 0)
413 return log_debug_errno(r, "Failed to determine root cgroup: %m");
414
415 _cleanup_free_ char *value = NULL;
416 r = cg_get_attribute(root, "cpuset.cpus.effective", &value);
417 if (r < 0)
418 return log_debug_errno(r, "Failed to read cpuset.cpus.effective cgroup attribute: %m");
419
420 _cleanup_(cpu_set_done) CPUSet cpus = {};
421 r = parse_cpu_set(value, &cpus);
422 if (r < 0)
423 return log_debug_errno(r, "Failed to parse cpuset.cpus.effective cgroup attribute: %m");
424
425 *ret = (unsigned) MIN(cpu_set_count(&cpus), UINT_MAX);
426 return 0;
427 }
428
429 int cpus_online(unsigned *ret) {
430 int r;
431
432 assert(ret);
433
434 /* In order to support containers nicely that have a configured cpuset we'll take the minimum of the
435 * physically reported amount of CPUs and the limit configured for the root cgroup, if there is
436 * any. */
437
438 long sc = sysconf(_SC_NPROCESSORS_ONLN);
439 if (sc < 0)
440 return log_debug_errno(errno, "sysconf(_SC_NPROCESSORS_ONLN) failed: %m");
441
442 unsigned cg, lc = (unsigned) CLAMP((unsigned long) sc, 1U, UINT_MAX);
443 r = cgroup_cpus_effective(&cg);
444 if (r < 0)
445 *ret = lc;
446 else
447 *ret = CLAMP(cg, 1U, lc);
448
449 return 0;
450 }