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.

Five questions this page answers
Question
1What are the three kinds of miss?
2Which two questions name a miss?
3Which kind do more lines cure, and which kind do more ways cure?
4Which kind does neither cure?
5Why 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

New words on this page
WordMeaning
Compulsory missA miss on the first read of a block. No cache could have held the block.
Capacity missA miss on a block that was pushed out because the cache has too few lines for the program.
Conflict missA miss on a block that was pushed out of its set, while the cache still had room elsewhere.
Shadow cacheOur name for a second cache that the lab keeps beside the real one. It has the same number of lines and is fully associative.
SweepA set of runs of one program, in which one size of the cache changes from run to run.

One Picture: Two Questions

Two Questions Name Every MissStep through the picture. It asks two questions about a miss, and each answer names a kind.
  • Processor
  • Control / FSM
  • Memory
Two Questions Name Every MissStep through the picture. It asks two questions about a miss, and each answer names a kind. component A miss the block is not in thecache component Read before? was this block everread component Compulsory miss the first read of theblock component Room anywhere? would it be here if anyline could hold it component Capacity miss the cache is too small component Conflict miss its set was too full first questionnoyesnoyes
  1. component A missthe block is not in the cache
    • first question → Read before?
  2. component Read before?was this block ever read
    • no → Compulsory miss
    • yes → Room anywhere?
  3. component Compulsory missthe first read of the block
  4. component Room anywhere?would it be here if any line could hold it
    • no → Capacity miss
    • yes → Conflict miss
  5. component Capacity missthe cache is too small
  6. component Conflict missits set was too full
  1. A miss

    The processor asks for a block, and the cache does not hold it. The question is why.

  2. Was it ever read?

    If this is the first read of the block, no cache could hold it. That is a compulsory miss.

  3. 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?

  4. No room: capacity

    If even that cache has lost the block, too many other blocks came in between. The cache is too small.

  5. 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.

Nine reads, each miss named
ReadLineThe line holdsRead before?Shadow cache beforeResult
block 00nothingnoemptycompulsory miss
block 40block 0no0compulsory miss
block 00block 4yes0, 4conflict miss
block 40block 0yes4, 0conflict miss
block 11nothingno0, 4compulsory miss
block 22nothingno0, 4, 1compulsory miss
block 33nothingno0, 4, 1, 2compulsory miss: the shadow drops block 0
block 00block 4yes4, 1, 2, 3capacity miss
block 11block 1yes1, 2, 3, 0hit

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

  1. Rule 1: The first read of a block is a compulsory miss.

    No size and no number of ways removes it.

  2. Rule 2: A miss that a fully associative cache of the same size shares is a capacity miss.

    Only more lines remove it.

  3. 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.

  4. Rule 4: Name the misses before you change the cache.

    A cure for the wrong kind costs area and buys nothing.

  5. 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.

Printed by the lab on Icarus. The first ten rows are the hand trace. The last ten are the loop, through five sizes with one way and with two.
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=0

Rules 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.

TransientMisses Against Size, for One Way and for TwoRecorded by the lab: the misses of one loop of 64 reads, through caches of five sizes.
  • One way
  • Two ways
010203040506070Misses out of 64 reads102030405060Lines in the cache020406080Misses out of 64 reads204060Lines in the cache
  1. 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.

  2. 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.

  3. 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 cacheOne wayTwo ways
86464
106452
166416
206416
306416
405216
503716
602216
641616

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 choiceThe alternativeWhat the choice buys, and costs
Name the misses by kindWatch the miss count aloneThe cure follows from the kind. It needs a model beside the cache.
More ways against conflictsMore lines against conflictsA few comparators do the work of four times the lines. The hit path gets longer.
More lines against capacityMore ways against capacityOnly lines add room. They cost bits, area and a slower look.
Accept the compulsory missesA cache that never forgetsNothing 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.

The same lab: the loop through 16 lines, through 32 lines, and through 16 lines as 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=0

The 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 page64 reads, 4 to 64 lines
A real studymillions of reads, thousands of lines

This section looks ahead. Real designs sort their misses the same way, on traces of millions of reads.

  1. 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.

  2. 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.

  3. Compulsory misses

    A cache can bring a block in before its first read. The next lesson shows how.

  4. 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.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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=0

Check Yourself

