]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man2/sched_setaffinity.2
getent.1, _syscall.2, acct.2, adjtimex.2, bdflush.2, brk.2, cacheflush.2, getsid...
[thirdparty/man-pages.git] / man2 / sched_setaffinity.2
1 .\" man2/sched_setaffinity.2 - sched_setaffinity and sched_getaffinity man page
2 .\"
3 .\" Copyright (C) 2002 Robert Love
4 .\" and Copyright (C) 2006 Michael Kerrisk
5 .\"
6 .\" %%%LICENSE_START(GPLv2+_doc_full)
7 .\" This is free documentation; you can redistribute it and/or
8 .\" modify it under the terms of the GNU General Public License as
9 .\" published by the Free Software Foundation; either version 2 of
10 .\" the License, or (at your option) any later version.
11 .\"
12 .\" The GNU General Public License's references to "object code"
13 .\" and "executables" are to be interpreted as the output of any
14 .\" document formatting or typesetting system, including
15 .\" intermediate and printed output.
16 .\"
17 .\" This manual is distributed in the hope that it will be useful,
18 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
19 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 .\" GNU General Public License for more details.
21 .\"
22 .\" You should have received a copy of the GNU General Public
23 .\" License along with this manual; if not, see
24 .\" <http://www.gnu.org/licenses/>.
25 .\" %%%LICENSE_END
26 .\"
27 .\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
28 .\" 2004-04-20 mtk - fixed description of return value
29 .\" 2004-04-22 aeb - added glibc prototype history
30 .\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
31 .\" migration and that CPU affinity is a per-thread attribute.
32 .\" 2006-02-03 mtk -- Major rewrite
33 .\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
34 .\" separate CPU_SET(3) page.
35 .\"
36 .TH SCHED_SETAFFINITY 2 2013-02-11 "Linux" "Linux Programmer's Manual"
37 .SH NAME
38 sched_setaffinity, sched_getaffinity \- \
39 set and get a process's CPU affinity mask
40 .SH SYNOPSIS
41 .nf
42 .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
43 .B #include <sched.h>
44 .sp
45 .BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
46 .BI " cpu_set_t *" mask );
47 .sp
48 .BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
49 .BI " cpu_set_t *" mask );
50 .fi
51 .SH DESCRIPTION
52 A process's CPU affinity mask determines the set of CPUs on which
53 it is eligible to run.
54 On a multiprocessor system, setting the CPU affinity mask
55 can be used to obtain performance benefits.
56 For example,
57 by dedicating one CPU to a particular process
58 (i.e., setting the affinity mask of that process to specify a single CPU,
59 and setting the affinity mask of all other processes to exclude that CPU),
60 it is possible to ensure maximum execution speed for that process.
61 Restricting a process to run on a single CPU also avoids
62 the performance cost caused by the cache invalidation that occurs
63 when a process ceases to execute on one CPU and then
64 recommences execution on a different CPU.
65
66 A CPU affinity mask is represented by the
67 .I cpu_set_t
68 structure, a "CPU set", pointed to by
69 .IR mask .
70 A set of macros for manipulating CPU sets is described in
71 .BR CPU_SET (3).
72
73 .BR sched_setaffinity ()
74 sets the CPU affinity mask of the process whose ID is
75 .I pid
76 to the value specified by
77 .IR mask .
78 If
79 .I pid
80 is zero, then the calling process is used.
81 The argument
82 .I cpusetsize
83 is the length (in bytes) of the data pointed to by
84 .IR mask .
85 Normally this argument would be specified as
86 .IR "sizeof(cpu_set_t)" .
87
88 If the process specified by
89 .I pid
90 is not currently running on one of the CPUs specified in
91 .IR mask ,
92 then that process is migrated to one of the CPUs specified in
93 .IR mask .
94
95 .BR sched_getaffinity ()
96 writes the affinity mask of the process whose ID is
97 .I pid
98 into the
99 .I cpu_set_t
100 structure pointed to by
101 .IR mask .
102 The
103 .I cpusetsize
104 argument specifies the size (in bytes) of
105 .IR mask .
106 If
107 .I pid
108 is zero, then the mask of the calling process is returned.
109 .SH RETURN VALUE
110 On success,
111 .BR sched_setaffinity ()
112 and
113 .BR sched_getaffinity ()
114 return 0.
115 On error, \-1 is returned, and
116 .I errno
117 is set appropriately.
118 .SH ERRORS
119 .TP
120 .B EFAULT
121 A supplied memory address was invalid.
122 .TP
123 .B EINVAL
124 The affinity bit mask
125 .I mask
126 contains no processors that are currently physically on the system
127 and permitted to the process according to any restrictions that
128 may be imposed by the "cpuset" mechanism described in
129 .BR cpuset (7).
130 .TP
131 .B EINVAL
132 .RB ( sched_getaffinity ()
133 and, in kernels before 2.6.9,
134 .BR sched_setaffinity ())
135 .I cpusetsize
136 is smaller than the size of the affinity mask used by the kernel.
137 .TP
138 .B EPERM
139 .RB ( sched_setaffinity ())
140 The calling process does not have appropriate privileges.
141 The caller needs an effective user ID equal to the real user ID
142 or effective user ID of the process identified by
143 .IR pid ,
144 or it must possess the
145 .B CAP_SYS_NICE
146 capability.
147 .TP
148 .B ESRCH
149 The process whose ID is \fIpid\fP could not be found.
150 .SH VERSIONS
151 The CPU affinity system calls were introduced in Linux kernel 2.5.8.
152 The system call wrappers were introduced in glibc 2.3.
153 Initially, the glibc interfaces included a
154 .I cpusetsize
155 argument, typed as
156 .IR "unsigned int" .
157 In glibc 2.3.3, the
158 .I cpusetsize
159 argument was removed, but was then restored in glibc 2.3.4, with type
160 .IR size_t .
161 .SH CONFORMING TO
162 These system calls are Linux-specific.
163 .SH NOTES
164 After a call to
165 .BR sched_setaffinity (),
166 the set of CPUs on which the process will actually run is
167 the intersection of the set specified in the
168 .I mask
169 argument and the set of CPUs actually present on the system.
170 The system may further restrict the set of CPUs on which the process
171 runs if the "cpuset" mechanism described in
172 .BR cpuset (7)
173 is being used.
174 These restrictions on the actual set of CPUs on which the process
175 will run are silently imposed by the kernel.
176
177 .BR sched_setscheduler (2)
178 has a description of the Linux scheduling scheme.
179 .PP
180 The affinity mask is actually a per-thread attribute that can be
181 adjusted independently for each of the threads in a thread group.
182 The value returned from a call to
183 .BR gettid (2)
184 can be passed in the argument
185 .IR pid .
186 Specifying
187 .I pid
188 as 0 will set the attribute for the calling thread,
189 and passing the value returned from a call to
190 .BR getpid (2)
191 will set the attribute for the main thread of the thread group.
192 (If you are using the POSIX threads API, then use
193 .BR pthread_setaffinity_np (3)
194 instead of
195 .BR sched_setaffinity ().)
196
197 A child created via
198 .BR fork (2)
199 inherits its parent's CPU affinity mask.
200 The affinity mask is preserved across an
201 .BR execve (2).
202
203 This manual page describes the glibc interface for the CPU affinity calls.
204 The actual system call interface is slightly different, with the
205 .I mask
206 being typed as
207 .IR "unsigned long *" ,
208 reflecting the fact that the underlying implementation of CPU
209 sets is a simple bit mask.
210 On success, the raw
211 .BR sched_getaffinity ()
212 system call returns the size (in bytes) of the
213 .I cpumask_t
214 data type that is used internally by the kernel to
215 represent the CPU set bit mask.
216 .SH SEE ALSO
217 .ad l
218 .nh
219 .BR taskset (1),
220 .BR clone (2),
221 .BR getcpu (2),
222 .BR getpriority (2),
223 .BR gettid (2),
224 .BR nice (2),
225 .BR sched_get_priority_max (2),
226 .BR sched_get_priority_min (2),
227 .BR sched_getscheduler (2),
228 .BR sched_setscheduler (2),
229 .BR setpriority (2),
230 .BR CPU_SET (3),
231 .BR pthread_setaffinity_np (3),
232 .BR sched_getcpu (3),
233 .BR capabilities (7),
234 .BR cpuset (7)