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

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

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.

Count cache misses while the ls command runs
perf stat -e cache-misses ls > /dev/null

Note: 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.