gem5  v20.1.0.0
base.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013,2016,2018-2019 ARM Limited
3  * All rights reserved.
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Copyright (c) 2003-2005 The Regents of The University of Michigan
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions are
19  * met: redistributions of source code must retain the above copyright
20  * notice, this list of conditions and the following disclaimer;
21  * redistributions in binary form must reproduce the above copyright
22  * notice, this list of conditions and the following disclaimer in the
23  * documentation and/or other materials provided with the distribution;
24  * neither the name of the copyright holders nor the names of its
25  * contributors may be used to endorse or promote products derived from
26  * this software without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39  */
40 
46 #include "mem/cache/tags/base.hh"
47 
48 #include <cassert>
49 
50 #include "base/types.hh"
53 #include "mem/request.hh"
54 #include "sim/core.hh"
55 #include "sim/sim_exit.hh"
56 #include "sim/system.hh"
57 
59  : ClockedObject(p), blkSize(p->block_size), blkMask(blkSize - 1),
60  size(p->size), lookupLatency(p->tag_latency),
61  system(p->system), indexingPolicy(p->indexing_policy),
62  warmupBound((p->warmup_percentage/100.0) * (p->size / p->block_size)),
63  warmedUp(false), numBlocks(p->size / p->block_size),
64  dataBlks(new uint8_t[p->size]), // Allocate data storage in one big chunk
65  stats(*this)
66 {
67  registerExitCallback([this]() { cleanupRefs(); });
68 }
69 
71 BaseTags::findBlockBySetAndWay(int set, int way) const
72 {
73  return indexingPolicy->getEntry(set, way);
74 }
75 
76 CacheBlk*
77 BaseTags::findBlock(Addr addr, bool is_secure) const
78 {
79  // Extract block tag
80  Addr tag = extractTag(addr);
81 
82  // Find possible entries that may contain the given address
83  const std::vector<ReplaceableEntry*> entries =
85 
86  // Search for block
87  for (const auto& location : entries) {
88  CacheBlk* blk = static_cast<CacheBlk*>(location);
89  if ((blk->tag == tag) && blk->isValid() &&
90  (blk->isSecure() == is_secure)) {
91  return blk;
92  }
93  }
94 
95  // Did not find block
96  return nullptr;
97 }
98 
99 void
101 {
102  assert(!blk->isValid());
103 
104  // Previous block, if existed, has been removed, and now we have
105  // to insert the new one
106 
107  // Deal with what we are bringing in
108  RequestorID requestor_id = pkt->req->requestorId();
109  assert(requestor_id < system->maxRequestors());
110  stats.occupancies[requestor_id]++;
111 
112  // Insert block with tag, src requestor id and task id
113  blk->insert(extractTag(pkt->getAddr()), pkt->isSecure(), requestor_id,
114  pkt->req->taskId());
115 
116  // Check if cache warm up is done
117  if (!warmedUp && stats.tagsInUse.value() >= warmupBound) {
118  warmedUp = true;
120  }
121 
122  // We only need to write into one tag and one data block.
123  stats.tagAccesses += 1;
124  stats.dataAccesses += 1;
125 }
126 
127 Addr
129 {
130  return indexingPolicy->extractTag(addr);
131 }
132 
133 void
135 {
136  if (blk.isValid()) {
137  stats.totalRefs += blk.refCount;
138  ++stats.sampledRefs;
139  }
140 }
141 
142 void
144 {
145  forEachBlk([this](CacheBlk &blk) { cleanupRefsVisitor(blk); });
146 }
147 
148 void
150 {
151  if (blk.isValid()) {
154  assert(blk.tickInserted <= curTick());
155  Tick age = curTick() - blk.tickInserted;
156 
157  int age_index;
158  if (age / SimClock::Int::us < 10) { // <10us
159  age_index = 0;
160  } else if (age / SimClock::Int::us < 100) { // <100us
161  age_index = 1;
162  } else if (age / SimClock::Int::ms < 1) { // <1ms
163  age_index = 2;
164  } else if (age / SimClock::Int::ms < 10) { // <10ms
165  age_index = 3;
166  } else
167  age_index = 4; // >10ms
168 
169  stats.ageTaskId[blk.task_id][age_index]++;
170  }
171 }
172 
173 void
175 {
176  for (unsigned i = 0; i < ContextSwitchTaskId::NumTaskId; ++i) {
178  for (unsigned j = 0; j < 5; ++j) {
179  stats.ageTaskId[i][j] = 0;
180  }
181  }
182 
183  forEachBlk([this](CacheBlk &blk) { computeStatsVisitor(blk); });
184 }
185 
186 std::string
188 {
189  std::string str;
190 
191  auto print_blk = [&str](CacheBlk &blk) {
192  if (blk.isValid())
193  str += csprintf("\tBlock: %s\n", blk.print());
194  };
195  forEachBlk(print_blk);
196 
197  if (str.empty())
198  str = "no valid tags\n";
199 
200  return str;
201 }
202 
204  : Stats::Group(&_tags),
205  tags(_tags),
206 
207  tagsInUse(this, "tagsinuse",
208  "Cycle average of tags in use"),
209  totalRefs(this, "total_refs",
210  "Total number of references to valid blocks."),
211  sampledRefs(this, "sampled_refs",
212  "Sample count of references to valid blocks."),
213  avgRefs(this, "avg_refs",
214  "Average number of references to valid blocks."),
215  warmupCycle(this, "warmup_cycle",
216  "Cycle when the warmup percentage was hit."),
217  occupancies(this, "occ_blocks",
218  "Average occupied blocks per requestor"),
219  avgOccs(this, "occ_percent",
220  "Average percentage of cache occupancy"),
221  occupanciesTaskId(this, "occ_task_id_blocks",
222  "Occupied blocks per task id"),
223  ageTaskId(this, "age_task_id_blocks", "Occupied blocks per task id"),
224  percentOccsTaskId(this, "occ_task_id_percent",
225  "Percentage of cache occupancy per task id"),
226  tagAccesses(this, "tag_accesses", "Number of tag accesses"),
227  dataAccesses(this, "data_accesses", "Number of data accesses")
228 {
229 }
230 
231 void
233 {
234  using namespace Stats;
235 
237 
238  System *system = tags.system;
239 
240  avgRefs = totalRefs / sampledRefs;
241 
242  occupancies
244  .flags(nozero | nonan)
245  ;
246  for (int i = 0; i < system->maxRequestors(); i++) {
247  occupancies.subname(i, system->getRequestorName(i));
248  }
249 
250  avgOccs.flags(nozero | total);
251  for (int i = 0; i < system->maxRequestors(); i++) {
252  avgOccs.subname(i, system->getRequestorName(i));
253  }
254 
255  avgOccs = occupancies / Stats::constant(tags.numBlocks);
256 
257  occupanciesTaskId
259  .flags(nozero | nonan)
260  ;
261 
262  ageTaskId
264  .flags(nozero | nonan)
265  ;
266 
267  percentOccsTaskId.flags(nozero);
268 
269  percentOccsTaskId = occupanciesTaskId / Stats::constant(tags.numBlocks);
270 }
271 
272 void
274 {
276 
277  tags.computeStats();
278 }
ReplaceableEntry
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
Definition: replaceable_entry.hh:53
Stats::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
BaseTags::BaseTagStats::occupancies
Stats::AverageVector occupancies
Average occupancy of each requestor using the cache.
Definition: base.hh:137
BaseTags::cleanupRefs
void cleanupRefs()
Average in the reference count for valid blocks when the simulation exits.
Definition: base.cc:143
BaseTags::computeStatsVisitor
void computeStatsVisitor(CacheBlk &blk)
Update the occupancy and age stats using data from the input block.
Definition: base.cc:149
system.hh
Packet::getAddr
Addr getAddr() const
Definition: packet.hh:754
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
BaseIndexingPolicy::getEntry
ReplaceableEntry * getEntry(const uint32_t set, const uint32_t way) const
Get an entry based on its set and way.
Definition: base.cc:72
System::init
void init() override
After all objects have been created and all ports are connected, check that the system port is connec...
Definition: system.cc:273
BaseTags::warmupBound
const unsigned warmupBound
The number of tags that need to be touched to meet the warmup percentage.
Definition: base.hh:92
BaseTags::print
std::string print()
Print all tags used.
Definition: base.cc:187
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
SimClock::Int::us
Tick us
microsecond
Definition: core.cc:64
Packet::req
RequestPtr req
A pointer to the original request.
Definition: packet.hh:340
Stats::Group::preDumpStats
virtual void preDumpStats()
Callback before stats are dumped.
Definition: group.cc:95
std::vector
STL vector class.
Definition: stl.hh:37
BaseTags::BaseTagStats::preDumpStats
void preDumpStats() override
Callback before stats are dumped.
Definition: base.cc:273
CacheBlk::task_id
uint32_t task_id
Task Id associated with this block.
Definition: cache_blk.hh:88
BaseTags::extractTag
virtual Addr extractTag(const Addr addr) const
Generate the tag from the given address.
Definition: base.cc:128
BaseTags::BaseTagStats::BaseTagStats
BaseTagStats(BaseTags &tags)
Definition: base.cc:203
sim_exit.hh
request.hh
ContextSwitchTaskId::NumTaskId
@ NumTaskId
Definition: request.hh:76
Packet::isSecure
bool isSecure() const
Definition: packet.hh:783
ClockedObject
The ClockedObject class extends the SimObject with a clock and accessor functions to relate ticks to ...
Definition: clocked_object.hh:231
BaseTags::BaseTagStats::occupanciesTaskId
Stats::Vector occupanciesTaskId
Occupancy of each context/cpu using the cache.
Definition: base.hh:143
base.hh
replaceable_entry.hh
X86ISA::system
Bitfield< 15 > system
Definition: misc.hh:997
RequestorID
uint16_t RequestorID
Definition: request.hh:85
ArmISA::j
Bitfield< 24 > j
Definition: miscregs_types.hh:54
BaseTags::BaseTags
BaseTags(const Params *p)
Definition: base.cc:58
registerExitCallback
void registerExitCallback(const std::function< void()> &callback)
Register an exit callback.
Definition: core.cc:140
BaseTags::BaseTagStats::tagsInUse
Stats::Average tagsInUse
Per cycle average of the number of tags that hold valid data.
Definition: base.hh:115
BaseTags::BaseTagStats::totalRefs
Stats::Scalar totalRefs
The total number of references to a block before it is replaced.
Definition: base.hh:118
Stats::ScalarBase::value
Counter value() const
Return the current value of this stat as its base type.
Definition: statistics.hh:698
System
Definition: system.hh:73
BaseTags
A common base class of Cache tagstore objects.
Definition: base.hh:70
BaseTags::insertBlock
virtual void insertBlock(const PacketPtr pkt, CacheBlk *blk)
Insert the new block into the cache and update stats.
Definition: base.cc:100
BaseTags::BaseTagStats::warmupCycle
Stats::Scalar warmupCycle
The cycle that the warmup percentage was hit.
Definition: base.hh:134
SimClock::Int::ms
Tick ms
millisecond
Definition: core.cc:63
BaseTags::BaseTagStats::tagAccesses
Stats::Scalar tagAccesses
Number of tags consulted over all accesses.
Definition: base.hh:152
BaseTags::Params
BaseTagsParams Params
Definition: base.hh:158
BaseTags::BaseTagStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: base.cc:232
base.hh
BaseIndexingPolicy::extractTag
virtual Addr extractTag(const Addr addr) const
Generate the tag from the given address.
Definition: base.cc:96
BaseTags::BaseTagStats::ageTaskId
Stats::Vector2d ageTaskId
Occupancy of each context/cpu using the cache.
Definition: base.hh:146
CacheBlk::refCount
unsigned refCount
Number of references to this block since it was brought in.
Definition: cache_blk.hh:114
BaseTags::indexingPolicy
BaseIndexingPolicy * indexingPolicy
Indexing policy.
Definition: base.hh:86
core.hh
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
BaseIndexingPolicy::getPossibleEntries
virtual std::vector< ReplaceableEntry * > getPossibleEntries(const Addr addr) const =0
Find all possible entries for insertion and replacement of an address.
BaseTags::BaseTagStats::dataAccesses
Stats::Scalar dataAccesses
Number of data blocks consulted over all accesses.
Definition: base.hh:154
Stats::nozero
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:57
CacheBlk::tag
Addr tag
Data block tag value.
Definition: cache_blk.hh:91
BaseTags::stats
BaseTags::BaseTagStats stats
CacheBlk::tickInserted
Tick tickInserted
Tick on which the block was inserted in the cache.
Definition: cache_blk.hh:123
CacheBlk::isValid
bool isValid() const
Checks that a block is valid.
Definition: cache_blk.hh:203
CacheBlk::insert
virtual void insert(const Addr tag, const bool is_secure, const int src_requestor_ID, const uint32_t task_ID)
Set member variables when a block insertion occurs.
Definition: cache_blk.cc:46
CacheBlk
A Basic Cache block.
Definition: cache_blk.hh:84
System::maxRequestors
RequestorID maxRequestors()
Get the number of requestors registered in the system.
Definition: system.hh:503
types.hh
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:257
addr
ip6_addr_t addr
Definition: inet.hh:423
BaseTags::findBlock
virtual CacheBlk * findBlock(Addr addr, bool is_secure) const
Finds the block in the cache without touching it.
Definition: base.cc:77
BaseTags::computeStats
void computeStats()
Computes stats just prior to dump event.
Definition: base.cc:174
System::getRequestorName
std::string getRequestorName(RequestorID requestor_id)
Get the name of an object for a given request id.
Definition: system.cc:651
Stats
Definition: statistics.cc:61
BaseTags::forEachBlk
virtual void forEachBlk(std::function< void(CacheBlk &)> visitor)=0
Visit each block in the tags and apply a visitor.
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
Stats::total
const FlagsType total
Print the total.
Definition: info.hh:49
BaseTags::system
System * system
System we are currently operating in.
Definition: base.hh:83
csprintf
std::string csprintf(const char *format, const Args &...args)
Definition: cprintf.hh:158
BaseTags::BaseTagStats::sampledRefs
Stats::Scalar sampledRefs
The number of reference counts sampled.
Definition: base.hh:125
CacheBlk::isSecure
bool isSecure() const
Check if this block holds data from the secure memory space.
Definition: cache_blk.hh:245
BaseTags::warmedUp
bool warmedUp
Marked true when the cache is warmed up.
Definition: base.hh:94
Stats::nonan
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:59
Stats::constant
Temp constant(T val)
Definition: statistics.hh:3357
curTick
Tick curTick()
The current simulated tick.
Definition: core.hh:45
BaseTags::findBlockBySetAndWay
virtual ReplaceableEntry * findBlockBySetAndWay(int set, int way) const
Find a block given set and way.
Definition: base.cc:71
BaseTags::cleanupRefsVisitor
void cleanupRefsVisitor(CacheBlk &blk)
Update the reference stats using data from the input block.
Definition: base.cc:134

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17