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 * A cache compressor must consist of a compression and a decompression
34 * methods. It must also be aware of the size of an uncompressed cache
35 * line.
36 */
37
38#ifndef __MEM_CACHE_COMPRESSORS_BASE_HH__
39#define __MEM_CACHE_COMPRESSORS_BASE_HH__
40
41#include <cstdint>
42
43#include "base/statistics.hh"
44#include "base/types.hh"
45#include "sim/sim_object.hh"
46
47class CacheBlk;
48struct BaseCacheCompressorParams;
49
50/**
51 * Base cache compressor interface. Every cache compressor must implement a
52 * compression and a decompression method.
53 */
54class BaseCacheCompressor : public SimObject {
55  protected:
56    /**
57     * Forward declaration of compression data. Every new compressor must
58     * create a new compression data based on it.
59     */
60    class CompressionData;
61
62    /**
63     * Uncompressed cache line size (in bytes).
64     */
65    const std::size_t blkSize;
66
67    /**
68     * @defgroup CompressionStats Compression specific statistics.
69     * @{
70     */
71
72    /** Number of blocks that were compressed to this power of two size. */
73    Stats::Vector compressionSize;
74
75    /**
76     * @}
77     */
78
79    /**
80     * Apply the compression process to the cache line.
81     * Returns the number of cycles used by the compressor, however it is
82     * usually covered by a good pipelined execution, and is currently ignored.
83     * The decompression latency is also returned, in order to avoid
84     * increasing simulation time and memory consumption.
85     *
86     * @param cache_line The cache line to be compressed.
87     * @param comp_lat Compression latency in number of cycles.
88     * @param decomp_lat Decompression latency in number of cycles.
89     * @return Cache line after compression.
90     */
91    virtual std::unique_ptr<CompressionData> compress(
92        const uint64_t* cache_line, Cycles& comp_lat, Cycles& decomp_lat) = 0;
93
94    /**
95     * Apply the decompression process to the compressed data.
96     *
97     * @param comp_data Compressed cache line.
98     * @param cache_line The cache line to be decompressed.
99     */
100    virtual void decompress(const CompressionData* comp_data,
101                              uint64_t* cache_line) = 0;
102
103  public:
104    /** Convenience typedef. */
105     typedef BaseCacheCompressorParams Params;
106
107    /**
108     * Default constructor.
109     */
110    BaseCacheCompressor(const Params *p);
111
112    /**
113     * Default destructor.
114     */
115    virtual ~BaseCacheCompressor() {};
116
117    /**
118     * Apply the compression process to the cache line. Ignores compression
119     * cycles.
120     *
121     * @param data The cache line to be compressed.
122     * @param comp_lat Compression latency in number of cycles.
123     * @param decomp_lat Decompression latency in number of cycles.
124     * @param comp_size_bits Compressed data size (in bits).
125     */
126    void compress(const uint64_t* data, Cycles& comp_lat,
127                  Cycles& decomp_lat, std::size_t& comp_size_bits);
128
129    /**
130     * Get the decompression latency if the block is compressed. Latency is 0
131     * otherwise.
132     *
133     * @param blk The compressed block.
134     */
135    Cycles getDecompressionLatency(const CacheBlk* blk) const;
136
137    /**
138     * Set the decompression latency of compressed block.
139     *
140     * @param blk The compressed block.
141     * @param lat The decompression latency.
142     */
143    static void setDecompressionLatency(CacheBlk* blk, const Cycles lat);
144
145    /**
146     * Set the size of the compressed block, in bits.
147     *
148     * @param blk The compressed block.
149     * @param size_bits The block size.
150     */
151    static void setSizeBits(CacheBlk* blk, const std::size_t size_bits);
152
153    /**
154     * Register local statistics.
155     */
156    void regStats() override;
157};
158
159class BaseCacheCompressor::CompressionData {
160  private:
161    /**
162     * Compressed cache line size (in bits).
163     */
164    std::size_t _size;
165
166  public:
167    /**
168     * Default constructor.
169     */
170    CompressionData();
171
172    /**
173     * Virtual destructor. Without it unique_ptr will cause mem leak.
174     */
175    virtual ~CompressionData();
176
177    /**
178     * Set compression size (in bits).
179     *
180     * @param size Compressed data size.
181     */
182    void setSizeBits(std::size_t size);
183
184    /**
185     * Get compression size (in bits).
186     *
187     * @return Compressed data size.
188     */
189    std::size_t getSizeBits() const;
190
191    /**
192     * Get compression size (in bytes).
193     *
194     * @return Compressed data size.
195     */
196    std::size_t getSize() const;
197};
198
199#endif //__MEM_CACHE_COMPRESSORS_BASE_HH__
200