Second-Level Caches, Split Caches and Choosing a Cache

One cache cannot be both fast and large. So a chip uses several, and each has a job. Which arrangement wins depends on the program, and only a run can say.

You Will Be Able To

  • Explain what a second-level cache saves, and when it saves nothing.
  • Explain why level 1 is often split into an instruction cache and a data cache.
  • Choose between cache designs by running the workloads that matter.
Lesson
S3.20
Time
25 min
Runs on
Your browser
Check
6 questions

A team adds a second cache behind the cache of its chip. It is the same size as the first. Every program gets slower.

Before You Start

You need S3.2: cache levels L1 and L2, and the cycles of an access. You need S3.6: capacity and conflict misses.

You also need S0.6: a processor fetches instructions, and some of them read data.

Five questions this page answers
Question
1What does a second-level cache save?
2When is a second-level cache useless?
3Why is level 1 often split in two?
4What does a split cache cost?
5How is a cache chosen for a workload?

The Idea: Each Cache Has a Job

A cache of level 1 must answer inside the clock cycle of the processor. That keeps it small and with few ways. So it has capacity misses, and each of them goes to the slow memory.

A second cache behind it has another job. It need not be fast. It must only be much faster than memory, and much larger than level 1. Then most misses of level 1 end there, and few reach memory. This is the second-level cache.

It must be larger. A second level that holds the same blocks as the first can never help. It only adds its own look to every miss.

Level 1 has a second problem. A processor fetches instructions and reads data. Each is a stream of addresses, and both streams want the cache. In one shared cache, a block of data can push out a block of code that the very next fetch needs.

So level 1 is often two caches. An instruction cache serves the fetches. A data cache serves the loads and stores. The price is a fixed border. If a program has much code and little data, the unused half cannot help.

Which arrangement is best? There is no single answer. It depends on the workload: the programs that the chip will really run. The last two pages gave the method. Run the workload, and count cycles.

New Words

New words on this page
WordMeaning
Second-level cacheA larger and slower cache behind level 1. It is asked only when level 1 misses. Also called the level-2 cache.
Instruction cacheA cache of level 1 that holds only blocks of code. The fetch uses it.
Data cacheA cache of level 1 that holds only blocks of data. Loads and stores use it.
Split cacheA level 1 built as an instruction cache and a data cache.
Unified cacheOne cache that holds code and data together.
WorkloadThe set of programs that a chip is expected to run.

One Picture: Three Caches and Their Jobs

Two Small Caches in Front, One Large Cache BehindStep through the picture. It splits the first level in two, and then puts a second level behind both.
  • Processor
  • Memory
Two Small Caches in Front, One Large Cache BehindStep through the picture. It splits the first level in two, and then puts a second level behind both. component Processor fetches instructionsand reads data component Instruction cache level 1, for fetchesonly component Data cache level 1, for loads andstores component Level-2 cache larger and slower, forboth component Slow memory the last stop fetchload, storea missa missa miss
  1. component Processorfetches instructions and reads data
    • fetch → Instruction cache
    • load, store → Data cache
  2. component Instruction cachelevel 1, for fetches only
    • a miss → Level-2 cache
  3. component Data cachelevel 1, for loads and stores
    • a miss → Level-2 cache
  4. component Level-2 cachelarger and slower, for both
    • a miss → Slow memory
  5. component Slow memorythe last stop
  1. Two streams from one processor

    A processor fetches one instruction after another. Many instructions also read or write data. These are two streams of addresses.

  2. A cache for each stream

    Give each stream its own cache of level 1. Both can be looked up in the same cycle, and neither pushes out blocks of the other.

  3. A second level behind both

    A miss in either cache asks a larger cache first. It is slower than level 1, and far faster than memory.

  4. Memory only as the last stop

    Only a miss in level 2 goes to the slow memory. Level 2 exists to make that rare.

