Search code examples
c++gem5

Printing all cache_blocks in a file in gem5


I need to print all the cache_blocks with their associated sets in a text file that I defined, at every predetermined Tick (e.g every 10000 Tick). Does anybody know how can I do this?


Solution

  • Regarding the classic cache model, and assuming your local gem5 copy does not diverge much from gem5-v19.0.0.0, you are going to have to create an event that happens every X ticks and calls a function that prints what you need into a file.

    You are likely going to use BaseSetAssoc as your tags, but to make it generic in the following walkthrough I will assume that you have implemented ExampleTags, a special tags class that inherits from BaseTags.

    You must add the event wrapper to src/mem/cache/tags/example_tags.hh and the function that this wrapper will call:

    class ExampleTags : public BaseTags
    {
      protected:
        EventFunctionWrapper snapshotEvent;
    
        void takeSnapshot();
    
        /** Whatever else is needed for this class. */
    }
    

    Now, to src/mem/cache/tags/example_tags.cc. Your tags constructor should initialize the event with the function it has to call:

    ExampleTags::ExampleTags(const Params *p)
      : BaseTags(p),
        /** ... Initialization of other members ... */
        snapshotInterval(p->snapshot_interval),
        snapshotEvent([this]{ takeSnapshot(); }, name())
    {
        // Constructor contents
    }
    

    Due to the way gem5 initializes things, however, the first event should NOT be scheduled in the constructor, but in the startup() function, which must be overridden from the SimObject class. This is very important, since if you are checkpointing things WILL break otherwise (curTick() has an incorrect value in the constructor):

    void
    ExampleTags::startup()
    {
        BaseTags::startup();
    
        // We can only store relevant block information after the blocks have
        // been initialized
        schedule(snapshotEvent, curTick() + snapshotInterval);
    } 
    

    Finally, the snapshot function contains whatever you want to do in this interval:

    void
    ExampleTags::takeSnapshot()
    {
        // You can even use the tags' forEachBlk()
        for (const auto& blk : blks) {
            // Print what you need to the file. The blk.print() function
            // can satisfy your needs
        }
    
        // Schedule next snapshot
        schedule(snapshotEvent, curTick() + snapshotInterval);
    }
    

    Where snapshot_interval would be declared in the tags' equivalent python declaration, in src/mem/cache/tags/Tags.py:

    class ExampleTags(BaseTags):
    
        # ... Other parameters of these tags ...
    
        snapshot_interval = Param.Unsigned(10000,
            "Number of ticks between snapshots")