Worked examples — Multi-level cache hierarchy (L1 - L2 - L3)
5.4.8 · D3· Hardware › Memory Hierarchy & Caches › Multi-level cache hierarchy (L1 - L2 - L3)
Yeh page ek kaam ke liye hai: practice karo jab tak koi bhi case surprise na kar sake. Parent note ne AMAT formula banaya. Yahan hum har tarah ke numbers uthate hain jo woh formula throw kar sakta hai — perfect caches, useless caches, missing levels, real workloads, aur exam traps.
Neeche ka sab kuch ek recursive idea par tikaa hai, toh koi bhi symbol aane se pehle use plain words mein dobaara bol lete hain.
Pieces ko ek baar name kar lete hain (parent ne yeh define kiye hain; hum inhe rakhte hain taaki line one readable rahe).
Kyunki hamare paas teen cache levels hain, hum har rate ko us level ke saath tag karte hain jis se woh belong karta hai. Yeh sirf wahi aur hai jo teen baar likhee gayi hai:
The scenario matrix
Problems solve karne se pehle, yahan cases ka pura space hai. Neeche har example us cell ke saath tagged hai jo woh cover karta hai, taaki tum dekh sako kuch skip nahi hua.
| Cell | Kya isse special banata hai | Kahan yeh bite kar sakta hai |
|---|---|---|
| A0. Single level | sirf L1 hai | woh base scenario jis se puri recursion grow hoti hai |
| A. Baseline | ordinary numbers, saare levels present | "vanilla" 3-level AMAT |
| B. Zero miss (perfect cache) | koi | neeche ke pure levels gayab ho jaate hain — kya formula phir bhi kaam karta hai? |
| C. Full miss (useless cache) | koi | cache latency add karta hai par koi fayda nahi — cost badh jaati hai |
| D. Missing level | L3 bilkul nahi | 3-level formula ko 2-level mein collapse karo |
| E. Limiting behaviour | vs | AMAT aur full chain ke beech bounded |
| F. Solve backwards | AMAT diya, hit rate nikalo | algebra, sirf plug-in nahi |
| G. Word problem | wall-clock seconds, GHz | cycles ↔ time unit conversion |
| H. Exam twist | global vs local miss rate | classic galti jo marks khaata hai |
Neeche ki figure is matrix ko ek pipeline ki tarah draw karti hai: har access left se enter karta hai (Cell A0 pehle box mein hi rehta hai), "abhi tak nahi mila" wala shrinking stream rightward flow karta hai jo Cells A–E track karte hain, aur labelled arrows precisely wahi miss rates hain jo hum har example mein tag karte hain. Ek worked example padho, phir picture mein uska box dhundho.

