-
Notifications
You must be signed in to change notification settings - Fork 0
/
sched_setaffinity.html
497 lines (393 loc) · 16 KB
/
sched_setaffinity.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
<!-- Creator : groff version 1.22.4 -->
<!-- CreationDate: Wed Jan 29 11:25:44 2020 -->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta name="generator" content="groff -Thtml, see www.gnu.org">
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<meta name="Content-Style" content="text/css">
<style type="text/css">
p { margin-top: 0; margin-bottom: 0; vertical-align: top }
pre { margin-top: 0; margin-bottom: 0; vertical-align: top }
table { margin-top: 0; margin-bottom: 0; vertical-align: top }
h1 { text-align: center }
</style>
<title>SCHED_SETAFFINITY</title>
</head>
<body>
<h1 align="center">SCHED_SETAFFINITY</h1>
<a href="#NAME">NAME</a><br>
<a href="#SYNOPSIS">SYNOPSIS</a><br>
<a href="#DESCRIPTION">DESCRIPTION</a><br>
<a href="#RETURN VALUE">RETURN VALUE</a><br>
<a href="#ERRORS">ERRORS</a><br>
<a href="#VERSIONS">VERSIONS</a><br>
<a href="#CONFORMING TO">CONFORMING TO</a><br>
<a href="#NOTES">NOTES</a><br>
<a href="#EXAMPLE">EXAMPLE</a><br>
<a href="#SEE ALSO">SEE ALSO</a><br>
<a href="#COLOPHON">COLOPHON</a><br>
<hr>
<h2>NAME
<a name="NAME"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">sched_setaffinity,
sched_getaffinity - set and get a thread’s CPU
affinity mask</p>
<h2>SYNOPSIS
<a name="SYNOPSIS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>#define
_GNU_SOURCE</b> /* See feature_test_macros(7) */ <b><br>
#include <sched.h></b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
sched_setaffinity(pid_t</b> <i>pid</i><b>, size_t</b>
<i>cpusetsize</i><b>, <br>
const cpu_set_t *</b><i>mask</i><b>);</b></p>
<p style="margin-left:11%; margin-top: 1em"><b>int
sched_getaffinity(pid_t</b> <i>pid</i><b>, size_t</b>
<i>cpusetsize</i><b>, <br>
cpu_set_t *</b><i>mask</i><b>);</b></p>
<h2>DESCRIPTION
<a name="DESCRIPTION"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">A
thread’s CPU affinity mask determines the set of CPUs
on which it is eligible to run. On a multiprocessor system,
setting the CPU affinity mask can be used to obtain
performance benefits. For example, by dedicating one CPU to
a particular thread (i.e., setting the affinity mask of that
thread to specify a single CPU, and setting the affinity
mask of all other threads to exclude that CPU), it is
possible to ensure maximum execution speed for that thread.
Restricting a thread to run on a single CPU also avoids the
performance cost caused by the cache invalidation that
occurs when a thread ceases to execute on one CPU and then
recommences execution on a different CPU.</p>
<p style="margin-left:11%; margin-top: 1em">A CPU affinity
mask is represented by the <i>cpu_set_t</i> structure, a
"CPU set", pointed to by <i>mask</i>. A set of
macros for manipulating CPU sets is described in
<b>CPU_SET</b>(3).</p>
<p style="margin-left:11%; margin-top: 1em"><b>sched_setaffinity</b>()
sets the CPU affinity mask of the thread whose ID is
<i>pid</i> to the value specified by <i>mask</i>. If
<i>pid</i> is zero, then the calling thread is used. The
argument <i>cpusetsize</i> is the length (in bytes) of the
data pointed to by <i>mask</i>. Normally this argument would
be specified as <i>sizeof(cpu_set_t)</i>.</p>
<p style="margin-left:11%; margin-top: 1em">If the thread
specified by <i>pid</i> is not currently running on one of
the CPUs specified in <i>mask</i>, then that thread is
migrated to one of the CPUs specified in <i>mask</i>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>sched_getaffinity</b>()
writes the affinity mask of the thread whose ID is
<i>pid</i> into the <i>cpu_set_t</i> structure pointed to by
<i>mask</i>. The <i>cpusetsize</i> argument specifies the
size (in bytes) of <i>mask</i>. If <i>pid</i> is zero, then
the mask of the calling thread is returned.</p>
<h2>RETURN VALUE
<a name="RETURN VALUE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">On success,
<b>sched_setaffinity</b>() and <b>sched_getaffinity</b>()
return 0. On error, -1 is returned, and <i>errno</i> is set
appropriately.</p>
<h2>ERRORS
<a name="ERRORS"></a>
</h2>
<table width="100%" border="0" rules="none" frame="void"
cellspacing="0" cellpadding="0">
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p style="margin-top: 1em"><b>EFAULT</b></p></td>
<td width="2%"></td>
<td width="78%">
<p style="margin-top: 1em">A supplied memory address was
invalid.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The affinity bit mask <i>mask</i> contains no processors
that are currently physically on the system and permitted to
the thread according to any restrictions that may be imposed
by <i>cpuset</i> cgroups or the "cpuset" mechanism
described in <b>cpuset</b>(7).</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EINVAL</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>(<b>sched_getaffinity</b>() and, in kernels before
2.6.9, <b>sched_setaffinity</b>()) <i>cpusetsize</i> is
smaller than the size of the affinity mask used by the
kernel.</p> </td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>EPERM</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>(<b>sched_setaffinity</b>()) The calling thread does not
have appropriate privileges. The caller needs an effective
user ID equal to the real user ID or effective user ID of
the thread identified by <i>pid</i>, or it must possess the
<b>CAP_SYS_NICE</b> capability in the user namespace of the
thread <i>pid</i>.</p></td></tr>
<tr valign="top" align="left">
<td width="11%"></td>
<td width="9%">
<p><b>ESRCH</b></p></td>
<td width="2%"></td>
<td width="78%">
<p>The thread whose ID is <i>pid</i> could not be
found.</p> </td></tr>
</table>
<h2>VERSIONS
<a name="VERSIONS"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The CPU
affinity system calls were introduced in Linux kernel 2.5.8.
The system call wrappers were introduced in glibc 2.3.
Initially, the glibc interfaces included a <i>cpusetsize</i>
argument, typed as <i>unsigned int</i>. In glibc 2.3.3, the
<i>cpusetsize</i> argument was removed, but was then
restored in glibc 2.3.4, with type <i>size_t</i>.</p>
<h2>CONFORMING TO
<a name="CONFORMING TO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">These system
calls are Linux-specific.</p>
<h2>NOTES
<a name="NOTES"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">After a call to
<b>sched_setaffinity</b>(), the set of CPUs on which the
thread will actually run is the intersection of the set
specified in the <i>mask</i> argument and the set of CPUs
actually present on the system. The system may further
restrict the set of CPUs on which the thread runs if the
"cpuset" mechanism described in <b>cpuset</b>(7)
is being used. These restrictions on the actual set of CPUs
on which the thread will run are silently imposed by the
kernel.</p>
<p style="margin-left:11%; margin-top: 1em">There are
various ways of determining the number of CPUs available on
the system, including: inspecting the contents of
<i>/proc/cpuinfo</i>; using <b>sysconf</b>(3) to obtain the
values of the <b>_SC_NPROCESSORS_CONF</b> and
<b>_SC_NPROCESSORS_ONLN</b> parameters; and inspecting the
list of CPU directories under
<i>/sys/devices/system/cpu/</i>.</p>
<p style="margin-left:11%; margin-top: 1em"><b>sched</b>(7)
has a description of the Linux scheduling scheme.</p>
<p style="margin-left:11%; margin-top: 1em">The affinity
mask is a per-thread attribute that can be adjusted
independently for each of the threads in a thread group. The
value returned from a call to <b>gettid</b>(2) can be passed
in the argument <i>pid</i>. Specifying <i>pid</i> as 0 will
set the attribute for the calling thread, and passing the
value returned from a call to <b>getpid</b>(2) will set the
attribute for the main thread of the thread group. (If you
are using the POSIX threads API, then use
<b>pthread_setaffinity_np</b>(3) instead of
<b>sched_setaffinity</b>().)</p>
<p style="margin-left:11%; margin-top: 1em">The
<i>isolcpus</i> boot option can be used to isolate one or
more CPUs at boot time, so that no processes are scheduled
onto those CPUs. Following the use of this boot option, the
only way to schedule processes onto the isolated CPUs is via
<b>sched_setaffinity</b>() or the <b>cpuset</b>(7)
mechanism. For further information, see the kernel source
file <i>Documentation/admin-guide/kernel-parameters.txt</i>.
As noted in that file, <i>isolcpus</i> is the preferred
mechanism of isolating CPUs (versus the alternative of
manually setting the CPU affinity of all processes on the
system).</p>
<p style="margin-left:11%; margin-top: 1em">A child created
via <b>fork</b>(2) inherits its parent’s CPU affinity
mask. The affinity mask is preserved across an
<b>execve</b>(2).</p>
<p style="margin-left:11%; margin-top: 1em"><b>C
library/kernel differences</b> <br>
This manual page describes the glibc interface for the CPU
affinity calls. The actual system call interface is slightly
different, with the <i>mask</i> being typed as <i>unsigned
long *</i>, reflecting the fact that the underlying
implementation of CPU sets is a simple bit mask.</p>
<p style="margin-left:11%; margin-top: 1em">On success, the
raw <b>sched_getaffinity</b>() system call returns the
number of bytes placed copied into the <i>mask</i> buffer;
this will be the minimum of <i>cpusetsize</i> and the size
(in bytes) of the <i>cpumask_t</i> data type that is used
internally by the kernel to represent the CPU set bit
mask.</p>
<p style="margin-left:11%; margin-top: 1em"><b>Handling
systems with large CPU affinity masks</b> <br>
The underlying system calls (which represent CPU masks as
bit masks of type <i>unsigned long *</i>) impose no
restriction on the size of the CPU mask. However, the
<i>cpu_set_t</i> data type used by glibc has a fixed size of
128 bytes, meaning that the maximum CPU number that can be
represented is 1023. If the kernel CPU affinity mask is
larger than 1024, then calls of the form:</p>
<p style="margin-left:11%; margin-top: 1em">sched_getaffinity(pid,
sizeof(cpu_set_t), &mask);</p>
<p style="margin-left:11%; margin-top: 1em">fail with the
error <b>EINVAL</b>, the error produced by the underlying
system call for the case where the <i>mask</i> size
specified in <i>cpusetsize</i> is smaller than the size of
the affinity mask used by the kernel. (Depending on the
system CPU topology, the kernel affinity mask can be
substantially larger than the number of active CPUs in the
system.)</p>
<p style="margin-left:11%; margin-top: 1em">When working on
systems with large kernel CPU affinity masks, one must
dynamically allocate the <i>mask</i> argument (see
<b>CPU_ALLOC</b>(3)). Currently, the only way to do this is
by probing for the size of the required mask using
<b>sched_getaffinity</b>() calls with increasing mask sizes
(until the call does not fail with the error
<b>EINVAL</b>).</p>
<p style="margin-left:11%; margin-top: 1em">Be aware that
<b>CPU_ALLOC</b>(3) may allocate a slightly larger CPU set
than requested (because CPU sets are implemented as bit
masks allocated in units of <i>sizeof(long)</i>).
Consequently, <b>sched_getaffinity</b>() can set bits beyond
the requested allocation size, because the kernel sees a few
additional bits. Therefore, the caller should iterate over
the bits in the returned set, counting those which are set,
and stop upon reaching the value returned by
<b>CPU_COUNT</b>(3) (rather than iterating over the number
of bits requested to be allocated).</p>
<h2>EXAMPLE
<a name="EXAMPLE"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">The program
below creates a child process. The parent and child then
each assign themselves to a specified CPU and execute
identical loops that consume some CPU time. Before
terminating, the parent waits for the child to complete. The
program takes three command-line arguments: the CPU number
for the parent, the CPU number for the child, and the number
of loop iterations that both processes should perform.</p>
<p style="margin-left:11%; margin-top: 1em">As the sample
runs below demonstrate, the amount of real and CPU time
consumed when running the program will depend on intra-core
caching effects and whether the processes are using the same
CPU.</p>
<p style="margin-left:11%; margin-top: 1em">We first employ
<b>lscpu</b>(1) to determine that this (x86) system has two
cores, each with two CPUs:</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>lscpu |
egrep -i ’core.*:|socket’</b> <br>
Thread(s) per core: 2 <br>
Core(s) per socket: 2 <br>
Socket(s): 1</p>
<p style="margin-left:11%; margin-top: 1em">We then time
the operation of the example program for three cases: both
processes running on the same CPU; both processes running on
different CPUs on the same core; and both processes running
on different CPUs on different cores.</p>
<p style="margin-left:17%; margin-top: 1em">$ <b>time -p
./a.out 0 0 100000000</b> <br>
real 14.75 <br>
user 3.02 <br>
sys 11.73 <br>
$ <b>time -p ./a.out 0 1 100000000</b> <br>
real 11.52 <br>
user 3.98 <br>
sys 19.06 <br>
$ <b>time -p ./a.out 0 3 100000000</b> <br>
real 7.89 <br>
user 3.29 <br>
sys 12.07</p>
<p style="margin-left:11%; margin-top: 1em"><b>Program
source</b> <br>
#define _GNU_SOURCE <br>
#include <sched.h> <br>
#include <stdio.h> <br>
#include <stdlib.h> <br>
#include <unistd.h> <br>
#include <sys/wait.h></p>
<p style="margin-left:11%; margin-top: 1em">#define
errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ <br>
} while (0)</p>
<p style="margin-left:11%; margin-top: 1em">int <br>
main(int argc, char *argv[]) <br>
{ <br>
cpu_set_t set; <br>
int parentCPU, childCPU; <br>
int nloops, j;</p>
<p style="margin-left:11%; margin-top: 1em">if (argc != 4)
{ <br>
fprintf(stderr, "Usage: %s parent-cpu child-cpu
num-loops\n", <br>
argv[0]); <br>
exit(EXIT_FAILURE); <br>
}</p>
<p style="margin-left:11%; margin-top: 1em">parentCPU =
atoi(argv[1]); <br>
childCPU = atoi(argv[2]); <br>
nloops = atoi(argv[3]);</p>
<p style="margin-left:11%; margin-top: 1em">CPU_ZERO(&set);</p>
<p style="margin-left:11%; margin-top: 1em">switch (fork())
{ <br>
case -1: /* Error */ <br>
errExit("fork");</p>
<p style="margin-left:11%; margin-top: 1em">case 0: /*
Child */ <br>
CPU_SET(childCPU, &set);</p>
<p style="margin-left:11%; margin-top: 1em">if
(sched_setaffinity(getpid(), sizeof(set), &set) == -1)
<br>
errExit("sched_setaffinity");</p>
<p style="margin-left:11%; margin-top: 1em">for (j = 0; j
< nloops; j++) <br>
getppid();</p>
<p style="margin-left:11%; margin-top: 1em">exit(EXIT_SUCCESS);</p>
<p style="margin-left:11%; margin-top: 1em">default: /*
Parent */ <br>
CPU_SET(parentCPU, &set);</p>
<p style="margin-left:11%; margin-top: 1em">if
(sched_setaffinity(getpid(), sizeof(set), &set) == -1)
<br>
errExit("sched_setaffinity");</p>
<p style="margin-left:11%; margin-top: 1em">for (j = 0; j
< nloops; j++) <br>
getppid();</p>
<p style="margin-left:11%; margin-top: 1em">wait(NULL); /*
Wait for child to terminate */ <br>
exit(EXIT_SUCCESS); <br>
} <br>
}</p>
<h2>SEE ALSO
<a name="SEE ALSO"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em"><b>lscpu</b>(1),
<b>nproc</b>(1), <b>taskset</b>(1), <b>clone</b>(2),
<b>getcpu</b>(2), <b>getpriority</b>(2), <b>gettid</b>(2),
<b>nice</b>(2), <b>sched_get_priority_max</b>(2),
<b>sched_get_priority_min</b>(2),
<b>sched_getscheduler</b>(2), <b>sched_setscheduler</b>(2),
<b>setpriority</b>(2), <b>CPU_SET</b>(3),
<b>get_nprocs</b>(3), <b>pthread_setaffinity_np</b>(3),
<b>sched_getcpu</b>(3), <b>capabilities</b>(7),
<b>cpuset</b>(7), <b>sched</b>(7), <b>numactl</b>(8)</p>
<h2>COLOPHON
<a name="COLOPHON"></a>
</h2>
<p style="margin-left:11%; margin-top: 1em">This page is
part of release 5.02 of the Linux <i>man-pages</i> project.
A description of the project, information about reporting
bugs, and the latest version of this page, can be found at
https://www.kernel.org/doc/man-pages/.</p>
<hr>
</body>
</html>