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

C8 · Lesson 45 · Writes, coherence and DMA

What happens to stores, and who else might be looking at memory

A cache is easy to reason about while the processor only reads: the cached copy and memory always agree. Stores break that. A write-back cache holds data newer than memory, and a DMA controller or a second core reading memory directly sees the old values. Going the other way, a DMA controller that writes fresh data into memory leaves stale copies in the cache. Getting writes right is about traffic and speed, and also about correctness whenever anything besides the one core touches memory.

  • Each line of the cache is filled from scratch by a loop that stores to a fraction x of its words, k times each, before the line is evicted.
  • Write-through sends every store to memory: k · x · L bytes per line. Write-back with write-allocate first reads the whole line in, then writes the whole line back once: 2L bytes, however many stores there were.
  • With a single pass over every word (k = 1, x = 1), write-back moves twice as much data. From k = 2 on it wins, and the more often each word is rewritten, the more it wins.

Predict

A loop writes 4000 bytes into a fresh array, each word once, through a write-back, write-allocate cache with 64-byte lines. About how many bytes cross the memory bus?

Name it

On a store miss: allocate or not

  • Write-allocate brings the line into the cache, then writes it. It pays a line read for each store miss, which wins when the line will be read or written again soon.
  • No-write-allocate (write-around) sends the store to memory and leaves the cache alone. It wins for data written once and not read back, such as an output buffer.

Write-back caches usually allocate; write-through caches usually don't.

Write buffers

A write-through cache would make the processor wait for memory on every store. A write buffer, a small queue of pending stores, lets it continue while memory catches up. It only hides the delay on average: if stores arrive faster than memory accepts them, the buffer fills and the processor stalls. With a store fraction f, one instruction a cycle and memory taking m cycles per write, the buffer keeps up only while f⋅m<1.

Dirty evictions lengthen misses

In a write-back cache, a miss that evicts a dirty line has to write it back before (or while) the new line comes in. If a fraction d of evicted lines are dirty and each transfer takes P,

average miss penalty≈P(1+d)

unless a buffer lets the write-back happen later.

Coherence with DMA

A DMA controller reads and writes memory directly, bypassing the cache. Two operations keep the cache honest:

  • Clean (write back) the lines of a buffer before a DMA controller reads it, so memory holds what the processor wrote.
  • Invalidate the lines of a buffer after a DMA controller writes it, so the processor's next loads miss and fetch the new data.

DMA buffers should start on a line boundary and fill whole lines. Invalidating a line that also holds an unrelated variable would throw away that variable's newest value. Some systems instead mark DMA buffers as uncacheable, or have hardware that snoops DMA traffic.

Coherence between cores

With several cores, each with its own cache, a coherence protocol keeps their copies consistent. The usual rule is write-invalidate: before a core writes a line, every other cached copy is invalidated, so there's only ever one writer and readers get the newest value on their next miss. Each line is in a state such as Modified (dirty, only copy), Shared (clean, maybe in several caches) or Invalid. Course S4 goes into the protocols.

Worked example

A store-heavy loop is 12% stores at one instruction a cycle, through a write-through cache whose write buffer drains one store every 10 cycles. What happens?

Show the solution
  1. Compare the rates. Stores arrive at 0.12 per cycle, but memory accepts only 0.1.
  2. The buffer fills. However deep the buffer, it eventually fills, and from then on the processor can only issue a store when one drains.
  3. The throughput. At most 0.1 stores a cycle means at most 0.10.12=0.83 instructions a cycle, a CPI of 1.2 instead of 1.
  4. Write-back. A write-back cache would absorb most of those stores, since loops tend to write the same lines repeatedly.

Answer the write buffer fills, and the processor slows to 0.83 instructions a cycle, a CPI of 1.2.

Worked example

A cache misses 5% of the time with a 100-cycle penalty, and 30% of evicted lines are dirty. What's the average access time, with a 1-cycle hit, if write-backs aren't buffered?

Show the solution
  1. The average miss. Every miss fetches a line, and 30% also write one back first: 100(1+0.3)=130cycles
  2. AMAT. AMAT=1+0.05×130=7.5cycles against 6 if no line were ever dirty.
  3. A victim buffer. A victim buffer that holds the dirty line while the new one is fetched recovers most of the difference.

Answer 7.5 cycles, against 6 if no line were ever dirty.

Worked example

A network controller writes a 1500-byte frame by DMA into a buffer, with 64-byte cache lines. How many lines must the driver invalidate, and what goes wrong if the buffer starts 20 bytes into a line?

Show the solution
  1. Aligned. The frame covers ⌈150064⌉=24lines
  2. Starting 20 bytes in. It covers ⌈1520/64⌉ = 24 lines too, but the first one also holds 20 bytes of something else.
  3. What goes wrong. If the processor had written to those 20 bytes and the line was dirty, invalidating it discards the processor's write; cleaning it first would instead overwrite the start of the frame in memory.
  4. The cure. Neither is right, which is why DMA buffers are aligned to lines and padded to whole lines.

Answer 24 lines; starting 20 bytes in, the first line also holds other data, so invalidating it loses the processor's write and cleaning it overwrites the frame.

Check

A loop stores 4096 words (4 bytes each) into a fresh array, once each, with 32-byte lines.

(a)

How many bytes reach memory through a write-through, no-allocate cache?

(b)

And through a write-back, write-allocate cache?

Check

A program is 20% stores; memory accepts one write every 8 cycles through a write buffer.

(a)

How many stores per cycle would it issue at one instruction a cycle?

(b)

What's the lowest CPI the memory allows once the buffer is full?

Check

A write-back cache misses 5% of the time with a 50-cycle penalty, and 20% of evicted lines are dirty. Hits take 1 cycle.

(a)

What's the average miss penalty without a write buffer?

(b)

What's the AMAT?

Check

A DMA controller fills a 1500-byte receive buffer, and the cache has 32-byte lines.

(a)

How many lines must be invalidated if the buffer starts on a line boundary?

(b)

And if it starts 20 bytes into a line?

Check

A DMA controller has just written new samples into a buffer the processor read earlier. What must the driver do before the processor reads it?

Check

The processor has filled a transmit buffer through a write-back cache and is about to start a DMA transfer that reads it. What must it do first?

Check

Cores 1 and 2 both have line X cached. Core 1 stores to X. Under a write-invalidate protocol, what happens?

Check

Before eviction, a loop stores k times to each of a fraction x of a freshly allocated line's words; the line is L bytes.

Step 1

How many bytes does write-through send?

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 must a driver invalidate a DMA receive buffer's cache lines, and why should the buffer be aligned to whole lines?

Practice

Keep practicing

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