misc: Delete the now unnecessary create methods.
[gem5.git] / src / mem / cache / tags / compressed_tags.cc
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
29 /**
30 * @file
31 * Definitions of a base set associative compressed superblocks tag store.
32 */
33
34 #include "mem/cache/tags/compressed_tags.hh"
35
36 #include "base/trace.hh"
37 #include "debug/CacheComp.hh"
38 #include "mem/cache/replacement_policies/base.hh"
39 #include "mem/cache/replacement_policies/replaceable_entry.hh"
40 #include "mem/cache/tags/indexing_policies/base.hh"
41 #include "mem/packet.hh"
42 #include "params/CompressedTags.hh"
43
44 CompressedTags::CompressedTags(const Params &p)
45 : SectorTags(p)
46 {
47 }
48
49 void
50 CompressedTags::tagsInit()
51 {
52 // Create blocks and superblocks
53 blks = std::vector<CompressionBlk>(numBlocks);
54 superBlks = std::vector<SuperBlk>(numSectors);
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
69 superblock->replacementData = replacementPolicy->instantiateEntry();
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 =
108 indexingPolicy->getPossibleEntries(addr);
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 (superblock->matchTag(tag, is_secure) &&
119 !superblock->blks[offset]->isValid() &&
120 superblock->isCompressed() &&
121 superblock->canCoAllocate(compressed_size))
122 {
123 victim_superblock = superblock;
124 is_co_allocation = true;
125 break;
126 }
127 }
128
129 // If the superblock is not present or cannot be co-allocated a
130 // superblock must be replaced
131 if (victim_superblock == nullptr){
132 // Choose replacement victim from replacement candidates
133 victim_superblock = static_cast<SuperBlk*>(
134 replacementPolicy->getVictim(superblock_entries));
135
136 // The whole superblock must be evicted to make room for the new one
137 for (const auto& blk : victim_superblock->blks){
138 if (blk->isValid()) {
139 evict_blks.push_back(blk);
140 }
141 }
142 }
143
144 // Get the location of the victim block within the superblock
145 SectorSubBlk* victim = victim_superblock->blks[offset];
146
147 // It would be a hit if victim was valid in a co-allocation, and upgrades
148 // do not call findVictim, so it cannot happen
149 if (is_co_allocation){
150 assert(!victim->isValid());
151
152 // Print all co-allocated blocks
153 DPRINTF(CacheComp, "Co-Allocation: offset %d with blocks\n", offset);
154 for (const auto& blk : victim_superblock->blks){
155 if (blk->isValid()) {
156 DPRINTFR(CacheComp, "\t[%s]\n", blk->print());
157 }
158 }
159 }
160
161 // Update number of sub-blocks evicted due to a replacement
162 sectorStats.evictionsReplacement[evict_blks.size()]++;
163
164 return victim;
165 }
166
167 void
168 CompressedTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
169 {
170 // We check if block can co-allocate before inserting, because this check
171 // assumes the block is still invalid
172 CompressionBlk* compression_blk = static_cast<CompressionBlk*>(blk);
173 const SuperBlk* superblock = static_cast<const SuperBlk*>(
174 compression_blk->getSectorBlock());
175 const bool is_co_allocatable = superblock->isCompressed() &&
176 superblock->canCoAllocate(compression_blk->getSizeBits());
177
178 // Insert block
179 SectorTags::insertBlock(pkt, blk);
180
181 // We always store compressed blocks when possible
182 if (is_co_allocatable) {
183 compression_blk->setCompressed();
184 } else {
185 compression_blk->setUncompressed();
186 }
187 }
188
189 void
190 CompressedTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
191 {
192 for (CompressionBlk& blk : blks) {
193 visitor(blk);
194 }
195 }
196
197 bool
198 CompressedTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
199 {
200 for (CompressionBlk& blk : blks) {
201 if (visitor(blk)) {
202 return true;
203 }
204 }
205 return false;
206 }