5.4.12 · D3 · HinglishMemory Hierarchy & Caches

Worked examplesTLB (translation lookaside buffer)

3,510 words16 min read↑ Read in English

5.4.12 · D3 · Hardware › Memory Hierarchy & Caches › TLB (translation lookaside buffer)

Prerequisites jo hum use karte hain: Virtual Memory, Page Table, Cache (memory hierarchy), Context Switch, Page Fault, Locality of Reference.


Scenario matrix

Kuch bhi solve karne se pehle, chalte hain har us cheez ki list banate hain jo ek TLB question mein aa sakti hai. Ise poore deck of cards ki tarah socho; neeche ke examples har card ko kam se kam ek baar khelate hain.

# Scenario class Kya alag banata hai ise Covered by
A TLB hit translation already cached → no walk Ex 1
B Address bit-split VA ko VPN + offset mein todna non-obvious page sizes ke liye Ex 2
C TLB miss, page present page table walk karo, page RAM mein hai Ex 3
D TLB miss → page fault page RAM mein nahi, disk load chahiye Ex 4
E EMAT single-level performance maths, Ex 5
F EMAT multi-level walk , x86-64 ka case Ex 6
G Context switch (flush vs ASID) stale/wrong-process entries Ex 7
H Degenerate / limiting inputs , , Ex 8
I Real-world word problem array pe loop, locality in action Ex 9
J Exam twist given EMAT, ulta solve karo ke liye Ex 10

Har cell A–J neeche kaam kiya gaya hai. Chalte hain.


Reminders jo baar baar kaam aayenge (taaki koi symbol unexplained na rahe)

Figure — TLB (translation lookaside buffer)

Figure dekho: address ek ruler hai. Bit position pe ek vertical red line ise saaf baant deti hai. Line ke upar kuch bhi kabhi within-page position nahi badalta; line ke neeche kuch bhi kabhi translate nahi hota.


Ex 1 — Scenario A: ek clean TLB hit

Step 1 — kitne offset bits? , toh offset bits. Ye step kyun? 12 bits = exactly 3 hex digits (har hex digit 4 bits ka hota hai), jo split ko hex boundary pe laata hai — acha lagta hai.

Step 2 — VA slice karo. Low 3 hex digits = offset = 0x2C4. Remaining top = 0x1F0AB = VPN. Kyun? Lowest bits definition se offset hain; hex digits.

Step 3 — VPN ko TLB mein lookup karo. 0x1F0AB present hai → hit, PFN = 0x0072. Kyun? Ye scenario A hai: cached translation, toh hum page table bilkul skip karte hain.

Step 4 — PA rebuild karo. . 12 se shift kyun? PFN 0x72 ek 4KiB frame ko name karta hai; uska base byte address hai, aur 12 bits left shift karna wohi multiply hai. Phir hum untranslated offset ko emptied low bits mein OR karte hain.

Recall Verify

Offset VA aur PA mein unchanged hai? VA low = 2C4, PA low = 2C4. ✓ ; OR 0x2C4 deta hai 0x722C4. ✓ Koi walk nahi hua.


Ex 2 — Scenario B: ek awkward page size (bit-split care)

Step 1 — offset bits. , toh offset bits. Kyun? Wahi rule: . Lekin 13, 4 ka multiple nahi hai — toh split hex boundary pe nahi padti. Trap!

Step 2 — VPN width. VPN bits bits. Kyun? Total address bits minus offset bits = page number ke liye bacha hua sab kuch.

Step 3 — offset mask banao, phir offset extract karo. Low 13 bits rakhne ke liye hum ek mask se AND karte hain jisme thirteen 1-bits hain. Woh mask hai . Compute karo: , toh , aur (chaar hex digits 1FFF = binary 1 1111 1111 1111, jo exactly thirteen 1s hain). Ab 0xABCD binary mein (16 bits). 0x1FFF se AND karna low 13 bits rakhta hai — bit 13 (value 0x2000) aur usse upar drop ho jaate hain, bit 12 se neeche sab survive karta hai. 0xABCD ke bits 15–13 hain 101, toh wo clear ho jaate hain; surviving low 13 bits hain 0 1011 1100 1101 = 0x0BCD. Kyun? Offset = lowest 13 bits; ek mask "low bits rakhne" ki standard trick hai kyunki ye bit 13 ke neeche all-ones aur upar all-zeros hota hai.

Step 4 — VPN extract karo. Shift the whole VA right by 13: 0xABCD >> 13 = 0x5 (sirf top 3 bits 101 bachte hain). Kyun? VPN = offset ke upar sab kuch, yaani VA divided by .

Recall Verify

