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