FR-FCFS 解説:記憶體控制器如何選擇下一個命令,以及為何會造成飢餓
FR-FCFS Explained: How Memory Controllers Pick the Next Command (and Why It Starves)
FR-FCFS 記憶體排程器會在本週期可合法發出的命令中,優先選擇命中已開啟資料列的命令,同時以等待時間較長者打破平手。這能減少資料列衝突並讓不同記憶體銀行的操作重疊,但持續到達的命中請求可能令其他請求無限期等待;等待時間超過門檻後提高優先級,可避免飢餓。
"How does a memory controller decide what to do next?" is a great interview question because it has no single right answer. But almost every answer starts from the same baseline: FR-FCFS. Here's how it works, the worked example interviewers like, and the flaw they'll ask you to fix.
The problem a scheduler solves
A DRAM bank can hold one open row at a time. A request to that open row (a hit) only needs a column command. A request to a different row (a conflict) needs a precharge, an activate, and then the column command, roughly three times the latency at DDR5-4800 (about 16.7 ns versus 48.7 ns to first data). So the order you serve requests in changes your bandwidth dramatically.
FR-FCFS in one line
First-Ready, First-Come-First-Served (Rixner et al., ISCA 2000): among the commands that can legally issue this cycle, prefer ones that hit an open row; break ties by age.
The "ready" part matters. The scheduler isn't picking whole requests, it's picking the next legal command, so work in different banks overlaps.
A worked example
Bank 0 has row 5 open. Four requests are queued, oldest first:
| Request | Bank | Row | Arrived |
|---|---|---|---|
| A | 0 | 5 | t = 1 |
| B | 0 | 7 | t = 2 |
| C | 0 | 5 | t = 3 |
| D | 1 | 2 | t = 4 |
Plain FCFS serves A, then B (precharge row 5, activate row 7), then C, which now needs row 5 back: another precharge and activate. Two expensive conflicts.
FR-FCFS serves A, then C, because both are hits on the open row. Then B, paying one conflict instead of two. Meanwhile D's activate to bank 1 can issue in parallel, because bank 1 is idle and the command is ready. That's bank-level parallelism falling out of "first ready."
The flaw: starvation
Suppose requests to row 5 keep arriving. Every one is a hit, so FR-FCFS keeps choosing them, and B, the second-oldest request in the queue, waits indefinitely. In a real system that might be the display controller, and a starved display shows up as screen glitches.
The fix interviewers want to hear: an age cap. Once a request has waited past a threshold, it wins regardless of hit status. Real controllers go further, with per-requestor QoS classes and priority for latency-sensitive traffic.
Two more details that score points
- Write draining. Switching the data bus between reads and writes costs turnaround time, so controllers buffer writes and drain them in bursts between a high and a low watermark.
- Refresh is a scheduling input. A good scheduler avoids opening a row in a bank that's about to be force-precharged for refresh.
How to answer in an interview
Start from FR-FCFS, walk a tiny example like the one above, name starvation without being asked, and propose the age cap. Then say what you'd measure (bandwidth, row-hit rate, worst-case latency) to tune it.
Free cheat sheet
Hit, empty, and conflict latencies, every DDR5-4800 timing parameter, and scheduling rules of thumb on one page: download it free. For the full treatment with 30 interview questions and model answers, see the DRAM & DDR5 Interview Field Guide.
This article was generated with AI from the author's study notes.
來源:DEV Community · dev.to