mem โ Memory System
The complete memory subsystem of gem5: ports, packets, caches, coherence protocols, DRAM controllers, and Memory Policies | gem5 Wiki" href="qos/index.html">QoS policies.
Table of Contents
Overview
Think of the gem5 memory system as a postal service. A CPU generates a Packet (letter), which travels through Ports (mailboxes). The letter may be intercepted by a Cache (a local post office that keeps copies of popular items), or it may travel all the way to DRAM (the central warehouse). The Ruby system adds a full coherence protocol network โ like a fleet of delivery trucks following traffic rules to prevent duplicate deliveries.
The src/mem/ directory contains all memory-related simulation models in gem5. It spans from the low-level Packet and Port abstractions through full cache hierarchies (Classic and Ruby), DRAM/NVM controllers, QoS policies, and memory probes.
Architecture Diagram
flowchart LR
CPU["CPU Core"]
ICache["L1 ICache"]
DCache["L1 DCache"]
L2["L2 Cache"]
Bus["Coherent XBar"]
MC["Memory Controller"]
DRAM["DRAM / HBM"]
Ruby["Ruby Network"]
Dir["Directory Controller"]
QoS["QoS Policy"]
CPU -->|"RequestPort"| ICache
CPU -->|"RequestPort"| DCache
ICache -->|"ResponsePort"| L2
DCache -->|"ResponsePort"| L2
L2 -->|"RequestPort"| Bus
Bus -->|"ResponsePort"| MC
MC --> QoS
QoS --> DRAM
CPU -.->|"Alternative"| Ruby
Ruby --> Dir
Dir --> DRAM
Data Flow: Memory Request Lifecycle
sequenceDiagram
participant CPU
participant L1 as L1 DCache
participant L2 as L2 Cache
participant XBar as Coherent XBar
participant MC as MemCtrl
participant DRAM
CPU->>L1: sendReq(ReadReq pkt)
L1->>L1: Tag lookup
alt Cache Hit
L1-->>CPU: sendResp(ReadResp)
else Cache Miss
L1->>L2: sendReq(ReadReq)
L2->>L2: Tag lookup
alt L2 Hit
L2-->>L1: sendResp(ReadResp)
L1->>L1: Allocate block
L1-->>CPU: sendResp(ReadResp)
else L2 Miss
L2->>XBar: sendReq(ReadReq)
XBar->>MC: forward(ReadReq)
MC->>DRAM: access()
DRAM-->>MC: data
MC-->>XBar: sendResp(ReadResp)
XBar-->>L2: sendResp(ReadResp)
L2->>L2: Allocate block
L2-->>L1: sendResp(ReadResp)
L1->>L1: Allocate block
L1-->>CPU: sendResp(ReadResp)
end
end
Narrated Walkthrough: Cache Miss Request
Follow a ReadReq packet from the CPU through to DRAM and back:
Packet with command ReadReq and calls sendTimingReq() on its data port.CpuSidePort::recvTimingReq() handler in BaseCache begins processing.CacheBlk is looked up via the indexing policy (e.g., set-associative).MSHR (Miss Status Holding Register) to track the outstanding request.MemSidePort using sendTimingReq().MemCtrl places the request in its read queue. QoS policies may reorder requests based on priority.ReadResp back to the CPU via sendTimingResp(), completing the memory access.Key Components
| Class / File | Header | Description |
|---|---|---|
Packet | packet.hh | Core data transfer unit carrying command, address, data, and flags across the memory system. |
MemCmd | packet.hh | Enumeration of all memory commands: ReadReq, WriteReq, WritebackDirty, Snoop*, etc. |
Request | request.hh | Encapsulates the original CPU request with virtual/physical addresses and flags. |
RequestPort | port.hh | Initiator-side port that sends requests and receives responses. |
ResponsePort | port.hh | Target-side port that receives requests and sends responses. |
BaseCache | cache/base.hh | Abstract base for all classic caches; manages tags, MSHRs, write buffers. |
Cache | cache/cache.hh | Coherent cache supporting snooping, invalidation, and flexible topologies. |
NoncoherentCache | cache/noncoherent_cache.hh | Simplified cache without coherence protocol โ used for instruction caches or accelerators. |
MSHR | cache/mshr.hh | Miss Status Holding Register โ tracks pending cache misses and coalesces duplicate requests. |
AbstractMemory | abstract_mem.hh | Base class for all memory backends (SimpleMemory, DRAM, NVM). |
MemCtrl | mem_ctrl.hh | Main memory controller with read/write queuing, scheduling, and timing. |
DRAMInterface | dram_interface.hh | Cycle-accurate DRAM timing model for DDR3/DDR4/DDR5/LPDDR/HBM. |
HBMCtrl | hbm_ctrl.hh | Dual-channel HBM controller leveraging pseudo-channel architecture. |
CommMonitor | comm_monitor.hh | Transparent probe inserted between ports to gather statistics. |
PacketQueue | packet_queue.hh | Queue of packets with scheduling logic for deferred sends. |
Sub-module Deep Dives
cache
Classic cache hierarchy: BaseCache, Cache, NoncoherentCache, MSHR, write buffers, replacement policies (LRU, BIP, BRRIP, SHiP, TreePLRU, โฆ), prefetchers (Stride, BOP, AMPM, SPP, โฆ), compressors, and tag organizations.
ruby
Protocol-driven coherence simulation using SLICC. Includes network models (Atomic and Timing Simple CPUs โ gem5 Wiki" href="../cpu_doc/simple/index.html">simple, Garnet), sequencers, directory controllers, message buffers, GPU coalescer, and HTM support.
qos
Quality of Service policies for memory controllers: fixed-priority, proportional-fair scheduling, turnaround policies, and queue management for multi-tenant workloads.
probes
Memory probe infrastructure: MemTraceProbe for trace generation, StackDistProbe for reuse-distance analysis, MemFootprintProbe for working-set tracking.
protocol
Low-level protocol definitions: atomic.hh, functional.hh, timing.hh โ the three transport interfaces that ports implement for different simulation modes.
Controllers & Interfaces
DRAM controllers (MemCtrl, HBMCtrl, HeteroMemCtrl), external interfaces (DRAMSim2, DRAMSim3, DRAMSys), serial links, and the XBar system bridges.
Directory Map
Configuration Reference
| Parameter | System & Standard Library ยท gem5 Wiki" class="xref">SimObject | Description | Default |
|---|---|---|---|
size | Cache | Total cache capacity | 64kB (L1), 2MB (L2) |
assoc | Cache | Set associativity | 2 (L1), 8 (L2) |
tag_latency | Cache | Tag array access cycles | 2 |
data_latency | Cache | Data array access cycles | 2 |
response_latency | Cache | Extra response delay | 2 |
mshrs | Cache | Number of MSHRs | 4 |
write_buffers | Cache | Write buffer entries | 8 |
replacement_policy | Cache | Replacement policy object | LRURP |
prefetcher | Cache | Prefetcher object | None |
mem_ranges | AbstractMemory | Address ranges served | โ |
device_size | DRAMInterface | Per-device capacity | Varies by config |
tCK | DRAMInterface | Clock period | Technology-dependent |
Glossary
- MSHR
- Miss Status Holding Register โ tracks outstanding cache misses and coalesces duplicate requests to the same block.
- Snooping
- A coherence mechanism where caches monitor (snoop) bus transactions to maintain data consistency.
- SLICC
- Specification Language for Implementing Cache Coherence โ a domain-specific language used by Ruby to define protocols.
- Garnet
- A detailed on-chip network model within Ruby that models routers, links, and virtual channels.
- XBar
- Crossbar interconnect that routes packets between multiple ports based on address ranges.
- QoS
- Quality of Service โ policies that prioritize or schedule memory requests based on requestor identity or priority.
- Clusivity
- Describes cache inclusion policy: inclusive (all L1 data in L2), exclusive (no duplication), or mostly-inclusive.