sched_setaffinityのヘルプ・マニュアル
日本語 英語
sched_setaffinity --help
man sched_setaffinity
SCHED_SETAFFINITY(2) Linux Programmer’s Manual SCHED_SETAFFINITY(2)
名前
sched_setaffinity, sched_getaffinity - プロセスの CPU affinity マスクを
設定・取得する
書式
#define _GNU_SOURCE
#include
int sched_setaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
説明
プロセスの CPU affinity (親和度) マスクは、そのプロセスが実行を許可され
ている CPU の集合を決定する。マルチプロセッサ・システムでは、CPU affin-
ity マスクを設定することで性能上のメリットを得られる可能性がある。例 え
ば 、 特 定 のプロセスを一つの CPU に括り付け (すなわち、そのプロセスの
affinity マスクを一つの CPU に設定し)、他の全てのプロセスの affinity マ
スクからその CPU を除外することで、確実にそのプロセスの実行速度を最大に
することができる。また、あるプロセスの実行を一つの CPU に限定することで
、 一つの CPU での実行を停止してから別の CPU で実行を再開するときに発生
するキャッシュ無効化 (cache invalidation) による性能面の劣化を避ける こ
ともできる。
CPU affinity マスクは「CPU の集合」を表す cpu_set_t 構造体で表現され、
cpu_set_t へのポインタ mask で指定される。 CPU 集合を操作するためのマク
ロ群については CPU_SET(3) で記載されている。
sched_setaffinity() は、プロセスID が pid のプロセスの CPU affinity マ
スクを mask で指定された値に設定する。 pid が 0 の場合、呼び出し元プ ロ
セ スが使われる。 cpusetsize 引き数には mask が指すデータの長さ (バイト
単位) である。通常は、この引き数には sizeof(cpu_set_t) を指定すればよい
。
pid で指定されたプロセスが mask で指定された CPU のいずれかで現在実行さ
れていない場合、そのプロセスは mask で指定された CPU のいずれかに移動さ
れる。
sched_getaffinity() は、プロセスID が pid のプロセスの affinity マスク
を mask が指す cpu_set_t 構造体に書き込む。 cpusetsize 引き数には mask
の (バイト単位の) 大きさを指定する。
関 数 sched_getaffinity() は長さ len のポインタ mask にプロセス pid の
affinity マスクを書き込む。 pid が 0 の場合、呼び出し元のプロセスのマス
クが返される。
返り値
成功した場合、 sched_setaffinity() と sched_getaffinity() は 0 を返す。
エラーの場合は -1 を返し、 errno を適切に設定する。
エラー
EFAULT 指定されたメモリ番地が不正である。
EINVAL システム上に現在実際に存在し、かつ "cpuset" 機構が課す制限におい
てそのプロセスに対して許可されているプロセッサが、 affinity ビッ
トマスク mask に含まれていない。 "cpuset" 機 構 に つ い て は
cpuset(7) を参照。
EINVAL (sched_getaffinity() と、カーネル 2.6.9 以前の sched_setaffin-
ity()) cpusetsize がカーネルで使われている affinity マスクのサイ
ズより小さい。
EPERM (sched_setaffinity()) 呼び出し元のプロセスに適切な特権がなかった
。呼び出し元は、実効ユーザ ID が pid で識別されるプロセスのユ ー
ザ ID または実効ユーザ ID と同じであるか、 CAP_SYS_NICE ケーパビ
リティ (capability) を持たなければならない。
ESRCH プロセス ID pid のプロセスが見つからなかった。
バージョン
CPU affinity システムコールは Linux kernel 2.5.8 で導入された。これらの
システムコールのラッパー関数は glibc 2.3 で導入された。最初は、glibc の
インタフェースには unsigned int 型の cpusetsize 引き数が入って い た 。
glibc 2.3.3 では cpusetsize 引き数が削除されたが、glibc 2.3.4 で size_t
型で復活した。
準拠
これらのシステムコールは Linux 固有である。
注意
sched_setaffinity() を呼び出した後は、プロセスが実際に実行される CPU の
集 合は、 mask 引き数で指定された集合と、システム上に実際に存在する CPU
の集合の共通集合 (AND) となる。 "cpuset" 機構が使用されている場合には、
プロセスが動作する CPU 集合に対してシステムはさらに制限を加えるかもしれ
ない ("cpuset" 機構については cpuset(7) を参照)。プロセスが動作する実際
の CPU 集合に対する制限はカーネルにより暗黙のうちに適用される。
sched_setscheduler(2) には Linux におけるスケジューリング機構についての
説明がある。
実際には affinity マスクはスレッド単位の属性で、スレッドグループの各 ス
レッド単位に独立して調整することができる。 gettid(2) コールからの返り値
をこのコールの pid 引き数として渡すことができる。 pid に 0 を指定すると
呼 び 出し元のスレッドの属性が設定され、 getpid(2) コールからの返り値を
pid に指定するとスレッドグループのメインスレッドの属性が 設 定 さ れ る
(POSIX スレッド API を使用している場合、 sched_setaffinity() の代わりに
pthread_setaffinity_np(3) を使用すること)。
fork(2) 経由で生成された子プロセスは親プロセスの CPU affinity マスク を
継承する。 affinity マスクは execve(2) の前後で保存される。
このマニュアルページでは CPU affinity コールの glibc インタフェースを説
明している。実際のシステムコール・インタフェースは少し違っており、実 際
の 実装では CPU 集合は簡単なビットマスクであるという実状を反映し、 mask
の型が unsigned long * となっている。成功時には、生 の sched_getaffin-
ity() システムコール自身は cpumask_t データ型の (バイト単位の) 大きさを
返す。 cpumask_t はカーネル内部で CPU 集合のビットマスクを表現するの に
使われているデータ型である。
関連項目
clone(2), getcpu(2), getpriority(2), gettid(2), nice(2), sched_get_pri-
ority_max(2), sched_get_priority_min(2), sched_getscheduler(2),
sched_setscheduler(2), setpriority(2), pthread_setaffinity_np(3),
CPU_SET(3), sched_getcpu(3), capabilities(7), cpuset(7)
Linux 2008-11-14 SCHED_SETAFFINITY(2)
SCHED_SETAFFINITY(2) Linux Programmer’s Manual SCHED_SETAFFINITY(2)
NAME
sched_setaffinity, sched_getaffinity - set and get a process’s CPU
affinity mask
SYNOPSIS
#define _GNU_SOURCE
#include
int sched_setaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
int sched_getaffinity(pid_t pid, size_t cpusetsize,
cpu_set_t *mask);
DESCRIPTION
A process’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 dedi-
cating one CPU to a particular process (i.e., setting the affinity mask
of that process to specify a single CPU, and setting the affinity mask
of all other processes to exclude that CPU), it is possible to ensure
maximum execution speed for that process. Restricting a process to run
on a single CPU also avoids the performance cost caused by the cache
invalidation that occurs when a process ceases to execute on one CPU
and then recommences execution on a different CPU.
A CPU affinity mask is represented by the cpu_set_t structure, a "CPU
set", pointed to by mask. A set of macros for manipulating CPU sets is
described in CPU_SET(3).
sched_setaffinity() sets the CPU affinity mask of the process whose ID
is pid to the value specified by mask. If pid is zero, then the call-
ing process is used. The argument cpusetsize is the length (in bytes)
of the data pointed to by mask. Normally this argument would be speci-
fied as sizeof(cpu_set_t).
If the process specified by pid is not currently running on one of the
CPUs specified in mask, then that process is migrated to one of the
CPUs specified in mask.
sched_getaffinity() writes the affinity mask of the process whose ID is
pid into the cpu_set_t structure pointed to by mask. The cpusetsize
argument specifies the size (in bytes) of mask. If pid is zero, then
the mask of the calling process is returned.
RETURN VALUE
On success, sched_setaffinity() and sched_getaffinity() return 0. On
error, -1 is returned, and errno is set appropriately.
ERRORS
EFAULT A supplied memory address was invalid.
EINVAL The affinity bit mask mask contains no processors that are cur-
rently physically on the system and permitted to the process
according to any restrictions that may be imposed by the
"cpuset" mechanism described in cpuset(7).
EINVAL (sched_getaffinity() and, in kernels before 2.6.9,
sched_setaffinity()) cpusetsize is smaller than the size of the
affinity mask used by the kernel.
EPERM (sched_setaffinity()) The calling process does not have appro-
priate privileges. The caller needs an effective user ID equal
to the user ID or effective user ID of the process identified by
pid, or it must possess the CAP_SYS_NICE capability.
ESRCH The process whose ID is pid could not be found.
VERSIONS
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 cpusetsize argument, typed as unsigned int.
In glibc 2.3.3, the cpusetsize argument was removed, but was then
restored in glibc 2.3.4, with type size_t.
CONFORMING TO
These system calls are Linux-specific.
NOTES
After a call to sched_setaffinity(), the set of CPUs on which the pro-
cess will actually run is the intersection of the set specified in the
mask argument and the set of CPUs actually present on the system. The
system may further restrict the set of CPUs on which the process runs
if the "cpuset" mechanism described in cpuset(7) is being used. These
restrictions on the actual set of CPUs on which the process will run
are silently imposed by the kernel.
sched_setscheduler(2) has a description of the Linux scheduling scheme.
The affinity mask is actually 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 gettid(2) can be passed in the argument
pid. Specifying pid as 0 will set the attribute for the calling
thread, and passing the value returned from a call to getpid(2) will
set the attribute for the main thread of the thread group. (If you are
using the POSIX threads API, then use pthread_setaffinity_np (3)
instead of sched_setaffinity().)
A child created via fork(2) inherits its parent’s CPU affinity mask.
The affinity mask is preserved across an execve(2).
This manual page describes the glibc interface for the CPU affinity
calls. The actual system call interface is slightly different, with
the mask being typed as unsigned long *, reflecting the fact that the
underlying implementation of CPU sets is a simple bit mask. On suc-
cess, the raw sched_getaffinity() system call returns the size (in
bytes) of the cpumask_t data type that is used internally by the kernel
to represent the CPU set bit mask.
SEE ALSO
clone(2), getcpu(2), getpriority(2), gettid(2), nice(2), sched_get_pri-
ority_max(2), sched_get_priority_min(2), sched_getscheduler(2),
sched_setscheduler(2), setpriority(2), CPU_SET(3), sched_getcpu(3),
capabilities(7), pthread_setaffinity_np(3), cpuset(7)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
Linux 2008-11-14 SCHED_SETAFFINITY(2)