Skip to content
systemcoding.dev
Performance

Bind Processes to NUMA Nodes With numactl --cpunodebind and --membind

Pin a process and its memory to one NUMA node with numactl --cpunodebind and --membind, compare it with --interleave and --preferred, and verify placement.

5 min read
Summarize in AI
Flat illustration of a CPU core with its cache levels and main memory, with the C, Intel and Apple logos.

When a program runs on one socket and keeps its data on the other, every cache miss crosses the interconnect. The simplest fix needs no code change at all: start the program under numactl and tell it which node's CPUs to run on and which node's memory to allocate from. --cpunodebind restricts where threads run, --membind restricts where pages come from, and together they keep a process entirely local to one node. This post shows the two flags, the neighboring policies --interleave, --preferred and --localalloc, how to verify the result, and the failure that surprises people most: a hard bind that runs the node out of memory. It follows the topology reading in inspecting NUMA topology and uses numactl 2.0.19 on a current Linux kernel.

The two halves of a binding

A NUMA binding has a CPU half and a memory half, and they are independent settings.

--cpunodebind=N (short form -N) restricts the process to the CPUs of node N, which you can list with numactl --hardware. Under the hood this is a CPU affinity mask, the same mechanism taskset and sched_setaffinity use. --physcpubind= takes explicit CPU numbers when you want individual cores instead of a whole node.

--membind=N (short form -m N) sets a memory policy of bind with node N. Pages for the process are allocated only from that node. If the node has no free memory, the allocation does not fall back to another node. The kernel first reclaims what it can from that node, and failing that, the process is killed by the out-of-memory killer, or a call fails with ENOMEM, depending on the call.

Run the pair together and the process is local:

Text
numactl --cpunodebind=0 --membind=0 ./server

Prove it worked

Never assume a binding took effect. Start the program, then inspect where its pages actually are. numastat -p <pid> prints per-node memory for a process, and /proc/<pid>/numa_maps lists each mapping with the node counts.

Text
numactl --cpunodebind=0 --membind=0 ./server &
numastat -p $!

Illustrative output for a process that allocated memory only on node 0:

Text
Per-node process memory usage (in MBs) for PID 20311 (server)
                           Node 0          Node 1           Total
                  --------------- --------------- ---------------
Huge                         0.00            0.00            0.00
Heap                        42.17            0.00           42.17
Stack                        0.04            0.00            0.04
Private                   2048.55            0.01         2048.56
----------------  --------------- --------------- ---------------
Total                     2090.76            0.01         2090.77

If node 1 shows a large number, something escaped the binding: a child that reset its policy, shared memory created by another process, or the page cache. numa_maps is finer-grained, and each line ends with counts such as N0=512, meaning 512 pages on node 0 for that mapping. Reading the file is documented in the kernel's proc filesystem documentation.

Choose the right policy

--membind is not always the right call. The four common choices behave differently.

PolicyFlagBehaviorUse when
Bind--membind=NAllocate only on the listed nodes, fail or kill if fullThe working set fits and you want strict locality
Preferred--preferred=NTry node N first, fall back to othersLocality matters but running out must not kill the process
Interleave--interleave=0,1 or --interleave=allRound-robin pages across nodesA large shared structure is read by threads on every node
Local--localallocAllocate on the node of the CPU that touches the pageYou want the default behavior explicitly

Interleave spreads bandwidth. If one large table is read by threads on both sockets, binding it to one node makes that node's memory controller and the link into it the bottleneck. Interleaving gives each node an equal share of the pages, so the load spreads across both controllers, and the average access is half local and half remote. That is worse than perfect locality and better than all-remote.

Preferred is the safer default for a service whose memory use is not tightly known. Strict bind turns a transient spike into a kill.

The out-of-memory trap

A bound process sees only the memory of its node. A machine with 256 GB in total and two nodes of 128 GB gives a node-0-bound process 128 GB, no more. A database with a 160 GB buffer pool bound to node 0 will hit the limit while node 1 sits idle. Check node 0 free in numactl --hardware before binding, and size the working set to fit.

Page cache complicates it further. A process bound with --membind can still push file cache onto its node, and the kernel will reclaim cache before refusing an allocation, but a flood of dirty pages can make allocation stall. If a bound service shows direct-reclaim stalls in /proc/vmstat, relax the bind to --preferred.

Bind inside the program

numactl is the quick tool. A program can set the same policies itself with the system calls that numactl wraps: set_mempolicy for the whole thread, mbind for one range, and sched_setaffinity for CPUs. The wrappers live in <numaif.h> and <numa.h> (link with -lnuma). A minimal version of numactl --cpunodebind=0 --membind=0 is:

bindnode.cc
#include <numa.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    if (numa_available() < 0) { puts("no NUMA"); return 1; }

    numa_run_on_node(0);           /* CPUs of node 0 only */
    numa_set_preferred(0);         /* soft: prefer node 0 for new pages */

    struct bitmask *nodes = numa_allocate_nodemask();
    numa_bitmask_setbit(nodes, 0);
    numa_set_membind(nodes);       /* hard: pages only from node 0 */
    numa_free_nodemask(nodes);

    int cpu = numa_node_of_cpu(0);
    printf("cpu 0 is on node %d\n", cpu);
    return 0;
}

numa_run_on_node(0) sets the affinity mask to node 0's CPUs, and numa_set_membind installs the bind policy for later allocations. The program calls numa_set_preferred(0) before the hard bind only to show the soft form, and the second call overrides the first. On the machine from the topology post, it prints:

Text
cpu 0 is on node 0

Compile with gcc bindnode.c -lnuma. Calling numa_run_on_node before touching memory matters, because the first touch decides placement, as explained in first-touch page placement.

Binding and containers

In a container, the cpuset cgroup controller applies the same restriction from outside: cpuset.cpus and cpuset.mems list the allowed CPUs and memory nodes. A process inside can only narrow that set. If numactl --membind=1 is refused with "set_mempolicy: Invalid argument", the node is not in cpuset.mems. Kubernetes exposes this through the CPU manager and topology manager policies rather than through numactl.

When binding hurts

Bind a multi-threaded server to one node and you cap it at that node's cores and memory. If the program is not memory-bound, the lost cores cost more than remote latency would. Bind per process, not per machine: run one instance per node, each bound to its own, and put a load balancer in front. That uses every core and keeps each instance local, and it is the usual shape of a well-tuned NUMA deployment.

Also inHardware
Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.