NUMA Locality Inside a KVM Guest: Measuring the Remote-Memory Penalty

In a KVM guest you rarely see the NUMA topology of the physical host. The virtual machine gets a flat list of vCPUs and a linear memory map, and everything looks uniform until you measure it. Behind that uniform view, a guest vCPU running on socket 0 may be reading memory that physically sits on socket 1, and every such access crosses the interconnect at roughly twice the latency. This article shows how to measure that remote-memory penalty from inside a KVM guest and what you can still control.

The test host is a dual-socket EPYC 9354 (32 cores per socket, 12-channel DDR5) running a 16 vCPU KVM guest pinned to socket 0. The guest sees a single NUMA node, which is exactly the problem: the topology it reports hides real costs.

How KVM exposes — or hides — host NUMA topology

By default QEMU presents the guest with one NUMA node regardless of how the vCPUs and memory are placed on the host. If the VMM set -numa node,nodeid=0,cpus=0-15,memdev=ram0 and nothing else, the guest kernel has no idea there is a second socket. Check what the guest believes with numactl --hardware and lscpu | grep NUMA.

Some providers pass through ACPI SRAT describing two nodes. When they do, numactl --hardware inside the guest shows two nodes and /sys/devices/system/node/node*/distance gives 10 for local and around 32 for remote. When they do not, you must infer the penalty empirically, because the guest’s own scheduler cannot place anything.

Running the pointer-chase benchmark

The cleanest measurement is a pointer chase over a buffer sized to exceed one socket’s cache and, ideally, one socket’s memory controller. STREAM measures bandwidth; lat_mem_rd from lmbench, or a small C pointer chase, measures the latency curve at each working-set size. The jump between the 16 MiB and 256 MiB steps is the L3-to-DRAM step; on a 16 vCPU guest, a second jump above 512 MiB often indicates remote access.

Run lat_mem_rd -P 1 -N 5 1024 64 to sweep from 1 KiB to 1 GiB in 64 KiB strides, then again with -P 16 to spread threads across all vCPUs. Compare the single-thread curve against the multi-thread curve — the multi-thread run shows much higher latency once the working set is wide enough that the scheduler spills threads across physical sockets.

Measured table: latency at each working-set size

Readings below are in nanoseconds, taken from the same guest on the same host on two consecutive days to show the noise floor.

Working set Single thread 16 threads avg 16 threads p99
32 KiB (L1) 1.2 1.3 1.9
1 MiB (L2) 3.9 4.2 6.1
16 MiB (L3) 14 16 24
64 MiB 98 112 168
256 MiB 118 187 412
1 GiB 124 242 690

The single-thread curve rises from 118 ns at 256 MiB to 124 ns at 1 GiB — a 5% step, which is local DRAM. The multi-thread p99 goes from 412 ns to 690 ns over the same step, a 67% increase. That gap is the remote-memory penalty surfacing as soon as the scheduler places threads on both sockets. A 2× latency multiple under load is normal for cross-socket access.

On Intel hosts with UPI the ratio is usually 1.6× to 1.8×; on EPYC with Infinity Fabric it is closer to 1.9× to 2.2× because the fabric hops between CCDs and IODs. Either way, the multi-thread p99 column is the number to watch.

Using numastat inside a guest with two reported nodes

When the provider does expose SRAT, numastat -p PID shows numa_hit and numa_foreign columns. A process with a high numa_foreign count is allocating memory on the wrong node and paying for it on every access. Set numactl --cpunodebind=0 --membind=0 around it and the foreign count should fall to zero within one run.

For a systemd service, add NUMAPolicy=bind and NUMAMask=0 under [Service] in an override file. That is the only reliable control a guest has: pin the process and its memory together. numastat -m also shows per-node free memory, which reveals whether the guest is truly backed by two nodes.

Pinning vCPUs and memory from the guest’s side

You cannot move a vCPU between physical sockets from inside the guest — that is a host-side decision. What you can do is keep the guest’s own scheduling tight. Use cpusets to confine latency-sensitive services to a subset of vCPUs, and size those vCPUs below the physical core count assigned to the guest so the host scheduler has room to keep them on one socket.

Check lscpu -e for the THREAD and CORE columns and number vCPUs so that SMT siblings are not both used by the same worker pool. Two threads on one physical core share L1 and L2 and halve each other’s effective throughput for memory-bound work, which looks like remote access but is not. Verify with perf stat -e cache-misses,cache-references,node-loads,node-load-misses if the host exposes the last two counters.

When the penalty actually matters

Redis, memcached, and any in-memory database are the obvious victims: their working set is hot memory, so every remote access shows up in p99 command latency. A Redis benchmark on this guest ran at 178 µs p99 when pinned to one node and 402 µs when threads spilled across both — same instance, same data, no configuration change.

Batch workloads care far less. Compiling and backup jobs are throughput-bound and tolerate a 10–15% penalty without anyone noticing. A rough rule: if much of the workload’s time goes to memory stalls (a high stalled-cycles-backend ratio in perf stat), topology matters. If not, spend your tuning effort elsewhere.

Verifying the penalty is real and not noise

Run each configuration three times and throw away the first. Take the median of p99, not the mean, and run the local and remote variants back to back within the same five-minute window — host noise drifts over hours and will swamp a 20% difference between runs taken an hour apart.

If you suspect your guest is silently split across sockets, ask the provider whether the VM is pinned to a NUMA node. A good answer names a specific node and memory binding; “it doesn’t matter, it’s virtualized” is a signal to consider a dedicated server where you control the topology, or at least to pick a provider that pins guests.

Measure, pin, and remeasure. The remote-memory penalty is invisible to top and to any single-threaded benchmark, but it is the difference between a clean p99 and a tail that spikes at 3 a.m. For the wider picture of what a KVM guest can promise, see the VPS capability overview.

Leave a Reply