---
title: Cache performance and the three Cs
heading: Average memory access time across levels, local and global miss rates, and classifying misses
eyebrow: S4 · Lesson 18 · Cache performance and the three Cs
xp: 50
---

::: hook
A processor that executes an instruction every fraction of a nanosecond is fed by a DRAM that answers in about a hundred nanoseconds. Caches bridge that gap, and their quality shows in a single number: the average memory access time. Improving it means knowing where the misses come from. Some are unavoidable first touches, some happen because the program's data doesn't fit, and some because two addresses fight over the same set. Each kind has a different cure, and every cure has a cost, often a slower hit.
:::

::: play
- Average memory access time for a single cache against its size, when the miss rate follows the empirical $\sqrt 2$ rule, $m(S) = m_0(S/32\text{ KB})^{-\alpha}$ with α near 0.5, and misses cost P cycles.
- This sim assumes the hit time grows half a cycle per doubling, $1 + 0.5\log_2(S/4\text{ KB})$ cycles, since bigger arrays have longer wires.
- The total has a minimum: a bigger cache misses less but every hit gets slower. That's why real systems use several levels, small and fast near the core, large and slower behind.
:::
:::

```sim plot
view: graph
x: S = 4..4096 KB log
y: A = 0..30 cycles
slider: m = 0.01..0.15 = 0.05
slider: a = 0.3..0.7 = 0.5
slider: P = 50..400 = 200 cycles
plot: 1 + 0.5*log(S/4)/log(2) + m*(S/32)^(-a)*P | samples: 300 | label: AMAT, cycles
plot: 1 + 0.5*log(S/4)/log(2) | dashed | color: ch2 | label: hit time, cycles
```

::: predict
## An L1 cache hits in 1 cycle and misses 5% of the time; the L2 hits in 10 cycles and misses 20% of what reaches it; memory takes 200 cycles. What's the average access time?

- [ ] 1.5 cycles
- [x] 3.5 cycles
- [ ] 11 cycles
- [ ] 20 cycles

::: reveal
3.5 cycles: $1 + 0.05 \times (10 + 0.2 \times 200)$. The L2's 20% looks bad, but it's a **local** miss rate: only 5% of accesses reach the L2, so its **global** miss rate is 0.05 × 0.2 = 1%. Without the L2, every L1 miss would cost 200 cycles and the AMAT would be 11.
:::
:::

::: nameit
## Average memory access time

$$\text{AMAT} = h_1 + m_1(h_2 + m_2(h_3 + \dots))$$

where $m_i$ is the **local** miss rate of level i (misses over accesses reaching it). The **global** miss rate of level k is $m_1m_2\cdots m_k$. Memory stall cycles per instruction = accesses per instruction × (AMAT − $h_1$), when misses don't overlap. **MPKI**, misses per thousand instructions, = 1000 × accesses per instruction × global miss rate.

## The three Cs

- **Compulsory:** the first access to a line; even an infinite cache has them. A single pass over D bytes with B-byte lines has D/B.
- **Capacity:** the working set doesn't fit; these remain in a fully associative cache of the same size.
- **Conflict:** too many lines map to the same set; these vanish with full associativity.

Counting misses for an infinite cache, a fully associative cache of the size, and the real cache separates them: compulsory = infinite; capacity = fully associative − compulsory; conflict = real − fully associative. (Multiprocessors add a fourth C, coherence misses, lesson 32.)

## Remedies and their costs

| Change | Cuts | Costs |
|---|---|---|
| larger cache | capacity | hit time, area, power |
| higher associativity | conflict | hit time, power |
| larger lines | compulsory (spatial locality) | miss penalty, pollution |
| more levels | effective penalty | complexity |

The empirical $\sqrt 2$ rule: doubling a cache's size divides its miss rate by about √2 (α ≈ 0.5).
:::

::: example
## For the 1 / 10 / 200-cycle hierarchy above, with 1.3 memory accesses per instruction, find the global L2 miss rate, the L2 MPKI and the stall cycles per instruction.

::: solution
1. **Global L2 miss rate.**
   $$0.05 \times 0.2 = 1\%$$
2. **L2 MPKI.**
   $$1000 \times 1.3 \times 0.01 = 13$$
