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