mem-cache: Fix invalidation of prefetchers
[gem5.git] / src / mem / cache / compressors / base.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 * Authors: Daniel Carvalho
29 */
30
31 /** @file
32 * Definition of a basic cache compressor.
33 */
34
35 #include "mem/cache/compressors/base.hh"
36
37 #include <algorithm>
38 #include <cmath>
39 #include <cstdint>
40 #include <string>
41
42 #include "debug/CacheComp.hh"
43 #include "mem/cache/tags/super_blk.hh"
44 #include "params/BaseCacheCompressor.hh"
45
46 // Uncomment this line if debugging compression
47 //#define DEBUG_COMPRESSION
48
49 BaseCacheCompressor::CompressionData::CompressionData()
50 : _size(0)
51 {
52 }
53
54 BaseCacheCompressor::CompressionData::~CompressionData()
55 {
56 }
57
58 void
59 BaseCacheCompressor::CompressionData::setSizeBits(std::size_t size)
60 {
61 _size = size;
62 }
63
64 std::size_t
65 BaseCacheCompressor::CompressionData::getSizeBits() const
66 {
67 return _size;
68 }
69
70 std::size_t
71 BaseCacheCompressor::CompressionData::getSize() const
72 {
73 return std::ceil(_size/8);
74 }
75
76 BaseCacheCompressor::BaseCacheCompressor(const Params *p)
77 : SimObject(p), blkSize(p->block_size), sizeThreshold(p->size_threshold),
78 stats(*this)
79 {
80 fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
81 }
82
83 void
84 BaseCacheCompressor::compress(const uint64_t* data, Cycles& comp_lat,
85 Cycles& decomp_lat, std::size_t& comp_size_bits)
86 {
87 // Apply compression
88 std::unique_ptr<CompressionData> comp_data =
89 compress(data, comp_lat, decomp_lat);
90
91 // If we are in debug mode apply decompression just after the compression.
92 // If the results do not match, we've got an error
93 #ifdef DEBUG_COMPRESSION
94 uint64_t decomp_data[blkSize/8];
95
96 // Apply decompression
97 decompress(comp_data.get(), decomp_data);
98
99 // Check if decompressed line matches original cache line
100 fatal_if(std::memcmp(data, decomp_data, blkSize),
101 "Decompressed line does not match original line.");
102 #endif
103
104 // Get compression size. If compressed size is greater than the size
105 // threshold, the compression is seen as unsuccessful
106 comp_size_bits = comp_data->getSizeBits();
107 if (comp_size_bits >= sizeThreshold * 8) {
108 comp_size_bits = blkSize * 8;
109 }
110
111 // Update stats
112 stats.compressions++;
113 stats.compressionSizeBits += comp_size_bits;
114 stats.compressionSize[std::ceil(std::log2(comp_size_bits))]++;
115
116 // Print debug information
117 DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
118 "Compression latency: %llu, decompression latency: %llu\n",
119 blkSize*8, comp_size_bits, comp_lat, decomp_lat);
120 }
121
122 Cycles
123 BaseCacheCompressor::getDecompressionLatency(const CacheBlk* blk)
124 {
125 const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
126
127 // If block is compressed, return its decompression latency
128 if (comp_blk && comp_blk->isCompressed()){
129 const Cycles decomp_lat = comp_blk->getDecompressionLatency();
130 DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
131 comp_blk->print(), decomp_lat);
132 stats.decompressions += 1;
133 return decomp_lat;
134 }
135
136 // Block is not compressed, so there is no decompression latency
137 return Cycles(0);
138 }
139
140 void
141 BaseCacheCompressor::setDecompressionLatency(CacheBlk* blk, const Cycles lat)
142 {
143 // Sanity check
144 assert(blk != nullptr);
145
146 // Assign latency
147 static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
148 }
149
150 void
151 BaseCacheCompressor::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
152 {
153 // Sanity check
154 assert(blk != nullptr);
155
156 // Assign size
157 static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
158 }
159
160 BaseCacheCompressor::BaseCacheCompressorStats::BaseCacheCompressorStats(
161 BaseCacheCompressor& _compressor)
162 : Stats::Group(&_compressor), compressor(_compressor),
163 compressions(this, "compressions",
164 "Total number of compressions"),
165 compressionSize(this, "compression_size",
166 "Number of blocks that were compressed to this power of two size"),
167 compressionSizeBits(this, "compression_size_bits",
168 "Total compressed data size, in bits"),
169 avgCompressionSizeBits(this, "avg_compression_size_bits",
170 "Average compression size, in bits"),
171 decompressions(this, "total_decompressions",
172 "Total number of decompressions")
173 {
174 }
175
176 void
177 BaseCacheCompressor::BaseCacheCompressorStats::regStats()
178 {
179 Stats::Group::regStats();
180
181 compressionSize.init(std::log2(compressor.blkSize*8) + 1);
182 for (unsigned i = 0; i <= std::log2(compressor.blkSize*8); ++i) {
183 std::string str_i = std::to_string(1 << i);
184 compressionSize.subname(i, str_i);
185 compressionSize.subdesc(i,
186 "Number of blocks that compressed to fit in " + str_i + " bits");
187 }
188
189 avgCompressionSizeBits.flags(Stats::total | Stats::nozero | Stats::nonan);
190 avgCompressionSizeBits = compressionSizeBits / compressions;
191 }
192