gem5  v19.0.0.0
base.cc
Go to the documentation of this file.
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 
36 
37 #include <algorithm>
38 #include <cmath>
39 #include <cstdint>
40 #include <string>
41 
42 #include "debug/CacheComp.hh"
44 #include "params/BaseCacheCompressor.hh"
45 
46 // Uncomment this line if debugging compression
47 //#define DEBUG_COMPRESSION
48 
50  : _size(0)
51 {
52 }
53 
55 {
56 }
57 
58 void
60 {
61  _size = size;
62 }
63 
64 std::size_t
66 {
67  return _size;
68 }
69 
70 std::size_t
72 {
73  return std::ceil(_size/8);
74 }
75 
77  : SimObject(p), blkSize(p->block_size), sizeThreshold(p->size_threshold),
78  stats(*this)
79 {
80  fatal_if(blkSize < sizeThreshold, "Compressed data must fit in a block");
81 }
82 
83 void
84 BaseCacheCompressor::compress(const uint64_t* data, Cycles& comp_lat,
85  Cycles& decomp_lat, std::size_t& comp_size_bits)
86 {
87  // Apply compression
88  std::unique_ptr<CompressionData> comp_data =
89  compress(data, comp_lat, decomp_lat);
90 
91  // If we are in debug mode apply decompression just after the compression.
92  // If the results do not match, we've got an error
93  #ifdef DEBUG_COMPRESSION
94  uint64_t decomp_data[blkSize/8];
95 
96  // Apply decompression
97  decompress(comp_data.get(), decomp_data);
98 
99  // Check if decompressed line matches original cache line
100  fatal_if(std::memcmp(data, decomp_data, blkSize),
101  "Decompressed line does not match original line.");
102  #endif
103 
104  // Get compression size. If compressed size is greater than the size
105  // threshold, the compression is seen as unsuccessful
106  comp_size_bits = comp_data->getSizeBits();
107  if (comp_size_bits >= sizeThreshold * 8) {
108  comp_size_bits = blkSize * 8;
109  }
110 
111  // Update stats
113  stats.compressionSizeBits += comp_size_bits;
114  stats.compressionSize[std::ceil(std::log2(comp_size_bits))]++;
115 
116  // Print debug information
117  DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \
118  "Compression latency: %llu, decompression latency: %llu\n",
119  blkSize*8, comp_size_bits, comp_lat, decomp_lat);
120 }
121 
122 Cycles
124 {
125  const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk);
126 
127  // If block is compressed, return its decompression latency
128  if (comp_blk && comp_blk->isCompressed()){
129  const Cycles decomp_lat = comp_blk->getDecompressionLatency();
130  DPRINTF(CacheComp, "Decompressing block: %s (%d cycles)\n",
131  comp_blk->print(), decomp_lat);
132  stats.decompressions += 1;
133  return decomp_lat;
134  }
135 
136  // Block is not compressed, so there is no decompression latency
137  return Cycles(0);
138 }
139 
140 void
142 {
143  // Sanity check
144  assert(blk != nullptr);
145 
146  // Assign latency
147  static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat);
148 }
149 
150 void
151 BaseCacheCompressor::setSizeBits(CacheBlk* blk, const std::size_t size_bits)
152 {
153  // Sanity check
154  assert(blk != nullptr);
155 
156  // Assign size
157  static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits);
158 }
159 
161  BaseCacheCompressor& _compressor)
162  : Stats::Group(&_compressor), compressor(_compressor),
163  compressions(this, "compressions",
164  "Total number of compressions"),
165  compressionSize(this, "compression_size",
166  "Number of blocks that were compressed to this power of two size"),
167  compressionSizeBits(this, "compression_size_bits",
168  "Total compressed data size, in bits"),
169  avgCompressionSizeBits(this, "avg_compression_size_bits",
170  "Average compression size, in bits"),
171  decompressions(this, "total_decompressions",
172  "Total number of decompressions")
173 {
174 }
175 
176 void
178 {
180 
181  compressionSize.init(std::log2(compressor.blkSize*8) + 1);
182  for (unsigned i = 0; i <= std::log2(compressor.blkSize*8); ++i) {
183  std::string str_i = std::to_string(1 << i);
184  compressionSize.subname(i, str_i);
186  "Number of blocks that compressed to fit in " + str_i + " bits");
187  }
188 
191 }
192 
virtual std::unique_ptr< CompressionData > compress(const uint64_t *cache_line, Cycles &comp_lat, Cycles &decomp_lat)=0
Apply the compression process to the cache line.
#define DPRINTF(x,...)
Definition: trace.hh:229
CompressionData()
Default constructor.
Definition: base.cc:49
BaseCacheCompressor(const Params *p)
Default constructor.
Definition: base.cc:76
Derived & subname(off_type index, const std::string &name)
Set the subfield name for the given index, and marks this stat to print at the end of simulation...
Definition: statistics.hh:379
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:83
Bitfield< 7 > i
Stats::Scalar decompressions
Number of decompressions performed.
Definition: base.hh:101
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:78
const FlagsType nonan
Don&#39;t print if this is NAN.
Definition: info.hh:61
Cycles getDecompressionLatency() const
Get number of cycles needed to decompress this block.
Definition: super_blk.cc:77
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:66
Derived & subdesc(off_type index, const std::string &desc)
Set the subfield description for the given index and marks this stat to print at the end of simulatio...
Definition: statistics.hh:403
Definition of a basic cache compressor.
Derived & flags(Flags _flags)
Set the flags and marks this stat to print at the end of simulation.
Definition: statistics.hh:336
Stats::Scalar compressionSizeBits
Total compressed data size, in number of bits.
Definition: base.hh:95
std::size_t getSizeBits() const
Get compression size (in bits).
Definition: base.cc:65
Derived & init(size_type size)
Set this vector to have the given size.
Definition: statistics.hh:1152
static void setSizeBits(CacheBlk *blk, const std::size_t size_bits)
Set the size of the compressed block, in bits.
Definition: base.cc:151
A Basic Cache block.
Definition: cache_blk.hh:87
Copyright (c) 2018 Inria All rights reserved.
const BaseCacheCompressor & compressor
Definition: base.hh:82
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:67
Group()=delete
A superblock is composed of sub-blocks, and each sub-block has information regarding its superblock a...
Definition: super_blk.hh:50
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition: logging.hh:203
BaseCacheCompressor::BaseCacheCompressorStats stats
Base cache compressor interface.
Definition: base.hh:54
const FlagsType total
Print the total.
Definition: info.hh:51
Stats::Scalar compressions
Number of compressions performed.
Definition: base.hh:89
Stats::Formula avgCompressionSizeBits
Average data size after compression, in number of bits.
Definition: base.hh:98
bool isCompressed() const
Check if this block holds compressed data.
Definition: super_blk.cc:47
std::size_t _size
Compressed cache line size (in bits).
Definition: base.hh:184
BaseCacheCompressorParams Params
Convenience typedef.
Definition: base.hh:130
BaseCacheCompressorStats(BaseCacheCompressor &compressor)
Definition: base.cc:160
Cycles getDecompressionLatency(const CacheBlk *blk)
Get the decompression latency if the block is compressed.
Definition: base.cc:123
std::string print() const override
Pretty-print sector offset and other CacheBlk information.
Definition: super_blk.cc:89
void regStats() override
Callback to set stat parameters.
Definition: base.cc:177
Stats::Vector compressionSize
Number of blocks that were compressed to this power of two size.
Definition: base.hh:92
std::size_t getSize() const
Get compression size (in bytes).
Definition: base.cc:71
static void setDecompressionLatency(CacheBlk *blk, const Cycles lat)
Set the decompression latency of compressed block.
Definition: base.cc:141
void setSizeBits(std::size_t size)
Set compression size (in bits).
Definition: base.cc:59
const FlagsType nozero
Don&#39;t print if this is zero.
Definition: info.hh:59
Bitfield< 0 > p
const char data[]
Abstract superclass for simulation objects.
Definition: sim_object.hh:96
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:60
virtual ~CompressionData()
Virtual destructor.
Definition: base.cc:54
virtual void decompress(const CompressionData *comp_data, uint64_t *cache_line)=0
Apply the decompression process to the compressed data.

Generated on Fri Feb 28 2020 16:26:59 for gem5 by doxygen 1.8.13