liveBook · Manning

Cache trashing

As explained in Latency

CPU cache thrashing is repeated eviction and reloading of useful cache data caused by competing or changing working sets.

Cache thrashing is the repeated eviction and reloading of useful CPU-cache data when competing or changing working sets prevent reuse. The processor keeps fetching data from memory instead of benefiting from recently loaded cache lines. section 9.2.1 section 7.4.5

Thread context switches can trigger thrashing because each thread brings a different working set. The direct switch may take hundreds of nanoseconds, but cache disruption can raise the effective cost to a microsecond or more and dominate execution when many threads are managed. section 9.2.1

Reduce cache thrashing by aligning data access with cache capacity and locality:

  • Process large datasets in batches small enough to remain useful in CPU caches rather than repeatedly scanning the entire dataset. section 7.4.5
  • Measure the working-set size and access pattern before choosing a memory strategy; data that fits in L1 can run far faster than data served from DRAM. section 7.4.5
  • For NUMA systems, place a CPU’s working set on nearby physical memory; Linux mbind() and topology tools such as hwloc support this placement. section 7.4.5
  • Prefer user-space concurrency or hardware parallelism when excessive kernel-thread switching makes cache disruption dominate useful execution. section 9.2.1
Figure 9.2 The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context-switching between them. In this example, every thread is run for exactly 4 milliseconds, and then the OS switches to another thread. In real-world systems, a thread might block to wait for I/O, for example, causing the OS to switch to another thread more quickly.
Figure 9.2: The OS runs multiple threads at the same time (but not simultaneously) on a single CPU core by context switching between then.

Use the context-switching timeline to visualize how one CPU core alternates between threads and repeatedly changes its active working set. section 9.2.1

How Latency covers it

How deep it goes
Defines eviction and reload behavior, connects it to working sets and latency, and stops short of detailed cache-hardware internals.
Examples
Uses alternating threads, repeated large-dataset scans, cache-sized batches, mbind(), and hwloc as concrete examples.
What it assumes
Assumes familiarity with threads and memory access while introducing working sets, cache levels, DRAM latency, and NUMA locality.
What you can do after
Apply it via cache-sized batching, working-set analysis, NUMA-aware placement, and reduced kernel-thread switching.
How it connects
Connects cache thrashing to context switching, working sets, CPU caches, DRAM, NUMA, memory topology, fibers, and GPUs.

This explainer was generated by Manning's knowledge graph from the text of Latency. The section links point to the parts of the book it draws on.