Picture dikhati hai kyun: of accesses hit the L1 box, only survive to L2, only to L3, only to DRAM. Those percentages are the products of miss rates we will compute in Cell H. Box widths dhyan mein rakho — jitna gehre jaao, stream utni hi patli.
Cell A0 — Single-level base case (sirf L1)
Forecast: Sirf ek cache ke saath, har miss seedha DRAM jaata hai. Kya answer 3-level case se bada hoga ya chhota? (Padhne se pehle socho — L2/L3 kuch bhi catch karne ke liye nahi hai.)
-
One-level formula lo. Yeh step kyun? Koi bracket nest karne ke liye nahi hai — L1 miss ke paas memory ke alawa kaheen jaane ki jagah nahi, isliye uski penalty hi hai. Yeh sabse simple, non-recursive doll hai.
-
Plug in karo. Yeh step kyun? Har access -cycle L1 look pay karta hai. Sirf miss karte hain, har ek pay karta hai, average mein cycles contribute karta hai.
Verify: (agar kuch miss nahi kiya) aur (agar sab miss kiya) ke beech baittha hai. Yeh ki taraf jhukta hai kyunki hit karte hain. ✓ Yeh seed hai: neeche har richer formula ko ek cache level ke average cost se replace karke bana hai. (Yeh Cell A0 hai.)
Cell A — Baseline three-level AMAT
Forecast: Abhi andaza lagao — kya answer ke kareeb hoga ya ke kareeb? (Zyaadatar log over-guess karte hain; answer shocking roop se chhota lagega.)
-
Sabse inner bracket: L3 ka average cost. Yeh step kyun? L3 mein dekhne mein hamesha lagta hai. L3 tak pahunchne wale sirf accesses DRAM ke tak fall through karte hain. Toh average par ek "L3-region" access cost karta hai. Yeh number upar wale level ke liye DRAM ko replace karta hai.
-
Middle bracket: L2 ka average cost. Yeh step kyun? Humne abhi kaha "L3 tak pahunchna average par cost karta hai," toh L2 ke point of view se ek miss penalty hai, nahi. L2 looks cost karti hain; unka us tak fall through karta hai.
-
Outer: final answer. Yeh step kyun? Har access L1 ka pay karta hai. Sirf kabhi deeper machinery jagaate hain, aur unke liye woh machinery average karti hai.
Verify: Sanity check — (best possible) aur (agar L1 ke misses seedhe DRAM jaate) ke beech baittha hai. Yeh ke bahut kareeb hai kyunki L2/L3 heavy filtering karte hain. ✓ (Yeh Cell A hai.)
Cell B — Ek perfect level (zero miss rate)
Forecast: Agar L2 sab kuch catch karta hai jo usse poocha jaata hai, toh kya L3 aur DRAM matter karte hain?
-
Innermost phir bhi evaluate hota hai — par zero se multiply hota hai. Yeh step kyun? Hum ise compute karte hain, par dekho aage kya hota hai.
-
Middle bracket ise maar deta hai. Yeh step kyun? matlab koi access kabhi L2 nahi chodta. Humne jo compute kiya woh real hai par kissi tak pahunchta nahi. Formula ise multiply away kar deta hai — exactly wahi jo "perfect L2" karna chahiye.
-
Outer. Yeh step kyun? Yeh standard outer layer hai: har access L1 ka pay karta hai, aur jo L1 miss karte hain ab ek aise level se milte hain jiska pura cost sirf L2 ka hai (kyunki L2 kabhi fail nahi hota). multiply karke add karne par average milta hai.
Verify: Ek perfect L2 ke saath, kisi bhi access ka sabse gehri cheez L2 at cycles ho sakti hai. Toh AMAT zyada se zyada honi chahiye. Match karta hai. ✓ (Yeh Cell B hai — zero miss rate us point par hierarchy ko truncate kar deta hai.)
Cell C — Ek useless cache (miss rate of one)
Forecast: Agar L2 kabhi kuch nahi dhundta, toh kya woh time barbad karne ke alawa kuch kar raha hai?
-
Innermost. Yeh step kyun? L3 aur DRAM ka behaviour Cell A se nahi badla, toh "L3 tak pahunchne" ka average cost abhi bhi cycles hai. Hum yeh number dobara establish karte hain kyunki neeche wale outer levels ko ise apni miss penalty ke roop mein chahiye — ek toota hua L2 bhi apne misses usi L3 ko deta hai.
-
Middle — full pass-through. Yeh step kyun? matlab har L2 lookup fail hoti hai, toh har ek neeche wale level ke pure plus L2 mein dekhne mein waste hue cycles pay karta hai. L2 pure overhead ban gaya.
-
Outer.
Verify: Bina L2 wali machine se compare karo (accesses L1 → L3 jaate hain): . Useless L2 deta hai — usne cheezein exactly barbad cycles se worse kar diya. ✓ wala cache kisi cache se bhi bura hai. (Yeh Cell C hai.)
Cell D — Ek level missing hai (2-level machine)
Forecast: Hamare teen formulas mein se kaun sa uthate hain? (Hint: memory nahi, caches gino.)
-
Two-level formula use karo. Yeh step kyun? L3 nahi hai toh L2 miss seedhe DRAM jaata hai, isliye L2 ki miss penalty hi hai — koi middle doll nahi.
-
Inside-out. Yeh step kyun? Yeh inner bracket L2 ka average cost hai. L2 mein dekhne mein hamesha lagta hai, aur L2 tak pahunchne wale accesses DRAM ke tak fall through karte hain (koi L3 unhe catch karne ke liye nahi hai), deta hai . Yeh L1 ki miss penalty ban jaata hai.
-
Outer.
Verify: Magnitude ka sanity check karo, 3-level formula ko manipulate karke nahi. (Trap se bachke raho: aur se fake L3 banaa ke 3-level expression reuse karna L3 ka cost correctly deta, par aur se fake karne par DRAM do baar pay hota — — jo galat hai. "L3 remove karne" ka safe tarika uski bracket drop karna hai, jo bas 2-level formula hai jo humne use kiya.) Bajaay: full 3-level (Cell A) se upar aur useless-L2 (Cell C) se neeche hai. Ek real L3 add karna (Cell A) help karta hai, aur karta hai: . ✓ (Yeh Cell D hai.)
Cell E — Limiting behaviour (do extremes)
Forecast: Floor kya hai? Ceiling kya hai? Padhne se pehle do numbers guess karo.
-
Best case, har . Yeh step kyun? Agar L1 se kuch kabhi miss nahi karta, toh term bilkul gayab ho jaata hai. AMAT kabhi se kam nahi ho sakta — tum hamesha pehla lookup pay karte ho.
-
Worst case, har . Yeh step kyun? Agar har level miss karta hai, toh har access puri chain chalke jaata hai, raaste mein har hit time pay karta hai aur finally DRAM. Yeh absolute ceiling hai.
Verify: Cell-A ka real answer yeh satisfy karta hai: . Tumhara compute kiya hua har AMAT mein hona chahiye. Is range ke bahar koi bhi answer arithmetic error hai. ✓ (Yeh Cell E hai.)
Cell F — Solve backwards (AMAT diya, hit rate nikalo)
Forecast: Cell A mein tha jo deta tha. AMAT ko tak lower karne ke liye, kya upar jaana chahiye ya neeche?
-
unknown ke saath equation likho. Yeh step kyun? ke alawa sab fixed hai, toh yeh ek linear equation hai ek unknown mein.
-
isolate karo. Yeh step kyun? Hamesha pay hone wala subtract karo, phir miss penalty se divide karo. Yeh formula ko "undo" karta hai.
-
Hit rate mein convert karo. Yeh step kyun? Sawaal hit rate ke liye tha, par hamaara formula miss rate ke terms mein likha hai. Definition se , toh ; miss rate ko se subtract karna us quantity ko convert karta hai jo humne solve ki woh quantity mein jo designer chahta hai.
Verify: Wapas plug karo: . ✓ Aur sense bhi banta hai: Cell-A ka beat karne ke liye humhe ek lower miss rate chahiye tha (), yani thoda better L1. (Yeh Cell F hai.)
Cell G — Word problem (cycles se wall-clock seconds)
Forecast: matlab cycles per second. Kya cached version seconds lega ya milliseconds?
-
Total cycles, cached. Yeh step kyun? AMAT ki units cycles-per-access hain; access count se multiply karo aur "access" unit cancel ho jaata hai, cycles bachte hain.
-
Cycles ko seconds mein. Yeh step kyun? Cycles ko (cycles per second) se divide karne par seconds bachte hain — clean unit check.
-
No-cache comparison. Yeh step kyun? Hum exact same two-stage conversion repeat karte hain (accesses × cycles-per-access, phir ÷ cycles-per-second) par ki jagah cycles per access ke baseline cost ke saath. No-cache number usi units mein banana hi honest ratio lene deta hai Verify line mein.
Verify: Speedup , parent ka match karta hai (ratio unit-free hai, toh GHz cancel ho jaata hai — good). ✓ (Yeh Cell G hai.)
Cell H — Exam twist: global vs local miss rate
Forecast: Kya local L2 miss rate se bada hai ya chhota? (Sirf accesses L2 tak pahunchte hain…)
-
Global aur local ko relate karo. Yeh step kyun? Ek access globally L2 miss karta hai tabhi jab usne pehle L1 miss kiya ho (rate ) aur phir L2 miss kiya ho (rate ). Do independent hurdles ki probabilities multiply hoti hain. Solve karne par local milta hai — se bahut bada, kyunki iska base chhota stream hai, sab nahi. (Yeh exactly figure mein "thinning stream" hai: L3 box original width rakhta hai.)
-
Ab local ko standard formula mein plug karo (inside-out). Yeh step kyun? Ek baar correct local ho, yeh Cell A se identical hai — prove karta hai ki diya gaya global figure sirf Cell A disguise mein tha.
Verify: Global check doosre taraf se: . ✓ Agar tumne galti se use kiya hota, tum paate — ek alag, galat answer jo exam mein plausible lagta. (Yeh Cell H hai.)
Pull it together
Recall Which formula and which numbers?
mein subscript ka kya matlab hai? ::: "Level 2 ka local miss rate" — jo accesses L2 tak pahunche unka woh fraction jo use miss kare. 3-level cache formula mein kitne brackets hote hain? ::: Teen nested brackets (L1 bahar, L2 middle, L3+DRAM andar). wala level neeche wale levels ke saath kya karta hai? ::: Unhe truncate karta hai — unka cost zero se multiply ho jaata hai aur kabhi reach nahi hota. wale cache vs bilkul koi cache nahi — kaun better? ::: wala cache worse hai — woh apna hit time pure overhead ki tarah add karta hai bina kisi benefit ke. Koi bhi AMAT ka absolute minimum? ::: , L1 hit time jo tum hamesha pay karte ho. Absolute maximum? ::: , har level missing. Global miss rate local se kaise relate karta hai? ::: (us level tak ke local miss rates ka product).
Prerequisites & neighbours: yahan hit/miss counts Cache Organization aur Cache Replacement Policies se aate hain; "shared L3" reasoning Cache Coherence se connect karta hai; miss rates tumhare Memory Access Patterns par depend karte hain; aur yeh cycles execution mein kahan fit hote hain woh hai CPU Pipeline aur DRAM Architecture.