S424 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S4 · Lesson 24 · Virtual memory and TLBs

Page tables, multi-level walks, TLB reach, huge pages, virtually indexed caches and page faults

Every load and store a program issues uses a virtual address, which the hardware translates to a physical one through page tables the operating system maintains. That translation gives each process its own address space, protects them from each other and lets memory spill to disk, but it would double or quintuple the cost of every access if done naively, since the page tables themselves live in memory. A small cache of recent translations, the TLB, makes the cost almost disappear, as long as the program's working set fits within the TLB's reach. When it doesn't, huge pages and page-walk caches come to the rescue.

  • TLB reach, the memory a TLB can map at once (entries × page size), against page size, on log scales. Programs whose working set exceeds the reach take a page walk on many accesses.
  • With 4 KB pages, a 64-entry TLB reaches only 256 KB, smaller than many L2 caches. With 2 MB huge pages the same TLB reaches 128 MB.
  • Each page-table level resolves log2⁡(page/8) bits with 8-byte entries, so bigger pages also mean shorter page walks.

Predict

A 48-bit virtual address space uses 4 KB pages and 8-byte page table entries. How large would a single flat page table be?

Name it

Translation

A virtual address splits into a page number and an offset of log2⁡(page size) bits. The page table maps page numbers to physical frames, with permission bits (read, write, execute, user) checked on every access.

  • A flat table needs 2V−offset entries.
  • A multi-level (radix) table resolves log2⁡(page/PTE size) bits per level, so it has ⌈V−offsetlog2⁡(page/PTE)⌉ levels.

The TLB

A small, usually fully associative cache of translations. Reach = entries × page size. A miss triggers a page walk (in hardware on x86, ARM and RISC-V), up to one memory access per level, helped by page-walk caches. With TLB hit rate h, TLB time tT, memory time tM and L levels:

t=tT+tM+(1−h)LtM

Huge pages (2 MB, 1 GB) raise reach and remove levels from the walk.

Caches and translation

A virtually indexed, physically tagged (VIPT) L1 looks up its set with untranslated offset bits while the TLB translates in parallel. That works only if the index and line offset fit in the page offset: each way can be at most one page, so the cache is at most page size × ways. That's why L1 caches are 32 to 48 KB with 8 to 12 ways and 4 KB pages.

Page faults

A missing page costs milliseconds to fetch from storage. With fault rate p, fault time F and memory time tM, the average access is (1−p)tM+pF, so p must be tiny.

Worked example

For 48-bit virtual addresses, 4 KB pages and 8-byte entries: how many levels, and what does a 64-entry TLB reach with 4 KB and with 2 MB pages?

Show the solution
  1. Bits per level. log2⁡(4096/8)=9bits
  2. Levels. The page number has 48 − 12 = 36 bits: 36/9=4levels
  3. Reach with 4 KB pages. 64×4KB=256KB
  4. Reach with 2 MB pages. 64×2MB=128MB and the walk has one level fewer (21-bit offset, 27 bits left, 3 levels).

Answer 4 levels; the TLB reaches 256 KB with 4 KB pages and 128 MB with 2 MB pages.

Worked example

Memory takes 100 ns and a page fault 10 ms. What fault rate keeps the slowdown under 10%? And can a 64 KB, 8-way L1 be VIPT with 4 KB pages?

Show the solution
  1. Fault rate. The slowdown stays under 10% if pF<0.1tM: p<10ns10ms=10−6 under one fault per million accesses.
  2. Size of a way. 64KB/8=8KB per way more than a 4 KB page, so one index bit would come from the translated part.
  3. What it would take. It would need 16 ways, or tricks to handle aliases.

Answer the fault rate must stay under 10−6; the cache can't be plain VIPT with 8 ways (it would need 16 ways, or alias handling).

Worked example

A TLB lookup takes 1 ns, memory 100 ns, and a miss needs a 4-level walk. What is the average access time at 99% and 99.9% TLB hit rates?

Show the solution
  1. Average access time. t=tT+tM+(1−h)LtM
  2. At 99%. t=1+100+0.01×4×100=105ns
  3. At 99.9%. t=1+100+0.001×4×100=101.4ns
  4. Why huge pages matter. A 1% miss rate costs 4%, which is why huge pages, by cutting both the miss rate and the walk length, matter so much for big-memory programs.

Answer 105 ns at 99% and 101.4 ns at 99.9%.

Check

A TLB has 64 entries for 4 KB pages. What's its reach?

Check

A 48-bit virtual address space uses pages of 212 bytes and 8-byte page table entries, with each table one page in size. How many levels does the page table need?

Check

How big would a flat page table be for a 39-bit address space with 212-byte pages and 8-byte entries?

Check

A TLB lookup takes 1 ns and memory 80 ns. The TLB hits 0.995 of the time; a miss walks 4 levels, each a memory access. What's the average time for a memory access?

Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.

Check

With 4 KB pages, how large can a 4-way VIPT cache be without aliasing?

Check

Memory takes 60 ns and a page fault 5 ms. What's the largest fault rate per access that keeps the slowdown within 0.2?

Check

A machine has 48-bit virtual addresses, 4 KB pages, 8-byte page-table entries and a 1536-entry TLB with a 0.995 hit rate; memory takes 100 ns and the TLB 1 ns.

(a)

How many levels does its page table need?

(b)

What is the TLB's reach?

(c)

What is the average access time?

Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.

Check

Memory takes M seconds and a page fault F; faults happen with probability p per access.

Step 1

What is the average access time?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why do huge pages speed up programs with large working sets?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.