Predict the outputThe hand trace is 0, 4, 0, 4, 1, 2, 3, 0, 1. It now runs through 2 sets of 2 ways. What does the last row say?
// A counting model of one cache. A read names a word. The model counts hits, misses and cycles.
module tb;
  int LINES = 4;               // lines in the whole cache
  int WAYS  = 1;               // lines in one set
  int WORDS = 1;               // words in one block
  bit NEXT  = 0;               // 1: a miss also fetches the block after it (next-line prefetch)
  int SETS;
  localparam T_HIT = 1;        // cycles for a look in the cache
  localparam T_BASE = 8;       // a miss on top: 8 cycles to start the fetch ...
  localparam T_WORD = 2;       // ... and 2 more for each word of the block. All three are choices of this lab

  logic valid [0:63][0:15];    // the cache: [set][way]
  int   holds [0:63][0:15];
  int   used  [0:63][0:15];
  // Two helpers, only for naming a miss.
  bit   seen  [0:1023];        // every block that was ever read
  logic any_valid [0:63];      // a shadow cache: the same number of lines, but a block may use any of them
  int   any_holds [0:63], any_used [0:63];
  int now, reads, hits, misses, compulsory, capacity, conflict, cycles, prefetches;
  bit quiet = 1;

  task automatic start(input int lines, input int ways, input int words, input bit next);
    LINES = lines;  WAYS = ways;  WORDS = words;  NEXT = next;  SETS = LINES / WAYS;
    for (int s = 0; s < 64; s++) begin
      any_valid[s] = 0;
      for (int w = 0; w < 16; w++) valid[s][w] = 0;
    end
    for (int b = 0; b < 1024; b++) seen[b] = 0;
    now = 0;  reads = 0;  hits = 0;  misses = 0;  compulsory = 0;  capacity = 0;  conflict = 0;  cycles = 0;  prefetches = 0;
  endtask

  function automatic int way_of(input int block);        // the way that holds the block, or -1
    int set;
    set = block % SETS;
    way_of = -1;
    for (int w = 0; w < WAYS; w++) if (valid[set][w] && holds[set][w] == block) way_of = w;
  endfunction

  task automatic bring_in(input int block);              // an empty way first, or else the way used longest ago
    int set, victim;
    set = block % SETS;  victim = -1;
    for (int w = WAYS - 1; w >= 0; w--) if (!valid[set][w]) victim = w;
    if (victim < 0) begin
      victim = 0;
      for (int w = 1; w < WAYS; w++) if (used[set][w] < used[set][victim]) victim = w;
    end
    valid[set][victim] = 1;  holds[set][victim] = block;  used[set][victim] = now;
  endtask

  function automatic bit in_shadow(input int block);     // was the block in the shadow cache? Then use it there too
    int at, victim;
    at = -1;  victim = -1;
    for (int i = 0; i < LINES; i++) if (any_valid[i] && any_holds[i] == block) at = i;
    in_shadow = (at >= 0);
    if (at < 0) begin
      for (int i = LINES - 1; i >= 0; i--) if (!any_valid[i]) victim = i;
      if (victim < 0) begin
        victim = 0;
        for (int i = 1; i < LINES; i++) if (any_used[i] < any_used[victim]) victim = i;
      end
      at = victim;  any_valid[at] = 1;  any_holds[at] = block;
    end
    any_used[at] = now;
  endfunction

  task automatic read(input int word);
    int block, way;
    bit shadow_had_it;
    string kind;
    block = word / WORDS;
    now++;  reads++;
    shadow_had_it = in_shadow(block);
    way = way_of(block);
    if (way >= 0) begin
      hits++;  cycles += T_HIT;  kind = "hit";
      used[block % SETS][way] = now;
    end else begin
      misses++;  cycles += T_HIT + T_BASE + T_WORD * WORDS;
      if (!seen[block])        begin compulsory++;  kind = "miss, compulsory: the first read of this block"; end
      else if (!shadow_had_it) begin capacity++;    kind = "miss, capacity: the shadow cache lost it too"; end
      else                     begin conflict++;    kind = "miss, conflict: the shadow cache still had it"; end
      bring_in(block);
      if (NEXT && way_of(block + 1) < 0) begin
        prefetches++;  bring_in(block + 1);
      end
    end
    seen[block] = 1;
    if (!quiet) $display("read word %2d: block %2d, set %0d | %s", word, block, block % SETS, kind);
  endtask

  task automatic kinds;        // one row: the misses by kind
    $display("lines=%0d ways=%0d: reads=%0d hits=%0d misses=%0d | compulsory=%0d capacity=%0d conflict=%0d", LINES, WAYS, reads, hits, misses, compulsory, capacity, conflict);
  endtask
  task automatic costs;        // one row: the misses and the cycles
    $display("words=%0d lines=%0d next=%0d: reads=%0d misses=%0d cycles=%0d", WORDS, LINES, NEXT, reads, misses, cycles);
  endtask

  task automatic program_run;  // a loop that reads a[i] and b[i] in turn. a starts at word 0, b at word 32
    for (int pass = 0; pass < 4; pass++)
      for (int i = 0; i < 8; i++) begin read(i); read(32 + i); end
  endtask

  initial begin
    start(4, 2, 1, 0);  quiet = 0;
    read(0); read(4); read(0); read(4); read(1); read(2); read(3); read(0); read(1);
    kinds;
  end
endmodule
ConceptWhich miss does no cache avoid, whatever its size and shape?
ConceptA program moves through 100 blocks again and again. The cache has 32 lines. Which kind of miss dominates after the first round?
ConceptA sweep shows capacity 0 and conflict 48. What is the cheapest cure?
ConceptHow does the lab tell a capacity miss from a conflict miss?
ConceptA direct-mapped cache of 16 lines has only conflict misses on a loop. It grows to 32 lines, and nothing changes. Why?

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.

Practice

Report a problem with this page