5.4.12 · D4Memory Hierarchy & Caches

Exercises — TLB (translation lookaside buffer)

2,894 words13 min readBack to topic

Reference numbers used throughout (unless a problem overrides them):

Before touching a single problem, see what that formula means. The picture below breaks the EMAT into three stacked blocks: the pale lavender block you pay on every access (searching the TLB), the butter block you also pay on every access (the final data read), and the thin coral block — the miss penalty — which is present only for the miss fraction. Notice the coral block shrinks toward nothing as : that visual shrinking is the whole reason the TLB helps.

Figure — TLB (translation lookaside buffer)

Every access also has to decide hit vs. miss, and a miss is not the end of the story — it can end in a clean fill, or in a page fault if the page isn't in memory at all. That branching is easiest to follow as a flowchart:

hit

miss

yes

no

yes

no

Split VA into VPN and offset

VPN in TLB?

Read PFN from TLB

Walk the page table

PTE valid bit set?

Insert VPN to PFN into TLB

Raise page fault to OS

OS loads page from disk

Permission bits OK?

Form PA and access memory

Raise protection fault

Keep this flowchart in mind: the valid-bit branch (E) is the case most students forget, and it is the subject of Exercise 3.4.


Level 1 — Recognition

Exercise 1.1 — Splitting an address

A system uses pages and 32-bit virtual addresses. For the virtual address 0x00012ABC, state (a) how many offset bits, (b) the offset value, (c) the VPN.

The bit-field picture below shows exactly where the cut falls: the top 20 bits are the VPN (lavender), the bottom 12 bits are the offset (mint), and the vertical dashed line is the boundary at bit 11/12.

Figure — TLB (translation lookaside buffer)
Recall Solution 1.1

(a) bytes, so the offset needs 12 bits to name each byte inside the page. (b) The offset is the low 12 bits. 0x00012ABC in the bottom 3 hex digits is ABC — but 3 hex digits = 12 bits exactly, so offset = 0xABC. (c) The VPN is everything above bit 11 → the high 20 bits = 0x00012. Why split this way? Paging never moves a byte within its page, so the low bits pass through untranslated; only the page number is looked up.

Exercise 1.2 — What lives in a TLB entry?

Which of these belong inside a single TLB entry? (VPN, PFN, the page offset, valid bit, permission bits, the ASID tag, the whole page's data.)

Recall Solution 1.2

A TLB entry stores: VPN (the tag/key), PFN (the answer), a valid bit, permission bits (read/write/execute, user/kernel), and — on designs that avoid flushing — an ASID (Address-Space Identifier) tag naming which process owns the mapping. It does NOT store the page offset (that comes from the address itself, untranslated) and it does NOT store the page's data (that's in cache/DRAM, not the TLB). Why the ASID field matters: without it, VPN 0x5 for process A is indistinguishable from VPN 0x5 for process B, forcing a full flush on every Context Switch. The tag lets both entries live side by side (used in Exercise 4.2).


Level 2 — Application

Exercise 2.1 — Build the physical address

Given VPN 0x00012 → PFN 0x0037 in the TLB, , translate VA = 0x00012ABC to a physical address.

Recall Solution 2.1
  1. Split: offset = 0xABC, VPN = 0x00012 (from Exercise 1.1).
  2. Lookup: VPN 0x00012 is present → hit, PFN = 0x0037.
  3. Form PA: . Shifting 0x0037 left by 12 bits = 0x00037000 (three hex zeros appended = 12 bits). OR with 0xABCPA = 0x00037ABC. Why shift by 12? The PFN names a whole 4KiB frame; its base byte address is , which is exactly a left shift by 12.

Exercise 2.2 — EMAT plug-in

ns, ns, single-level walk (), hit rate . Find the EMAT.

Recall Solution 2.2

Extra miss cost ns. . Why this shape? You always search the TLB () and always do one final data read (); the walk is the only extra cost and it only happens on the of accesses that miss. This is exactly the three-block picture from the top of the page with .


Level 3 — Analysis

Exercise 3.1 — Multi-level walk hurts more

Same system as 2.2 (, ) but a 4-level page table () and . Compare EMAT to the single-level case at the same hit rate.

Recall Solution 3.1

Single-level (): extra → EMAT ns. Four-level (): extra → EMAT . Analysis: the miss fraction is identical (2%), but each miss now costs the walk work. So deeper page tables multiply the penalty per miss, which is exactly why high matters more on x86-64.

Exercise 3.2 — Solve for the required hit rate

You want ns with , , . What minimum hit rate do you need?

Recall Solution 3.2

Set up: . . Minimum hit rate = 96%. Why invert the formula? EMAT rises as falls; the constraint is an inequality on the miss fraction , so we isolate it and read off the boundary.

