set_mempolicy

Section: System Calls (2)
Updated: 2026-02-08
Index JM Home Page roff page
 

名前

set_mempolicy - スレッドとその子スレッドの NUMA メモリーの デフォルトポリシーを設定する  

ライブラリ

NUMA (Non-Uniform Memory Access) ポリシーライブラリ (libnuma,~-lnuma)  

書式

#include <numaif.h>

long set_mempolicy(int mode, const unsigned long *nodemask,
                   unsigned long maxnode);
 

説明

set_mempolicy() は、呼び出し元スレッドの NUMA ポリシーを policy に設定する。 NUMA ポリシーはポリシーモードと 0 個以上のノードから構成され、 設定内容は引数 mode, nodemask, maxnode により指定される。

NUMA (非対称メモリーアクセス) マシンでは、CPU により メモリーコントローラーが異なり、距離も異なっている。 メモリーポリシーは、どのノードからメモリーをそのスレッドに 割り当てるかを定めるものである。

set_mempolicy() は、スレッドのデフォルトポリシーを定める。スレッドのポリシーは、 mbind(2) で設定される特定のポリシーにより制御されるメモリー領域以外のスレッドのアドレス空間におけるページの割り当てに適用される。 スレッドのデフォルトポリシーは、 MAP_PRIVATE フラグを指定した mmap(2) を使ってマップされたメモリーマップファイルに対する読み出し専用 (ロードされるだけ) のページの割り当てにも適用される。 また、 MAP_SHARED フラグを指定した mmap(2) を使ってマップされたメモリーマップファイルに対するページの割り当てにも 適用される (この場合はページのアクセス種別に関わらず適用される) 。 設定したポリシーは、スレッドに新規のページが割り当てられるときにのみ 適用される。無名メモリー (anonymous memory) の場合、新規ページの割り当ては アプリケーションが初めてページにアクセスした際に行われる。

The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND, MPOL_INTERLEAVE, MPOL_WEIGHTED_INTERLEAVE, MPOL_PREFERRED, MPOL_PREFERRED_MANY, or MPOL_LOCAL (which are described in detail below). All modes except MPOL_DEFAULT require the caller to specify the node or nodes to which the mode applies, via the nodemask argument.

mode 引数には、追加で モードフラグ を含めることもできる。 サポートされている モードフラグ は以下の通りである。

MPOL_F_NUMA_BALANCING (Linux 5.12 以降)
When mode is MPOL_BIND, enable the kernel NUMA balancing for the task if it is supported by the kernel. If the flag isn't supported by the kernel, or is used with mode other than MPOL_BIND, -1 is returned and errno is set to EINVAL.
MPOL_F_RELATIVE_NODES (Linux 2.6.26 以降)
空でない nodemask は、そのプロセスの現在の CPU 集合で許可されているノード ID 集合 における相対的なノード ID である。
MPOL_F_STATIC_NODES (Linux 2.6.26 以降)
空でない nodemask は、物理ノード ID である。 Linux では、そのプロセスが異なる CPU 集合コンテキスト (cpuset context) に移動した場合でも、そのプロセスの現在の CPU 集合コンテキストで 許可されているノード集合が変化した場合でも、 nodemask をマッピングし直すことはない。

nodemask は、最大で maxnode ビットから構成されるノード ID のビットマスクを指す。 ビットマスクの大きさは、直近の sizeof(unsigned long) の倍数に切り上げられるが、カーネルが使用するのは maxnode 個までのビットだけである。 NULL 値の nodemask もしくは値が 0 の maxnode はノードの空集合を表す。 maxnode の値が 0 の場合、 nodemask 引数は無視される。

nodemask が必須の場面では、 nodemask に、オンラインで、そのプロセスの現在の CPU 集合コンテキストで 許可されており (MPOL_F_STATIC_NODES モードフラグが指定されていない場合)、メモリーがあるノードが 少なくとも一つ入っていなければならない。 mode に MPOL_F_STATIC_NODES がセットされ、 nodemask が必須の場面で、 nodemask に、そのプロセスの現在の CPU 集合コンテキストで許可されたノードが 一つも含まれていない場合には、メモリーのポリシーとしてデフォルトの 「ローカルから割り当て (local allocation)」が適用される。 そのプロセスの CPU 集合コンテキストが nodemask で指定されたノードを少なくとも一つ含むようになるまでは、 結果的に指定された動作が上書きされることになる。

mode 引数には、以下の値のいずれかひとつを含めなければならない。

