AlgoViz

Caches, NUMA and memory locality

medium 7 min

A cache line is the unit moved between CPU caches.

Video lesson · Chapter 1 of 8

A worker moves away from its data

0:00 / 5:36

Theory

Read or listen first. About 2 min.

What it is

A cache line is the unit moved between CPU caches. Two cores that write different counters on the same line can repeatedly invalidate each other’s copy. At a larger scale, NUMA makes the cost of a memory access depend on where a page lives relative to its CPU. Adding threads can multiply both effects.

Why it exists

After a placement change a worker spends longer waiting on loads although total CPU use looks similar. Compare local and remote allocations alongside the CPUs where the threads actually run. Moving a thread leaves its already allocated pages behind. Changing placement before the hot pages are first touched can keep both together.

If page placement is local, look for false sharing and memory bandwidth saturation before moving the process again.

A worker moves away from its data

Worker core → Memory controller (access memory). Memory controller → Remote NUMA node (route remote load)
Worker core → Memory controller (access memory). Memory controller → Remote NUMA node (route remote load)

Inside this chapter

CPU Cache L1/L2/L3 hierarchy and latency
Private and shared caches reduce average access latency when a working set has locality. Once a workload outgrows a level, the miss penalty can dominate instruction count.
NUMA (Non-Uniform Memory Access) architecture
NUMA nodes attach memory closer to some CPUs. First-touch allocation and later thread migration can separate a worker from its data. Measure the actual placement.
False Sharing and Cache Coherence
Independent counters on one cache line can force coherence traffic among writing cores. Padding or per-core aggregation trades a larger memory footprint for fewer invalidations.
Huge Pages usage
Larger pages extend TLB coverage. They can also waste space or introduce allocation and compaction delays. Compare tail behavior and translation misses under the intended policy.
Memory Bandwidth and Prefetching
Sequential streams can benefit from prefetching, but simultaneous streams may saturate memory bandwidth. Extra threads can then increase contention without increasing useful throughput.
Memory Latency Numbers Every Programmer Should Know
A cache hit, remote memory access and storage read operate at different scales. Measure representative hardware and access patterns before using a memorized latency to size a service.

How it works

Step through the idea, or press Play.

Step 1 of 4

Place two counters. Two independent counters happen to occupy the same cache line.

Understand it

Worked incident

A worker moves away from its data

ObservationRemote accesses per 100 samples (accesses)Interpretation
Baseline5The worker runs close to the pages it allocated.
Incident71Thread migration leaves most sampled accesses remote.
After intervention9Coordinate CPU and first-touch placement, then remeasure using an equal-sized working set.
Illustrative measurements from the same workload at three checkpoints.

Choose the next investigation

If page placement is local, look for false sharing and memory bandwidth saturation before moving the process again.

Summary

The whole topic in one minute.

Thread affinity controls where a thread may run. Migration or new allocations under the intended memory policy determine where its pages live.

If page placement is local, look for false sharing and memory bandwidth saturation before moving the process again.

Sources