optional · CSS study notes
Cache Memory
CSS Optional — Paper-II, Section-A · Chapter 7 · 🔥 Highest-yield numerical topic in this chapter
Cache exists to solve one problem: the CPU is roughly 100× faster than main memory. A 3 GHz processor completes a cycle in 0.33 ns, but a DRAM access takes 60–100 ns. Without cache, the CPU would spend most of its life stalled. Cache is a small, fast buffer holding the data the CPU is likely to need next.
🗺️ Architecture Diagram — Memory Hierarchy
graph TD
R["Registers
~1 cycle, smallest"] --> L1["L1 Cache
~1-4 cycles"]
L1 --> L2["L2 Cache
~10 cycles"]
L2 --> L3["L3 Cache
~30-40 cycles"]
L3 --> MM["Main Memory - RAM
~100+ cycles"]
MM --> SEC["Secondary Storage - SSD/HDD
~10,000+ cycles, largest"]
SECTION 1 — Why Cache Works: The Principle of Locality
Cache would be useless if memory access were random. It works because real programs are predictable in two ways:
- Temporal locality — if you used it recently, you'll use it again soon. A loop counter is read and written every iteration. This justifies keeping recently used data.
- Spatial locality — if you used an address, you'll use its neighbours. Arrays are traversed sequentially; instructions execute in order. This justifies fetching an entire block (typically 64 bytes) rather than a single byte.
Sequential locality (a sub-case of spatial) refers specifically to instructions executing in address order.
SECTION 2 — Cache Terminology
| Term | Meaning |
|---|---|
| Block / Line | Unit of transfer between cache and main memory (commonly 16–64 bytes) |
| Block size (B) | Number of bytes per block |
| Cache lines (L) | Number of block-sized slots in the cache |
| Hit | Requested data found in cache |
| Miss | Data not in cache; must be fetched from the level below |
| Hit time | Time to access data that is present in cache |
| Miss penalty | Extra time required to service a miss |
| Tag | Bits stored with each line identifying which memory block occupies it |
| Valid bit | Marks whether the line holds meaningful data (0 after power-on) |
| Dirty bit | Marks that a cached block was modified and must be written back |
The Three C's — classification of misses (frequently examined)
- Compulsory (cold) miss — the first-ever reference to a block. Unavoidable; reduced only by larger block size or prefetching.
- Capacity miss — the working set exceeds total cache size. Reduced only by a larger cache.
- Conflict miss — the block was evicted because too many blocks map to the same line/set, even though the cache had free space elsewhere. Reduced by higher associativity. Direct-mapped caches suffer these worst; fully associative caches have none.
SECTION 3 — Cache Mapping Techniques ⭐
The central question: when a memory block is brought in, which cache line may it occupy?
3.1 Address Partitioning — The Master Method
Every mapping problem is solved by splitting the physical address into fields. Learn this procedure and every variant follows.
Step 1: Address bits
Step 2: Offset bits (which byte inside the block)
Step 3: Number of lines
Step 4: Middle field
- Direct mapped:
- N-way set-associative: ,
- Fully associative: no middle field (0 bits)
Step 5: Tag bits
Always verify: Tag + Middle + Offset = total address bits.
3.2 Direct Mapping
Each memory block maps to exactly one permitted cache line:
| Tag | Line | Offset |
|---|---|---|
| Which memory block sits here | Which cache line | Which byte in the block |
- ✅ Simplest and fastest — only one tag comparison needed.
- ✅ Cheapest hardware; no replacement algorithm required (only one candidate line).
- ❌ Severe conflict misses. Two hot blocks mapping to the same line evict each other repeatedly — called thrashing — even with the rest of the cache empty.
3.3 Fully Associative Mapping
A block may be placed in any cache line.
| Tag | Offset |
|---|---|
| Which memory block (compared with every line) | Which byte in the block |
- ✅ Maximum placement freedom; zero conflict misses.
- ✅ Best possible hit rate for a given cache size.
- ❌ Every tag must be compared in parallel, requiring one comparator per line — prohibitively expensive (CAM hardware). Used only for very small structures such as TLBs.
- ❌ Requires a replacement algorithm.
3.4 Set-Associative Mapping (the practical compromise)
The cache is divided into sets; a block maps to one set but may occupy any line within that set. An N-way set-associative cache has N lines per set.
| Tag | Set | Offset |
|---|---|---|
| Which memory block (compared with the N lines of the set) | Which set | Which byte in the block |
- ✅ Most of the flexibility of fully associative at a fraction of the hardware cost — only N comparators needed.
- Real CPUs use this almost universally: L1 typically 4–8 way, L3 often 12–16 way.
Unifying insight worth stating in an exam:
Direct-mapped = 1-way set-associative. Fully associative = L-way set-associative (one single set). Both are special cases of the same scheme.
3.5 Comparison Table (high-yield)
| Feature | Direct | Set-associative (N-way) | Fully associative |
|---|---|---|---|
| Placement freedom | 1 fixed line | Any of N lines in its set | Any line |
| Comparators needed | 1 | N | L (all lines) |
| Middle address field | Line bits | Set bits | None |
| Tag size | Smallest | Medium | Largest |
| Conflict misses | Highest | Low | None |
| Hardware cost | Lowest | Moderate | Highest |
| Replacement algorithm | Not needed | Required | Required |
| Search speed | Fastest | Moderate | Slowest |
| Real-world use | Rare | Standard in all modern CPUs | Small TLBs only |
SECTION 4 — Replacement Algorithms ⭐
Needed whenever a set is full and a new block arrives (i.e., for associative and set-associative caches; not for direct-mapped).
4.1 FIFO (First In, First Out)
Evicts the block that has been resident longest, regardless of use. Implemented with a simple queue or counter.
- ✅ Very cheap hardware.
- ❌ May evict a heavily used block simply because it arrived early.
- ❌ Suffers Bélády's anomaly — increasing cache size can increase misses.
4.2 LRU (Least Recently Used) ⭐
Evicts the block unused for the longest time. Directly exploits temporal locality.
- ✅ Consistently the best hit rates among practical policies.
- ❌ Requires age tracking. For an N-way set, exact LRU needs roughly log₂(N!) bits per set, so real hardware uses pseudo-LRU approximations beyond 4-way.
4.3 LFU (Least Frequently Used)
Evicts the block with the lowest access count.
- ❌ Cache pollution — a block accessed heavily once and never again keeps a high count and resists eviction forever.
4.4 Random
Evicts a randomly chosen line.
- ✅ Free to implement; no state at all.
- ✅ Surprisingly competitive at high associativity (within a few percent of LRU).
4.5 Optimal (OPT / Bélády's algorithm)
Evicts the block that will be needed furthest in the future.
- ❌ Requires knowledge of future references — impossible to implement.
- ✅ Used as a theoretical benchmark to measure how good a real policy is.
| Algorithm | Hardware cost | Hit rate | Bélády's anomaly | Practical? |
|---|---|---|---|---|
| FIFO | Very low | Poor | ✅ Yes | Yes |
| LRU | High | Best practical | ❌ No | Yes (pseudo-LRU) |
| LFU | Medium | Variable | ❌ No | Rarely |
| Random | None | Good at high N | ❌ No | Yes |
| Optimal | — | Best possible | ❌ No | No (benchmark only) |
SECTION 5 — Write Policies
| Policy | Mechanism | Advantage | Disadvantage |
|---|---|---|---|
| Write-through | Write to cache and memory simultaneously | Memory always consistent; simple; safe on power loss | Every write pays full memory latency |
| Write-back | Write to cache only; set dirty bit; write to memory on eviction | Much faster; repeated writes to one block cost one memory write | Memory temporarily stale; data loss on crash; complicates multiprocessor coherence |
On a write miss:
- Write-allocate — load the block into cache first, then write. Usually paired with write-back.
- No-write-allocate (write-around) — write straight to memory, bypassing cache. Usually paired with write-through.
Write buffer — a small FIFO that holds pending writes so the CPU need not stall waiting for memory; mitigates write-through's main weakness.
SECTION 6 — All Formulas (Derived and Explained)
One idea builds every formula below:
🗣️ In simple words: list the cases (hit, miss, …), write how often each happens and how long it takes, multiply, add. Forget a formula in the exam → rebuild it this way.
6.1 Hit and Miss
What it finds: how often the cache already has the data.
6.2 Average Access Time — Which Formula When ⚠️
6.2.1 The Difference First
| What the question gives | Model | Formula | Short form | A miss costs |
|---|---|---|---|---|
| Cache time + memory time, "simultaneous / parallel / look-aside" | Form A — simultaneous | only | ||
| Cache time + memory time, "hierarchical / sequential / look-through" | Form A — hierarchical | |||
| Hit time + miss penalty (extra time) | Form B — AMAT | same |
If the question names no model, write your assumption in one line before solving.
6.2.2 Derivation — Simultaneous
Step 1: Cases — hit: chance , time ; miss: chance , time (memory was already searching).
Step 2: Chance × time, add
Step 3: Rewrite in AMAT shape
So here the miss penalty is .
6.2.3 Derivation — Hierarchical
Step 1: Cases — hit: chance , time ; miss: chance , time .
Step 2: Chance × time, add
Step 3: Simplify
So here the miss penalty is the full .
🗣️ In simple words: everyone pays the cache time; only the misses pay the extra.
6.2.4 Why Form A and Form B Are One Formula
Both reduce to:
- The only question to ask: "On a miss, how much EXTRA time beyond the hit?"
- Gap between the two models:
(the cache time wasted on misses — Numerical 3: ns)
6.2.5 Reverse Formulas (finding H or M)
Check with Numerical 14: .
6.3 Miss Penalty with Block Transfer (asked in 2025)
What it finds: how long it takes to bring a whole line from memory.
Alternative wording (latency + bandwidth):
If the cache waits for the line, then re-executes as a hit:
Check with the 2025 question: 64-byte line = 16 words → ns, ns.
🗣️ In simple words: a bigger block raises H but also raises P — always compute both averages and compare.
6.4 Multi-Level Cache
6.4.1 Two Levels — Derivation
= local miss rates; = L1, L2, memory times (hierarchical).
| Case | Chance | Time paid |
|---|---|---|
| Hit in L1 | ||
| Miss L1, hit L2 | ||
| Miss both, go to memory |
Collecting each time term ( is in all cases, in the last two, in the last one):
Nested form (work inside out):
Check with Numerical 4: ns.
6.4.2 Three and n Levels
🗣️ In simple words: each level's time × the fraction of accesses that reach it. Keep multiplying the misses.
6.4.3 Local vs Global Miss Rate
- Fraction of all accesses that reach memory = product of all local miss rates.
6.5 Cache Organisation
Counting:
Where an address goes:
Bits:
Shortcut for tag bits (no address split needed):
- Why it works: the tag says which of the many memory blocks that share one line (or set) is sitting there. Memory ÷ cache = how many blocks compete for one line; a set holds N, so multiply by N.
- Check with Numerical 5: → 5 bits. Numerical 6: → 21 bits.
Hardware:
- Comparators: direct = 1, N-way = N, fully associative = L (each one T bits wide).
6.6 Write-Policy Formulas
6.6.1 Write-Back — Dirty Blocks
Step 1: Cases on a miss ( = fraction of evicted blocks that are dirty)
- Clean victim → chance → fetch new block:
- Dirty victim → chance → write old block back, then fetch:
Step 2: Effective penalty
🗣️ In simple words: every dirty eviction costs one extra memory trip.
6.6.2 Write-Through (simple model, no write buffer)
Reads (fraction ) behave like normal cache accesses; every write (fraction ) goes to memory:
Textbooks vary on this model — state your assumption. A write buffer hides most of the cost.
6.7 Performance Impact
Split instruction / data caches (more detailed):
6.8 One-Look Formula Sheet
| Formula | What it finds | Use when |
|---|---|---|
| Average access time | Simultaneous; total times given | |
| Average access time | Hierarchical; total times given | |
| AMAT | Miss penalty (extra time) given | |
| AMAT, 2-level cache | L1 + L2 + memory | |
| Required hit ratio | Reverse, simultaneous | |
| Miss penalty | Block transfer word by word | |
| AMAT, write-back | Dirty fraction given | |
| Tag bits | Any mapping | |
| Tag bits (shortcut) | N-way; N = 1 for direct | |
| Tag directory size | Total cache size questions | |
| Effective CPI | Performance questions |
6.9 Easy Ways to Remember 🧠
6.9.1 What Cache Is — The Chai Dhaba
- The dhaba keeps a few cups of tea ready on the counter (cache) instead of brewing every cup from scratch in the back (main memory). Most customers want the usual tea (locality), so most get it instantly (hit).
6.9.2 Mapping — Exam Hall Seating
- Direct mapped = fixed seat by roll number (roll no. mod seats). Finding someone is instant (1 comparator), but two students with the same seat keep pushing each other out → conflict misses.
- Fully associative = free seating. Sit anywhere (no conflicts), but to find someone you must check every seat (L comparators → expensive).
- Set-associative = your row is fixed, any seat in that row. Check only N seats → the practical compromise.
- Hook: "Mod gives the seat, divide gives the tag." Line/Set = block mod L (or S); Tag = block ÷ L (or S).
6.9.3 Address Split — "T-S-O" (Tag, Set, Offset), left to right
- Offset = which page of the book, Set/Line = which shelf, Tag = whose book it is.
- Bits must always add up to the address width — your built-in answer check.
- Shortcut hook: "Memory ÷ Cache = how many fight for one line" → that number in bits is the tag (×N for N-way).
6.9.4 The Three C's — First Day at School
- Compulsory = first day at a new school; nobody knows you yet (first-ever reference).
- Capacity = your school bag is simply too small for all your books.
- Conflict = two students forced into the same seat while other seats are empty.
6.9.5 Replacement Policies
- FIFO = bank queue — whoever came first leaves first, even if still busy. Only FIFO shows Belády's anomaly ("more space, more misses").
- LRU = the dusty book — the book you haven't opened for the longest goes back to the library.
- LFU = least popular student — problem: an old celebrity with a huge count never leaves (cache pollution).
- OPT = fortune teller — perfect, but nobody can see the future (benchmark only).
- Random = lottery — free, and surprisingly decent.
6.9.6 Write Policies — Notebook and Register
- Write-through = write in your rough notebook AND the official register every time — always safe, always slow.
- Write-back = write only in the notebook; mark the page red (dirty bit); copy to the register only when the page is torn out (evicted) — fast, but if the notebook is lost (power failure) the register is out of date.
- Dirty penalty hook: "Dirty = double trip" → .
6.9.7 Formula Hooks
- AMAT: "Sab ka hit time, missers ka penalty." Always ask: on a miss, how much EXTRA?
- Multi-level: "Keep multiplying the misses."
- Block transfer: "First word slow, rest quick" — like a counter queue: the clerk takes time to arrive, then serves everyone quickly.
- CPI: "Base + stalls"; stalls = how often × how unlucky × how long (accesses × miss rate × penalty).
- Reverse H: "Gap covered ÷ total gap" = .
6.9.8 Quick Answer Checks ✅
- Average time must lie between cache time and memory time (or for hierarchical).
- Hierarchical answer > simultaneous answer for the same data.
- Tag + Middle + Offset = address bits.
- Hits + misses = total references in a trace.
- Convert penalties to the same unit (ns or cycles) before adding.
SECTION 7 — Solved Numericals
🧮 Numerical 1 — Hit Ratio and Average Access Time (Form A)
Q: A CPU makes 1,000 memory references, of which 950 are found in cache. Cache access time is 10 ns; main memory access time is 100 ns. Find the hit ratio, miss ratio, and average access time.
Step 1: Hit ratio
Step 2: Miss ratio
Step 3: Average access time (weighted average form)
Answer: H = 95%, M = 5%, Average access time = 14.5 ns.
Interpretation: without cache every access costs 100 ns. With a 95% hit rate the average falls to 14.5 ns — a 6.9× speedup from a small buffer. This one line converts a mechanical answer into an analytical one.
🧮 Numerical 2 — AMAT with Miss Penalty (Form B)
Q: Hit time = 2 ns, miss rate = 4%, miss penalty = 50 ns. Find AMAT.
Answer: 4 ns.
⚠️ Contrast with Numerical 1. There, 100 ns was the total memory time. Here, 50 ns is the extra penalty on top of the 2 ns hit time. Applying the wrong form gives the wrong answer — always identify which quantity the question has given.
🧮 Numerical 3 — Hierarchical vs Simultaneous Access
Q: Cache access = 10 ns, main memory access = 100 ns, hit ratio = 0.9. Compute average access time under (a) simultaneous access, (b) hierarchical access.
(a) Simultaneous — cache and memory searched together; a miss costs memory time only
(b) Hierarchical — memory searched only after the cache lookup fails; a miss costs both
Answer: (a) 19 ns, (b) 20 ns.
The 1 ns difference is exactly the wasted cache lookup on the 10% of accesses that miss. Examiners use this pair to test whether you read the access model or merely pattern-matched a formula.
🧮 Numerical 4 — Two-Level Cache
Q: L1 hit time = 1 ns, L1 miss rate = 10%. L2 hit time = 8 ns, L2 local miss rate = 20%. Main memory = 100 ns. Find AMAT and the global L2 miss rate.
Step 1: Effective L2 access time (work inside out)
Step 2: AMAT
Step 3: Global L2 miss rate
Answer: AMAT = 3.8 ns; global L2 miss rate = 2%.
Only 10% of accesses reach L2 and only 2% reach main memory — the quantitative case for multi-level caching.
🧮 Numerical 5 — Direct-Mapped Address Breakdown
Q: A direct-mapped cache has 128 lines with a block size of 16 bytes. Main memory is 64 KB. Determine the tag, line, and offset bits.
Step 1: Address bits
Step 2: Offset bits
Step 3: Line bits (direct mapped)
Step 4: Tag bits
Verify: ✓ · Shortcut: cache = 128 × 16 B = 2 KB, ✓
Answer: Tag = 5 bits, Line = 7 bits, Offset = 4 bits.
| Tag | Line | Offset |
|---|---|---|
| 5 bits | 7 bits | 4 bits |
🧮 Numerical 6 — Set-Associative Address Breakdown
Q: A 4-way set-associative cache has 256 lines, block size 32 bytes, and a 32-bit address. Find the tag, set, and offset bits.
Step 1: Number of sets
Step 2: Offset bits
Step 3: Set bits
Step 4: Tag bits
Verify: ✓
Answer: Tag = 21 bits, Set = 6 bits, Offset = 5 bits.
🧮 Numerical 7 — Fully Associative Breakdown (comparison)
Q: For the same cache as Numerical 6 (32-bit address, 32-byte blocks) organised as fully associative, find the field sizes. Compare tag storage overhead with the 4-way design.
Step 1: Fields (fully associative has no set field)
Step 2: Tag storage (256 lines each)
Step 3: Extra overhead
Answer: Tag = 27 bits, Offset = 5 bits; fully associative needs 29% more tag storage — plus 256 comparators instead of 4. This quantifies exactly why full associativity is impractical at scale.
🧮 Numerical 8 — Block Placement (which line does an address map to?)
Q: A direct-mapped cache has 64 lines and 8-byte blocks. Which cache line will memory address 1200 map to, and what is its tag?
Step 1: Block number
Step 2: Line number
Step 3: Tag
Step 4: Offset within the block
Answer: Line 22, Tag 2, Offset 0.
🧮 Numerical 9 — Set Placement (set-associative)
Q: A 2-way set-associative cache has 128 lines and 16-byte blocks. Which set does address 4096 map to?
Step 1: Number of sets
Step 2: Block number
Step 3: Set number
Answer: Set 0 (and it may occupy either of the 2 lines within that set).
🧮 Numerical 10 — FIFO Replacement Trace ⭐
Q: A fully associative cache holds 3 blocks and is initially empty. Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5. Using FIFO, find the number of hits and misses.
| Ref | Cache contents (oldest → newest) | Result |
|---|---|---|
| 1 | 1 | Miss |
| 2 | 1, 2 | Miss |
| 3 | 1, 2, 3 | Miss |
| 4 | 2, 3, 4 | Miss (evict 1) |
| 1 | 3, 4, 1 | Miss (evict 2) |
| 2 | 4, 1, 2 | Miss (evict 3) |
| 5 | 1, 2, 5 | Miss (evict 4) |
| 1 | 1, 2, 5 | Hit |
| 2 | 1, 2, 5 | Hit |
| 3 | 2, 5, 3 | Miss (evict 1) |
| 4 | 5, 3, 4 | Miss (evict 2) |
| 5 | 5, 3, 4 | Hit |
Answer: 3 hits, 9 misses, hit ratio 25%.
🧮 Numerical 11 — LRU Replacement Trace ⭐
Q: Same 3-block cache and same reference string (1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5) using LRU. Compare with FIFO.
| Ref | Cache (least recent → most recent) | Result |
|---|---|---|
| 1 | 1 | Miss |
| 2 | 1, 2 | Miss |
| 3 | 1, 2, 3 | Miss |
| 4 | 2, 3, 4 | Miss (evict 1, least recent) |
| 1 | 3, 4, 1 | Miss (evict 2) |
| 2 | 4, 1, 2 | Miss (evict 3) |
| 5 | 1, 2, 5 | Miss (evict 4) |
| 1 | 2, 5, 1 | Hit (1 becomes most recent) |
| 2 | 5, 1, 2 | Hit |
| 3 | 1, 2, 3 | Miss (evict 5) |
| 4 | 2, 3, 4 | Miss (evict 1) |
| 5 | 3, 4, 5 | Miss (evict 2) |
Answer: LRU gives 2 hits; FIFO gave 3 hits.
Important exam point: LRU is generally better than FIFO, but not always — this specific reference string is a counterexample. If an exam trace produces this result, it is not an error. State explicitly that no replacement policy is optimal for every reference string; only Bélády's OPT is, and it is unimplementable.
🧮 Numerical 12 — Optimal (OPT) Replacement for Comparison
Q: Same cache and string using the Optimal algorithm — evict the block needed furthest in the future.
| Ref | Cache | Result | Reasoning |
|---|---|---|---|
| 1 | 1 | Miss | — |
| 2 | 1, 2 | Miss | — |
| 3 | 1, 2, 3 | Miss | — |
| 4 | 1, 2, 4 | Miss | Evict 3 (next used at position 10, furthest) |
| 1 | 1, 2, 4 | Hit | — |
| 2 | 1, 2, 4 | Hit | — |
| 5 | 1, 2, 5 | Miss | Evict 4 (next used at position 11) |
| 1 | 1, 2, 5 | Hit | — |
| 2 | 1, 2, 5 | Hit | — |
| 3 | 3, 2, 5 | Miss | Evict 1 (never used again) |
| 4 | 3, 4, 5 | Miss | Evict 2 (never used again) |
| 5 | 3, 4, 5 | Hit | — |
Answer: OPT achieves 5 hits — the theoretical maximum.
Summary comparison for this reference string:
| Algorithm | Hits | Misses | Hit ratio |
|---|---|---|---|
| FIFO | 3 | 9 | 25.0% |
| LRU | 2 | 10 | 16.7% |
| OPT | 5 | 7 | 41.7% |
Presenting all three in one table is exactly what a "compare replacement algorithms" question wants.
🧮 Numerical 13 — Bélády's Anomaly (FIFO with a larger cache)
Q: Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 with FIFO. Compare a 3-block cache with a 4-block cache.
4-block FIFO trace:
| Ref | Cache (oldest → newest) | Result |
|---|---|---|
| 1 | 1 | Miss |
| 2 | 1, 2 | Miss |
| 3 | 1, 2, 3 | Miss |
| 4 | 1, 2, 3, 4 | Miss |
| 1 | 1, 2, 3, 4 | Hit |
| 2 | 1, 2, 3, 4 | Hit |
| 5 | 2, 3, 4, 5 | Miss (evict 1) |
| 1 | 3, 4, 5, 1 | Miss (evict 2) |
| 2 | 4, 5, 1, 2 | Miss (evict 3) |
| 3 | 5, 1, 2, 3 | Miss (evict 4) |
| 4 | 1, 2, 3, 4 | Miss (evict 5) |
| 5 | 2, 3, 4, 5 | Miss (evict 1) |
Answer: increasing the cache from 3 to 4 blocks increased misses from 9 to 10.
This is Bélády's anomaly — a counterintuitive property unique to FIFO among the common policies. LRU and OPT are "stack algorithms" and can never exhibit it. Naming the anomaly is a strong mark-earner.
🧮 Numerical 14 — Required Hit Ratio (reverse problem)
Q: Cache access = 5 ns, memory access = 80 ns. What hit ratio is required to achieve an average access time of 10 ns?
Step 1: Set up the weighted-average equation
Step 2: Expand and collect the H terms
Step 3: Solve
Shortcut check: ✓
Answer: 93.33% hit ratio required.
Reverse problems appear regularly. The method is always the same: write the weighted-average equation and solve for the unknown.
🧮 Numerical 15 — Cache Size Including Tag Directory
Q: A 4-way set-associative cache has a total data capacity of 16 KB, block size 32 bytes, and a 32-bit address. Each line also stores a valid bit and a dirty bit. Find the total physical size of the cache including the tag directory.
Step 1: Number of lines
Step 2: Number of sets
Step 3: Address fields
Step 4: Bits per line in the directory
Step 5: Tag directory size
Step 6: Total cache size
Answer: 17.375 KB total.
Overhead: 1.375/16 ≈ 8.6% of the data size is spent on metadata. Noting this overhead shows you understand that "16 KB cache" describes data capacity only.
🧮 Numerical 16 — Effective CPI Including Cache Misses
Q: A processor has a base CPI of 1.0. 30% of instructions are memory references. The cache miss rate is 5% and the miss penalty is 100 cycles. Find the effective CPI and the performance loss.
Step 1: Memory accesses per instruction
Step 2: Stall cycles per instruction
Step 3: Effective CPI
Step 4: Performance loss
Answer: Effective CPI = 7.5 — the processor runs 7.5× slower than its ideal.
Interpretation: a 5% miss rate sounds trivial, but with a 100-cycle penalty it dominates execution time. This is the strongest possible argument for why cache design receives so much engineering attention, and it links directly to Performance Metrics.
🧮 Numerical 17 — Speedup from Adding Cache
Q: A system without cache has a memory access time of 80 ns. Adding a cache with 5 ns access time and a 92% hit ratio. Find the speedup.
Step 1: Average access time with cache
Step 2: Speedup
Answer: 7.27× speedup.
🧮 Numerical 18 — Three-Level Cache ⭐
Q: L1 = 1 ns (local miss 10%), L2 = 5 ns (local miss 20%), L3 = 20 ns (local miss 25%), main memory = 100 ns. Find AMAT and the fraction of accesses reaching memory.
Step 1: L3 subsystem (innermost)
Step 2: L2 subsystem
Step 3: AMAT
Step 4: Check with the expanded form
Step 5: Fraction reaching memory
Answer: AMAT = 2.4 ns; only 0.5% of accesses reach main memory.
🧮 Numerical 19 — Write-Back with Dirty Blocks
Q: A write-back cache has hit time 2 ns, miss rate 5%, and a block transfer time of 40 ns. 30% of replaced blocks are dirty. Find AMAT. What would it be if dirty blocks were ignored?
Step 1: Effective miss penalty
Step 2: AMAT
Step 3: Ignoring dirty blocks
Answer: 4.6 ns — dirty write-backs add 0.6 ns (15%) to every average access.
SECTION 8 — Exam Guidance
🎯 CSS Exam Angle
Cache is the most numerically examined topic in Computer Organization. A complete answer usually needs three layers:
- Conceptual — locality, why cache works, the Three C's of misses.
- Structural — the three mapping techniques with the address-partition diagram, plus replacement policies.
- Numerical — hit ratio, AMAT, and address breakdown, with the formula written before substitution.
For a replacement-algorithm question, always draw the trace table (reference / cache contents / hit-or-miss). Examiners award marks for the table itself even if one row is wrong.
For a mapping question, always draw the address partition as a labelled box diagram and verify the bits sum to the address width.
🔁 Repeated CSS Questions
- Explain cache mapping techniques — direct, fully associative, and set-associative — with diagrams. (Very frequently repeated)
- Calculate average memory access time given hit ratio, cache time, and memory time. (Numerical — very frequently repeated)
- Given a reference string and cache size, compute hits and misses under FIFO and LRU. (Numerical — frequently repeated)
- Differentiate between write-through and write-back cache policies.
- Given a cache configuration and memory size, determine tag, line/set, and offset bits. (Numerical)
- What is the principle of locality? Explain temporal and spatial locality with examples.
- Compare LRU, FIFO, Optimal, and Random replacement algorithms.
- What is Bélády's anomaly? Which algorithms exhibit it?
- Explain the three types of cache miss (compulsory, capacity, conflict) and how each is reduced.
- Calculate the effective CPI of a processor accounting for cache misses. (Numerical)
⚠️ MCQ Traps
- Direct mapping is the fastest to search but has the worst hit rate. Lookup speed and hit quality are opposite properties here — candidates conflate them constantly.
- Direct-mapped caches need no replacement algorithm. "Which replacement policy does a direct-mapped cache use?" is a trick question — there is only one candidate line.
- Two average-access-time formulas exist.
Hit time + (miss rate × miss penalty)where the penalty is extra time, versus(H × cache) + (M × memory)where memory time is total. Mixing them is the most common source of wrong answers. - Simultaneous vs hierarchical access changes the miss cost. Hierarchical adds the wasted cache lookup to every miss.
- Only FIFO suffers Bélády's anomaly among the common policies. LRU and OPT are stack algorithms and cannot.
- LRU is not always better than FIFO for a specific reference string, though it is better on average.
- Optimal replacement is unimplementable — it needs future knowledge. It exists only as a benchmark.
- Conflict misses do not occur in a fully associative cache — by definition, any block may go anywhere.
- Write-back is faster but riskier; write-through is safer but slower. Frequently reversed in MCQs.
- Cache is managed entirely by hardware, unlike virtual memory, which the OS manages.
- A fully associative cache is a set-associative cache with one set; a direct-mapped cache is 1-way set-associative.
- "16 KB cache" refers to data capacity only — the tag directory adds roughly 5–10% more physical storage.
📜 Past-Paper Questions & MCQs (CSS 2016–2026)
How to write: For numerical questions, present Given → Required → Formula → Substitution → Result → Check. For conceptual questions, answer each part directly with relevant technical reasoning and a small example. Marks allocations below guide answer depth; they are not an official FPSC marking scheme.
2022 · Paper-II · Q4(b) · 6 marks — Set-associative cache address format
Question: A set-associative cache consists of 64 lines, divided into 4-line sets. Main memory contains 4K blocks of 128 words each. Show the format of main memory addresses.
Given
| Quantity | Value |
|---|---|
| Cache lines, L | 64 |
| Lines per set, N | 4 |
| Main-memory blocks | 4K = 4096 = 2¹² |
| Words per block, B | 128 = 2⁷ |
Required: Find the number of tag, set-index, and word-offset bits and draw the address format.
Step 1 — Total main-memory address bits. Assuming word-addressable memory:
Step 2 — Word offset. There are 128 words in each block:
Step 3 — Number of sets and set index. Four-way associative means four cache lines per set, not four sets:
Step 4 — Tag.
Step 5 — Address diagram.
| High-order bits | Middle bits | Low-order bits |
|---|---|---|
| Tag = 8 bits | Set = 4 bits | Word = 7 bits |
| Bits 18–11 | Bits 10–7 | Bits 6–0 |
| TAG (8) | SET INDEX (4) | WORD OFFSET (7) |
Check: 8 + 4 + 7 = 19 bits. Thus the 19-bit main-memory address contains an 8-bit tag, 4-bit set index and 7-bit word offset.
Interpretation: The set bits select one of 16 sets; the tag identifies which memory block occupies one of the four candidate lines; the word bits select the required word within the block. Since bytes per word were not specified, no extra byte-offset field can be determined.
2024 · Paper-II · Q3(a) · 7 marks — Cache and data representation
Question: Why is cache memory considered a critical component in a computer system? How does internal and external data representation contribute to optimising memory usage and system efficiency?
Model answer (7 marks)
Introduction: Cache memory is a small, fast memory placed close to the processor. It stores recently used blocks of main-memory data and instructions so that the CPU does not have to access slower RAM for every request.
1. Why cache is critical. Cache exploits temporal locality (recently accessed information may be used again) and spatial locality (nearby addresses are likely to be accessed). On a cache hit, data is obtained quickly; on a miss, the block must be fetched from a lower memory level. This reduces the average memory-access time.
2. Quantifying the benefit. When the miss penalty is the additional time on a miss:
For an illustrative cache with hit time 2 ns, hit ratio 0.95 and an extra miss penalty of 80 ns:
3. Internal representation. Data is stored internally as binary values, such as two's-complement integers, IEEE 754 floating-point numbers and encoded character bytes. Choosing appropriate widths and memory layouts can reduce wasted space; contiguous and aligned data structures can let one cache line serve several successive values.
4. External representation. Data presented to users or exchanged between programs may use decimal text, human-readable dates or formats such as JSON. Converting these to suitable internal formats for processing can reduce repeated text parsing and unnecessary memory traffic. External formats do not themselves determine cache organization.
5. Concrete example. A 64-byte cache line can contain sixteen contiguous 4-byte integers. When a program scans an array sequentially, fetching the first integer may bring the following fifteen integers into the cache. Scattered object references may require more separate cache-line accesses.
Conclusion: Cache improves performance by exploiting locality, while efficient internal representation and sensible data layout help conserve memory and increase cache usefulness.
2025 · Paper-II · Q2(b) · 7 marks — Line size, miss time and AMAT
Question: A single-level cache has an access time of 2.5 ns, a line size of 64 bytes and a hit ratio H = 0.95. Main memory has a first-word (4 bytes) access time of 50 ns and 5 ns for each following word. (i) What is the access time on a cache miss, if the cache waits until the line is fetched and then re-executes for a hit? (ii) If a 128-byte line raises H to 0.97, does this reduce the average access time?
Given
| Parameter | Original cache | Alternative cache |
|---|---|---|
| Cache hit/access time | 2.5 ns | 2.5 ns |
| Line size | 64 bytes | 128 bytes |
| Hit ratio | 0.95 | 0.97 |
| Word size | 4 bytes | 4 bytes |
| Memory first-word time | 50 ns | 50 ns |
| Each additional word | 5 ns | 5 ns |
Required: (i) Total time on a cache miss for the 64-byte line; (ii) compare average access times and determine whether doubling line size helps.
Step 1 — Calculate the number of words in the original line.
Step 2 — Calculate full-line fetch time. The first word takes 50 ns; the remaining 15 words take 5 ns each:
Step 3 — Calculate the miss access time. The question explicitly requires an initial unsuccessful cache lookup and then a second cache hit after loading:
Answer to (i): 130 ns.
Step 4 — Calculate AMAT for the 64-byte cache. A hit takes 2.5 ns; a miss takes 130 ns, so use the weighted average of total hit and miss times:
Step 5 — Repeat for a 128-byte line.
Step 6 — Calculate the new average access time (H = 0.97).
Step 7 — Compare and conclude.
| Result | 64-byte line | 128-byte line |
|---|---|---|
| Transfer words | 16 | 32 |
| Fetch time | 125 ns | 205 ns |
| Total cache miss time | 130 ns | 210 ns |
| Average access time | 8.875 ns | 8.725 ns |
Relative decrease = (0.150 / 8.875) × 100 ≈ 1.69%.
Answer to (ii): Yes. The 128-byte line reduces average access time slightly, from 8.875 ns to 8.725 ns, because the higher hit ratio compensates for the longer miss penalty.
Conceptual trade-off: Larger blocks may exploit more spatial locality, but increase miss transfer time and can reduce the number of distinct blocks that fit in a fixed-size cache. Thus a larger line is not always better.
Exam trap: Do not use the AMAT formula with miss penalty as extra time and then substitute a total miss access time without adjusting it. The weighted-average calculation above consistently uses total hit and miss times.
📝 Practice MCQs (exam-style)
1. A direct-mapped cache has 128 lines of 16 bytes; main memory is 64 KB. How many tag bits?
- (A) 4
- (B) 5
- (C) 7
- (D) 9
Answer: (B) 5. Address = 16 bits, offset = 4, line = 7 → tag = 16 − 7 − 4 = 5. Shortcut: .
2. Which replacement policy can suffer Bélády's anomaly?
- (A) LRU
- (B) Optimal
- (C) FIFO
- (D) None of these
Answer: (C) FIFO. LRU and OPT are stack algorithms, so a bigger cache can never give them more misses. FIFO evicts by arrival order alone, so it can.
3. Conflict misses never occur in a:
- (A) Direct-mapped cache
- (B) 2-way set-associative cache
- (C) 4-way set-associative cache
- (D) Fully associative cache
Answer: (D). A block may go into any line, so no block is ever evicted while free space exists elsewhere.
4. Hit time = 2 ns, miss rate = 5%, miss penalty = 40 ns. The AMAT is:
- (A) 2.0 ns
- (B) 4.0 ns
- (C) 3.9 ns
- (D) 42 ns
Answer: (B) 4.0 ns. . Option (C) is the trap of using the weighted-average form on a penalty value.
5. Cache = 10 ns, memory = 100 ns, H = 0.9, simultaneous access. Average access time is:
- (A) 10 ns
- (B) 19 ns
- (C) 20 ns
- (D) 28 ns
Answer: (B) 19 ns. . Option (C) is the hierarchical answer.
6. Which cache organisation needs no replacement algorithm?
- (A) Direct mapped
- (B) Fully associative
- (C) 4-way set-associative
- (D) 8-way set-associative
Answer: (A). Each block has exactly one possible line, so there is no choice to make.
7. The dirty bit is needed in a:
- (A) Write-through cache
- (B) Write-back cache
- (C) No-write-allocate cache only
- (D) Read-only instruction cache
Answer: (B). Write-back updates memory only on eviction, so it must remember which lines were modified.
8. L1 miss rate = 10%, L2 local miss rate = 20%. The global L2 miss rate is:
- (A) 30%
- (B) 20%
- (C) 10%
- (D) 2%
Answer: (D) 2%. Global = . Keep multiplying the misses.
9. A 4-way set-associative cache has 256 lines. The number of sets is:
- (A) 4
- (B) 64
- (C) 256
- (D) 1024
Answer: (B) 64. .
10. Fetching a whole block on a miss, instead of a single word, mainly exploits:
- (A) Temporal locality
- (B) Spatial locality
- (C) Bélády's anomaly
- (D) Write-back policy
Answer: (B) Spatial locality. Neighbouring addresses are likely to be used next, so bringing the whole block turns future accesses into hits.