]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/cpu-set-util.c
systemctl: present CPUAffinity mask as a list of CPU index ranges
[thirdparty/systemd.git] / src / shared / cpu-set-util.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
618234a5 2
11c3a366
TA
3#include <errno.h>
4#include <stddef.h>
a832893f 5#include <stdio.h>
11c3a366
TA
6#include <syslog.h>
7
b5efdb8a 8#include "alloc-util.h"
6bedfcbb 9#include "cpu-set-util.h"
84ac7bea 10#include "extract-word.h"
11c3a366
TA
11#include "log.h"
12#include "macro.h"
0985c7c4 13#include "memory-util.h"
93cc7779 14#include "parse-util.h"
b11d6a7b 15#include "string-util.h"
618234a5 16
0985c7c4 17char* cpu_set_to_string(const CPUSet *a) {
a832893f
ZJS
18 _cleanup_free_ char *str = NULL;
19 size_t allocated = 0, len = 0;
20 int i, r;
21
0985c7c4
ZJS
22 for (i = 0; (size_t) i < a->allocated * 8; i++) {
23 if (!CPU_ISSET_S(i, a->allocated, a->set))
a832893f
ZJS
24 continue;
25
26 if (!GREEDY_REALLOC(str, allocated, len + 1 + DECIMAL_STR_MAX(int)))
27 return NULL;
28
29 r = sprintf(str + len, len > 0 ? " %d" : "%d", i);
30 assert_se(r > 0);
31 len += r;
32 }
33
34 return TAKE_PTR(str) ?: strdup("");
35}
36
71b28519
MS
37char *cpu_set_to_range_string(const CPUSet *set) {
38 unsigned range_start = 0, range_end;
39 _cleanup_free_ char *str = NULL;
40 size_t allocated = 0, len = 0;
41 bool in_range = false;
42 int r;
43
44 for (unsigned i = 0; i < set->allocated * 8; i++)
45 if (CPU_ISSET_S(i, set->allocated, set->set)) {
46 if (in_range)
47 range_end++;
48 else {
49 range_start = range_end = i;
50 in_range = true;
51 }
52 } else if (in_range) {
53 in_range = false;
54
55 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
56 return NULL;
57
58 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
59 assert_se(r > 0);
60 len += r;
61 }
62
63 if (in_range) {
64 if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int)))
65 return NULL;
66
67 r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end);
68 assert_se(r > 0);
69 }
70
71 return TAKE_PTR(str) ?: strdup("");
72}
73
167a776d 74int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
0985c7c4
ZJS
75 size_t need;
76
77 assert(cpu_set);
78
79 need = CPU_ALLOC_SIZE(ncpus);
80 if (need > cpu_set->allocated) {
81 cpu_set_t *t;
82
83 t = realloc(cpu_set->set, need);
84 if (!t)
85 return -ENOMEM;
86
87 memzero((uint8_t*) t + cpu_set->allocated, need - cpu_set->allocated);
88
89 cpu_set->set = t;
90 cpu_set->allocated = need;
91 }
92
93 return 0;
94}
95
96static int cpu_set_add(CPUSet *cpu_set, unsigned cpu) {
97 int r;
98
99 if (cpu >= 8192)
100 /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */
101 return -ERANGE;
102
103 r = cpu_set_realloc(cpu_set, cpu + 1);
104 if (r < 0)
105 return r;
106
107 CPU_SET_S(cpu, cpu_set->allocated, cpu_set->set);
108 return 0;
109}
110
111int cpu_set_add_all(CPUSet *a, const CPUSet *b) {
112 int r;
113
114 /* Do this backwards, so if we fail, we fail before changing anything. */
115 for (unsigned cpu_p1 = b->allocated * 8; cpu_p1 > 0; cpu_p1--)
116 if (CPU_ISSET_S(cpu_p1 - 1, b->allocated, b->set)) {
117 r = cpu_set_add(a, cpu_p1 - 1);
118 if (r < 0)
119 return r;
120 }
121
122 return 0;
123}
124
125int parse_cpu_set_full(
618234a5 126 const char *rvalue,
0985c7c4 127 CPUSet *cpu_set,
6d8a29b2 128 bool warn,
618234a5
LP
129 const char *unit,
130 const char *filename,
131 unsigned line,
132 const char *lvalue) {
133
0985c7c4 134 _cleanup_(cpu_set_reset) CPUSet c = {};
6d8a29b2 135 const char *p = rvalue;
618234a5 136
0985c7c4 137 assert(p);
618234a5
LP
138
139 for (;;) {
140 _cleanup_free_ char *word = NULL;
0985c7c4 141 unsigned cpu_lower, cpu_upper;
618234a5
LP
142 int r;
143
6d8a29b2
YW
144 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES);
145 if (r == -ENOMEM)
146 return warn ? log_oom() : -ENOMEM;
a26662ce 147 if (r < 0)
6d8a29b2 148 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
618234a5
LP
149 if (r == 0)
150 break;
151
a26662ce
FB
152 r = parse_range(word, &cpu_lower, &cpu_upper);
153 if (r < 0)
6d8a29b2 154 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
032cf8e4 155
6d8a29b2
YW
156 if (cpu_lower > cpu_upper) {
157 if (warn)
0985c7c4
ZJS
158 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.",
159 word, cpu_lower, cpu_upper);
160
161 /* Make sure something is allocated, to distinguish this from the empty case */
162 r = cpu_set_realloc(&c, 1);
163 if (r < 0)
164 return r;
032cf8e4
YW
165 }
166
0985c7c4
ZJS
167 for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) {
168 r = cpu_set_add(&c, cpu_p1 - 1);
169 if (r < 0)
170 return warn ? log_syntax(unit, LOG_ERR, filename, line, r,
171 "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r;
172 }
032cf8e4
YW
173 }
174
0985c7c4
ZJS
175 /* On success, transfer ownership to the output variable */
176 *cpu_set = c;
177 c = (CPUSet) {};
178
179 return 0;
180}
181
182int parse_cpu_set_extend(
183 const char *rvalue,
184 CPUSet *old,
185 bool warn,
186 const char *unit,
187 const char *filename,
188 unsigned line,
189 const char *lvalue) {
190
191 _cleanup_(cpu_set_reset) CPUSet cpuset = {};
192 int r;
193
194 r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue);
195 if (r < 0)
196 return r;
197
198 if (!cpuset.set) {
199 /* An empty assignment resets the CPU list */
200 cpu_set_reset(old);
201 return 0;
202 }
203
204 if (!old->set) {
205 *old = cpuset;
206 cpuset = (CPUSet) {};
207 return 0;
208 }
032cf8e4 209
0985c7c4 210 return cpu_set_add_all(old, &cpuset);
032cf8e4 211}
f44b3035
ZJS
212
213int cpus_in_affinity_mask(void) {
214 size_t n = 16;
215 int r;
216
217 for (;;) {
218 cpu_set_t *c;
219
220 c = CPU_ALLOC(n);
221 if (!c)
222 return -ENOMEM;
223
224 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
225 int k;
226
227 k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
228 CPU_FREE(c);
229
230 if (k <= 0)
231 return -EINVAL;
232
233 return k;
234 }
235
236 r = -errno;
237 CPU_FREE(c);
238
239 if (r != -EINVAL)
240 return r;
241 if (n > SIZE_MAX/2)
242 return -ENOMEM;
243 n *= 2;
244 }
245}
c367f996
MS
246
247int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
248 uint8_t *out;
249
250 assert(set);
251 assert(ret);
252
253 out = new0(uint8_t, set->allocated);
254 if (!out)
255 return -ENOMEM;
256
257 for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
258 if (CPU_ISSET_S(cpu, set->allocated, set->set))
259 out[cpu / 8] |= 1u << (cpu % 8);
260
261 *ret = out;
262 *allocated = set->allocated;
263 return 0;
264}
265
266int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
267 _cleanup_(cpu_set_reset) CPUSet s = {};
268 int r;
269
270 assert(bits);
271 assert(set);
272
273 for (unsigned cpu = size * 8; cpu > 0; cpu--)
274 if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
275 r = cpu_set_add(&s, cpu - 1);
276 if (r < 0)
277 return r;
278 }
279
280 *set = s;
281 s = (CPUSet) {};
282 return 0;
283}