]>
Commit | Line | Data |
---|---|---|
1 | #include <stdio.h> | |
2 | #include <string.h> | |
3 | #include <stdbool.h> | |
4 | #include <stdlib.h> | |
5 | ||
6 | /* | |
7 | * Helper functions for cpuset_in-set | |
8 | */ | |
9 | static char *cpuset_nexttok(const char *c) | |
10 | { | |
11 | char *r = strchr(c+1, ','); | |
12 | if (r) | |
13 | return r+1; | |
14 | return NULL; | |
15 | } | |
16 | ||
17 | static int cpuset_getrange(const char *c, int *a, int *b) | |
18 | { | |
19 | int ret; | |
20 | ||
21 | ret = sscanf(c, "%d-%d", a, b); | |
22 | return ret; | |
23 | } | |
24 | ||
25 | /* | |
26 | * cpusets are in format "1,2-3,4" | |
27 | * iow, comma-delimited ranges | |
28 | */ | |
29 | bool cpu_in_cpuset(int cpu, const char *cpuset) | |
30 | { | |
31 | const char *c; | |
32 | ||
33 | for (c = cpuset; c; c = cpuset_nexttok(c)) { | |
34 | int a, b, ret; | |
35 | ||
36 | ret = cpuset_getrange(c, &a, &b); | |
37 | if (ret == 1 && cpu == a) // "1" or "1,6" | |
38 | return true; | |
39 | else if (ret == 2 && cpu >= a && cpu <= b) // range match | |
40 | return true; | |
41 | } | |
42 | ||
43 | return false; | |
44 | } | |
45 |