from now on it's in exame

A write buffer removes most of the time the CPU would otherwise spend waiting on writes. A merging write buffer (also called write merging) makes it more efficient still. The exercise at the end builds on both.
With a write-through L1, every store must also be written to the next level (L2 or memory), which is slow. Without help, the CPU would wait on every store.
A write buffer is a small queue between L1 and the next level:
The CPU waits only when the buffer is full.





Each buffer entry holds a whole block's worth of space, for example 4 words of 8 bytes, with a valid bit per word. When a new store arrives, the buffer checks: does a valid entry already cover this address?
Without merging, each store takes its own entry even when the stores are to neighboring addresses, so the buffer fills up quickly.
In the simulator below, the stores come faster than the buffer can drain. Run the same 9 stores with merging off, then on, and compare the stalls.
Two more points worth knowing:

L1 is write-through. 95% of the time the store can go into the write buffer; 5% of the time the buffer is full. How do you compute the write time?
Case A, buffer has space (95%): the store goes into L1 and the buffer, and the CPU continues. The time is just the L1 write time, the L1 hit time.
Case B, buffer is full (5%): the CPU must wait until an entry is written to L2 and freed. That wait is the time to write one entry into L2, including L2 misses:
T_L2write = hit time L2 + miss rate L2 × miss penalty L2
Write time = 0.95 × T_L1 + 0.05 × (T_L1 + T_L2write)
= T_L1 + 0.05 × T_L2write
Every store pays the L1 time, and 5% of stores also pay the full drain time.
If the question asks for stall time only (as in the earlier homework):
Write stall time = 0.95 × 0 + 0.05 × T_L2write
L1 hit time = 1 ns, L2 hit time = 15 ns, L2 local miss rate = 20%, L2 miss penalty = 150 ns.
T_L2write = 15 + 0.20 × 150 = 15 + 30 = 45 ns
Write time = 1 + 0.05 × 45 = 1 + 2.25 = 3.25 ns
Write stall time = 0.05 × 45 = 2.25 ns
Read the question for what to include. "Stall time only" → 2.25 ns. "Write time to L1" including the access → 3.25 ns. Show the formula so you get credit either way.
The waiting time depends on the stated assumption. In your homework, the store arrives just as the buffer starts writing an entry to L2, so it waits the entire 45 ns. If a problem instead says a store arrives at a random point during the drain, the average wait would be about half of that. Use exactly what the problem states.
The L1 miss rate is not used for writes when the L1 is write-through, no-write-allocate. Every store goes into the buffer whether it hits or misses in L1. If a problem says write-allocate, write misses would also pay an L1 miss penalty.
Don't forget the L2 miss part of T_L2write. Using only the 15 ns L2 hit time (giving 0.05 × 15 = 0.75 ns) is a common mistake. L2 is write-allocate, so 20% of buffer writes miss in L2 and pay the 150 ns.


The notes write miss_rate(RLL + %dirtyD), which means miss_rate × (RLL + %dirty × D).
The one idea behind every equation
AMAT = (time every access pays) + Σ (how often an extra event happens) × (how long it takes)
Every access pays its hit time (RH or WH). Then you ask which extra trips to the lower level this access can cause, and how often each one happens. There are only four possible trips:
Fetching a block (RLL). Happens when a miss brings the block into the cache. Reads always do this. Writes do it only with write-allocate. It happens only on misses, so it is multiplied by miss_rate.
Writing back a dirty victim (D). When a block is brought in, another is thrown out. Only write-back caches have dirty blocks, so D appears only for write-back, only inside the miss term, and only %dirty of the time.
Sending a write down (WLL).
A read miss always brings the block in, whatever the write policy. Write-allocate vs. no-allocate decides what happens on write misses only, so it never changes the read formula. Only one thing matters for reads: can the replaced block be dirty?

Write-back + no-write-allocate still has the dirty term. No-allocate only means write misses don't bring blocks in. Write hits still mark blocks dirty, so a later read miss can evict a dirty block.
Write-through + write-allocate: WH + WLL + miss_rate × RLL
Write-through + no-write-allocate: WH + WLL
Write-back + write-allocate: WH + miss_rate × (RLL + %dirty × D)
Write-back + no-write-allocate: WH + miss_rate × WLL
The write buffer only affects writes going down. Find every WLL and D term and multiply it by (1 − %wb). Leave RH, WH, and RLL alone.

That turns all 8 buffer formulas from your notes into the 8 non-buffer ones with one change each. For write-through reads nothing changes at all, since there is no WLL or D in them.
Where (1 − %wb) comes from: with probability %wb the write drops into the buffer and the CPU waits 0. With probability (1 − %wb) the buffer is full, and the CPU waits the whole write time:
%wb × 0 + (1 − %wb) × WLL = (1 − %wb) × WLL
질문 ① 미스 때 블록을 가져오나? → miss_rate × RLL
질문 ② 블록을 가져올 때, 밀려나는 블록이 dirty일 수 있나? → + %dirty × D (미스 항 안에)
질문 ③ 쓰기가 아래로 내려가나? → WLL
질문 ④ 쓰기 버퍼가 있나? → WLL과 D 앞에 (1 − %wb)



Write through + write allocate
- Read AMAT = RH + miss_rate*RLL
- Write AMAT= WH + WLL + miss_rate*RLL
Write through + no write allocate
- Read AMAT = RH + miss_rate*RLL
- Write AMAT = WH + WLL
Write back + write allocate
- Read AMAT = RH + miss_rate*(RLL+%dirty*D)
- Write AMAT = WH + miss_rate*(RLL+%dirty*D)
Write back + no write allocate
- Read AMAT = RH + miss_rate*(RLL+%dirty*D)
- Write AMAT = WH + miss_rate*WLL
Write through + write allocate + write buffer
- Read AMAT = RH + miss_rate*RLL
- Write AMAT= WH + (1-%wb)*WLL + miss_rate*RLL
Write through + no write allocate + write buffer
- Read AMAT = RH + miss_rate*RLL
- Write AMAT = WH + (1-%wb)* WLL
Write back + write allocate + write buffer
- Read AMAT = RH + miss_rate*(RLL+%dirty* (1-%wb)* D)
- Write AMAT = WH + miss_rate*(RLL+%dirty* (1-%wb)* D)
Write back + no write allocate + write buffer
- Read AMAT = RH + miss_rate*(RLL+% dirty* (1-%wb)* D)
- Write AMAT = WH + miss_rate* (1-%wb)* WLL