Mask: . ✓ 0xABCD & 0x1FFF = 0x0BCD (offset) aur 0xABCD >> 13 = 0x5 (VPN). Reassemble karo: . ✓ Round-trip original VA se match karta hai.


Ex 3 — Scenario C: TLB miss, page memory mein hai

Step 1 — TLB lookup fail hoti hai. VPN 0x0055 absent → TLB miss. Kyun? Scenario C: translation simply abhi tak cached nahi tha (cold entry ya evicted).

Step 2 — page-table walk. Hardware DRAM mein page table entry padhta hai → PFN 0x0300, valid = 1. Ye step kyun? Mapping page table mein tab bhi rehti hai jab TLB use bhool jaata hai — TLB ek cache hai, sach ka source nahi (Page Table hai).

Step 3 — no fault, page present. Valid bit = 1 → page physical RAM mein hai. Koi Page Fault nahi, koi disk nahi. Kyun? Ye doosre parent mistake ka poora point hai: ek TLB miss ek fast event hai (ek ya zyada DRAM reads), na ki ek slow wala (disk).

Step 4 — fill aur retry. TLB mein 0x0055 → 0x0300 insert karo, phir access re-run karo → ab hit hoti hai. Kyun? Locality of Reference: is page ke liye agla access ab hit hoga — walk ek baar bharo, kai baar faida uthao.

Recall Verify

Is access ki cost = TLB search + 1 walk read + data read = kuch ns. Koi milliseconds (disk) kahin nahi. ✓


Ex 4 — Scenario D: TLB miss jo page fault ban jaata hai

Step 1 — TLB miss → walk. VPN 0x0400 TLB se absent → TLB miss, toh hardware page-table walk shuru karta hai. Kyun? Kisi bhi miss pe TLB ke paas offer karne ke liye koi translation nahi hoti, aur authoritative mapping sirf Page Table mein DRAM mein rehti hai — toh hume frame jaanne ke liye (ya ki koi nahi hai) page table entry padhni padti hai.

Step 2 — valid bit = 0. Page-table entry kehti hai: RAM mein resident nahi. Ye OS ko ek page fault trap raise karta hai. Kyun? Valid bit woh flag hai jo "RAM mein" aur "disk pe / kabhi allocate nahi hua" ke beech farq karta hai.

Step 3 — OS fault handle karta hai. OS ek free (ya evicted) frame chunta hai, disk se page ko usme padhta hai, page table entry update karta hai (valid = 1, naya PFN), phir return karta hai. Kyun? Sirf software disk layout aur eviction policy jaanta hai; hardware ye nahi kar sakta.

Step 4 — retry, ab TLB miss → walk → hit. Faulting instruction restart hoti hai; is baar walk succeed hoti hai aur TLB fill hoti hai.

Step 5 — cost comparison. DRAM access ≈ 100 ns; disk access ≈ 10 ms ns. Ratio Ex 3 walk se slower. Ye kyun dikhayein? "TLB miss ≠ page fault" ko quantitative banana ke liye: ek nanoseconds ka hai, doosra hundred-thousandfold worse hai.

Recall Verify

; . ✓


Ex 5 — Scenario E: EMAT, single-level walk

Step 1 — formula mein plug karo. . Ye form kyun? Tum hamesha TLB search + ek data read bharte ho; sirf variable extra walk hai, miss fraction pe charge hota hai.

Step 2 — extra term evaluate karo. ns. Kyun? 5% accesses walk karte hain, har ek ns cost karta hai; average extra ns.

Step 3 — total. ns.

Step 4 — no TLB se compare karo. TLB ke bina kuch search karne ki zaroorat nahi, toh ; aur har access zaroor walk karta hai, effectively . Formula reduce hota hai: Speedup . Kyun? Dikhata hai ki TLB ek modest 95% hit rate pe bhi access time almost half kar deta hai — locality ka faida milta hai.

Recall Verify

. ✓ . ✓


Ex 6 — Scenario F: EMAT 4-level walk ke saath (x86-64)

Step 1 — same formula, naya . . Kyun? Sirf walk length badi: ek 4-level walk, 1 ki jagah 4 DRAM reads hai.

Step 2 — extra term. ns. Kyun? 5% misses mein se har ek ab ns walking cost karta hai; sab accesses pe average: ns.

Step 3 — total. ns.

Step 4 — Ex 5 se contrast. Single-level = 106ns, four-level = 121ns. Usi 5% miss rate se ab zyada nuksan hota hai (20ns vs 5ns extra). Kyun matter karta hai: deeper page tables TLB ko zyada valuable banate hain, kam nahi — isliye real CPUs L2 TLBs add karte hain.

Recall Verify

. ✓ Extra term ratio . ✓


Ex 7 — Scenario G: context switch, flush vs ASID

