2 * Copyright (c) 2018-2020 Inria
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.
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.
30 * Definition of a basic cache compressor.
33 #include "mem/cache/compressors/base.hh"
40 #include "base/trace.hh"
41 #include "debug/CacheComp.hh"
42 #include "mem/cache/tags/super_blk.hh"
43 #include "params/BaseCacheCompressor.hh"
45 namespace Compressor
{
47 // Uncomment this line if debugging compression
48 //#define DEBUG_COMPRESSION
50 Base::CompressionData::CompressionData()
55 Base::CompressionData::~CompressionData()
60 Base::CompressionData::setSizeBits(std::size_t size
)
66 Base::CompressionData::getSizeBits() const
72 Base::CompressionData::getSize() const
74 return std::ceil(_size
/8);
77 Base::Base(const Params
*p
)
78 : SimObject(p
), blkSize(p
->block_size
), sizeThreshold(p
->size_threshold
),
81 fatal_if(blkSize
< sizeThreshold
, "Compressed data must fit in a block");
85 Base::compress(const uint64_t* data
, Cycles
& comp_lat
,
86 Cycles
& decomp_lat
, std::size_t& comp_size_bits
)
89 std::unique_ptr
<CompressionData
> comp_data
=
90 compress(data
, comp_lat
, decomp_lat
);
92 // If we are in debug mode apply decompression just after the compression.
93 // If the results do not match, we've got an error
94 #ifdef DEBUG_COMPRESSION
95 uint64_t decomp_data
[blkSize
/8];
97 // Apply decompression
98 decompress(comp_data
.get(), decomp_data
);
100 // Check if decompressed line matches original cache line
101 fatal_if(std::memcmp(data
, decomp_data
, blkSize
),
102 "Decompressed line does not match original line.");
105 // Get compression size. If compressed size is greater than the size
106 // threshold, the compression is seen as unsuccessful
107 comp_size_bits
= comp_data
->getSizeBits();
108 if (comp_size_bits
>= sizeThreshold
* 8) {
109 comp_size_bits
= blkSize
* 8;
113 stats
.compressions
++;
114 stats
.compressionSizeBits
+= comp_size_bits
;
115 stats
.compressionSize
[std::ceil(std::log2(comp_size_bits
))]++;
117 // Print debug information
118 DPRINTF(CacheComp
, "Compressed cache line from %d to %d bits. " \
119 "Compression latency: %llu, decompression latency: %llu\n",
120 blkSize
*8, comp_size_bits
, comp_lat
, decomp_lat
);
124 Base::getDecompressionLatency(const CacheBlk
* blk
)
126 const CompressionBlk
* comp_blk
= static_cast<const CompressionBlk
*>(blk
);
128 // If block is compressed, return its decompression latency
129 if (comp_blk
&& comp_blk
->isCompressed()){
130 const Cycles decomp_lat
= comp_blk
->getDecompressionLatency();
131 DPRINTF(CacheComp
, "Decompressing block: %s (%d cycles)\n",
132 comp_blk
->print(), decomp_lat
);
133 stats
.decompressions
+= 1;
137 // Block is not compressed, so there is no decompression latency
142 Base::setDecompressionLatency(CacheBlk
* blk
, const Cycles lat
)
145 assert(blk
!= nullptr);
148 static_cast<CompressionBlk
*>(blk
)->setDecompressionLatency(lat
);
152 Base::setSizeBits(CacheBlk
* blk
, const std::size_t size_bits
)
155 assert(blk
!= nullptr);
158 static_cast<CompressionBlk
*>(blk
)->setSizeBits(size_bits
);
161 Base::BaseStats::BaseStats(Base
& _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")
177 Base::BaseStats::regStats()
179 Stats::Group::regStats();
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
);
185 compressionSize
.subdesc(i
,
186 "Number of blocks that compressed to fit in " + str_i
+ " bits");
189 avgCompressionSizeBits
.flags(Stats::total
| Stats::nozero
| Stats::nonan
);
190 avgCompressionSizeBits
= compressionSizeBits
/ compressions
;
193 } // namespace Compressor