mem-cache: Encapsulate CacheBlk's refCount
[gem5.git] / src / mem / cache / tags / sector_tags.cc
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
29 /**
30 * @file
31 * Definitions of a sector tag store.
32 */
33
34 #include "mem/cache/tags/sector_tags.hh"
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"
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"
47
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)
56 {
57 // Check parameters
58 fatal_if(blkSize < 4 || !isPowerOf2(blkSize),
59 "Block size must be at least 4 and a power of 2");
60 fatal_if(!isPowerOf2(numBlocksPerSector),
61 "# of blocks per sector must be non-zero and a power of 2");
62 }
63
64 void
65 SectorTags::tagsInit()
66 {
67 // Create blocks and sector blocks
68 blks = std::vector<SectorSubBlk>(numBlocks);
69 secBlks = std::vector<SectorBlk>(numSectors);
70
71 // Initialize all blocks
72 unsigned blk_index = 0; // index into blks array
73 for (unsigned sec_blk_index = 0; sec_blk_index < numSectors;
74 sec_blk_index++)
75 {
76 // Locate next cache sector
77 SectorBlk* sec_blk = &secBlks[sec_blk_index];
78
79 // Associate a replacement data entry to the sector
80 sec_blk->replacementData = replacementPolicy->instantiateEntry();
81
82 // Initialize all blocks in this sector
83 sec_blk->blks.resize(numBlocksPerSector);
84 for (unsigned k = 0; k < numBlocksPerSector; ++k){
85 // Select block within the set to be linked
86 SectorSubBlk*& blk = sec_blk->blks[k];
87
88 // Locate next cache block
89 blk = &blks[blk_index];
90
91 // Associate a data chunk to the block
92 blk->data = &dataBlks[blkSize*blk_index];
93
94 // Associate sector block to this block
95 blk->setSectorBlock(sec_blk);
96
97 // Associate the sector replacement data to this block
98 blk->replacementData = sec_blk->replacementData;
99
100 // Set its index and sector offset
101 blk->setSectorOffset(k);
102
103 // Update block index
104 ++blk_index;
105 }
106
107 // Link block to indexing policy
108 indexingPolicy->setEntry(sec_blk, sec_blk_index);
109 }
110 }
111
112 void
113 SectorTags::invalidate(CacheBlk *blk)
114 {
115 BaseTags::invalidate(blk);
116
117 // Get block's sector
118 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
119 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
120
121 // When a block in a sector is invalidated, it does not make the tag
122 // invalid automatically, as there might be other blocks in the sector
123 // using it. The tag is invalidated only when there is a single block
124 // in the sector.
125 if (!sector_blk->isValid()) {
126 // Decrease the number of tags in use
127 stats.tagsInUse--;
128
129 // Invalidate replacement data, as we're invalidating the sector
130 replacementPolicy->invalidate(sector_blk->replacementData);
131 }
132 }
133
134 CacheBlk*
135 SectorTags::accessBlock(Addr addr, bool is_secure, Cycles &lat)
136 {
137 CacheBlk *blk = findBlock(addr, is_secure);
138
139 // Access all tags in parallel, hence one in each way. The data side
140 // either accesses all blocks in parallel, or one block sequentially on
141 // a hit. Sequential access with a miss doesn't access data.
142 stats.tagAccesses += allocAssoc;
143 if (sequentialAccess) {
144 if (blk != nullptr) {
145 stats.dataAccesses += 1;
146 }
147 } else {
148 stats.dataAccesses += allocAssoc*numBlocksPerSector;
149 }
150
151 // If a cache hit
152 if (blk != nullptr) {
153 // Update number of references to accessed block
154 blk->increaseRefCount();
155
156 // Get block's sector
157 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
158 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
159
160 // Update replacement data of accessed block, which is shared with
161 // the whole sector it belongs to
162 replacementPolicy->touch(sector_blk->replacementData);
163 }
164
165 // The tag lookup latency is the same for a hit or a miss
166 lat = lookupLatency;
167
168 return blk;
169 }
170
171 void
172 SectorTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
173 {
174 // Get block's sector
175 SectorSubBlk* sub_blk = static_cast<SectorSubBlk*>(blk);
176 const SectorBlk* sector_blk = sub_blk->getSectorBlock();
177
178 // When a block is inserted, the tag is only a newly used tag if the
179 // sector was not previously present in the cache.
180 if (sector_blk->isValid()) {
181 // An existing entry's replacement data is just updated
182 replacementPolicy->touch(sector_blk->replacementData);
183 } else {
184 // Increment tag counter
185 stats.tagsInUse++;
186
187 // A new entry resets the replacement data
188 replacementPolicy->reset(sector_blk->replacementData);
189 }
190
191 // Do common block insertion functionality
192 BaseTags::insertBlock(pkt, blk);
193 }
194
195 CacheBlk*
196 SectorTags::findBlock(Addr addr, bool is_secure) const
197 {
198 // Extract sector tag
199 const Addr tag = extractTag(addr);
200
201 // The address can only be mapped to a specific location of a sector
202 // due to sectors being composed of contiguous-address entries
203 const Addr offset = extractSectorOffset(addr);
204
205 // Find all possible sector entries that may contain the given address
206 const std::vector<ReplaceableEntry*> entries =
207 indexingPolicy->getPossibleEntries(addr);
208
209 // Search for block
210 for (const auto& sector : entries) {
211 auto blk = static_cast<SectorBlk*>(sector)->blks[offset];
212 if (blk->matchTag(tag, is_secure)) {
213 return blk;
214 }
215 }
216
217 // Did not find block
218 return nullptr;
219 }
220
221 CacheBlk*
222 SectorTags::findVictim(Addr addr, const bool is_secure, const std::size_t size,
223 std::vector<CacheBlk*>& evict_blks)
224 {
225 // Get possible entries to be victimized
226 const std::vector<ReplaceableEntry*> sector_entries =
227 indexingPolicy->getPossibleEntries(addr);
228
229 // Check if the sector this address belongs to has been allocated
230 Addr tag = extractTag(addr);
231 SectorBlk* victim_sector = nullptr;
232 for (const auto& sector : sector_entries) {
233 SectorBlk* sector_blk = static_cast<SectorBlk*>(sector);
234 if (sector_blk->matchTag(tag, is_secure)) {
235 victim_sector = sector_blk;
236 break;
237 }
238 }
239
240 // If the sector is not present
241 if (victim_sector == nullptr){
242 // Choose replacement victim from replacement candidates
243 victim_sector = static_cast<SectorBlk*>(replacementPolicy->getVictim(
244 sector_entries));
245 }
246
247 // Get the entry of the victim block within the sector
248 SectorSubBlk* victim = victim_sector->blks[extractSectorOffset(addr)];
249
250 // Get evicted blocks. Blocks are only evicted if the sectors mismatch and
251 // the currently existing sector is valid.
252 if (victim_sector->matchTag(tag, is_secure)) {
253 // It would be a hit if victim was valid, and upgrades do not call
254 // findVictim, so it cannot happen
255 assert(!victim->isValid());
256 } else {
257 // The whole sector must be evicted to make room for the new sector
258 for (const auto& blk : victim_sector->blks){
259 if (blk->isValid()) {
260 evict_blks.push_back(blk);
261 }
262 }
263 }
264
265 // Update number of sub-blocks evicted due to a replacement
266 sectorStats.evictionsReplacement[evict_blks.size()]++;
267
268 return victim;
269 }
270
271 int
272 SectorTags::extractSectorOffset(Addr addr) const
273 {
274 return (addr >> sectorShift) & sectorMask;
275 }
276
277 Addr
278 SectorTags::regenerateBlkAddr(const CacheBlk* blk) const
279 {
280 const SectorSubBlk* blk_cast = static_cast<const SectorSubBlk*>(blk);
281 const SectorBlk* sec_blk = blk_cast->getSectorBlock();
282 const Addr sec_addr =
283 indexingPolicy->regenerateAddr(blk->getTag(), sec_blk);
284 return sec_addr | ((Addr)blk_cast->getSectorOffset() << sectorShift);
285 }
286
287 SectorTags::SectorTagsStats::SectorTagsStats(BaseTagStats &base_group,
288 SectorTags& _tags)
289 : Stats::Group(&base_group), tags(_tags),
290 evictionsReplacement(this, "evictions_replacement",
291 "Number of blocks evicted due to a replacement")
292 {
293 }
294
295 void
296 SectorTags::SectorTagsStats::regStats()
297 {
298 Stats::Group::regStats();
299
300 evictionsReplacement.init(tags.numBlocksPerSector + 1);
301 for (unsigned i = 0; i <= tags.numBlocksPerSector; ++i) {
302 evictionsReplacement.subname(i, std::to_string(i));
303 evictionsReplacement.subdesc(i, "Number of replacements that caused " \
304 "the eviction of " + std::to_string(i) + " blocks");
305 }
306 }
307
308 void
309 SectorTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
310 {
311 for (SectorSubBlk& blk : blks) {
312 visitor(blk);
313 }
314 }
315
316 bool
317 SectorTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
318 {
319 for (SectorSubBlk& blk : blks) {
320 if (visitor(blk)) {
321 return true;
322 }
323 }
324 return false;
325 }
326
327 SectorTags *
328 SectorTagsParams::create()
329 {
330 // There must be a indexing policy
331 fatal_if(!indexing_policy, "An indexing policy is required");
332
333 return new SectorTags(this);
334 }