Sub topic 6
Compulsory, capacity, and conflict misses
This chapter assumes you know what a cache line is and how spatial and temporal locality work. It also assumes you can read basic C. A cache miss happens when the CPU asks for data that is not in the cache. Not all misses have the same cause, and the cause tells you which fix to try.
The three kinds of miss
| Miss type | Cause | Typical fix |
|---|---|---|
| Compulsory | First access to a line. It was never in the cache. | Prefetching, or reading memory sequentially |
| Capacity | The working set is larger than the cache, so useful lines are evicted before reuse. | Shrink the data, or reuse it sooner (blocking) |
| Conflict | Too many lines map to the same cache set, so they evict each other even though the cache has free space elsewhere. | Change strides, padding, or layout |
A compulsory miss is also called a cold miss. You cannot avoid it, but you can hide its cost, because sequential access lets the hardware prefetcher fetch lines early.
A capacity miss depends on the working set, which is the data a loop touches repeatedly. If a loop makes many passes over 64 MB of data and the cache holds 8 MB, each pass evicts the lines the next pass needs.
A conflict miss comes from how the cache places lines. In a set-associative cache, each address maps to one set, and each set holds a small number of lines called ways. An 8-way cache keeps at most 8 lines per set. Ask for a ninth line that maps to the same set, and one of the others is evicted.
Spotting conflict misses
Addresses that are a large power of two apart often share a set. This code reads a column of a matrix whose row length is 4096 doubles, so each row is exactly 32768 bytes apart.
#include <stddef.h>
#define ROWS 64
#define COLS 4096
/* Rows are 32768 bytes apart, so these accesses tend to land in the same set. */
double sum_column(const double (*m)[COLS], size_t col) {
double s = 0.0;
for (size_t i = 0; i < ROWS; i++)
s += m[i][col];
return s;
}Only 64 lines are touched, which is far below the capacity of any modern cache. Yet if many of them map to one set, a column sum run repeatedly can still miss on every access. If the number of rows exceeds the ways, the rows evict each other. Changing COLS to 4104 (adding 8 doubles of padding) spreads the rows across sets.
Telling them apart
Ask these questions in order:
- Does the miss happen only on the first touch of each line? It is compulsory.
- Does the miss rate drop when you shrink the data set? It is capacity.
- Does a small data set still miss badly, and does the rate change when you alter a stride slightly? It is a conflict.
Exercise
A loop sums a 2 KB array 1,000,000 times on a CPU with a 32 KB L1 cache. The miss rate is still high, and changing the array length from 512 to 520 elements lowers it sharply. Which miss type is the likely cause, and why not the other two?
Show answer
Conflict misses. The array fits in the cache, so it is not a capacity problem. Compulsory misses happen only on the first pass and would be negligible over a million passes. A small change in length that alters the miss rate points to set mapping.