The Cache Hierarchy of a Core Cluster

A large chip puts three caches between a core and its memory. Two are private to the core, and one is shared by the cluster. Where a read stops decides what it costs.

You Will Be Able To

  • Follow a read through three cache levels, and say where it stops and what it pays.
  • Tell a private cache from a shared one, and say what a job finds after it moves to another core.
  • Work out the mean time of a read from the count of reads that reach each level.
Lesson
S10.2
Time
25 min
Runs on
Your browser
Check
5 questions

A video job runs smoothly on one core of the phone. The system moves it to a second core, and the next frame takes longer.

No byte of memory moved, and the two cores are alike. The time went into caches that the job left behind.

Before You Start

You need Memory and Caches in One Picture: a cache, a line, a hit, a miss and the tag.

You need Cache Coherence: MSI and MESI: a block, and one cache for each core. This page only reads, so no copy ever goes stale.

You need From Microcontroller to Application Processor: ap_phone and its core cluster.

Five questions this page answers
Question
1Why does a chip use several cache levels in place of one large cache?
2A read misses in the first two levels and hits in the third. What does it pay?
3A job moves to another core. Which cache can still help it?
4Of 5 reads, 2 reach memory. Why is the miss rate of the last level not 2 in 5?
5Two cores read blocks 8 and 9 in a new order. What does the lab print last?

The Idea: A Small Fast Cache in Front of a Large Slow One

One cache faces a choice that it cannot win. A small cache answers fast and misses often. A large cache misses seldom and answers slowly, as every large memory does.

So a large chip uses both, one behind the other. The core asks a small cache first. Only on a miss does the read go on to a larger one. If that one misses too, the read goes on again. The caches form levels. Counted from the core outwards, their names are L1, L2, L3.

A read stops at the first level that holds the block, and it pays for every level it reached. A hit in L1 costs one short lookup. A read that goes all the way pays three lookups and the memory as well.

With several cores the levels are split in a second way. L1 and L2 belong to one core: each is a private cache. L3 serves every core of the cluster: it is the shared cache. A private cache is close, and no other core fills it. The shared one is larger and further away, and it holds what any core has fetched.

That split explains the slow frame. A job that moves finds private caches that have never seen its data. Only the shared level still remembers.

New Words

New words on this page
WordMeaning
Cache levelOne cache in a row of caches between a core and memory. A read tries them in order.
L1, L2, L3The levels, counted from the core. L1 is the smallest and the fastest.
Private cacheA cache that serves one core only. Here L1 and L2.
Shared cacheA cache that serves every core of a cluster. Here L3, the last level before memory.
Lookup costThe time that one level takes to say hit or miss.
MigrationA running job is stopped on one core and goes on at another.
Cold cacheA cache that holds none of the blocks a job uses. A warm cache holds them already.
Local miss rateThe misses at one level, divided by the reads that reached that level.
Global miss rateThe reads that reach memory, divided by all reads.

One Picture: Three Levels, Two of Them Private

Three Levels, Two of Them PrivateStep through the picture. It starts with one cache, adds two more behind it, and then adds a second core.
  • Processor
  • Memory
