Cache Memory Organization & Mapping
Direct mapping, Associative mapping, Set-associative mapping, Replacement policies (FIFO, LRU, LFU), and AMAT performance formulas
- Master Direct, Associative, and Set-Associative cache mapping formulas
- Compute Tag, Line/Set, and Offset address fields for any cache configuration
- Evaluate replacement policies (FIFO, LRU, LFU, Optimal) and trace cache hits/misses
- Calculate Average Memory Access Time (AMAT) for multi-level caches
⭐ Highest weightage topic in COA (31.9%) — Master every formula and numerical pattern here.
1. Memory Chip Configuration
1.1 Memory Organization Basics
A memory unit is organized as a 2-D array of storage cells: rows (locations) and columns (bits per location). The CPU accesses memory through the address bus (selects a location) and data bus (transfers the content).
1.2 Chip Configuration `2^k n` Notation
A memory chip is described as 2^k × n, meaning:
2^k= number of addressable locations (sok= number of address lines needed)n= number of bits stored per location (data lines needed)
1┌───────────────────────────────┐2│ Memory Chip │3│ │4│ Address lines: k │5│ ───────────────► 2^k words │6│ │7│ Data lines: n (per word) │8└───────────────────────────────┘9
10Total bits in chip = 2^k × n📦 Formula Box ``Number of address lines (k) = log₂(Number of locations) Total chip capacity (bits) = 2^k × n Total chip capacity (bytes) = (2^k × n) / 8``
1.3 Cell Size Classification
- Each memory cell stores 1 bit.
- Chips are classified by word size (n): e.g., 1-bit chips (need
nchips to build ann-bit-wide memory), 4-bit chips, 8-bit chips, etc. - To build a memory system wider or deeper than a single chip, chips are combined:
- Increase word size (width expansion) → chips connected in parallel (same address lines, different data line groups).
- Increase number of locations (depth expansion) → chips connected in series using extra address lines + a decoder to select the correct chip.
1.4 Memory Address Decoding
- A decoder converts a binary address into a single active output line, used to select a specific memory location/chip.
- An
n-to-2^ndecoder is required to select among2^nlocations. - Two decoding schemes:
- Linear/Full decoding: All address bits are decoded — no wasted address space, but decoder hardware is larger.
- Partial decoding: Only some address bits are decoded — simpler hardware but causes memory aliasing/foldback (a location responds to multiple addresses).
1.5 Memory Interpretation Mechanism
When the CPU places an address on the address bus:
- The address is split into chip-select bits (which chip) and internal address bits (which location within that chip).
- The decoder activates the correct chip via its Chip Select (CS) line.
- The internal address bits locate the exact word within the selected chip.
- Data flows via the shared data bus (read/write controlled by R/W control line).
Solved Numericals
Q1. A memory chip is specified as 1K × 8. How many address lines and data lines does it need?
Solution
: 1K = 2¹⁰ locations → 10 address lines. Word size = 8 bits → 8 data lines.
Q2. Design a 16K × 16 memory using 4K × 8 chips. How many chips are needed, and how should they be arranged?
Solution
: - Depth needed: 16K / 4K = 4 (chips arranged in "depth" groups using extra address decoding) - Width needed: 16 / 8 = 2 (chips arranged in parallel for width) - Total chips = 4 × 2 = 8 chips - Extra address lines for depth selection = log₂(4) = 2 lines, decoded via a 2-to-4 decoder.
Q3. A memory system has a 20-bit address bus and an 8-bit data bus. What is the total memory capacity?
Solution
: Capacity = 2²⁰ locations × 8 bits = 2²⁰ bytes = 1 MB.
Q4. How many 2K × 4 chips are required to build a 8K × 8 memory?
Solution
: Depth factor = 8K/2K = 4; Width factor = 8/4 = 2 → Total chips = 4 × 2 = 8 chips.
⚠️ Common Traps & Exam Tips
- 2^k × n — always identify which number is the location count (base for address lines) vs word width (data lines); swapping them is the #1 mistake. - When combining chips: width expansion → parallel (same address, split data); depth expansion → series (extra address bits + decoder). Many students mix these up. - Always convert K, M, G to powers of 2 (1K = 2¹⁰) — NOT 1000, in memory-chip contexts.
2. Hierarchy & Locality
2.1 Memory Hierarchy Diagram
1 ┌───────────────┐ Fastest, Smallest, Costliest2 │ Registers │3 ├───────────────┤4 │ Cache (L1/L2/L3) │5 ├───────────────┤6 │ Main Memory (RAM) │7 ├───────────────┤8 │ Secondary Memory (HDD/SSD) │9 └───────────────┘ Slowest, Largest, CheapestThe hierarchy exploits the fact that fast memory is expensive and cheap memory is slow — by keeping frequently used data in the fastest tiers, we approximate the speed of expensive memory at the cost of cheap memory.
2.2 Principle of Locality
- Temporal Locality: If a memory location is referenced, it is likely to be referenced again soon (e.g., loop variables, loop instructions).
- Spatial Locality: If a memory location is referenced, nearby locations are likely to be referenced soon after (e.g., sequential array access, sequential instruction fetch).
Cache design directly exploits both: fetching a whole block (not just one word) exploits spatial locality; keeping recently used blocks in cache exploits temporal locality.
2.3 Why Hierarchy Works
- Programs don't access memory uniformly at random — locality means a small "working set" of data/instructions is reused repeatedly.
- Hence, a small, fast cache holding the current working set can service the vast majority of memory references, while the large slow memory holds everything else.
2.4 Cache Hit / Cache Miss
- Cache Hit: The CPU finds the requested data already in cache.
- Cache Miss: The requested data is NOT in cache, forcing a fetch from a slower lower level (main memory).
📦 Formula Box ``Hit Ratio (h) = Number of Hits / Total Memory References Miss Ratio (1-h) = Number of Misses / Total Memory References h + (1 - h) = 1``
⚠️ Common Traps & Exam Tips
- Temporal = time (reused soon); Spatial = space (nearby address reused soon) — don't swap the definitions. - Hit ratio is always expressed as a fraction/probability (0 to 1), not a raw count — watch for questions giving raw hit/miss counts requiring you to first compute the ratio.
3. Units & Cache Mapping Techniques Basics
3.1 Key Terminology
- Block/Line: The smallest unit of data transferred between main memory and cache (a group of contiguous words).
- Tag: Bits used to uniquely identify which memory block is currently occupying a cache line.
- Set: A group of cache lines (used in set-associative mapping) that a given memory block can map into.
- Offset (Word/Byte offset): Bits identifying the specific word/byte within a block/line.
3.2 General Address Breakdown
1┌─────────────┬─────────────┬─────────────┐2│ TAG │ SET/LINE │ OFFSET │3└─────────────┴─────────────┴─────────────┘The exact split of these fields depends on the mapping technique used.
3.3 Overview of Three Mapping Techniques
| Technique | Where can a block go? |
|---|---|
| Direct Mapping | Exactly ONE specific cache line (fixed) |
| Fully Associative Mapping | ANYWHERE in the cache |
| Set-Associative Mapping (k-way) | Anywhere within ONE specific set (of k lines) |
⚠️ Common Traps & Exam Tips
- "Offset" bits are determined by block size, NOT cache size — a frequent source of error: offset bits = log₂(block size). - Always compute offset bits FIRST, since they are common to all three mapping techniques.
4. Direct Mapping
4.1 Working Principle
Each block of main memory maps to exactly one specific cache line, using:
1Cache Line Number = (Block Address) MOD (Number of Cache Lines)4.2 Address Division
1┌─────────────┬─────────────┬─────────────┐2│ TAG │ LINE NUMBER │ WORD/OFFSET│3└─────────────┴─────────────┴─────────────┘📦 Formula Box ``Offset bits = log₂(Block size in words/bytes) Line bits = log₂(Number of cache lines) Tag bits = Total address bits − (Offset bits + Line bits)``
4.3 Diagram-in-Text
1Main Memory Blocks: Cache Lines (say 4 lines: 0,1,2,3):2Block 0 ──┐ ┌──────┐3Block 4 ──┼───────────► │ Line0│4Block 8 ──┘ ├──────┤5Block 1 ──┐ │ Line1│6Block 5 ──┼───────────► ├──────┤7Block 9 ──┘ │ Line2│8 ├──────┤9 │ Line3│10 └──────┘11(Blocks 0,4,8,... all compete for Line 0 — only ONE can reside at a time)4.4 Advantages / Disadvantages
| Advantages | Disadvantages |
|---|---|
| Simple hardware, cheap to implement | High conflict misses (rigid one-to-one mapping) |
| Fast lookup (no searching needed — direct index) | Poor hit ratio if multiple frequently used blocks map to same line |
| Easy tag comparison (only 1 comparator needed) | Cannot exploit full cache capacity if access pattern is unfavorable |
Solved Numericals
Q1. Main memory = 4 GB, Cache = 16 KB, Block size = 16 bytes. Find tag, line, and offset bits.
Solution
: - Total address bits = log₂(4 × 2³⁰) = 32 bits - Offset bits = log₂(16) = 4 bits - Number of lines = Cache size / Block size = 16KB / 16B = 1024 lines → Line bits = log₂(1024) = 10 bits - Tag bits = 32 − (4 + 10) = 18 bits
Q2. A system has a 24-bit address, cache of 256 lines, block size = 8 words. Find the tag size.
Solution
: - Offset bits = log₂(8) = 3 bits - Line bits = log₂(256) = 8 bits - Tag bits = 24 − (3 + 8) = 13 bits
Q3. Cache size = 64 KB, block size = 32 bytes, main memory = 1 GB. Using direct mapping, how many blocks of main memory map to the SAME cache line?
Solution
: - Number of cache lines = 64KB / 32B = 2048 lines - Total main memory blocks = 1GB / 32B = 2²⁵ blocks - Blocks mapping to same line = Total blocks / Number of lines = 2²⁵ / 2¹¹ = 2¹⁴ = 16384 blocks
Q4. A byte-addressable memory has 32-bit addresses. Cache has 128 lines, block size 64 bytes. Given address 0x0001A4F8, find the line number it maps to.
Solution
: - Offset bits = log₂(64) = 6 bits → Line bits = log₂(128) = 7 bits - Address = 0x0001A4F8 = 107768 (decimal) - Block Address = ⌊Address / Block size⌋ = ⌊107768 / 64⌋ = 1683 - Line Number = Block Address MOD Number of Lines = 1683 MOD 128 = 1683 − (13 × 128) = 1683 − 1664 = Line 19 - (The remaining 6 bits, 107768 − 1683×64 = 56, form the byte offset within that block.)
⚠️ Common Traps & Exam Tips
- Direct mapping has exactly one comparator — a common conceptual MCQ ("how many comparators needed in direct mapping?" → 1). - The classic off-by-one bit-width error: always double check log₂ computations — e.g., 1024 lines → 10 bits, NOT 11. - Remember: Line bits are derived from number of cache LINES, not cache SIZE directly — divide cache size by block size first to get line count.
5. Associative Mapping (Fully Associative)
5.1 Working Principle
A memory block can be placed in ANY cache line. There is no fixed line number — instead, the entire address (minus offset) is stored as a tag, and hardware searches ALL cache lines in parallel (using comparators) to find a match.
5.2 Address Division
1┌─────────────────────────────┬─────────────┐2│ TAG │ WORD/OFFSET│3└─────────────────────────────┴─────────────┘📦 Formula Box ``Offset bits = log₂(Block size) Tag bits = Total address bits − Offset bits (No separate "line" field — the whole remaining address IS the tag)``
5.3 Advantages / Disadvantages
| Advantages | Disadvantages |
|---|---|
| Maximum flexibility — no conflict misses due to fixed mapping | Expensive hardware (needs a comparator PER cache line, all searched simultaneously) |
| Best hit ratio among the three techniques | Slower due to complex parallel search circuitry (in practice, needs CAM — Content Addressable Memory) |
| Efficient use of cache space | Not scalable for large caches (cost grows with number of lines) |
Solved Numericals
Q1. Main memory address = 32 bits, block size = 16 bytes. Find the tag size in fully associative mapping.
Solution
: - Offset bits = log₂(16) = 4 bits - Tag bits = 32 − 4 = 28 bits
Q2. Cache has 128 lines. In fully associative mapping, how many comparators are required?
Solution
: 128 comparators (one per cache line — all compared simultaneously).
Q3. A 1 MB main memory with block size 64 bytes uses fully associative mapping with a cache of 512 lines. What is the tag size, given a byte-addressable memory?
Solution
: - Address bits = log₂(1M) = 20 bits - Offset bits = log₂(64) = 6 bits - Tag bits = 20 − 6 = 14 bits (Note: the 512-line cache size does NOT affect tag computation in associative mapping — it only affects HOW MANY blocks can be cached simultaneously, not the tag width.)
⚠️ Common Traps & Exam Tips
- In fully associative mapping, cache size/number of lines does NOT determine tag bits — tag bits depend only on address size and offset. This trips up many students who try to subtract a "line field" that doesn't exist here. - Number of comparators required = number of cache lines — always tested directly as an MCQ.
6. Set Associative Mapping
6.1 k-Way Set Associative Mapping
A compromise between direct and fully associative mapping: cache is divided into sets, each containing k lines ("k-way"). A memory block maps to exactly one set (like direct mapping) but can occupy any of the k lines within that set (like associative mapping).
1Set = (Block Address) MOD (Number of Sets)6.2 Address Division
1┌─────────────┬─────────────┬─────────────┐2│ TAG │ SET NUMBER │ WORD/OFFSET│3└─────────────┴─────────────┴─────────────┘📦 Formula Box ``Offset bits = log₂(Block size) Set bits = log₂(Number of sets) Number of sets = (Number of cache lines) / k Tag bits = Total address bits − (Offset bits + Set bits)``
6.3 Comparison: Direct vs Fully Associative vs Set-Associative
| Feature | Direct Mapping | Fully Associative | Set-Associative (k-way) |
|---|---|---|---|
| Block placement | Fixed, single line | Anywhere | Anywhere within one specific set |
| Comparators needed | 1 | Equal to number of lines | Equal to k (per set) |
| Hardware cost | Lowest | Highest | Medium |
| Conflict misses | Highest | None (only capacity/compulsory) | Lower than direct, higher than associative |
| Hit ratio | Lowest (generally) | Highest (generally) | Balanced (in between) |
| Search speed | Fastest (1 comparison) | Slowest (parallel search across all lines) | Moderate |
| Special case | k=1 → same as Direct Mapping | k=Total Lines → same as Fully Associative | Practical middle ground (common: 2-way, 4-way, 8-way) |
Solved Numericals
Q1. Cache = 32 KB, block size = 32 bytes, 4-way set associative. Address = 32 bits. Find the number of sets, and tag bits.
Solution
: - Number of lines = 32KB / 32B = 1024 - Number of sets = 1024 / 4 = 256 → Set bits = log₂(256) = 8 bits - Offset bits = log₂(32) = 5 bits - Tag bits = 32 − (8 + 5) = 19 bits
Q2. A 2-way set associative cache has 512 lines total, block size = 64 bytes, and the system has a 36-bit address. Find tag size.
Solution
: - Number of sets = 512 / 2 = 256 → Set bits = log₂(256) = 8 bits - Offset bits = log₂(64) = 6 bits - Tag bits = 36 − (8 + 6) = 22 bits
Q3. How many comparators are needed for an 8-way set associative cache?
Solution
: 8 comparators (one per line within each accessed set — sets are selected directly by index, only the k lines within that set are compared in parallel).
Q4. Main memory = 64 MB, byte addressable, cache = 128 KB, block size = 16 bytes, 4-way set associative. Find the total address bits, set bits, offset bits, and tag bits.
Solution
: - Address bits = log₂(64M) = 26 bits - Offset bits = log₂(16) = 4 bits - Number of lines = 128KB / 16B = 8192; Number of sets = 8192 / 4 = 2048 → Set bits = log₂(2048) = 11 bits - Tag bits = 26 − (4 + 11) = 11 bits
⚠️ Common Traps & Exam Tips
- Always compute Number of Sets = Total Lines / k BEFORE computing set bits — forgetting to divide by k is the single most common set-associative mistake. - When k = 1 → Set-Associative reduces to Direct Mapping; when k = total lines → reduces to Fully Associative — a very common conceptual MCQ. - Number of comparators in k-way set associative = k (NOT total number of lines in cache) — since only the target SET's lines are searched, not the whole cache.
7. Cache Replacement Policies & Miss Types
7.1 Why Replacement Policies?
When a cache miss occurs and the target set/cache is full, a replacement policy decides which existing block to evict to make room for the new block. (Only relevant for associative & set-associative mapping — direct mapping has no choice, the line is fixed.)
7.2 FIFO (First-In-First-Out)
Evicts the block that has been in the cache longest, regardless of usage frequency.
Worked Trace — Reference string: 1 2 3 4 1 2 5 1 2 3 4 5, Cache capacity = 3 frames
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Frames | 1 | 1,2 | 1,2,3 | 2,3,4 | 2,3,4 | 2,3,4 | 3,4,5 | 3,4,5 | 3,4,5 | 3,4,5→4,5,3? | ... | ... |
| Hit/Miss | M | M | M | M(evict 1) | H | H | M(evict 2) | H | H | H | M(evict 3) | M(evict 4) |
(Total misses = 8, Hits = 4 for this trace — demonstrates FIFO's "Belady's anomaly" susceptibility.)
7.3 LRU (Least Recently Used)
Evicts the block that has not been used for the longest time (tracks recency of access).
Worked Trace — Reference string: 1 2 3 1 4, Cache capacity = 3 frames
| Ref | 1 | 2 | 3 | 1 | 4 |
|---|---|---|---|---|---|
| Cache state | {1} | {1,2} | {1,2,3} | {1,2,3} (hit, 1 now most recent) | {2,3,4} (evict 2 — least recently used) |
| Hit/Miss | M | M | M | H | M |
7.4 LFU (Least Frequently Used)
Evicts the block with the lowest access frequency count. Ties broken by LRU or FIFO typically.
7.5 Optimal / Belady's Algorithm
Evicts the block that will not be used for the longest time in the future — theoretically optimal, but requires future knowledge (used only as a benchmark, not implementable in practice).
Worked Trace — Reference string: 1 2 3 4 1 2 5 1 2 3 4 5, Cache capacity = 3 frames
| Ref | 1 | 2 | 3 | 4 | 1 | 2 | 5 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Cache | 1 | 1,2 | 1,2,3 | 1,2,4(evict3,not needed soonest) | 1,2,4 | 1,2,4 | 1,2,5(evict 4) | 1,2,5 | 1,2,5 | 1,2,5→ evict... | ... | ... |
| Hit/Miss | M | M | M | M | H | H | M | H | H | M | M | H |
(Optimal gives the fewest possible misses = 7 for this trace — always ≤ FIFO/LRU/LFU for the same reference string.)
7.6 Comparison of Replacement Policies
| Policy | Basis for Eviction | Implementation Complexity | Performance |
|---|---|---|---|
| FIFO | Age (arrival order) | Simple (queue) | Moderate, prone to Belady's anomaly |
| LRU | Recency of use | Moderate-Complex (needs counters/stack) | Good, approximates optimal well |
| LFU | Frequency of use | Complex (needs frequency counters) | Good for stable access patterns, poor for changing patterns |
| Optimal | Future knowledge | Not implementable (theoretical) | Best possible (used as a benchmark) |
7.7 The 3 C's of Cache Misses
| Type | Definition | Example |
|---|---|---|
| Compulsory (Cold) | Occurs on the very FIRST access to a block (unavoidable — block was never in cache) | First loop iteration touching a new array |
| Capacity | Occurs because the cache cannot contain all blocks needed during execution, even with optimal/fully-associative mapping | Working set larger than cache size |
| Conflict | Occurs because multiple blocks map to the same set/line (direct/set-associative only) even though the cache overall has free space elsewhere | Two frequently used blocks alternately evict each other in direct mapping |
⚠️ Common Traps & Exam Tips
- Belady's Anomaly: In FIFO, increasing cache size can sometimes increase the number of misses — a classic exception to intuition, tested often. LRU and Optimal do NOT suffer from this anomaly (they follow the "stack property"). - Compulsory misses cannot be eliminated by any mapping technique or larger cache (except pre-fetching); Conflict misses can be eliminated by moving to fully associative mapping; Capacity misses can only be reduced by increasing cache size. - Fully associative and set-associative caches never have conflict misses if k equals the block's needs... more precisely, direct mapping has ALL three miss types; fully associative eliminates conflict misses entirely.
8. Memory Organisation & Performance
8.1 Average Memory Access Time (AMAT) Formula & Derivation
When a request is made, the CPU first checks the cache. If it's a hit, access time = cache hit time. If it's a miss, additional time (miss penalty) is incurred to fetch from the next level.
📦 Formula Box ``AMAT = Hit Time + (Miss Rate × Miss Penalty)``
Derivation logic: On average, every access incurs the hit time (since we always check cache first). Additionally, a fraction of accesses (the miss rate) also incur the miss penalty (extra time to fetch from the next slower level).
8.2 Multi-Level Cache Performance
For a 2-level cache system (L1, L2):
📦 Formula Box ``AMAT = Hit Time_L1 + Miss Rate_L1 × (Hit Time_L2 + Miss Rate_L2 × Miss Penalty_L2)``
This generalizes recursively for L3, main memory, etc. — each level's "miss penalty" is effectively the AMAT of the next level down.
Solved Numericals
Q1. Cache hit time = 2 ns, miss penalty = 20 ns, hit ratio = 0.9. Find AMAT.
Solution
: AMAT = 2 + (1 − 0.9) × 20 = 2 + 0.1×20 = 2 + 2 = 4 ns
Q2. A system has cache access time 5 ns, main memory access time 100 ns, hit ratio = 0.95. Find AMAT.
Solution
: Miss penalty = Main memory time (since on a miss, we still pay cache check + full memory access — assume miss penalty = memory access time here for simplicity as commonly asked) AMAT = 5 + (1 − 0.95) × 100 = 5 + 5 = 10 ns
Q3. Two-level cache: L1 hit time = 1 ns, L1 hit ratio = 0.9; L2 hit time = 10 ns, L2 hit ratio = 0.8; Main memory access time = 100 ns. Find AMAT.
Solution
: AMAT = HitTime_L1 + MissRate_L1 × (HitTime_L2 + MissRate_L2 × MemTime) = 1 + 0.1 × (10 + 0.2 × 100) = 1 + 0.1 × (10 + 20) = 1 + 0.1 × 30 = 1 + 3 = 4 ns
Q4. If hit ratio improves from 0.9 to 0.98 in Q1's system (hit time 2ns, miss penalty 20ns), what is the new AMAT and % improvement?
Solution
: New AMAT = 2 + (1−0.98)×20 = 2 + 0.4 = 2.4 ns Improvement = (4 − 2.4)/4 × 100 = 40% reduction in AMAT
Q5. A processor has a cache with 95% hit rate, hit time 1 cycle, miss penalty 50 cycles. Compute average cycles per memory access.
Solution
: AMAT = 1 + 0.05 × 50 = 1 + 2.5 = 3.5 cycles
⚠️ Common Traps & Exam Tips
- Never forget the "Hit Time" is paid on EVERY access (hit or miss) — a very common error is to compute AMAT as just MissRate × MissPenalty, forgetting the base hit time term. - In multi-level cache formulas, "Miss Penalty" for level 1 = the AMAT-like cost of accessing level 2 (which itself includes ITS hit time + its own miss handling) — treat it recursively, level by level. - Watch units carefully — mixing nanoseconds and clock cycles without conversion is a common numerical trap.
9. Coherence & Write Policy
9.1 Write-Through vs Write-Back
| Feature | Write-Through | Write-Back |
|---|---|---|
| Mechanism | Every write updates BOTH cache and main memory immediately | Write updates ONLY cache; main memory updated later (on eviction) |
| Consistency | Main memory always up-to-date (simpler consistency) | Main memory may be temporarily stale (needs "dirty bit") |
| Speed | Slower writes (memory access every time) | Faster writes (memory accessed only on eviction) |
| Memory traffic | Higher (frequent memory writes) | Lower (memory writes only when dirty blocks evicted) |
| Complexity | Simple (no dirty bit tracking needed) | More complex (requires dirty bit per line) |
| Data loss risk on crash | Lower (memory always current) | Higher (unflushed dirty data lost on power failure) |
9.2 Write Allocate vs No-Write Allocate
| Policy | Behavior on WRITE MISS |
|---|---|
| Write Allocate | Block is FIRST loaded into cache, then written to (commonly paired with Write-Back) |
| No-Write Allocate | Block is written DIRECTLY to main memory, bypassing cache (commonly paired with Write-Through) |
9.3 Cache Coherence Problem in Multiprocessor Systems
- In multiprocessor systems, each CPU core may have its own private cache holding a copy of the same memory block.
- Cache Coherence Problem: If one processor updates its cached copy, other processors' cached copies become stale/inconsistent unless properly synchronized.
- This can lead to different processors reading different values for what should be the same memory location — a correctness-breaking issue in parallel systems.
9.4 Basic Coherence Protocols Overview
- Coherence protocols ensure all caches see a consistent view of memory, typically via:
- Snooping protocols: Each cache "listens" (snoops) on a shared bus for write operations by other caches and invalidates/updates its own copy accordingly.
- Directory-based protocols: A central directory tracks which caches hold copies of each block, coordinating invalidations/updates without needing a shared bus (scales better for large multiprocessor systems).
- MESI Protocol (a widely used snooping-based protocol) — each cache line is in one of 4 states:
- M (Modified): Line is modified (dirty), present only in this cache; memory is stale.
- E (Exclusive): Line is clean and present only in this cache (not modified).
- S (Shared): Line is clean and may be present in multiple caches.
- I (Invalid): Line is not valid (stale/unused) in this cache.
⚠️ Common Traps & Exam Tips
- Write-Back needs a dirty bit; Write-Through does NOT — a direct and frequently tested MCQ fact. - Write-Allocate ↔ Write-Back and No-Write-Allocate ↔ Write-Through are the typical (not mandatory) pairings — questions sometimes test the exception, so read carefully. - MESI states — remember the mnemonic Modified, Exclusive, Shared, Invalid; "Exclusive" means clean+only copy (often confused with "Modified" which means dirty+only copy).
Quick Revision Summary All Formulas Together
1━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━2 CHIP CONFIGURATION3 Address lines (k) = log₂(Number of locations)4 Chip capacity (bits) = 2^k × n5
6 HIT / MISS RATIO7 Hit Ratio (h) = Hits / Total References8 Miss Ratio = 1 − h9
10 ADDRESS FIELD BREAKDOWN (ALL MAPPING TYPES)11 Offset bits = log₂(Block size)12
13 DIRECT MAPPING14 Line bits = log₂(Number of cache lines)15 Tag bits = Total address bits − (Offset + Line bits)16 Cache Line Number = Block Address MOD Number of Lines17
18 FULLY ASSOCIATIVE MAPPING19 Tag bits = Total address bits − Offset bits20 Comparators needed = Number of cache lines21
22 SET-ASSOCIATIVE MAPPING (k-way)23 Number of Sets = Total Lines / k24 Set bits = log₂(Number of Sets)25 Tag bits = Total address bits − (Offset + Set bits)26 Comparators needed = k27
28 AMAT (SINGLE LEVEL)29 AMAT = Hit Time + Miss Rate × Miss Penalty30
31 AMAT (MULTI-LEVEL, e.g. L1+L2)32 AMAT = HitTime_L1 + MissRate_L1 × (HitTime_L2 + MissRate_L2 × MemTime)33━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━Key Conceptual Recap
- Mapping techniques: Direct (1 line, rigid) → Set-Associative (k lines/set, balanced) → Fully Associative (any line, flexible but costly).
- Replacement policies: FIFO (age-based, Belady's anomaly risk) < LFU (frequency-based) < LRU (recency-based, close to optimal) < Optimal (theoretical benchmark, fewest misses).
- 3 C's of misses: Compulsory (first access, unavoidable) → Capacity (cache too small) → Conflict (mapping restriction, direct/set-associative only).
- Write policies: Write-Through (memory always current, slower) vs Write-Back (faster, needs dirty bit, risk of data loss).
- Coherence: Needed in multiprocessor systems to keep multiple cached copies consistent — via Snooping (bus-based) or Directory-based protocols; MESI is the classic 4-state snooping protocol.
Mastery check
How well can you recall this?
Finished this lesson?