S343 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

S3 · Lesson 43 · Caches in hardware

Tag and data arrays, address fields, comparators, write policies and miss handling

C8 treated a cache as a policy: which lines it keeps and how often it hits. Built in RTL, a cache is two SRAM arrays (one of tags, one of data), a row of comparators, a multiplexer to pick the hitting way, and a state machine that handles misses by fetching a line from memory while the processor waits. The hardware view adds costs the policy view hides. Every line carries a tag, a valid bit and usually a dirty bit, so a 32 KB cache is really more than 33 KB of SRAM. Every extra way adds a comparator and a wider multiplexer on the hit path, and every miss costs the memory's latency plus the time to move a whole line.

  • The storage overhead of a cache, the tag, valid and dirty bits as a percentage of the data bits, against the line size.
  • The tag width depends only on the address width and the size of one way: tag = address bits − log2⁡(bytes per way). Bigger lines spread it over more data bits.
  • More ways at the same size shrink each way, so the tags grow, and each way needs its own comparator.

Predict

A 32 KB, 8-way cache with 64-byte lines serves 32-bit addresses. How wide is each tag?

Name it

Address fields

For a cache of S bytes, lines of B bytes, W ways and A-bit addresses:

  • offset: log2⁡B bits;
  • sets: SBW, index: log2⁡SBW bits;
  • tag: A−index−offset bits.

The arrays and the hit path

  • Data array: S bytes. Tag array: one entry per line, SB entries of tag + valid (+ dirty for write-back, + replacement state).
  • Hit path: the index reads all W tags and all W data words of a set at once; W comparators check the tags in parallel; a W-to-1 multiplexer picks the data. Associativity adds comparators and multiplexer delay to every access, which is why first-level caches stay at 4 to 12 ways.
  • Large caches access tags first and then only the matching data way, saving power at the cost of a cycle.

Writes

  • Write-through: every store also goes to the next level, usually through a write buffer. Simple, but the store traffic is the full store bandwidth.
  • Write-back: stores update only the cache and set the dirty bit; a dirty line is written back when evicted.
  • Write-allocate or not: whether a store miss brings the line in.

Misses

A miss state machine requests the line, waits out the memory latency, then moves the line over a bus of width w bytes:

penalty=tlatency+Bwcycles

Critical word first returns the requested word first and lets the processor go on, cutting the wait to about latency + 1. Non-blocking caches keep handling hits while misses are outstanding, tracked in miss status holding registers (MSHRs). AMAT = hit time + miss rate × penalty.

Worked example

For the 32 KB, 8-way, 64-byte-line cache with 32-bit addresses and write-back, how much SRAM is there in all, and how many comparator bits?

Show the solution
  1. Bits per line. 512 lines, each with 512 data bits, a 20-bit tag, a valid bit and a dirty bit: 512+20+1+1=534bits
  2. Total. N=512×534=273,408bits=33.4KB a 4.3% overhead (before replacement bits).
  3. Hit path. 8 comparators of 20 bits each, 160 bits of comparison on every access.

Answer 273,408 bits (33.4 KB) of SRAM, and 160 comparator bits.

Worked example

A miss costs 50 cycles of latency, and the line comes over a 16-byte bus. With a 1-cycle hit and a 3% miss rate, what's the AMAT, with and without critical word first?

Show the solution
  1. Miss penalty. The latency plus the transfer of a 64-byte line: 50+6416=54cycles
  2. AMAT. AMAT=1+0.03×54=2.62cycles
  3. With critical word first. The processor resumes after about 51 cycles: AMAT=1+0.03×51=2.53cycles
  4. What dominates. The latency does; a bigger cache or prefetching would help more than a wider bus.

Answer 2.62 cycles, or 2.53 with critical word first.

Worked example

A 64 KB, 4-way cache with 32-byte lines serves 40-bit physical addresses and is write-back. Find its address fields and the size of its tag array.

Show the solution
  1. Offset. 32-byte lines need 5 bits.
  2. Index. There are 65536/32=2048 lines, in 512 sets: 9 index bits.
  3. Tag. 40−9−5=26bits
  4. Tag array. Each line stores tag, valid and dirty, 28 bits: 2048×28=57344bits which is 7 KB, 11% on top of the data.
  5. Longer lines. They would shrink that overhead and raise the miss penalty.

Answer 5 offset bits, 9 index bits and a 26-bit tag; the tag array holds 57 344 bits (7 KB).

Check

A 64 KB, 16-way cache has 32-byte lines and 40-bit addresses. How many tag bits?

Check

How many index bits does a 32 KB, 8-way cache with 32-byte lines need?

Check

A write-back cache of 16 KB, 8-way, with 32-byte lines and 48-bit addresses keeps a valid and a dirty bit per line. How many SRAM bits does it hold in all?

Check

A miss waits 80 cycles of latency and then moves a 32-byte line over a 16-byte bus, one transfer per cycle. What's the miss penalty?

Check

A cache hits in 2 cycles and misses 0.01 of the time. A miss waits 150 cycles and then moves a 64-byte line over a 16-byte bus. What's the AMAT?

Check

A core at 1 GHz executes one instruction per cycle, 0.08 of them stores of 8 bytes. How much write traffic does a write-through cache send to the next level?

Check

A 32 KB, 8-way cache with 32-byte lines serves 40-bit addresses.

(a)

How many offset bits?

(b)

How many index bits?

(c)

How many tag bits?

Check

A cache hits in h cycles and misses with probability m; a miss waits L cycles of latency and moves a B-byte line over a w-byte bus.

Step 1

What is the miss penalty?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does making a first-level cache more associative eventually slow down every access, even the hits?

Practice

Keep practicing

10 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.