gem5  [DEVELOP-FOR-23.0]
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
48 namespace gem5
49 {
50 
51 namespace compression
52 {
53 
54 // Uncomment this line if debugging compression
55 //#define DEBUG_COMPRESSION
56 
58  : _size(0)
59 {
60 }
61 
63 {
64 }
65 
66 void
68 {
69  _size = size;
70 }
71 
72 std::size_t
74 {
75  return _size;
76 }
77 
78 std::size_t
80 {
81  return std::ceil(_size/8);
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 
106 void
108 {
109  assert(!cache);
110  cache = _cache;
111 }
112 
114 Base::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 
132 void
133 Base::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 
149 std::unique_ptr<Base::CompressionData>
150 Base::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 {
184  stats.compressionSize[0]++;
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 
195 Cycles
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);
205  stats.decompressions += 1;
206  return decomp_lat;
207  }
208 
209  // Block is not compressed, so there is no decompression latency
210  return Cycles(0);
211 }
212 
213 void
215 {
216  // Sanity check
217  assert(blk != nullptr);
218 
219  // Assign latency
220  static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
221 }
222 
223 void
224 Base::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 
252 void
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
gem5::compression::Base::chunkSizeBits
const unsigned chunkSizeBits
Chunk size, in number of bits.
Definition: base.hh:96
base.hh
gem5::compression::Base::decompress
virtual void decompress(const CompressionData *comp_data, uint64_t *cache_line)=0
Apply the decompression process to the compressed data.
gem5::MipsISA::misc_reg::Count
@ Count
Definition: misc.hh:94
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::compression::Base::decompChunksPerCycle
const Cycles decompChunksPerCycle
Degree of parallelization of the decompression process.
Definition: base.hh:120
gem5::compression::Base::BaseStats::compressionSize
statistics::Vector compressionSize
Number of blocks that were compressed to this power of two size.
Definition: base.hh:146
base.hh
gem5::compression::Base::getDecompressionLatency
Cycles getDecompressionLatency(const CacheBlk *blk)
Get the decompression latency if the block is compressed.
Definition: base.cc:196
gem5::compression::Base::BaseStats::failedCompressions
statistics::Scalar failedCompressions
Number of failed compressions.
Definition: base.hh:143
gem5::replaceBits
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:213
gem5::statistics::nozero
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:67
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::compression::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:201
std::vector
STL vector class.
Definition: stl.hh:37
gem5::compression::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:93
gem5::statistics::nonan
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:69
gem5::compression::Base::compExtraLatency
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition: base.hh:114
gem5::compression::Base::BaseStats::BaseStats
BaseStats(Base &compressor)
Definition: base.cc:233
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:67
gem5::compression::Base::BaseStats::decompressions
statistics::Scalar decompressions
Number of decompressions performed.
Definition: base.hh:155
gem5::CompressionBlk::getDecompressionLatency
Cycles getDecompressionLatency() const
Get number of cycles needed to decompress this block.
Definition: super_blk.cc:129
gem5::CacheBlk
A Basic Cache block.
Definition: cache_blk.hh:70
gem5::compression::Base::setCache
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition: base.cc:107
gem5::compression::Base::fromChunks
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::Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:78
gem5::compression::Base::decompExtraLatency
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition: base.hh:126
gem5::compression::Base::CompressionData::getSize
std::size_t getSize() const
Get compression size (in bytes).
Definition: base.cc:79
gem5::compression::Base::cache
BaseCache * cache
Pointer to the parent cache.
Definition: base.hh:129
gem5::compression::Base::toChunks
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
gem5::VegaISA::p
Bitfield< 54 > p
Definition: pagetable.hh:70
gem5::CompressionBlk::print
std::string print() const override
Pretty-print sector offset and other CacheBlk information.
Definition: super_blk.cc:164
gem5::BaseCache
A basic cache interface.
Definition: base.hh:94
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:210
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::compression::Base::CompressionData::~CompressionData
virtual ~CompressionData()
Virtual destructor.
Definition: base.cc:62
gem5::compression::Base
Base cache compressor interface.
Definition: base.hh:64
gem5::compression::Base::setSizeBits
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition: base.cc:224
gem5::compression::Base::CompressionData::getSizeBits
std::size_t getSizeBits() const
Get compression size (in bits).
Definition: base.cc:73
gem5::bits
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:76
gem5::SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:146
gem5::compression::Base::Base
Base(const Params &p)
Definition: base.cc:84
gem5::CompressionBlk::isCompressed
bool isCompressed() const
Check if this block holds compressed data.
Definition: super_blk.cc:75
gem5::compression::Base::BaseStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: base.cc:253
gem5::compression::Base::stats
gem5::compression::Base::BaseStats stats
gem5::compression::Base::BaseStats::compressions
statistics::Scalar compressions
Number of compressions performed.
Definition: base.hh:140
gem5::statistics::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:68
gem5::compression::Base::BaseStats::compressionSizeBits
statistics::Scalar compressionSizeBits
Total compressed data size, in number of bits.
Definition: base.hh:149
super_blk.hh
Copyright (c) 2018 Inria All rights reserved.
gem5::CompressionBlk
A superblock is composed of sub-blocks, and each sub-block has information regarding its superblock a...
Definition: super_blk.hh:51
gem5::compression::Base::compress
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.
logging.hh
gem5::statistics::Group
Statistics container.
Definition: group.hh:92
gem5::compression::Base::compChunksPerCycle
const Cycles compChunksPerCycle
Degree of parallelization of the compression process.
Definition: base.hh:108
trace.hh
gem5::compression::Base::setDecompressionLatency
static void setDecompressionLatency(CacheBlk *blk, const Cycles lat)
Set the decompression latency of compressed block.
Definition: base.cc:214
fatal_if
#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
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: gpu_translation_state.hh:37
gem5::statistics::total
const FlagsType total
Print the total.
Definition: info.hh:59
gem5::compression::Base::sizeThreshold
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
gem5::compression::Base::CompressionData::CompressionData
CompressionData()
Default constructor.
Definition: base.cc:57
gem5::compression::Base::CompressionData::setSizeBits
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:67

Generated on Sun Jul 30 2023 01:56:52 for gem5 by doxygen 1.8.17