]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/cpu-set-util.c
basic: rename util.h to logarithm.h
[thirdparty/systemd.git] / src / shared / cpu-set-util.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
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"
b070c7c0
MS
10#include "dirent-util.h"
11#include "errno-util.h"
84ac7bea 12#include "extract-word.h"
b070c7c0 13#include "fd-util.h"
11c3a366
TA
14#include "log.h"
15#include "macro.h"
0985c7c4 16#include "memory-util.h"
93cc7779 17#include "parse-util.h"
b070c7c0 18#include "stat-util.h"
b11d6a7b 19#include "string-util.h"
b070c7c0 20#include "strv.h"
618234a5 21
0985c7c4 22char* cpu_set_to_string(const CPUSet *a) {
a832893f 23 _cleanup_free_ char *str = NULL;
319a4f4b 24 size_t len = 0;
a832893f
ZJS
25 int i, r;
26
0985c7c4
ZJS
27 for (i = 0; (size_t) i < a->allocated * 8; i++) {
28 if (!CPU_ISSET_S(i, a->allocated, a->set))
a832893f
ZJS
29 continue;
30
319a4f4b 31 if (!GREEDY_REALLOC(str, len + 1 + DECIMAL_STR_MAX(int)))
a832893f
ZJS
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
71b28519
MS
42char *cpu_set_to_range_string(const CPUSet *set) {
43 unsigned range_start = 0, range_end;
44 _cleanup_free_ char *str = NULL;
71b28519 45 bool in_range = false;
319a4f4b 46 size_t len = 0;
71b28519
MS
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
319a4f4b 60 if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(unsigned)))
71b28519
MS
61 return NULL;
62
71923237 63 if (range_end > range_start)
c0f86d66 64 r = sprintf(str + len, len > 0 ? " %u-%u" : "%u-%u", range_start, range_end);
1f57a176 65 else
c0f86d66 66 r = sprintf(str + len, len > 0 ? " %u" : "%u", range_start);
71b28519
MS
67 assert_se(r > 0);
68 len += r;
69 }
70
71 if (in_range) {
319a4f4b 72 if (!GREEDY_REALLOC(str, len + 2 + 2 * DECIMAL_STR_MAX(int)))
71b28519
MS
73 return NULL;
74
71923237 75 if (range_end > range_start)
c0f86d66 76 r = sprintf(str + len, len > 0 ? " %u-%u" : "%u-%u", range_start, range_end);
1f57a176 77 else
c0f86d66 78 r = sprintf(str + len, len > 0 ? " %u" : "%u", range_start);
71b28519
MS
79 assert_se(r > 0);
80 }
81
82 return TAKE_PTR(str) ?: strdup("");
83}
84
167a776d 85int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) {
0985c7c4
ZJS
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
332d387f 107int cpu_set_add(CPUSet *cpu_set, unsigned cpu) {
0985c7c4
ZJS
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
122int 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
e2b2fb7f 133 return 1;
0985c7c4
ZJS
134}
135
136int parse_cpu_set_full(
618234a5 137 const char *rvalue,
0985c7c4 138 CPUSet *cpu_set,
6d8a29b2 139 bool warn,
618234a5
LP
140 const char *unit,
141 const char *filename,
142 unsigned line,
143 const char *lvalue) {
144
0985c7c4 145 _cleanup_(cpu_set_reset) CPUSet c = {};
99534007 146 const char *p = ASSERT_PTR(rvalue);
618234a5
LP
147
148 for (;;) {
149 _cleanup_free_ char *word = NULL;
0985c7c4 150 unsigned cpu_lower, cpu_upper;
618234a5
LP
151 int r;
152
4ec85141 153 r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_UNQUOTE);
6d8a29b2
YW
154 if (r == -ENOMEM)
155 return warn ? log_oom() : -ENOMEM;
a26662ce 156 if (r < 0)
6d8a29b2 157 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r;
618234a5
LP
158 if (r == 0)
159 break;
160
a26662ce
FB
161 r = parse_range(word, &cpu_lower, &cpu_upper);
162 if (r < 0)
6d8a29b2 163 return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r;
032cf8e4 164
6d8a29b2
YW
165 if (cpu_lower > cpu_upper) {
166 if (warn)
0985c7c4
ZJS
167 log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.",
168 word, cpu_lower, cpu_upper);
169
170 /* Make sure something is allocated, to distinguish this from the empty case */
171 r = cpu_set_realloc(&c, 1);
172 if (r < 0)
173 return r;
032cf8e4
YW
174 }
175
0985c7c4
ZJS
176 for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) {
177 r = cpu_set_add(&c, cpu_p1 - 1);
178 if (r < 0)
179 return warn ? log_syntax(unit, LOG_ERR, filename, line, r,
180 "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r;
181 }
032cf8e4
YW
182 }
183
0985c7c4
ZJS
184 /* On success, transfer ownership to the output variable */
185 *cpu_set = c;
186 c = (CPUSet) {};
187
188 return 0;
189}
190
191int 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 r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue);
204 if (r < 0)
205 return r;
206
207 if (!cpuset.set) {
208 /* An empty assignment resets the CPU list */
209 cpu_set_reset(old);
210 return 0;
211 }
212
213 if (!old->set) {
214 *old = cpuset;
215 cpuset = (CPUSet) {};
e2b2fb7f 216 return 1;
0985c7c4 217 }
032cf8e4 218
0985c7c4 219 return cpu_set_add_all(old, &cpuset);
032cf8e4 220}
f44b3035
ZJS
221
222int cpus_in_affinity_mask(void) {
223 size_t n = 16;
224 int r;
225
226 for (;;) {
227 cpu_set_t *c;
228
229 c = CPU_ALLOC(n);
230 if (!c)
231 return -ENOMEM;
232
233 if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) {
234 int k;
235
236 k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c);
237 CPU_FREE(c);
238
239 if (k <= 0)
240 return -EINVAL;
241
242 return k;
243 }
244
245 r = -errno;
246 CPU_FREE(c);
247
248 if (r != -EINVAL)
249 return r;
250 if (n > SIZE_MAX/2)
251 return -ENOMEM;
252 n *= 2;
253 }
254}
c367f996
MS
255
256int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) {
257 uint8_t *out;
258
259 assert(set);
260 assert(ret);
261
262 out = new0(uint8_t, set->allocated);
263 if (!out)
264 return -ENOMEM;
265
266 for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++)
267 if (CPU_ISSET_S(cpu, set->allocated, set->set))
268 out[cpu / 8] |= 1u << (cpu % 8);
269
270 *ret = out;
271 *allocated = set->allocated;
272 return 0;
273}
274
275int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) {
276 _cleanup_(cpu_set_reset) CPUSet s = {};
277 int r;
278
279 assert(bits);
280 assert(set);
281
282 for (unsigned cpu = size * 8; cpu > 0; cpu--)
283 if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) {
284 r = cpu_set_add(&s, cpu - 1);
285 if (r < 0)
286 return r;
287 }
288
289 *set = s;
290 s = (CPUSet) {};
291 return 0;
292}