---
title: Problem set 10
heading: "Problem set 10: caches, virtual memory and simulation"
eyebrow: C8 · Problem set 10
type: problems
---

::: hook
These problems use lessons 44 to 49: set-associative caches, replacement and write policies, levels of cache, virtual memory with page tables and TLBs, and the cache, predictor and pipeline simulators of the lab. Addresses are 32 bits unless a problem says otherwise, and 1 KiB is 1024 bytes.
:::

::: pool
pick: 12

::: check
kind: parts
given: C = 16 | 64 | 512; L = 32 | 64 | 128; N = 2 | 8; let sets = C*1024/(L*N)

## A {C} KiB, {N}-way cache has {L}-byte lines.

::: part
answer: log(sets)/log(2)
tolerance: 0.5%

### How many index bits?

::: misses
- [log(C*1024/L)/log(2)] That indexes every line; with {N} ways the index picks a set.
- [any] Not yet. $\log_2$(sets).
:::
:::

::: part
answer: 32 - log(sets)/log(2) - log(L)/log(2)
tolerance: 0.5%

### How many tag bits?

::: misses
- [any] 32 − index − offset.
:::
:::
:::

::: check
kind: parts
given: A = 50000 | 123456 | 9000000; L = 32 | 64; S = 128 | 512

## A cache has {S} sets of {L}-byte lines. A load reads address {A}.

::: part
answer: floor(A/L) - S*floor(A/(L*S))
tolerance: 0

### Which set does it use?

::: misses
- [floor(A/L)] Take the line number modulo the number of sets.
- [any] Not yet. ⌊A/L⌋ mod S.
:::
:::

::: part
answer: floor(A/(L*S))
tolerance: 0

### What tag value is stored for it?

::: misses
- [any] The address with the offset and index bits removed: ⌊A/(L·S)⌋.
:::
:::
:::

::: check
kind: parts
given: t1 = 1 | 4; m1 = 0.02 | 0.06; t2 = 10 | 14; m2 = 0.2 | 0.5; P = 100 | 300

## L1: {t1} cycles, {m1} miss rate. L2: {t2} cycles, local miss rate {m2}. Memory: {P} cycles.

::: part
answer: t1 + m1*(t2 + m2*P)
unit: cycles
tolerance: 0.5%

### What is the AMAT?

::: misses
- [t1 + m1*t2 + m2*P] Only L1 misses reach L2, so its misses are a fraction of those.
- [any] Not yet. $t_1 + m_1(t_2 + m_2P)$.
:::
:::

::: part
answer: m1*m2*100
unit: %
tolerance: 0.5%

### What percentage of all accesses go to memory?

::: misses
- [m2*100] That's the fraction of L1 misses that miss again.
- [any] Not yet. $m_1 m_2$.
:::
:::
:::

::: check
kind: parts
given: b = 1 | 1.2; r = 0.25 | 0.4; m = 0.02 | 0.05; P = 50 | 150

## A processor has a base CPI of {b}, makes {r} data accesses per instruction (plus one instruction fetch, which always hits), and its data cache misses {m} of the time with a {P}-cycle penalty.

::: part
answer: b + r*m*P
tolerance: 0.5%

### What is the CPI including data misses?

::: misses
- [b + m*P] Only {r} of the instructions access data.
- [any] Not yet. base + accesses × miss rate × penalty.
:::
:::

::: part
answer: r*m*P/(b + r*m*P)*100
unit: %
tolerance: 0.5%

### What percentage of the cycles are spent waiting for misses?

::: misses
- [any] Miss cycles over all cycles.
:::
:::
:::

::: check
kind: parts
given: n = 2000 | 8192; e = 2 | 4 | 8; L = 32 | 64; p = 3 | 10

## A loop reads all {n} elements of a {e}-byte array in order, {p} times. The array is line-aligned and the cache, much larger than the array, starts empty with {L}-byte lines.

::: part
answer: n*e/L
tolerance: 0.5%

### How many misses?

::: misses
- [p*n*e/L] The array stays in the cache after the first pass.
- [n] Each miss brings a whole line.
- [any] Not yet. One per line, on the first pass only.
:::
:::

::: part
answer: e/(L*p)*100
unit: %
tolerance: 0.5%

### What is the miss rate over all the reads?

::: misses
- [any] Misses over n·p reads.
:::
:::
:::

::: check
kind: parts
given: k = 20 | 500; w = 4 | 16

## A loop stores to {w} different words of the same 64-byte line, {k} times each, then the line is evicted.

::: part
answer: k*w
tolerance: 0

### How many writes reach memory with a write-through cache?

::: misses
- [any] Every store goes through.
:::
:::

::: part
answer: 1
tolerance: 0

### How many line write-backs with a write-back cache?

::: misses
- [w] The words share one line, written back once when evicted.
- [any] Not yet. One dirty line.
:::
:::
:::

::: check
kind: parts
given: v = 3000000 | 74565000 | 2882400000

## A program touches virtual address {v:hex8}, with 4 KiB pages.

::: part
answer: floor(v/4096)
base: 16
width: 6

### What is the virtual page number?

::: misses
- [any] Shift right by 12.
:::
:::

::: part
answer: v - 4096*floor(v/4096)
base: 16
width: 3

