Self-Hosted eBPF NUMA-Aware Scheduling and Memory Tiering: A Practical Guide
Quick Summary (TL;DR)
- On multi-socket servers, remote memory access can cost 1.5–3× more than local access. Ignoring NUMA silently caps throughput.
- Use eBPF (
bpftrace, BCC) to measure NUMA locality misses and page migrations before you change anything — never tune blind.- Apply placement with
numactl,taskset, cgroupscpuset.mems, and systemdNUMAPolicyfor latency-sensitive workloads.- Enable kernel NUMA balancing and, where hardware supports it, CXL/DRAM memory tiering so hot pages stay in DRAM and cold pages demote to CXL.
- Validate every change with the same eBPF probes and a repeatable benchmark. Measure → place → tier → re-measure.
Introduction to NUMA and Memory Tiering
Modern self-hosted servers are rarely single-socket. A 2-, 4-, or 8-socket machine is a NUMA (Non-Uniform Memory Access) system: each CPU socket owns a slice of DRAM, and a CPU can reach its local memory quickly but must traverse an interconnect (UPI/Infinity Fabric/xGMI) to reach remote memory. That traversal is the source of most “mystery” latency on otherwise healthy hardware.
Two forces make this urgent in 2024+:
- Core counts keep climbing, so a single process is more likely to straddle sockets.
- Memory tiering (CXL-attached memory, persistent memory) is now mainstream. CXL memory is cheaper per GB but slower than DRAM, so the kernel must actively migrate hot pages up to DRAM and demote cold pages down to CXL.
This guide is a hands-on workflow: observe with eBPF, place workloads deliberately, then tier memory intelligently — and prove the win with numbers.
Understanding NUMA Locality and Cross-Node Latency
A NUMA node bundles CPUs and their directly attached memory. The kernel exposes topology via numactl --hardware and /sys/devices/system/node/. The mental model:
flowchart TD
subgraph Node0["NUMA Node 0 (Socket 0)"]
C0[CPUs 0-15] --> M0[(Local DRAM)]
end
subgraph Node1["NUMA Node 1 (Socket 1)"]
C1[CPUs 16-31] --> M1[(Local DRAM)]
end
C0 -. "remote access (slow)" .-> M1
C1 -. "remote access (slow)" .-> M0
CXL[(CXL Memory Tier<br/>slower, larger)] --- Node0
CXL --- Node1
Typical latency on a 2-socket Xeon/EPYC box: local ~80–100 ns, remote ~140–180 ns. That gap is why a workload whose pages live on the “wrong” node loses 40–80% of its memory-bound throughput. The kernel’s NUMA balancing feature periodically unmaps pages so the next access faults, letting the scheduler detect whether a page is being touched from a remote node and migrate it closer.
The catch: NUMA balancing is heuristic and costs CPU (fault overhead). It works well for steady workloads and poorly for short-lived or highly shared data. Your job is to know which regime you’re in — and eBPF tells you.
Observing NUMA Misses with eBPF
Before tuning, quantify. The kernel emits tracepoints and kprobes around NUMA balancing. The two most useful signals:
numa_migrate_misplaced_page— a page was migrated because it was being accessed from a remote node.numa_pte_updates— PTE updates from NUMA balancing (a proxy for “the balancer is working”).
A bpftrace one-liner to count misplaced-page migrations per process:
sudo bpftrace -e '
tracepoint:kmem:numa_migrate_misplaced_page
{
@migrated[comm] = count();
}
interval:s:10 { print(@migrated); clear(@migrated); }
'
To see which nodes pages move between, hook the migration kprobe and read the target node:
sudo bpftrace -e '
kprobe:migrate_misplaced_page
{
@from_to[comm, arg2] = count(); # arg2 = target nid (verify per kernel)
}
'
Tip: Tracepoint argument layouts drift between kernel versions. Always confirm field offsets with
bpftrace -lv 'tracepoint:kmem:*'orcat /sys/kernel/debug/tracing/events/kmem/numa_migrate_misplaced_page/formatbefore trusting a rawargN.
If you prefer BCC, perf with --call-graph on numa_* events gives you stacks showing which code path triggered the migration — invaluable for pointing at a specific allocation site.
Tracing Page Migration Behavior
NUMA balancing isn’t the only migrator. migrate_pages(2) syscalls (from numactl --migrate, mbind, or set_mempolicy) and memory-tiering demotion also move pages. Trace them separately so you don’t confuse “the balancer is correcting a miss” with “an operator moved a page.”
sequenceDiagram
participant App
participant Kernel as Kernel (NUMA balancer)
participant eBPF
App->>Kernel: access page on remote node
Kernel->>Kernel: unmap PTE (numa_pte_updates)
Kernel->>eBPF: tracepoint numa_migrate_misplaced_page
Kernel->>Kernel: migrate page to local node
Note over eBPF: count per-comm, per-node
App->>Kernel: migrate_pages() syscall (manual)
Kernel->>eBPF: kprobe migrate_pages
Trace the manual path with a syscall probe:
sudo bpftrace -e '
tracepoint:syscalls:sys_enter_migrate_pages
{
printf("%s migrating pid=%d\n", comm, pid);
}
'
Combine both signals into a single dashboard metric: migrations/sec per node pair. A healthy steady-state workload should show migrations decaying toward zero once pages settle. A workload that migrates forever is thrashing — usually shared memory touched by two nodes, where manual pinning beats balancing.
NUMA-Aware CPU and Memory Placement
Once you’ve measured, place deliberately. The tools, ranked by persistence:
| Method | Scope | Persistence | Best for |
|---|---|---|---|
numactl --cpunodebind --membind | single process | per-invocation | ad-hoc benchmarks, one-off jobs |
taskset -c | CPU only | per-invocation | quick CPU pinning (no memory control) |
cgroup cpuset.cpus + cpuset.mems | process tree | persistent | containers, service groups |
systemd CPUAffinity + NUMAPolicy | unit | persistent | production services |
mbind/set_mempolicy in app | allocations | in-process | apps that know their hot data |
A systemd unit for a latency-sensitive service pinned to node 0:
[Service]
NUMAPolicy=bind
NUMAMask=0
CPUAffinity=0-15
For containers, set the cpuset controller so both CPUs and memory nodes are constrained together — constraining only CPUs forces remote allocations:
# Pin container to node 0's CPUs and node 0's memory
echo 0-15 > /sys/fs/cgroup/myapp/cpuset.cpus
echo 0 > /sys/fs/cgroup/myapp/cpuset.mems
Key rule: always bind CPUs and memory to the same node(s). Binding CPUs without binding memory is the most common self-inflicted NUMA wound.
Implementing CXL/DRAM Tiering Policies
CXL memory appears to Linux as a NUMA node with higher latency. The kernel’s memory tiering subsystem (5.12+, CONFIG_MEMORY_TIERING) treats DRAM as the fast tier and CXL as the slow tier, auto-demoting cold pages. Verify detection first:
numactl --hardware # CXL node shows up with larger distances
cat /sys/devices/system/node/node*/distance
Enable automatic balancing and demotion:
# Let NUMA balancing promote hot pages back to DRAM
echo 1 | sudo tee /sys/kernel/mm/numa/numa_balancing
# Allow demotion of cold pages to the slower tier
echo 1 | sudo tee /sys/kernel/mm/numa/demotion_enabled
# Tune how aggressively pages demote (kernel-version dependent)
cat /sys/kernel/mm/numa/demotion_enabled
flowchart LR
Hot[Hot pages] -->|promote| DRAM[(DRAM - fast tier)]
DRAM -->|demote cold| CXL[(CXL - slow tier)]
CXL -->|re-promote on access| DRAM
style DRAM fill:#1f6feb,color:#fff
style CXL fill:#57606a,color:#fff
Policy guidance:
- Latency-critical working set →
membindto DRAM node; never let it spill to CXL. - Large, read-mostly datasets → allow demotion so DRAM stays free for hot data.
- Watch the demotion rate with eBPF: excessive promote/demote churn means your tiers are the wrong size or your threshold is too aggressive.
Case Study: Reducing Cross-Node Latency on a 4-Socket Server
A self-hosted 4-socket EPYC server running a memory-bound analytics service showed p99 latency of 210 ms with 35% of cycles stalled on memory. Baseline eBPF showed ~12,000 misplaced-page migrations/sec — the service was thrashing across all four nodes.
Step 1 — Measure. Ran the numa_migrate_misplaced_page probe; migrations were concentrated on two hot hash tables.
Step 2 — Place. Bound the service to nodes 0–1 (its two hot tables were allocated there) via systemd NUMAPolicy=bind + NUMAMask=0-1, and constrained the cgroup’s cpuset.mems to match.
Step 3 — Tier. Enabled numa_balancing and demotion so cold scratch buffers spilled to a CXL node.
Step 4 — Re-measure. Migrations dropped to ~400/sec (residual, healthy). p99 fell to 118 ms (−44%) and memory-stall cycles dropped to 14%.
The lesson: the win came from stopping the thrash, not from adding hardware. eBPF found the thrash; placement stopped it; tiering kept DRAM free.
Conclusion and Best Practices
NUMA-aware scheduling and memory tiering are not “set and forget” — they’re a measure → place → tier → re-measure loop. Keep these rules:
- Observe first. Never bind or tier without eBPF evidence of where misses occur.
- Bind CPUs and memory together. Mismatched binding is worse than none.
- Prefer persistent mechanisms (systemd, cgroups) over one-off
numactlfor production. - Treat CXL as a tier, not a target. Pin latency-critical data to DRAM; let cold data demote.
- Re-validate after every change with the same probes and a repeatable benchmark.
- Watch for thrash. Decaying migrations = healthy; constant migrations = wrong policy.
FAQ
What is NUMA and why does it matter for self-hosted servers? NUMA is a memory architecture where each CPU socket has local memory. Local access is faster than remote; ignoring it on multi-socket servers causes significant cross-node latency and throughput loss. NUMA-aware placement keeps workloads close to their memory.
How does eBPF help observe NUMA locality misses?
eBPF probes attach to kernel tracepoints and kprobes like numa_migrate_misplaced_page and numa_pte_updates, letting you quantify migration frequency per process and node pair — turning “it feels slow” into a measured number you can tune against.
What are the key eBPF tools for tracing page migration?
bpftrace (fast one-liners), BCC (richer tooling like perf), and perf trace for syscall-level views. Use tracepoint numa_migrate_misplaced_page for balancer activity and sys_enter_migrate_pages for manual migrations.
How do I apply NUMA-aware CPU and memory placement?
Use numactl --cpunodebind --membind for ad-hoc runs, cgroup cpuset.cpus/cpuset.mems for containers, and systemd CPUAffinity/NUMAPolicy/NUMAMask for services. Always constrain CPUs and memory to the same node(s).
What is CXL and how does it enable memory tiering?
CXL is a high-speed interconnect that attaches memory expanders as a slower, larger tier. Linux’s memory-tiering subsystem promotes hot pages to DRAM and demotes cold pages to CXL, balancing performance and cost. Enable it via numa_balancing and demotion_enabled, and verify topology with numactl --hardware.