5.4.8 · D3Memory Hierarchy & Caches

Worked examples — Multi-level cache hierarchy (L1 - L2 - L3)

3,355 words15 min readBack to topic

This page is one thing: practice until no case can surprise you. The parent note built the AMAT formula. Here we hit every kind of number it can throw at you — perfect caches, useless caches, missing levels, real workloads, and exam traps.

Everything below rests on one recursive idea, so let us restate it in plain words before any symbol appears.

Let us name the pieces once (the parent defined these; we keep them so line one is readable).

Because we have three cache levels, we tag each rate with the level it belongs to. This is just the same and written three times over:


The scenario matrix

Before working problems, here is the full space of cases. Every example below is tagged with the cell it covers, so you can see nothing is skipped.

Cell What makes it special Where it can bite you
A0. Single level only L1 exists the base scenario the whole recursion grows from
A. Baseline ordinary numbers, all levels present the "vanilla" 3-level AMAT
B. Zero miss (perfect cache) some whole levels below vanish — does the formula still work?
C. Full miss (useless cache) some cache adds latency but no benefit — cost goes up
D. Missing level no L3 at all collapse the 3-level formula to 2-level
E. Limiting behaviour vs AMAT bounded between and full chain
F. Solve backwards AMAT given, find a hit rate algebra, not just plug-in
G. Word problem wall-clock seconds, GHz cycles ↔ time unit conversion
H. Exam twist global vs local miss rate the classic mistake that loses marks

The figure below is this matrix drawn as a pipeline: every access enters at the left (Cell A0 lives entirely in the first box), the shrinking "still hasn't found it" stream flowing rightward is what Cells A–E track, and the labelled arrows are precisely the miss rates we tag in each example. Read a worked example, then find its box in the picture.

Figure — Multi-level cache hierarchy (L1 - L2 - L3)

The picture shows why: of accesses hit the L1 box, only survive to L2, only to L3, only to DRAM. Those percentages are the products of miss rates we will compute in Cell H. Keep the box widths in mind — the deeper you go, the thinner the stream.


Cell A0 — The single-level base case (L1 only)

Forecast: With just one cache, every miss goes straight to DRAM. Will the answer be bigger or smaller than the 3-level case? (Think before reading — no L2/L3 to catch anything.)

  1. Pick the one-level formula. Why this step? There is no bracket to nest — an L1 miss has nowhere to go but memory, so its penalty is . This is the simplest, non-recursive doll.

  2. Plug in. Why this step? Every access pays the -cycle L1 look. Only miss, each paying , contributing cycles on average.

Verify: sits between (if nothing missed) and (if everything missed). It leans toward because hit. ✓ This is the seed: every richer formula below is built by replacing with the average cost of a cache level. (This is Cell A0.)


Cell A — The baseline three-level AMAT

Forecast: Guess now — will it be closer to or closer to ? (Most people over-guess; the answer will feel shockingly small.)

  1. Innermost bracket: L3's average cost. Why this step? L3 always costs to look in. Only of the accesses that reach L3 fall through to DRAM's . So on average an "L3-region" access costs . This number replaces DRAM for the level above.

  2. Middle bracket: L2's average cost. Why this step? We just said "reaching L3 costs on average," so from L2's point of view a miss penalty is , not . L2 looks cost ; of them fall through to that .

  3. Outer: the final answer. Why this step? Every access pays L1's . Only ever wake up the deeper machinery, and for them that machinery averages .