MPOL_DEFAULT
This mode specifies that any nondefault thread memory policy be removed, so that the memory policy "falls back" to the system default policy. The system default policy is "local allocation"---that is, allocate memory on the node of the CPU that triggered the allocation. nodemask must be specified as NULL. If the "local node" contains no free memory, the system will attempt to allocate memory from a "near by" node.
MPOL_BIND
このモードは厳密なポリシーで、メモリー割り当ては nodemask に指定されたノードに限定される。 nodemask に 2 個以上のノードが指定された場合、ページの割り当ては ノード ID が数字として最小のノードから開始され、 そのノードに空きメモリーがなくなるまでそのノードから ページ割り当てが行われる。そのノードに空きメモリーがなくなったら、 次に小さなノード ID を持つノードからページ割り当てが行われる。 これを、 nodemask で指定された全てのノードで空きメモリーがなくなるまで繰り返す。 nodemask で指定された以外のノードからはページの割り当ては行われない。
MPOL_INTERLEAVE
このモードでは、ページ割り当てを nodemask に指定されたノードの間でノード ID の数字順で交互に行う。 このポリシーでは、複数のノードにページを広げて配置し、これらのページへのメモリー アクセスを分散することで、遅延ではなく、帯域を最適化する。 ただし、一つのページへのアクセスに関しては依然として一つのノードのメモリー帯域 が上限となる。
MPOL_WEIGHTED_INTERLEAVE (Linux 6.9 以降)
This mode interleaves page allocations across the nodes specified in nodemask according to the weights in /sys/kernel/mm/mempolicy/weighted_interleave. For example, if bits 0, 2, and 5 are set in nodemask, and the contents of /sys/kernel/mm/mempolicy/weighted_interleave/node0, /sys/.../node2, and /sys/.../node5 are 4, 7, and 9, respectively, then pages in this region will be allocated on nodes 0, 2, and 5 in a 4:7:9 ratio.
MPOL_PREFERRED
このモードは、割り当て時に優先されるノード (preferred node) を設定する。 カーネルはまず優先ノードからページ割り当てを行おうとし、 優先ノードに空きメモリーが少ない場合に「近くの (near by)」ノードから 割り当てを行う。 nodemask に複数のノード ID が指定された場合は、 nodemask 内の最初のノードが優先ノードとして選択される。 引数 nodemask, maxnode で空集合が指定された場合は、ポリシーは (上述のシステムの デフォルトポリシーと同様に) 「ローカルからの割り当て」となる。
MPOL_PREFERRED_MANY (Linux 5.15 以降)
This mode specifies a preference for nodes from which the kernel will try to allocate from. This differs from MPOL_PREFERRED in that it accepts a set of nodes versus a single node. This policy is intended to benefit page allocations where specific memory types (i.e., non-volatile, high-bandwidth, or accelerator memory) are of greater importance than node location.
MPOL_LOCAL (Linux 3.8 以降)
This mode specifies "local allocation"; the memory is allocated on the node of the CPU that triggered the allocation (the "local node"). The nodemask and maxnode arguments must specify the empty set. If the "local node" is low on free memory, the kernel will try to allocate memory from other nodes. The kernel will allocate memory from the "local node" whenever memory for this node is available. If the "local node" is not allowed by the process's current cpuset context, the kernel will try to allocate memory from other nodes. The kernel will allocate memory from the "local node" whenever it becomes allowed by the process's current cpuset context.

スレッドのメモリーポリシーは execve(2) の前後で保持され、 fork(2) や clone(2) を使って作成された子スレッドに継承される。  

返り値

成功すると、 set_mempolicy() は 0 を返す。エラーの場合、-1 を返し、 errno にエラーを示す値を設定する。  

エラー

EFAULT
nodemask と maxnode で指定されたメモリー領域の一部または全部が、 呼び出し元がアクセス可能なアドレス空間外を指している。
EINVAL
mode is invalid. Or, mode is MPOL_DEFAULT and nodemask is nonempty, or mode is MPOL_BIND or MPOL_INTERLEAVE and nodemask is empty. Or, maxnode specifies more than a page worth of bits. Or, nodemask specifies one or more node IDs that are greater than the maximum supported node ID. Or, none of the node IDs specified by nodemask are on-line and allowed by the process's current cpuset context, or none of the specified nodes contain memory. Or, the mode argument specified both MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES. Or, the MPOL_F_NUMA_BALANCING isn't supported by the kernel, or is used with mode other than MPOL_BIND.
ENOMEM
利用可能なカーネルメモリーが十分でなかった。
 

標準規格

Linux.  

歴史

Linux 2.6.7.  

注意

ページがスワップアウトされる場合には、 メモリポリシーの設定は推奨されない。 スワップアウトされたページがページインする際にも、 ページ割り当て時に適用されるスレッドのポリシーやメモリー領域ポリシーが 使用される。

ライブラリによるサポートについては numa(7) を参照。  

関連項目

get_mempolicy(2), getcpu(2), mbind(2), mmap(2), numa(3), cpuset(7), numa(7), numactl(8)


 

Index

名前
ライブラリ
書式
説明
返り値
エラー
標準規格
歴史
注意
関連項目

This document was created by man2html, using the manual pages.