Step 1 — khatara. VPN 0x5 A vs B mein alag frames mean karta hai. Ek stale A-entry B ko PFN 0x0100 pe bhej degi — B A ki memory padhta hai. Ek correctness disaster. Kyun? TLB ko koi idea nahi ki uske paas kiska address space hai jab tak hum batayein nahi.

Step 2 — (i) koi protection nahi. TLB abhi bhi A ka 0x5 → 0x0100 hold karta hai → 0x0100 return karta hai. GALAT. B ko A ka data milta hai. Ye kyun dikhayein? Ye woh failure hai jisko rokne ke liye baaki do options exist karte hain.

Step 3 — (ii) switch pe flush. Switch pe, OS poora TLB clear karta hai. B ka 0x5 pe access ab miss karta hai, B ki page table walk karta hai, sahi 0x0900 milta hai. Kyun? Sahi hai — lekin cold TLB har switch ke turant baad ek miss storm cause karta hai. Context Switch dekho.

Step 4 — (iii) ASID tags. Ek ASID (address-space identifier) ek chhota number hai jo OS har process ke address space ko assign karta hai. Har TLB entry us process ke ASID ke saath tagged hoti hai jis ki woh hai. A ki entry ASID=A tagged hai; B ASID=B ke saath search karta hai, match nahi karta, miss karta hai, walk karta hai, aur 0x5[B] → 0x0900 insert karta hai. Dono entries coexist karti hain. Kyun? Koi flush nahi → A ki entries tab ke liye survive karti hain jab hum switch back karein → kam misses. Modern win.

Recall Verify

B ke liye sahi frame = 0x0900. Options (ii) aur (iii) 0x0900 return karte hain; option (i) 0x0100 return karta hai (galat). ✓


Ex 8 — Scenario H: degenerate & limiting inputs

Step 1 — (a) . ns. Kyun? Kabhi koi miss nahi → walk term vanish ho jaata hai. Tum abhi bhi 1ns TLB search + 100ns data read bharte ho.

Step 2 — (b) . ns. Kyun? Har access miss karta hai → har access 4 reads walk karta hai. Ye worst case hai — TLB pure overhead hai (extra 1ns) plus har baar ek full walk.

Step 3 — (c) ideal TLB, , . ns. Kyun? Ek free, always-hitting TLB access time ko exactly ek raw memory read tak reduce kar deta hai — theoretical floor. Tum "bas data ek baar padhne" se beat nahi kar sakte.

Step 4 — range ki sanity. Real EMAT floor (100ns) aur ceiling (501ns) ke beech rehta hai; Ex 6 ka 121ns floor ke karib baitha hai, confirm karta hai ki ek accha hit rate best case ke saath chipka rehta hai.

Recall Verify

(a) . (b) . (c) . ✓ Aur . ✓


Ex 9 — Scenario I: real-world word problem (loop mein locality)

Step 1 — total accesses. bytes; 4 bytes per int par → accesses. Kyun? Har int read pe ek memory access.

Step 2 — accesses per page. Har 4KiB page mein ints aate hain. Kyun? bytes per page bytes per int.

Step 3 — misses count karo. Har page ke pehle access pe miss hoti hai (cold, TLB mein abhi nahi); usi page ke baaki 1023 accesses hit karte hain. 4 pages ke saath ye deta hai misses total. Kyun? Ye Locality of Reference concrete bana ke: ek miss 1024 baar usi translation ke use ka "payment" karta hai (spatial locality).

Step 4 — hit rate. (lagbhag 99.90%). Kyun? Total accesses mein se hits. Sequential scans TLB ke best friend hain.

Recall Verify

accesses; per page; misses ;


Ex 10 — Scenario J: exam twist (ulta solve karo)

Step 1 — EMAT likho aur miss term isolate karo. . Always-paid part subtract karo: . Kyun? har access pe bhara jaata hai; bacha hua ns walk overhead hona chahiye.

Step 2 — solve karo. . Kyun? factor divide out karo.

Step 3 — solve karo. (86% hit rate). Kyun? Miss rate hai, toh hit rate uska complement hai.

Step 4 — confirm karne ke liye plug back karo. ns. ✓ Kyun? Inverse verify karna: substitute karna given EMAT exactly reproduce karta hai.

Recall Verify

; . ✓


Cover-check: kya humne har cell fill kiya?

Scenario matrix

A hit Ex1

B bitsplit Ex2

C miss present Ex3

D page fault Ex4

E EMAT L1 Ex5

F EMAT L4 Ex6

G ctx switch Ex7

H limits Ex8

I loop Ex9

J backward Ex10

Har card A–J kheela gaya. Space mein kuch bhi unshown nahi chuta.