base.hh revision 13218:5e7df60c6cab
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#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
32#define __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
33
34#include <memory>
35
36#include "params/BaseReplacementPolicy.hh"
37#include "sim/sim_object.hh"
38
39/**
40 * The replacement data needed by the replacement policy.
41 * Each replacement policy should have its own replacement data.
42 */
43struct ReplacementData {};
44
45/**
46 * A replaceable entry is used by any table-like structure that needs to
47 * implement replacement functionality. It provides the replacement data
48 * pointer instantiated and needed by the replacement policy used.
49 * @sa Replacement Policies
50 */
51class ReplaceableEntry
52{
53  private:
54    /**
55     * Set to which this entry belongs.
56     */
57    uint32_t _set;
58
59    /**
60     * Way (relative position within the set) to which this entry belongs.
61     */
62    uint32_t _way;
63
64   public:
65     /**
66      * Replacement data associated to this entry.
67      * It is instantiated by the replacement policy.
68      */
69     std::shared_ptr<ReplacementData> replacementData;
70
71    /**
72     * Set both the set and way. Should be called only once.
73     *
74     * @param set The set of this entry.
75     * @param way The way of this entry.
76     */
77    void setPosition(const uint32_t set, const uint32_t way) {
78        _set = set;
79        _way = way;
80    }
81
82    /**
83     * Get set number.
84     *
85     * @return The set to which this entry belongs.
86     */
87    uint32_t getSet() const { return _set; }
88
89    /**
90     * Get way number.
91     *
92     * @return The way to which this entry belongs.
93     */
94    uint32_t getWay() const { return _way; }
95};
96
97/**
98 * Replacement candidates as chosen by the indexing policy.
99 */
100typedef std::vector<ReplaceableEntry*> ReplacementCandidates;
101
102/**
103 * A common base class of cache replacement policy objects.
104 */
105class BaseReplacementPolicy : public SimObject
106{
107  public:
108    /**
109      * Convenience typedef.
110      */
111    typedef BaseReplacementPolicyParams Params;
112
113    /**
114     * Construct and initiliaze this replacement policy.
115     */
116    BaseReplacementPolicy(const Params *p) : SimObject(p) {}
117
118    /**
119     * Destructor.
120     */
121    virtual ~BaseReplacementPolicy() {}
122
123    /**
124     * Invalidate replacement data to set it as the next probable victim.
125     *
126     * @param replacement_data Replacement data to be invalidated.
127     */
128    virtual void invalidate(const std::shared_ptr<ReplacementData>&
129                                                replacement_data) const = 0;
130
131    /**
132     * Update replacement data.
133     *
134     * @param replacement_data Replacement data to be touched.
135     */
136    virtual void touch(const std::shared_ptr<ReplacementData>&
137                                                replacement_data) const = 0;
138
139    /**
140     * Reset replacement data. Used when it's holder is inserted/validated.
141     *
142     * @param replacement_data Replacement data to be reset.
143     */
144    virtual void reset(const std::shared_ptr<ReplacementData>&
145                                                replacement_data) const = 0;
146
147    /**
148     * Find replacement victim among candidates.
149     *
150     * @param candidates Replacement candidates, selected by indexing policy.
151     * @return Replacement entry to be replaced.
152     */
153    virtual ReplaceableEntry* getVictim(
154                           const ReplacementCandidates& candidates) const = 0;
155
156    /**
157     * Instantiate a replacement data entry.
158     *
159     * @return A shared pointer to the new replacement data.
160     */
161    virtual std::shared_ptr<ReplacementData> instantiateEntry() = 0;
162};
163
164#endif // __MEM_CACHE_REPLACEMENT_POLICIES_BASE_HH__
165