Verify: Sanity check — sits between (best possible) and (if L1's misses went straight to DRAM). It is much nearer because L2/L3 do heavy filtering. ✓ (This is Cell A.)


Cell B — A perfect level (zero miss rate)

Forecast: If L2 catches everything it is asked, do L3 and DRAM matter at all?

  1. Innermost still evaluates — but gets multiplied by zero. Why this step? We compute it, but watch what happens to it next.

  2. Middle bracket kills it. Why this step? means no access ever leaves L2. The we computed is real but reaches no one. The formula multiplies it away — exactly what "perfect L2" should do.

  3. Outer. Why this step? This is the standard outer layer: every access pays L1's , and the that miss L1 now hit a level whose entire cost is just L2's (since L2 never fails). Multiplying and adding gives the average.

Verify: With a perfect L2, the deepest thing any access can touch is L2 at cycles. So AMAT must be at most . Matches. ✓ (This is Cell B — a zero miss rate truncates the hierarchy at that point.)


Cell C — A useless cache (miss rate of one)

Forecast: If L2 never finds anything, is it doing anything except costing time?

  1. Innermost. Why this step? L3 and DRAM behaviour has not changed from Cell A, so the average cost of "reaching L3" is still cycles. We must re-establish this number because the outer levels below will need it as their miss penalty — even a broken L2 still hands its misses down to this same L3.

  2. Middle — full pass-through. Why this step? means every L2 lookup fails, so every one pays the full of the level below plus the wasted cycles it spent looking in L2. L2 became pure overhead.

  3. Outer.

Verify: Compare to a machine with no L2 at all (accesses go L1 → L3): . The useless L2 gives — it made things worse by exactly the wasted cycles. ✓ A cache with is worse than no cache. (This is Cell C.)


Cell D — A level is missing (2-level machine)

Forecast: Which of our three formulas do we grab? (Hint: count the caches, not the memory.)

  1. Use the two-level formula. Why this step? With no L3, an L2 miss goes straight to DRAM, so L2's miss penalty is itself — no middle doll.

  2. Inside-out. Why this step? This inner bracket is L2's average cost. L2 always costs to look in, and of accesses that reach it fall all the way through to DRAM's (there is no L3 to catch them), giving . This becomes L1's miss penalty.

  3. Outer.

Verify: Sanity check the magnitude, not by mangling the 3-level formula. (Beware the trap: reusing the 3-level expression by faking an L3 with and would give L3's cost as correctly, but faking it with and instead pays DRAM twice — which is wrong. The safe way to "remove L3" is to drop its bracket entirely, which is just the 2-level formula we used.) Instead: lies above the full 3-level (Cell A) and below the useless-L2 (Cell C). Adding a real L3 (Cell A) should help, and it does: . ✓ (This is Cell D.)


Cell E — Limiting behaviour (the two extremes)

Forecast: What is the floor? What is the ceiling? Guess the two numbers before reading.

  1. Best case, every . Why this step? If nothing ever misses L1, the term vanishes entirely. AMAT can never be less than — you always pay the first lookup.

  2. Worst case, every . Why this step? If every level misses, each access walks the whole chain, paying each hit time on the way plus the final DRAM. This is the absolute ceiling.

Verify: Cell-A's real answer obeys . Every AMAT you ever compute must live in . Any answer outside that range means an arithmetic error. ✓ (This is Cell E.)


Cell F — Solve backwards (given AMAT, find a hit rate)

Forecast: Cell A had giving . To lower AMAT to , must go up or down?

  1. Write the equation with unknown. Why this step? Everything except is fixed, so this is one linear equation in one unknown.

  2. Isolate . Why this step? Subtract the always-paid , then divide by the miss penalty. This "undoes" the formula.

  3. Convert to hit rate. Why this step? The question asked for the hit rate, but our formula is written in terms of the miss rate. By definition , so ; subtracting the miss rate from converts the quantity we solved for into the quantity the designer wants.

Verify: Plug back: . ✓ And it makes sense: to beat Cell-A's we needed a lower miss rate (), i.e. a slightly better L1. (This is Cell F.)


Cell G — Word problem (cycles to wall-clock seconds)

Forecast: means cycles per second. Will the cached version take seconds or milliseconds?

  1. Total cycles, cached. Why this step? AMAT has units cycles-per-access; multiply by the access count and the "access" unit cancels, leaving cycles.

  2. Cycles to seconds. Why this step? Dividing cycles by (cycles per second) leaves seconds — a clean unit check.

  3. No-cache comparison. Why this step? We repeat the exact same two-stage conversion (accesses × cycles-per-access, then ÷ cycles-per-second) but with the baseline cost of cycles per access instead of . Building the no-cache number in the same units is what lets us take an honest ratio in the Verify line.

Verify: Speedup , matching the parent's (the ratio is unit-free, so GHz cancels — good). ✓ (This is Cell G.)


Cell H — Exam twist: global vs local miss rate

Forecast: Is the local L2 miss rate bigger or smaller than ? (Only of accesses even reach L2…)

  1. Relate global and local. Why this step? An access misses L2 globally only if it first missed L1 (rate ) and then missed L2 (rate ). Probabilities of the two independent hurdles multiply. Solving gives the local — much bigger than , because its base is the small stream, not everyone. (This is exactly the "thinning stream" in the figure: the L3 box holds of the original width.)

  2. Now plug the local into the standard formula (inside-out). Why this step? Once is the correct local , this is identical to Cell A — proving the given global figure was just Cell A in disguise.

Verify: Global check the other way: . ✓ Had you wrongly used , you'd get — a different, wrong answer that would look plausible on an exam. (This is Cell H.)


Pull it together

Recall Which formula and which numbers?

What does the subscript in mean? ::: "Level 2's local miss rate" — the fraction of accesses that reached L2 which miss it. How many brackets does a 3-level cache formula have? ::: Three nested brackets (L1 outside, L2 middle, L3+DRAM inside). A level with does what to the levels below it? ::: Truncates them — their cost is multiplied by zero and never reached. A cache with compared to having no cache at all? ::: Worse — it adds its hit time as pure overhead with no benefit. The absolute minimum any AMAT can be? ::: , the L1 hit time you always pay. The absolute maximum? ::: , every level missing. Global miss rate relates to local how? ::: (product of local miss rates above and including it).

Prerequisites & neighbours: the hit/miss counts here come from Cache Organization and Cache Replacement Policies; the "shared L3" reasoning connects to Cache Coherence; miss rates depend on your Memory Access Patterns; and where these cycles fit in execution is CPU Pipeline and DRAM Architecture.