/src/util-linux/lib/cpuset.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * SPDX-License-Identifier: LGPL-2.1-or-later |
3 | | * |
4 | | * Terminology: |
5 | | * |
6 | | * cpuset - (libc) cpu_set_t data structure represents set of CPUs |
7 | | * cpumask - string with hex mask (e.g. "0x00000001") |
8 | | * cpulist - string with CPU ranges (e.g. "0-3,5,7,8") |
9 | | * |
10 | | * Based on code from taskset.c and Linux kernel. |
11 | | * |
12 | | * This file may be redistributed under the terms of the |
13 | | * GNU Lesser General Public License. |
14 | | * |
15 | | * Copyright (C) 2010 Karel Zak <kzak@redhat.com> |
16 | | */ |
17 | | |
18 | | #include <stdio.h> |
19 | | #include <stdlib.h> |
20 | | #include <unistd.h> |
21 | | #include <sched.h> |
22 | | #include <errno.h> |
23 | | #include <string.h> |
24 | | #include <ctype.h> |
25 | | #ifdef HAVE_SYS_SYSCALL_H |
26 | | #include <sys/syscall.h> |
27 | | #endif |
28 | | |
29 | | #include "cpuset.h" |
30 | | #include "c.h" |
31 | | |
32 | | static inline int val_to_char(int v) |
33 | 0 | { |
34 | 0 | if (v >= 0 && v < 10) |
35 | 0 | return '0' + v; |
36 | 0 | if (v >= 10 && v < 16) |
37 | 0 | return ('a' - 10) + v; |
38 | 0 | return -1; |
39 | 0 | } |
40 | | |
41 | | static inline int char_to_val(int c) |
42 | 0 | { |
43 | 0 | int cl; |
44 | |
|
45 | 0 | if (c >= '0' && c <= '9') |
46 | 0 | return c - '0'; |
47 | 0 | cl = tolower(c); |
48 | 0 | if (cl >= 'a' && cl <= 'f') |
49 | 0 | return cl + (10 - 'a'); |
50 | 0 | return -1; |
51 | 0 | } |
52 | | |
53 | | static const char *nexttoken(const char *q, int sep) |
54 | 0 | { |
55 | 0 | if (q) |
56 | 0 | q = strchr(q, sep); |
57 | 0 | if (q) |
58 | 0 | q++; |
59 | 0 | return q; |
60 | 0 | } |
61 | | |
62 | | /* |
63 | | * Number of bits in a CPU bitmask on current system |
64 | | */ |
65 | | int get_max_number_of_cpus(void) |
66 | 0 | { |
67 | 0 | #ifdef SYS_sched_getaffinity |
68 | 0 | int n, cpus = 2048; |
69 | 0 | size_t setsize; |
70 | 0 | cpu_set_t *set = cpuset_alloc(cpus, &setsize, NULL); |
71 | |
|
72 | 0 | if (!set) |
73 | 0 | return -1; /* error */ |
74 | | |
75 | 0 | for (;;) { |
76 | 0 | CPU_ZERO_S(setsize, set); |
77 | | |
78 | | /* the library version does not return size of cpumask_t */ |
79 | 0 | n = syscall(SYS_sched_getaffinity, 0, setsize, set); |
80 | |
|
81 | 0 | if (n < 0 && errno == EINVAL && cpus < 1024 * 1024) { |
82 | 0 | cpuset_free(set); |
83 | 0 | cpus *= 2; |
84 | 0 | set = cpuset_alloc(cpus, &setsize, NULL); |
85 | 0 | if (!set) |
86 | 0 | return -1; /* error */ |
87 | 0 | continue; |
88 | 0 | } |
89 | 0 | cpuset_free(set); |
90 | 0 | return n * 8; |
91 | 0 | } |
92 | 0 | #endif |
93 | 0 | return -1; |
94 | 0 | } |
95 | | |
96 | | /* |
97 | | * Allocates a new set for ncpus and returns size in bytes and size in bits |
98 | | */ |
99 | | cpu_set_t *cpuset_alloc(int ncpus, size_t *setsize, size_t *nbits) |
100 | 0 | { |
101 | 0 | cpu_set_t *set = CPU_ALLOC(ncpus); |
102 | |
|
103 | 0 | if (!set) |
104 | 0 | return NULL; |
105 | 0 | if (setsize) |
106 | 0 | *setsize = CPU_ALLOC_SIZE(ncpus); |
107 | 0 | if (nbits) |
108 | 0 | *nbits = cpuset_nbits(CPU_ALLOC_SIZE(ncpus)); |
109 | 0 | return set; |
110 | 0 | } |
111 | | |
112 | | void cpuset_free(cpu_set_t *set) |
113 | 0 | { |
114 | 0 | CPU_FREE(set); |
115 | 0 | } |
116 | | |
117 | | #if !HAVE_DECL_CPU_ALLOC |
118 | | /* Please, use CPU_COUNT_S() macro. This is fallback */ |
119 | | int __cpuset_count_s(size_t setsize, const cpu_set_t *set) |
120 | | { |
121 | | int s = 0; |
122 | | const __cpu_mask *p = set->__bits; |
123 | | const __cpu_mask *end = &set->__bits[setsize / sizeof (__cpu_mask)]; |
124 | | |
125 | | while (p < end) { |
126 | | __cpu_mask l = *p++; |
127 | | |
128 | | if (l == 0) |
129 | | continue; |
130 | | # if LONG_BIT > 32 |
131 | | l = (l & 0x5555555555555555ul) + ((l >> 1) & 0x5555555555555555ul); |
132 | | l = (l & 0x3333333333333333ul) + ((l >> 2) & 0x3333333333333333ul); |
133 | | l = (l & 0x0f0f0f0f0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0f0f0f0f0ful); |
134 | | l = (l & 0x00ff00ff00ff00fful) + ((l >> 8) & 0x00ff00ff00ff00fful); |
135 | | l = (l & 0x0000ffff0000fffful) + ((l >> 16) & 0x0000ffff0000fffful); |
136 | | l = (l & 0x00000000fffffffful) + ((l >> 32) & 0x00000000fffffffful); |
137 | | # else |
138 | | l = (l & 0x55555555ul) + ((l >> 1) & 0x55555555ul); |
139 | | l = (l & 0x33333333ul) + ((l >> 2) & 0x33333333ul); |
140 | | l = (l & 0x0f0f0f0ful) + ((l >> 4) & 0x0f0f0f0ful); |
141 | | l = (l & 0x00ff00fful) + ((l >> 8) & 0x00ff00fful); |
142 | | l = (l & 0x0000fffful) + ((l >> 16) & 0x0000fffful); |
143 | | # endif |
144 | | s += l; |
145 | | } |
146 | | return s; |
147 | | } |
148 | | #endif |
149 | | |
150 | | /* |
151 | | * Finds the first CPU present after the specified index. |
152 | | * |
153 | | * start: starting index, inclusive. |
154 | | * setsize: size of the set in *bytes*. |
155 | | * set: CPU set to search. |
156 | | * |
157 | | * Return: the index of the first CPU present in `set`, starting at `start`. |
158 | | * If no such CPU exists, returns the size of the set in *bits*. |
159 | | */ |
160 | | static size_t find_next_cpu(size_t start, size_t setsize, cpu_set_t *set) |
161 | 0 | { |
162 | 0 | size_t nbits = cpuset_nbits(setsize); |
163 | 0 | for (; start < nbits; start++) |
164 | 0 | if (CPU_ISSET_S(start, setsize, set)) |
165 | 0 | return start; |
166 | 0 | return start; |
167 | 0 | } |
168 | | |
169 | | /* |
170 | | * Returns human readable representation of the cpuset. The output format is |
171 | | * a list of CPUs with ranges (for example, "0,1,3-9:3"). |
172 | | */ |
173 | | char *cpulist_create(char *str, size_t len, |
174 | | cpu_set_t *set, size_t setsize) |
175 | 0 | { |
176 | 0 | char *ptr = str; |
177 | 0 | int entry_made = 0; |
178 | 0 | size_t max = cpuset_nbits(setsize); |
179 | 0 | size_t a = 0; /* min for cpu range */ |
180 | 0 | size_t next = 0; /* where to start looking for next cpu */ |
181 | |
|
182 | 0 | while ((a = find_next_cpu(next, setsize, set)) < max) { |
183 | 0 | int rlen; |
184 | 0 | next = find_next_cpu(a + 1, setsize, set); |
185 | 0 | if (next == max) { |
186 | 0 | rlen = snprintf(ptr, len, "%zu,", a); |
187 | 0 | } else { |
188 | | /* Extend range as long as we have the same stride. */ |
189 | 0 | size_t b = next; |
190 | 0 | size_t s = b - a; |
191 | 0 | while (((next = find_next_cpu(b + 1, setsize, set)) < |
192 | 0 | max) && next - b == s) { |
193 | 0 | b = next; |
194 | 0 | } |
195 | 0 | if (b - a == s) { |
196 | | /* |
197 | | * Only print one CPU. Hope the next one can |
198 | | * be put in the next range. |
199 | | */ |
200 | 0 | rlen = snprintf(ptr, len, "%zu,", a); |
201 | 0 | next = b; |
202 | 0 | } else if (s == 1) { |
203 | 0 | rlen = snprintf(ptr, len, "%zu-%zu,", a, b); |
204 | 0 | } else { |
205 | 0 | rlen = snprintf(ptr, len, "%zu-%zu:%zu,", |
206 | 0 | a, b, s); |
207 | 0 | } |
208 | 0 | } |
209 | 0 | if (rlen < 0 || (size_t) rlen >= len) |
210 | 0 | return NULL; |
211 | 0 | ptr += rlen; |
212 | 0 | len -= rlen; |
213 | 0 | entry_made = 1; |
214 | 0 | } |
215 | 0 | ptr -= entry_made; |
216 | 0 | *ptr = '\0'; |
217 | |
|
218 | 0 | return str; |
219 | 0 | } |
220 | | |
221 | | /* |
222 | | * Returns string with CPU mask. |
223 | | */ |
224 | | char *cpumask_create(char *str, size_t len, |
225 | | cpu_set_t *set, size_t setsize) |
226 | 0 | { |
227 | 0 | char *ptr = str; |
228 | 0 | char *ret = NULL; |
229 | 0 | int cpu; |
230 | |
|
231 | 0 | for (cpu = cpuset_nbits(setsize) - 4; cpu >= 0; cpu -= 4) { |
232 | 0 | char val = 0; |
233 | |
|
234 | 0 | if (len == (size_t) (ptr - str)) |
235 | 0 | break; |
236 | | |
237 | 0 | if (CPU_ISSET_S(cpu, setsize, set)) |
238 | 0 | val |= 1; |
239 | 0 | if (CPU_ISSET_S(cpu + 1, setsize, set)) |
240 | 0 | val |= 2; |
241 | 0 | if (CPU_ISSET_S(cpu + 2, setsize, set)) |
242 | 0 | val |= 4; |
243 | 0 | if (CPU_ISSET_S(cpu + 3, setsize, set)) |
244 | 0 | val |= 8; |
245 | |
|
246 | 0 | if (!ret && val) |
247 | 0 | ret = ptr; |
248 | 0 | *ptr++ = val_to_char(val); |
249 | 0 | } |
250 | 0 | *ptr = '\0'; |
251 | 0 | return ret ? ret : ptr - 1; |
252 | 0 | } |
253 | | |
254 | | /* |
255 | | * Parses string with CPUs mask. |
256 | | */ |
257 | | int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize) |
258 | 0 | { |
259 | 0 | int len = strlen(str); |
260 | 0 | const char *ptr = str + len - 1; |
261 | 0 | int cpu = 0; |
262 | | |
263 | | /* skip 0x, it's all hex anyway */ |
264 | 0 | if (len > 1 && !memcmp(str, "0x", 2L)) |
265 | 0 | str += 2; |
266 | |
|
267 | 0 | CPU_ZERO_S(setsize, set); |
268 | |
|
269 | 0 | while (ptr >= str) { |
270 | 0 | char val; |
271 | | |
272 | | /* cpu masks in /sys uses comma as a separator */ |
273 | 0 | if (*ptr == ',') |
274 | 0 | ptr--; |
275 | |
|
276 | 0 | val = char_to_val(*ptr); |
277 | 0 | if (val == (char) -1) |
278 | 0 | return -1; |
279 | 0 | if (val & 1) |
280 | 0 | CPU_SET_S(cpu, setsize, set); |
281 | 0 | if (val & 2) |
282 | 0 | CPU_SET_S(cpu + 1, setsize, set); |
283 | 0 | if (val & 4) |
284 | 0 | CPU_SET_S(cpu + 2, setsize, set); |
285 | 0 | if (val & 8) |
286 | 0 | CPU_SET_S(cpu + 3, setsize, set); |
287 | 0 | ptr--; |
288 | 0 | cpu += 4; |
289 | 0 | } |
290 | | |
291 | 0 | return 0; |
292 | 0 | } |
293 | | |
294 | | static int nextnumber(const char *str, char **end, unsigned int *result) |
295 | 0 | { |
296 | 0 | errno = 0; |
297 | 0 | if (str == NULL || *str == '\0' || !isdigit(*str)) |
298 | 0 | return -EINVAL; |
299 | 0 | *result = (unsigned int) strtoul(str, end, 10); |
300 | 0 | if (errno) |
301 | 0 | return -errno; |
302 | 0 | if (str == *end) |
303 | 0 | return -EINVAL; |
304 | 0 | return 0; |
305 | 0 | } |
306 | | |
307 | | /* |
308 | | * Parses string with list of CPU ranges. |
309 | | * Returns 0 on success. |
310 | | * Returns 1 on error. |
311 | | * Returns 2 if fail is set and a cpu number passed in the list doesn't fit |
312 | | * into the cpu_set. If fail is not set cpu numbers that do not fit are |
313 | | * ignored and 0 is returned instead. |
314 | | */ |
315 | | int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail) |
316 | 0 | { |
317 | 0 | const size_t max = cpuset_nbits(setsize); |
318 | 0 | const char *p, *q; |
319 | 0 | char *end = NULL; |
320 | |
|
321 | 0 | q = str; |
322 | 0 | CPU_ZERO_S(setsize, set); |
323 | |
|
324 | 0 | while (p = q, q = nexttoken(q, ','), p) { |
325 | 0 | unsigned int a; /* beginning of range */ |
326 | 0 | unsigned int b; /* end of range */ |
327 | 0 | unsigned int s; /* stride */ |
328 | 0 | const char *c1, *c2; |
329 | |
|
330 | 0 | if (nextnumber(p, &end, &a) != 0) |
331 | 0 | return 1; |
332 | 0 | b = a; |
333 | 0 | s = 1; |
334 | 0 | p = end; |
335 | |
|
336 | 0 | c1 = nexttoken(p, '-'); |
337 | 0 | c2 = nexttoken(p, ','); |
338 | |
|
339 | 0 | if (c1 != NULL && (c2 == NULL || c1 < c2)) { |
340 | 0 | if (nextnumber(c1, &end, &b) != 0) |
341 | 0 | return 1; |
342 | | |
343 | 0 | c1 = end && *end ? nexttoken(end, ':') : NULL; |
344 | |
|
345 | 0 | if (c1 != NULL && (c2 == NULL || c1 < c2)) { |
346 | 0 | if (nextnumber(c1, &end, &s) != 0) |
347 | 0 | return 1; |
348 | 0 | if (s == 0) |
349 | 0 | return 1; |
350 | 0 | } |
351 | 0 | } |
352 | | |
353 | 0 | if (!(a <= b)) |
354 | 0 | return 1; |
355 | 0 | while (a <= b) { |
356 | 0 | if (a >= max) { |
357 | 0 | if (fail) |
358 | 0 | return 2; |
359 | 0 | else |
360 | 0 | break; |
361 | 0 | } |
362 | 0 | CPU_SET_S(a, setsize, set); |
363 | 0 | a += s; |
364 | 0 | } |
365 | 0 | } |
366 | | |
367 | 0 | if (end && *end) |
368 | 0 | return 1; |
369 | 0 | return 0; |
370 | 0 | } |
371 | | |
372 | | #ifdef TEST_PROGRAM_CPUSET |
373 | | |
374 | | #include <getopt.h> |
375 | | |
376 | | int main(int argc, char *argv[]) |
377 | | { |
378 | | cpu_set_t *set; |
379 | | size_t setsize, buflen, nbits; |
380 | | char *buf, *mask = NULL, *range = NULL; |
381 | | int ncpus = 2048, rc, c; |
382 | | |
383 | | static const struct option longopts[] = { |
384 | | { "ncpus", 1, NULL, 'n' }, |
385 | | { "mask", 1, NULL, 'm' }, |
386 | | { "range", 1, NULL, 'r' }, |
387 | | { NULL, 0, NULL, 0 } |
388 | | }; |
389 | | |
390 | | while ((c = getopt_long(argc, argv, "n:m:r:", longopts, NULL)) != -1) { |
391 | | switch(c) { |
392 | | case 'n': |
393 | | ncpus = atoi(optarg); |
394 | | break; |
395 | | case 'm': |
396 | | mask = strdup(optarg); |
397 | | break; |
398 | | case 'r': |
399 | | range = strdup(optarg); |
400 | | break; |
401 | | default: |
402 | | goto usage_err; |
403 | | } |
404 | | } |
405 | | |
406 | | if (!mask && !range) |
407 | | goto usage_err; |
408 | | |
409 | | set = cpuset_alloc(ncpus, &setsize, &nbits); |
410 | | if (!set) |
411 | | err(EXIT_FAILURE, "failed to allocate cpu set"); |
412 | | |
413 | | /* |
414 | | fprintf(stderr, "ncpus: %d, cpuset bits: %zd, cpuset bytes: %zd\n", |
415 | | ncpus, nbits, setsize); |
416 | | */ |
417 | | |
418 | | buflen = 7 * nbits; |
419 | | buf = malloc(buflen); |
420 | | if (!buf) |
421 | | err(EXIT_FAILURE, "failed to allocate cpu set buffer"); |
422 | | |
423 | | if (mask) |
424 | | rc = cpumask_parse(mask, set, setsize); |
425 | | else |
426 | | rc = cpulist_parse(range, set, setsize, 0); |
427 | | |
428 | | if (rc) |
429 | | errx(EXIT_FAILURE, "failed to parse string: %s", mask ? : range); |
430 | | |
431 | | printf("%-15s = %15s ", mask ? : range, |
432 | | cpumask_create(buf, buflen, set, setsize)); |
433 | | printf("[%s]\n", cpulist_create(buf, buflen, set, setsize)); |
434 | | |
435 | | free(buf); |
436 | | free(mask); |
437 | | free(range); |
438 | | cpuset_free(set); |
439 | | |
440 | | return EXIT_SUCCESS; |
441 | | |
442 | | usage_err: |
443 | | fprintf(stderr, |
444 | | "usage: %s [--ncpus <num>] --mask <mask> | --range <list>\n", |
445 | | program_invocation_short_name); |
446 | | exit(EXIT_FAILURE); |
447 | | } |
448 | | #endif |