Level 3 — ProductionMemory Hierarchy & Caches

Memory Hierarchy & Caches

45 minutes60 marksprintable — key stays hidden on paper

Level 3 — Production (from-scratch derivations, code-from-memory, explain-out-loud) Time limit: 45 minutes Total marks: 60


Question 1 — Cache addressing derivation from scratch (10 marks)

A processor has a 32-bit byte address. You design a 4-way set-associative cache with total data capacity 32 KiB and a cache line (block) size of 64 bytes.

(a) Derive the number of sets, and the bit-widths of the offset, index, and tag fields. Show the arithmetic. (6)

(b) Compute the total tag-storage overhead in bits, including one valid bit and one dirty bit per line. (4)


Question 2 — AMAT multi-level derivation (12 marks)

A system has this hierarchy (all access times are to reach that level):

  • L1: hit time 1 cycle, miss rate 8%
  • L2: hit time 12 cycles, local miss rate 25%
  • Main memory: access time 200 cycles

(a) Derive the AMAT formula for a two-level cache from first principles, defining local vs global miss rate. (4)

(b) Compute the AMAT in cycles. (4)

(c) A design change reduces the L2 hit time to 10 cycles but raises L1 miss rate to 10%. Recompute AMAT and state whether it is an improvement. (4)


Question 3 — Replacement policy trace (10 marks)

A fully associative cache holds 4 lines. Given the reference stream of block numbers:

1,  2,  3,  4,  1,  2,  5,  1,  2,  3,  4,  51,\;2,\;3,\;4,\;1,\;2,\;5,\;1,\;2,\;3,\;4,\;5

(a) Trace LRU and count hits/misses. (5)

(b) Trace FIFO and count hits/misses. Comment on which performs better here and why. (5)


Question 4 — Write policy & explain-out-loud (8 marks)

(a) Explain, out loud in your own words, the difference between write-through/no-write-allocate and write-back/write-allocate, including what happens on a write miss in each. (5)

(b) For a workload that repeatedly writes the same recently-loaded blocks, which combination minimises memory traffic and why? (3)


Question 5 — MESI coherence trace (12 marks)

Two cores (C0, C1) share a bus with a MESI protocol; a cache line starts invalid in both. Trace the state of the line in each cache and any bus transactions for this sequence:

  1. C0 reads X
  2. C1 reads X
  3. C0 writes X
  4. C1 reads X
  5. C1 writes X

Give a step-by-step table (Core action → C0 state, C1 state, bus action). (9) Then state which single MESI state is unique to the O (Owned) addition in MOESI and what problem it solves. (3)


Question 6 — Virtual memory / TLB from memory (8 marks)

A system uses 48-bit virtual addresses, 4 KiB pages, and a 4-level page table where each level indexes with an equal number of bits and each page-table entry is 8 bytes.

(a) Derive the number of index bits per level and confirm a single page-table node fits in one page. (5)

(b) Explain the role of the TLB and estimate, using AMAT-style reasoning, the effective address-translation time given TLB hit rate 98%, TLB hit cost 1 cycle, and a full 4-level walk cost of 100 cycles on a miss. (3)

Answer keyMark scheme & solutions

Question 1 (10 marks)

(a) Capacity =32 KiB=32768= 32\text{ KiB} = 32768 B. Line size =64= 64 B.

  • Offset bits: log264=6\log_2 64 = 6 bits. (1)
  • Number of lines =32768/64=512= 32768 / 64 = 512 lines. (1)
  • 4-way: number of sets =512/4=128= 512 / 4 = 128 sets. (1)
  • Index bits: log2128=7\log_2 128 = 7 bits. (1)
  • Tag bits =3276=19= 32 - 7 - 6 = 19 bits. (2)

Why: offset selects a byte within a line, index selects a set, tag disambiguates lines mapping to the same set.

(b) Per line overhead =tag+valid+dirty=19+1+1=21= \text{tag} + \text{valid} + \text{dirty} = 19 + 1 + 1 = 21 bits. (2) Total lines =512= 512. Overhead =512×21=10752= 512 \times 21 = 10752 bits =1344= 1344 B. (2)


Question 2 (12 marks)

(a) Local miss rate == misses at that level / accesses that reach that level. Global == misses / total CPU accesses. For two levels: (2) AMAT=tL1+mL1(tL2+mL2tmem)\text{AMAT} = t_{L1} + m_{L1}\,(t_{L2} + m_{L2}\,t_{mem}) where mL2m_{L2} is the local L2 miss rate. (2)

(b) AMAT=1+0.08(12+0.25×200)=1+0.08(12+50)=1+0.08×62=1+4.96=5.96\text{AMAT} = 1 + 0.08(12 + 0.25\times200) = 1 + 0.08(12+50) = 1 + 0.08\times62 = 1 + 4.96 = 5.96 cycles. (4)

