1 .\" Copyright (C) 2002 Robert Love
2 .\" and Copyright (C) 2006, 2015 Michael Kerrisk
4 .\" SPDX-License-Identifier: GPL-2.0-or-later
6 .\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
7 .\" 2004-04-20 mtk - fixed description of return value
8 .\" 2004-04-22 aeb - added glibc prototype history
9 .\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
10 .\" migration and that CPU affinity is a per-thread attribute.
11 .\" 2006-02-03 mtk -- Major rewrite
12 .\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
13 .\" separate CPU_SET(3) page.
15 .TH sched_setaffinity 2 (date) "Linux man-pages (unreleased)"
17 sched_setaffinity, sched_getaffinity \- \
18 set and get a thread's CPU affinity mask
21 .RI ( libc ", " \-lc )
24 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
27 .BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
28 .BI " const cpu_set_t *" mask );
29 .BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
30 .BI " cpu_set_t *" mask );
33 A thread's CPU affinity mask determines the set of CPUs on which
34 it is eligible to run.
35 On a multiprocessor system, setting the CPU affinity mask
36 can be used to obtain performance benefits.
38 by dedicating one CPU to a particular thread
39 (i.e., setting the affinity mask of that thread to specify a single CPU,
40 and setting the affinity mask of all other threads to exclude that CPU),
41 it is possible to ensure maximum execution speed for that thread.
42 Restricting a thread to run on a single CPU also avoids
43 the performance cost caused by the cache invalidation that occurs
44 when a thread ceases to execute on one CPU and then
45 recommences execution on a different CPU.
47 A CPU affinity mask is represented by the
49 structure, a "CPU set", pointed to by
51 A set of macros for manipulating CPU sets is described in
54 .BR sched_setaffinity ()
55 sets the CPU affinity mask of the thread whose ID is
57 to the value specified by
61 is zero, then the calling thread is used.
64 is the length (in bytes) of the data pointed to by
66 Normally this argument would be specified as
67 .IR "sizeof(cpu_set_t)" .
69 If the thread specified by
71 is not currently running on one of the CPUs specified in
73 then that thread is migrated to one of the CPUs specified in
76 .BR sched_getaffinity ()
77 writes the affinity mask of the thread whose ID is
81 structure pointed to by
85 argument specifies the size (in bytes) of
89 is zero, then the mask of the calling thread is returned.
92 .BR sched_setaffinity ()
94 .BR sched_getaffinity ()
95 return 0 (but see "C library/kernel differences" below,
96 which notes that the underlying
97 .BR sched_getaffinity ()
98 differs in its return value).
99 On failure, \-1 is returned, and
101 is set to indicate the error.
105 A supplied memory address was invalid.
108 The affinity bit mask
110 contains no processors that are currently physically on the system
111 and permitted to the thread according to any restrictions that
114 cgroups or the "cpuset" mechanism described in
118 .RB ( sched_getaffinity ()
119 and, before Linux 2.6.9,
120 .BR sched_setaffinity ())
122 is smaller than the size of the affinity mask used by the kernel.
125 .RB ( sched_setaffinity ())
126 The calling thread does not have appropriate privileges.
127 The caller needs an effective user ID equal to the real user ID
128 or effective user ID of the thread identified by
130 or it must possess the
132 capability in the user namespace of the thread
136 The thread whose ID is \fIpid\fP could not be found.
138 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
139 The system call wrappers were introduced in glibc 2.3.
140 Initially, the glibc interfaces included a
146 argument was removed, but was then restored in glibc 2.3.4, with type
149 These system calls are Linux-specific.
152 .BR sched_setaffinity (),
153 the set of CPUs on which the thread will actually run is
154 the intersection of the set specified in the
156 argument and the set of CPUs actually present on the system.
157 The system may further restrict the set of CPUs on which the thread
158 runs if the "cpuset" mechanism described in
161 These restrictions on the actual set of CPUs on which the thread
162 will run are silently imposed by the kernel.
164 There are various ways of determining the number of CPUs
165 available on the system, including: inspecting the contents of
169 to obtain the values of the
170 .B _SC_NPROCESSORS_CONF
172 .B _SC_NPROCESSORS_ONLN
173 parameters; and inspecting the list of CPU directories under
174 .IR /sys/devices/system/cpu/ .
177 has a description of the Linux scheduling scheme.
179 The affinity mask is a per-thread attribute that can be
180 adjusted independently for each of the threads in a thread group.
181 The value returned from a call to
183 can be passed in the argument
187 as 0 will set the attribute for the calling thread,
188 and passing the value returned from a call to
190 will set the attribute for the main thread of the thread group.
191 (If you are using the POSIX threads API, then use
192 .BR pthread_setaffinity_np (3)
194 .BR sched_setaffinity ().)
198 boot option can be used to isolate one or more CPUs at boot time,
199 so that no processes are scheduled onto those CPUs.
200 Following the use of this boot option,
201 the only way to schedule processes onto the isolated CPUs is via
202 .BR sched_setaffinity ()
206 For further information, see the kernel source file
207 .IR Documentation/admin\-guide/kernel\-parameters.txt .
208 As noted in that file,
210 is the preferred mechanism of isolating CPUs
211 (versus the alternative of manually setting the CPU affinity
212 of all processes on the system).
216 inherits its parent's CPU affinity mask.
217 The affinity mask is preserved across an
219 .SS C library/kernel differences
220 This manual page describes the glibc interface for the CPU affinity calls.
221 The actual system call interface is slightly different, with the
224 .IR "unsigned long\ *" ,
225 reflecting the fact that the underlying implementation of CPU
226 sets is a simple bit mask.
229 .BR sched_getaffinity ()
230 system call returns the number of bytes placed copied into the
233 this will be the minimum of
235 and the size (in bytes) of the
237 data type that is used internally by the kernel to
238 represent the CPU set bit mask.
239 .SS Handling systems with large CPU affinity masks
240 The underlying system calls (which represent CPU masks as bit masks of type
241 .IR "unsigned long\ *" )
242 impose no restriction on the size of the CPU mask.
245 data type used by glibc has a fixed size of 128 bytes,
246 meaning that the maximum CPU number that can be represented is 1023.
247 .\" FIXME . See https://sourceware.org/bugzilla/show_bug.cgi?id=15630
248 .\" and https://sourceware.org/ml/libc-alpha/2013-07/msg00288.html
249 If the kernel CPU affinity mask is larger than 1024,
250 then calls of the form:
254 sched_getaffinity(pid, sizeof(cpu_set_t), &mask);
260 the error produced by the underlying system call for the case where the
264 is smaller than the size of the affinity mask used by the kernel.
265 (Depending on the system CPU topology, the kernel affinity mask can
266 be substantially larger than the number of active CPUs in the system.)
268 When working on systems with large kernel CPU affinity masks,
269 one must dynamically allocate the
273 Currently, the only way to do this is by probing for the size
274 of the required mask using
275 .BR sched_getaffinity ()
276 calls with increasing mask sizes (until the call does not fail with the error
281 may allocate a slightly larger CPU set than requested
282 (because CPU sets are implemented as bit masks allocated in units of
285 .BR sched_getaffinity ()
286 can set bits beyond the requested allocation size, because the kernel
287 sees a few additional bits.
288 Therefore, the caller should iterate over the bits in the returned set,
289 counting those which are set, and stop upon reaching the value returned by
291 (rather than iterating over the number of bits
292 requested to be allocated).
294 The program below creates a child process.
295 The parent and child then each assign themselves to a specified CPU
296 and execute identical loops that consume some CPU time.
297 Before terminating, the parent waits for the child to complete.
298 The program takes three command-line arguments:
299 the CPU number for the parent,
300 the CPU number for the child,
301 and the number of loop iterations that both processes should perform.
303 As the sample runs below demonstrate, the amount of real and CPU time
304 consumed when running the program will depend on intra-core caching effects
305 and whether the processes are using the same CPU.
309 to determine that this (x86)
310 system has two cores, each with two CPUs:
314 $ \fBlscpu | egrep \-i \[aq]core.*:|socket\[aq]\fP
315 Thread(s) per core: 2
316 Core(s) per socket: 2
321 We then time the operation of the example program for three cases:
322 both processes running on the same CPU;
323 both processes running on different CPUs on the same core;
324 and both processes running on different CPUs on different cores.
328 $ \fBtime \-p ./a.out 0 0 100000000\fP
332 $ \fBtime \-p ./a.out 0 1 100000000\fP
336 $ \fBtime \-p ./a.out 0 3 100000000\fP
344 .\" SRC BEGIN (sched_setaffinity.c)
351 #include <sys/wait.h>
355 main(int argc, char *argv[])
357 int parentCPU, childCPU;
362 fprintf(stderr, "Usage: %s parent\-cpu child\-cpu num\-loops\en",
367 parentCPU = atoi(argv[1]);
368 childCPU = atoi(argv[2]);
369 nloops = atoi(argv[3]);
374 case \-1: /* Error */
375 err(EXIT_FAILURE, "fork");
378 CPU_SET(childCPU, &set);
380 if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
381 err(EXIT_FAILURE, "sched_setaffinity");
383 for (unsigned int j = 0; j < nloops; j++)
388 default: /* Parent */
389 CPU_SET(parentCPU, &set);
391 if (sched_setaffinity(getpid(), sizeof(set), &set) == \-1)
392 err(EXIT_FAILURE, "sched_setaffinity");
394 for (unsigned int j = 0; j < nloops; j++)
397 wait(NULL); /* Wait for child to terminate */
414 .BR sched_get_priority_max (2),
415 .BR sched_get_priority_min (2),
416 .BR sched_getscheduler (2),
417 .BR sched_setscheduler (2),
421 .BR pthread_setaffinity_np (3),
422 .BR sched_getcpu (3),
423 .BR capabilities (7),