C8 · Lesson 48 · Main memory and buses
How long DRAM really takes, and how data crosses the bus
A memory module's label says DDR4-3200, and its datasheet promises 25.6 GB/s. Yet a single cache miss still takes tens of nanoseconds, about as long as it did twenty years ago. Both numbers are true. DRAM is organized in rows that must be opened before they can be read, so the time to the first byte depends on what the memory was doing before, while the rate at which bytes stream out once a row is open has grown enormously. Processors are built around that difference.
- The curve is the time to get a whole 64-byte line from DRAM, against the fraction of accesses that find their row already open in the row buffer.
- A row hit costs just the CAS latency CL. A row conflict must close the old row (tRP), open the new one (tRCD), then read (CL). Then the 64 bytes stream out in a burst of 8 transfers.
- Raise the transfer rate: the burst shrinks, but the latency hardly changes. Row hits are what make memory fast.
Predict
DDR4-3200 makes 3200 million transfers a second on a 64-bit (8-byte) channel. How long does a 64-byte cache line take to stream out, once the data starts arriving?
2.5 ns: 64 bytes is 8 transfers of 8 bytes, each 0.3125 ns. The wait before the first transfer is the long part: about 14 ns for a row hit and 41 ns for a row conflict at this module's timings.
Name it
How DRAM is organized
A DRAM chip holds its bits in banks, each a grid of rows and columns. To read, the controller first activates a row: the whole row, several kilobytes, is copied into the bank's row buffer by sense amplifiers. Then it reads columns from the buffer. Before a different row can be opened, the current one must be written back and the bank precharged.
| Timing | Meaning |
|---|---|
| CL (CAS latency) | column read to first data, with the row already open |
| tRCD | activate a row before a column can be read |
| tRP | precharge, closing the open row |
So an access costs CL on a row hit, tRCD + CL to an idle bank, and tRP + tRCD + CL on a row conflict. Timings are quoted in clock cycles: DDR4-3200 CL22 runs a 1600 MHz clock, so 22 cycles is 13.75 ns.
Bandwidth
DDR ("double data rate") transfers on both clock edges. Peak bandwidth is
A cache line arrives as a burst: 8 transfers of 8 bytes fill a 64-byte line. Several banks, and several channels, can work on different requests at once, which is how sustained bandwidth approaches the peak.
Latency, bandwidth and parallelism
Little's law ties them together: to keep a memory system with latency T busy at bandwidth B, the processor must have
misses in flight at once. That's why processors track many outstanding misses, prefetch, and run ahead of stalled loads.
Buses and address decoding
On a microcontroller, the processor, DMA controllers, flash, RAM and peripherals share one or more buses. An address decoder looks at the top address bits to choose which device answers: on many ARM chips, 0x0… is flash, 0x2… is SRAM and 0x4… the peripherals. When two masters want the bus at once, an arbiter chooses, so a busy DMA controller can slow the processor.
Wait states
Slow devices make the bus wait. On-chip flash might need 40 ns per read, while a 72 MHz core's cycle is 13.9 ns, so each flash read takes 3 cycles: 2 wait states. Flash interfaces fetch wide words, 128 bits or more, into a prefetch buffer, so straight-line code still runs near one instruction a cycle; branches pay the wait states again.
Worked example
DDR4-3200 CL22 has tRCD = tRP = 22 cycles too, at a 1600 MHz clock. What does a row hit, an access to an idle bank, and a row conflict each cost? What's the average if 60% of accesses hit and the rest conflict?
Show the solution
- Each timing.
- The three cases. A hit costs 13.75 ns, an idle bank 27.5 ns, and a conflict 41.25 ns.
- The average.
- In practice. That's before the 2.5 ns burst and the time spent in the processor's caches and memory controller, which in practice bring a miss to 60 to 100 ns.
Answer 13.75 ns for a row hit, 27.5 ns for an idle bank, 41.25 ns for a row conflict, and 24.75 ns on average.
Worked example
A core has 80 ns of memory latency and a 25.6 GB/s channel. How many 64-byte misses must be in flight to use the full bandwidth?
Show the solution
- Misses in flight.
- One at a time. One miss at a time would reach only 64 bytes per 80 ns, 0.8 GB/s, 3% of the channel.
- Real cores. Out-of-order cores track 10 to 20 misses each, and hardware prefetchers add more, which is why streaming code gets close to peak and pointer-chasing code doesn't.
Answer 32 misses must be in flight.
Worked example
A 72 MHz microcontroller reads flash with a 40 ns access time. How many wait states does it need, and what does a prefetch buffer of four instructions do for straight-line code?
Show the solution
- The cycle time. A cycle is 13.9 ns.
- Cycles per flash read. so the read needs 2 wait states.
- Without help. Every instruction fetch would take 3 cycles.
- With the prefetch buffer. A 128-bit fetch brings in four 32-bit instructions per 3-cycle read, which covers four 1-cycle instructions, so sequential code runs at full speed and only taken branches stall while the buffer refills.
Answer 2 wait states; the prefetch buffer lets straight-line code run at full speed, and only taken branches stall.
Check
A DDR channel makes 3200 million transfers a second, 32 bits wide.
(a)
What's its peak bandwidth?
(b)
How many transfers does a 64-byte line take?
Check
A DRAM has CL = tRCD = tRP = 13.75 ns. 40% of accesses are row hits; the rest are row conflicts.
(a)
What does a row conflict cost to the first data?
Write units the way you normally would: 1.5 ns, 1500 ps and 0.0015 µs are the same answer.
(b)
What's the average?
Check
Flash takes 40 ns per read, and the core runs at 48 MHz.
(a)
How many cycles does a read take?
(b)
How many wait states is that?
Check
On a chip whose address decoder uses bits 31:28 to pick the region, an access goes to 20001000.
(a)
Which region number (0 to 15) does it select?
(b)
What's its offset within the region?
Check
A memory system has latency T and bandwidth B, and moves L-byte lines.
Step 1
What bandwidth does one miss at a time reach?
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°.
Step 2
How many misses must be in flight to reach B?
Check
A channel delivers 25.6 GB/s with 60 ns of latency and 64-byte lines.
(a)
How many misses must be in flight to use it fully?
(b)
What share of the bandwidth does one miss at a time use?
Check
Why has DRAM latency in nanoseconds barely improved for decades, while bandwidth has grown many times?
Explain why
Why does a processor need many cache misses outstanding at once to use a memory channel's bandwidth?
Each miss spends tens of nanoseconds waiting for the DRAM to open a row and return data, but once data flows a 64-byte line streams out in a few nanoseconds. If the processor waits for one miss before starting the next, the channel sits idle almost all the time: one line per latency is a few percent of the peak. Bandwidth is only used if new requests keep arriving while earlier ones are still in progress, so that the channel and the banks always have work. By Little's law the number of requests in flight must equal bandwidth × latency ÷ line size, which for a modern channel is dozens. Out-of-order execution, non-blocking caches and prefetchers exist to keep that many misses going.
Practice
Keep practicing
8 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.