Exercises — Cache line size and tags
5.4.4 · D4· Hardware › Memory Hierarchy & Caches › Cache line size and tags
Yeh page parent topic ki practice ground hai. Neeche har problem fully solved hai ek collapsible callout ke andar — pehle khud try karo, phir reveal karo. Levels seedhe fields ko pehchaanna se lekar scratch se cache design karna tak jaate hain.
Shuru karne se pehle, ek figure saari vocabulary fix kar deta hai taaki koi symbol kabhi unexplained na rahe.

Level 1 — Recognition
Exercise 1.1
Ek cache 128-byte lines use karti hai. Har address ko kitne offset bits chahiye?
Recall Solution 1.1
Offset ko ek line ke andar har byte ko name karna hoga. byte-positions hain. kyun? 128 distinct positions ke liye exactly utne bits chahiye jo se tak count kar sakein, aur . Toh 7 bits.
Exercise 1.2
Ek direct-mapped cache mein lines hain. Kitne index bits?
Recall Solution 1.2
Direct-mapped matlab , toh sets ki sankhya = lines ki sankhya = . Index mein se ek slot pick karta hai. 11 bits.
Exercise 1.3
Field widths , , diye hain, toh total address width kya hai?
Recall Solution 1.3
Teeno fields hi poori address hain, edge to edge — koi bit leftover nahi, koi shared nahi. 32-bit address space.
Level 2 — Application
Exercise 2.1
Ek 16 KiB direct-mapped cache 32-bit machine par 32-byte lines use karti hai. , , nikalo.
Recall Solution 2.1
Step 1 — offset. . (line mein har byte ko name karta hai)
Step 2 — lines ki sankhya. B. Lines .
Step 3 — index. , toh . (har slot ko name karta hai)
Step 4 — tag. .
Answer: [18-bit Tag | 9-bit Index | 5-bit Offset].
Exercise 2.2
2.1 wali hi cache. CPU address 0x0000_2C64 request karta hai. Ise Tag / Index / Offset mein split karo.
Recall Solution 2.2
Pehle, 0x2C64 = (decimal).
32-bit binary mein:
0000 0000 0000 0000 0010 1100 0110 0100
Top se [18 tag | 9 index | 5 offset] split karo:
- Offset = low 5 bits =
0 0100= 4. - Index = agle 9 bits =
011 0011shifted... seedha compute karte hain. Offset drop karo: line number hai. Index . - Tag = high bits .
Answer: Tag , Index , Offset . Check: ✓ (note , toh tag ko se multiply kiya gaya).
Exercise 2.3
2.1 wali cache ke liye, poori cache kitne tag storage bits use karti hai (tag + valid bit, no dirty bit)?
Recall Solution 2.3
Per line: tag bits, valid bit bits. lines hain.
Level 3 — Analysis
Exercise 3.1
Do engineers ek fixed 32 KiB, direct-mapped, 32-bit cache ke baare mein argue kar rahe hain. Engineer A line size ko 64 B se double karke 128 B kar deta hai. (a) index bits, (b) tag bits, (c) total tag-storage bytes ka kya hota hai? Har direction explain karo.
Recall Solution 3.1
Cache size B fixed rehti hai. , .
Case 64 B: . Lines . . Tag storage (tag + valid) bits B.
Case 128 B: . Lines . . Tag storage bits B.
(a) Index: — 1 se ghatta, kyunki line size double karne se lines ki sankhya half ho jaati hai. (b) Tag: — yahan unchanged. Humne 1 offset bit khoya lekin 1 index bit bhi khoya; dono cancel ho gaye (, aur 15 pe hi raha). (c) Tag storage: B — half ho gayi, kyunki half lines hain, har ek usi 18 metadata bits ke saath.
Exercise 3.2
Ek cache designer line size B aur address width rakhta hai, lekin cache ko 16 KiB se 64 KiB tak grow karta hai (dono direct-mapped). Dikhao ki tag bits shrink karte hain aur kitne.
Recall Solution 3.2
throughout.
16 KiB: lines . . 64 KiB: lines . .
Cache ko grow karne se index bits add hote hain, jo tag se 2 bits chura lete hain: . Intuition: badi cache mein zyada slots hain, toh address ka zyada hissa yeh batane mein kharch hota hai ki kaunsa slot — blocks ko alag karne ke liye fewer bits bachte hain jo same slot mein land karte hain.
Level 4 — Synthesis
Exercise 4.1
Design task. Ek 64-bit machine ko 4-way set-associative cache chahiye, total size KiB, line size B. Sets, , , , aur total metadata (tag + valid + dirty) KiB mein compute karo.
Recall Solution 4.1
Step 1 — offset. . Step 2 — total lines. lines. Step 3 — sets. Set-associative lines per set pack karta hai: Toh (index ab ek set ko name karta hai, single line ko nahi). Step 4 — tag. bits. Step 5 — metadata. lines mein se har ek tag (48) + valid (1) + dirty (1) bits carry karti hai. Answer: 1024 sets, , , ; metadata KiB.
Exercise 4.2
4.1 jaisa hi lekin direct-mapped (A=1) bana do, baaki sab same. Kya badlega — , , ya — aur kyun tag badhta hai?
Recall Solution 4.2
(line size unchanged). Ab sets lines . bits.
4.1 se compare karein: index (+2) badha aur tag (−2) gira... ruko — tag chota hua, bada nahi. Same ke saath, direct-mapped ke zyada sets hain ( vs ), toh zyada index bits chahiye aur kam tag bits.
Lesson: associativity badhana (fewer sets) tag ko badhata hai; ghataana tag ko ghataata hai. Set-associative version (4.1, ) ka tag bada hai.
Level 5 — Mastery
Exercise 5.1
Reverse-engineering. Bataya gaya hai ki ek address [Tag | Index | Offset] ke roop mein split hota hai jisme lookup circuit index bits aur offset bits use karta hai ek 32-bit machine par, aur cache 2-way set-associative hai. , sets ki sankhya, total cache size , aur recover karo.
Recall Solution 5.1
Line size: B. Sets: sets . Cache size: B KiB. Tag: bits. Sanity check via formula: ✓. Answer: B, 128 sets, KiB, .
Exercise 5.2
Capstone trade-off. Do designs, dono 32-bit, 16 KiB, direct-mapped:
- Design P: B.
- Design Q: B. Dono ke liye metadata overhead fraction (tag + valid + dirty per line, data bytes ke upar) compute karo, aur batao kaunsa overhead mein jeetta hai aur har ek kya sacrifice karta hai.
Recall Solution 5.2
B, , .
Design P (): . Lines . . Metadata/line bits B. Data/line B. Overhead fraction .
Design Q (): . Lines . . Metadata/line bits B. Data/line B. Overhead fraction .
Overhead mein winner: Design Q — badi lines fixed 20-bit tag block ko kahin zyada data pe amortise karti hain (2.0% vs 15.6%). Sacrifice: Q ke paas sirf 128 lines hain P ki 1024 ke mukable, toh Q zyada conflict misses aur zyada miss penalty (128 B transfers) suffer karta hai. P chip area metadata par waste karta hai lekin kaafi saare distinct slots rakhta hai. Yahi exactly woh spatial-locality trade-off hai jo real CPUs ko 64 B tak le jaata hai — beech ka raasta.

Recall Ek-line field recovery cheat card
Widths se: ; sets ; ; . Ek address value split karne ke liye: offset ; line# ; index ; tag .
Connections
- Cache line size and tags — parent theory jise yeh drills exercise karti hain.
- 5.4.02-Direct-mapped-caches — L1–L3 maan lete hain .
- 5.4.03-Set-associative-caches — L4–L5 sets aur step introduce karte hain.
- 5.4.05-Cache-miss-types — L5 trade-off ke peeche conflict-miss cost.
- 5.4.08-Write-policies — jahan se metadata counts mein dirty bit aata hai.
- 3.1.04-Spatial-locality — kyun badi lines help karti hain.
#flashcards/hardware