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 31/** @file 32 * Definition of a basic cache compressor. 33 */ 34 35#include "mem/cache/compressors/base.hh" 36 37#include <algorithm> 38#include <cstdint>
| 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 31/** @file 32 * Definition of a basic cache compressor. 33 */ 34 35#include "mem/cache/compressors/base.hh" 36 37#include <algorithm> 38#include <cstdint>
|
| 39#include <string>
|
39 40#include "debug/CacheComp.hh" 41#include "mem/cache/tags/super_blk.hh" 42#include "params/BaseCacheCompressor.hh" 43 44// Uncomment this line if debugging compression 45//#define DEBUG_COMPRESSION 46 47BaseCacheCompressor::CompressionData::CompressionData() 48 : _size(0) 49{ 50} 51 52BaseCacheCompressor::CompressionData::~CompressionData() 53{ 54} 55 56void 57BaseCacheCompressor::CompressionData::setSizeBits(std::size_t size) 58{ 59 _size = size; 60} 61 62std::size_t 63BaseCacheCompressor::CompressionData::getSizeBits() const 64{ 65 return _size; 66} 67 68std::size_t 69BaseCacheCompressor::CompressionData::getSize() const 70{ 71 return std::ceil(_size/8); 72} 73 74BaseCacheCompressor::BaseCacheCompressor(const Params *p) 75 : SimObject(p), blkSize(p->block_size) 76{ 77} 78 79void 80BaseCacheCompressor::compress(const uint64_t* data, Cycles& comp_lat, 81 Cycles& decomp_lat, std::size_t& comp_size_bits) 82{ 83 // Apply compression 84 std::unique_ptr<CompressionData> comp_data = 85 compress(data, comp_lat, decomp_lat); 86 87 // If we are in debug mode apply decompression just after the compression. 88 // If the results do not match, we've got an error 89 #ifdef DEBUG_COMPRESSION 90 uint64_t decomp_data[blkSize/8]; 91 92 // Apply decompression 93 decompress(comp_data.get(), decomp_data); 94 95 // Check if decompressed line matches original cache line 96 fatal_if(std::memcmp(data, decomp_data, blkSize), 97 "Decompressed line does not match original line."); 98 #endif 99 100 // Get compression size 101 comp_size_bits = comp_data->getSizeBits(); 102
| 40 41#include "debug/CacheComp.hh" 42#include "mem/cache/tags/super_blk.hh" 43#include "params/BaseCacheCompressor.hh" 44 45// Uncomment this line if debugging compression 46//#define DEBUG_COMPRESSION 47 48BaseCacheCompressor::CompressionData::CompressionData() 49 : _size(0) 50{ 51} 52 53BaseCacheCompressor::CompressionData::~CompressionData() 54{ 55} 56 57void 58BaseCacheCompressor::CompressionData::setSizeBits(std::size_t size) 59{ 60 _size = size; 61} 62 63std::size_t 64BaseCacheCompressor::CompressionData::getSizeBits() const 65{ 66 return _size; 67} 68 69std::size_t 70BaseCacheCompressor::CompressionData::getSize() const 71{ 72 return std::ceil(_size/8); 73} 74 75BaseCacheCompressor::BaseCacheCompressor(const Params *p) 76 : SimObject(p), blkSize(p->block_size) 77{ 78} 79 80void 81BaseCacheCompressor::compress(const uint64_t* data, Cycles& comp_lat, 82 Cycles& decomp_lat, std::size_t& comp_size_bits) 83{ 84 // Apply compression 85 std::unique_ptr<CompressionData> comp_data = 86 compress(data, comp_lat, decomp_lat); 87 88 // If we are in debug mode apply decompression just after the compression. 89 // If the results do not match, we've got an error 90 #ifdef DEBUG_COMPRESSION 91 uint64_t decomp_data[blkSize/8]; 92 93 // Apply decompression 94 decompress(comp_data.get(), decomp_data); 95 96 // Check if decompressed line matches original cache line 97 fatal_if(std::memcmp(data, decomp_data, blkSize), 98 "Decompressed line does not match original line."); 99 #endif 100 101 // Get compression size 102 comp_size_bits = comp_data->getSizeBits(); 103
|
| 104 // Update stats 105 compressionSize[std::ceil(std::log2(comp_size_bits))]++; 106
|
103 // Print debug information 104 DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \ 105 "Compression latency: %llu, decompression latency: %llu\n", 106 blkSize*8, comp_size_bits, comp_lat, decomp_lat); 107} 108 109Cycles 110BaseCacheCompressor::getDecompressionLatency(const CacheBlk* blk) 111{ 112 const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk); 113 114 // If block is compressed, return its decompression latency 115 if (comp_blk && comp_blk->isCompressed()){ 116 return comp_blk->getDecompressionLatency(); 117 } 118 119 // Block is not compressed, so there is no decompression latency 120 return Cycles(0); 121} 122 123void 124BaseCacheCompressor::setDecompressionLatency(CacheBlk* blk, const Cycles lat) 125{ 126 // Sanity check 127 assert(blk != nullptr); 128 129 // Assign latency 130 static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat); 131} 132 133void 134BaseCacheCompressor::setSizeBits(CacheBlk* blk, const std::size_t size_bits) 135{ 136 // Sanity check 137 assert(blk != nullptr); 138 139 // Assign size 140 static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits); 141} 142
| 107 // Print debug information 108 DPRINTF(CacheComp, "Compressed cache line from %d to %d bits. " \ 109 "Compression latency: %llu, decompression latency: %llu\n", 110 blkSize*8, comp_size_bits, comp_lat, decomp_lat); 111} 112 113Cycles 114BaseCacheCompressor::getDecompressionLatency(const CacheBlk* blk) 115{ 116 const CompressionBlk* comp_blk = static_cast<const CompressionBlk*>(blk); 117 118 // If block is compressed, return its decompression latency 119 if (comp_blk && comp_blk->isCompressed()){ 120 return comp_blk->getDecompressionLatency(); 121 } 122 123 // Block is not compressed, so there is no decompression latency 124 return Cycles(0); 125} 126 127void 128BaseCacheCompressor::setDecompressionLatency(CacheBlk* blk, const Cycles lat) 129{ 130 // Sanity check 131 assert(blk != nullptr); 132 133 // Assign latency 134 static_cast<CompressionBlk*>(blk)->setDecompressionLatency(lat); 135} 136 137void 138BaseCacheCompressor::setSizeBits(CacheBlk* blk, const std::size_t size_bits) 139{ 140 // Sanity check 141 assert(blk != nullptr); 142 143 // Assign size 144 static_cast<CompressionBlk*>(blk)->setSizeBits(size_bits); 145} 146
|
| 147void 148BaseCacheCompressor::regStats() 149{ 150 SimObject::regStats(); 151 152 // We also store when compression is bigger than original block size 153 compressionSize 154 .init(std::log2(blkSize*8) + 2) 155 .name(name() + ".compression_size") 156 .desc("Number of blocks that were compressed to this power of" \ 157 "two size.") 158 ; 159 160 for (unsigned i = 0; i <= std::log2(blkSize*8) + 1; ++i) { 161 compressionSize.subname(i, std::to_string(1 << i)); 162 compressionSize.subdesc(i, "Number of blocks that compressed to fit " \ 163 "in " + std::to_string(1 << i) + " bits"); 164 } 165} 166
|
| |