mem-cache: Fix copy ellision on base compressor
[gem5.git] / src / mem / cache / tags / compressed_tags.hh
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 * Declaration of a compressed set associative tag store using superblocks.
32 */
33
34 #ifndef __MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__
35 #define __MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__
36
37 #include <vector>
38
39 #include "mem/cache/tags/sector_tags.hh"
40 #include "mem/cache/tags/super_blk.hh"
41
42 class BaseCache;
43 class CacheBlk;
44 struct CompressedTagsParams;
45
46 /**
47 * A CompressedTags cache tag store.
48 * @sa \ref gem5MemorySystem "gem5 Memory System"
49 *
50 * The Compression Ratio (CR) of a superblock is defined by
51 * CR = uncompressed_size / compressed_size.
52 *
53 * The CompressedTags placement policy divides the cache into s sets of w
54 * superblocks (ways). Each superblock can then contain up to CR compressed
55 * blocks.
56 *
57 * For each tag entry there can be multiple data blocks. We have the same
58 * number of tags a conventional cache would have, but we instantiate the
59 * maximum number of data blocks (according to the compression ratio) per
60 * tag, to virtually implement compression without increasing the complexity
61 * of the simulator.
62 *
63 * This is a simple implementation of cache compression, where superblocks
64 * can only have at most numBlocksPerSector compressed blocks, each compressed
65 * to at least (100/numBlocksPerSector)% of its size.
66 *
67 * numBlocksPerSector holds the maximum number of blocks a superblock with
68 * the best possible compression factor would hold. It is equivalent to CR
69 * from the previous definition.
70 */
71 class CompressedTags : public SectorTags
72 {
73 private:
74 /** The cache blocks. */
75 std::vector<CompressionBlk> blks;
76 /** The cache superblocks. */
77 std::vector<SuperBlk> superBlks;
78
79 public:
80 /** Convenience typedef. */
81 typedef CompressedTagsParams Params;
82
83 /**
84 * Construct and initialize this tag store.
85 */
86 CompressedTags(const Params *p);
87
88 /**
89 * Destructor.
90 */
91 virtual ~CompressedTags() {};
92
93 /**
94 * Initialize blocks as SuperBlk and CompressionBlk instances.
95 */
96 void tagsInit() override;
97
98 /**
99 * Find replacement victim based on address. Checks if data can be co-
100 * allocated before choosing blocks to be evicted.
101 *
102 * @param addr Address to find a victim for.
103 * @param is_secure True if the target memory space is secure.
104 * @param compressed_size Size, in bits, of new block to allocate.
105 * @param evict_blks Cache blocks to be evicted.
106 * @return Cache block to be replaced.
107 */
108 CacheBlk* findVictim(Addr addr, const bool is_secure,
109 const std::size_t compressed_size,
110 std::vector<CacheBlk*>& evict_blks) override;
111
112 /**
113 * Insert the new block into the cache and update replacement data.
114 *
115 * @param pkt Packet holding the address to update
116 * @param blk The block to update.
117 */
118 void insertBlock(const PacketPtr pkt, CacheBlk *blk) override;
119
120 /**
121 * Visit each sub-block in the tags and apply a visitor.
122 *
123 * The visitor should be a std::function that takes a cache block.
124 * reference as its parameter.
125 *
126 * @param visitor Visitor to call on each block.
127 */
128 void forEachBlk(std::function<void(CacheBlk &)> visitor) override;
129
130 /**
131 * Find if any of the sub-blocks satisfies a condition.
132 *
133 * The visitor should be a std::function that takes a cache block
134 * reference as its parameter. The visitor will terminate the
135 * traversal early if the condition is satisfied.
136 *
137 * @param visitor Visitor to call on each block.
138 */
139 bool anyBlk(std::function<bool(CacheBlk &)> visitor) override;
140 };
141
142 #endif //__MEM_CACHE_TAGS_COMPRESSED_TAGS_HH__