(c) New: 1+0.10(10+0.25×200)=1+0.10(10+50)=1+0.10×60=1+6.0=7.01 + 0.10(10 + 0.25\times200) = 1 + 0.10(10+50) = 1 + 0.10\times60 = 1 + 6.0 = 7.0 cycles. (3) 7.0>5.967.0 > 5.96not an improvement; the higher L1 miss rate dominates the small L2 hit-time gain. (1)


Question 3 (10 marks)

Stream: 1 2 3 4 1 2 5 1 2 3 4 5

(a) LRU (4 lines): (4 for trace, 1 for count)

Ref Hit? Cache (LRU→MRU)
1 miss 1
2 miss 1 2
3 miss 1 2 3
4 miss 1 2 3 4
1 hit 2 3 4 1
2 hit 3 4 1 2
5 miss (evict 3) 4 1 2 5
1 hit 4 2 5 1
2 hit 4 5 1 2
3 miss (evict 4) 5 1 2 3
4 miss (evict 5) 1 2 3 4
5 miss (evict 1) 2 3 4 5

Hits = 4, Misses = 8.

(b) FIFO (queue order = insertion): (4 for trace, 1 for count/comment)

Ref Hit? Queue (oldest→newest)
1 miss 1
2 miss 1 2
3 miss 1 2 3
4 miss 1 2 3 4
1 hit 1 2 3 4
2 hit 1 2 3 4
5 miss (evict 1) 2 3 4 5
1 miss (evict 2) 3 4 5 1
2 miss (evict 3) 4 5 1 2
3 miss (evict 4) 5 1 2 3
4 miss (evict 5) 1 2 3 4
5 miss (evict 1) 2 3 4 5

Hits = 2, Misses = 10.

Comment: LRU beats FIFO here because it keeps recently-reused blocks (1,2); FIFO evicts by age regardless of reuse, so it discards still-hot blocks. (1)


Question 4 (8 marks)

(a) (5)

  • Write-through, no-write-allocate: every write updates memory immediately. On a write miss, the block is not loaded into cache — write goes straight to memory. Simpler, always consistent with memory, but high write traffic. (3)
  • Write-back, write-allocate: writes update only the cache line and set a dirty bit; the block is written to memory only on eviction. On a write miss, the block is loaded (allocated) into cache, then written. Lower traffic, needs dirty tracking. (2)

(b) Write-back + write-allocate. Repeated writes to the same block are absorbed in the cache line; memory is touched only once on eventual eviction, minimising traffic. (3)


Question 5 (12 marks)

(9) for table:

Step Action C0 C1 Bus
1 C0 read X E I BusRd (no sharer)
2 C1 read X S S BusRd (C0 sees, downgrades E→S)
3 C0 write X M I BusRdX / BusUpgr (invalidate C1)
4 C1 read X S S BusRd (C0 M→S, supplies data)
5 C1 write X I M BusRdX / BusUpgr (invalidate C0)

(Award ~1.8 per correctly reasoned step.)

MOESI Owned state (3): The O state lets a cache hold a modified-but-shared line (dirty, but other caches may hold read copies in S). It solves the problem that in MESI a dirty line must be written back to memory before it can be shared; O allows the owner to supply dirty data directly to other caches without a write-back, reducing memory traffic. (3)


Question 6 (8 marks)

(a) Page offset =log24096=12= \log_2 4096 = 12 bits. (1) VPN bits =4812=36= 48 - 12 = 36 bits over 4 levels 36/4=9\Rightarrow 36/4 = 9 bits per level. (2) Entries per node =29=512= 2^9 = 512; each entry 8 B 512×8=4096\Rightarrow 512 \times 8 = 4096 B =4= 4 KiB == one page. ✓ (2)

(b) The TLB caches recent virtual→physical page translations so the multi-level page-table walk is skipped on a hit. (1) Effective translation time =0.98×1+0.02×100=0.98+2.0=2.98= 0.98\times1 + 0.02\times100 = 0.98 + 2.0 = 2.98 cycles. (2)


[
  {"claim":"Q1 tag bits = 19", "code":"offset=6; index=7; tag=32-offset-index; result = (tag==19)"},
  {"claim":"Q1 total overhead = 10752 bits", "code":"lines=512; per=19+1+1; result = (lines*per==10752)"},
  {"claim":"Q2b AMAT = 5.96", "code":"amat=1+0.08*(12+0.25*200); result = (abs(amat-5.96)<1e-9)"},
  {"claim":"Q2c AMAT = 7.0 (worse)", "code":"a=1+0.10*(10+0.25*200); result = (abs(a-7.0)<1e-9 and a>5.96)"},
  {"claim":"Q6 index bits per level = 9 and node fits page", "code":"vpn=48-12; per=vpn/4; entries=2**per; result = (per==9 and entries*8==4096)"},
  {"claim":"Q6 effective translation = 2.98 cycles", "code":"t=0.98*1+0.02*100; result = (abs(t-2.98)<1e-9)"}
]