1 .\" man2/sched_setaffinity.2 - sched_setaffinity and sched_getaffinity man page
3 .\" Copyright (C) 2002 Robert Love
4 .\" and Copyright (C) 2006 Michael Kerrisk
6 .\" This is free documentation; you can redistribute it and/or
7 .\" modify it under the terms of the GNU General Public License as
8 .\" published by the Free Software Foundation; either version 2 of
9 .\" the License, or (at your option) any later version.
11 .\" The GNU General Public License's references to "object code"
12 .\" and "executables" are to be interpreted as the output of any
13 .\" document formatting or typesetting system, including
14 .\" intermediate and printed output.
16 .\" This manual is distributed in the hope that it will be useful,
17 .\" but WITHOUT ANY WARRANTY; without even the implied warranty of
18 .\" MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 .\" GNU General Public License for more details.
21 .\" You should have received a copy of the GNU General Public
22 .\" License along with this manual; if not, write to the Free
23 .\" Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111,
26 .\" 2002-11-19 Robert Love <rml@tech9.net> - initial version
27 .\" 2004-04-20 mtk - fixed description of return value
28 .\" 2004-04-22 aeb - added glibc prototype history
29 .\" 2005-05-03 mtk - noted that sched_setaffinity may cause thread
30 .\" migration and that CPU affinity is a per-thread attribute.
31 .\" 2006-02-03 mtk -- Major rewrite
32 .\" 2008-11-12, mtk, removed CPU_*() macro descriptions to a
33 .\" separate CPU_SET(3) page.
35 .\" Japanese Version Copyright (c) 2003, 2005 Yuichi SATO
36 .\" all rights reserved.
37 .\" Translated 2003-01-23, Yuichi SATO <ysato444@yahoo.co.jp>
38 .\" Updated & Modified 2003-09-23, Yuichi SATO
39 .\" Updated & Modified 2005-01-03, Yuichi SATO
40 .\" Updated & Modified 2005-09-13, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
41 .\" Updated & Modified 2006-07-14, Akihiro MOTOKI, LDP v2.34
42 .\" Updated & Modified 2008-12-24, Akihiro MOTOKI, LDP v3.14
44 .\"WORD: affinity ¿ÆÏÂÅÙ
45 .\"WORD: most significant bit ºÇ¾å°Ì¥Ó¥Ã¥È
46 .\"WORD: least significant bit ºÇ²¼°Ì¥Ó¥Ã¥È
47 .\"WORD: capability ¸¢¸Â
49 .TH SCHED_SETAFFINITY 2 2010-11-06 "Linux" "Linux Programmer's Manual"
52 .\"O sched_setaffinity, sched_getaffinity \- \
53 .\"O set and get a process's CPU affinity mask
54 sched_setaffinity, sched_getaffinity \- \
55 ¥×¥í¥»¥¹¤Î CPU affinity ¥Þ¥¹¥¯¤òÀßÄꡦ¼èÆÀ¤¹¤ë
59 .\"O .BR "#define _GNU_SOURCE" " /* See feature_test_macros(7) */"
60 .BR "#define _GNU_SOURCE" " /* feature_test_macros(7) »²¾È */"
63 .BI "int sched_setaffinity(pid_t " pid ", size_t " cpusetsize ,
64 .BI " cpu_set_t *" mask );
66 .BI "int sched_getaffinity(pid_t " pid ", size_t " cpusetsize ,
67 .BI " cpu_set_t *" mask );
72 .\"Osato: Kernel ¥É¥¥å¥á¥ó¥È IRQ-affinity.txt ¤ÎÌõ¤Ç¤Ï¡¢
73 .\"Osato: "affinity" ¤Ï¡Ö¥¢¥Õ¥£¥Ë¥Æ¥£¡×¤Ë¤Ê¤Ã¤Æ¤¤¤Þ¤¹¤¬¡¢
74 .\"Osato: ¤³¤³¤Ë°ì²ó¤À¤±Ìõ¸ì (¿ÆÏÂÅÙ) ¤òÆþ¤ì¤Æ¤ª¤¯¤À¤±¤Ç¡¢
75 .\"Osato: ÆäËÌõ¤µ¤Ê¤¤¤³¤È¤Ë¤·¤è¤¦¤È»×¤¤¤Þ¤¹¡£
77 .\"O A process's CPU affinity mask determines the set of CPUs on which
78 .\"O it is eligible to run.
79 .\"O On a multiprocessor system, setting the CPU affinity mask
80 .\"O can be used to obtain performance benefits.
82 .\"O by dedicating one CPU to a particular process
83 .\"O (i.e., setting the affinity mask of that process to specify a single CPU,
84 .\"O and setting the affinity mask of all other processes to exclude that CPU),
85 .\"O it is possible to ensure maximum execution speed for that process.
86 .\"O Restricting a process to run on a single CPU also avoids
87 .\"O the performance cost caused by the cache invalidation that occurs
88 .\"O when a process ceases to execute on one CPU and then
89 .\"O recommences execution on a different CPU.
90 ¥×¥í¥»¥¹¤Î CPU affinity (¿ÆÏÂÅÙ) ¥Þ¥¹¥¯¤Ï¡¢¤½¤Î¥×¥í¥»¥¹¤¬
91 ¼Â¹Ô¤òµö²Ä¤µ¤ì¤Æ¤¤¤ë CPU ¤Î½¸¹ç¤ò·èÄꤹ¤ë¡£
92 ¥Þ¥ë¥Á¥×¥í¥»¥Ã¥µ¡¦¥·¥¹¥Æ¥à¤Ç¤Ï¡¢CPU affinity ¥Þ¥¹¥¯¤òÀßÄꤹ¤ë¤³¤È¤Ç
93 Àǽ¾å¤Î¥á¥ê¥Ã¥È¤òÆÀ¤é¤ì¤ë²ÄǽÀ¤¬¤¢¤ë¡£
94 Î㤨¤Ð¡¢ÆÃÄê¤Î¥×¥í¥»¥¹¤ò°ì¤Ä¤Î CPU ¤Ë³ç¤êÉÕ¤±
95 (¤¹¤Ê¤ï¤Á¡¢¤½¤Î¥×¥í¥»¥¹¤Î affinity ¥Þ¥¹¥¯¤ò°ì¤Ä¤Î CPU ¤ËÀßÄꤷ)¡¢
96 ¾¤ÎÁ´¤Æ¤Î¥×¥í¥»¥¹¤Î affinity ¥Þ¥¹¥¯¤«¤é¤½¤Î CPU ¤ò½ü³°¤¹¤ë¤³¤È¤Ç¡¢
97 ³Î¼Â¤Ë¤½¤Î¥×¥í¥»¥¹¤Î¼Â¹Ô®ÅÙ¤òºÇÂç¤Ë¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
98 ¤Þ¤¿¡¢¤¢¤ë¥×¥í¥»¥¹¤Î¼Â¹Ô¤ò°ì¤Ä¤Î CPU ¤Ë¸ÂÄꤹ¤ë¤³¤È¤Ç¡¢
99 °ì¤Ä¤Î CPU ¤Ç¤Î¼Â¹Ô¤òÄä»ß¤·¤Æ¤«¤éÊ̤ΠCPU ¤Ç¼Â¹Ô¤òºÆ³«¤¹¤ë¤È¤¤ËȯÀ¸¤¹¤ë
100 ¥¥ã¥Ã¥·¥å̵¸ú²½ (cache invalidation) ¤Ë¤è¤ëÀǽÌ̤ÎÎô²½¤òÈò¤±¤ë¤³¤È¤â¤Ç¤¤ë¡£
102 .\"O A CPU affinity mask is represented by the
104 .\"O structure, a "CPU set", pointed to by
106 CPU affinity ¥Þ¥¹¥¯¤Ï¡ÖCPU ¤Î½¸¹ç¡×¤òɽ¤¹
113 .\"O A set of macros for manipulating CPU sets is described in
114 .\"O .BR CPU_SET (3).
115 CPU ½¸¹ç¤òÁàºî¤¹¤ë¤¿¤á¤Î¥Þ¥¯¥í·²¤Ë¤Ä¤¤¤Æ¤Ï
119 .\"O .BR sched_setaffinity ()
120 .\"O sets the CPU affinity mask of the process whose ID is
122 .\"O to the value specified by
124 .BR sched_setaffinity ()
127 ¤Î¥×¥í¥»¥¹¤Î CPU affinity ¥Þ¥¹¥¯¤ò
129 ¤Ç»ØÄꤵ¤ì¤¿ÃͤËÀßÄꤹ¤ë¡£
132 .\"O is zero, then the calling process is used.
135 .\"O is the length (in bytes) of the data pointed to by
138 ¤¬ 0 ¤Î¾ì¹ç¡¢¸Æ¤Ó½Ð¤·¸µ¥×¥í¥»¥¹¤¬»È¤ï¤ì¤ë¡£
142 ¤¬»Ø¤¹¥Ç¡¼¥¿¤ÎŤµ (¥Ð¥¤¥Èñ°Ì) ¤Ç¤¢¤ë¡£
143 .\"O Normally this argument would be specified as
144 .\"O .IR "sizeof(cpu_set_t)" .
145 Ä̾ï¤Ï¡¢¤³¤Î°ú¤¿ô¤Ë¤Ï
146 .I "sizeof(cpu_set_t)"
149 .\"O If the process specified by
151 .\"O is not currently running on one of the CPUs specified in
153 .\"O then that process is migrated to one of the CPUs specified in
156 ¤Ç»ØÄꤵ¤ì¤¿¥×¥í¥»¥¹¤¬
158 ¤Ç»ØÄꤵ¤ì¤¿ CPU ¤Î¤¤¤º¤ì¤«¤Ç¸½ºß¼Â¹Ô¤µ¤ì¤Æ¤¤¤Ê¤¤¾ì¹ç¡¢
161 ¤Ç»ØÄꤵ¤ì¤¿ CPU ¤Î¤¤¤º¤ì¤«¤Ë°ÜÆ°¤µ¤ì¤ë¡£
163 .\"O .BR sched_getaffinity ()
164 .\"O writes the affinity mask of the process whose ID is
168 .\"O structure pointed to by
172 .\"O argument specifies the size (in bytes) of
174 .BR sched_getaffinity ()
178 ¤Î¥×¥í¥»¥¹¤Î affinity ¥Þ¥¹¥¯¤ò
186 ¤Î (¥Ð¥¤¥Èñ°Ì¤Î) Â礤µ¤ò»ØÄꤹ¤ë¡£
189 .BR sched_getaffinity ()
196 ¤Î affinity ¥Þ¥¹¥¯¤ò½ñ¤¹þ¤à¡£
199 .\"O is zero, then the mask of the calling process is returned.
201 ¤¬ 0 ¤Î¾ì¹ç¡¢¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤Î¥Þ¥¹¥¯¤¬ÊÖ¤µ¤ì¤ë¡£
202 .\"O .SH "RETURN VALUE"
205 .\"O .BR sched_setaffinity ()
207 .\"O .BR sched_getaffinity ()
210 .BR sched_setaffinity ()
212 .BR sched_getaffinity ()
214 .\"O On error, \-1 is returned, and
216 .\"O is set appropriately.
217 ¥¨¥é¡¼¤Î¾ì¹ç¤Ï \-1 ¤òÊÖ¤·¡¢
224 .\"O A supplied memory address was invalid.
225 »ØÄꤵ¤ì¤¿¥á¥â¥êÈÖÃϤ¬ÉÔÀµ¤Ç¤¢¤ë¡£
228 .\"O The affinity bit mask
230 .\"O contains no processors that are currently physically on the system
231 .\"O and permitted to the process according to any restrictions that
232 .\"O may be imposed by the "cpuset" mechanism described in
234 ¥·¥¹¥Æ¥à¾å¤Ë¸½ºß¼ÂºÝ¤Ë¸ºß¤·¡¢¤«¤Ä
235 "cpuset" µ¡¹½¤¬²Ý¤¹À©¸Â¤Ë¤ª¤¤¤Æ¤½¤Î¥×¥í¥»¥¹¤ËÂФ·¤Æµö²Ä¤µ¤ì¤Æ¤¤¤ë
237 affinity ¥Ó¥Ã¥È¥Þ¥¹¥¯
240 "cpuset" µ¡¹½¤Ë¤Ä¤¤¤Æ¤Ï
245 .\"O .RB ( sched_getaffinity ()
246 .\"O and, in kernels before 2.6.9,
247 .\"O .BR sched_setaffinity ())
249 .\"O is smaller than the size of the affinity mask used by the kernel.
250 .RB ( sched_getaffinity ()
251 ¤È¡¢¥«¡¼¥Í¥ë 2.6.9 °ÊÁ°¤Î
252 .BR sched_setaffinity ())
254 ¤¬¥«¡¼¥Í¥ë¤Ç»È¤ï¤ì¤Æ¤¤¤ë affinity ¥Þ¥¹¥¯¤Î¥µ¥¤¥º¤è¤ê¾®¤µ¤¤¡£
257 .\"O .RB ( sched_setaffinity ())
258 .\"O The calling process does not have appropriate privileges.
259 .\"O The caller needs an effective user ID equal to the real user ID
260 .\"O or effective user ID of the process identified by
262 .\"O or it must possess the
265 .RB ( sched_setaffinity ())
266 ¸Æ¤Ó½Ð¤·¸µ¤Î¥×¥í¥»¥¹¤ËŬÀÚ¤ÊÆø¢¤¬¤Ê¤«¤Ã¤¿¡£
267 ¸Æ¤Ó½Ð¤·¸µ¤Ï¡¢¼Â¸ú¥æ¡¼¥¶ ID ¤¬
269 ¤Ç¼±Ê̤µ¤ì¤ë¥×¥í¥»¥¹¤Î¼Â¥æ¡¼¥¶ ID ¤Þ¤¿¤Ï¼Â¸ú¥æ¡¼¥¶ ID ¤ÈƱ¤¸¤Ç¤¢¤ë¤«¡¢
271 ¥±¡¼¥Ñ¥Ó¥ê¥Æ¥£ (capability) ¤ò»ý¤¿¤Ê¤±¤ì¤Ð¤Ê¤é¤Ê¤¤¡£
274 .\"O The process whose ID is \fIpid\fP could not be found.
275 ¥×¥í¥»¥¹ ID \fIpid\fP ¤Î¥×¥í¥»¥¹¤¬¸«¤Ä¤«¤é¤Ê¤«¤Ã¤¿¡£
278 .\"O The CPU affinity system calls were introduced in Linux kernel 2.5.8.
279 CPU affinity ¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï Linux kernel 2.5.8 ¤ÇƳÆþ¤µ¤ì¤¿¡£
280 .\"O The system call wrappers were introduced in glibc 2.3.
281 .\"O Initially, the glibc interfaces included a
283 .\"O argument, typed as
284 .\"O .IR "unsigned int" .
285 .\"O In glibc 2.3.3, the
287 .\"O argument was removed, but was then restored in glibc 2.3.4, with type
289 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Î¥é¥Ã¥Ñ¡¼´Ø¿ô¤Ï glibc 2.3 ¤ÇƳÆþ¤µ¤ì¤¿¡£
290 ºÇ½é¤Ï¡¢glibc ¤Î¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ë¤Ï
297 °ú¤¿ô¤¬ºï½ü¤µ¤ì¤¿¤¬¡¢glibc 2.3.4 ¤Ç
300 .\"O .SH "CONFORMING TO"
302 .\"O These system calls are Linux-specific.
303 ¤³¤ì¤é¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¤Ï Linux ¸ÇͤǤ¢¤ë¡£
307 .\"O .BR sched_setaffinity (),
308 .\"O the set of CPUs on which the process will actually run is
309 .\"O the intersection of the set specified in the
311 .\"O argument and the set of CPUs actually present on the system.
312 .BR sched_setaffinity ()
313 ¤ò¸Æ¤Ó½Ð¤·¤¿¸å¤Ï¡¢¥×¥í¥»¥¹¤¬¼ÂºÝ¤Ë¼Â¹Ô¤µ¤ì¤ë CPU ¤Î½¸¹ç¤Ï¡¢
315 °ú¤¿ô¤Ç»ØÄꤵ¤ì¤¿½¸¹ç¤È¡¢¥·¥¹¥Æ¥à¾å¤Ë¼ÂºÝ¤Ë¸ºß¤¹¤ë CPU ¤Î½¸¹ç¤Î
316 ¶¦Ä̽¸¹ç (AND) ¤È¤Ê¤ë¡£
317 .\"O The system may further restrict the set of CPUs on which the process
318 .\"O runs if the "cpuset" mechanism described in
321 .\"O These restrictions on the actual set of CPUs on which the process
322 .\"O will run are silently imposed by the kernel.
323 "cpuset" µ¡¹½¤¬»ÈÍѤµ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï¡¢¥×¥í¥»¥¹¤¬Æ°ºî¤¹¤ë CPU ½¸¹ç
324 ¤ËÂФ·¤Æ¥·¥¹¥Æ¥à¤Ï¤µ¤é¤ËÀ©¸Â¤ò²Ã¤¨¤ë¤«¤â¤·¤ì¤Ê¤¤
325 ("cpuset" µ¡¹½¤Ë¤Ä¤¤¤Æ¤Ï
328 ¥×¥í¥»¥¹¤¬Æ°ºî¤¹¤ë¼ÂºÝ¤Î CPU ½¸¹ç¤ËÂФ¹¤ëÀ©¸Â¤Ï¥«¡¼¥Í¥ë¤Ë¤è¤ê
329 °ÅÌۤΤ¦¤Á¤ËŬÍѤµ¤ì¤ë¡£
331 .\"O .BR sched_setscheduler (2)
332 .\"O has a description of the Linux scheduling scheme.
333 .BR sched_setscheduler (2)
334 ¤Ë¤Ï Linux ¤Ë¤ª¤±¤ë¥¹¥±¥¸¥å¡¼¥ê¥ó¥°µ¡¹½¤Ë¤Ä¤¤¤Æ¤ÎÀâÌÀ¤¬¤¢¤ë¡£
336 .\"O The affinity mask is actually a per-thread attribute that can be
337 .\"O adjusted independently for each of the threads in a thread group.
338 .\"O The value returned from a call to
340 .\"O can be passed in the argument
342 ¼ÂºÝ¤Ë¤Ï affinity ¥Þ¥¹¥¯¤Ï¥¹¥ì¥Ã¥Éñ°Ì¤Î°À¤Ç¡¢¥¹¥ì¥Ã¥É¥°¥ë¡¼¥×¤Î
343 ³Æ¥¹¥ì¥Ã¥Éñ°Ì¤ËÆÈΩ¤·¤ÆÄ´À°¤¹¤ë¤³¤È¤¬¤Ç¤¤ë¡£
345 ¥³¡¼¥ë¤«¤é¤ÎÊÖ¤êÃͤò¤³¤Î¥³¡¼¥ë¤Î
347 °ú¤¿ô¤È¤·¤ÆÅϤ¹¤³¤È¤¬¤Ç¤¤ë¡£
350 .\"O as 0 will set the attribute for the calling thread,
351 .\"O and passing the value returned from a call to
353 .\"O will set the attribute for the main thread of the thread group.
354 .\"O (If you are using the POSIX threads API, then use
355 .\"O .BR pthread_setaffinity_np (3)
357 .\"O .BR sched_setaffinity ().)
359 ¤Ë 0 ¤ò»ØÄꤹ¤ë¤È¸Æ¤Ó½Ð¤·¸µ¤Î¥¹¥ì¥Ã¥É¤Î°À¤¬ÀßÄꤵ¤ì¡¢
363 ¤Ë»ØÄꤹ¤ë¤È¥¹¥ì¥Ã¥É¥°¥ë¡¼¥×¤Î¥á¥¤¥ó¥¹¥ì¥Ã¥É¤Î°À¤¬ÀßÄꤵ¤ì¤ë
364 (POSIX ¥¹¥ì¥Ã¥É API ¤ò»ÈÍѤ·¤Æ¤¤¤ë¾ì¹ç¡¢
365 .BR sched_setaffinity ()
367 .BR pthread_setaffinity_np (3)
370 .\"O A child created via
372 .\"O inherits its parent's CPU affinity mask.
373 .\"O The affinity mask is preserved across an
376 ·Ðͳ¤ÇÀ¸À®¤µ¤ì¤¿»Ò¥×¥í¥»¥¹¤Ï¿Æ¥×¥í¥»¥¹¤Î CPU affinity ¥Þ¥¹¥¯¤ò·Ñ¾µ¤¹¤ë¡£
381 .\"O This manual page describes the glibc interface for the CPU affinity calls.
382 .\"O The actual system call interface is slightly different, with the
385 .\"O .IR "unsigned long *" ,
386 .\"O reflecting the fact that the underlying implementation of CPU
387 .\"O sets is a simple bit mask.
388 .\"O On success, the raw
389 .\"O .BR sched_getaffinity ()
390 .\"O system call returns the size (in bytes) of the
392 .\"O data type that is used internally by the kernel to
393 .\"O represent the CPU set bit mask.
394 ¤³¤Î¥Þ¥Ë¥å¥¢¥ë¥Ú¡¼¥¸¤Ç¤Ï CPU affinity ¥³¡¼¥ë¤Î glibc ¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤ò
395 ÀâÌÀ¤·¤Æ¤¤¤ë¡£¼ÂºÝ¤Î¥·¥¹¥Æ¥à¥³¡¼¥ë¡¦¥¤¥ó¥¿¥Õ¥§¡¼¥¹¤Ï¾¯¤·°ã¤Ã¤Æ¤ª¤ê¡¢
396 ¼ÂºÝ¤Î¼ÂÁõ¤Ç¤Ï CPU ½¸¹ç¤Ï´Êñ¤Ê¥Ó¥Ã¥È¥Þ¥¹¥¯¤Ç¤¢¤ë¤È¤¤¤¦¼Â¾õ¤òÈ¿±Ç¤·¡¢
399 .IR "unsigned long *"
402 .BR sched_getaffinity ()
405 ¥Ç¡¼¥¿·¿¤Î (¥Ð¥¤¥Èñ°Ì¤Î) Â礤µ¤òÊÖ¤¹¡£
407 ¤Ï¥«¡¼¥Í¥ëÆâÉô¤Ç CPU ½¸¹ç¤Î¥Ó¥Ã¥È¥Þ¥¹¥¯¤òɽ¸½¤¹¤ë¤Î¤Ë
408 »È¤ï¤ì¤Æ¤¤¤ë¥Ç¡¼¥¿·¿¤Ç¤¢¤ë¡£
416 .BR sched_get_priority_max (2),
417 .BR sched_get_priority_min (2),
418 .BR sched_getscheduler (2),
419 .BR sched_setscheduler (2),
422 .BR pthread_setaffinity_np (3),
423 .BR sched_getcpu (3),
424 .BR capabilities (7),