Cache Performance: The Three Kinds of Miss
A miss count says that a cache is losing. It does not say why. Name each miss by its cause, and the cure follows from the name.
You Will Be Able To
- Name a miss as compulsory, capacity or conflict, with two questions.
- Say which kind more lines cure, and which kind more ways cure.
- Read a sweep of sizes and ways, and choose the cheaper cure.
- Lesson
- S3.6
- Time
- 25 min
- Runs on
- Your browser
- Check
- 6 questions
A loop misses on three reads out of four. The team doubles the cache, and the misses do not move. They double it again. Nothing. The cache was never too small.
Before You Start
You need S3.4: sets, ways, and the fully associative cache. You need S3.16: a full set gives up the block that was used longest ago.
You also need the hit, the miss and the block from S3.2.
| Question | |
|---|---|
| 1 | What are the three kinds of miss? |
| 2 | Which two questions name a miss? |
| 3 | Which kind do more lines cure, and which kind do more ways cure? |
| 4 | Which kind does neither cure? |
| 5 | Why is a bigger cache the wrong answer to a conflict miss? |
The Idea: Every Miss Has a Cause
A cache misses for one of three reasons. The reasons are different, and so are the cures. A team that does not know the reason buys the wrong cure.
The first reason: the block was never read before. No cache can hold a block that it has never seen. This is a compulsory miss. A bigger cache does not help, and more ways do not help.
The second reason: the cache is too small for the job. The program moves through more blocks than the cache has lines. Some block must go, and later it is wanted again. This is a capacity miss. Only more lines help.
The third reason: the cache had room, but not in the right set. Too many blocks share one index, while other sets stand empty. This is a conflict miss. More ways help. So do more sets, sometimes.
How do we tell the second from the third? We ask what a fully associative cache of the same size would do. In that cache any line may hold any block, so no block can lose a fight over a set. If it also lost the block, the cache is too small. If it kept the block, the miss is a conflict.
New Words
| Word | Meaning |
|---|---|
| Compulsory miss | A miss on the first read of a block. No cache could have held the block. |
| Capacity miss | A miss on a block that was pushed out because the cache has too few lines for the program. |
| Conflict miss | A miss on a block that was pushed out of its set, while the cache still had room elsewhere. |
| Shadow cache | Our name for a second cache that the lab keeps beside the real one. It has the same number of lines and is fully associative. |
| Sweep | A set of runs of one program, in which one size of the cache changes from run to run. |
One Picture: Two Questions
- Processor
- Control / FSM
- Memory
- A miss
The processor asks for a block, and the cache does not hold it. The question is why.
- Was it ever read?
If this is the first read of the block, no cache could hold it. That is a compulsory miss.
- Was there room anywhere?
Picture a cache with the same number of lines, where any line may hold any block. Would the block still be there?
- No room: capacity
If even that cache has lost the block, too many other blocks came in between. The cache is too small.
- Room, but not in its set: conflict
If that cache still has the block, there was room. The block lost a fight over its own set.
Read the diagram as text
- A miss: the block is not in the cache
- Read before?: was this block ever read
- Compulsory miss: the first read of the block
- Room anywhere?: would it be here if any line could hold it
- Capacity miss: the cache is too small
- Conflict miss: its set was too full
- A miss → Read before?: first question
- Read before? → Compulsory miss: no
- Read before? → Room anywhere?: yes
- Room anywhere? → Capacity miss: no
- Room anywhere? → Conflict miss: yes
Two questions, three kinds. Each kind has its own cure.
The Smallest Example, Worked by Hand
Take a direct-mapped cache of four lines. A block is one word, so a read names its block. The line of a block is the remainder of its number divided by 4.
Beside it, keep the shadow cache on paper: four lines, and any line may hold any block. When it is full, it gives up the block that was used longest ago.
The trace has nine reads: 0, 4, 0, 4, 1, 2, 3, 0, 1.
| Read | Line | The line holds | Read before? | Shadow cache before | Result |
|---|---|---|---|---|---|
| block 0 | 0 | nothing | no | empty | compulsory miss |
| block 4 | 0 | block 0 | no | 0 | compulsory miss |
| block 0 | 0 | block 4 | yes | 0, 4 | conflict miss |
| block 4 | 0 | block 0 | yes | 4, 0 | conflict miss |
| block 1 | 1 | nothing | no | 0, 4 | compulsory miss |
| block 2 | 2 | nothing | no | 0, 4, 1 | compulsory miss |
| block 3 | 3 | nothing | no | 0, 4, 1, 2 | compulsory miss: the shadow drops block 0 |
| block 0 | 0 | block 4 | yes | 4, 1, 2, 3 | capacity miss |
| block 1 | 1 | block 1 | yes | 1, 2, 3, 0 | hit |
The shadow column lists its blocks, with the one used longest ago first.
Look at the third read. Block 0 was read before, and the shadow cache still holds it. Two blocks fit easily into four lines. The real cache lost block 0 only because block 4 wanted the same line. That is a conflict.
Now the eighth read. Block 0 was read before, but this time the shadow cache has lost it too. Five different blocks came through four lines. That is a capacity miss.
The count: 5 compulsory, 1 capacity, 2 conflict, and 1 hit.
The Rules
Rule 1: The first read of a block is a compulsory miss.
No size and no number of ways removes it.
Rule 2: A miss that a fully associative cache of the same size shares is a capacity miss.
Only more lines remove it.
Rule 3: A miss that such a cache would not have is a conflict miss.
More ways remove it. More sets remove it only if the blocks then get different sets.
Rule 4: Name the misses before you change the cache.
A cure for the wrong kind costs area and buys nothing.
Rule 5: Every cure has a price.
More lines cost bits and a slower look. More ways cost comparators and a longer hit path.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.8. See "The Three Cs: An Intuitive Model for Understanding the Behavior of Memory Hierarchies": the three kinds of miss and what changes each.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4: compulsory, capacity and conflict misses, and miss rate against cache size and associativity.
The shadow cache is our way to apply the textbook test. It replaces the block used longest ago, which is one choice among several.
The four lines and both traces are our own teaching examples. The chip of this course has no cache, so no trace of it is used.
Now Run It
The lab at the end of this page is a counting model in test code. It holds block numbers and no data. Beside the cache it keeps the shadow cache, and a note of every block ever read.
First it runs the hand trace with a row for each read. Then it runs a loop through ten caches. The loop reads two arrays of eight words in step, four times round. One array starts at word 0, the other at word 32.
read word 0: block 0, set 0 | miss, compulsory: the first read of this block
read word 4: block 4, set 0 | miss, compulsory: the first read of this block
read word 0: block 0, set 0 | miss, conflict: the shadow cache still had it
read word 4: block 4, set 0 | miss, conflict: the shadow cache still had it
read word 1: block 1, set 1 | miss, compulsory: the first read of this block
read word 2: block 2, set 2 | miss, compulsory: the first read of this block
read word 3: block 3, set 3 | miss, compulsory: the first read of this block
read word 0: block 0, set 0 | miss, capacity: the shadow cache lost it too
read word 1: block 1, set 1 | hit
lines=4 ways=1: reads=9 hits=1 misses=8 | compulsory=5 capacity=1 conflict=2
lines=4 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0
lines=8 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0
lines=16 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48
lines=32 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48
lines=64 ways=1: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0
lines=4 ways=2: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0
lines=8 ways=2: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0
lines=16 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0
lines=32 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0
lines=64 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0Rules 1 to 3 at work: the first nine rows match the hand table, kind for kind.
Now the loop. It uses 16 blocks, so 16 misses are compulsory in every row. The other 48 reads could all hit.
- One way
- Two ways
- Lines in the cache 8One way = 64 · Two ways = 64
Eight lines. The loop uses 16 blocks, so no shape of this cache can hold them: capacity misses.
- Lines in the cache 16One way = 64 · Two ways = 16
Sixteen lines. Two ways now hold everything. One way still misses every read, and these are conflict misses.
- Lines in the cache 64One way = 16 · Two ways = 16
Sixty-four lines. Only now does one way stop the fight. The 16 misses that stay are compulsory.
Read the plot as a table
| Lines in the cache | One way | Two ways |
|---|---|---|
| 8 | 64 | 64 |
| 10 | 64 | 52 |
| 16 | 64 | 16 |
| 20 | 64 | 16 |
| 30 | 64 | 16 |
| 40 | 52 | 16 |
| 50 | 37 | 16 |
| 60 | 22 | 16 |
| 64 | 16 | 16 |
Recorded by the lab. The lines join five measured points each. Both curves end at 16, the compulsory misses.
Rule 2 at work: with 4 or 8 lines, the other 48 misses are capacity misses. Sixteen blocks do not fit into eight lines, whatever the shape.
Rule 3 at work: with 16 or 32 lines and one way, all 48 are conflict misses. Word 0 and word 32 share a line, and so does every pair after them. Two ways end the fight at 16 lines.
One way needs 64 lines for the same result. Only then do word 0 and word 32 get different lines.
This lab leaves out the data, the time of a miss, and blocks of more than one word. The next page adds those.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Name the misses by kind | Watch the miss count alone | The cure follows from the kind. It needs a model beside the cache. |
| More ways against conflicts | More lines against conflicts | A few comparators do the work of four times the lines. The hit path gets longer. |
| More lines against capacity | More ways against capacity | Only lines add room. They cost bits, area and a slower look. |
| Accept the compulsory misses | A cache that never forgets | Nothing in this page removes them. The next page shows two ways to do it. |
The Trap: A Bigger Cache for a Conflict
This is the team from the top of the page. Their cache has 16 lines and one way, and the loop misses on every read. They double it to 32 lines. Then somebody tries two ways.
lines=16 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48
lines=32 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48
lines=16 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0The second row is the failure. Twice the lines, and not one miss fewer. Words 0 and 32 share a line in a cache of 16 lines. In a cache of 32 lines they still do.
The third row costs one more comparator and one tag bit for each line. It removes all 48 misses. The first row already said so: capacity 0, conflict 48.
At Scale
This section looks ahead. Real designs sort their misses the same way, on traces of millions of reads.
Sweeps before silicon
A team runs traces of real programs through a model of the cache, size by size, long before the cache is built.
The three kinds blur
On a real program a change of size also changes which misses are conflicts. The names are a guide, not an exact account.
Compulsory misses
A cache can bring a block in before its first read. The next lesson shows how.
Several levels
A large chip answers capacity misses with more levels of cache: The Cache Hierarchy of a Core Cluster.
Try It
Run the lab. Check the first nine rows against the hand table.
Change the first start(4, 1, 1, 0) to start(4, 4, 1, 0). Which kind of miss must vanish? Run it.
In program_run, move the second array from word 32 to word 40. Predict the rows for 16 and 32 lines with one way.
read word 0: block 0, set 0 | miss, compulsory: the first read of this block read word 4: block 4, set 0 | miss, compulsory: the first read of this block read word 0: block 0, set 0 | miss, conflict: the shadow cache still had it read word 4: block 4, set 0 | miss, conflict: the shadow cache still had it read word 1: block 1, set 1 | miss, compulsory: the first read of this block read word 2: block 2, set 2 | miss, compulsory: the first read of this block read word 3: block 3, set 3 | miss, compulsory: the first read of this block read word 0: block 0, set 0 | miss, capacity: the shadow cache lost it too read word 1: block 1, set 1 | hit lines=4 ways=1: reads=9 hits=1 misses=8 | compulsory=5 capacity=1 conflict=2 lines=4 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0 lines=8 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0 lines=16 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48 lines=32 ways=1: reads=64 hits=0 misses=64 | compulsory=16 capacity=0 conflict=48 lines=64 ways=1: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0 lines=4 ways=2: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0 lines=8 ways=2: reads=64 hits=0 misses=64 | compulsory=16 capacity=48 conflict=0 lines=16 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0 lines=32 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0 lines=64 ways=2: reads=64 hits=48 misses=16 | compulsory=16 capacity=0 conflict=0Check Yourself
Common Mistakes
Wrong Double the cache whenever the miss count is high.
More lines remove capacity misses only. Conflict and compulsory misses stay.
Fix Name the misses first, and buy the cure for the largest kind.
Wrong Call every repeated miss a capacity miss.
A block can be pushed out of a full set while the rest of the cache is empty.
Fix Ask whether a fully associative cache of the same size would have kept it.
Wrong Expect more ways to help a first read.
A compulsory miss is on a block that no cache has seen.
Fix Count the different blocks: that many misses stay.
Remember
- Compulsory: the first read of a block. Capacity: too few lines. Conflict: a set too full.
- A fully associative cache of the same size tells capacity from conflict.
- More lines cure capacity misses. More ways cure conflict misses.
- Name the misses before you pay for a cure.