3. **Stall cycles.** If no misses overlap:
   $$1.3 \times (3.5 - 1) = 3.25$$
   per instruction.
4. **In practice.** An out-of-order core overlaps many of them, which is why measured stalls are usually lower.

**Answer:** a global L2 miss rate of 1%, an L2 MPKI of 13, and 3.25 stall cycles per instruction if no misses overlap.
:::
:::

::: example
## A program has 10,000 misses on a direct-mapped cache, 6,500 on a fully associative cache of the same size, and 2,000 on an infinite one. Classify them.

::: solution
1. **Compulsory.** These are the infinite cache's misses: 2,000.
2. **Capacity.**
   $$6{,}500 - 2{,}000 = 4{,}500$$
3. **Conflict.**
   $$10{,}000 - 6{,}500 = 3{,}500$$
4. **What helps each.** Higher associativity could remove up to 3,500 misses; only a larger cache helps the 4,500 capacity misses; nothing but prefetching helps the compulsory ones.

**Answer:** 2,000 compulsory, 4,500 capacity and 3,500 conflict misses.
:::
:::

::: example
## A 32 KB L1 hits in 1 cycle and misses 4% of the time; a 128 KB L1 would miss half as often (the √2 rule, twice over) but hit in 2 cycles. The miss penalty is 80 cycles. Which gives the lower AMAT?

::: solution
1. **32 KB.**
   $$\begin{aligned} \text{AMAT} &= 1 + 0.04 \times 80 \\ &= 4.2\ \text{cycles} \end{aligned}$$
2. **128 KB.**
   $$\begin{aligned} \text{AMAT} &= 2 + 0.02 \times 80 \\ &= 3.6\ \text{cycles} \end{aligned}$$
   so the bigger cache wins here.
3. **Shorter penalties.** With a 50-cycle penalty both give 3.0, and with a shorter one the small, fast cache wins, which is why L1s stay small when an L2 sits close behind them.

**Answer:** the 128 KB cache gives the lower AMAT, 3.6 cycles against 4.2.
:::
:::

::: check
given: h = 1 | 2; m = 0.03 | 0.08; k = 8 | 14; n = 0.15 | 0.4; M = 150 | 300
answer: h + m*(k + n*M)
tolerance: 0.5%

## L1 hits in {h} cycles and misses {m}; L2 hits in {k} cycles and misses {n} locally; memory takes {M} cycles. What's the AMAT?

::: right
Right. $h_1 + m_1(h_2 + m_2M)$ = {answer} cycles.
:::

::: misses
- [h + m*k + n*M] The L2 miss rate applies only to accesses that reach the L2.
- [h + m*(k + M)] Only L2 misses go to memory.
- [any] Not yet. Nest the levels.
:::
:::

::: check
given: m = 0.02 | 0.06; n = 0.1 | 0.3 | 0.5
answer: 100*m*n
unit: %
tolerance: 0.5%

## L1 misses {m} of accesses; L2's local miss rate is {n}. What's L2's global miss rate?

::: right
Right. $m_1m_2$ = {answer}%.
:::

::: misses
- [100*n] That's the local rate; only L1 misses reach L2.
- [any] Not yet. Multiply the local rates.
:::
:::

::: check
given: a = 1.2 | 1.4; A = 2.5 | 4; h = 1 | 2
answer: a*(A - h)
tolerance: 0.5%

## A program makes {a} memory accesses per instruction; AMAT is {A} cycles and the hit time {h}. How many stall cycles per instruction does memory add, with no overlap?

::: right
Right. $a(\text{AMAT} - h_1)$ = {answer}.
:::

::: misses
- [a*A] The hit time is already part of the pipeline's base CPI.
- [any] Not yet. Accesses × extra cycles each.
:::
:::

::: check
given: m = 0.04 | 0.08; S = 32 | 64; T = 128 | 512; a = 0.4 | 0.5
answer: 100*m*(T/S)^(-a)
unit: %
tolerance: 0.5%

## A {S} KB cache misses {m} of the time. By a power law with exponent {a}, what miss rate should a {T} KB cache have?

::: right
Right. $m(T/S)^{-\alpha}$ = {answer}%.
:::

::: misses
- [100*m*S/T] The miss rate falls more slowly than the size grows.
- [any] Not yet. Scale by the size ratio to the power −α.
:::
:::