Three Levels, Two of Them PrivateStep through the picture. It starts with one cache, adds two more behind it, and then adds a second core. component Core 0 runs the job first component Core 1 gets the job later component L1 of core 0 private: 1 line, 1 tick component L1 of core 1 private: 1 line, 1 tick component L2 of core 0 private: 2 lines, 3ticks component L2 of core 1 private: 2 lines, 3ticks component L3 shared by both cores:4 lines, 6 ticks component Memory holds every block: 20ticks readon a misson a misson a missreadon a misson a missthe job moves
  1. component Core 0runs the job first
    • read → L1 of core 0
    • the job moves → Core 1
  2. component L1 of core 0private: 1 line, 1 tick
    • on a miss → L2 of core 0
  3. component L2 of core 0private: 2 lines, 3 ticks
    • on a miss → L3
  4. component L3shared by both cores: 4 lines, 6 ticks
    • on a miss → Memory
  5. component Memoryholds every block: 20 ticks
  6. component Core 1gets the job later
    • read → L1 of core 1
  7. component L1 of core 1private: 1 line, 1 tick
    • on a miss → L2 of core 1
  8. component L2 of core 1private: 2 lines, 3 ticks
    • on a miss → L3
  1. One small cache

    Core 0 asks its L1 first. It is tiny and answers in 1 tick. On a miss the read goes on.

  2. A larger one behind it

    L2 holds more and takes 3 ticks. A read reaches it only after a miss in L1, so it pays for both.

  3. The last level, then memory

    L3 is the largest and the slowest cache. Behind it is memory, which holds every block and takes 20 ticks.

  4. A second core

    Core 1 has an L1 and an L2 of its own. Both cores use the same L3.

  5. The job moves

    The job goes on at core 1. Its private caches hold nothing of the job. L3 still holds what core 0 fetched.

Read the diagram as text
  • Core 0: runs the job first
  • Core 1: gets the job later
  • L1 of core 0: private: 1 line, 1 tick
  • L1 of core 1: private: 1 line, 1 tick
  • L2 of core 0: private: 2 lines, 3 ticks
  • L2 of core 1: private: 2 lines, 3 ticks
  • L3: shared by both cores: 4 lines, 6 ticks
  • Memory: holds every block: 20 ticks
  • Core 0 → L1 of core 0: read
  • L1 of core 0 → L2 of core 0: on a miss
  • L2 of core 0 → L3: on a miss
  • L3 → Memory: on a miss
  • Core 1 → L1 of core 1: read
  • L1 of core 1 → L2 of core 1: on a miss
  • L2 of core 1 → L3: on a miss
  • Core 0 → Core 1: the job moves

Two cores of the cluster of ap_phone, with the sizes and costs of the lab. Real caches are far larger.

The Smallest Example, Worked by Hand

No simulator yet. Take core 0 alone, with every cache empty. A lookup costs 1 tick in L1, 3 in L2 and 6 in L3, and memory costs 20.

L1 has one line. L2 has two: one for blocks with an even number, one for odd ones. A read that misses leaves a copy at every level it passed. Follow four reads.

Four reads of core 0
ReadBlockL1L2L3Served byTicks
18missmissmissmemory1 + 3 + 6 + 20 = 30
28hitL11
39missmissmissmemory30
48misshitL21 + 3 = 4

Look at read 4. Read 3 put block 9 into the only line of L1, so block 8 lost its place there. In L2 the two blocks use different lines, and both stay. The read pays 4 ticks, not 30.

That is the whole reason for a second level. It catches what the first one had to drop.

The Rules

  1. Rule 1: A read stops at the first level that holds the block.

    It never looks further out than it must.

  2. Rule 2: A read pays for every level it reaches.

    A hit in L2 costs the lookup in L1 and the lookup in L2.

  3. Rule 3: In this lab, a miss leaves a copy at every level it passed.

    So the next read of that block stops earlier. Real chips differ in this choice.

  4. Rule 4: Private levels belong to one core.

    A job that moves takes nothing of them along. It starts on cold private caches.

  5. Rule 5: Count the reads by the level they reach.

    The total time is the sum, over the levels, of the reads that reached a level times its lookup cost.

  6. Rule 6: A miss rate needs its denominator.

    Local: of the reads that reached this level. Global: of all reads.

Sources for These Rules

A second cache level lowers the cost of a miss in the first. It and the global miss rate are in Patterson and Hennessy, Computer Organization and Design, ARM edition, section 5.4.

Private first and second levels and a shared third level on real multicore chips: the same book, section 5.13.

Rule 3 is a choice of this lab. The sizes, the lookup costs and the tick are lab numbers, chosen to be small.

Rules 5 and 6 are arithmetic on the counts that the lab prints.

Now Run It

