]>
git.proxmox.com Git - mirror_lxcfs.git/blob - src/cpuset_parse.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #ifndef FUSE_USE_VERSION
8 #define FUSE_USE_VERSION 26
11 #define _FILE_OFFSET_BITS 64
13 #define __STDC_FORMAT_MACROS
21 #include "cgroups/cgroup.h"
22 #include "cgroups/cgroup_utils.h"
24 #include "memory_utils.h"
25 #include "proc_loadavg.h"
29 * Helper functions for cpuset_in-set
31 static char *cpuset_nexttok(const char *c
)
35 r
= strchr(c
+ 1, ',');
36 return r
? (r
+ 1) : NULL
;
39 static int cpuset_getrange(const char *c
, int *a
, int *b
)
43 ret
= sscanf(c
, "%d-%d", a
, b
);
48 * cpusets are in format "1,2-3,4"
49 * iow, comma-delimited ranges
51 bool cpu_in_cpuset(int cpu
, const char *cpuset
)
53 for (const char *c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
56 ret
= cpuset_getrange(c
, &a
, &b
);
57 if (ret
== 1 && cpu
== a
) /* "1" or "1,6" */
59 else if (ret
== 2 && cpu
>= a
&& cpu
<= b
) /* range match */
67 * get cpu number in cpuset
69 int cpu_number_in_cpuset(const char *cpuset
)
73 for (const char *c
= cpuset
; c
; c
= cpuset_nexttok(c
)) {
76 ret
= cpuset_getrange(c
, &a
, &b
);
80 cpu_number
+= a
> b
? a
- b
+ 1 : b
- a
+ 1;