Read the diagram as text
  • Processor: fetches instructions and reads data
  • Instruction cache: level 1, for fetches only
  • Data cache: level 1, for loads and stores
  • Level-2 cache: larger and slower, for both
  • Slow memory: the last stop
  • Processor → Instruction cache: fetch
  • Processor → Data cache: load, store
  • Instruction cache → Level-2 cache: a miss
  • Data cache → Level-2 cache: a miss
  • Level-2 cache → Slow memory: a miss

The arrangement of the lab on this page. The sizes of the three caches are what a team must choose.

The Smallest Example, Worked by Hand

Take a direct-mapped level 1 of two lines. A program reads blocks 0, 2, 4 and 6, and then the same four again. All four are even, so all use line 0.

The prices of this page: a look in level 1 costs 1 cycle. A look in level 2 costs 4 more. A read from memory costs 20 more.

Eight reads, without and with a second level
DesignFirst roundSecond roundCycles in all
Level 1 alone4 misses to memory: 4 x 214 misses to memory: 4 x 21168
Level 1, and a level 2 of 8 lines4 misses to memory: 4 x 254 hits in level 2: 4 x 5120

With level 2, a read from memory costs 1 + 4 + 20 = 25 cycles. A hit in level 2 costs 1 + 4 = 5.

Level 1 never hits: the four blocks fight over one line. In the first round the second level makes things worse, by 4 cycles for each read. Its own look comes on top.

In the second round it pays. The four blocks are all in level 2, which has a line for each. Four reads cost 5 cycles each, where they cost 21 before.

The Rules

  1. Rule 1: Level 1 is built for a short hit time. Level 2 is built for few misses.

    Each level has one job, so each can be good at it.

  2. Rule 2: A second level adds its look to every miss of level 1.

    It pays only when it hits often enough.

  3. Rule 3: A second level must hold blocks that the first has lost.

    So it must be larger, or arranged differently.

  4. Rule 4: A split level 1 keeps code and data from pushing each other out.

    It also lets a fetch and a load look in the same cycle.

  5. Rule 5: A split fixes the share of each stream.

    Lines that one stream leaves unused cannot serve the other.

  6. Rule 6: Choose a cache by running the workload.

    Different programs rank the same designs differently.

Sources for These Rules

The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.4. See "Reducing the Miss Penalty Using Multilevel Caches". Section 5.3 describes a real processor with split instruction and data caches.

The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4: multiple-level caches.

The prices of 1, 4 and 20 cycles are choices of this lab. They are the prices of the lab in S3.2.

Both workloads are our own teaching traces. The chip of this course has no cache.

Now Run It

The lab at the end of this page is a counting model of up to three small direct-mapped caches. An access is a fetch or a load, and it names a block.

It runs two workloads. Workload A is a loop of two code blocks that walks 16 blocks of data, twice. Workload B is a loop of six code blocks that keeps using two blocks of data.

Each workload goes through three designs. The first is one unified cache of 8 lines. The second is a split of 4 and 4 lines. The third adds a level 2 of 32 lines to that split.

Printed by the lab on Icarus. Three designs for workload A, and then the same three for workload B.
A one cache of 8     : accesses=96 l1_hits=54 l2_hits=0 mem_reads=42 cycles=936
A split 4 + 4        : accesses=96 l1_hits=62 l2_hits=0 mem_reads=34 cycles=776
A split 4 + 4, L2 32 : accesses=96 l1_hits=62 l2_hits=16 mem_reads=18 cycles=592
B one cache of 8     : accesses=64 l1_hits=56 l2_hits=0 mem_reads=8 cycles=224
B split 4 + 4        : accesses=64 l1_hits=28 l2_hits=0 mem_reads=36 cycles=784
B split 4 + 4, L2 32 : accesses=64 l1_hits=28 l2_hits=28 mem_reads=8 cycles=368
Cycles of each workload in each design
DesignWorkload AWorkload B
One unified cache of 8 lines936224
Split: 4 lines and 4 lines776784
Split, with a level 2 of 32 lines592368

