H10 — The Memory Controller Inside the CPU: IMC Architecture and Core Interaction
Key Insight: The best memory control makes the CPU unaware of latency.
Location of the Memory Controller (IMC)
The Memory Controller is the bridge between the CPU and DRAM. Modern CPUs integrate the IMC on the CPU die.
CPU 芯片架构(以 Intel Skylake 为例):
┌─────────────────────────────────────────────────┐
│ CPU Package │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ Core 0 │ │ Core 1 │ │ Core 2 │ ... │
│ │ L1/L2 │ │ L1/L2 │ │ L1/L2 │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ ┌────┴─────────────┴──────────────┴────┐ │
│ │ Ring Bus / Mesh Interconnect │ │
│ └────┬─────────────┬──────────────┬────┘ │
│ │ │ │ │
│ ┌────┴────┐ ┌────┴────┐ ┌─────┴─────┐ │
│ │ IMC │ │ System │ │ PCIe │ │
│ │ Channel0 │ │ Agent │ │ Controller│ │
│ │ Channel1 │ │ │ │ │ │
│ └─────────┘ └──────────┘ └───────────┘ │
└─────────────────────────────────────────────────┘
IMC(Integrated Memory Controller)的位置:
- IMC 在 CPU 芯片上(Integrated)
- 通过 Ring Bus(核心数少)或 Mesh(核心数多)和核心连接
- 直接连接到 DDR PHY(物理层),再到 DIMM 插槽
vs 早期架构(North Bridge):
- 早期 CPU 是"CPU + North Bridge + South Bridge"分离
- 内存控制器在 North Bridge 里
- CPU 和内存之间通过 FSB(Front Side Bus)通信
- 延迟高(~80ns),带宽低Memory Controller Architecture
IMC 的内部结构(DDR4 双通道):
┌──────────────────────────────────────────────────┐
│ Memory Controller │
│ │
│ ┌────────────┐ ┌────────────┐ │
│ │ Channel 0 │ │ Channel 1 │ │
│ │ DDR4-3200 │ │ DDR4-3200 │ │
│ │ 64-bit │ │ 64-bit │ │
│ └──────┬──────┘ └──────┬──────┘ │
│ │ │ │
│ ┌────┴──────────────────┴────┐ │
│ │ Address Decode / Routing │ │
│ └─────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────┐ │
│ │ Read/Write Queue (Scheduler) │ │
│ │ - 命令重排(Command Reordering) │ │
│ │ - 行冲突优化(Bank Conflict) │ │
│ │ - 读优先(Read Prioritization) │ │
│ └────────────────────────────────────────┘ │
│ │
│ ┌────────────────────────────────────────┐ │
│ │ ECC Engine(纠错) │ │
│ │ - SECDED 计算 │ │
│ │ - 地址保护 │ │
│ └────────────────────────────────────────┘ │
└──────────────────────────────────────────────────┘IMC Core Functions:
- Address Decoding: Translates physical addresses into Channel/Rank/Bank/Row/Column
- Command Scheduling: Reorders memory accesses to minimize row conflicts
- ECC Correction: Computes and checks ECC (RDIMM or ECC UDIMM)
- Timing Control: Sends ACT/READ/WRITE/PRE/REF commands
Memory Controller Scheduler
Memory access scheduling is key to performance.
调度器的优化策略:
1. 命令重排(Command Reordering):
- 尽量把同一行的访问放在一起(行命中)
- 避免行冲突(行切换)
- 把 READ 优先于 WRITE(CPU 需要数据才能继续)
2. 读优先(Read Prioritization):
- 读操作通常影响指令执行(stall)
- 写操作可以延迟(Write Buffer)
- 调度器通常优先处理 READ
3. 行冲突优化(Bank Conflict Avoidance):
- 如果两个请求访问不同行,先访问已打开的行
- 把 PRE 和 ACT 延迟到真正需要时(Lazy Precharge)
4. 刷新协调(Refresh Coordination):
- 刷新期间,IMC 会延迟非紧急请求
- 如果刷新和紧急请求冲突,IMC 可能延迟刷新(tREFI 内完成即可)
实际调度效果:
请求序列(无调度):
Request A: Row=1 → ACT → READ → PRE
Request B: Row=2 → PRE → ACT → READ → PRE
Request C: Row=1 → ACT → READ
→ 总时间:3×ACT + 3×PRE + 2×READ = ~100ns
请求序列(有调度):
Reorder: C 先(A 已经有 Row 1 打开)
Request C: Row=1(打开)→ READ(直接读)
Request A: Row=1(打开)→ READ(直接读)
Request B: Row=2 → ACT → READ → PRE
→ 总时间:2×READ + ACT + READ + PRE = ~60ns
优化效果:减少 40% 延迟IMC-Core Interconnect: Ring Bus vs Mesh
多核 CPU 的内存控制器连接方式:
Ring Bus(Intel Haswell ~ Coffee Lake,4~8 核):
┌─────────────────────────────┐
│ Core0 ─── Ring ─── Core1 │
│ │ │ │
│ Core2 ─── Ring ─── Core3 │
│ │ │ │
│ IMC ── Ring ── Cache Agent │
└─────────────────────────────┘
- 所有核心和 IMC 都在同一个环上
- 访问延迟 = 跳数 × 每跳延迟
- 最坏情况:4 核系统,Core 0 到 IMC 要跳 2 站
- 典型延迟:50~60ns(L3 miss + IMC)
Mesh(Intel Skylake,10+ 核):
┌───────┬───────┬───────┬───────┐
│ Core0 │ Core1 │ Core2 │ Core3 │
├───┬───┼───┬───┼───┬───┼───┬───┤
│IMC│ │ │ │ │ │ │ IMC│
├───┴───┼───┴───┼───┴───┼───┴───┤
│ Core4 │ Core5 │ Core6 │ Core7 │
└───────┴───────┴───────┴───────┘
- 每行/列交叉点是路由器
- 路径可以绕过拥塞点
- 延迟增长比 Ring Bus 慢(O(sqrt(n)) vs O(n))
- 典型延迟:45~55ns(取决于位置)
AMD Zen 架构的 Infinity Fabric:
- 每 4 核(CCD)一个 Fabric 节点
- 节点之间用 Infinity Fabric 互联
- 延迟取决于跨节点访问的跳数Memory Controller and NUMA
On multi-socket servers (2P/4P), each CPU has its own IMC:
NUMA(Non-Uniform Memory Access):
2 路服务器(2 CPUs):
CPU 0:
- 核心 0~7
- IMC → Channel 0~3(本地内存)
- 连接到 CPU 1 的 UPI 链路
CPU 1:
- 核心 8~15
- IMC → Channel 0~3(本地内存)
- 连接到 CPU 0 的 UPI 链路
访问延迟:
- CPU 0 访问本地内存:~60ns
- CPU 0 访问 CPU 1 的内存:~100ns(跨 UPI)
NUMA 感知操作系统:
- Linux 可以用 numactl 绑核和内存在同一节点
- 进程尽量访问本地内存,避免跨节点访问
# 将进程绑定到 Node 0
numactl --membind=0 --cpunodebind=0 ./my_program
这就是为什么高性能数据库需要 NUMA 优化——
把数据放在访问它的 CPU 对应的内存节点上。Summary
- IMC Location: Integrated on the CPU die (modern Intel/AMD CPUs); connected to cores via Ring Bus or Mesh
- IMC Functions: Address decoding, command scheduling (reordering), ECC correction, timing control
- Scheduler Optimization: Command reordering (row hit first), read priority, Lazy Precharge, refresh coordination
- Ring Bus: Suitable for 4-8 cores; latency ~50-60ns; worst-case hops 2-3
- Mesh: Suitable for 10+ cores; latency ~45-55ns; routes can bypass congestion
- NUMA: Each multi-socket CPU has its own IMC; local access is fast, cross-node access is slow (requires numactl optimization)
H-Layer Summary: Hardware layer covers DIMM structure (H01), dual channel (H02), timings (H03), XMP/EXPO (H04), SPD/Training (H05), PCB traces (H06), power (H07), ECC (H08), manufacturing/testing (H09), and IMC architecture (H10). This comprehensively covers the complete chain of memory hardware from physical connection to CPU controller interaction.
Next (B01): How does the computer know how much memory there is and where it is? The e820 memory map. BIOS’s first map for the OS at boot.
Comments