### What is the offset?

::: misses
- [any] The low 12 bits.
:::
:::
:::

::: check
kind: parts
given: p = 77 | 498 | 4097; o = 16 | 2604 | 4000; g = 4096

## A virtual page is mapped to physical page {p:hex5}; the access is at offset {o:hex3} in a {g}-byte page.

::: part
answer: p*g + o
base: 16
width: 8

### What is the physical address?

::: misses
- [p + o] The page number sits above the 12 offset bits.
- [any] Not yet. PPN × 4096 + offset.
:::
:::

::: part
answer: o
base: 16
width: 3

### What are its low 12 bits?

::: misses
- [any] The offset passes through translation unchanged.
:::
:::
:::

::: check
kind: parts
given: n = 32 | 39 | 48; g = 12 | 21; e = 4 | 8

## A machine has {n}-bit virtual addresses, $2^{\given{g}}$-byte pages and {e}-byte page-table entries.

::: part
answer: n - g
tolerance: 0

### How many bits is the virtual page number?

::: misses
- [any] Address bits minus offset bits.
:::
:::

::: part
answer: log(2^(n - g)*e)/log(2)
tolerance: 0.5%

### A single-level table would be $2^x$ bytes. What is x?

::: misses
- [any] $\log_2$ of entries × entry size.
:::
:::
:::

::: check
kind: parts
given: h = 0.97 | 0.995; M = 80 | 150; n = 2 | 3 | 4

## A 1-cycle TLB hits {h} of the time; memory takes {M} cycles; the page table has {n} levels.

::: part
answer: h*(1 + M) + (1 - h)*(1 + n*M + M)
unit: cycles
tolerance: 0.5%

### What is the average access time?

::: misses
- [any] Hits: 1 + M; misses: 1 + nM + M.
:::
:::

::: part
answer: (1 - h)*n*M/(h*(1 + M) + (1 - h)*(1 + n*M + M))*100
unit: %
tolerance: 0.5%

### What percentage of that time is page-table walking?

::: misses
- [any] (1 − h)·nM over the total.
:::
:::
:::

::: check
kind: parts
given: E = 64 | 128 | 1024; P = 4 | 16 | 2048

## A TLB has {E} entries and pages are {P} KiB.

::: part
answer: E*P
unit: KiB
tolerance: 0.5%

### What is its reach?

::: misses
- [any] Entries × page size.
:::
:::

::: part
answer: E*P/4
tolerance: 0.5%

### How many 4 KiB pages' worth of memory is that?

::: misses
- [any] The reach over 4 KiB.
:::
:::
:::

::: check
kind: parts
given: W = 9 | 17 | 40; C = 8 | 16 | 32; p = 3 | 5

## The lab's fully associative LRU cache has {C} KiB in 64-byte lines. A loop reads every word of a {W} KiB array, {p} times.

::: part
answer: W*16*(1 + (p - 1)*step(W - C))
tolerance: 0

### How many misses?

::: misses
- [any] Not yet. One per line on the first pass, and on every pass if the array is larger than the cache.
:::
:::

::: part
answer: W*16
tolerance: 0

### How many lines does the array span?

::: misses
- [any] W KiB / 64 bytes.
:::
:::
:::

::: check
kind: parts
given: L = 2 | 3 | 4; M = 80 | 100; h = 98 | 99.5

## A TLB hits {h}% of the time in 1 cycle; a miss walks an {L}-level page table in memory ({M} cycles per access) before the data access.

::: part
answer: L
tolerance: 0

### How many memory accesses does a TLB miss add?

::: misses
- [L + 1] The data access happens either way.
- [any] One per level.
:::
:::

::: part
answer: 1 + M + (1 - h/100)*L*M
tolerance: 0.5%

### What is the average access time, with the data in memory?

::: misses
- [any] 1 + M, plus the walks.
:::
:::
:::

::: check
kind: parts
given: W = 8 | 24 | 40; C = 16 | 32; L = 64

## A loop sweeps {W} KiB repeatedly through a fully associative LRU cache of {C} KiB with 64-byte lines.

::: part
answer: W*1024/64*step(W - C - 0.5)
tolerance: 0

### After the first pass, how many misses does each pass take?

::: misses
- [any] If the data fits, none; if not, LRU evicts every line just before it's needed again.
:::
:::

::: part
answer: W*1024/64
tolerance: 0

### How many does the first pass take?

::: misses
- [any] Every line once.
:::
:::
:::

::: check
kind: parts
given: R = 1600 | 3200; t = 13.75 | 16; h = 50 | 80

## A DDR channel makes {R} million transfers a second over 64 bits; CL = tRCD = tRP = {t} ns, and {h}% of accesses hit an open row (the rest conflict).

::: part
answer: R*8/1000
unit: GB/s
tolerance: 0.5%

### What is its peak bandwidth?

::: misses
- [any] Transfers × 8 bytes.
:::
:::

::: part
answer: h/100*t + (1 - h/100)*3*t + 64/8/R*1000
unit: ns
tolerance: 0.5%

### What is the average time for a 64-byte line, latency plus burst?

::: misses
- [any] Average latency plus 8 transfers.
:::
:::
:::
:::