Exercise 3.3 — Speedup vs. no TLB

With , , , single-level walk, compare against a machine with no TLB (every access pays one page-table read + one data read ). What is the speedup?

Recall Solution 3.3

No TLB: every access ns. With TLB: ns. Speedup . Reading it: locality concentrates accesses on a few pages → high → the TLB nearly halves access time here.

Exercise 3.4 — The miss that isn't just a walk (valid-bit edge case)

Follow the flowchart's branch E. A memory access misses in the TLB, so hardware walks the page table — and finds the PTE's valid bit = 0, meaning the page is not resident in physical memory. Times: ns, single walk ns (), and servicing a Page Fault from disk costs ns (5 ms). After the OS loads the page it re-walks (another ns) and does the data read (ns). What is the total time for this one access, and how many times larger is it than an ordinary TLB hit (ns)?

Recall Solution 3.4

Walk down the flowchart branch by branch:

  1. TLB search: ns.
  2. First walk finds valid bit = 0: ns.
  3. Page fault to disk (branch E → G → H): ns.
  4. Re-walk after the page is resident: ns.
  5. Data read: ns.

Total . Ratio vs. a hit . The point: a TLB miss and a page fault are different quadrants. Most misses stop at step 2 (valid bit = 1 → clean fill, ~100ns). The valid-bit-zero branch is the rare, catastrophic one — five million nanoseconds — because it hits the disk. The EMAT formula only models the common (resident) miss; page faults are a separate, much rarer term.


Level 4 — Synthesis

Exercise 4.1 — Two-level TLB EMAT

A design has an L1 TLB (search ns) and an L2 TLB (search ns). L1 hit rate . Of the L1 misses, the L2 catches . An L2 miss triggers a full walk with , . On any access you also pay one final data read . Ignore when L1 hits (parallel/short-circuit). Compute EMAT.

Recall Solution 4.1

Build cost by cases, then always add the final data read :

  • L1 hit (prob ): translation cost .
  • L1 miss, L2 hit (prob ): cost .
  • L1 miss, L2 miss (prob ): cost .

Expected translation cost: Add the data read: . Why two levels? A tiny fast L1 keeps the common case cheap; a bigger L2 rescues most L1 misses without paying the 400ns walk — same layering idea as data caches.

Exercise 4.2 — Flush vs. ASID cost after a context switch

On a Context Switch, design A flushes the TLB; design B uses ASIDs — the Address-Space Identifier field from Exercise 1.2 that tags each entry with its owning process, so no flush is needed. After a switch, the returning process makes accesses. With ASIDs its warm hit rate is . After a flush the TLB is cold: for these accesses the average hit rate drops to (miss storm while it refills). Using , , , how much total time does ASID save over flush across these 1000 accesses?

Recall Solution 4.2

Per-access EMAT:

  • ASID: ns.
  • Flush: ns. Over accesses:
  • ASID total ns.
  • Flush total ns. Savings ns per switch window. Synthesis point: the flush's damage is the cold-start miss storm — the cost isn't the flush instruction itself but the many walks that follow. The ASID tag lets entries survive the switch, avoiding refill.

Level 5 — Mastery

Exercise 5.1 — When is a bigger TLB actually worse?

A small TLB has ns and hit rate . A big TLB raises the hit rate to but, being on the critical path of every access, its search time rises to ns. With , , which wins on EMAT?

Recall Solution 5.1

Small: ns. Big: ns. Big TLB wins here (103 < 106). But notice the tension: the big TLB saved ns on the walk term yet added ns to on every single access. If its search time had risen to, say, ns, it would lose: . Mastery point: every nanosecond added to is paid unconditionally; walk savings are paid only on the miss fraction. Bigger only wins while .

Exercise 5.2 — Derive the break-even search-time penalty

Generalise 5.1. A bigger TLB improves the hit rate from to but adds ns to the search time. Derive the largest for which the bigger TLB still helps, in terms of . Evaluate for .

Recall Solution 5.2

Break-even is where both EMATs are equal: Cancel the common : Evaluate: . Interpretation: with a deep 4-level walk, the walk penalty is big, so you can afford up to 16ns of extra search time before the bigger TLB stops paying off. Deeper page tables → more headroom for larger TLBs. This is exactly why real CPUs pair deep page tables with multi-level TLBs.

The trade-off in Exercises 5.1 and 5.2 is easiest to see as a graph. The figure below plots the small-TLB EMAT (a flat line — it never changes) against the big-TLB EMAT (which rises as its extra search time grows). Read off exactly where the rising line crosses the flat one: that crossing is , and the shaded region to its left is where the bigger TLB wins.

Figure — TLB (translation lookaside buffer)