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