5.4.4 · D2 · HinglishMemory Hierarchy & Caches

Visual walkthroughCache line size and tags

2,555 words12 min read↑ Read in English

5.4.4 · D2 · Hardware › Memory Hierarchy & Caches › Cache line size and tags

Shuru karne se pehle, ek word jo hum baar baar use karenge:


Step 1 — Ek memory address bas ek ghar ka number hai

KYA HAI. Main memory byte-boxes ki ek enormous row hai jisme numbers likhe hain: box , box , box , … last box tak. Ek memory address simply woh number hai jo ek box par likha hota hai.

KYUN. Isse pehle ki hum ek address split karein, hume agree karna hoga ki yeh hai kya. Yeh koi mysterious code nahi hai — yeh ek badi lambi street par ek plain ghar ka number hai. Agar CPU se lekar tak addresses produce kar sakta hai, to hum isse ek -bit address space kehte hain, kyunki sabse bade ghar ka number likhne mein exactly bits lagte hain.

PICTURE. Neeche byte-boxes ki street dekho. Amber box woh byte hai jisko CPU ne maanga; uska number, neeche binary mein likha hua, address hai.


Step 2 — Cache ek puri line laata hai, to low bits ek byte ko line ke andar name karti hain

KYA HAI. Cache kabhi ek byte fetch nahi karta; woh neighbouring bytes ka ek fixed clump fetch karta hai jise cache line kehte hain. ko power of two choose karo — maano bytes. Ek poori line cache mein aane ke baad bhi hume yeh batana hota hai ki 64 mein se kaun sa byte CPU chahta tha. Yeh kaam address ke lowest bits karte hain: yeh hain offset.

YEH BITS KYUN, AUR ITNI KYUN? Ek line ke andar ke bytes numbered hain. possibilities count karne ke liye exactly utne bits chahiye jo "" satisfy kare. Woh sawaal — kaunsi power mujhe degi? — ka jawab se milta hai. Isliye aata hai: yeh " bits cheezein count karta hai" ka inverse hai.

ke liye: , kyunki . To har address ke bottom 6 bits offset hain.

PICTURE. Ek cache line jo apne byte-slots mein unroll hui hai, slot numbers -bit binary mein likhe hain. Address ke bottom bits wahi slot numbers hain.


Step 3 — Offset hatao, aur baaki ek line number hai

KYA HAI. Koi bhi address lo aur mentally bottom bits hata do. Jo bachta hai woh ek line number hai: yeh bataata hai ki memory ke saare clumps mein se yeh byte kaunse -byte clump mein hai.

KYUN. Same line ke do addresses mein offset bits ke alawa sab kuch same hota hai. To inhe bits drop karna same hai jaise address ko se integer-divide karna. Result line identify karta hai, byte ko nahi — aur yahi woh number hai jiske baare mein cache reason karega.

PICTURE. Puri address bar jisme ek scissors low bits kaat rahi hai; amber remainder "line number" label hai.


Step 4 — Cache mein sirf kuch slots hain, to line numbers ko wrap around karna padta hai

KYA HAI. Cache chhota hota hai: ek direct-mapped cache mein kuch slots hote hain, use slots kaho (yeh bhi power of two). Memory mein lines ke mukable cache ke slots bahut kam hote hain. To bahut sari alag lines ek hi slot share karne par majboor hoti hain. Kaunsi slot mein ek line jaayegi iska rule yeh hai: line number lo aur sirf uske low bits rakho — wahi index hai.

KYUN. "Line number ke low bits rakho" same hai jaise "line number mod " (remainder after dividing by ). Mod isliye choose kiya jaata hai kyunki yeh line numbers ki unlimited range ko slots ke fixed set par map karta hai, sasta mein, bina kisi arithmetic ke — bas bits read karo. slots count karne ke liye hum phir poochte hain "power of two kaunsi hai?", to:

Yahan = total cache size bytes mein, = line size, = associativity (kitni lines ek slot share karti hain; direct-mapped ke liye, set-associative ke liye zyada). ko se divide karne par cache mein jitni lines hain unki count milti hai; phir se divide karne par unhe slots (sets) mein group kiya jaata hai.

PICTURE. Baayein memory lines ka ek lamba column, ek arrow "mod " unhe daayein cache slots ke chhote column par fold kar raha hai — kai memory lines ek hi slot par land kar rahi hain.


Step 5 — Alag lines ek hi slot par aayi, to unhe alag karne ke liye ek tag chahiye

KYA HAI. Kyunki bahut si lines ek slot par fold hoti hain (Step 4), slot jaanna kaafi nahi hai — CPU ko confirm karna hota hai ki wahan rakhhi line wahi hai jo usne chaahi thi. Tag woh sab kuch hai jo bacha hai: address ke high bits, index aur offset ke upar. Cache yeh tag data ke saath store karta hai, aur har access par compare karta hai.

