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