The Write Buffer and Its Read Hazard
A write buffer takes slow memory writes out of the way of the processor. It also creates a short time in which memory is wrong, and a read can fall into it.
You Will Be Able To
- Say what a write buffer saves, and where it sits.
- Explain how a read can return old data while a write waits in the buffer.
- Compare the two cures: wait for the buffer, or look in it.
- Lesson
- S3.18
- Time
- 24 min
- Runs on
- Your browser
- Check
- 6 questions
A program stores 2222 in a word and reads the word back in the next line of code. It gets 1111, the old value. It reads again and gets 1111 again. Memory holds 2222 by then, and the program will never see it.
Before You Start
You need S3.17: a write must reach memory, and a memory write is slow. You need S3.5: a read that misses fetches from memory.
Nothing else is needed.
| Question | |
|---|---|
| 1 | What does a write buffer save? |
| 2 | Why can memory hold old data while the buffer is not empty? |
| 3 | When does a read return a stale word? |
| 4 | Which two cures are there, and what does each cost? |
| 5 | What must a buffer answer when it is asked for an address? |
The Idea: A Waiting Room for Writes, and a Read That Jumps the Queue
Some writes must go to memory: every write under write-through, and every dirty block under write-back. Memory is slow. If the processor waited for each of these, the gain of the cache would be lost on writes.
So a cache puts a write buffer between itself and memory. It is a small store for an address and its data. A write goes into it within one cycle, and the processor goes on. The buffer hands the write to memory when memory has time.
The saving has a price that is easy to miss. While a write waits, memory holds the old data. The newest value of that word is in the buffer and nowhere else. This lasts only a few cycles. It is still long enough for a read.
A read that hits in the cache is safe: the line was updated by the write. A read that misses goes to memory. If it asks for an address whose write is still waiting, memory gives the old word. The read has passed the write. A word that is old in this way is called stale.
There are two cures. The read can wait until the buffer is empty, which is simple and slow. Or the read can ask the buffer first: is a write to my address waiting here? If so, the buffer hands over the data, and memory is not asked at all.
New Words
| Word | Meaning |
|---|---|
| Write buffer | A small store between a cache and memory. It holds writes, each an address and its data, until memory has taken them. |
| Entry of a buffer | One place in the buffer: one address and one word of data. |
| Waiting write | A write that is in the buffer and has not reached memory yet. |
| Stale data | Data that was correct once and has been replaced since. A read that returns it is a stale read. |
| Read hazard | The danger that a read goes to memory while a write to the same address still waits. |
| Empty the buffer | To wait until memory has taken every waiting write. |
| Look in the buffer | To compare the address of a read with the addresses of the waiting writes. On a match, the data is taken from the buffer. |
One Picture: The Write Buffer and the Read That Passes
- Processor
- Memory
- Bus
- Control / FSM
- A write without a buffer
A write that goes to memory makes the processor wait until memory has taken it. Memory is slow, so that is many cycles.
- The write buffer
Put a small store in between. The write goes in within one cycle, and the processor goes on. The buffer passes the write to memory in the background.
- Memory is behind
While the write waits in the buffer, memory still holds the old word. For those cycles the newest data is in the buffer, and nowhere else.
- A read passes the write
Now the processor reads the same address. The cache does not have it, so the read goes to memory. Memory answers with the old word.
- The cure
A read that misses must respect the buffer. Either it waits until the buffer is empty, or it looks in the buffer first and takes the word from there.
Read the diagram as text
- Processor: writes, then reads
- Cache: does not hold this word
- Write buffer: an address and a word that wait
- Memory: slow: still holds the old word
- Look in the buffer: compare the address of the read
- Processor → Write buffer: a write: one cycle
- Write buffer → Memory: later, in the background
- Processor → Cache: a read
- Cache → Memory: a miss goes to memory
- Cache → Look in the buffer: a miss asks here first
- Look in the buffer → Write buffer: same address?
A write buffer between a cache and memory. The lab below has a buffer with one entry.
The Smallest Example, Worked by Hand
Time is counted in ticks. Memory needs 6 ticks to take a write and 4 ticks to answer a read. Word 20 of memory holds 1111, and it is not in the cache. The program writes 2222 to word 20, and reads word 20 straight after.
| Tick | The buffer | Memory word 20 | The read |
|---|---|---|---|
| 1 | 2222 for word 20 goes in | 1111 | starts: a miss, so it asks memory |
| 2 to 4 | waits | 1111 | waits for memory |
| 5 | waits | 1111 | memory answers: 1111 |
| 6 | memory takes the write: empty | 2222 | too late: 1111 is in the cache now |
The read ends at tick 5. The write reaches memory at tick 6. The read was faster by one tick.
The read returned 1111, and the cache kept it. From tick 6 on memory holds 2222, but every later read of word 20 hits in the cache and gets 1111. One tick of bad luck has become a permanent fault.
Now the two cures, on the same two steps.
| Cure | What the read does | The read ends at tick | Data |
|---|---|---|---|
| None | goes to memory at once | 5 | 1111, stale |
| Wait for the buffer | waits until tick 6, then asks memory | 10 | 2222 |
| Look in the buffer | finds word 20 in the buffer | 2 | 2222 |
Waiting costs 5 more ticks. Looking costs a comparator for each entry, and it is even faster than memory.
The Rules
Rule 1: A write buffer lets the processor go on after one cycle.
The buffer passes the write to memory in the background.
Rule 2: While a write waits in the buffer, memory holds old data for that address.
The newest value is in the buffer, and nowhere else.
Rule 3: A read that hits in the cache is safe. A read that goes to memory is not.
It returns stale data if a write to its address still waits.
Rule 4: A stale read is kept: the old word goes into the cache.
Later reads hit on it, long after memory has become right.
Rule 5: Cure one: empty the buffer before any read goes to memory.
Simple. Every such read waits for the slow writes in front of it.
Rule 6: Cure two: look in the buffer first, and answer from it on a match.
The buffer must give the word that memory would hold once the buffer is empty.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.3, "Handling Writes". The write buffer holds data that waits to be written to memory, so that the processor can go on. Section 5.4 counts the stalls of a full write buffer.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.4, on write policies.
The books describe the write buffer and what it saves. The read hazard and its two cures are common practice in cache design. They are given here as such, not checked against a book. The plan of this course lists the hazard for this lesson.
The times of 6 and 4 ticks, the buffer of one entry and the cache of one line are our own teaching example.
Now Run It
The lab at the end of this page is test code. It has a memory, a cache of one line, and a write buffer with one entry. A write that misses goes to the buffer only. Each row gives the tick at which a step ends.
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=5 read 20: miss | data=00001111, fetched from memory
t=6 read 20: hit | data=00001111
t=16 the end | memory word 20 holds 00002222, and the cache holds 00001111Rules 1 and 2 at work: the first row. The write is done at tick 1, as far as the processor can tell. Memory will not have it before tick 6.
Rule 3 at work: the second row. The read is a miss, goes to memory, and ends at tick 5 with 1111. Rule 4 at work: the third row. The next read is a hit, and it returns 1111 again.
The last row is the proof. Memory word 20 holds 2222, and the cache holds 1111. The two will not agree again until the line is replaced.
Here is the same run with the first cure, and then with the second:
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=10 read 20: miss | data=00002222, fetched from memory
t=11 read 20: hit | data=00002222
t=21 the end | memory word 20 holds 00002222, and the cache holds 00002222
t=1 write 20 <= 00002222 | into the buffer, the processor goes on
t=2 read 20: miss | data=00002222, taken from the buffer
t=3 read 20: hit | data=00002222
t=13 the end | memory word 20 holds 00002222, and the cache holds 00002222Rule 5 at work: with FIX = 1 the read ends at tick 10 and returns 2222. Rule 6 at work: with FIX = 2 it ends at tick 2, with the word taken from the buffer.
This lab leaves out a buffer with several entries, and writes of single bytes. Both make the look harder, and the practice problem of this page is about the first.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A write buffer | The processor waits for every memory write | A write costs one cycle. Memory is behind for a while, and reads must take care. |
| Look in the buffer | Empty the buffer first | No waiting. One address comparator for each entry, on the path of every miss. |
| Empty the buffer first | Look in the buffer | Almost no logic. A read behind a write is as slow as that write. |
| Several entries | One entry | Several writes in a row fit. The look must then choose among several matches. |
The Trap: The Test That Was Too Polite
The team that built the buffer with no cure had a test for it. The test wrote a word, did five ticks of other work, and read the word back. It passed every night. Here is that test in the lab:
t=1 write 20 <= 00003333 | into the buffer, the processor goes on
t=10 read 20: miss | data=00003333, fetched from memory
t=20 the end | memory word 20 holds 00003333, and the cache holds 00003333The read starts at tick 6. By then memory has taken the write, so the read returns 3333 and all is well. The hazard is only open while the write waits, and the test never looked inside that time.
Real programs are not polite. A compiler stores a value and loads it again in the next instruction all the time. Rule 3 names the exact case to test: a read that misses in the cache, for an address that is still in the buffer.
At Scale
This section looks ahead. Real write buffers have several entries, and the same hazard appears in other places.
Several matches
Two waiting writes can have the same address. The look must then give what memory will hold in the end.
Parts of a word
A waiting write of one byte and a read of the whole word match only in part. Many designs empty the buffer in that case.
A full buffer
When writes arrive faster than memory takes them, the buffer fills up, and the processor must wait after all.
The same hazard elsewhere
The wrapper in front of an SRAM had it in a small form: a read that meets a write waited one cycle.
Try It
Run the lab. Check the ticks against the hand table.
Set FIX to 1 and then to 2. Predict the tick and the data of the first read each time.
Set WRITE_TICKS to 3 with FIX at 0. Predict the data of the first read, and say why the fault has gone into hiding.
t=1 write 20 <= 00002222 | into the buffer, the processor goes on t=5 read 20: miss | data=00001111, fetched from memory t=6 read 20: hit | data=00001111 t=16 the end | memory word 20 holds 00002222, and the cache holds 00001111Check Yourself
Common Mistakes
Wrong Let a read that misses go to memory without a look at the buffer.
Memory holds old data while a write waits.
Fix Look in the buffer first, or wait until it is empty.
Wrong Test the buffer with a pause between write and read.
The hazard exists only while the write waits.
Fix Read the same address at once, with the word not in the cache.
Wrong Think of a stale read as a short glitch.
The old word is stored in the cache, and later reads hit on it.
Fix Treat it as lost data: it stays wrong until the line is replaced.
Remember
- A write buffer lets the processor go on after one cycle. Memory gets the write later.
- While a write waits, memory is stale for that address.
- A read that misses must wait for the buffer, or look in it first.
- A stale read is stored in the cache, so the fault stays.