2 * Copyright (c) 2018 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 base set associative compressed superblocks tag store.
34 #include "mem/cache/tags/compressed_tags.hh"
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"
44 CompressedTags::CompressedTags(const Params
&p
)
50 CompressedTags::tagsInit()
52 // Create blocks and superblocks
53 blks
= std::vector
<CompressionBlk
>(numBlocks
);
54 superBlks
= std::vector
<SuperBlk
>(numSectors
);
56 // Initialize all blocks
57 unsigned blk_index
= 0; // index into blks array
58 for (unsigned superblock_index
= 0; superblock_index
< numSectors
;
61 // Locate next cache superblock
62 SuperBlk
* superblock
= &superBlks
[superblock_index
];
64 // Superblocks must be aware of the block size due to their co-
65 // allocation conditions
66 superblock
->setBlkSize(blkSize
);
68 // Associate a replacement data entry to the block
69 superblock
->replacementData
= replacementPolicy
->instantiateEntry();
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
];
77 // Locate next cache block
78 blk
= &blks
[blk_index
];
80 // Associate a data chunk to the block
81 blk
->data
= &dataBlks
[blkSize
*blk_index
];
83 // Associate superblock to this block
84 blk
->setSectorBlock(superblock
);
86 // Associate the superblock replacement data to this block
87 blk
->replacementData
= superblock
->replacementData
;
89 // Set its index and sector offset
90 blk
->setSectorOffset(k
);
96 // Link block to indexing policy
97 indexingPolicy
->setEntry(superblock
, superblock_index
);
102 CompressedTags::findVictim(Addr addr
, const bool is_secure
,
103 const std::size_t compressed_size
,
104 std::vector
<CacheBlk
*>& evict_blks
)
106 // Get all possible locations of this superblock
107 const std::vector
<ReplaceableEntry
*> superblock_entries
=
108 indexingPolicy
->getPossibleEntries(addr
);
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
))
123 victim_superblock
= superblock
;
124 is_co_allocation
= true;
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
));
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
);
144 // Get the location of the victim block within the superblock
145 SectorSubBlk
* victim
= victim_superblock
->blks
[offset
];
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());
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());
161 // Update number of sub-blocks evicted due to a replacement
162 sectorStats
.evictionsReplacement
[evict_blks
.size()]++;
168 CompressedTags::insertBlock(const PacketPtr pkt
, CacheBlk
*blk
)
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());
179 SectorTags::insertBlock(pkt
, blk
);
181 // We always store compressed blocks when possible
182 if (is_co_allocatable
) {
183 compression_blk
->setCompressed();
185 compression_blk
->setUncompressed();
190 CompressedTags::forEachBlk(std::function
<void(CacheBlk
&)> visitor
)
192 for (CompressionBlk
& blk
: blks
) {
198 CompressedTags::anyBlk(std::function
<bool(CacheBlk
&)> visitor
)
200 for (CompressionBlk
& blk
: blks
) {