]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man2/sched_setaffinity.2
getent.1, iconv.1, ldd.1, locale.1, localedef.1, memusage.1, memusagestat.1, pldd...
[thirdparty/man-pages.git] / man2 / sched_setaffinity.2
CommitLineData
fea681da 1.\" Copyright (C) 2002 Robert Love
2236e65b 2.\" and Copyright (C) 2006, 2015 Michael Kerrisk
fea681da 3.\"
1dd72f9c 4.\" %%%LICENSE_START(GPLv2+_DOC_FULL)
fea681da
MK
5.\" This is free documentation; you can redistribute it and/or
6.\" modify it under the terms of the GNU General Public License as
7.\" published by the Free Software Foundation; either version 2 of
8.\" the License, or (at your option) any later version.
9.\"
10.\" The GNU General Public License's references to "object code"
11.\" and "executables" are to be interpreted as the output of any
12.\" document formatting or typesetting system, including
13.\" intermediate and printed output.
14.\"
15.\" This manual is distributed in the hope that it will be useful,
16.\" but WITHOUT ANY WARRANTY; without even the implied warranty of
17.\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18.\" GNU General Public License for more details.
19.\"
20.\" You should have received a copy of the GNU General Public
c715f741
MK
21.\" License along with this manual; if not, see
22.\" <http://www.gnu.org/licenses/>.
6a8d8745 23.\" %%%LICENSE_END
fea681da
MK
24.\"
25.\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
26.\" 2004-04-20 mtk - fixed description of return value
27.\" 2004-04-22 aeb - added glibc prototype history
c13182ef 28.\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
d2b76164 29.\" migration and that CPU affinity is a per-thread attribute.
48be1144 30.\" 2006-02-03 mtk -- Major rewrite
ad3f4748
MK
31.\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
32.\" separate CPU_SET(3) page.
fea681da 33.\"
9ba01802 34.TH SCHED_SETAFFINITY 2 2019-03-06 "Linux" "Linux Programmer's Manual"
fea681da 35.SH NAME
ad3f4748 36sched_setaffinity, sched_getaffinity \- \
6a7fcf3c 37set and get a thread's CPU affinity mask
fea681da 38.SH SYNOPSIS
ee2fa120 39.nf
86b91fdf 40.BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
fea681da 41.B #include <sched.h>
68e4db0a 42.PP
d3df61c4 43.BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
475b1ecf 44.BI " const cpu_set_t *" mask );
68e4db0a 45.PP
d3df61c4 46.BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
ee2fa120 47.BI " cpu_set_t *" mask );
ee2fa120 48.fi
fea681da 49.SH DESCRIPTION
6a7fcf3c 50A thread's CPU affinity mask determines the set of CPUs on which
48be1144
MK
51it is eligible to run.
52On a multiprocessor system, setting the CPU affinity mask
c13182ef 53can be used to obtain performance benefits.
48be1144 54For example,
6a7fcf3c
MK
55by dedicating one CPU to a particular thread
56(i.e., setting the affinity mask of that thread to specify a single CPU,
57and setting the affinity mask of all other threads to exclude that CPU),
58it is possible to ensure maximum execution speed for that thread.
59Restricting a thread to run on a single CPU also avoids
c13182ef 60the performance cost caused by the cache invalidation that occurs
6a7fcf3c 61when a thread ceases to execute on one CPU and then
48be1144 62recommences execution on a different CPU.
efeece04 63.PP
c13182ef
MK
64A CPU affinity mask is represented by the
65.I cpu_set_t
48be1144
MK
66structure, a "CPU set", pointed to by
67.IR mask .
ad3f4748
MK
68A set of macros for manipulating CPU sets is described in
69.BR CPU_SET (3).
efeece04 70.PP
e511ffb6 71.BR sched_setaffinity ()
6a7fcf3c 72sets the CPU affinity mask of the thread whose ID is
0daa9e92 73.I pid
48be1144
MK
74to the value specified by
75.IR mask .
fea681da
MK
76If
77.I pid
6a7fcf3c 78is zero, then the calling thread is used.
48be1144
MK
79The argument
80.I cpusetsize
81is the length (in bytes) of the data pointed to by
fea681da 82.IR mask .
48be1144
MK
83Normally this argument would be specified as
84.IR "sizeof(cpu_set_t)" .
efeece04 85.PP
6a7fcf3c 86If the thread specified by
6fbc0235 87.I pid
48be1144 88is not currently running on one of the CPUs specified in
6fbc0235 89.IR mask ,
6a7fcf3c 90then that thread is migrated to one of the CPUs specified in
6fbc0235 91.IR mask .
efeece04 92.PP
e511ffb6 93.BR sched_getaffinity ()
6a7fcf3c 94writes the affinity mask of the thread whose ID is
0daa9e92 95.I pid
c13182ef 96into the
48be1144
MK
97.I cpu_set_t
98structure pointed to by
99.IR mask .
c13182ef 100The
48be1144
MK
101.I cpusetsize
102argument specifies the size (in bytes) of
103.IR mask .
fea681da
MK
104If
105.I pid
6a7fcf3c 106is zero, then the mask of the calling thread is returned.
47297adb 107.SH RETURN VALUE
fea681da 108On success,
e511ffb6 109.BR sched_setaffinity ()
48be1144 110and
e511ffb6 111.BR sched_getaffinity ()
48be1144 112return 0.
fea681da
MK
113On error, \-1 is returned, and
114.I errno
115is set appropriately.
fea681da
MK
116.SH ERRORS
117.TP
118.B EFAULT
119A supplied memory address was invalid.
120.TP
121.B EINVAL
10f5f294 122The affinity bit mask
fea681da 123.I mask
9c7cf182 124contains no processors that are currently physically on the system
6a7fcf3c 125and permitted to the thread according to any restrictions that
bcaf6dc0
MK
126may be imposed by
127.I cpuset
0fdca718 128cgroups or the "cpuset" mechanism described in
9c7cf182 129.BR cpuset (7).
a2126943
MK
130.TP
131.B EINVAL
132.RB ( sched_getaffinity ()
133and, in kernels before 2.6.9,
134.BR sched_setaffinity ())
48be1144 135.I cpusetsize
fea681da
MK
136is smaller than the size of the affinity mask used by the kernel.
137.TP
138.B EPERM
3213bb28 139.RB ( sched_setaffinity ())
6a7fcf3c 140The calling thread does not have appropriate privileges.
6b2953e4 141The caller needs an effective user ID equal to the real user ID
6a7fcf3c 142or effective user ID of the thread identified by
fea681da
MK
143.IR pid ,
144or it must possess the
0daa9e92 145.B CAP_SYS_NICE
1090e3f0
MK
146capability in the user namespace of the thread
147.IR pid .
fea681da
MK
148.TP
149.B ESRCH
6a7fcf3c 150The thread whose ID is \fIpid\fP could not be found.
a1d5f77c
MK
151.SH VERSIONS
152The CPU affinity system calls were introduced in Linux kernel 2.5.8.
718af393 153The system call wrappers were introduced in glibc 2.3.
a1d5f77c
MK
154Initially, the glibc interfaces included a
155.I cpusetsize
d3df61c4
MK
156argument, typed as
157.IR "unsigned int" .
a1d5f77c
MK
158In glibc 2.3.3, the
159.I cpusetsize
d3df61c4
MK
160argument was removed, but was then restored in glibc 2.3.4, with type
161.IR size_t .
47297adb 162.SH CONFORMING TO
8382f16d 163These system calls are Linux-specific.
47297adb 164.SH NOTES
9c7cf182
MK
165After a call to
166.BR sched_setaffinity (),
6a7fcf3c 167the set of CPUs on which the thread will actually run is
9c7cf182
MK
168the intersection of the set specified in the
169.I mask
170argument and the set of CPUs actually present on the system.
6a7fcf3c 171The system may further restrict the set of CPUs on which the thread
9c7cf182
MK
172runs if the "cpuset" mechanism described in
173.BR cpuset (7)
174is being used.
6a7fcf3c 175These restrictions on the actual set of CPUs on which the thread
9c7cf182 176will run are silently imposed by the kernel.
efeece04 177.PP
9c11e37c
MK
178There are various ways of determining the number of CPUs
179available on the system, including: inspecting the contents of
180.IR /proc/cpuinfo ;
181using
144d1a36 182.BR sysconf (3)
9c11e37c
MK
183to obtain the values of the
184.BR _SC_NPROCESSORS_CONF
185and
186.BR _SC_NPROCESSORS_ONLN
4efcd5dd 187parameters; and inspecting the list of CPU directories under
9c11e37c 188.IR /sys/devices/system/cpu/ .
efeece04 189.PP
476883d7 190.BR sched (7)
9e20ad24
MK
191has a description of the Linux scheduling scheme.
192.PP
6a7fcf3c 193The affinity mask is a per-thread attribute that can be
d2b76164
MK
194adjusted independently for each of the threads in a thread group.
195The value returned from a call to
196.BR gettid (2)
197can be passed in the argument
198.IR pid .
da49ae18
MK
199Specifying
200.I pid
ecbb9b58 201as 0 will set the attribute for the calling thread,
da49ae18
MK
202and passing the value returned from a call to
203.BR getpid (2)
204will set the attribute for the main thread of the thread group.
d7ddac24 205(If you are using the POSIX threads API, then use