gem5  v20.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/trace.hh"
42 #include "debug/CacheComp.hh"
44 #include "params/BaseCacheCompressor.hh"
45 
46 namespace Compressor {
47 
48 // Uncomment this line if debugging compression
49 //#define DEBUG_COMPRESSION
50 
52  : _size(0)
53 {
54 }
55 
57 {
58 }
59 
60 void
62 {
63  _size = size;
64 }
65 
66 std::size_t
68 {
69  return _size;
70 }
71 
72 std::size_t
74 {
75  return std::ceil(_size/8);
76 }
77 
79  : SimObject(p), blkSize(p->block_size), chunkSizeBits(p->chunk_size_bits),
80  sizeThreshold((blkSize * p->size_threshold_percentage) / 100),
81  stats(*this)
82 {
84  "64 must be a multiple of the chunk granularity.");
85 
86  fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
87 }
88 
90 Base::toChunks(const uint64_t* data) const
91 {
92  // Number of chunks in a 64-bit value
93  const unsigned num_chunks_per_64 =
94  (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
95 
96  // Turn a 64-bit array into a chunkSizeBits-array
97  std::vector<Chunk> chunks((blkSize * CHAR_BIT) / chunkSizeBits, 0);
98  for (int i = 0; i < chunks.size(); i++) {
99  const int index_64 = std::floor(i / (double)num_chunks_per_64);
100  const unsigned start = i % num_chunks_per_64;
101  chunks[i] = bits(data[index_64],
102  (start + 1) * chunkSizeBits - 1, start * chunkSizeBits);
103  }
104 
105  return chunks;
106 }
107 
108 void
109 Base::fromChunks(const std::vector<Chunk>& chunks, uint64_t* data) const
110 {
111  // Number of chunks in a 64-bit value
112  const unsigned num_chunks_per_64 =
113  (sizeof(uint64_t) * CHAR_BIT) / chunkSizeBits;
114 
115  // Turn a chunkSizeBits-array into a 64-bit array
116  std::memset(data, 0, blkSize);
117  for (int i = 0; i < chunks.size(); i++) {
118  const int index_64 = std::floor(i / (double)num_chunks_per_64);
119  const unsigned start = i % num_chunks_per_64;
120  replaceBits(data[index_64], (start + 1) * chunkSizeBits - 1,
121  start * chunkSizeBits, chunks[i]);
122  }
123 }
124 
125 std::unique_ptr<Base::CompressionData>
126 Base::compress(const uint64_t* data, Cycles& comp_lat, Cycles& decomp_lat)
127 {
128  // Apply compression
129  std::unique_ptr<CompressionData> comp_data =
130  compress(toChunks(data), comp_lat, decomp_lat);
131 
132  // If we are in debug mode apply decompression just after the compression.
133  // If the results do not match, we've got an error
134  #ifdef DEBUG_COMPRESSION
135  uint64_t decomp_data[blkSize/8];
136 
137  // Apply decompression
138  decompress(comp_data.get(), decomp_data);
139 
140  // Check if decompressed line matches original cache line
141  fatal_if(std::memcmp(data, decomp_data, blkSize),
142  "Decompressed line does not match original line.");
143  #endif
144 
145  // Get compression size. If compressed size is greater than the size
146  // threshold, the compression is seen as unsuccessful
147  std::size_t comp_size_bits = comp_data->getSizeBits();
148  if (comp_size_bits > sizeThreshold * CHAR_BIT) {
149  comp_size_bits = blkSize * CHAR_BIT;
150  comp_data->setSizeBits(comp_size_bits);
152  }
153 
154  // Update stats
156  stats.compressionSizeBits += comp_size_bits;
157  if (comp_size_bits != 0) {
158  stats.compressionSize[1 + std::ceil(std::log2(comp_size_bits))]++;
159  } else {
160  stats.compressionSize[0]++;
161  }
162 
163  // Print debug information
164  DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
165  "Compression latency: %llu, decompression latency: %llu\n",
166  blkSize*8, comp_size_bits, comp_lat, decomp_lat);
167 
168  return comp_data;
169 }
170 
171 Cycles
173 {
174  const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
175 
176  // If block is compressed, return its decompression latency
177  if (comp_blk && comp_blk->isCompressed()){
178  const Cycles decomp_lat = comp_blk->getDecompressionLatency();
179  DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
180  comp_blk->print(), decomp_lat);
181  stats.decompressions += 1;
182  return decomp_lat;
183  }
184 
185  // Block is not compressed, so there is no decompression latency
186  return Cycles(0);
187 }
188 
189 void
191 {
192  // Sanity check
193  assert(blk != nullptr);
194 
195  // Assign latency
196  static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
197 }
198 
199 void
200 Base::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
201 {
202  // Sanity check
203  assert(blk != nullptr);
204 
205  // Assign size
206  static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
207 }
208 
210  : Stats::Group(&_compressor), compressor(_compressor),
211  compressions(this, "compressions",
212  "Total number of compressions"),
213  failedCompressions(this, "failed_compressions",
214  "Total number of failed compressions"),
215  compressionSize(this, "compression_size",
216  "Number of blocks that were compressed to this power of two size"),
217  compressionSizeBits(this, "compression_size_bits",
218  "Total compressed data size, in bits"),
219  avgCompressionSizeBits(this, "avg_compression_size_bits",
220  "Average compression size, in bits"),
221  decompressions(this, "total_decompressions",
222  "Total number of decompressions")
223 {
224 }
225 
226 void
228 {
230 
231  // Values comprised are {0, 1, 2, 4, ..., blkSize}
232  compressionSize.init(std::log2(compressor.blkSize*8) + 2);
233  compressionSize.subname(0, "0");
234  compressionSize.subdesc(0,
235  "Number of blocks that compressed to fit in 0 bits");
236  for (unsigned i = 0; i <= std::log2(compressor.blkSize*8); ++i) {
237  std::string str_i = std::to_string(1 << i);
238  compressionSize.subname(1+i, str_i);
239  compressionSize.subdesc(1+i,
240  "Number of blocks that compressed to fit in " + str_i + " bits");
241  }
242 
243  avgCompressionSizeBits.flags(Stats::total | Stats::nozero | Stats::nonan);
244  avgCompressionSizeBits = compressionSizeBits / compressions;
245 }
246 
247 } // namespace Compressor
Compressor::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:96
Stats::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:64
replaceBits
void replaceBits(T &val, int first, int last, B bit_val)
A convenience function to replace bits first to last of val with bit_val in place.
Definition: bitfield.hh:179
Compressor::Base::CompressionData::CompressionData
CompressionData()
Default constructor.
Definition: base.cc:51
data
const char data[]
Definition: circlebuf.test.cc:42
Compressor::Base::chunkSizeBits
const unsigned chunkSizeBits
Chunk size, in number of bits.
Definition: base.hh:90
Compressor::Base::BaseStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: base.cc:227
ArmISA::i
Bitfield< 7 > i
Definition: miscregs_types.hh:63
Compressor::Base::BaseStats::failedCompressions
Stats::Scalar failedCompressions
Number of failed compressions.
Definition: base.hh:110
base.hh
Compressor::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:87
Compressor
Definition: base.cc:46
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
Compressor::Base::CompressionData::~CompressionData
virtual ~CompressionData()
Virtual destructor.
Definition: base.cc:56
Compressor::Base::CompressionData::getSizeBits
std::size_t getSizeBits() const
Get compression size (in bits).
Definition: base.cc:67
std::vector
STL vector class.
Definition: stl.hh:37
Compressor::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:109
Compressor::Base
Base cache compressor interface.
Definition: base.hh:58
Compressor::Base::BaseStats::BaseStats
BaseStats(Base &compressor)
Definition: base.cc:209
Compressor::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.
Compressor::Base::BaseStats::compressionSize
Stats::Vector compressionSize
Number of blocks that were compressed to this power of two size.
Definition: base.hh:113
Compressor::Base::setDecompressionLatency
static void setDecompressionLatency(CacheBlk *blk, const Cycles lat)
Set the decompression latency of compressed block.
Definition: base.cc:190
Compressor::Base::decompress
virtual void decompress(const CompressionData *comp_data, uint64_t *cache_line)=0
Apply the decompression process to the compressed data.
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
Compressor::Base::getDecompressionLatency
Cycles getDecompressionLatency(const CacheBlk *blk)
Get the decompression latency if the block is compressed.
Definition: base.cc:172
Compressor::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:168
CompressionBlk::getDecompressionLatency
Cycles getDecompressionLatency() const
Get number of cycles needed to decompress this block.
Definition: super_blk.cc:75
CompressionBlk::isCompressed
bool isCompressed() const
Check if this block holds compressed data.
Definition: super_blk.cc:45
Stats::nozero
const FlagsType nozero
Don't print if this is zero.
Definition: info.hh:57
super_blk.hh
Copyright (c) 2018 Inria All rights reserved.
Compressor::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:90
Compressor::Base::BaseStats::compressions
Stats::Scalar compressions
Number of compressions performed.
Definition: base.hh:107
Compressor::Base::BaseStats::compressionSizeBits
Stats::Scalar compressionSizeBits
Total compressed data size, in number of bits.
Definition: base.hh:116
CacheBlk
A Basic Cache block.
Definition: cache_blk.hh:84
Compressor::Base::CompressionData::getSize
std::size_t getSize() const
Get compression size (in bytes).
Definition: base.cc:73
Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:83
Stats
Definition: statistics.cc:61
Compressor::Base::stats
Compressor::Base::BaseStats stats
Compressor::Base::BaseStats::decompressions
Stats::Scalar decompressions
Number of decompressions performed.
Definition: base.hh:122
trace.hh
Compressor::Base::CompressionData::setSizeBits
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:61
MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:323
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:219
Stats::total
const FlagsType total
Print the total.
Definition: info.hh:49
Compressor::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:200
CompressionBlk
A superblock is composed of sub-blocks, and each sub-block has information regarding its superblock a...
Definition: super_blk.hh:48
Stats::nonan
const FlagsType nonan
Don't print if this is NAN.
Definition: info.hh:59
Compressor::Base::Base
Base(const Params *p)
Definition: base.cc:78
CompressionBlk::print
std::string print() const override
Pretty-print sector offset and other CacheBlk information.
Definition: super_blk.cc:87
bits
T bits(T val, int first, int last)
Extract the bitfield from position 'first' to 'last' (inclusive) from 'val' and right justify it.
Definition: bitfield.hh:75
SimObject
Abstract superclass for simulation objects.
Definition: sim_object.hh:92

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17