gem5 v24.0.0.0
Loading...
Searching...
No Matches
base.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018-2020 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
34
35#include <algorithm>
36#include <climits>
37#include <cmath>
38#include <cstdint>
39#include <string>
40
41#include "base/logging.hh"
42#include "base/trace.hh"
43#include "debug/CacheComp.hh"
44#include "mem/cache/base.hh"
46#include "params/BaseCacheCompressor.hh"
47
48namespace gem5
49{
50
51namespace compression
52{
53
54// Uncomment this line if debugging compression
55//#define DEBUG_COMPRESSION
56
61
65
66void
68{
69 _size = size;
70}
71
72std::size_t
74{
75 return _size;
76}
77
78std::size_t
80{
81 return std::ceil(_size/(float)CHAR_BIT);
82}
83
85 : SimObject(p), blkSize(p.block_size), chunkSizeBits(p.chunk_size_bits),
86 sizeThreshold((blkSize * p.size_threshold_percentage) / 100),
87 compChunksPerCycle(p.comp_chunks_per_cycle),
88 compExtraLatency(p.comp_extra_latency),
89 decompChunksPerCycle(p.decomp_chunks_per_cycle),
90 decompExtraLatency(p.decomp_extra_latency),
91 cache(nullptr), stats(*this)
92{
94 "64 must be a multiple of the chunk granularity.");
95
97 "Compressor processes more chunks per cycle than the number of "
98 "chunks in the input");
100 "Decompressor processes more chunks per cycle than the number of "
101 "chunks in the input");
102
103 fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
104}
105
106void
108{
109 assert(!cache);
110 cache = _cache;
111}
112
114Base::toChunks(const uint64_t* data) const
115{
116 // Number of chunks in a 64-bit value
117 const unsigned num_chunks_per_64 =
118 (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
119
120 // Turn a 64-bit array into a chunkSizeBits-array
121 std::vector<Chunk> chunks((blkSize * CHAR_BIT) / chunkSizeBits, 0);
122 for (int i = 0; i < chunks.size(); i++) {
123 const int index_64 = std::floor(i / (double)num_chunks_per_64);
124 const unsigned start = i % num_chunks_per_64;
125 chunks[i] = bits(data[index_64],
126 (start + 1) * chunkSizeBits - 1, start * chunkSizeBits);
127 }
128
129 return chunks;
130}
131
132void
133Base::fromChunks(const std::vector<Chunk>& chunks, uint64_t* data) const
134{
135 // Number of chunks in a 64-bit value
136 const unsigned num_chunks_per_64 =
137 (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
138
139 // Turn a chunkSizeBits-array into a 64-bit array
140 std::memset(data, 0, blkSize);
141 for (int i = 0; i < chunks.size(); i++) {
142 const int index_64 = std::floor(i / (double)num_chunks_per_64);
143 const unsigned start = i % num_chunks_per_64;
144 replaceBits(data[index_64], (start + 1) * chunkSizeBits - 1,
145 start * chunkSizeBits, chunks[i]);
146 }
147}
148
149std::unique_ptr<Base::CompressionData>
150Base::compress(const uint64_t* data, Cycles& comp_lat, Cycles& decomp_lat)
151{
152 // Apply compression
153 std::unique_ptr<CompressionData> comp_data =
154 compress(toChunks(data), comp_lat, decomp_lat);
155
156 // If we are in debug mode apply decompression just after the compression.
157 // If the results do not match, we've got an error
158 #ifdef DEBUG_COMPRESSION
159 uint64_t decomp_data[blkSize/8];
160
161 // Apply decompression
162 decompress(comp_data.get(), decomp_data);
163
164 // Check if decompressed line matches original cache line
165 fatal_if(std::memcmp(data, decomp_data, blkSize),
166 "Decompressed line does not match original line.");
167 #endif
168
169 // Get compression size. If compressed size is greater than the size
170 // threshold, the compression is seen as unsuccessful
171 std::size_t comp_size_bits = comp_data->getSizeBits();
172 if (comp_size_bits > sizeThreshold * CHAR_BIT) {
173 comp_size_bits = blkSize * CHAR_BIT;
174 comp_data->setSizeBits(comp_size_bits);
176 }
177
178 // Update stats
180 stats.compressionSizeBits += comp_size_bits;
181 if (comp_size_bits != 0) {
182 stats.compressionSize[1 + std::ceil(std::log2(comp_size_bits))]++;
183 } else {
185 }
186
187 // Print debug information
188 DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
189 "Compression latency: %llu, decompression latency: %llu\n",
190 blkSize*8, comp_size_bits, comp_lat, decomp_lat);
191
192 return comp_data;
193}
194
195Cycles
197{
198 const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
199
200 // If block is compressed, return its decompression latency
201 if (comp_blk && comp_blk->isCompressed()){
202 const Cycles decomp_lat = comp_blk->getDecompressionLatency();
203 DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
204 comp_blk->print(), decomp_lat);
206 return decomp_lat;
207 }
208
209 // Block is not compressed, so there is no decompression latency
210 return Cycles(0);
211}
212
213void
215{
216 // Sanity check
217 assert(blk != nullptr);
218
219 // Assign latency
220 static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
221}
222
223void
224Base::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
225{
226 // Sanity check
227 assert(blk != nullptr);
228
229 // Assign size
230 static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
231}
232
234 : statistics::Group(&_compressor), compressor(_compressor),
235 ADD_STAT(compressions, statistics::units::Count::get(),
236 "Total number of compressions"),
237 ADD_STAT(failedCompressions, statistics::units::Count::get(),
238 "Total number of failed compressions"),
239 ADD_STAT(compressionSize, statistics::units::Count::get(),
240 "Number of blocks that were compressed to this power of two "
241 "size"),
242 ADD_STAT(compressionSizeBits, statistics::units::Bit::get(),
243 "Total compressed data size"),
244 ADD_STAT(avgCompressionSizeBits, statistics::units::Rate<
245 statistics::units::Bit, statistics::units::Count>::get(),
246 "Average compression size"),
247 ADD_STAT(decompressions, statistics::units::Count::get(),
248 "Total number of decompressions")
249{
250}
251
252void
254{
256
257 // Values comprised are {0, 1, 2, 4, ..., blkSize}
258 compressionSize.init(std::log2(compressor.blkSize*8) + 2);
259 compressionSize.subname(0, "0");
260 compressionSize.subdesc(0,
261 "Number of blocks that compressed to fit in 0 bits");
262 for (unsigned i = 0; i <= std::log2(compressor.blkSize*8); ++i) {
263 std::string str_i = std::to_string(1 << i);
264 compressionSize.subname(1+i, str_i);
265 compressionSize.subdesc(1+i,
266 "Number of blocks that compressed to fit in " + str_i + " bits");
267 }
268
269 avgCompressionSizeBits.flags(statistics::total | statistics::nozero |
271 avgCompressionSizeBits = compressionSizeBits / compressions;
272}
273
274} // namespace compression
275} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
const char data[]
A basic cache interface.
Definition base.hh:100
A Basic Cache block.
Definition cache_blk.hh:72
A superblock is composed of sub-blocks, and each sub-block has information regarding its superblock a...
Definition super_blk.hh:52
std::string print() const override
Pretty-print sector offset and other CacheBlk information.
Definition super_blk.cc:164
bool isCompressed() const
Check if this block holds compressed data.
Definition super_blk.cc:75
Cycles getDecompressionLatency() const
Get number of cycles needed to decompress this block.
Definition super_blk.cc:129
Cycles is a wrapper class for representing cycle counts, i.e.
Definition types.hh:79
Abstract superclass for simulation objects.
virtual ~CompressionData()
Virtual destructor.
Definition base.cc:62
CompressionData()
Default constructor.
Definition base.cc:57
std::size_t getSize() const
Get compression size (in bytes).
Definition base.cc:79
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition base.cc:67
std::size_t getSizeBits() const
Get compression size (in bits).
Definition base.cc:73
Base cache compressor interface.
Definition base.hh:65
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition base.hh:114
BaseCacheCompressorParams Params
Definition base.hh:201
void fromChunks(const std::vector< Chunk > &chunks, uint64_t *data) const
This function re-joins the chunks to recreate the original data.
Definition base.cc:133
gem5::compression::Base::BaseStats stats
Base(const Params &p)
Definition base.cc:84
BaseCache * cache
Pointer to the parent cache.
Definition base.hh:129
std::vector< Chunk > toChunks(const uint64_t *data) const
This function splits the raw data into chunks, so that it can be parsed by the compressor.
Definition base.cc:114
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition base.hh:126
const Cycles compChunksPerCycle
Degree of parallelization of the compression process.
Definition base.hh:108
Cycles getDecompressionLatency(const CacheBlk *blk)
Get the decompression latency if the block is compressed.
Definition base.cc:196
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition base.cc:224
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition base.hh:93
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition base.cc:107
virtual void decompress(const CompressionData *comp_data, uint64_t *cache_line)=0
Apply the decompression process to the compressed data.
const unsigned chunkSizeBits
Chunk size, in number of bits.
Definition base.hh:96
const std::size_t sizeThreshold
Size in bytes at which a compression is classified as bad and therefore the compressed block is resto...
Definition base.hh:102
static void setDecompressionLatency(CacheBlk *blk, const Cycles lat)
Set the decompression latency of compressed block.
Definition base.cc:214
virtual std::unique_ptr< CompressionData > compress(const std::vector< Chunk > &chunks, Cycles &comp_lat, Cycles &decomp_lat)=0
Apply the compression process to the cache line.
const Cycles decompChunksPerCycle
Degree of parallelization of the decompression process.
Definition base.hh:120
Statistics container.
Definition group.hh:93
STL vector class.
Definition stl.hh:37
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition group.hh:75
constexpr T bits(T val, unsigned first, unsigned last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition bitfield.hh:79
constexpr void replaceBits(T &val, unsigned first, unsigned last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Definition bitfield.hh:216
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
virtual void regStats()
Callback to set stat parameters.
Definition group.cc:68
Declares a basic cache interface BaseCache.
Definition of a basic cache compressor.
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 0 > p
const FlagsType nonan
Don't print if this is NAN.
Definition info.hh:69
const FlagsType nozero
Don't print if this is zero.
Definition info.hh:67
const FlagsType total
Print the total.
Definition info.hh:59
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
statistics::Scalar compressions
Number of compressions performed.
Definition base.hh:140
void regStats() override
Callback to set stat parameters.
Definition base.cc:253
BaseStats(Base &compressor)
Definition base.cc:233
statistics::Scalar decompressions
Number of decompressions performed.
Definition base.hh:155
statistics::Vector compressionSize
Number of blocks that were compressed to this power of two size.
Definition base.hh:146
statistics::Scalar failedCompressions
Number of failed compressions.
Definition base.hh:143
statistics::Scalar compressionSizeBits
Total compressed data size, in number of bits.
Definition base.hh:149
Copyright (c) 2018 Inria All rights reserved.

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0