mem-cache: Limit compression size
[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 <cstdint>
39 #include <string>
40
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 {
78 fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
79 }
80
81 void
82 BaseCacheCompressor::compress(const uint64_t* data, Cycles& comp_lat,
83 Cycles& decomp_lat, std::size_t& comp_size_bits)
84 {
85 // Apply compression
86 std::unique_ptr<CompressionData> comp_data =
87 compress(data, comp_lat, decomp_lat);
88
89 // If we are in debug mode apply decompression just after the compression.
90 // If the results do not match, we've got an error
91 #ifdef DEBUG_COMPRESSION
92 uint64_t decomp_data[blkSize/8];
93
94 // Apply decompression
95 decompress(comp_data.get(), decomp_data);
96
97 // Check if decompressed line matches original cache line
98 fatal_if(std::memcmp(data, decomp_data, blkSize),
99 "Decompressed line does not match original line.");
100 #endif
101
102 // Get compression size. If compressed size is greater than the size
103 // threshold, the compression is seen as unsuccessful
104 comp_size_bits = comp_data->getSizeBits();
105 if (comp_size_bits >= sizeThreshold * 8) {
106 comp_size_bits = blkSize * 8;
107 }
108
109 // Update stats
110 compressionSize[std::ceil(std::log2(comp_size_bits))]++;
111
112 // Print debug information
113 DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
114 "Compression latency: %llu, decompression latency: %llu\n",
115 blkSize*8, comp_size_bits, comp_lat, decomp_lat);
116 }
117
118 Cycles
119 BaseCacheCompressor::getDecompressionLatency(const CacheBlk* blk) const
120 {
121 const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
122
123 // If block is compressed, return its decompression latency
124 if (comp_blk && comp_blk->isCompressed()){
125 const Cycles decomp_lat = comp_blk->getDecompressionLatency();
126 DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
127 comp_blk->print(), decomp_lat);
128 return decomp_lat;
129 }
130
131 // Block is not compressed, so there is no decompression latency
132 return Cycles(0);
133 }
134
135 void
136 BaseCacheCompressor::setDecompressionLatency(CacheBlk* blk, const Cycles lat)
137 {
138 // Sanity check
139 assert(blk != nullptr);
140
141 // Assign latency
142 static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
143 }
144
145 void
146 BaseCacheCompressor::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
147 {
148 // Sanity check
149 assert(blk != nullptr);
150
151 // Assign size
152 static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
153 }
154
155 void
156 BaseCacheCompressor::regStats()
157 {
158 SimObject::regStats();
159
160 compressionSize
161 .init(std::log2(blkSize*8) + 1)
162 .name(name() + ".compression_size")
163 .desc("Number of blocks that were compressed to this power of" \
164 "two size.")
165 ;
166
167 for (unsigned i = 0; i <= std::log2(blkSize*8); ++i) {
168 compressionSize.subname(i, std::to_string(1 << i));
169 compressionSize.subdesc(i, "Number of blocks that compressed to fit " \
170 "in " + std::to_string(1 << i) + " bits");
171 }
172 }
173