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.

6
Sub-modules
750
Files
Classic + Ruby
Cache Hierarchy | gem5 Wiki" href="cache/index.html">Cache Models
C++
Language
12+
Replacement Policies
14+
Prefetchers
Table of Contents

    Overview

    Analogy

    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

    Click to expand
    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

    Click to expand
    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:

    CPU issues load โ€” The CPU pipeline creates a Packet with command ReadReq and calls sendTimingReq() on its data port.
    L1 DCache receives request โ€” The CpuSidePort::recvTimingReq() handler in BaseCache begins processing.
    Tag lookup โ€” The cache checks its tag array. The CacheBlk is looked up via the indexing policy (e.g., set-associative).
    Miss detected โ€” No valid block found. The cache allocates an MSHR (Miss Status Holding Register) to track the outstanding request.
    Forward to L2 โ€” The L1 sends the request downstream via its MemSidePort using sendTimingReq().
    L2 tag lookup โ€” Same process repeats. If the L2 also misses, another MSHR is allocated.
    Coherent XBar routing โ€” The crossbar receives the request and routes it to the appropriate memory controller based on address ranges.
    Memory controller queuing โ€” The MemCtrl places the request in its read queue. QoS policies may reorder requests based on priority.
    DRAM access โ€” The controller issues the DRAM command sequence: activate row, read column, precharge. Timing is modeled cycle-accurately.
    Response propagation โ€” Data travels back: DRAM โ†’ MemCtrl โ†’ XBar โ†’ L2 โ†’ L1, each level filling its cache block.
    MSHR deallocation โ€” As each cache level receives the response, it deallocates its MSHR and unblocks any waiting requests for the same block.
    CPU receives data โ€” The L1 sends the ReadResp back to the CPU via sendTimingResp(), completing the memory access.

    Key Components

    Class / FileHeaderDescription
    Packetpacket.hhCore data transfer unit carrying command, address, data, and flags across the memory system.
    MemCmdpacket.hhEnumeration of all memory commands: ReadReq, WriteReq, WritebackDirty, Snoop*, etc.
    Requestrequest.hhEncapsulates the original CPU request with virtual/physical addresses and flags.
    RequestPortport.hhInitiator-side port that sends requests and receives responses.
    ResponsePortport.hhTarget-side port that receives requests and sends responses.
    BaseCachecache/base.hhAbstract base for all classic caches; manages tags, MSHRs, write buffers.
    Cachecache/cache.hhCoherent cache supporting snooping, invalidation, and flexible topologies.
    NoncoherentCachecache/noncoherent_cache.hhSimplified cache without coherence protocol โ€” used for instruction caches or accelerators.
    MSHRcache/mshr.hhMiss Status Holding Register โ€” tracks pending cache misses and coalesces duplicate requests.
    AbstractMemoryabstract_mem.hhBase class for all memory backends (SimpleMemory, DRAM, NVM).
    MemCtrlmem_ctrl.hhMain memory controller with read/write queuing, scheduling, and timing.
    DRAMInterfacedram_interface.hhCycle-accurate DRAM timing model for DDR3/DDR4/DDR5/LPDDR/HBM.
    HBMCtrlhbm_ctrl.hhDual-channel HBM controller leveraging pseudo-channel architecture.
    CommMonitorcomm_monitor.hhTransparent probe inserted between ports to gather statistics.
    PacketQueuepacket_queue.hhQueue 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.

    26 files ยท 4 sub-dirs (tags, replacement_policies, prefetch, compressors)

    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.

    5 sub-dirs (system, network, structures, slicc_interface, protocol)

    qos

    Quality of Service policies for memory controllers: fixed-priority, proportional-fair scheduling, turnaround policies, and queue management for multi-tenant workloads.

    21 files ยท FixedPrio + PF policies

    probes

    Memory probe infrastructure: MemTraceProbe for trace generation, StackDistProbe for reuse-distance analysis, MemFootprintProbe for working-set tracking.

    4 headers ยท Plugs into any Port

    protocol

    Low-level protocol definitions: atomic.hh, functional.hh, timing.hh โ€” the three transport interfaces that ports implement for different simulation modes.

    3 headers

    Controllers & Interfaces

    DRAM controllers (MemCtrl, HBMCtrl, HeteroMemCtrl), external interfaces (DRAMSim2, DRAMSim3, DRAMSys), serial links, and the XBar system bridges.

    15+ files ยท DDR3 / DDR4 / DDR5 / HBM / NVM

    Directory Map

    src/mem/ โ”œโ”€โ”€ packet.hh / packet.cc # Packet & MemCmd โ”œโ”€โ”€ port.hh / port.cc # RequestPort, ResponsePort โ”œโ”€โ”€ request.hh # Request class โ”œโ”€โ”€ abstract_mem.hh / .cc # AbstractMemory base โ”œโ”€โ”€ mem_ctrl.hh / .cc # Main memory controller โ”œโ”€โ”€ dram_interface.hh / .cc # DRAM timing model โ”œโ”€โ”€ hbm_ctrl.hh / .cc # HBM controller โ”œโ”€โ”€ hetero_mem_ctrl.hh / .cc # Heterogeneous memory โ”œโ”€โ”€ comm_monitor.hh / .cc # Communication monitor โ”œโ”€โ”€ packet_queue.hh / .cc # Packet queuing โ”œโ”€โ”€ cache/ # Classic cache hierarchy โ”‚ โ”œโ”€โ”€ base.hh, cache.hh # BaseCache, Cache โ”‚ โ”œโ”€โ”€ mshr.hh, write_queue.hh # Miss handling โ”‚ โ”œโ”€โ”€ replacement_policies/ # LRU, FIFO, BIP, BRRIP, โ€ฆ โ”‚ โ”œโ”€โ”€ prefetch/ # Stride, BOP, AMPM, SPP, โ€ฆ โ”‚ โ”œโ”€โ”€ tags/ # Set-assoc, FA-LRU, sector โ”‚ โ””โ”€โ”€ compressors/ # BDI, C-Pack, FPC, FPCD, โ€ฆ โ”œโ”€โ”€ ruby/ # Ruby coherence โ”‚ โ”œโ”€โ”€ system/ # Sequencer, RubySystem โ”‚ โ”œโ”€โ”€ network/ # Simple, Garnet networks โ”‚ โ”œโ”€โ”€ structures/ # CacheMemory, DirectoryMemory โ”‚ โ”œโ”€โ”€ slicc_interface/ # SLICC controllers โ”‚ โ””โ”€โ”€ protocol/ # Protocol definitions โ”œโ”€โ”€ qos/ # Quality of Service โ”‚ โ”œโ”€โ”€ mem_ctrl.hh # QoS MemCtrl โ”‚ โ”œโ”€โ”€ policy.hh # Policy base โ”‚ โ””โ”€โ”€ policy_fixed_prio.hh # Fixed-priority โ””โ”€โ”€ probes/ # Memory probes โ”œโ”€โ”€ mem_trace.hh # Trace probe โ””โ”€โ”€ stack_dist.hh # Stack distance

    Configuration Reference

    ParameterSystem & Standard Library ยท gem5 Wiki" class="xref">SimObjectDescriptionDefault
    sizeCacheTotal cache capacity64kB (L1), 2MB (L2)
    assocCacheSet associativity2 (L1), 8 (L2)
    tag_latencyCacheTag array access cycles2
    data_latencyCacheData array access cycles2
    response_latencyCacheExtra response delay2
    mshrsCacheNumber of MSHRs4
    write_buffersCacheWrite buffer entries8
    replacement_policyCacheReplacement policy objectLRURP
    prefetcherCachePrefetcher objectNone
    mem_rangesAbstractMemoryAddress ranges servedโ€”
    device_sizeDRAMInterfacePer-device capacityVaries by config
    tCKDRAMInterfaceClock periodTechnology-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.