]>
Commit | Line | Data |
---|---|---|
db0463bf | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
1f5596dd | 2 | |
f834b6bf SP |
3 | #include "config.h" |
4 | ||
fa47bb52 SH |
5 | #include <stdio.h> |
6 | #include <string.h> | |
7 | #include <stdbool.h> | |
8 | #include <stdlib.h> | |
9 | ||
1f5596dd CB |
10 | #include "cgroups/cgroup.h" |
11 | #include "cgroups/cgroup_utils.h" | |
1f5596dd | 12 | #include "memory_utils.h" |
1f5596dd | 13 | |
fa47bb52 SH |
14 | /* |
15 | * Helper functions for cpuset_in-set | |
16 | */ | |
17 | static char *cpuset_nexttok(const char *c) | |
18 | { | |
e3ad3cb3 CB |
19 | char *r; |
20 | ||
21 | r = strchr(c + 1, ','); | |
22 | return r ? (r + 1) : NULL; | |
fa47bb52 SH |
23 | } |
24 | ||
25 | static int cpuset_getrange(const char *c, int *a, int *b) | |
26 | { | |
27 | int ret; | |
28 | ||
29 | ret = sscanf(c, "%d-%d", a, b); | |
30 | return ret; | |
31 | } | |
32 | ||
33 | /* | |
34 | * cpusets are in format "1,2-3,4" | |
35 | * iow, comma-delimited ranges | |
36 | */ | |
37 | bool cpu_in_cpuset(int cpu, const char *cpuset) | |
38 | { | |
e3ad3cb3 | 39 | for (const char *c = cpuset; c; c = cpuset_nexttok(c)) { |
fa47bb52 SH |
40 | int a, b, ret; |
41 | ||
42 | ret = cpuset_getrange(c, &a, &b); | |
e3ad3cb3 | 43 | if (ret == 1 && cpu == a) /* "1" or "1,6" */ |
fa47bb52 | 44 | return true; |
757a63e7 | 45 | else if (ret == 2 && cpu >= a && cpu <= b) /* range match */ |
fa47bb52 SH |
46 | return true; |
47 | } | |
48 | ||
49 | return false; | |
50 | } | |
51 | ||
1e14fd04 HY |
52 | /* |
53 | * get cpu number in cpuset | |
54 | */ | |
55 | int cpu_number_in_cpuset(const char *cpuset) | |
56 | { | |
1e14fd04 HY |
57 | int cpu_number = 0; |
58 | ||
e3ad3cb3 | 59 | for (const char *c = cpuset; c; c = cpuset_nexttok(c)) { |
1e14fd04 HY |
60 | int a, b, ret; |
61 | ||
62 | ret = cpuset_getrange(c, &a, &b); | |
63 | if (ret == 1) | |
64 | cpu_number++; | |
65 | else if (ret == 2) | |
e3ad3cb3 | 66 | cpu_number += a > b ? a - b + 1 : b - a + 1; |
1e14fd04 | 67 | } |
e3ad3cb3 | 68 | |
1e14fd04 HY |
69 | return cpu_number; |
70 | } |