From the recorded rows. Fewer cycles are better.

Rule 4 at work: workload A, first two rows. In the unified cache the data walks over the two code blocks again and again. The split gives the code its own lines, and 8 more accesses hit.

Rule 5 at work: workload B, the same two rows. Six code blocks want six lines, and the split gives them four. Two lines of the data half stand empty. The cycles rise from 224 to 784.

Rules 1 and 2 at work: the third row of each workload. In A the second round of the walk hits in level 2, 16 times. In B the code blocks that fight in level 1 are all found in level 2, 28 times.

Rule 6 at work: read the table by columns. For A the unified cache is the worst design. For B it is the best. A team that tested only one workload would have chosen wrongly for the other.

This lab leaves out the data, the ways and the area of each design. Every cache in it is direct-mapped.

Why It Is Built This Way

Design choiceThe alternativeWhat the choice buys, and costs
Two levelsOne large level 1A short hit time and few reads from memory. A second look on every miss of level 1.
A level 2 several times largerA level 2 of the same sizeIt holds what level 1 has lost. It costs area.
A split level 1A unified level 1A fetch and a load in the same cycle, and no fights between the streams. A fixed border.
A unified level 2A split level 2Code and data share the room as they need it. Level 2 is asked seldom, so one look at a time is enough.

The Trap: A Second Level of the Same Size

This is the team from the top of the page. Their level 1 is a unified cache of 8 lines. They add a level 2 of 8 lines, also direct-mapped. Then they try 32 lines.

The same lab with workload A. Level 1 alone, then with a level 2 of 8 lines, then of 32 lines.
A one cache of 8       : accesses=96 l1_hits=54 l2_hits=0 mem_reads=42 cycles=936
A one cache of 8, L2 8 : accesses=96 l1_hits=54 l2_hits=0 mem_reads=42 cycles=1104
A one cache of 8, L2 32: accesses=96 l1_hits=54 l2_hits=18 mem_reads=24 cycles=744

The second row is the failure. Level 2 never hits. It has the same lines and the same index as level 1, so it holds exactly the same blocks. A block that level 1 has lost, level 2 has lost too.

It still costs. Each of the 42 misses pays 4 cycles for the useless look: 168 cycles more. Rule 3 is the check. The third row has a level 2 of 32 lines, and it hits 18 times.

At Scale

This page4 to 32 lines, 2 levels
A phone chipthree levels, the last shared by all cores

This section looks ahead. Real chips have two or three levels, and each level is tuned on many programs.

  1. Split first, unified behind

    Common practice is a split level 1 for each core, and unified larger levels behind it.

  2. A level for all cores

    The last level is often shared by a group of cores: The Cache Hierarchy of a Core Cluster.

  3. Many workloads

    A team scores each design on a set of programs, and weighs each by how much it matters.

  4. Area is the budget

    Every line costs bits. The practice problem of this page gives you a bit budget to spend.

Try It

Run the lab. Fill the table of cycles from the six rows.

Try start(6, 2, 0) for both workloads. Predict the cycles of each before you run it.

Change T_L2 from 4 to 12. Does the level 2 still pay for workload A?

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: A one cache of 8 : accesses=96 l1_hits=54 l2_hits=0 mem_reads=42 cycles=936 A split 4 + 4 : accesses=96 l1_hits=62 l2_hits=0 mem_reads=34 cycles=776 A split 4 + 4, L2 32 : accesses=96 l1_hits=62 l2_hits=16 mem_reads=18 cycles=592 B one cache of 8 : accesses=64 l1_hits=56 l2_hits=0 mem_reads=8 cycles=224 B split 4 + 4 : accesses=64 l1_hits=28 l2_hits=0 mem_reads=36 cycles=784 B split 4 + 4, L2 32 : accesses=64 l1_hits=28 l2_hits=28 mem_reads=8 cycles=368

Check Yourself

