1 .\" Copyright (C) 2006 Michael Kerrisk
2 .\" and Copyright (C) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
25 .TH CPU_SET 3 2012-03-15 "Linux" "Linux Programmer's Manual"
27 CPU_SET, CPU_CLR, CPU_ISSET, CPU_ZERO, CPU_COUNT,
28 CPU_AND, CPU_OR, CPU_XOR, CPU_EQUAL,
29 CPU_ALLOC, CPU_ALLOC_SIZE, CPU_FREE,
30 CPU_SET_S, CPU_CLR_S, CPU_ISSET_S, CPU_ZERO_S,
31 CPU_COUNT_S, CPU_AND_S, CPU_OR_S, CPU_XOR_S, CPU_EQUAL_S \-
32 macros for manipulating CPU sets
35 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
38 .BI "void CPU_ZERO(cpu_set_t *" set );
40 .BI "void CPU_SET(int " cpu ", cpu_set_t *" set );
41 .BI "void CPU_CLR(int " cpu ", cpu_set_t *" set );
42 .BI "int CPU_ISSET(int " cpu ", cpu_set_t *" set );
44 .BI "int CPU_COUNT(cpu_set_t *" set );
46 .BI "void CPU_AND(cpu_set_t *" destset ,
47 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
48 .BI "void CPU_OR(cpu_set_t *" destset ,
49 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
50 .BI "void CPU_XOR(cpu_set_t *" destset ,
51 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
53 .BI "int CPU_EQUAL(cpu_set_t *" set1 ", cpu_set_t *" set2 );
55 .BI "cpu_set_t *CPU_ALLOC(int " num_cpus );
56 .BI "void CPU_FREE(cpu_set_t *" set );
57 .BI "size_t CPU_ALLOC_SIZE(int " num_cpus );
59 .BI "void CPU_ZERO_S(size_t " setsize ", cpu_set_t *" set );
61 .BI "void CPU_SET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
62 .BI "void CPU_CLR_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
63 .BI "int CPU_ISSET_S(int " cpu ", size_t " setsize ", cpu_set_t *" set );
65 .BI "int CPU_COUNT_S(size_t " setsize ", cpu_set_t *" set );
67 .BI "void CPU_AND_S(size_t " setsize ", cpu_set_t *" destset ,
68 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
69 .BI "void CPU_OR_S(size_t " setsize ", cpu_set_t *" destset ,
70 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
71 .BI "void CPU_XOR_S(size_t " setsize ", cpu_set_t *" destset ,
72 .BI " cpu_set_t *" srcset1 ", cpu_set_t *" srcset2 );
74 .BI "int CPU_EQUAL_S(size_t " setsize ", cpu_set_t *" set1 \
75 ", cpu_set_t *" set2 );
80 data structure represents a set of CPUs.
82 .BR sched_setaffinity (2)
83 and similar interfaces.
87 data type is implemented as a bitset.
88 However, the data structure treated as considered opaque:
89 all manipulation of CPU sets should be done via the macros
90 described in this page.
92 The following macros are provided to operate on the CPU set
98 so that it contains no CPUs.
119 Return the number of CPUs in
124 argument is specified, it should not produce side effects,
125 since the above macros may evaluate the argument more than once.
127 The first available CPU on the system corresponds to a
129 value of 0, the next CPU corresponds to a
131 value of 1, and so on.
134 (currently 1024) specifies a value one greater than the maximum CPU
135 number that can be stored in
138 The following macros perform logical operations on CPU sets:
141 Store the intersection of the sets
147 (which may be one of the source sets).
150 Store the union of the sets
156 (which may be one of the source sets).
159 Store the XOR of the sets
165 (which may be one of the source sets).
166 The XOR means the set of CPUs that are in either
173 Test whether two CPU set contain exactly the same CPUs.
174 .SS Dynamically sized CPU sets
175 Because some applications may require the ability to dynamically
176 size CPU sets (e.g., to allocate sets larger than that
177 defined by the standard
179 data type), glibc nowadays provides a set of macros to support this.
181 The following macros are used to allocate and deallocate CPU sets:
184 Allocate a CPU set large enough to hold CPUs
188 .BR CPU_ALLOC_SIZE ()
189 Return the size in bytes of the CPU set that would be needed to
190 hold CPUs in the range 0 to
192 This macro provides the value that can be used for the
196 macros described below.
199 Free a CPU set previously allocated by
202 The macros whose names end with "_S" are the analogs of
203 the similarly named macros without the suffix.
204 These macros perform the same tasks as their analogs,
205 but operate on the dynamically allocated CPU set(s) whose size is
216 otherwise, it returns 0.
221 return the number of CPUs in
227 return nonzero if the two CPU sets are equal; otherwise it returns 0.
230 returns a pointer on success, or NULL on failure.
234 .BR CPU_ALLOC_SIZE ()
235 returns the number of bytes required to store a
236 CPU set of the specified cardinality.
238 The other functions do not return a value.
246 macros were added in glibc 2.3.3.
249 first appeared in glibc 2.6.
256 .BR CPU_ALLOC_SIZE (),
267 first appeared in glibc 2.7.
269 These interfaces are Linux-specific.
271 To duplicate a CPU set, use
274 Since CPU sets are bitsets allocated in units of long words,
275 the actual number of CPUs in a dynamically
276 allocated CPU set will be rounded up to the next multiple of
277 .IR "sizeof(unsigned long)" .
278 An application should consider the contents of these extra bits
281 Notwithstanding the similarity in the names,
282 note that the constant
284 indicates the number of CPUs in the
286 data type (thus, it is effectively a count of bits in the bitset),
291 macros is a size in bytes.
293 The data types for arguments and return values shown
294 in the SYNOPSIS are hints what about is expected in each case.
295 However, since these interfaces are implemented as macros,
296 the compiler won't necessarily catch all type errors
297 if you violate the suggestions.
299 On 32-bit platforms with glibc 2.8 and earlier,
301 allocates twice as much space as is required, and
302 .BR CPU_ALLOC_SIZE ()
303 returns a value twice as large as it should.
304 This bug should not affect the semantics of a program,
305 but does result in wasted memory
306 and less efficient operation of the macros that
307 operate on dynamically allocated CPU sets.
308 These bugs are fixed in glibc 2.9.
309 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=7029
311 The following program demonstrates the use of some of the macros
312 used for dynamically allocated CPU sets.
323 main(int argc, char *argv[])
330 fprintf(stderr, "Usage: %s <num\-cpus>\\n", argv[0]);
334 num_cpus = atoi(argv[1]);
336 cpusetp = CPU_ALLOC(num_cpus);
337 if (cpusetp == NULL) {
342 size = CPU_ALLOC_SIZE(num_cpus);
344 CPU_ZERO_S(size, cpusetp);
345 for (cpu = 0; cpu < num_cpus; cpu += 2)
346 CPU_SET_S(cpu, size, cpusetp);
348 printf("CPU_COUNT() of set: %d\\n", CPU_COUNT_S(size, cpusetp));
355 .BR sched_setaffinity (2),
356 .BR pthread_attr_setaffinity_np (3),
357 .BR pthread_setaffinity_np (3),