ยง19.6โ€“19.8Issue: Replacement Policy โ€ฆ Summary

Part I OSTEP pp. 208โ€“210 ยท ~5 min read

  • lru
  • tlb coverage

Two loose ends โ€” which entry to evict, and what a real TLB entry looks like โ€” then the bill of remaining worries.

19.6 Issue: Replacement Policy

Installing a new entry means evicting an old one.

The Crux: How To Design TLB Replacement Policy

Which TLB entry should be replaced when we add a new one? The goal: minimize the miss rate and improve performance.

The usual first answer is least-recently-used (LRU) โ€” exploit locality, evict the entry idle longest. The usual second answer is random โ€” simple, and immune to corner cases. Here is the corner case, live:

LRU, ambushed: 5 pages, 4 slots. Click the presets left-to-right, then again โ€” every single access misses.

virtual address

0

VPN 0 ยท offset 0

โ†’

MMU: TLB first, page table only on a miss

no access yet โ€” click a preset (or โ–ถ access)

โ†’
โ€ฆ
TLB slotVPNPFNvalidnote
0โ€”โ€”0
1โ€”โ€”0
2โ€”โ€”0
3โ€”โ€”0

Loop over n+1 pages with an n-entry TLB and LRU self-destructs: inserting page 4 evicts page 0 โ€” exactly the page the loop needs next; fetching page 0 evicts page 1; and so on, forever. The "reasonable" policy behaves quite unreasonably. Watch the trace: M M M M M M M M M M.

Same trap, random replacement (seeded): click the same tour twice โ€” some accesses survive.

virtual address

0

VPN 0 ยท offset 0

โ†’

MMU: TLB first, page table only on a miss

no access yet โ€” click a preset (or โ–ถ access)

โ†’
โ€ฆ
TLB slotVPNPFNvalidnote
0โ€”โ€”0
1โ€”โ€”0
2โ€”โ€”0
3โ€”โ€”0

Random sometimes evicts a page the loop won't need for a while โ€” so hits leak through where LRU managed zero. Its virtues: simplicity, and immunity to exactly this kind of corner-case rhythm.

(Deeper study of replacement policies waits for the swapping chapters, where the same question returns with disks attached.)

19.7 A Real TLB Entry

The MIPS R4000 โ€” a modern, software-managed design. Its (slightly simplified) 64-bit entry, Figure 19.4:

Figure 19.4: a MIPS TLB entry (hover the fields; gray = unused)

VPN (19)GASID (8)ยทยทยท
PFN (24)CDVยทยทยท

Details worth savoring: only 19 VPN bits (user code owns just half the 32-bit space); a 24-bit PFN (64GB of physical memory); a global bit that bypasses the ASID for pages shared by everyone; a page mask (not shown) enabling multiple page sizes. MIPS TLBs hold 32โ€“64 entries, a few of them wired โ€” a register tells the hardware how many slots to reserve for OS translations that must never miss (the miss handler, above all). And since the TLB is software-managed, there are instructions to manage it:

The four (privileged!) MIPS TLB instructions
what it does
TLBPprobe: is a particular translation in the TLB?
TLBRread a TLB entry into registers
TLBWIwrite (replace) a specific indexed entry
TLBWRwrite (replace) a random entry
Dotted-underlined cells have explanations โ€” click one.

Privileged, of course โ€” imagine what a user process could do if it could write TLB entries (hint: just about anything, including taking over the machine, running its own malicious โ€œOSโ€, or making the Sun disappear).

Tip: RAM Isnโ€™t Always RAM (Cullerโ€™s Law)

Random-access memory implies every access costs the same. But if the pages you touch exceed what your TLB maps, โ€œrandomโ€ access turns expensive โ€” the machine quietly walks page tables beneath you. Named for David Culler, who pointed at the TLB as the source of many a performance mystery.

19.8 Summary

The TLB makes the common case โ€” a cached translation โ€” cost almost nothing, so programs mostly run as if memory werenโ€™t virtualized at all: an excellent achievement, and essential to pagingโ€™s viability. Two clouds remain. Exceed the TLB coverage โ€” touch more pages in a short window than the TLB can map โ€” and misses dominate; one remedy is larger pages (a favorite of DBMSs with big, randomly-accessed structures), which the next chapterโ€™s machinery accommodates. And the TLB sits on the CPUโ€™s critical path: with a physically-indexed cache, translation must finish before the cache lookup starts, which is why architects flirt with virtually-indexed caches and their own attendant headaches.

Homework (Measurement): tlb.c

Measure your own TLB ร  la Saavedra-Barrera: touch one int per page across N pages, time millions of iterations, and plot time-per-access as N grows. Jumps in the curve reveal the hierarchy โ€” on the bookโ€™s machine, ~5ns until โ‰ˆ16 pages (L1 TLB), ~20ns until โ‰ˆ512 (L2), ~70ns beyond: a 14ร— swing. Mind the traps: timer precision, compilers deleting your loop, CPU migration (pin the thread!), and demand-zero first touches. Get it at ostep-homework.

Check yourself

1.A program loops repeatedly over 5 pages; the TLB holds 4 entries. Compare LRU and random replacement.

2.The MIPS R4000 supports a 32-bit address space with 4KB pages, yet its TLB entry has only a 19-bit VPN. Where did the missing bit go?

3.What is the MIPS wired register for?

4.MIPS exposes TLBP, TLBR, TLBWI, and TLBWR to manage TLB contents. Why must these be privileged instructions?

5.The homework's measurement (Fig 19.5) shows access times stepping 5ns โ†’ 20ns โ†’ 70ns as the touched-page count grows past ~16 and ~1024. What's the reading?

5 questions