KYUN. Index bataata hai kahan dekhna hai; lekin "kahan" ambiguous hai (Step 4 ka folding). Tag woh leftover bits hain jo line number ko unique banate hain. Kyunki address = tag-bits + index-bits + offset-bits aur inhe tak add hona chahiye:

PICTURE. Puri -bit address bar teen zones mein coloured — amber Tag (high), cyan Index (middle), white Offset (low) — ek bracket ke saath jo dikhata hai.


Step 6 — Ek lookup dekho: hit vs miss

KYA HAI. Teen fields ko kaam par lagao. (1) Index use karke ek slot par jump karo. (2) Wahan store hua tag padho aur address ke tag se compare karo. (3) Valid bit bhi check karo — ek single bit jiska matlab hai "is slot mein real data hai, power-on ka garbage nahi". Agar tag match kare aur valid hit; byte return karne ke liye offset use karo. Warna → miss; memory se line fetch karne jao.

KYUN. Yahi poora payoff hai. Index lookup ko banata hai (ek jump, koi searching nahi). Tag ise sahi banata hai (us galat line ko reject karta hai jo same slot par fold hui thi). Valid ise safe banata hai (ek cold, empty cache kabhi falsely hit report nahi karta). Inme se koi ek na ho to machine toot jaati hai.

PICTURE. Do panels: upar = HIT (stored tag address tag, valid , green check, byte returned). Neeche = MISS (tags differ, red cross, arrow back to memory).


Step 7 — Edge cases: degenerate corners jo tumhe survive karne chahiye

KYA / KYUN / PICTURE. Jo formula tum corners par nahi tod sako woh formula tumhe samajh aata hai. Chaar corners:

  • Line size byte (). To : koi offset bits nahi. Har byte apni khud ki line hai. Legal hai, bas wasteful — sari spatial locality chali jaati hai.
  • Poori memory cache mein fit ho jaaye ( memory size, ). To badhta hai jab tak nahi ho jaata: koi tag bits nahi. Har line ka permanent ghar hai, koi folding nahi, to disambiguate karne ke liye koi tag nahi chahiye.
  • Fully associative ( number of lines, to ). To : koi index bits nahi. Ek giant slot; line kahin bhi reh sakti hai, to tag ko saare line-number bits carry karne honge.
  • Byte-offset zero / line-aligned address. Bottom bits sab hain: CPU ne line ka pehla byte manga tha. Kuch special nahi toot ta — offset bas read karta hai.

Har corner mein, mein se ek tak shrink ho jaata hai lekin identity tab bhi hold karti hai. Koi bhi case isse escape nahi kar sakta.


Step 8 — Ek poora numeric run, Worked Example 2 se match karta hua

KYA HAI. B, , , lo. To , lines , to , aur . Ab address 0x0012A40 decode karo.

KYUN. Numbers pictures ko concrete banate hain aur split machine-check karne dete hain. -bit pattern ko top se slice karo: Tag high bits , Index agle bits , Offset low bits .

PICTURE. ki -bit binary jisme cut lines drawn hain, har slice ki decimal value neeche label ki gayi hai.


Ek-picture summary

Upar ki sab kuch ek single diagram mein compress: ek address upar se enter karta hai; low bits Offset ban ke split hoti hain, agle bits fold ho ke (mod ) slot via Index pick karte hain, top bits Tag ban jaate hain jo slot mein stored cheez se compare hoti hain — neeche hit ya miss.

Recall Feynman retelling — plain words mein vapas bolo

Memory ek mile-lambi street hai numbered byte-boxes ki; address ek box ka number hai. Cache greedy hota hai: ek box ki jagah woh boxes ka poora neighbourhood grab karta hai — ek line — kyunki generally tumhe jald hi next box chahiye hota hai. To address ke bottom bits bas yeh kehte hain ki neighbourhood mein kaun sa box tumhara matlab tha: yeh offset hai. Unhe chop karo aur jo number bachta hai woh poore neighbourhood ka naam hai — line number. Lekin cache mein sirf parking slots hain, neighbourhoods se bahut kam, to yeh line numbers ko slots par fold karta hai unke low bits rakh ke: yeh index hai — yahi parking rule hai. Bahut se neighbourhoods ek hi slot mein park karte hain, to jab tum aate ho to tumhe ek label check karna hota hai ki slot mein jo car hai woh tumhari hai: leftover high bits woh label hain, yeh tag hai, aur ek tiny valid flag hai jo kehta hai ki slot empty nahi hai. Lookup teen moves hai: index tumhe slot tak drive karta hai, tag confirm karta hai ki line sahi hai, offset tumhara byte grab karta hai. Aur chahe cache kaisi bhi size karo — tiny lines, huge associativity, memory jo completely fit ho — teen widths hamesha tak add back ho jaate hain.


Connections