gem5  v22.1.0.0
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 GEM5_DEPRECATED_NAMESPACE(Compressor, compression);
52 namespace compression
53 {
54 
55 // Uncomment this line if debugging compression
56 //#define DEBUG_COMPRESSION
57 
59  : _size(0)
60 {
61 }
62 
64 {
65 }
66 
67 void
69 {
70  _size = size;
71 }
72 
73 std::size_t
75 {
76  return _size;
77 }
78 
79 std::size_t
81 {
82  return std::ceil(_size/8);
83 }
84 
86  : SimObject(p), blkSize(p.block_size), chunkSizeBits(p.chunk_size_bits),
87  sizeThreshold((blkSize * p.size_threshold_percentage) / 100),
88  compChunksPerCycle(p.comp_chunks_per_cycle),
89  compExtraLatency(p.comp_extra_latency),
90  decompChunksPerCycle(p.decomp_chunks_per_cycle),
91  decompExtraLatency(p.decomp_extra_latency),
92  cache(nullptr), stats(*this)
93 {
95  "64 must be a multiple of the chunk granularity.");
96 
98  "Compressor processes more chunks per cycle than the number of "
99  "chunks in the input");
101  "Decompressor processes more chunks per cycle than the number of "
102  "chunks in the input");
103 
104  fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
105 }
106 
107 void
109 {
110  assert(!cache);
111  cache = _cache;
112 }
113 
115 Base::toChunks(const uint64_t* data) const
116 {
117  // Number of chunks in a 64-bit value
118  const unsigned num_chunks_per_64 =
119  (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
120 
121  // Turn a 64-bit array into a chunkSizeBits-array
122  std::vector<Chunk> chunks((blkSize * CHAR_BIT) / chunkSizeBits, 0);
123  for (int i = 0; i < chunks.size(); i++) {
124  const int index_64 = std::floor(i / (double)num_chunks_per_64);
125  const unsigned start = i % num_chunks_per_64;
126  chunks[i] = bits(data[index_64],
127  (start + 1) * chunkSizeBits - 1, start * chunkSizeBits);
128  }
129 
130  return chunks;
131 }
132 
133 void
134 Base::fromChunks(const std::vector<Chunk>& chunks, uint64_t* data) const
135 {
136  // Number of chunks in a 64-bit value
137  const unsigned num_chunks_per_64 =
138  (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
139 
140  // Turn a chunkSizeBits-array into a 64-bit array
141  std::memset(data, 0, blkSize);
142  for (int i = 0; i < chunks.size(); i++) {
143  const int index_64 = std::floor(i / (double)num_chunks_per_64);
144  const unsigned start = i % num_chunks_per_64;
145  replaceBits(data[index_64], (start + 1) * chunkSizeBits - 1,
146  start * chunkSizeBits, chunks[i]);
147  }
148 }
149 
150 std::unique_ptr<Base::CompressionData>
151 Base::compress(const uint64_t* data, Cycles& comp_lat, Cycles& decomp_lat)
152 {
153  // Apply compression
154  std::unique_ptr<CompressionData> comp_data =
155  compress(toChunks(data), comp_lat, decomp_lat);
156 
157  // If we are in debug mode apply decompression just after the compression.
158  // If the results do not match, we've got an error
159  #ifdef DEBUG_COMPRESSION
160  uint64_t decomp_data[blkSize/8];
161 
162  // Apply decompression
163  decompress(comp_data.get(), decomp_data);
164 
165  // Check if decompressed line matches original cache line
166  fatal_if(std::memcmp(data, decomp_data, blkSize),
167  "Decompressed line does not match original line.");
168  #endif
169 
170  // Get compression size. If compressed size is greater than the size
171  // threshold, the compression is seen as unsuccessful
172  std::size_t comp_size_bits = comp_data->getSizeBits();
173  if (comp_size_bits > sizeThreshold * CHAR_BIT) {
174  comp_size_bits = blkSize * CHAR_BIT;
175  comp_data->setSizeBits(comp_size_bits);
177  }
178 
179  // Update stats
181  stats.compressionSizeBits += comp_size_bits;
182  if (comp_size_bits != 0) {
183  stats.compressionSize[1 + std::ceil(std::log2(comp_size_bits))]++;
184  } else {
185  stats.compressionSize[0]++;
186  }
187 
188  // Print debug information
189  DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
190  "Compression latency: %llu, decompression latency: %llu\n",
191  blkSize*8, comp_size_bits, comp_lat, decomp_lat);
192 
193  return comp_data;
194 }
195 
196 Cycles
198 {
199  const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
200 
201  // If block is compressed, return its decompression latency
202  if (comp_blk && comp_blk->isCompressed()){
203  const Cycles decomp_lat = comp_blk->getDecompressionLatency();
204  DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
205  comp_blk->print(), decomp_lat);
206  stats.decompressions += 1;
207  return decomp_lat;
208  }
209 
210  // Block is not compressed, so there is no decompression latency
211  return Cycles(0);
212 }
213 
214 void
216 {
217  // Sanity check
218  assert(blk != nullptr);
219 
220  // Assign latency
221  static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
222 }
223 
224 void
225 Base::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
226 {
227  // Sanity check
228  assert(blk != nullptr);
229 
230  // Assign size
231  static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
232 }
233 
235  : statistics::Group(&_compressor), compressor(_compressor),
236  ADD_STAT(compressions, statistics::units::Count::get(),
237  "Total number of compressions"),
238  ADD_STAT(failedCompressions, statistics::units::Count::get(),
239  "Total number of failed compressions"),
240  ADD_STAT(compressionSize, statistics::units::Count::get(),
241  "Number of blocks that were compressed to this power of two "
242  "size"),
243  ADD_STAT(compressionSizeBits, statistics::units::Bit::get(),
244  "Total compressed data size"),
245  ADD_STAT(avgCompressionSizeBits, statistics::units::Rate<
246  statistics::units::Bit, statistics::units::Count>::get(),
247  "Average compression size"),
248  ADD_STAT(decompressions, statistics::units::Count::get(),
249  "Total number of decompressions")
250 {
251 }
252 
253 void
255 {
257 
258  // Values comprised are {0, 1, 2, 4, ..., blkSize}
259  compressionSize.init(std::log2(compressor.blkSize*8) + 2);
260  compressionSize.subname(0, "0");
261  compressionSize.subdesc(0,
262  "Number of blocks that compressed to fit in 0 bits");
263  for (unsigned i = 0; i <= std::log2(compressor.blkSize*8); ++i) {
264  std::string str_i = std::to_string(1 << i);
265  compressionSize.subname(1+i, str_i);
266  compressionSize.subdesc(1+i,
267  "Number of blocks that compressed to fit in " + str_i + " bits");
268  }
269 
270  avgCompressionSizeBits.flags(statistics::total | statistics::nozero |
272  avgCompressionSizeBits = compressionSizeBits / compressions;
273 }
274 
275 } // namespace compression
276 } // namespace gem5
#define DPRINTF(x,...)
Definition: trace.hh:186
const char data[]
A basic cache interface.
Definition: base.hh:96
A Basic Cache block.
Definition: cache_blk.hh:71
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.
Definition: sim_object.hh:148
virtual ~CompressionData()
Virtual destructor.
Definition: base.cc:63
CompressionData()
Default constructor.
Definition: base.cc:58
std::size_t getSize() const
Get compression size (in bytes).
Definition: base.cc:80
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:68
std::size_t getSizeBits() const
Get compression size (in bits).
Definition: base.cc:74
Base cache compressor interface.
Definition: base.hh:66
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition: base.hh:115
BaseCacheCompressorParams Params
Definition: base.hh:202
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:134
gem5::compression::Base::BaseStats stats
Base(const Params &p)
Definition: base.cc:85
BaseCache * cache
Pointer to the parent cache.
Definition: base.hh:130
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:115
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition: base.hh:127
const Cycles compChunksPerCycle
Degree of parallelization of the compression process.
Definition: base.hh:109
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.
Cycles getDecompressionLatency(const CacheBlk *blk)
Get the decompression latency if the block is compressed.
Definition: base.cc:197
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition: base.cc:225
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:94
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition: base.cc:108
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:97
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:103
static void setDecompressionLatency(CacheBlk *blk, const Cycles lat)
Set the decompression latency of compressed block.
Definition: base.cc:215
const Cycles decompChunksPerCycle
Degree of parallelization of the decompression process.
Definition: base.hh:121
Statistics container.
Definition: group.hh:94
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:76
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:197
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:226
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
Declares a basic cache interface BaseCache.
Definition of a basic cache compressor.
Bitfield< 7 > i
Definition: misc_types.hh:67
Bitfield< 54 > p
Definition: pagetable.hh:70
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:70
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:68
const FlagsType total
Print the total.
Definition: info.hh:60
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60
statistics::Scalar compressions
Number of compressions performed.
Definition: base.hh:141
void regStats() override
Callback to set stat parameters.
Definition: base.cc:254
BaseStats(Base &compressor)
Definition: base.cc:234
statistics::Scalar decompressions
Number of decompressions performed.
Definition: base.hh:156
statistics::Vector compressionSize
Number of blocks that were compressed to this power of two size.
Definition: base.hh:147
statistics::Scalar failedCompressions
Number of failed compressions.
Definition: base.hh:144
statistics::Scalar compressionSizeBits
Total compressed data size, in number of bits.
Definition: base.hh:150
Copyright (c) 2018 Inria All rights reserved.

Generated on Wed Dec 21 2022 10:22:30 for gem5 by doxygen 1.9.1