::: check
given: D = 1 | 8 | 64; B = 32 | 64 | 128
answer: D*1024*1024/B
tolerance: 0.5%

## A program streams once through {D} MB of data with {B}-byte cache lines. How many compulsory misses does it take?

::: right
Right. D/B = {answer}.
:::

::: misses
- [D*1e6/B] A megabyte here is $2^{20}$ bytes.
- [any] Not yet. One miss per line touched.
:::
:::

::: check
given: d = 8000 | 12000; f = 5000 | 7000; c = 1000 | 2500
answer: d - f
tolerance: 0

## A program has {d} misses on a direct-mapped cache, {f} on a fully associative one of the same size, and {c} on an infinite cache. How many are conflict misses?

::: right
Right. {answer}; capacity misses are {f} − {c}.
:::

::: misses
- [f - c] That's the capacity misses.
- [d - c] That counts capacity and conflict together.
- [any] Not yet. What associativity removes.
:::
:::

::: check
kind: parts
given: h = 1 | 4; m = 0.03 | 0.08; k = 10 | 14; q = 0.2 | 0.5; M = 150 | 250

## An L1 hits in {h} cycles with a {m} miss rate; its L2 hits in {k} cycles with a local miss rate of {q}; memory takes {M} cycles. There are 1.3 memory accesses per instruction, and misses don't overlap.

::: part
answer: h + m*(k + q*M)
unit: cycles
tolerance: 0.5%

### What is the AMAT?

::: misses
- [h + m*k + q*M] The L2's misses happen only on L1 misses.
- [any] $h_1 + m_1(h_2 + m_2t_\text{mem})$.
:::
:::

::: part
answer: m*q
tolerance: 0.5%

### What is the global L2 miss rate?

::: misses
- [q] That's the local rate; multiply by the L1's.
- [any] $m_1m_2$.
:::
:::

::: part
answer: 1.3*m*(k + q*M)
tolerance: 0.5%

### How many stall cycles per instruction does memory add?

::: misses
- [any] Accesses per instruction × (AMAT − $h_1$).
:::
:::
:::

::: check
kind: derivation
vars: m = 0.005..0.2, S = 4..1024, T = 4..1024, h = 1..5, P = 10..300

## A cache of size S misses at rate m; the √2 rule predicts how the rate changes at size T. Hits take h cycles and misses P.

::: step
answer: m*sqrt(S/T)

### What miss rate does size T have?
:::

::: step
answer: h + m*sqrt(S/T)*P

### What is its AMAT?
:::

::: right
Right. Quadrupling a cache halves its miss rate, by the rule, which is worth it only if the hit time doesn't grow too much.
:::
:::

::: explain
## Why does a second-level cache help even though its own miss rate often looks poor?

::: model
The L2 only sees the accesses that already missed in the L1, and those are the hardest ones: the first-level cache has already caught the easy, frequently reused data. So the L2's local miss rate, measured over its own accesses, is naturally high, perhaps 20 to 50%. What matters is how many accesses it saves from main memory. Without an L2, every L1 miss pays the full memory latency; with it, most of them pay only the L2's much shorter hit time, and only the L2's misses, a small global fraction of all accesses, go to memory. The average access time can drop by a factor of several even with a local miss rate that sounds bad.
:::
:::

::: practice

::: check
given: h = 1 | 3; m = 0.04 | 0.1; k = 10 | 20; n = 0.2 | 0.5; M = 200 | 400
answer: h + m*(k + n*M)
tolerance: 0.5%

## AMAT: L1 {h} / {m}, L2 {k} / {n}, memory {M}?

::: misses
- [any] $h_1 + m_1(h_2 + m_2M)$.
:::
:::

::: check
given: a = 1.25 | 1.5; m = 0.01 | 0.03
answer: 1000*a*m
tolerance: 0.5%

## MPKI with {a} accesses per instruction and a {m} global miss rate?

::: misses
- [any] 1000 × accesses × miss rate.
:::
:::

::: check
given: d = 9000 | 15000; f = 6000 | 8000; c = 1500 | 3000
answer: f - c
tolerance: 0

## Capacity misses: DM {d}, FA {f}, infinite {c}?

::: misses
- [any] FA minus compulsory.
:::
:::
:::
