1 .\" Copyright (C) 2006 Michael Kerrisk
2 .\" and Copyright (C) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
27 .TH CPU_SET 3 2021-03-22 "Linux" "Linux Programmer's Manual"
29 CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT,
30 CPU_AND, CPU_OR, CPU_XOR, CPU_EQUAL,
31 CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE,
32 CPU_SET_S, CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S,
33 CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S, CPU_EQUAL_S \-
34 macros for manipulating CPU sets
37 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
40 .BI "void CPU_ZERO(cpu_set_t *" set );
42 .BI "void CPU_SET(int " cpu ", cpu_set_t *" set );
43 .BI "void CPU_CLR(int " cpu ", cpu_set_t *" set );
44 .BI "int CPU_ISSET(int " cpu ", cpu_set_t *" set );
46 .BI "int CPU_COUNT(cpu_set_t *" set );
48 .BI "void CPU_AND(cpu_set_t *" destset ,
49 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
50 .BI "void CPU_OR(cpu_set_t *" destset ,
51 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
52 .BI "void CPU_XOR(cpu_set_t *" destset ,
53 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
55 .BI "int CPU_EQUAL(cpu_set_t *" set1 ", cpu_set_t *" set2 );
57 .BI "cpu_set_t *CPU_ALLOC(int " num_cpus );
58 .BI "void CPU_FREE(cpu_set_t *" set );
59 .BI "size_t CPU_ALLOC_SIZE(int " num_cpus );
61 .BI "void CPU_ZERO_S(size_t " setsize ", cpu_set_t *" set );
63 .BI "void CPU_SET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
64 .BI "void CPU_CLR_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
65 .BI "int CPU_ISSET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
67 .BI "int CPU_COUNT_S(size_t " setsize ", cpu_set_t *" set );
69 .BI "void CPU_AND_S(size_t " setsize ", cpu_set_t *" destset ,
70 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
71 .BI "void CPU_OR_S(size_t " setsize ", cpu_set_t *" destset ,
72 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
73 .BI "void CPU_XOR_S(size_t " setsize ", cpu_set_t *" destset ,
74 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
76 .BI "int CPU_EQUAL_S(size_t " setsize ", cpu_set_t *" set1 \
77 ", cpu_set_t *" set2 );
82 data structure represents a set of CPUs.
84 .BR sched_setaffinity (2)
85 and similar interfaces.
89 data type is implemented as a bit mask.
90 However, the data structure should be treated as opaque:
91 all manipulation of CPU sets should be done via the macros
92 described in this page.
94 The following macros are provided to operate on the CPU set
100 so that it contains no CPUs.
121 Return the number of CPUs in
126 argument is specified, it should not produce side effects,
127 since the above macros may evaluate the argument more than once.
129 The first CPU on the system corresponds to a
131 value of 0, the next CPU corresponds to a
133 value of 1, and so on.
134 No assumptions should be made about particular CPUs being
135 available, or the set of CPUs being contiguous, since CPUs can
136 be taken offline dynamically or be otherwise absent.
139 (currently 1024) specifies a value one greater than the maximum CPU
140 number that can be stored in
143 The following macros perform logical operations on CPU sets:
146 Store the intersection of the sets
152 (which may be one of the source sets).
155 Store the union of the sets
161 (which may be one of the source sets).
164 Store the XOR of the sets
170 (which may be one of the source sets).
171 The XOR means the set of CPUs that are in either
178 Test whether two CPU set contain exactly the same CPUs.
179 .SS Dynamically sized CPU sets
180 Because some applications may require the ability to dynamically
181 size CPU sets (e.g., to allocate sets larger than that
182 defined by the standard
184 data type), glibc nowadays provides a set of macros to support this.
186 The following macros are used to allocate and deallocate CPU sets:
189 Allocate a CPU set large enough to hold CPUs
193 .BR CPU_ALLOC_SIZE ()
194 Return the size in bytes of the CPU set that would be needed to
195 hold CPUs in the range 0 to
197 This macro provides the value that can be used for the
201 macros described below.
204 Free a CPU set previously allocated by
207 The macros whose names end with "_S" are the analogs of
208 the similarly named macros without the suffix.
209 These macros perform the same tasks as their analogs,
210 but operate on the dynamically allocated CPU set(s) whose size is
221 otherwise, it returns 0.
226 return the number of CPUs in
232 return nonzero if the two CPU sets are equal; otherwise they return 0.
235 returns a pointer on success, or NULL on failure.
239 .BR CPU_ALLOC_SIZE ()
240 returns the number of bytes required to store a
241 CPU set of the specified cardinality.
243 The other functions do not return a value.
251 macros were added in glibc 2.3.3.
254 first appeared in glibc 2.6.
261 .BR CPU_ALLOC_SIZE (),
272 first appeared in glibc 2.7.
274 These interfaces are Linux-specific.
276 To duplicate a CPU set, use
279 Since CPU sets are bit masks allocated in units of long words,
280 the actual number of CPUs in a dynamically
281 allocated CPU set will be rounded up to the next multiple of
282 .IR "sizeof(unsigned long)" .
283 An application should consider the contents of these extra bits
286 Notwithstanding the similarity in the names,
287 note that the constant
289 indicates the number of CPUs in the
291 data type (thus, it is effectively a count of the bits in the bit mask),
296 macros is a size in bytes.
298 The data types for arguments and return values shown
299 in the SYNOPSIS are hints what about is expected in each case.
300 However, since these interfaces are implemented as macros,
301 the compiler won't necessarily catch all type errors
302 if you violate the suggestions.
304 On 32-bit platforms with glibc 2.8 and earlier,
306 allocates twice as much space as is required, and
307 .BR CPU_ALLOC_SIZE ()
308 returns a value twice as large as it should.
309 This bug should not affect the semantics of a program,
310 but does result in wasted memory
311 and less efficient operation of the macros that
312 operate on dynamically allocated CPU sets.
313 These bugs are fixed in glibc 2.9.
314 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7029
316 The following program demonstrates the use of some of the macros
317 used for dynamically allocated CPU sets.
328 main(int argc, char *argv[])
335 fprintf(stderr, "Usage: %s <num\-cpus>\en", argv[0]);
339 num_cpus = atoi(argv[1]);
341 cpusetp = CPU_ALLOC(num_cpus);
342 if (cpusetp == NULL) {
347 size = CPU_ALLOC_SIZE(num_cpus);
349 CPU_ZERO_S(size, cpusetp);
350 for (int cpu = 0; cpu < num_cpus; cpu += 2)
351 CPU_SET_S(cpu, size, cpusetp);
353 printf("CPU_COUNT() of set: %d\en", CPU_COUNT_S(size, cpusetp));
360 .BR sched_setaffinity (2),
361 .BR pthread_attr_setaffinity_np (3),
362 .BR pthread_setaffinity_np (3),