/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  |  |  * Returns human readable representation of the cpuset. The output format is  | 
152  |  |  * a list of CPUs with ranges (for example, "0,1,3-9").  | 
153  |  |  */  | 
154  |  | char *cpulist_create(char *str, size_t len,  | 
155  |  |       cpu_set_t *set, size_t setsize)  | 
156  | 0  | { | 
157  | 0  |   size_t i;  | 
158  | 0  |   char *ptr = str;  | 
159  | 0  |   int entry_made = 0;  | 
160  | 0  |   size_t max = cpuset_nbits(setsize);  | 
161  |  | 
  | 
162  | 0  |   for (i = 0; i < max; i++) { | 
163  | 0  |     if (CPU_ISSET_S(i, setsize, set)) { | 
164  | 0  |       int rlen;  | 
165  | 0  |       size_t j, run = 0;  | 
166  | 0  |       entry_made = 1;  | 
167  | 0  |       for (j = i + 1; j < max; j++) { | 
168  | 0  |         if (CPU_ISSET_S(j, setsize, set))  | 
169  | 0  |           run++;  | 
170  | 0  |         else  | 
171  | 0  |           break;  | 
172  | 0  |       }  | 
173  | 0  |       if (!run)  | 
174  | 0  |         rlen = snprintf(ptr, len, "%zu,", i);  | 
175  | 0  |       else if (run == 1) { | 
176  | 0  |         rlen = snprintf(ptr, len, "%zu,%zu,", i, i + 1);  | 
177  | 0  |         i++;  | 
178  | 0  |       } else { | 
179  | 0  |         rlen = snprintf(ptr, len, "%zu-%zu,", i, i + run);  | 
180  | 0  |         i += run;  | 
181  | 0  |       }  | 
182  | 0  |       if (rlen < 0 || (size_t) rlen >= len)  | 
183  | 0  |         return NULL;  | 
184  | 0  |       ptr += rlen;  | 
185  | 0  |       len -= rlen;  | 
186  | 0  |     }  | 
187  | 0  |   }  | 
188  | 0  |   ptr -= entry_made;  | 
189  | 0  |   *ptr = '\0';  | 
190  |  | 
  | 
191  | 0  |   return str;  | 
192  | 0  | }  | 
193  |  |  | 
194  |  | /*  | 
195  |  |  * Returns string with CPU mask.  | 
196  |  |  */  | 
197  |  | char *cpumask_create(char *str, size_t len,  | 
198  |  |       cpu_set_t *set, size_t setsize)  | 
199  | 0  | { | 
200  | 0  |   char *ptr = str;  | 
201  | 0  |   char *ret = NULL;  | 
202  | 0  |   int cpu;  | 
203  |  | 
  | 
204  | 0  |   for (cpu = cpuset_nbits(setsize) - 4; cpu >= 0; cpu -= 4) { | 
205  | 0  |     char val = 0;  | 
206  |  | 
  | 
207  | 0  |     if (len == (size_t) (ptr - str))  | 
208  | 0  |       break;  | 
209  |  |  | 
210  | 0  |     if (CPU_ISSET_S(cpu, setsize, set))  | 
211  | 0  |       val |= 1;  | 
212  | 0  |     if (CPU_ISSET_S(cpu + 1, setsize, set))  | 
213  | 0  |       val |= 2;  | 
214  | 0  |     if (CPU_ISSET_S(cpu + 2, setsize, set))  | 
215  | 0  |       val |= 4;  | 
216  | 0  |     if (CPU_ISSET_S(cpu + 3, setsize, set))  | 
217  | 0  |       val |= 8;  | 
218  |  | 
  | 
219  | 0  |     if (!ret && val)  | 
220  | 0  |       ret = ptr;  | 
221  | 0  |     *ptr++ = val_to_char(val);  | 
222  | 0  |   }  | 
223  | 0  |   *ptr = '\0';  | 
224  | 0  |   return ret ? ret : ptr - 1;  | 
225  | 0  | }  | 
226  |  |  | 
227  |  | /*  | 
228  |  |  * Parses string with CPUs mask.  | 
229  |  |  */  | 
230  |  | int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize)  | 
231  | 0  | { | 
232  | 0  |   int len = strlen(str);  | 
233  | 0  |   const char *ptr = str + len - 1;  | 
234  | 0  |   int cpu = 0;  | 
235  |  |  | 
236  |  |   /* skip 0x, it's all hex anyway */  | 
237  | 0  |   if (len > 1 && !memcmp(str, "0x", 2L))  | 
238  | 0  |     str += 2;  | 
239  |  | 
  | 
240  | 0  |   CPU_ZERO_S(setsize, set);  | 
241  |  | 
  | 
242  | 0  |   while (ptr >= str) { | 
243  | 0  |     char val;  | 
244  |  |  | 
245  |  |     /* cpu masks in /sys uses comma as a separator */  | 
246  | 0  |     if (*ptr == ',')  | 
247  | 0  |       ptr--;  | 
248  |  | 
  | 
249  | 0  |     val = char_to_val(*ptr);  | 
250  | 0  |     if (val == (char) -1)  | 
251  | 0  |       return -1;  | 
252  | 0  |     if (val & 1)  | 
253  | 0  |       CPU_SET_S(cpu, setsize, set);  | 
254  | 0  |     if (val & 2)  | 
255  | 0  |       CPU_SET_S(cpu + 1, setsize, set);  | 
256  | 0  |     if (val & 4)  | 
257  | 0  |       CPU_SET_S(cpu + 2, setsize, set);  | 
258  | 0  |     if (val & 8)  | 
259  | 0  |       CPU_SET_S(cpu + 3, setsize, set);  | 
260  | 0  |     ptr--;  | 
261  | 0  |     cpu += 4;  | 
262  | 0  |   }  | 
263  |  |  | 
264  | 0  |   return 0;  | 
265  | 0  | }  | 
266  |  |  | 
267  |  | static int nextnumber(const char *str, char **end, unsigned int *result)  | 
268  | 0  | { | 
269  | 0  |   errno = 0;  | 
270  | 0  |   if (str == NULL || *str == '\0' || !isdigit(*str))  | 
271  | 0  |     return -EINVAL;  | 
272  | 0  |   *result = (unsigned int) strtoul(str, end, 10);  | 
273  | 0  |   if (errno)  | 
274  | 0  |     return -errno;  | 
275  | 0  |   if (str == *end)  | 
276  | 0  |     return -EINVAL;  | 
277  | 0  |   return 0;  | 
278  | 0  | }  | 
279  |  |  | 
280  |  | /*  | 
281  |  |  * Parses string with list of CPU ranges.  | 
282  |  |  * Returns 0 on success.  | 
283  |  |  * Returns 1 on error.  | 
284  |  |  * Returns 2 if fail is set and a cpu number passed in the list doesn't fit  | 
285  |  |  * into the cpu_set. If fail is not set cpu numbers that do not fit are  | 
286  |  |  * ignored and 0 is returned instead.  | 
287  |  |  */  | 
288  |  | int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail)  | 
289  | 0  | { | 
290  | 0  |   const size_t max = cpuset_nbits(setsize);  | 
291  | 0  |   const char *p, *q;  | 
292  | 0  |   char *end = NULL;  | 
293  |  | 
  | 
294  | 0  |   q = str;  | 
295  | 0  |   CPU_ZERO_S(setsize, set);  | 
296  |  | 
  | 
297  | 0  |   while (p = q, q = nexttoken(q, ','), p) { | 
298  | 0  |     unsigned int a; /* beginning of range */  | 
299  | 0  |     unsigned int b; /* end of range */  | 
300  | 0  |     unsigned int s; /* stride */  | 
301  | 0  |     const char *c1, *c2;  | 
302  |  | 
  | 
303  | 0  |     if (nextnumber(p, &end, &a) != 0)  | 
304  | 0  |       return 1;  | 
305  | 0  |     b = a;  | 
306  | 0  |     s = 1;  | 
307  | 0  |     p = end;  | 
308  |  | 
  | 
309  | 0  |     c1 = nexttoken(p, '-');  | 
310  | 0  |     c2 = nexttoken(p, ',');  | 
311  |  | 
  | 
312  | 0  |     if (c1 != NULL && (c2 == NULL || c1 < c2)) { | 
313  | 0  |       if (nextnumber(c1, &end, &b) != 0)  | 
314  | 0  |         return 1;  | 
315  |  |  | 
316  | 0  |       c1 = end && *end ? nexttoken(end, ':') : NULL;  | 
317  |  | 
  | 
318  | 0  |       if (c1 != NULL && (c2 == NULL || c1 < c2)) { | 
319  | 0  |         if (nextnumber(c1, &end, &s) != 0)  | 
320  | 0  |           return 1;  | 
321  | 0  |         if (s == 0)  | 
322  | 0  |           return 1;  | 
323  | 0  |       }  | 
324  | 0  |     }  | 
325  |  |  | 
326  | 0  |     if (!(a <= b))  | 
327  | 0  |       return 1;  | 
328  | 0  |     while (a <= b) { | 
329  | 0  |       if (a >= max) { | 
330  | 0  |         if (fail)  | 
331  | 0  |           return 2;  | 
332  | 0  |         else  | 
333  | 0  |           break;  | 
334  | 0  |       }  | 
335  | 0  |       CPU_SET_S(a, setsize, set);  | 
336  | 0  |       a += s;  | 
337  | 0  |     }  | 
338  | 0  |   }  | 
339  |  |  | 
340  | 0  |   if (end && *end)  | 
341  | 0  |     return 1;  | 
342  | 0  |   return 0;  | 
343  | 0  | }  | 
344  |  |  | 
345  |  | #ifdef TEST_PROGRAM_CPUSET  | 
346  |  |  | 
347  |  | #include <getopt.h>  | 
348  |  |  | 
349  |  | int main(int argc, char *argv[])  | 
350  |  | { | 
351  |  |   cpu_set_t *set;  | 
352  |  |   size_t setsize, buflen, nbits;  | 
353  |  |   char *buf, *mask = NULL, *range = NULL;  | 
354  |  |   int ncpus = 2048, rc, c;  | 
355  |  |  | 
356  |  |   static const struct option longopts[] = { | 
357  |  |       { "ncpus", 1, NULL, 'n' }, | 
358  |  |       { "mask",  1, NULL, 'm' }, | 
359  |  |       { "range", 1, NULL, 'r' }, | 
360  |  |       { NULL,    0, NULL, 0 } | 
361  |  |   };  | 
362  |  |  | 
363  |  |   while ((c = getopt_long(argc, argv, "n:m:r:", longopts, NULL)) != -1) { | 
364  |  |     switch(c) { | 
365  |  |     case 'n':  | 
366  |  |       ncpus = atoi(optarg);  | 
367  |  |       break;  | 
368  |  |     case 'm':  | 
369  |  |       mask = strdup(optarg);  | 
370  |  |       break;  | 
371  |  |     case 'r':  | 
372  |  |       range = strdup(optarg);  | 
373  |  |       break;  | 
374  |  |     default:  | 
375  |  |       goto usage_err;  | 
376  |  |     }  | 
377  |  |   }  | 
378  |  |  | 
379  |  |   if (!mask && !range)  | 
380  |  |     goto usage_err;  | 
381  |  |  | 
382  |  |   set = cpuset_alloc(ncpus, &setsize, &nbits);  | 
383  |  |   if (!set)  | 
384  |  |     err(EXIT_FAILURE, "failed to allocate cpu set");  | 
385  |  |  | 
386  |  |   /*  | 
387  |  |   fprintf(stderr, "ncpus: %d, cpuset bits: %zd, cpuset bytes: %zd\n",  | 
388  |  |       ncpus, nbits, setsize);  | 
389  |  |   */  | 
390  |  |  | 
391  |  |   buflen = 7 * nbits;  | 
392  |  |   buf = malloc(buflen);  | 
393  |  |   if (!buf)  | 
394  |  |     err(EXIT_FAILURE, "failed to allocate cpu set buffer");  | 
395  |  |  | 
396  |  |   if (mask)  | 
397  |  |     rc = cpumask_parse(mask, set, setsize);  | 
398  |  |   else  | 
399  |  |     rc = cpulist_parse(range, set, setsize, 0);  | 
400  |  |  | 
401  |  |   if (rc)  | 
402  |  |     errx(EXIT_FAILURE, "failed to parse string: %s", mask ? : range);  | 
403  |  |  | 
404  |  |   printf("%-15s = %15s ", mask ? : range, | 
405  |  |         cpumask_create(buf, buflen, set, setsize));  | 
406  |  |   printf("[%s]\n", cpulist_create(buf, buflen, set, setsize)); | 
407  |  |  | 
408  |  |   free(buf);  | 
409  |  |   free(mask);  | 
410  |  |   free(range);  | 
411  |  |   cpuset_free(set);  | 
412  |  |  | 
413  |  |   return EXIT_SUCCESS;  | 
414  |  |  | 
415  |  | usage_err:  | 
416  |  |   fprintf(stderr,  | 
417  |  |     "usage: %s [--ncpus <num>] --mask <mask> | --range <list>\n",  | 
418  |  |     program_invocation_short_name);  | 
419  |  |   exit(EXIT_FAILURE);  | 
420  |  | }  | 
421  |  | #endif  |