Working Through the gem5 Tutorial "Learning Gem5" (13. Creating a SimpleMemobj for the Memory System)

Let's try the tutorial on adding the SimObjects that make up gem5.

www.gem5.org

  • gem5 Article Index

https://msyksphinz-en.hatenablog.com/entry/2023/06/24/040000msyksphinz-en.hatenablog.com


A Simple Memory Object Example

In this section, we build a simple memory object. At first, it simply passes requests from the CPU side (a simple CPU) to the memory side (a simple memory bus). Take a look at the figure below. There is one master port for sending requests to the memory bus, and two CPU-side ports for the CPU's instruction cache port and data cache port. In the next chapter, simplecache-chapter, we will add the logic that turns this object into a cache.

Declaring the SimObject

To declare the SimObject, just as when we created the simple SimObject in hello-simobject-chapter, the first step is to create a SimObject Python file. We call this simple memory object SimpleMemobj, and we create the SimObject Python file in src/learning_gem5/simple_memobj.

from m5.params import *
from m5.proxy import *
from m5.SimObject import SimObject

class SimpleMemobj(SimObject):
    type = 'SimpleMemobj'
    cxx_header = "learning_gem5/part2/simple_memobj.hh"

    inst_port = SlavePort("CPU side port, receives requests")
    data_port = SlavePort("CPU side port, receives requests")
    mem_side = MasterPort("Memory side port, sends requests")

This object inherits from SimObject. The SimObject class has a pure virtual function, getPort, that must be defined in the C++ implementation.

The parameters of this object are three ports: two ports for the CPU to connect to as the instruction port and the data port, and one port for connecting to the memory bus. These ports have no default values, only a brief description.

It is important to remember the names of these ports. We will use these names explicitly when implementing SimpleMemobj and defining the getPort function.

Of course, we also need to create, in the new directory, an SConscript file that declares the SimObject Python file.

Defining the SimpleMemobj Class

Create the header file for SimpleMemobj.

#include "mem/port.hh"
#include "params/SimpleMemobj.hh"
#include "sim/sim_object.hh"

class SimpleMemobj : public SimObject
{
  private:

  public:

    /** constructor
     */
    SimpleMemobj(SimpleMemobjParams *params);
};

Defining the Slave Port Type

Next, we need to define the classes for the two kinds of ports (the CPU-side port and the memory-side port). Since no other object will use these classes, we declare them inside the SimpleMemobj class.

Let's start with the slave port (the CPU-side port). It inherits from the SlavePort class. The following is the code needed to override all of the pure virtual functions of the SlavePort class.

class CPUSidePort : public SlavePort
{
  private:
    SimpleMemobj *owner;

  public:
    CPUSidePort(const std::string& name, SimpleMemobj *owner) :
        SlavePort(name, owner), owner(owner)
    { }

    AddrRangeList getAddrRanges() const override;

  protected:
    Tick recvAtomic(PacketPtr pkt) override { panic("recvAtomic unimpl."); }
    void recvFunctional(PacketPtr pkt) override;
    bool recvTimingReq(PacketPtr pkt) override;
    void recvRespRetry() override;
};

We need to define five functions for this object.

This object also has one member variable, which lets it call the functions of the owning object.

Defining the Master Port Type

Next, we need to define the master port type. This will be the memory-side port that forwards requests from the CPU side to the rest of the memory system.

class MemSidePort : public MasterPort
{
  private:
    SimpleMemobj *owner;

  public:
    MemSidePort(const std::string& name, SimpleMemobj *owner) :
        MasterPort(name, owner), owner(owner)
    { }

  protected:
    bool recvTimingResp(PacketPtr pkt) override;
    void recvReqRetry() override;
    void recvRangeChange() override;
};

This class has three pure virtual functions that must be overridden.

Defining the SimObject Interface

So far, we have defined two new types, CPUSidePort and MemSidePort, and are now able to declare the three ports used by SimpleMemobj. We also need to define the pure virtual function of the SimObject class, which is getPort. This function is executed during the initialization phase, when memory objects are connected through their ports.

class SimpleMemobj : public SimObject
{
  private:

    <CPUSidePort declaration>
    <MemSidePort declaration>

    CPUSidePort instPort;
    CPUSidePort dataPort;

    MemSidePort memPort;

  public:
    SimpleMemobj(SimpleMemobjParams *params);

    Port &getPort(const std::string &if_name,
                  PortID idx=InvalidPortID) override;
};

Implementing the Basic SimObject Functions

In the SimpleMemobj constructor, the SimObject constructor is simply called. We also need to initialize all of the ports. The constructor of each port takes two arguments: the name of the port and a pointer to its owner, and we use the ones defined in the header file. The name can be any string, but for convenience we make it the same as the name in the Python SimObject file. We also set the blocked variable to false.

#include "learning_gem5/part2/simple_memobj.hh"
#include "debug/SimpleMemobj.hh"

SimpleMemobj::SimpleMemobj(SimpleMemobjParams *params) :
    SimObject(params),
    instPort(params->name + ".inst_port", this),
    dataPort(params->name + ".data_port", this),
    memPort(params->name + ".mem_side", this), blocked(false)
{
}

To implement getPort, we compare if_name and check whether the name is mem_side. If so, we return the memPort object. If the name is inst_port, we return instPort, and if it is data_port, we return dataPort. Otherwise, we end up returning the parent class's getPort().

Port &
SimpleMemobj::getPort(const std::string &if_name, PortID idx)
{
    panic_if(idx != InvalidPortID, "This object doesn't support vector ports");

    // This is the name from the Python SimObject declaration (SimpleMemobj.py)
    if (if_name == "mem_side") {
        return memPort;
    } else if (if_name == "inst_port") {
        return instPort;
    } else if (if_name == "data_port") {
        return dataPort;
    } else {
        // pass it along to our super class
        return SimObject::getPort(if_name, idx);
    }
}

Implementing the Slave Port and Master Port Functions

The slave and master port implementations are simple. In most cases, the port functions just forward the information to the main memory object (SimpleMemobj).

We start with two simple functions: getAddrRanges() and recvFunctional() simply call the SimpleMemobj versions.

AddrRangeList
SimpleMemobj::CPUSidePort::getAddrRanges() const
{
    return owner->getAddrRanges();
}

void
SimpleMemobj::CPUSidePort::recvFunctional(PacketPtr pkt)
{
    return owner->handleFunctional(pkt);
}

The implementations of these functions are identical to those in SimpleMemobj. These implementations simply forward the request to the memory side. It is also possible to use the DPRINTF function to track how the functions are being called.

void
SimpleMemobj::handleFunctional(PacketPtr pkt)
{
    memPort.sendFunctional(pkt);
}

AddrRangeList
SimpleMemobj::getAddrRanges() const
{
    DPRINTF(SimpleMemobj, "Sending new ranges\n");
    return memPort.getAddrRanges();
}

Likewise, MemSidePort needs to implement recvRangeChange, which forwards the change through SimpleMemobj to the slave ports.

void
SimpleMemobj::MemSidePort::recvRangeChange()
{
    owner->sendRangeChange();
}
void
SimpleMemobj::sendRangeChange()
{
    instPort.sendRangeChange();
    dataPort.sendRangeChange();
}

This is an English translation of the original Japanese post: Gem5のチュートリアル "Learning Gem5"をやってみる(13. メモリシステムへのSimpleMemobjの作成)