]> git.ipfire.org Git - thirdparty/util-linux.git/blob - include/cpuset.h
Merge branch 'lslogins/man-shell' of https://github.com/t-8ch/util-linux
[thirdparty/util-linux.git] / include / cpuset.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7 #ifndef UTIL_LINUX_CPUSET_H
8 #define UTIL_LINUX_CPUSET_H
9
10 #include <sched.h>
11
12 /*
13 * Fallback for old or obscure libcs without dynamically allocated cpusets
14 *
15 * The following macros are based on code from glibc.
16 *
17 * The GNU C Library is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU Lesser General Public
19 * License as published by the Free Software Foundation; either
20 * version 2.1 of the License, or (at your option) any later version.
21 */
22 #if !HAVE_DECL_CPU_ALLOC
23
24 # define CPU_ZERO_S(setsize, cpusetp) \
25 do { \
26 size_t __i; \
27 size_t __imax = (setsize) / sizeof (__cpu_mask); \
28 __cpu_mask *__bits = (cpusetp)->__bits; \
29 for (__i = 0; __i < __imax; ++__i) \
30 __bits[__i] = 0; \
31 } while (0)
32
33 # define CPU_SET_S(cpu, setsize, cpusetp) \
34 ({ size_t __cpu = (cpu); \
35 __cpu < 8 * (setsize) \
36 ? (((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
37 |= __CPUMASK (__cpu)) \
38 : 0; })
39
40 # define CPU_ISSET_S(cpu, setsize, cpusetp) \
41 ({ size_t __cpu = (cpu); \
42 __cpu < 8 * (setsize) \
43 ? ((((__cpu_mask *) ((cpusetp)->__bits))[__CPUELT (__cpu)] \
44 & __CPUMASK (__cpu))) != 0 \
45 : 0; })
46
47 # define CPU_EQUAL_S(setsize, cpusetp1, cpusetp2) \
48 ({ __cpu_mask *__arr1 = (cpusetp1)->__bits; \
49 __cpu_mask *__arr2 = (cpusetp2)->__bits; \
50 size_t __imax = (setsize) / sizeof (__cpu_mask); \
51 size_t __i; \
52 for (__i = 0; __i < __imax; ++__i) \
53 if (__arr1[__i] != __arr2[__i]) \
54 break; \
55 __i == __imax; })
56
57 extern int __cpuset_count_s(size_t setsize, const cpu_set_t *set);
58 # define CPU_COUNT_S(setsize, cpusetp) __cpuset_count_s(setsize, cpusetp)
59
60 # define CPU_ALLOC_SIZE(count) \
61 ((((count) + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask))
62 # define CPU_ALLOC(count) (malloc(CPU_ALLOC_SIZE(count)))
63 # define CPU_FREE(cpuset) (free(cpuset))
64
65 #endif /* !HAVE_DECL_CPU_ALLOC */
66
67
68 #define cpuset_nbits(setsize) (8 * (setsize))
69
70 /*
71 * The @idx parameter returns an index of the first mask from @ary array where
72 * the @cpu is set.
73 *
74 * Returns: 0 if found, otherwise 1.
75 */
76 static inline int cpuset_ary_isset(size_t cpu, cpu_set_t **ary, size_t nmemb,
77 size_t setsize, size_t *idx)
78 {
79 size_t i;
80
81 for (i = 0; i < nmemb; i++) {
82 if (CPU_ISSET_S(cpu, setsize, ary[i])) {
83 *idx = i;
84 return 0;
85 }
86 }
87 return 1;
88 }
89
90 extern int get_max_number_of_cpus(void);
91
92 extern cpu_set_t *cpuset_alloc(int ncpus, size_t *setsize, size_t *nbits);
93 extern void cpuset_free(cpu_set_t *set);
94
95 extern char *cpulist_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
96 extern int cpulist_parse(const char *str, cpu_set_t *set, size_t setsize, int fail);
97
98 extern char *cpumask_create(char *str, size_t len, cpu_set_t *set, size_t setsize);
99 extern int cpumask_parse(const char *str, cpu_set_t *set, size_t setsize);
100
101 #endif /* UTIL_LINUX_CPUSET_H */