Level 2 — RecallMemory Hierarchy & Caches

Memory Hierarchy & Caches

30 minutes50 marksprintable — key stays hidden on paper

Level 2 — Recall & Standard Problems Time limit: 30 minutes Total marks: 50


Q1. Define the two forms of the principle of locality and give one concrete example of each. (4 marks)

Q2. A byte-addressable machine has a direct-mapped cache with the following parameters:

  • Total cache data size = 16 KB
  • Cache line (block) size = 32 bytes
  • Physical address = 32 bits

Compute: (a) number of cache lines, (b) number of index bits, (c) number of block-offset bits, (d) number of tag bits. (6 marks)

Q3. State the three classic categories of cache misses (the "3 Cs") and give a one-line definition of each. (6 marks)

Q4. Compare write-through vs write-back caches. For each, state (a) when main memory is updated and (b) one advantage. (4 marks)

Q5. A single-level cache has:

  • hit time = 2 ns
  • miss rate = 5%
  • miss penalty = 100 ns

(a) Compute the AMAT. (b) If the miss rate is halved to 2.5%, recompute AMAT and state the improvement in ns. (5 marks)

Q6. Consider a 2-way set-associative cache with the following access sequence to set 0 (addresses map to the same set), using LRU replacement with 2 lines per set:

Block references (to set 0): A, B, A, C, B, A

List, after each reference, the contents of the set and whether the access was a hit or miss. State the total number of misses. (6 marks)

Q7. Define write-allocate and no-write-allocate policies. Which one is normally paired with write-back, and why? (4 marks)

Q8. A two-level cache hierarchy has:

  • L1 hit time = 1 ns, L1 miss rate = 10%
  • L2 hit time = 10 ns, L2 (local) miss rate = 20%
  • main memory access time = 100 ns

Compute the AMAT (measured from L1). (5 marks)

Q9. In the MESI protocol, name the four states and state, for each, whether the line may be dirty and whether other caches may hold a copy. (6 marks)

Q10. What is a TLB, and why does it improve performance in a paged virtual-memory system? Give one sentence on what happens on a TLB miss. (4 marks)


End of paper.

Answer keyMark scheme & solutions

Q1 (4 marks)

  • Temporal locality (1): if a memory location is accessed, it is likely to be accessed again soon. Example (1): a loop counter / repeatedly used variable, instructions in a loop.
  • Spatial locality (1): if a location is accessed, nearby locations are likely to be accessed soon. Example (1): sequential array traversal, sequential instruction fetch.

Q2 (6 marks)

  • Number of lines = cache size / line size = 16KB/32=16384/32=51216\text{KB}/32 = 16384/32 = 512 lines. (2)
  • Index bits = log2512=9\log_2 512 = 9 bits. (1)
  • Block-offset bits = log232=5\log_2 32 = 5 bits. (1)
  • Tag bits = 3295=1832 - 9 - 5 = 18 bits. (2)

Why: offset selects a byte within a line, index selects the line, remaining high-order bits are the tag.


Q3 (6 marks) (2 each)

  • Compulsory (cold) miss: first-ever reference to a block; unavoidable regardless of cache size.
  • Capacity miss: cache cannot hold all blocks of the working set; occurs even with full associativity.
  • Conflict (collision) miss: multiple blocks map to the same set and evict each other; would not occur in a fully associative cache of equal size.

Q4 (4 marks)

  • Write-through: (a) memory updated on every write, immediately. (b) Advantage: simpler, memory always consistent / easier coherence. (2)
  • Write-back: (a) memory updated only when a dirty line is evicted. (b) Advantage: fewer memory writes / lower bandwidth. (2)

Q5 (5 marks) AMAT = hit time + miss rate × miss penalty. (a) 2+0.05×100=2+5=7 ns2 + 0.05 \times 100 = 2 + 5 = \mathbf{7\ ns}. (2) (b) 2+0.025×100=2+2.5=4.5 ns2 + 0.025 \times 100 = 2 + 2.5 = \mathbf{4.5\ ns}. (2) Improvement = 74.5=2.5 ns7 - 4.5 = \mathbf{2.5\ ns}. (1)


Q6 (6 marks) LRU, 2 ways. (MRU listed first.)

Ref Result Set contents (MRU→LRU)
A miss (compulsory) A
B miss (compulsory) B, A
A hit A, B
C miss (evict B, LRU) C, A
B miss (evict A, LRU) B, C
A miss (evict C, LRU) A, B

Total misses = 5 (1 hit). (mark: 0.5–1 per correct row + 1 for total)


Q7 (4 marks)

  • Write-allocate: on a write miss, the block is loaded into the cache first, then written. (1.5)
  • No-write-allocate: on a write miss, the write goes directly to memory; the block is not brought into the cache. (1.5)
  • Pairing: write-allocate is normally paired with write-back, because subsequent writes to the same block hit in cache and are absorbed (accumulated) before a single write-back, exploiting locality. (1)

Q8 (5 marks) AMAT (from L1) = L1 hit + L1 miss rate × (L2 hit + L2 miss rate × mem). =1+0.10×(10+0.20×100)= 1 + 0.10 \times (10 + 0.20 \times 100) =1+0.10×(10+20)= 1 + 0.10 \times (10 + 20) =1+0.10×30= 1 + 0.10 \times 30 =1+3=4 ns= 1 + 3 = \mathbf{4\ ns}. (5)


Q9 (6 marks) (1.5 each)

  • Modified (M): dirty = yes; other copies = no (exclusive, owner must write back).
  • Exclusive (E): dirty = no (clean); other copies = no.
  • Shared (S): dirty = no (clean); other copies = yes (may exist).
  • Invalid (I): line holds no valid data.

Q10 (4 marks)

  • TLB (Translation Lookaside Buffer): a small, fast cache of recent virtual-to-physical page-number translations (page-table entries). (2)
  • Why faster: avoids walking the page table in memory on every access; a TLB hit provides the physical frame in ~1 cycle. (1)
  • On a TLB miss: the page table is walked (by hardware or OS) to fetch the translation, which is then inserted into the TLB. (1)

[
  {"claim":"Q2 tag bits = 18","code":"lines=16*1024//32; index=int(__import__('math').log2(lines)); offset=int(__import__('math').log2(32)); tag=32-index-offset; result = (lines==512 and index==9 and offset==5 and tag==18)"},
  {"claim":"Q5 AMAT values 7 and 4.5, improvement 2.5","code":"a=2+0.05*100; b=2+0.025*100; result = (a==7 and b==4.5 and (a-b)==2.5)"},
  {"claim":"Q8 two-level AMAT = 4 ns","code":"amat=1+0.10*(10+0.20*100); result = (amat==4)"},
  {"claim":"Q6 total misses = 5","code":"seq=['A','B','A','C','B','A']; cache=[]; misses=0\nfor x in seq:\n    if x in cache:\n        cache.remove(x); cache.insert(0,x)\n    else:\n        misses+=1\n        if len(cache)>=2: cache.pop()\n        cache.insert(0,x)\nresult = (misses==5)"}
]