second_chance_rp.hh revision 12685:dcf85db6ec5c
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/**
32 * @file
33 * Declaration of a Second-Chance replacement policy.
34 * The victim is chosen using the timestamp. The oldest entry is chosen
35 * to be evicted, if it hasn't been touched since its insertion. If it
36 * has been touched, it is given a second chance and re-inserted at the
37 * end of the queue.
38 */
39
40#ifndef __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__
41#define __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__
42
43#include "mem/cache/replacement_policies/fifo_rp.hh"
44#include "params/SecondChanceRP.hh"
45
46class SecondChanceRP : public FIFORP
47{
48  protected:
49    /** Second-Chance-specific implementation of replacement data. */
50    struct SecondChanceReplData : public FIFOReplData
51    {
52        /**
53         * This is different from isTouched because isTouched accounts only
54         * for insertion, while this bit is reset every new re-insertion.
55         * @sa SecondChanceRP.
56         */
57        bool hasSecondChance;
58
59        /**
60         * Default constructor.
61         */
62        SecondChanceReplData() : FIFOReplData(), hasSecondChance(false) {}
63    };
64
65    /**
66     * Use replacement data's second chance.
67     *
68     * @param replacement_data Entry that will use its second chance.
69     */
70    void useSecondChance(
71        const std::shared_ptr<SecondChanceReplData>& replacement_data) const;
72
73  public:
74    /** Convenience typedef. */
75    typedef SecondChanceRPParams Params;
76
77    /**
78     * Construct and initiliaze this replacement policy.
79     */
80    SecondChanceRP(const Params *p);
81
82    /**
83     * Destructor.
84     */
85    ~SecondChanceRP() {}
86
87    /**
88     * Invalidate replacement data to set it as the next probable victim.
89     * Invalid entries do not have a second chance, and their last touch tick
90     * is set as the oldest possible.
91     *
92     * @param replacement_data Replacement data to be invalidated.
93     */
94    void invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
95                                                              const override;
96
97    /**
98     * Touch an entry to update its re-insertion tick and second chance bit.
99     *
100     * @param replacement_data Replacement data to be touched.
101     */
102    void touch(const std::shared_ptr<ReplacementData>& replacement_data) const
103                                                                     override;
104
105    /**
106     * Reset replacement data. Used when an entry is inserted or re-inserted
107     * in the queue.
108     * Sets its insertion tick and second chance bit.
109     *
110     * @param replacement_data Replacement data to be reset.
111     */
112    void reset(const std::shared_ptr<ReplacementData>& replacement_data) const
113                                                                     override;
114
115    /**
116     * Find replacement victim using insertion timestamps and second chance
117     * bit.
118     *
119     * @param cands Replacement candidates, selected by indexing policy.
120     * @return Replacement entry to be replaced.
121     */
122    ReplaceableEntry* getVictim(const ReplacementCandidates& candidates) const
123                                                                     override;
124
125    /**
126     * Instantiate a replacement data entry.
127     *
128     * @return A shared pointer to the new replacement data.
129     */
130    std::shared_ptr<ReplacementData> instantiateEntry() override;
131};
132
133#endif // __MEM_CACHE_REPLACEMENT_POLICIES_SECOND_CHANCE_RP_HH__
134