CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Performance Tuning / numactl

numactl

Control NUMA CPU and memory placement

Advancedperformancenumamemory

On servers with more than one CPU socket, each socket has its own 'local' memory and reaching the other socket's memory is slower. numactl shows that layout and runs a program with a chosen CPU and memory policy: pinned to one node, or interleaved across all of them.

Syntaxnumactl [--hardware | --show] | numactl [--cpunodebind=N] [--membind=N | --interleave=all | --preferred=N] [--] COMMAND [ARG]...

Watch it run

Before tuning anything you want to know whether NUMA even matters on this machine.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
numactl --hardware

One node: every CPU is equally close to all memory. This is the case on most cloud VMs and small servers, and numactl tuning has nothing to improve here.

Anatomy of a command

$
commandnumactl

Sets a NUMA policy for itself, then execs the command, which inherits it.

How it works

On a multi-socket machine the memory controllers live in the CPUs, so each socket (a NUMA node) has fast local RAM and slower remote RAM reached over the interconnect. The kernel publishes the layout in /sys/devices/system/node: CPUs per node, memory per node and a distance table (10 = local; 20-32 = remote, roughly 1.5-2x the latency). By default Linux allocates a page on the node of the CPU that first touches it. numactl changes that for one program: it calls sched_setaffinity for --cpunodebind/--physcpubind and set_mempolicy for --membind (only these nodes), --preferred (try this node first) or --interleave (round-robin pages across nodes), then execs the command; children inherit both. numastat reads per-node counters (numa_hit, numa_miss, other_node) and per-process placement from /proc/PID/numa_maps. Most cloud VMs below the largest sizes expose a single node, where numactl changes nothing.

Options you'll actually use

--hardware, -HNodes, their CPUs, memory size and free memory, distances
--show, -sThe NUMA policy and CPU binding of the current process
--cpunodebind=NRun on the CPUs of node(s) N (e.g. 0 or 0,1)
--physcpubind=LISTRun on specific CPUs, e.g. 0-7
--membind=NAllocate only from node(s) N; fail (or OOM) if they're full
--preferred=NPrefer node N, fall back to others when it's full
--interleave=allSpread pages round-robin across nodes (databases with huge caches)
--localallocAlways allocate on the node the thread is running on
numastat [-p PID|NAME]Per-node hit/miss counters; per-process memory by node

Common mistakes

Adding numactl tuning on a cloud VM with a single NUMA node

With one node there is no remote memory; the policy has no effect and only makes the start command harder to read.

Check numactl --hardware or lscpu first
Using --membind for a process whose memory can exceed one node

membind is strict: when that node is full the process is OOM-killed even if other nodes are empty.

--preferred=N to prefer a node but allow spill, or --interleave=all for big shared caches
Pinning CPUs with --physcpubind but not the memory

Threads run on node 0 while memory first touched elsewhere stays remote, so you get the restriction without the benefit.

Pair --cpunodebind=N with --membind=N or --localalloc

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Find how many NUMA nodes this machine has and how much memory each one has free.

  2. 2

    Show the NUMA policy and CPU binding of your current shell.

  3. 3

    Run 'numactl --show' under an interleave policy across all nodes and read the policy line.

Related commands

lscpuShow CPU architecture, cores, threads and cachesfreeShow used, free and available memorysysctlRead and tune kernel parameters at runtimempstatPer-CPU utilisation: find the hot core
Previousulimit
Linux / Performance Tuning / numactl

numactl

Control NUMA CPU and memory placement

Advancedperformancenumamemory

On servers with more than one CPU socket, each socket has its own 'local' memory and reaching the other socket's memory is slower. numactl shows that layout and runs a program with a chosen CPU and memory policy: pinned to one node, or interleaved across all of them.

Syntaxnumactl [--hardware | --show] | numactl [--cpunodebind=N] [--membind=N | --interleave=all | --preferred=N] [--] COMMAND [ARG]...

Watch it run

Before tuning anything you want to know whether NUMA even matters on this machine.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 2
numactl --hardware

One node: every CPU is equally close to all memory. This is the case on most cloud VMs and small servers, and numactl tuning has nothing to improve here.

Anatomy of a command

$
commandnumactl

Sets a NUMA policy for itself, then execs the command, which inherits it.

How it works

On a multi-socket machine the memory controllers live in the CPUs, so each socket (a NUMA node) has fast local RAM and slower remote RAM reached over the interconnect. The kernel publishes the layout in /sys/devices/system/node: CPUs per node, memory per node and a distance table (10 = local; 20-32 = remote, roughly 1.5-2x the latency). By default Linux allocates a page on the node of the CPU that first touches it. numactl changes that for one program: it calls sched_setaffinity for --cpunodebind/--physcpubind and set_mempolicy for --membind (only these nodes), --preferred (try this node first) or --interleave (round-robin pages across nodes), then execs the command; children inherit both. numastat reads per-node counters (numa_hit, numa_miss, other_node) and per-process placement from /proc/PID/numa_maps. Most cloud VMs below the largest sizes expose a single node, where numactl changes nothing.

Options you'll actually use

--hardware, -HNodes, their CPUs, memory size and free memory, distances
--show, -sThe NUMA policy and CPU binding of the current process
--cpunodebind=NRun on the CPUs of node(s) N (e.g. 0 or 0,1)
--physcpubind=LISTRun on specific CPUs, e.g. 0-7
--membind=NAllocate only from node(s) N; fail (or OOM) if they're full
--preferred=NPrefer node N, fall back to others when it's full
--interleave=allSpread pages round-robin across nodes (databases with huge caches)
--localallocAlways allocate on the node the thread is running on
numastat [-p PID|NAME]Per-node hit/miss counters; per-process memory by node

Common mistakes

Adding numactl tuning on a cloud VM with a single NUMA node

With one node there is no remote memory; the policy has no effect and only makes the start command harder to read.

Check numactl --hardware or lscpu first
Using --membind for a process whose memory can exceed one node

membind is strict: when that node is full the process is OOM-killed even if other nodes are empty.

--preferred=N to prefer a node but allow spill, or --interleave=all for big shared caches
Pinning CPUs with --physcpubind but not the memory

Threads run on node 0 while memory first touched elsewhere stays remote, so you get the restriction without the benefit.

Pair --cpunodebind=N with --membind=N or --localalloc

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Find how many NUMA nodes this machine has and how much memory each one has free.

  2. 2

    Show the NUMA policy and CPU binding of your current shell.

  3. 3

    Run 'numactl --show' under an interleave policy across all nodes and read the policy line.

Related commands

lscpuShow CPU architecture, cores, threads and cachesfreeShow used, free and available memorysysctlRead and tune kernel parameters at runtimempstatPer-CPU utilisation: find the hot core
Previousulimit