gem5  v21.1.0.2
multi.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-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 
35 
36 #include <cmath>
37 #include <queue>
38 
39 #include "base/bitfield.hh"
40 #include "base/logging.hh"
41 #include "base/trace.hh"
42 #include "debug/CacheComp.hh"
43 #include "params/MultiCompressor.hh"
44 
45 namespace gem5
46 {
47 
48 GEM5_DEPRECATED_NAMESPACE(Compressor, compression);
49 namespace compression
50 {
51 
53  std::unique_ptr<Base::CompressionData> comp_data)
54  : CompressionData(), index(index), compData(std::move(comp_data))
55 {
56  setSizeBits(compData->getSizeBits());
57 }
58 
59 uint8_t
61 {
62  return index;
63 }
64 
67  numEncodingBits(p.encoding_in_tags ? 0 :
68  std::log2(alignToPowerOfTwo(compressors.size()))),
69  multiStats(stats, *this)
70 {
71  fatal_if(compressors.size() == 0, "There must be at least one compressor");
72 }
73 
75 {
76  for (auto& compressor : compressors) {
77  delete compressor;
78  }
79 }
80 
81 void
83 {
84  Base::setCache(_cache);
85  for (auto& compressor : compressors) {
86  compressor->setCache(_cache);
87  }
88 }
89 
90 std::unique_ptr<Base::CompressionData>
91 Multi::compress(const std::vector<Chunk>& chunks, Cycles& comp_lat,
92  Cycles& decomp_lat)
93 {
94  struct Results
95  {
96  unsigned index;
97  std::unique_ptr<Base::CompressionData> compData;
98  Cycles decompLat;
99  uint8_t compressionFactor;
100 
101  Results(unsigned index,
102  std::unique_ptr<Base::CompressionData> comp_data,
103  Cycles decomp_lat, std::size_t blk_size)
104  : index(index), compData(std::move(comp_data)),
105  decompLat(decomp_lat)
106  {
107  const std::size_t size = compData->getSize();
108  // If the compressed size is worse than the uncompressed size,
109  // we assume the size is the uncompressed size, and thus the
110  // compression factor is 1.
111  //
112  // Some compressors (notably the zero compressor) may rely on
113  // extra information being stored in the tags, or added in
114  // another compression layer. Their size can be 0, so it is
115  // assigned the highest possible compression factor (the original
116  // block's size).
117  compressionFactor = (size > blk_size) ? 1 :
118  ((size == 0) ? blk_size :
119  alignToPowerOfTwo(std::floor(blk_size / (double) size)));
120  }
121  };
122  struct ResultsComparator
123  {
124  bool
125  operator()(const std::shared_ptr<Results>& lhs,
126  const std::shared_ptr<Results>& rhs) const
127  {
128  const std::size_t lhs_cf = lhs->compressionFactor;
129  const std::size_t rhs_cf = rhs->compressionFactor;
130 
131  if (lhs_cf == rhs_cf) {
132  // When they have similar compressed sizes, give the one
133  // with fastest decompression privilege
134  return lhs->decompLat > rhs->decompLat;
135  }
136  return lhs_cf < rhs_cf;
137  }
138  };
139 
140  // Each sub-compressor can have its own chunk size; therefore, revert
141  // the chunks to raw data, so that they handle the conversion internally
142  uint64_t data[blkSize / sizeof(uint64_t)];
143  std::memset(data, 0, blkSize);
144  fromChunks(chunks, data);
145 
146  // Find the ranking of the compressor outputs
147  std::priority_queue<std::shared_ptr<Results>,
148  std::vector<std::shared_ptr<Results>>, ResultsComparator> results;
149  Cycles max_comp_lat;
150  for (unsigned i = 0; i < compressors.size(); i++) {
151  Cycles temp_decomp_lat;
152  auto temp_comp_data =
153  compressors[i]->compress(data, comp_lat, temp_decomp_lat);
154  temp_comp_data->setSizeBits(temp_comp_data->getSizeBits() +
156  results.push(std::make_shared<Results>(i, std::move(temp_comp_data),
157  temp_decomp_lat, blkSize));
158  max_comp_lat = std::max(max_comp_lat, comp_lat);
159  }
160 
161  // Assign best compressor to compression data
162  const unsigned best_index = results.top()->index;
163  std::unique_ptr<CompressionData> multi_comp_data =
164  std::unique_ptr<MultiCompData>(
165  new MultiCompData(best_index, std::move(results.top()->compData)));
166  DPRINTF(CacheComp, "Best compressor: %d\n", best_index);
167 
168  // Set decompression latency of the best compressor
169  decomp_lat = results.top()->decompLat + decompExtraLatency;
170 
171  // Update compressor ranking stats
172  for (int rank = 0; rank < compressors.size(); rank++) {
173  multiStats.ranks[results.top()->index][rank]++;
174  results.pop();
175  }
176 
177  // Set compression latency (compression latency of the slowest compressor
178  // and 1 cycle to pack)
179  comp_lat = Cycles(max_comp_lat + compExtraLatency);
180 
181  return multi_comp_data;
182 }
183 
184 void
186  uint64_t* cache_line)
187 {
188  const MultiCompData* casted_comp_data =
189  static_cast<const MultiCompData*>(comp_data);
190  compressors[casted_comp_data->getIndex()]->decompress(
191  casted_comp_data->compData.get(), cache_line);
192 }
193 
195  : statistics::Group(&base_group), compressor(_compressor),
196  ADD_STAT(ranks, statistics::units::Count::get(),
197  "Number of times each compressor had the nth best compression")
198 {
199 }
200 
201 void
203 {
205 
206  const std::size_t num_compressors = compressor.compressors.size();
207  ranks.init(num_compressors, num_compressors);
208  for (unsigned compressor = 0; compressor < num_compressors; compressor++) {
209  ranks.subname(compressor, std::to_string(compressor));
210  ranks.subdesc(compressor, "Number of times compressor " +
211  std::to_string(compressor) + " had the nth best compression.");
212  for (unsigned rank = 0; rank < num_compressors; rank++) {
213  ranks.ysubname(rank, std::to_string(rank));
214  }
215  }
216 }
217 
218 } // namespace compression
219 } // namespace gem5
gem5::compression::Base::CompressionData
Definition: base.hh:246
data
const char data[]
Definition: circlebuf.test.cc:48
gem5::compression::Multi::compressors
std::vector< Base * > compressors
List of sub-compressors.
Definition: multi.hh:61
gem5::MipsISA::index
Bitfield< 30, 0 > index
Definition: pra_constants.hh:47
gem5::compression::Multi::MultiCompData::compData
std::unique_ptr< Base::CompressionData > compData
Compression data of the best compressor.
Definition: multi.hh:126
sc_dt::to_string
const std::string to_string(sc_enc enc)
Definition: sc_fxdefs.cc:91
gem5::compression::Base::Params
BaseCacheCompressorParams Params
Definition: base.hh:202
std::vector< Chunk >
gem5::compression::Base::blkSize
const std::size_t blkSize
Uncompressed cache line size (in bytes).
Definition: base.hh:94
gem5::compression::Base::compExtraLatency
const Cycles compExtraLatency
Extra latency added to compression due to packaging, shifting or other operations.
Definition: base.hh:115
gem5::ArmISA::i
Bitfield< 7 > i
Definition: misc_types.hh:66
gem5::compression::Multi::numEncodingBits
const std::size_t numEncodingBits
An encoding is associated to each sub-compressor to inform which sub-compressor to use when decompres...
Definition: multi.hh:81
multi.hh
gem5::compression::Multi::decompress
void decompress(const CompressionData *comp_data, uint64_t *data) override
Apply the decompression process to the compressed data.
Definition: multi.cc:185
gem5::compression::Base::setCache
virtual void setCache(BaseCache *_cache)
The cache can only be set once.
Definition: base.cc:108
gem5::compression::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:134
gem5::Cycles
Cycles is a wrapper class for representing cycle counts, i.e.
Definition: types.hh:78
gem5::compression::Multi::MultiCompData::MultiCompData
MultiCompData(unsigned index, std::unique_ptr< Base::CompressionData > comp_data)
Default constructor.
Definition: multi.cc:52
gem5::compression::Base::decompExtraLatency
const Cycles decompExtraLatency
Extra latency added to decompression due to packaging, shifting or other operations.
Definition: base.hh:127
bitfield.hh
gem5::BaseCache
A basic cache interface.
Definition: base.hh:95
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:186
ADD_STAT
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
Definition: group.hh:75
gem5::MipsISA::p
Bitfield< 0 > p
Definition: pra_constants.hh:326
gem5::compression::Base
Base cache compressor interface.
Definition: base.hh:65
gem5::compression::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:225
gem5::compression::Multi::MultiStats::ranks
statistics::Vector2d ranks
Number of times each compressor provided the nth best compression.
Definition: multi.hh:101
gem5::compression::Base::stats
gem5::compression::Base::BaseStats stats
gem5::GEM5_DEPRECATED_NAMESPACE
GEM5_DEPRECATED_NAMESPACE(GuestABI, guest_abi)
gem5::statistics::Group::regStats
virtual void regStats()
Callback to set stat parameters.
Definition: group.cc:69
gem5::compression::Multi::MultiStats::regStats
void regStats() override
Callback to set stat parameters.
Definition: multi.cc:202
gem5::compression::Multi::setCache
void setCache(BaseCache *_cache) override
The cache can only be set once.
Definition: multi.cc:82
gem5::compression::Base::Multi
friend class Multi
This compressor must be able to access the protected functions of its sub-compressors.
Definition: base.hh:89
gem5::compression::Multi::MultiStats::MultiStats
MultiStats(BaseStats &base_group, Multi &_compressor)
Definition: multi.cc:194
std
Overload hash function for BasicBlockRange type.
Definition: types.hh:111
gem5::compression::Multi::multiStats
gem5::compression::Multi::MultiStats multiStats
gem5::compression::Multi::compress
std::unique_ptr< Base::CompressionData > compress(const std::vector< Base::Chunk > &chunks, Cycles &comp_lat, Cycles &decomp_lat) override
Apply the compression process to the cache line.
Definition: multi.cc:91
logging.hh
gem5::statistics::Group
Statistics container.
Definition: group.hh:93
gem5::compression::Multi::~Multi
~Multi()
Definition: multi.cc:74
gem5::compression::Multi::MultiCompData
Definition: multi.hh:118
trace.hh
gem5::compression::Multi::MultiCompData::getIndex
uint8_t getIndex() const
Get the index of the best compressor.
Definition: multi.cc:60
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:225
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40
gem5::alignToPowerOfTwo
constexpr uint64_t alignToPowerOfTwo(uint64_t val)
Align to the next highest power of two.
Definition: bitfield.hh:372
gem5::compression::Multi
Definition: multi.hh:53
gem5::compression::Base::BaseStats
Definition: base.hh:132

Generated on Tue Sep 21 2021 12:25:29 for gem5 by doxygen 1.8.17