The lab at the end of this page is the hand example with a fifth read. Before that read the job moves, so core 1 makes it. The model is test code: it keeps the number of a block in each line, and it adds up ticks.

Printed by the lab on Icarus. One row for each read, then the counts.
core=0 block=8 served=memory start=0 end=30 ticks=30
core=0 block=8 served=L1 start=30 end=31 ticks=1
core=0 block=9 served=memory start=31 end=61 ticks=30
core=0 block=8 served=L2 start=61 end=65 ticks=4
core=1 block=8 served=L3 start=65 end=75 ticks=10
reads=5 at_l2=4 at_l3=3 at_memory=2 ticks=75

Rule 2 at work, in the first row: three lookups and the memory, 30 ticks. In the fourth row the read stops at L2 and pays 4.

Rule 4, in the fifth row. Core 1 has cold private caches, so L1 and L2 miss. L3 still holds block 8 from the first read. The row says served=L3 and ticks=10, and no read went to memory.

Had the job stayed, core 0 would have found block 8 in its L1. Change the last read to core 0, and the lab prints ticks=1 for it and 66 in total.

Rule 5, in the last row. All 5 reads paid 1 tick in L1. Then 4 paid 3 ticks, 3 paid 6 and 2 paid 20. The sum is 5 + 12 + 18 + 40 = 75 ticks, a mean of 15 for each read.

Rule 6, in the same row. L3 saw 3 reads and missed 2: a local miss rate of 2 in 3. Of all 5 reads, 2 reached memory: a global miss rate of 2 in 5.

This lab leaves things out. It only reads: a write would need the rules of coherence. Its caches hold a handful of lines, and ap_soc has no cache at all. The cluster belongs to ap_phone.

Why It Is Built This Way

ChoiceThe alternativeIts cost
Three levelsOne large cache next to the coreEvery read, hit or miss, would pay the slow lookup of a large memory.
Private L1 and L2Every level sharedEach core would wait its turn on every read, and a busy core would push out the blocks of the others.
A shared last levelPrivate caches onlyA moved job would fetch everything from memory again. So would two cores that read the same data.

The middle row has a price too. Two private caches can hold the same block, and then a write must reach both. That is the subject of Cache Coherence: MSI and MESI.

The Trap: Every Read Charged for Memory

A first budget for the video job takes 30 ticks for every read, because memory is that far away. The chip looks hopeless. Count by level, as rule 5 says, and the mean in the lab is 15.

The second trap is the wrong denominator. Two of five reads reached memory, and someone writes "L3 misses 40 percent". L3 saw three reads and missed two of them. Its own rate is far worse than the report says.

The third trap is the hit that comes late. L3 serves several cores, one at a time. A block can be in L3 and still arrive late, because another core is being served. A count of hits cannot show that. The lab has no such queue, and the practice problem adds one.

At Scale

This lab7 lines in 5 caches
A phone chipmany thousands of lines

This section looks ahead, to writes, to real sizes and to cores that differ.

  1. Writes

    When one core writes a block that another private cache holds, the other copy must go. The coherence rules do that, and the shared level often keeps track of who holds what.

  2. Real costs

    On a real chip the nearest level answers within a few clock cycles. Memory outside the package can take a hundred or more. The wider that gap, the more a middle level is worth.

  3. Cores that differ

    A cluster often mixes large fast cores with small frugal ones. Which core suits which job is a question about the inside of a core. The next page answers it.

Try It

Run the lab. The first four rows are the hand example. The fifth is the read after the job has moved.

Change the core of the last read to 0, so that the job stays. Compare the ticks of that read, and the total.

Add a read of block 12 by core 1 before the last read. Blocks 8 and 12 want the same line of L3. Predict where the last read is served.

