]>
Commit | Line | Data |
---|---|---|
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 | 17 | char* 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 |
37 | char *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 | ||
1f57a176 ZJS |
58 | if (range_end > range_start || len == 0) |
59 | r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); | |
60 | else | |
61 | r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start); | |
71b28519 MS |
62 | assert_se(r > 0); |
63 | len += r; | |
64 | } | |
65 | ||
66 | if (in_range) { | |
67 | if (!GREEDY_REALLOC(str, allocated, len + 2 + 2 * DECIMAL_STR_MAX(int))) | |
68 | return NULL; | |
69 | ||
1f57a176 ZJS |
70 | if (range_end > range_start || len == 0) |
71 | r = sprintf(str + len, len > 0 ? " %d-%d" : "%d-%d", range_start, range_end); | |
72 | else | |
73 | r = sprintf(str + len, len > 0 ? " %d" : "%d", range_start); | |
71b28519 MS |
74 | assert_se(r > 0); |
75 | } | |
76 | ||
77 | return TAKE_PTR(str) ?: strdup(""); | |
78 | } | |
79 | ||
167a776d | 80 | int cpu_set_realloc(CPUSet *cpu_set, unsigned ncpus) { |
0985c7c4 ZJS |
81 | size_t need; |
82 | ||
83 | assert(cpu_set); | |
84 | ||
85 | need = CPU_ALLOC_SIZE(ncpus); | |
86 | if (need > cpu_set->allocated) { | |
87 | cpu_set_t *t; | |
88 | ||
89 | t = realloc(cpu_set->set, need); | |
90 | if (!t) | |
91 | return -ENOMEM; | |
92 | ||
93 | memzero((uint8_t*) t + cpu_set->allocated, need - cpu_set->allocated); | |
94 | ||
95 | cpu_set->set = t; | |
96 | cpu_set->allocated = need; | |
97 | } | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | static int cpu_set_add(CPUSet *cpu_set, unsigned cpu) { | |
103 | int r; | |
104 | ||
105 | if (cpu >= 8192) | |
106 | /* As of kernel 5.1, CONFIG_NR_CPUS can be set to 8192 on PowerPC */ | |
107 | return -ERANGE; | |
108 | ||
109 | r = cpu_set_realloc(cpu_set, cpu + 1); | |
110 | if (r < 0) | |
111 | return r; | |
112 | ||
113 | CPU_SET_S(cpu, cpu_set->allocated, cpu_set->set); | |
114 | return 0; | |
115 | } | |
116 | ||
117 | int cpu_set_add_all(CPUSet *a, const CPUSet *b) { | |
118 | int r; | |
119 | ||
120 | /* Do this backwards, so if we fail, we fail before changing anything. */ | |
121 | for (unsigned cpu_p1 = b->allocated * 8; cpu_p1 > 0; cpu_p1--) | |
122 | if (CPU_ISSET_S(cpu_p1 - 1, b->allocated, b->set)) { | |
123 | r = cpu_set_add(a, cpu_p1 - 1); | |
124 | if (r < 0) | |
125 | return r; | |
126 | } | |
127 | ||
128 | return 0; | |
129 | } | |
130 | ||
131 | int parse_cpu_set_full( | |
618234a5 | 132 | const char *rvalue, |
0985c7c4 | 133 | CPUSet *cpu_set, |
6d8a29b2 | 134 | bool warn, |
618234a5 LP |
135 | const char *unit, |
136 | const char *filename, | |
137 | unsigned line, | |
138 | const char *lvalue) { | |
139 | ||
0985c7c4 | 140 | _cleanup_(cpu_set_reset) CPUSet c = {}; |
6d8a29b2 | 141 | const char *p = rvalue; |
618234a5 | 142 | |
0985c7c4 | 143 | assert(p); |
618234a5 LP |
144 | |
145 | for (;;) { | |
146 | _cleanup_free_ char *word = NULL; | |
0985c7c4 | 147 | unsigned cpu_lower, cpu_upper; |
618234a5 LP |
148 | int r; |
149 | ||
6d8a29b2 YW |
150 | r = extract_first_word(&p, &word, WHITESPACE ",", EXTRACT_QUOTES); |
151 | if (r == -ENOMEM) | |
152 | return warn ? log_oom() : -ENOMEM; | |
a26662ce | 153 | if (r < 0) |
6d8a29b2 | 154 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Invalid value for %s: %s", lvalue, rvalue) : r; |
618234a5 LP |
155 | if (r == 0) |
156 | break; | |
157 | ||
a26662ce FB |
158 | r = parse_range(word, &cpu_lower, &cpu_upper); |
159 | if (r < 0) | |
6d8a29b2 | 160 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse CPU affinity '%s'", word) : r; |
032cf8e4 | 161 | |
6d8a29b2 YW |
162 | if (cpu_lower > cpu_upper) { |
163 | if (warn) | |
0985c7c4 ZJS |
164 | log_syntax(unit, LOG_WARNING, filename, line, 0, "Range '%s' is invalid, %u > %u, ignoring.", |
165 | word, cpu_lower, cpu_upper); | |
166 | ||
167 | /* Make sure something is allocated, to distinguish this from the empty case */ | |
168 | r = cpu_set_realloc(&c, 1); | |
169 | if (r < 0) | |
170 | return r; | |
032cf8e4 YW |
171 | } |
172 | ||
0985c7c4 ZJS |
173 | for (unsigned cpu_p1 = MIN(cpu_upper, UINT_MAX-1) + 1; cpu_p1 > cpu_lower; cpu_p1--) { |
174 | r = cpu_set_add(&c, cpu_p1 - 1); | |
175 | if (r < 0) | |
176 | return warn ? log_syntax(unit, LOG_ERR, filename, line, r, | |
177 | "Cannot add CPU %u to set: %m", cpu_p1 - 1) : r; | |
178 | } | |
032cf8e4 YW |
179 | } |
180 | ||
0985c7c4 ZJS |
181 | /* On success, transfer ownership to the output variable */ |
182 | *cpu_set = c; | |
183 | c = (CPUSet) {}; | |
184 | ||
185 | return 0; | |
186 | } | |
187 | ||
188 | int parse_cpu_set_extend( | |
189 | const char *rvalue, | |
190 | CPUSet *old, | |
191 | bool warn, | |
192 | const char *unit, | |
193 | const char *filename, | |
194 | unsigned line, | |
195 | const char *lvalue) { | |
196 | ||
197 | _cleanup_(cpu_set_reset) CPUSet cpuset = {}; | |
198 | int r; | |
199 | ||
200 | r = parse_cpu_set_full(rvalue, &cpuset, true, unit, filename, line, lvalue); | |
201 | if (r < 0) | |
202 | return r; | |
203 | ||
204 | if (!cpuset.set) { | |
205 | /* An empty assignment resets the CPU list */ | |
206 | cpu_set_reset(old); | |
207 | return 0; | |
208 | } | |
209 | ||
210 | if (!old->set) { | |
211 | *old = cpuset; | |
212 | cpuset = (CPUSet) {}; | |
213 | return 0; | |
214 | } | |
032cf8e4 | 215 | |
0985c7c4 | 216 | return cpu_set_add_all(old, &cpuset); |
032cf8e4 | 217 | } |
f44b3035 ZJS |
218 | |
219 | int cpus_in_affinity_mask(void) { | |
220 | size_t n = 16; | |
221 | int r; | |
222 | ||
223 | for (;;) { | |
224 | cpu_set_t *c; | |
225 | ||
226 | c = CPU_ALLOC(n); | |
227 | if (!c) | |
228 | return -ENOMEM; | |
229 | ||
230 | if (sched_getaffinity(0, CPU_ALLOC_SIZE(n), c) >= 0) { | |
231 | int k; | |
232 | ||
233 | k = CPU_COUNT_S(CPU_ALLOC_SIZE(n), c); | |
234 | CPU_FREE(c); | |
235 | ||
236 | if (k <= 0) | |
237 | return -EINVAL; | |
238 | ||
239 | return k; | |
240 | } | |
241 | ||
242 | r = -errno; | |
243 | CPU_FREE(c); | |
244 | ||
245 | if (r != -EINVAL) | |
246 | return r; | |
247 | if (n > SIZE_MAX/2) | |
248 | return -ENOMEM; | |
249 | n *= 2; | |
250 | } | |
251 | } | |
c367f996 MS |
252 | |
253 | int cpu_set_to_dbus(const CPUSet *set, uint8_t **ret, size_t *allocated) { | |
254 | uint8_t *out; | |
255 | ||
256 | assert(set); | |
257 | assert(ret); | |
258 | ||
259 | out = new0(uint8_t, set->allocated); | |
260 | if (!out) | |
261 | return -ENOMEM; | |
262 | ||
263 | for (unsigned cpu = 0; cpu < set->allocated * 8; cpu++) | |
264 | if (CPU_ISSET_S(cpu, set->allocated, set->set)) | |
265 | out[cpu / 8] |= 1u << (cpu % 8); | |
266 | ||
267 | *ret = out; | |
268 | *allocated = set->allocated; | |
269 | return 0; | |
270 | } | |
271 | ||
272 | int cpu_set_from_dbus(const uint8_t *bits, size_t size, CPUSet *set) { | |
273 | _cleanup_(cpu_set_reset) CPUSet s = {}; | |
274 | int r; | |
275 | ||
276 | assert(bits); | |
277 | assert(set); | |
278 | ||
279 | for (unsigned cpu = size * 8; cpu > 0; cpu--) | |
280 | if (bits[(cpu - 1) / 8] & (1u << ((cpu - 1) % 8))) { | |
281 | r = cpu_set_add(&s, cpu - 1); | |
282 | if (r < 0) | |
283 | return r; | |
284 | } | |
285 | ||
286 | *set = s; | |
287 | s = (CPUSet) {}; | |
288 | return 0; | |
289 | } |