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?
-
Please try to make your question more clear/specific. In Ruby or Classic mode? – Daniel Carvalho Feb 24 '20 at 12:37
1 Answers
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")

- 473
- 1
- 5
- 17
-
Thank you so much. The main problem that I have is that I don't know how to call all blocks. I mean I should write a for ring that counts all blocks : for (int i=0; i
– Mahan Feb 24 '20 at 14:13 -
I gave you the complete answer regarding gem5, including the for loop on the blocks. The only thing that is missing is how to print to a file. – Daniel Carvalho Feb 24 '20 at 14:15
-
You may want to look into this answer if you don't know what a range for loop is: https://stackoverflow.com/questions/15927033/what-is-the-correct-way-of-using-c11s-range-based-for – Daniel Carvalho Feb 24 '20 at 14:18
-
yes. thank you so much. your explanations are very complete. but the name of files that I have to modify is not mentioned. – Mahan Feb 24 '20 at 14:21
-
I need more information about what you are running, but as an educated guess (i.e., assuming you are using the default configuration) you should be modifying base_set_assoc.cc, base_set_assoc.hh and Tags.py. All these files are found inside src/mem/cache/tags/. Also, as mentioned in the answer, instead of ExampleTags, you will use the existing class BaseSetAssoc. – Daniel Carvalho Feb 24 '20 at 14:25
-
Actually I've wrote a function in src/mem/cache/base.cc . this function apply some changes to a block every time a block has been written. I want to call a function into this function when curTick%10000 = 0. so, in your opinion, I should write a function in base_set_assoc.cc and call it using tags->myfunction(), am I true? – Mahan Feb 24 '20 at 14:40
-
This is another question, regarding program design, and only you (bearer of the code) will be able to decide what is the best to do. If it is possible to move that function of yours in base.cc, then it seems to be the best option. Otherwise, you may have to do some casts, but it would work too. Please do not forget to mark this question as answered. – Daniel Carvalho Feb 24 '20 at 14:47