1 .\" Copyright (C) 2006 Michael Kerrisk
2 .\" and Copyright (C) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH CPU_SET 3 (date) "Linux man-pages (unreleased)"
9 CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT,
10 CPU_AND, CPU_OR, CPU_XOR, CPU_EQUAL,
11 CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE,
12 CPU_SET_S, CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S,
13 CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S, CPU_EQUAL_S \-
14 macros for manipulating CPU sets
17 .RI ( libc ", " \-lc )
20 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
23 .BI "void CPU_ZERO(cpu_set_t *" set );
25 .BI "void CPU_SET(int " cpu ", cpu_set_t *" set );
26 .BI "void CPU_CLR(int " cpu ", cpu_set_t *" set );
27 .BI "int CPU_ISSET(int " cpu ", cpu_set_t *" set );
29 .BI "int CPU_COUNT(cpu_set_t *" set );
31 .BI "void CPU_AND(cpu_set_t *" destset ,
32 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
33 .BI "void CPU_OR(cpu_set_t *" destset ,
34 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
35 .BI "void CPU_XOR(cpu_set_t *" destset ,
36 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
38 .BI "int CPU_EQUAL(cpu_set_t *" set1 ", cpu_set_t *" set2 );
40 .BI "cpu_set_t *CPU_ALLOC(int " num_cpus );
41 .BI "void CPU_FREE(cpu_set_t *" set );
42 .BI "size_t CPU_ALLOC_SIZE(int " num_cpus );
44 .BI "void CPU_ZERO_S(size_t " setsize ", cpu_set_t *" set );
46 .BI "void CPU_SET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
47 .BI "void CPU_CLR_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
48 .BI "int CPU_ISSET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
50 .BI "int CPU_COUNT_S(size_t " setsize ", cpu_set_t *" set );
52 .BI "void CPU_AND_S(size_t " setsize ", cpu_set_t *" destset ,
53 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
54 .BI "void CPU_OR_S(size_t " setsize ", cpu_set_t *" destset ,
55 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
56 .BI "void CPU_XOR_S(size_t " setsize ", cpu_set_t *" destset ,
57 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
59 .BI "int CPU_EQUAL_S(size_t " setsize ", cpu_set_t *" set1 \
60 ", cpu_set_t *" set2 );
65 data structure represents a set of CPUs.
67 .BR sched_setaffinity (2)
68 and similar interfaces.
72 data type is implemented as a bit mask.
73 However, the data structure should be treated as opaque:
74 all manipulation of CPU sets should be done via the macros
75 described in this page.
77 The following macros are provided to operate on the CPU set
83 so that it contains no CPUs.
104 Return the number of CPUs in
109 argument is specified, it should not produce side effects,
110 since the above macros may evaluate the argument more than once.
112 The first CPU on the system corresponds to a
114 value of 0, the next CPU corresponds to a
116 value of 1, and so on.
117 No assumptions should be made about particular CPUs being
118 available, or the set of CPUs being contiguous, since CPUs can
119 be taken offline dynamically or be otherwise absent.
122 (currently 1024) specifies a value one greater than the maximum CPU
123 number that can be stored in
126 The following macros perform logical operations on CPU sets:
129 Store the intersection of the sets
135 (which may be one of the source sets).
138 Store the union of the sets
144 (which may be one of the source sets).
147 Store the XOR of the sets
153 (which may be one of the source sets).
154 The XOR means the set of CPUs that are in either
161 Test whether two CPU set contain exactly the same CPUs.
162 .SS Dynamically sized CPU sets
163 Because some applications may require the ability to dynamically
164 size CPU sets (e.g., to allocate sets larger than that
165 defined by the standard
167 data type), glibc nowadays provides a set of macros to support this.
169 The following macros are used to allocate and deallocate CPU sets:
172 Allocate a CPU set large enough to hold CPUs
176 .BR CPU_ALLOC_SIZE ()
177 Return the size in bytes of the CPU set that would be needed to
178 hold CPUs in the range 0 to
180 This macro provides the value that can be used for the
184 macros described below.
187 Free a CPU set previously allocated by
190 The macros whose names end with "_S" are the analogs of
191 the similarly named macros without the suffix.
192 These macros perform the same tasks as their analogs,
193 but operate on the dynamically allocated CPU set(s) whose size is
204 otherwise, it returns 0.
209 return the number of CPUs in
215 return nonzero if the two CPU sets are equal; otherwise they return 0.
218 returns a pointer on success, or NULL on failure.
222 .BR CPU_ALLOC_SIZE ()
223 returns the number of bytes required to store a
224 CPU set of the specified cardinality.
226 The other functions do not return a value.
234 macros were added in glibc 2.3.3.
237 first appeared in glibc 2.6.
244 .BR CPU_ALLOC_SIZE (),
255 first appeared in glibc 2.7.
257 These interfaces are Linux-specific.
259 To duplicate a CPU set, use
262 Since CPU sets are bit masks allocated in units of long words,
263 the actual number of CPUs in a dynamically
264 allocated CPU set will be rounded up to the next multiple of
265 .IR "sizeof(unsigned long)" .
266 An application should consider the contents of these extra bits
269 Notwithstanding the similarity in the names,
270 note that the constant
272 indicates the number of CPUs in the
274 data type (thus, it is effectively a count of the bits in the bit mask),
279 macros is a size in bytes.
281 The data types for arguments and return values shown
282 in the SYNOPSIS are hints what about is expected in each case.
283 However, since these interfaces are implemented as macros,
284 the compiler won't necessarily catch all type errors
285 if you violate the suggestions.
287 On 32-bit platforms with glibc 2.8 and earlier,
289 allocates twice as much space as is required, and
290 .BR CPU_ALLOC_SIZE ()
291 returns a value twice as large as it should.
292 This bug should not affect the semantics of a program,
293 but does result in wasted memory
294 and less efficient operation of the macros that
295 operate on dynamically allocated CPU sets.
296 These bugs are fixed in glibc 2.9.
297 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7029
299 The following program demonstrates the use of some of the macros
300 used for dynamically allocated CPU sets.
302 .\" SRC BEGIN (CPU_SET.c)
313 main(int argc, char *argv[])
316 size_t size, num_cpus;
319 fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]);
323 num_cpus = atoi(argv[1]);
325 cpusetp = CPU_ALLOC(num_cpus);
326 if (cpusetp == NULL) {
331 size = CPU_ALLOC_SIZE(num_cpus);
333 CPU_ZERO_S(size, cpusetp);
334 for (size_t cpu = 0; cpu < num_cpus; cpu += 2)
335 CPU_SET_S(cpu, size, cpusetp);
337 printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp));
345 .BR sched_setaffinity (2),
346 .BR pthread_attr_setaffinity_np (3),
347 .BR pthread_setaffinity_np (3),