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.
| Question | |
|---|---|
| 1 | What does a second-level cache save? |
| 2 | When is a second-level cache useless? |
| 3 | Why is level 1 often split in two? |
| 4 | What does a split cache cost? |
| 5 | How 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
| Word | Meaning |
|---|---|
| Second-level cache | A larger and slower cache behind level 1. It is asked only when level 1 misses. Also called the level-2 cache. |
| Instruction cache | A cache of level 1 that holds only blocks of code. The fetch uses it. |
| Data cache | A cache of level 1 that holds only blocks of data. Loads and stores use it. |
| Split cache | A level 1 built as an instruction cache and a data cache. |
| Unified cache | One cache that holds code and data together. |
| Workload | The set of programs that a chip is expected to run. |
One Picture: Three Caches and Their Jobs
- Processor
- Memory
- 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.
- 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.
- 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.
- 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.
| Design | First round | Second round | Cycles in all |
|---|---|---|---|
| Level 1 alone | 4 misses to memory: 4 x 21 | 4 misses to memory: 4 x 21 | 168 |
| Level 1, and a level 2 of 8 lines | 4 misses to memory: 4 x 25 | 4 hits in level 2: 4 x 5 | 120 |
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
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.
Rule 2: A second level adds its look to every miss of level 1.
It pays only when it hits often enough.
Rule 3: A second level must hold blocks that the first has lost.
So it must be larger, or arranged differently.
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.
Rule 5: A split fixes the share of each stream.
Lines that one stream leaves unused cannot serve the other.
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.
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| Design | Workload A | Workload B |
|---|---|---|
| One unified cache of 8 lines | 936 | 224 |
| Split: 4 lines and 4 lines | 776 | 784 |
| Split, with a level 2 of 32 lines | 592 | 368 |
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 choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Two levels | One large level 1 | A short hit time and few reads from memory. A second look on every miss of level 1. |
| A level 2 several times larger | A level 2 of the same size | It holds what level 1 has lost. It costs area. |
| A split level 1 | A unified level 1 | A fetch and a load in the same cycle, and no fights between the streams. A fixed border. |
| A unified level 2 | A split level 2 | Code 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.
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=744The 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 section looks ahead. Real chips have two or three levels, and each level is tuned on many programs.
Split first, unified behind
Common practice is a split level 1 for each core, and unified larger levels behind it.
A level for all cores
The last level is often shared by a group of cores: The Cache Hierarchy of a Core Cluster.
Many workloads
A team scores each design on a set of programs, and weighs each by how much it matters.
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?
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=368Check Yourself
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.