gem5  v20.1.0.0
compressed_tags.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 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 "base/trace.hh"
37 #include "debug/CacheComp.hh"
41 #include "mem/packet.hh"
42 #include "params/CompressedTags.hh"
43 
45  : SectorTags(p)
46 {
47 }
48 
49 void
51 {
52  // Create blocks and superblocks
55 
56  // Initialize all blocks
57  unsigned blk_index = 0; // index into blks array
58  for (unsigned superblock_index = 0; superblock_index < numSectors;
59  superblock_index++)
60  {
61  // Locate next cache superblock
62  SuperBlk* superblock = &superBlks[superblock_index];
63 
64  // Superblocks must be aware of the block size due to their co-
65  // allocation conditions
66  superblock->setBlkSize(blkSize);
67 
68  // Associate a replacement data entry to the block
70 
71  // Initialize all blocks in this superblock
72  superblock->blks.resize(numBlocksPerSector, nullptr);
73  for (unsigned k = 0; k < numBlocksPerSector; ++k){
74  // Select block within the set to be linked
75  SectorSubBlk*& blk = superblock->blks[k];
76 
77  // Locate next cache block
78  blk = &blks[blk_index];
79 
80  // Associate a data chunk to the block
81  blk->data = &dataBlks[blkSize*blk_index];
82 
83  // Associate superblock to this block
84  blk->setSectorBlock(superblock);
85 
86  // Associate the superblock replacement data to this block
87  blk->replacementData = superblock->replacementData;
88 
89  // Set its index and sector offset
90  blk->setSectorOffset(k);
91 
92  // Update block index
93  ++blk_index;
94  }
95 
96  // Link block to indexing policy
97  indexingPolicy->setEntry(superblock, superblock_index);
98  }
99 }
100 
101 CacheBlk*
102 CompressedTags::findVictim(Addr addr, const bool is_secure,
103  const std::size_t compressed_size,
104  std::vector<CacheBlk*>& evict_blks)
105 {
106  // Get all possible locations of this superblock
107  const std::vector<ReplaceableEntry*> superblock_entries =
109 
110  // Check if the superblock this address belongs to has been allocated. If
111  // so, try co-allocating
112  Addr tag = extractTag(addr);
113  SuperBlk* victim_superblock = nullptr;
114  bool is_co_allocation = false;
115  const uint64_t offset = extractSectorOffset(addr);
116  for (const auto& entry : superblock_entries){
117  SuperBlk* superblock = static_cast<SuperBlk*>(entry);
118  if ((tag == superblock->getTag()) && superblock->isValid() &&
119  (is_secure == superblock->isSecure()) &&
120  !superblock->blks[offset]->isValid() &&
121  superblock->isCompressed() &&
122  superblock->canCoAllocate(compressed_size))
123  {
124  victim_superblock = superblock;
125  is_co_allocation = true;
126  break;
127  }
128  }
129 
130  // If the superblock is not present or cannot be co-allocated a
131  // superblock must be replaced
132  if (victim_superblock == nullptr){
133  // Choose replacement victim from replacement candidates
134  victim_superblock = static_cast<SuperBlk*>(
135  replacementPolicy->getVictim(superblock_entries));
136 
137  // The whole superblock must be evicted to make room for the new one
138  for (const auto& blk : victim_superblock->blks){
139  if (blk->isValid()) {
140  evict_blks.push_back(blk);
141  }
142  }
143  }
144 
145  // Get the location of the victim block within the superblock
146  SectorSubBlk* victim = victim_superblock->blks[offset];
147 
148  // It would be a hit if victim was valid in a co-allocation, and upgrades
149  // do not call findVictim, so it cannot happen
150  if (is_co_allocation){
151  assert(!victim->isValid());
152 
153  // Print all co-allocated blocks
154  DPRINTF(CacheComp, "Co-Allocation: offset %d with blocks\n", offset);
155  for (const auto& blk : victim_superblock->blks){
156  if (blk->isValid()) {
157  DPRINTFR(CacheComp, "\t[%s]\n", blk->print());
158  }
159  }
160  }
161 
162  // Update number of sub-blocks evicted due to a replacement
163  sectorStats.evictionsReplacement[evict_blks.size()]++;
164 
165  return victim;
166 }
167 
168 void
170 {
171  // We check if block can co-allocate before inserting, because this check
172  // assumes the block is still invalid
173  CompressionBlk* compression_blk = static_cast<CompressionBlk*>(blk);
174  const SuperBlk* superblock = static_cast<const SuperBlk*>(
175  compression_blk->getSectorBlock());
176  const bool is_co_allocatable = superblock->isCompressed() &&
177  superblock->canCoAllocate(compression_blk->getSizeBits());
178 
179  // Insert block
180  SectorTags::insertBlock(pkt, blk);
181 
182  // We always store compressed blocks when possible
183  if (is_co_allocatable) {
184  compression_blk->setCompressed();
185  } else {
186  compression_blk->setUncompressed();
187  }
188 }
189 
190 void
191 CompressedTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
192 {
193  for (CompressionBlk& blk : blks) {
194  visitor(blk);
195  }
196 }
197 
198 bool
199 CompressedTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
200 {
201  for (CompressionBlk& blk : blks) {
202  if (visitor(blk)) {
203  return true;
204  }
205  }
206  return false;
207 }
208 
210 CompressedTagsParams::create()
211 {
212  return new CompressedTags(this);
213 }
SectorTags::extractSectorOffset
int extractSectorOffset(Addr addr) const
Calculate a block's offset in a sector from the address.
Definition: sector_tags.cc:275
SectorTags::replacementPolicy
BaseReplacementPolicy * replacementPolicy
Replacement policy.
Definition: sector_tags.hh:74
BaseTags::dataBlks
std::unique_ptr< uint8_t[]> dataBlks
The data blocks, 1 per cache block.
Definition: base.hh:100
SuperBlk::canCoAllocate
bool canCoAllocate(const std::size_t compressed_size) const
Checks whether a superblock can co-allocate given compressed data block.
Definition: super_blk.cc:108
BaseTags::numBlocks
const unsigned numBlocks
the number of blocks in the cache
Definition: base.hh:97
compressed_tags.hh
CompressedTags::findVictim
CacheBlk * findVictim(Addr addr, const bool is_secure, const std::size_t compressed_size, std::vector< CacheBlk * > &evict_blks) override
Find replacement victim based on address.
Definition: compressed_tags.cc:102
SectorTags::numBlocksPerSector
const unsigned numBlocksPerSector
Number of data blocks per sector.
Definition: sector_tags.hh:77
CompressedTags::blks
std::vector< CompressionBlk > blks
The cache blocks.
Definition: compressed_tags.hh:75
SectorTags::SectorTagsStats::evictionsReplacement
Stats::Vector evictionsReplacement
Number of sub-blocks evicted due to a replacement.
Definition: sector_tags.hh:99
std::vector< CompressionBlk >
BaseTags::extractTag
virtual Addr extractTag(const Addr addr) const
Generate the tag from the given address.
Definition: base.cc:128
CompressedTags::forEachBlk
void forEachBlk(std::function< void(CacheBlk &)> visitor) override
Visit each sub-block in the tags and apply a visitor.
Definition: compressed_tags.cc:191
BaseReplacementPolicy::instantiateEntry
virtual std::shared_ptr< ReplacementData > instantiateEntry()=0
Instantiate a replacement data entry.
packet.hh
base.hh
replaceable_entry.hh
CompressionBlk::getSizeBits
std::size_t getSizeBits() const
Definition: super_blk.cc:63
SectorBlk::getTag
Addr getTag() const
Get tag associated to this block.
Definition: sector_blk.cc:148
SectorBlk::blks
std::vector< SectorSubBlk * > blks
List of blocks associated to this sector.
Definition: sector_blk.hh:167
MipsISA::k
Bitfield< 23 > k
Definition: dt_constants.hh:78
SectorTags::insertBlock
void insertBlock(const PacketPtr pkt, CacheBlk *blk) override
Insert the new block into the cache and update replacement data.
Definition: sector_tags.cc:172
CompressedTags::insertBlock
void insertBlock(const PacketPtr pkt, CacheBlk *blk) override
Insert the new block into the cache and update replacement data.
Definition: compressed_tags.cc:169
SectorSubBlk::setSectorBlock
void setSectorBlock(SectorBlk *sector_blk)
Set sector block associated to this block.
Definition: sector_blk.cc:42
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
CompressedTags::anyBlk
bool anyBlk(std::function< bool(CacheBlk &)> visitor) override
Find if any of the sub-blocks satisfies a condition.
Definition: compressed_tags.cc:199
SectorTags
A SectorTags cache tag store.
Definition: sector_tags.hh:58
BaseTags::Params
BaseTagsParams Params
Definition: base.hh:158
BaseIndexingPolicy::setEntry
void setEntry(ReplaceableEntry *entry, const uint64_t index)
Associate a pointer to an entry to its physical counterpart.
Definition: base.cc:78
DPRINTFR
#define DPRINTFR(...)
Definition: trace.hh:236
BaseTags::blkSize
const unsigned blkSize
The block size of the cache.
Definition: base.hh:74
BaseReplacementPolicy::getVictim
virtual ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const =0
Find replacement victim among candidates.
SectorTags::sectorStats
SectorTags::SectorTagsStats sectorStats
SuperBlk::setBlkSize
void setBlkSize(const std::size_t blk_size)
Set block size.
Definition: super_blk.cc:117
base.hh
BaseTags::indexingPolicy
BaseIndexingPolicy * indexingPolicy
Indexing policy.
Definition: base.hh:86
Addr
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
CompressedTags::CompressedTags
CompressedTags(const Params *p)
Construct and initialize this tag store.
Definition: compressed_tags.cc:44
BaseIndexingPolicy::getPossibleEntries
virtual std::vector< ReplaceableEntry * > getPossibleEntries(const Addr addr) const =0
Find all possible entries for insertion and replacement of an address.
SectorBlk::isValid
bool isValid() const
Checks that a sector block is valid.
Definition: sector_blk.cc:122
CompressedTags::superBlks
std::vector< SuperBlk > superBlks
The cache superblocks.
Definition: compressed_tags.hh:77
CompressionBlk::setCompressed
void setCompressed()
Set compression bit.
Definition: super_blk.cc:51
CompressionBlk::setUncompressed
void setUncompressed()
Clear compression bit.
Definition: super_blk.cc:57
CacheBlk::isValid
bool isValid() const
Checks that a block is valid.
Definition: cache_blk.hh:203
SectorBlk::isSecure
bool isSecure() const
Checks that a sector block is secure.
Definition: sector_blk.cc:135
CacheBlk::data
uint8_t * data
Contains a copy of the data in this block for easy access.
Definition: cache_blk.hh:99
SuperBlk
A basic compression superblock.
Definition: super_blk.hh:125
CacheBlk
A Basic Cache block.
Definition: cache_blk.hh:84
SectorSubBlk::setSectorOffset
void setSectorOffset(const int sector_offset)
Set offset of this sub-block within the sector.
Definition: sector_blk.cc:55
SuperBlk::isCompressed
bool isCompressed(const CompressionBlk *ignored_blk=nullptr) const
Returns whether the superblock contains compressed blocks or not.
Definition: super_blk.cc:95
SectorTags::numSectors
const unsigned numSectors
The number of sectors in the cache.
Definition: sector_tags.hh:80
Packet
A Packet is used to encapsulate a transfer between two objects in the memory system (e....
Definition: packet.hh:257
SectorSubBlk::getSectorBlock
const SectorBlk * getSectorBlock() const
Get sector block associated to this block.
Definition: sector_blk.cc:49
addr
ip6_addr_t addr
Definition: inet.hh:423
SectorSubBlk
A sector is composed of sub-blocks, and each sub-block has information regarding its sector and a poi...
Definition: sector_blk.hh:48
ReplaceableEntry::replacementData
std::shared_ptr< ReplacementData > replacementData
Replacement data associated to this entry.
Definition: replaceable_entry.hh:74
trace.hh
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
CompressedTags
A CompressedTags cache tag store.
Definition: compressed_tags.hh:71
CompressionBlk
A superblock is composed of sub-blocks, and each sub-block has information regarding its superblock a...
Definition: super_blk.hh:48
CompressedTags::tagsInit
void tagsInit() override
Initialize blocks as SuperBlk and CompressionBlk instances.
Definition: compressed_tags.cc:50
ArmISA::offset
Bitfield< 23, 0 > offset
Definition: types.hh:153

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