mem-cache: Add an extra decomp lat to multi compressor
[gem5.git] / src / mem / cache / compressors / multi.hh
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
29 /** @file
30 * Definition of the a multi compressor that choses the best compression
31 * among multiple compressors.
32 */
33
34 #ifndef __MEM_CACHE_COMPRESSORS_MULTI_HH__
35 #define __MEM_CACHE_COMPRESSORS_MULTI_HH__
36
37 #include <cstdint>
38 #include <vector>
39
40 #include "base/statistics.hh"
41 #include "base/types.hh"
42 #include "mem/cache/compressors/base.hh"
43
44 struct MultiCompressorParams;
45
46 namespace Compressor {
47
48 class Multi : public Base
49 {
50 protected:
51 /**
52 * Compression data for the multi compressor. It contains the compression
53 * data of the best compressor, along with its index in the list of
54 * sub-compressors.
55 */
56 class MultiCompData;
57
58 /** List of sub-compressors. */
59 std::vector<Base*> compressors;
60
61 /**
62 * An encoding is associated to each sub-compressor to inform which
63 * sub-compressor to use when decompressing data. This information can
64 * be added either to the tag entry, in which case no extra bits are
65 * added to the compressed data (numEncodingBits = 0), or to the
66 * compressed data itself.
67 *
68 * There is no encoding reserved for the uncompressed case; it is assumed
69 * that an "is compressed" bit is stored in the tags. Therefore, even if
70 * storing the encoding within the compressed data, these extra bits are
71 * not added when the data is uncompressible.
72 *
73 * These extra bits are taken into account when thresholding the
74 * compressed data's size.
75 */
76 const std::size_t numEncodingBits;
77
78 /**
79 * Extra decompression latency to be added to the sub-compressor's
80 * decompression latency. This can different from zero due to decoding,
81 * shifting, or packaging, for example.
82 */
83 const Cycles extraDecompressionLatency;
84
85 struct MultiStats : public Stats::Group
86 {
87 const Multi& compressor;
88
89 MultiStats(BaseStats &base_group, Multi& _compressor);
90
91 void regStats() override;
92
93 /**
94 * Number of times each compressor provided the nth best compression.
95 */
96 Stats::Vector2d ranks;
97 } multiStats;
98
99 public:
100 typedef MultiCompressorParams Params;
101 Multi(const Params *p);
102 ~Multi();
103
104 std::unique_ptr<Base::CompressionData> compress(
105 const std::vector<Base::Chunk>& chunks,
106 Cycles& comp_lat, Cycles& decomp_lat) override;
107
108 void decompress(const CompressionData* comp_data, uint64_t* data) override;
109 };
110
111 class Multi::MultiCompData : public CompressionData
112 {
113 private:
114 /** Index of the compressor that provided these compression results. */
115 const uint8_t index;
116
117 public:
118 /** Compression data of the best compressor. */
119 std::unique_ptr<Base::CompressionData> compData;
120
121 /**
122 * Default constructor.
123 *
124 * @param index Index of the compressor that provided this compression.
125 * @param comp_data Compression data of the best compressor.
126 */
127 MultiCompData(unsigned index,
128 std::unique_ptr<Base::CompressionData> comp_data);
129
130 /** Default destructor. */
131 ~MultiCompData() = default;
132
133 /** Get the index of the best compressor. */
134 uint8_t getIndex() const;
135 };
136
137 } // namespace Compressor
138
139 #endif //__MEM_CACHE_COMPRESSORS_MULTI_HH__