1SET_MEMPOLICY(2) Linux Programmer's Manual SET_MEMPOLICY(2)
2
3
4
6 set_mempolicy - set default NUMA memory policy for a thread and its
7 children
8
10 #include <numaif.h>
11
12 long set_mempolicy(int mode, const unsigned long *nodemask,
13 unsigned long maxnode);
14
15 Link with -lnuma.
16
18 set_mempolicy() sets the NUMA memory policy of the calling thread,
19 which consists of a policy mode and zero or more nodes, to the values
20 specified by the mode, nodemask and maxnode arguments.
21
22 A NUMA machine has different memory controllers with different dis‐
23 tances to specific CPUs. The memory policy defines from which node
24 memory is allocated for the thread.
25
26 This system call defines the default policy for the thread. The thread
27 policy governs allocation of pages in the process's address space out‐
28 side of memory ranges controlled by a more specific policy set by
29 mbind(2). The thread default policy also controls allocation of any
30 pages for memory-mapped files mapped using the mmap(2) call with the
31 MAP_PRIVATE flag and that are only read (loaded) from by the thread and
32 of memory-mapped files mapped using the mmap(2) call with the
33 MAP_SHARED flag, regardless of the access type. The policy is applied
34 only when a new page is allocated for the thread. For anonymous memory
35 this is when the page is first touched by the thread.
36
37 The mode argument must specify one of MPOL_DEFAULT, MPOL_BIND,
38 MPOL_INTERLEAVE, MPOL_PREFERRED, or MPOL_LOCAL (which are described in
39 detail below). All modes except MPOL_DEFAULT require the caller to
40 specify the node or nodes to which the mode applies, via the nodemask
41 argument.
42
43 The mode argument may also include an optional mode flag. The sup‐
44 ported mode flags are:
45
46 MPOL_F_STATIC_NODES (since Linux 2.6.26)
47 A nonempty nodemask specifies physical node IDs. Linux will not
48 remap the nodemask when the process moves to a different cpuset
49 context, nor when the set of nodes allowed by the process's cur‐
50 rent cpuset context changes.
51
52 MPOL_F_RELATIVE_NODES (since Linux 2.6.26)
53 A nonempty nodemask specifies node IDs that are relative to the
54 set of node IDs allowed by the process's current cpuset.
55
56 nodemask points to a bit mask of node IDs that contains up to maxnode
57 bits. The bit mask size is rounded to the next multiple of
58 sizeof(unsigned long), but the kernel will use bits only up to maxnode.
59 A NULL value of nodemask or a maxnode value of zero specifies the empty
60 set of nodes. If the value of maxnode is zero, the nodemask argument
61 is ignored.
62
63 Where a nodemask is required, it must contain at least one node that is
64 on-line, allowed by the process's current cpuset context, (unless the
65 MPOL_F_STATIC_NODES mode flag is specified), and contains memory. If
66 the MPOL_F_STATIC_NODES is set in mode and a required nodemask contains
67 no nodes that are allowed by the process's current cpuset context, the
68 memory policy reverts to local allocation. This effectively overrides
69 the specified policy until the process's cpuset context includes one or
70 more of the nodes specified by nodemask.
71
72 The mode argument must include one of the following values:
73
74 MPOL_DEFAULT
75 This mode specifies that any nondefault thread memory policy be
76 removed, so that the memory policy "falls back" to the system
77 default policy. The system default policy is "local alloca‐
78 tion"—that is, allocate memory on the node of the CPU that trig‐
79 gered the allocation. nodemask must be specified as NULL. If
80 the "local node" contains no free memory, the system will
81 attempt to allocate memory from a "near by" node.
82
83 MPOL_BIND
84 This mode defines a strict policy that restricts memory alloca‐
85 tion to the nodes specified in nodemask. If nodemask specifies
86 more than one node, page allocations will come from the node
87 with the lowest numeric node ID first, until that node contains
88 no free memory. Allocations will then come from the node with
89 the next highest node ID specified in nodemask and so forth,
90 until none of the specified nodes contain free memory. Pages
91 will not be allocated from any node not specified in the node‐
92 mask.
93
94 MPOL_INTERLEAVE
95 This mode interleaves page allocations across the nodes speci‐
96 fied in nodemask in numeric node ID order. This optimizes for
97 bandwidth instead of latency by spreading out pages and memory
98 accesses to those pages across multiple nodes. However,
99 accesses to a single page will still be limited to the memory
100 bandwidth of a single node.
101
102 MPOL_PREFERRED
103 This mode sets the preferred node for allocation. The kernel
104 will try to allocate pages from this node first and fall back to
105 "near by" nodes if the preferred node is low on free memory. If
106 nodemask specifies more than one node ID, the first node in the
107 mask will be selected as the preferred node. If the nodemask
108 and maxnode arguments specify the empty set, then the policy
109 specifies "local allocation" (like the system default policy
110 discussed above).
111
112 MPOL_LOCAL (since Linux 3.8)
113 This mode specifies "local allocation"; the memory is allocated
114 on the node of the CPU that triggered the allocation (the "local
115 node"). The nodemask and maxnode arguments must specify the
116 empty set. If the "local node" is low on free memory, the ker‐
117 nel will try to allocate memory from other nodes. The kernel
118 will allocate memory from the "local node" whenever memory for
119 this node is available. If the "local node" is not allowed by
120 the process's current cpuset context, the kernel will try to
121 allocate memory from other nodes. The kernel will allocate mem‐
122 ory from the "local node" whenever it becomes allowed by the
123 process's current cpuset context.
124
125 The thread memory policy is preserved across an execve(2), and is
126 inherited by child threads created using fork(2) or clone(2).
127
129 On success, set_mempolicy() returns 0; on error, -1 is returned and
130 errno is set to indicate the error.
131
133 EFAULT Part of all of the memory range specified by nodemask and maxn‐
134 ode points outside your accessible address space.
135
136 EINVAL mode is invalid. Or, mode is MPOL_DEFAULT and nodemask is
137 nonempty, or mode is MPOL_BIND or MPOL_INTERLEAVE and nodemask
138 is empty. Or, maxnode specifies more than a page worth of bits.
139 Or, nodemask specifies one or more node IDs that are greater
140 than the maximum supported node ID. Or, none of the node IDs
141 specified by nodemask are on-line and allowed by the process's
142 current cpuset context, or none of the specified nodes contain
143 memory. Or, the mode argument specified both
144 MPOL_F_STATIC_NODES and MPOL_F_RELATIVE_NODES.
145
146 ENOMEM Insufficient kernel memory was available.
147
149 The set_mempolicy() system call was added to the Linux kernel in ver‐
150 sion 2.6.7.
151
153 This system call is Linux-specific.
154
156 Memory policy is not remembered if the page is swapped out. When such
157 a page is paged back in, it will use the policy of the thread or memory
158 range that is in effect at the time the page is allocated.
159
160 For information on library support, see numa(7).
161
163 get_mempolicy(2), getcpu(2), mbind(2), mmap(2), numa(3), cpuset(7),
164 numa(7), numactl(8)
165
167 This page is part of release 4.16 of the Linux man-pages project. A
168 description of the project, information about reporting bugs, and the
169 latest version of this page, can be found at
170 https://www.kernel.org/doc/man-pages/.
171
172
173
174Linux 2017-09-15 SET_MEMPOLICY(2)