gem5  v22.1.0.0
sector_tags.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, 2020 Inria
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
35 
36 #include <cassert>
37 #include <memory>
38 #include <string>
39 
40 #include "base/intmath.hh"
41 #include "base/logging.hh"
42 #include "base/types.hh"
43 #include "mem/cache/base.hh"
47 
48 namespace gem5
49 {
50 
51 SectorTags::SectorTags(const SectorTagsParams &p)
52  : BaseTags(p), allocAssoc(p.assoc),
53  sequentialAccess(p.sequential_access),
54  replacementPolicy(p.replacement_policy),
55  numBlocksPerSector(p.num_blocks_per_sector),
56  numSectors(numBlocks / numBlocksPerSector),
57  sectorShift(floorLog2(blkSize)), sectorMask(numBlocksPerSector - 1),
58  sectorStats(stats, *this)
59 {
60  // There must be a indexing policy
61  fatal_if(!p.indexing_policy, "An indexing policy is required");
62 
63  // Check parameters
65  "Block size must be at least 4 and a power of 2");
67  "# of blocks per sector must be non-zero and a power of 2");
68 }
69 
70 void
72 {
73  // Create blocks and sector blocks
76 
77  // Initialize all blocks
78  unsigned blk_index = 0; // index into blks array
79  for (unsigned sec_blk_index = 0; sec_blk_index < numSectors;
80  sec_blk_index++)
81  {
82  // Locate next cache sector
83  SectorBlk* sec_blk = &secBlks[sec_blk_index];
84 
85  // Associate a replacement data entry to the sector
87 
88  // Initialize all blocks in this sector
89  sec_blk->blks.resize(numBlocksPerSector);
90  for (unsigned k = 0; k < numBlocksPerSector; ++k){
91  // Select block within the set to be linked
92  SectorSubBlk*& blk = sec_blk->blks[k];
93 
94  // Locate next cache block
95  blk = &blks[blk_index];
96 
97  // Associate a data chunk to the block
98  blk->data = &dataBlks[blkSize*blk_index];
99 
100  // Associate sector block to this block
101  blk->setSectorBlock(sec_blk);
102 
103  // Associate the sector replacement data to this block
104  blk->replacementData = sec_blk->replacementData;
105 
106  // Set its index and sector offset
107  blk->setSectorOffset(k);
108 
109  // Update block index
110  ++blk_index;
111  }
112 
113  // Link block to indexing policy
114  indexingPolicy->setEntry(sec_blk, sec_blk_index);
115  }
116 }
117 
118 void
120 {
122 
123  // Get block's sector
124  SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
125  const SectorBlk* sector_blk = sub_blk->getSectorBlock();
126 
127  // When a block in a sector is invalidated, it does not make the tag
128  // invalid automatically, as there might be other blocks in the sector
129  // using it. The tag is invalidated only when there is a single block
130  // in the sector.
131  if (!sector_blk->isValid()) {
132  // Decrease the number of tags in use
133  stats.tagsInUse--;
134  assert(stats.tagsInUse.value() >= 0);
135 
136  // Invalidate replacement data, as we're invalidating the sector
138  }
139 }
140 
141 CacheBlk*
143 {
144  CacheBlk *blk = findBlock(pkt->getAddr(), pkt->isSecure());
145 
146  // Access all tags in parallel, hence one in each way. The data side
147  // either accesses all blocks in parallel, or one block sequentially on
148  // a hit. Sequential access with a miss doesn't access data.
150  if (sequentialAccess) {
151  if (blk != nullptr) {
152  stats.dataAccesses += 1;
153  }
154  } else {
156  }
157 
158  // If a cache hit
159  if (blk != nullptr) {
160  // Update number of references to accessed block
161  blk->increaseRefCount();
162 
163  // Get block's sector
164  SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
165  const SectorBlk* sector_blk = sub_blk->getSectorBlock();
166 
167  // Update replacement data of accessed block, which is shared with
168  // the whole sector it belongs to
169  replacementPolicy->touch(sector_blk->replacementData, pkt);
170  }
171 
172  // The tag lookup latency is the same for a hit or a miss
173  lat = lookupLatency;
174 
175  return blk;
176 }
177 
178 void
180 {
181  // Get block's sector
182  SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
183  const SectorBlk* sector_blk = sub_blk->getSectorBlock();
184 
185  // When a block is inserted, the tag is only a newly used tag if the
186  // sector was not previously present in the cache.
187  if (sector_blk->isValid()) {
188  // An existing entry's replacement data is just updated
189  replacementPolicy->touch(sector_blk->replacementData, pkt);
190  } else {
191  // Increment tag counter
192  stats.tagsInUse++;
193  assert(stats.tagsInUse.value() <= numSectors);
194 
195  // A new entry resets the replacement data
196  replacementPolicy->reset(sector_blk->replacementData, pkt);
197  }
198 
199  // Do common block insertion functionality
200  BaseTags::insertBlock(pkt, blk);
201 }
202 
203 void
205 {
206  const bool dest_was_valid =
207  static_cast<SectorSubBlk*>(dest_blk)->getSectorBlock()->isValid();
208 
209  BaseTags::moveBlock(src_blk, dest_blk);
210 
211  // Get blocks' sectors. The blocks have effectively been swapped by now,
212  // so src points to an invalid block, and dest to the moved valid one.
213  SectorSubBlk* src_sub_blk = static_cast<SectorSubBlk*>(src_blk);
214  const SectorBlk* src_sector_blk = src_sub_blk->getSectorBlock();
215  SectorSubBlk* dest_sub_blk = static_cast<SectorSubBlk*>(dest_blk);
216  const SectorBlk* dest_sector_blk = dest_sub_blk->getSectorBlock();
217 
218  // Since the blocks were using different replacement data pointers,
219  // we must touch the replacement data of the new entry, and invalidate
220  // the one that is being moved.
221  // When a block in a sector is invalidated, it does not make the tag
222  // invalid automatically, as there might be other blocks in the sector
223  // using it. The tag is invalidated only when there is a single block
224  // in the sector.
225  if (!src_sector_blk->isValid()) {
226  // Invalidate replacement data, as we're invalidating the sector
227  replacementPolicy->invalidate(src_sector_blk->replacementData);
228 
229  if (dest_was_valid) {
230  // If destination sector was valid, and the source sector became
231  // invalid, there is one less tag being used
232  stats.tagsInUse--;
233  assert(stats.tagsInUse.value() >= 0);
234  }
235  } else if (!dest_was_valid) {
236  // If destination sector was invalid and became valid, and the source
237  // sector is still valid, there is one extra tag being used
238  stats.tagsInUse++;
239  assert(stats.tagsInUse.value() <= numSectors);
240  }
241 
242  if (dest_was_valid) {
243  replacementPolicy->touch(dest_sector_blk->replacementData);
244  } else {
245  replacementPolicy->reset(dest_sector_blk->replacementData);
246  }
247 }
248 
249 CacheBlk*
250 SectorTags::findBlock(Addr addr, bool is_secure) const
251 {
252  // Extract sector tag
253  const Addr tag = extractTag(addr);
254 
255  // The address can only be mapped to a specific location of a sector
256  // due to sectors being composed of contiguous-address entries
258 
259  // Find all possible sector entries that may contain the given address
260  const std::vector<ReplaceableEntry*> entries =
262 
263  // Search for block
264  for (const auto& sector : entries) {
265  auto blk = static_cast<SectorBlk*>(sector)->blks[offset];
266  if (blk->matchTag(tag, is_secure)) {
267  return blk;
268  }
269  }
270 
271  // Did not find block
272  return nullptr;
273 }
274 
275 CacheBlk*
276 SectorTags::findVictim(Addr addr, const bool is_secure, const std::size_t size,
277  std::vector<CacheBlk*>& evict_blks)
278 {
279  // Get possible entries to be victimized
280  const std::vector<ReplaceableEntry*> sector_entries =
282 
283  // Check if the sector this address belongs to has been allocated
284  Addr tag = extractTag(addr);
285  SectorBlk* victim_sector = nullptr;
286  for (const auto& sector : sector_entries) {
287  SectorBlk* sector_blk = static_cast<SectorBlk*>(sector);
288  if (sector_blk->matchTag(tag, is_secure)) {
289  victim_sector = sector_blk;
290  break;
291  }
292  }
293 
294  // If the sector is not present
295  if (victim_sector == nullptr){
296  // Choose replacement victim from replacement candidates
297  victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim(
298  sector_entries));
299  }
300 
301  // Get the entry of the victim block within the sector
302  SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)];
303 
304  // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
305  // the currently existing sector is valid.
306  if (victim_sector->matchTag(tag, is_secure)) {
307  // It would be a hit if victim was valid, and upgrades do not call
308  // findVictim, so it cannot happen
309  assert(!victim->isValid());
310  } else {
311  // The whole sector must be evicted to make room for the new sector
312  for (const auto& blk : victim_sector->blks){
313  if (blk->isValid()) {
314  evict_blks.push_back(blk);
315  }
316  }
317  }
318 
319  // Update number of sub-blocks evicted due to a replacement
320  sectorStats.evictionsReplacement[evict_blks.size()]++;
321 
322  return victim;
323 }
324 
325 int
327 {
328  return (addr >> sectorShift) & sectorMask;
329 }
330 
331 Addr
333 {
334  const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk);
335  const SectorBlk* sec_blk = blk_cast->getSectorBlock();
336  const Addr sec_addr =
337  indexingPolicy->regenerateAddr(blk->getTag(), sec_blk);
338  return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift);
339 }
340 
342  SectorTags& _tags)
343  : statistics::Group(&base_group), tags(_tags),
344  ADD_STAT(evictionsReplacement, statistics::units::Count::get(),
345  "Number of blocks evicted due to a replacement")
346 {
347 }
348 
349 void
351 {
353 
354  evictionsReplacement.init(tags.numBlocksPerSector + 1);
355  for (unsigned i = 0; i <= tags.numBlocksPerSector; ++i) {
356  evictionsReplacement.subname(i, std::to_string(i));
357  evictionsReplacement.subdesc(i, "Number of replacements that caused " \
358  "the eviction of " + std::to_string(i) + " blocks");
359  }
360 }
361 
362 void
363 SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
364 {
365  for (SectorSubBlk& blk : blks) {
366  visitor(blk);
367  }
368 }
369 
370 bool
371 SectorTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
372 {
373  for (SectorSubBlk& blk : blks) {
374  if (visitor(blk)) {
375  return true;
376  }
377  }
378  return false;
379 }
380 
381 } // namespace gem5
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
virtual std::vector< ReplaceableEntry * > getPossibleEntries(const Addr addr) const =0
Find all possible entries for insertion and replacement of an address.
virtual Addr regenerateAddr(const Addr tag, const ReplaceableEntry *entry) const =0
Regenerate an entry's address from its tag and assigned indexing bits.
void setEntry(ReplaceableEntry *entry, const uint64_t index)
Associate a pointer to an entry to its physical counterpart.
Definition: base.cc:81
A common base class of Cache tagstore objects.
Definition: base.hh:74
virtual void insertBlock(const PacketPtr pkt, CacheBlk *blk)
Insert the new block into the cache and update stats.
Definition: base.cc:102
virtual Addr extractTag(const Addr addr) const
Generate the tag from the given address.
Definition: base.cc:143
const unsigned numBlocks
the number of blocks in the cache
Definition: base.hh:100
virtual void invalidate(CacheBlk *blk)
This function updates the tags when a block is invalidated.
Definition: base.hh:254
gem5::BaseTags::BaseTagStats stats
virtual void moveBlock(CacheBlk *src_blk, CacheBlk *dest_blk)
Move a block's metadata to another location decided by the replacement policy.
Definition: base.cc:130
BaseIndexingPolicy * indexingPolicy
Indexing policy.
Definition: base.hh:89
std::unique_ptr< uint8_t[]> dataBlks
The data blocks, 1 per cache block.
Definition: base.hh:103
const Cycles lookupLatency
The tag lookup latency of the cache.
Definition: base.hh:83
const unsigned blkSize
The block size of the cache.
Definition: base.hh:77
A Basic Cache block.
Definition: cache_blk.hh:71
void increaseRefCount()
Get the number of references to this block since insertion.
Definition: cache_blk.hh:294
uint8_t * data
Contains a copy of the data in this block for easy access.
Definition: cache_blk.hh:103
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:79
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:294
bool isSecure() const
Definition: packet.hh:834
Addr getAddr() const
Definition: packet.hh:805
std::shared_ptr< replacement_policy::ReplacementData > replacementData
Replacement data associated to this entry.
A Basic Sector block.
Definition: sector_blk.hh:135
std::vector< SectorSubBlk * > blks
List of blocks associated to this sector.
Definition: sector_blk.hh:147
bool isValid() const override
Checks that a sector block is valid.
Definition: sector_blk.cc:120
A sector is composed of sub-blocks, and each sub-block has information regarding its sector and a poi...
Definition: sector_blk.hh:52
int getSectorOffset() const
Get offset of this sub-block within the sector.
Definition: sector_blk.cc:64
void setSectorBlock(SectorBlk *sector_blk)
Set sector block associated to this block.
Definition: sector_blk.cc:45
void setSectorOffset(const int sector_offset)
Set offset of this sub-block within the sector.
Definition: sector_blk.cc:58
SectorBlk * getSectorBlock() const
Get sector block associated to this block.
Definition: sector_blk.cc:52
A SectorTags cache tag store.
Definition: sector_tags.hh:66
Addr regenerateBlkAddr(const CacheBlk *blk) const override
Regenerate the block address from the tag and location.
Definition: sector_tags.cc:332
const unsigned sectorMask
Mask out all bits that aren't part of the sector tag.
Definition: sector_tags.hh:95
std::vector< SectorSubBlk > blks
The cache blocks.
Definition: sector_tags.hh:69
replacement_policy::Base * replacementPolicy
Replacement policy.
Definition: sector_tags.hh:81
gem5::SectorTags::SectorTagsStats sectorStats
int extractSectorOffset(Addr addr) const
Calculate a block's offset in a sector from the address.
Definition: sector_tags.cc:326
CacheBlk * findBlock(Addr addr, bool is_secure) const override
Finds the given address in the cache, do not update replacement data.
Definition: sector_tags.cc:250
SectorTags(const Params &p)
Construct and initialize this tag store.
Definition: sector_tags.cc:51
void forEachBlk(std::function< void(CacheBlk &)> visitor) override
Visit each sub-block in the tags and apply a visitor.
Definition: sector_tags.cc:363
std::vector< SectorBlk > secBlks
The cache sector blocks.
Definition: sector_tags.hh:71
CacheBlk * findVictim(Addr addr, const bool is_secure, const std::size_t size, std::vector< CacheBlk * > &evict_blks) override
Find replacement victim based on address.
Definition: sector_tags.cc:276
const bool sequentialAccess
Whether tags and data are accessed sequentially.
Definition: sector_tags.hh:78
bool anyBlk(std::function< bool(CacheBlk &)> visitor) override
Find if any of the sub-blocks satisfies a condition.
Definition: sector_tags.cc:371
void tagsInit() override
Initialize blocks as SectorBlk and SectorSubBlk instances.
Definition: sector_tags.cc:71
void insertBlock(const PacketPtr pkt, CacheBlk *blk) override
Insert the new block into the cache and update replacement data.
Definition: sector_tags.cc:179
const unsigned numSectors
The number of sectors in the cache.
Definition: sector_tags.hh:87
const unsigned numBlocksPerSector
Number of data blocks per sector.
Definition: sector_tags.hh:84
const int sectorShift
The amount to shift the address to get the sector tag.
Definition: sector_tags.hh:92
unsigned allocAssoc
The allocatable associativity of the cache (alloc mask).
Definition: sector_tags.hh:75
void invalidate(CacheBlk *blk) override
This function updates the tags when a block is invalidated but does not invalidate the block itself.
Definition: sector_tags.cc:119
CacheBlk * accessBlock(const PacketPtr pkt, Cycles &lat) override
Access block and update replacement data.
Definition: sector_tags.cc:142
void moveBlock(CacheBlk *src_blk, CacheBlk *dest_blk) override
Move a block's metadata to another location decided by the replacement policy.
Definition: sector_tags.cc:204
virtual bool isValid() const
Checks if the entry is valid.
Definition: tagged_entry.hh:57
virtual bool matchTag(Addr tag, bool is_secure) const
Checks if the given tag information corresponds to this entry's.
Definition: tagged_entry.hh:81
virtual Addr getTag() const
Get tag associated to this block.
Definition: tagged_entry.hh:71
virtual void invalidate(const std::shared_ptr< ReplacementData > &replacement_data)=0
Invalidate replacement data to set it as the next probable victim.
virtual ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const =0
Find replacement victim among candidates.
virtual void reset(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Reset replacement data.
Definition: base.hh:90
virtual void touch(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Update replacement data.
Definition: base.hh:76
virtual std::shared_ptr< ReplacementData > instantiateEntry()=0
Instantiate a replacement data entry.
Statistics container.
Definition: group.hh:94
Counter value() const
Return the current value of this stat as its base type.
Definition: statistics.hh:622
STL vector class.
Definition: stl.hh:37
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
static constexpr std::enable_if_t< std::is_integral_v< T >, int > floorLog2(T x)
Definition: intmath.hh:59
static constexpr bool isPowerOf2(const T &n)
Definition: intmath.hh:98
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
Declares a basic cache interface BaseCache.
Declaration of a common framework for indexing policies.
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 23, 0 > offset
Definition: types.hh:144
Bitfield< 23 > k
Definition: dt_constants.hh:81
Bitfield< 54 > p
Definition: pagetable.hh:70
Bitfield< 3 > addr
Definition: types.hh:84
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60
Declaration of a sector tag store.
TODO: It would be good if these stats were acquired after warmup.
Definition: base.hh:109
statistics::Scalar tagAccesses
Number of tags consulted over all accesses.
Definition: base.hh:155
statistics::Scalar dataAccesses
Number of data blocks consulted over all accesses.
Definition: base.hh:157
statistics::Average tagsInUse
Per tick average of the number of tags that hold valid data.
Definition: base.hh:118
void regStats() override
Callback to set stat parameters.
Definition: sector_tags.cc:350
statistics::Vector evictionsReplacement
Number of sub-blocks evicted due to a replacement.
Definition: sector_tags.hh:106
SectorTagsStats(BaseTagStats &base_group, SectorTags &_tags)
Definition: sector_tags.cc:341

Generated on Wed Dec 21 2022 10:22:37 for gem5 by doxygen 1.9.1