4.2.26Operating Systems

Multi-level page tables — why, overhead

1,819 words8 min readdifficulty · medium3 backlinks

WHY do we even need this?

WHAT goes wrong? Let's compute the size of a flat table.


HOW multi-level fixes it

Figure — Multi-level page tables — why, overhead

Overhead: space vs time


Common mistakes


Flashcards

Why is a single-level page table wasteful for a 32-bit space?
It needs one PTE per virtual page (2202^{20} entries = 4MB per process) even though most pages are unused.
Size formula for a single-level table?
2Vp×E2^{V-p}\times E bytes, where VV=addr bits, p=log2(page size)p=\log_2(\text{page size}), EE=PTE size.
Why does multi-level save space?
Unused regions are a single "not present" entry in the directory, so their second-level tables are never allocated (sparsity → savings).
How do you split a virtual address for kk levels?
Add the bits: V=(L1 bits)++(Lk bits)+pV = (\text{L1 bits})+\dots+(\text{Lk bits})+ p (offset).
Why choose 10/10/12 for 32-bit, 4KB, 4B PTE?
So each second-level table (1024×4B = 4KB) fits exactly in one page; remaining bits go to directory and offset.
Time cost of a kk-level table per memory reference?
k+1k+1 accesses (kk for the walk, 1 for the data) — without a TLB.
What hardware hides the walk cost?
The TLB, which caches VPN→PFN; a hit skips the page-table walk.
Worst-case space vs single-level?
Slightly larger — you add directory page(s) on top of all leaf tables; multi-level only wins when sparse.
EAT formula with TLB hit rate hh?
h(tTLB+tmem)+(1h)((k+1)tmem+tTLB)h(t_{TLB}+t_{mem}) + (1-h)((k{+}1)t_{mem}+t_{TLB}).

Recall Feynman: explain to a 12-year-old

Imagine a giant hotel with a million rooms, and one huge guest list with a line for every room — even empty ones. That list is enormous and mostly blank. Instead, we keep a small floor directory: "Floor 3 has guests, here's its list; Floor 4 is empty — no list at all." To find a person you check the floor directory, then that floor's small list. You only keep lists for floors that actually have people. Looking someone up takes a couple of extra steps, so we keep a sticky-note (the TLB) of the people we looked up recently to go faster.

Connections

Concept Map

size formula

32-bit example

scaled by processes

motivates

attacked by

top bits index

present entry points to

unused chunk becomes

skips allocating

requires

choose bits so

Flat single-level table

Table size = 2^V-p x E

4 MB per process

Mostly unused space

Used pages cluster in regions

Multi-level page tables

Page directory L1

Second-level tables L2

Not-present entry

VPN split into stages

L2 fits one page b = p - log2 E

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, problem yeh hai: ek 32-bit process ke paas 4GB virtual space hota hai. Agar hum ek hi flat page table banayein, toh har page ke liye ek entry chahiye — total 2202^{20} entries, yaani 4MB sirf table ke liye, har process ke liye! Aur sach yeh hai ki process apne space ka bahut chhota hissa hi use karta hai, baaki table khaali padi rehti hai. Yeh memory ka waste hai.

Multi-level page table is waste ko khatam karta hai. Hum virtual address ke bits ko todte hain: upar ke bits directory (L1) ko index karte hain, beech ke bits second-level table (L2) ko, aur neeche ke bits offset hote hain. Agar koi region use hi nahi ho raha, toh directory mein bas "not present" likh do — uska L2 table banane ki zaroorat hi nahi. Isi sparsity se memory bachti hai. 32-bit ke liye split hota hai 10+10+12, taaki har L2 table exactly ek 4KB page mein fit ho jaye.

Lekin har cheez free nahi hoti. Ab ek address translate karne ke liye 2 extra memory reads lagte hain (L1, L2), phir actual data read — total 3 accesses ek ke badle. Yeh time overhead hai. Iska solution hai TLB: yeh recent translations ka cache hai. Agar TLB hit ho gaya, toh walk skip ho jaata hai aur speed wapas aa jaati hai. Isiliye locality important hai — agar program same pages baar-baar touch karta hai, TLB hit rate high rehta hai aur overhead almost zero ho jaata hai.

Yaad rakho: multi-level space bachata hai sirf jab table sparse ho. Agar pura space use ho, toh thoda zyada memory lagti hai (extra directory). Real life mein space hamesha sparse hota hai, isliye yeh approach jeet jaati hai — x86-64 toh 4 levels use karta hai!

Go deeper — visual, from zero

Test yourself — Operating Systems

Connections