2 * Copyright (c) 2018, 2020 Inria
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.
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.
31 * Definitions of a sector tag store.
34 #include "mem/cache/tags/sector_tags.hh"
40 #include "base/intmath.hh"
41 #include "base/logging.hh"
42 #include "base/types.hh"
43 #include "mem/cache/base.hh"
44 #include "mem/cache/replacement_policies/base.hh"
45 #include "mem/cache/replacement_policies/replaceable_entry.hh"
46 #include "mem/cache/tags/indexing_policies/base.hh"
48 SectorTags::SectorTags(const SectorTagsParams
&p
)
49 : BaseTags(p
), allocAssoc(p
.assoc
),
50 sequentialAccess(p
.sequential_access
),
51 replacementPolicy(p
.replacement_policy
),
52 numBlocksPerSector(p
.num_blocks_per_sector
),
53 numSectors(numBlocks
/ numBlocksPerSector
),
54 sectorShift(floorLog2(blkSize
)), sectorMask(numBlocksPerSector
- 1),
55 sectorStats(stats
, *this)
57 // There must be a indexing policy
58 fatal_if(!p
.indexing_policy
, "An indexing policy is required");
61 fatal_if(blkSize
< 4 || !isPowerOf2(blkSize
),
62 "Block size must be at least 4 and a power of 2");
63 fatal_if(!isPowerOf2(numBlocksPerSector
),
64 "# of blocks per sector must be non-zero and a power of 2");
68 SectorTags::tagsInit()
70 // Create blocks and sector blocks
71 blks
= std::vector
<SectorSubBlk
>(numBlocks
);
72 secBlks
= std::vector
<SectorBlk
>(numSectors
);
74 // Initialize all blocks
75 unsigned blk_index
= 0; // index into blks array
76 for (unsigned sec_blk_index
= 0; sec_blk_index
< numSectors
;
79 // Locate next cache sector
80 SectorBlk
* sec_blk
= &secBlks
[sec_blk_index
];
82 // Associate a replacement data entry to the sector
83 sec_blk
->replacementData
= replacementPolicy
->instantiateEntry();
85 // Initialize all blocks in this sector
86 sec_blk
->blks
.resize(numBlocksPerSector
);
87 for (unsigned k
= 0; k
< numBlocksPerSector
; ++k
){
88 // Select block within the set to be linked
89 SectorSubBlk
*& blk
= sec_blk
->blks
[k
];
91 // Locate next cache block
92 blk
= &blks
[blk_index
];
94 // Associate a data chunk to the block
95 blk
->data
= &dataBlks
[blkSize
*blk_index
];
97 // Associate sector block to this block
98 blk
->setSectorBlock(sec_blk
);
100 // Associate the sector replacement data to this block
101 blk
->replacementData
= sec_blk
->replacementData
;
103 // Set its index and sector offset
104 blk
->setSectorOffset(k
);
106 // Update block index
110 // Link block to indexing policy
111 indexingPolicy
->setEntry(sec_blk
, sec_blk_index
);
116 SectorTags::invalidate(CacheBlk
*blk
)
118 BaseTags::invalidate(blk
);
120 // Get block's sector
121 SectorSubBlk
* sub_blk
= static_cast<SectorSubBlk
*>(blk
);
122 const SectorBlk
* sector_blk
= sub_blk
->getSectorBlock();
124 // When a block in a sector is invalidated, it does not make the tag
125 // invalid automatically, as there might be other blocks in the sector
126 // using it. The tag is invalidated only when there is a single block
128 if (!sector_blk
->isValid()) {
129 // Decrease the number of tags in use
132 // Invalidate replacement data, as we're invalidating the sector
133 replacementPolicy
->invalidate(sector_blk
->replacementData
);
138 SectorTags::accessBlock(Addr addr
, bool is_secure
, Cycles
&lat
)
140 CacheBlk
*blk
= findBlock(addr
, is_secure
);
142 // Access all tags in parallel, hence one in each way. The data side
143 // either accesses all blocks in parallel, or one block sequentially on
144 // a hit. Sequential access with a miss doesn't access data.
145 stats
.tagAccesses
+= allocAssoc
;
146 if (sequentialAccess
) {
147 if (blk
!= nullptr) {
148 stats
.dataAccesses
+= 1;
151 stats
.dataAccesses
+= allocAssoc
*numBlocksPerSector
;
155 if (blk
!= nullptr) {
156 // Update number of references to accessed block
157 blk
->increaseRefCount();
159 // Get block's sector
160 SectorSubBlk
* sub_blk
= static_cast<SectorSubBlk
*>(blk
);
161 const SectorBlk
* sector_blk
= sub_blk
->getSectorBlock();
163 // Update replacement data of accessed block, which is shared with
164 // the whole sector it belongs to
165 replacementPolicy
->touch(sector_blk
->replacementData
);
168 // The tag lookup latency is the same for a hit or a miss
175 SectorTags::insertBlock(const PacketPtr pkt
, CacheBlk
*blk
)
177 // Get block's sector
178 SectorSubBlk
* sub_blk
= static_cast<SectorSubBlk
*>(blk
);
179 const SectorBlk
* sector_blk
= sub_blk
->getSectorBlock();
181 // When a block is inserted, the tag is only a newly used tag if the
182 // sector was not previously present in the cache.
183 if (sector_blk
->isValid()) {
184 // An existing entry's replacement data is just updated
185 replacementPolicy
->touch(sector_blk
->replacementData
);
187 // Increment tag counter
190 // A new entry resets the replacement data
191 replacementPolicy
->reset(sector_blk
->replacementData
);
194 // Do common block insertion functionality
195 BaseTags::insertBlock(pkt
, blk
);
199 SectorTags::findBlock(Addr addr
, bool is_secure
) const
201 // Extract sector tag
202 const Addr tag
= extractTag(addr
);
204 // The address can only be mapped to a specific location of a sector
205 // due to sectors being composed of contiguous-address entries
206 const Addr offset
= extractSectorOffset(addr
);
208 // Find all possible sector entries that may contain the given address
209 const std::vector
<ReplaceableEntry
*> entries
=
210 indexingPolicy
->getPossibleEntries(addr
);
213 for (const auto& sector
: entries
) {
214 auto blk
= static_cast<SectorBlk
*>(sector
)->blks
[offset
];
215 if (blk
->matchTag(tag
, is_secure
)) {
220 // Did not find block
225 SectorTags::findVictim(Addr addr
, const bool is_secure
, const std::size_t size
,
226 std::vector
<CacheBlk
*>& evict_blks
)
228 // Get possible entries to be victimized
229 const std::vector
<ReplaceableEntry
*> sector_entries
=
230 indexingPolicy
->getPossibleEntries(addr
);
232 // Check if the sector this address belongs to has been allocated
233 Addr tag
= extractTag(addr
);
234 SectorBlk
* victim_sector
= nullptr;
235 for (const auto& sector
: sector_entries
) {
236 SectorBlk
* sector_blk
= static_cast<SectorBlk
*>(sector
);
237 if (sector_blk
->matchTag(tag
, is_secure
)) {
238 victim_sector
= sector_blk
;
243 // If the sector is not present
244 if (victim_sector
== nullptr){
245 // Choose replacement victim from replacement candidates
246 victim_sector
= static_cast<SectorBlk
*>(replacementPolicy
->getVictim(
250 // Get the entry of the victim block within the sector
251 SectorSubBlk
* victim
= victim_sector
->blks
[extractSectorOffset(addr
)];
253 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
254 // the currently existing sector is valid.
255 if (victim_sector
->matchTag(tag
, is_secure
)) {
256 // It would be a hit if victim was valid, and upgrades do not call
257 // findVictim, so it cannot happen
258 assert(!victim
->isValid());
260 // The whole sector must be evicted to make room for the new sector
261 for (const auto& blk
: victim_sector
->blks
){
262 if (blk
->isValid()) {
263 evict_blks
.push_back(blk
);
268 // Update number of sub-blocks evicted due to a replacement
269 sectorStats
.evictionsReplacement
[evict_blks
.size()]++;
275 SectorTags::extractSectorOffset(Addr addr
) const
277 return (addr
>> sectorShift
) & sectorMask
;
281 SectorTags::regenerateBlkAddr(const CacheBlk
* blk
) const
283 const SectorSubBlk
* blk_cast
= static_cast<const SectorSubBlk
*>(blk
);
284 const SectorBlk
* sec_blk
= blk_cast
->getSectorBlock();
285 const Addr sec_addr
=
286 indexingPolicy
->regenerateAddr(blk
->getTag(), sec_blk
);
287 return sec_addr
| ((Addr
)blk_cast
->getSectorOffset() << sectorShift
);
290 SectorTags::SectorTagsStats::SectorTagsStats(BaseTagStats
&base_group
,
292 : Stats::Group(&base_group
), tags(_tags
),
293 evictionsReplacement(this, "evictions_replacement",
294 "Number of blocks evicted due to a replacement")
299 SectorTags::SectorTagsStats::regStats()
301 Stats::Group::regStats();
303 evictionsReplacement
.init(tags
.numBlocksPerSector
+ 1);
304 for (unsigned i
= 0; i
<= tags
.numBlocksPerSector
; ++i
) {
305 evictionsReplacement
.subname(i
, std::to_string(i
));
306 evictionsReplacement
.subdesc(i
, "Number of replacements that caused " \
307 "the eviction of " + std::to_string(i
) + " blocks");
312 SectorTags::forEachBlk(std::function
<void(CacheBlk
&)> visitor
)
314 for (SectorSubBlk
& blk
: blks
) {
320 SectorTags::anyBlk(std::function
<bool(CacheBlk
&)> visitor
)
322 for (SectorSubBlk
& blk
: blks
) {