Names Printed by the Lab
NameMeaning
core, blockThe core that reads, and the number of the block it reads. The lab uses block numbers in place of addresses.
servedThe first level that held the block: L1, L2, L3 or memory.
start, endThe time before and after the read, in ticks. A tick is the time unit of this lab.
ticksWhat this read cost: end minus start.
readsHow many reads were made. Every one of them reached L1.
at_l2, at_l3, at_memoryHow many reads went on to L2, to L3 and to memory.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: core=0 block=8 served=memory start=0 end=30 ticks=30 core=0 block=8 served=L1 start=30 end=31 ticks=1 core=0 block=9 served=memory start=31 end=61 ticks=30 core=0 block=8 served=L2 start=61 end=65 ticks=4 core=1 block=8 served=L3 start=65 end=75 ticks=10 reads=5 at_l2=4 at_l3=3 at_memory=2 ticks=75

Check Yourself

Predict the outputIn this run core 0 reads block 8, and core 1 reads block 8 twice. Then core 0 reads block 9, and core 1 reads block 9. What is the last line? Its form is: reads=5 at_l2=.. at_l3=.. at_memory=.. ticks=..
module tb;
  // Two cores of one cluster. Each cache line holds the number of one block of memory, or -1 when it is empty.
  int l1 [0:1];        // private, one line for each core
  int l2 [0:1][0:1];   // private, two lines for each core
  int l3 [0:3];        // shared by both cores, four lines
  // What a lookup costs at each level, in ticks. A read pays for every level it reaches.
  localparam int L1_COST = 1, L2_COST = 3, L3_COST = 6, MEMORY_COST = 20;
  int now = 0, reads = 0, at_l2 = 0, at_l3 = 0, at_memory = 0;

  task automatic read_block(input int core, input int block);
    int start, slot2, slot3;
    string served;
    start = now;
    slot2 = block % 2;                   // the L2 line this block may use
    slot3 = block % 4;                   // the L3 line this block may use
    reads++;
    now += L1_COST;
    if (l1[core] == block) served = "L1";
    else begin
      at_l2++;
      now += L2_COST;
      if (l2[core][slot2] == block) served = "L2";
      else begin
        at_l3++;
        now += L3_COST;
        if (l3[slot3] == block) served = "L3";
        else begin
          at_memory++;
          now += MEMORY_COST;
          served = "memory";
          l3[slot3] = block;             // leave a copy at every level passed
        end
        l2[core][slot2] = block;
      end
      l1[core] = block;
    end
    $display("core=%0d block=%0d served=%s start=%0d end=%0d ticks=%0d",
             core, block, served, start, now, now - start);
  endtask

  initial begin
    foreach (l1[c]) l1[c] = -1;
    foreach (l2[c, s]) l2[c][s] = -1;
    foreach (l3[s]) l3[s] = -1;
    // The reads, in order: read_block(core, block).
    read_block(0, 8);
    read_block(1, 8);
    read_block(1, 8);
    read_block(0, 9);
    read_block(1, 9);
    $display("reads=%0d at_l2=%0d at_l3=%0d at_memory=%0d ticks=%0d", reads, at_l2, at_l3, at_memory, now);
  end
endmodule
ConceptWhy does a chip use several cache levels in place of one large cache?
ConceptA read misses in L1 and in L2 and hits in L3. The lookups cost 1, 3 and 6 ticks. What does the read pay?
ConceptA job moves to another core. Which cache can still hold its blocks?
ConceptOf 5 reads, 3 reach L3, and 2 of those go on to memory. What is the local miss rate of L3?

Common Mistakes

Wrong Charge every read the cost of memory.

Most reads stop at a cache.

Fix Count the reads that reach each level, then add up.

Wrong Expect a moved job to find its private caches warm.

L1 and L2 belong to the core, not to the job.

Fix Follow the first read on the new core to the level that serves it.

Wrong Divide the misses of a level by all reads.

Only the reads that reached the level could miss there.

Fix Use the reads that arrived at that level.

Remember

  • A read stops at the first level that holds the block, and pays for every level it reached.
  • L1 and L2 are private to a core. L3 is shared by the cluster.
  • A moved job starts on cold private caches. The shared level can still serve it.
  • Count reads by the level they reach, and name the denominator of every miss rate.

Practice

Report a problem with this page