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