Cache misses, with real numbers
Video 022 of 140 in Virtualization Foundations, in the unit “CPUs: sockets, cores, threads and caches”. 3:42, with English and Hindi audio.
A cache miss happens when the data a core wants is not in the cache, so the core must fetch it from a slower level. The cost of one miss is tiny, measured in nanoseconds. But a program makes millions of reads, and the costs add up. The same code can run many times slower or faster, depending only on where its data sits. This article puts rough numbers on that cost and shows how to count misses on Linux.
What you will learn
- A read that hits L1 takes about one nanosecond. A read that goes out to RAM takes about one hundred nanoseconds.
- A loop of a million reads can take about one millisecond or about one hundred milliseconds, depending on where the data sits.
- A slow job is often waiting on memory, not short of processor power.
- The perf tool can count cache misses while a command runs.
What one memory read costs at each level
Take one memory read. A nanosecond is one billionth of a second, so the numbers are tiny, yet they add up. If the value sits in L1, the nearest cache, the answer takes about one nanosecond.
If the value misses L1 but is in L3, the answer takes roughly ten to twenty nanoseconds. If it misses every cache, RAM answers in roughly one hundred nanoseconds. That is around a hundred times slower than L1.
Note: These figures are typical values, not measurements from one chip. Use them as a rough guide to the size of each step.
How a million reads add up
Now take a loop that reads one million values, one after another. If every value is in L1, the total is about one millisecond, a thousandth of a second. If every value is in RAM, the same loop takes about one hundred milliseconds. It is the same code, but it runs a hundred times longer.
Real chips overlap some of these waits, so the totals are a rough guide, not a benchmark. The main point still holds: where the data lives decides how long the loop takes.
The path one read takes through the caches
- The core looks in L1, the nearest cache, inside the core.
- On a miss, it checks L2, then L3, each a little further away.
- If all of them miss, the request goes out to RAM, which is far slower.
- When the value arrives, it is copied into the caches. The next read of the same value is a hit, found quickly.
A real case shows how this plays out. Harbor Point Logistics runs an ERP database, the business system that holds orders and stock. It keeps its frequently used rows close, in cache and in RAM. Then a large month-end report reads far more data than the caches can hold, and the useful rows are pushed out. Afterwards the same query runs slowly, and people blame the disk first. Often the real cause is lost cache.
How to count cache misses with perf on Linux
On Linux, a tool called perf can count cache misses while a command runs. Counting turns a vague feeling of slowness into a number you can compare. The command below watches the ls command and discards its normal output.
perf stat -e cache-misses ls > /dev/nullNote: perf may need its package installed before this command works.
A common mistake is to blame the CPU for a slow job, when the core is really waiting on memory. The working set is the data a program is using right now. If it is bigger than the caches, misses pile up. High CPU use with little useful progress is the usual sign.
Terms
- Cache miss
- A request for data that is not in the cache, so the core must wait for a slower level of memory.
- Nanosecond
- One billionth of a second, the unit in which cache and memory delays are measured.
- Working set
- The data a program is using right now. If it is bigger than the caches, misses pile up.
- perf
- A Linux tool that counts events, such as cache misses, while a command runs.
Check yourself
About how much slower is a read from RAM than a read from L1?
Roughly a hundred times slower, about one hundred nanoseconds against about one nanosecond.
Why can the same code run a hundred times slower?
Its data may sit in RAM instead of L1, so each read waits much longer, and a loop of many reads adds up to a large difference.
Why might a slow job not be short of CPU power?
The core may be waiting on memory after cache misses, so it looks busy but makes little useful progress.
Previous: CPU caches: small, fast memory kept close. Next: Pipelines and branch prediction, in brief.