1SCHED_SETAFFINITY(2) Linux Programmer's Manual SCHED_SETAFFINITY(2)
2
3
4
6 sched_setaffinity, sched_getaffinity - set and get a thread's CPU
7 affinity mask
8
10 #define _GNU_SOURCE /* See feature_test_macros(7) */
11 #include <sched.h>
12
13 int sched_setaffinity(pid_t pid, size_t cpusetsize,
14 const cpu_set_t *mask);
15
16 int sched_getaffinity(pid_t pid, size_t cpusetsize,
17 cpu_set_t *mask);
18
20 A thread's CPU affinity mask determines the set of CPUs on which it is
21 eligible to run. On a multiprocessor system, setting the CPU affinity
22 mask can be used to obtain performance benefits. For example, by dedi‐
23 cating one CPU to a particular thread (i.e., setting the affinity mask
24 of that thread to specify a single CPU, and setting the affinity mask
25 of all other threads to exclude that CPU), it is possible to ensure
26 maximum execution speed for that thread. Restricting a thread to run
27 on a single CPU also avoids the performance cost caused by the cache
28 invalidation that occurs when a thread ceases to execute on one CPU and
29 then recommences execution on a different CPU.
30
31 A CPU affinity mask is represented by the cpu_set_t structure, a "CPU
32 set", pointed to by mask. A set of macros for manipulating CPU sets is
33 described in CPU_SET(3).
34
35 sched_setaffinity() sets the CPU affinity mask of the thread whose ID
36 is pid to the value specified by mask. If pid is zero, then the call‐
37 ing thread is used. The argument cpusetsize is the length (in bytes)
38 of the data pointed to by mask. Normally this argument would be speci‐
39 fied as sizeof(cpu_set_t).
40
41 If the thread specified by pid is not currently running on one of the
42 CPUs specified in mask, then that thread is migrated to one of the CPUs
43 specified in mask.
44
45 sched_getaffinity() writes the affinity mask of the thread whose ID is
46 pid into the cpu_set_t structure pointed to by mask. The cpusetsize
47 argument specifies the size (in bytes) of mask. If pid is zero, then
48 the mask of the calling thread is returned.
49
51 On success, sched_setaffinity() and sched_getaffinity() return 0 (but
52 see "C library/kernel differences" below, which notes that the underly‐
53 ing sched_getaffinity() differs in its return value). On error, -1 is
54 returned, and errno is set appropriately.
55
57 EFAULT A supplied memory address was invalid.
58
59 EINVAL The affinity bit mask mask contains no processors that are cur‐
60 rently physically on the system and permitted to the thread
61 according to any restrictions that may be imposed by cpuset
62 cgroups or the "cpuset" mechanism described in cpuset(7).
63
64 EINVAL (sched_getaffinity() and, in kernels before 2.6.9,
65 sched_setaffinity()) cpusetsize is smaller than the size of the
66 affinity mask used by the kernel.
67
68 EPERM (sched_setaffinity()) The calling thread does not have appropri‐
69 ate privileges. The caller needs an effective user ID equal to
70 the real user ID or effective user ID of the thread identified
71 by pid, or it must possess the CAP_SYS_NICE capability in the
72 user namespace of the thread pid.
73
74 ESRCH The thread whose ID is pid could not be found.
75
77 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
78 The system call wrappers were introduced in glibc 2.3. Initially, the
79 glibc interfaces included a cpusetsize argument, typed as unsigned int.
80 In glibc 2.3.3, the cpusetsize argument was removed, but was then
81 restored in glibc 2.3.4, with type size_t.
82
84 These system calls are Linux-specific.
85
87 After a call to sched_setaffinity(), the set of CPUs on which the
88 thread will actually run is the intersection of the set specified in
89 the mask argument and the set of CPUs actually present on the system.
90 The system may further restrict the set of CPUs on which the thread
91 runs if the "cpuset" mechanism described in cpuset(7) is being used.
92 These restrictions on the actual set of CPUs on which the thread will
93 run are silently imposed by the kernel.
94
95 There are various ways of determining the number of CPUs available on
96 the system, including: inspecting the contents of /proc/cpuinfo; using
97 sysconf(3) to obtain the values of the _SC_NPROCESSORS_CONF and
98 _SC_NPROCESSORS_ONLN parameters; and inspecting the list of CPU direc‐
99 tories under /sys/devices/system/cpu/.
100
101 sched(7) has a description of the Linux scheduling scheme.
102
103 The affinity mask is a per-thread attribute that can be adjusted inde‐
104 pendently for each of the threads in a thread group. The value
105 returned from a call to gettid(2) can be passed in the argument pid.
106 Specifying pid as 0 will set the attribute for the calling thread, and
107 passing the value returned from a call to getpid(2) will set the
108 attribute for the main thread of the thread group. (If you are using
109 the POSIX threads API, then use pthread_setaffinity_np(3) instead of
110 sched_setaffinity().)
111
112 The isolcpus boot option can be used to isolate one or more CPUs at
113 boot time, so that no processes are scheduled onto those CPUs. Follow‐
114 ing the use of this boot option, the only way to schedule processes
115 onto the isolated CPUs is via sched_setaffinity() or the cpuset(7)
116 mechanism. For further information, see the kernel source file Docu‐
117 mentation/admin-guide/kernel-parameters.txt. As noted in that file,
118 isolcpus is the preferred mechanism of isolating CPUs (versus the
119 alternative of manually setting the CPU affinity of all processes on
120 the system).
121
122 A child created via fork(2) inherits its parent's CPU affinity mask.
123 The affinity mask is preserved across an execve(2).
124
125 C library/kernel differences
126 This manual page describes the glibc interface for the CPU affinity
127 calls. The actual system call interface is slightly different, with
128 the mask being typed as unsigned long *, reflecting the fact that the
129 underlying implementation of CPU sets is a simple bit mask.
130
131 On success, the raw sched_getaffinity() system call returns the number
132 of bytes placed copied into the mask buffer; this will be the minimum
133 of cpusetsize and the size (in bytes) of the cpumask_t data type that
134 is used internally by the kernel to represent the CPU set bit mask.
135
136 Handling systems with large CPU affinity masks
137 The underlying system calls (which represent CPU masks as bit masks of
138 type unsigned long *) impose no restriction on the size of the CPU
139 mask. However, the cpu_set_t data type used by glibc has a fixed size
140 of 128 bytes, meaning that the maximum CPU number that can be repre‐
141 sented is 1023. If the kernel CPU affinity mask is larger than 1024,
142 then calls of the form:
143
144 sched_getaffinity(pid, sizeof(cpu_set_t), &mask);
145
146 fail with the error EINVAL, the error produced by the underlying system
147 call for the case where the mask size specified in cpusetsize is
148 smaller than the size of the affinity mask used by the kernel.
149 (Depending on the system CPU topology, the kernel affinity mask can be
150 substantially larger than the number of active CPUs in the system.)
151
152 When working on systems with large kernel CPU affinity masks, one must
153 dynamically allocate the mask argument (see CPU_ALLOC(3)). Currently,
154 the only way to do this is by probing for the size of the required mask
155 using sched_getaffinity() calls with increasing mask sizes (until the
156 call does not fail with the error EINVAL).
157
158 Be aware that CPU_ALLOC(3) may allocate a slightly larger CPU set than
159 requested (because CPU sets are implemented as bit masks allocated in
160 units of sizeof(long)). Consequently, sched_getaffinity() can set bits
161 beyond the requested allocation size, because the kernel sees a few
162 additional bits. Therefore, the caller should iterate over the bits in
163 the returned set, counting those which are set, and stop upon reaching
164 the value returned by CPU_COUNT(3) (rather than iterating over the num‐
165 ber of bits requested to be allocated).
166
168 The program below creates a child process. The parent and child then
169 each assign themselves to a specified CPU and execute identical loops
170 that consume some CPU time. Before terminating, the parent waits for
171 the child to complete. The program takes three command-line arguments:
172 the CPU number for the parent, the CPU number for the child, and the
173 number of loop iterations that both processes should perform.
174
175 As the sample runs below demonstrate, the amount of real and CPU time
176 consumed when running the program will depend on intra-core caching
177 effects and whether the processes are using the same CPU.
178
179 We first employ lscpu(1) to determine that this (x86) system has two
180 cores, each with two CPUs:
181
182 $ lscpu | egrep -i 'core.*:|socket'
183 Thread(s) per core: 2
184 Core(s) per socket: 2
185 Socket(s): 1
186
187 We then time the operation of the example program for three cases: both
188 processes running on the same CPU; both processes running on different
189 CPUs on the same core; and both processes running on different CPUs on
190 different cores.
191
192 $ time -p ./a.out 0 0 100000000
193 real 14.75
194 user 3.02
195 sys 11.73
196 $ time -p ./a.out 0 1 100000000
197 real 11.52
198 user 3.98
199 sys 19.06
200 $ time -p ./a.out 0 3 100000000
201 real 7.89
202 user 3.29
203 sys 12.07
204
205 Program source
206
207 #define _GNU_SOURCE
208 #include <sched.h>
209 #include <stdio.h>
210 #include <stdlib.h>
211 #include <unistd.h>
212 #include <sys/wait.h>
213
214 #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \
215 } while (0)
216
217 int
218 main(int argc, char *argv[])
219 {
220 cpu_set_t set;
221 int parentCPU, childCPU;
222 int nloops, j;
223
224 if (argc != 4) {
225 fprintf(stderr, "Usage: %s parent-cpu child-cpu num-loops\n",
226 argv[0]);
227 exit(EXIT_FAILURE);
228 }
229
230 parentCPU = atoi(argv[1]);
231 childCPU = atoi(argv[2]);
232 nloops = atoi(argv[3]);
233
234 CPU_ZERO(&set);
235
236 switch (fork()) {
237 case -1: /* Error */
238 errExit("fork");
239
240 case 0: /* Child */
241 CPU_SET(childCPU, &set);
242
243 if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
244 errExit("sched_setaffinity");
245
246 for (j = 0; j < nloops; j++)
247 getppid();
248
249 exit(EXIT_SUCCESS);
250
251 default: /* Parent */
252 CPU_SET(parentCPU, &set);
253
254 if (sched_setaffinity(getpid(), sizeof(set), &set) == -1)
255 errExit("sched_setaffinity");
256
257 for (j = 0; j < nloops; j++)
258 getppid();
259
260 wait(NULL); /* Wait for child to terminate */
261 exit(EXIT_SUCCESS);
262 }
263 }
264
266 lscpu(1), nproc(1), taskset(1), clone(2), getcpu(2), getpriority(2),
267 gettid(2), nice(2), sched_get_priority_max(2),
268 sched_get_priority_min(2), sched_getscheduler(2),
269 sched_setscheduler(2), setpriority(2), CPU_SET(3), get_nprocs(3),
270 pthread_setaffinity_np(3), sched_getcpu(3), capabilities(7), cpuset(7),
271 sched(7), numactl(8)
272
274 This page is part of release 5.04 of the Linux man-pages project. A
275 description of the project, information about reporting bugs, and the
276 latest version of this page, can be found at
277 https://www.kernel.org/doc/man-pages/.
278
279
280
281Linux 2019-10-10 SCHED_SETAFFINITY(2)