Predict the outputWorkload B has six code blocks and two data blocks. It runs through a unified cache of 8 lines, and then through a split of 6 and 2 lines. What does the last row say?
// Up to three small direct-mapped caches, as a counting model. An access names a block.
//   cache 0: level 1, or its instruction half    cache 1: the data half of level 1    cache 2: level 2
module tb;
  int  size [0:2];             // lines of each cache. 0: the cache is not there
  logic valid [0:2][0:63];
  int   holds [0:2][0:63];
  localparam T_L1 = 1, T_L2 = 4, T_MEM = 20;   // cycles for a look in level 1, in level 2 on top, in memory on top
  int accesses, l1_hits, l2_hits, mem_reads, cycles;

  task automatic start(input int instr_lines, input int data_lines, input int l2_lines);
    size[0] = instr_lines;  size[1] = data_lines;  size[2] = l2_lines;
    for (int c = 0; c < 3; c++) for (int i = 0; i < 64; i++) valid[c][i] = 0;
    accesses = 0;  l1_hits = 0;  l2_hits = 0;  mem_reads = 0;  cycles = 0;
  endtask

  function automatic bit look(input int c, input int block);   // a hit? If not, the block comes into this cache
    int line;
    line = block % size[c];
    look = valid[c][line] && holds[c][line] == block;
    valid[c][line] = 1;  holds[c][line] = block;
  endfunction

  task automatic access(input bit is_data, input int block);
    int c;
    c = (is_data && size[1] > 0) ? 1 : 0;      // with no data half, everything shares cache 0
    accesses++;  cycles += T_L1;
    if (look(c, block)) l1_hits++;
    else if (size[2] > 0) begin
      cycles += T_L2;
      if (look(2, block)) l2_hits++;
      else begin mem_reads++;  cycles += T_MEM; end
    end else begin
      mem_reads++;  cycles += T_MEM;
    end
  endtask
  task automatic fetch(input int block);  access(0, block);  endtask    // an instruction
  task automatic data(input int block);   access(1, block);  endtask    // a load

  // Workload A: a loop of two code blocks that walks 16 blocks of data, twice.
  task automatic walk_array;
    for (int pass = 0; pass < 2; pass++)
      for (int i = 0; i < 16; i++) begin fetch(0); data(64 + i); fetch(1); end
  endtask
  // Workload B: a loop of six code blocks that keeps two blocks of data, eight times round.
  task automatic big_code;
    for (int round = 0; round < 8; round++) begin
      for (int i = 0; i < 6; i++) fetch(i);
      data(70);  data(71);
    end
  endtask

  task automatic row(input string name);
    $display("%s: accesses=%0d l1_hits=%0d l2_hits=%0d mem_reads=%0d cycles=%0d", name, accesses, l1_hits, l2_hits, mem_reads, cycles);
  endtask

  initial begin
    start(8, 0, 0);  big_code;  row("B one cache of 8 ");
    start(6, 2, 0);  big_code;  row("B split 6 + 2    ");
  end
endmodule
ConceptWhat is the job of a second-level cache?
ConceptA level 2 has the same lines and the same index as level 1. How often does it hit?
ConceptWhy is level 1 often split into two caches?
ConceptWhat does a split cache cost?
ConceptTwo workloads rank three cache designs in opposite order. What should the team do?

Common Mistakes

Wrong Add a second level of the same size and shape as the first.

It holds the same blocks, so it misses whenever level 1 misses.

Fix Make level 2 several times larger.

Wrong Split every cache into code and data halves.

A fixed border wastes the lines of the lighter stream.

Fix Split level 1, where both streams need a look in the same cycle.

Wrong Choose a design on one test program.

Another program can rank the designs the other way round.

Fix Run every workload that matters, and compare the cycles.

Remember

  • Level 1 is built to be fast. Level 2 is built to miss seldom.
  • A second level must be larger than the first, or it holds the same blocks.
  • A split level 1 ends fights between code and data, at the price of a fixed border.
  • Choose a cache by cycles on the workloads that matter.

Practice

Report a problem with this page