second_chance_rp.cc 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#include "mem/cache/replacement_policies/second_chance_rp.hh"
32
33SecondChanceRP::SecondChanceRP(const Params *p)
34    : FIFORP(p)
35{
36}
37
38void
39SecondChanceRP::useSecondChance(
40    const std::shared_ptr<SecondChanceReplData>& replacement_data) const
41{
42    // Reset FIFO data
43    FIFORP::reset(replacement_data);
44
45    // Use second chance
46    replacement_data->hasSecondChance = false;
47}
48
49void
50SecondChanceRP::invalidate(
51    const std::shared_ptr<ReplacementData>& replacement_data) const
52{
53    FIFORP::invalidate(replacement_data);
54
55    // Do not give a second chance to invalid entries
56    std::static_pointer_cast<SecondChanceReplData>(
57        replacement_data)->hasSecondChance = false;
58}
59
60void
61SecondChanceRP::touch(const std::shared_ptr<ReplacementData>& replacement_data) const
62{
63    FIFORP::touch(replacement_data);
64
65    // Whenever an entry is touched, it is given a second chance
66    std::static_pointer_cast<SecondChanceReplData>(
67        replacement_data)->hasSecondChance = true;
68}
69
70void
71SecondChanceRP::reset(const std::shared_ptr<ReplacementData>& replacement_data) const
72{
73    FIFORP::reset(replacement_data);
74
75    // Entries are inserted with a second chance
76    std::static_pointer_cast<SecondChanceReplData>(
77        replacement_data)->hasSecondChance = true;
78}
79
80ReplaceableEntry*
81SecondChanceRP::getVictim(const ReplacementCandidates& candidates) const
82{
83    // There must be at least one replacement candidate
84    assert(candidates.size() > 0);
85
86    // Search for invalid entries, as they have the eviction priority
87    for (const auto& candidate : candidates) {
88        // Cast candidate's replacement data
89        std::shared_ptr<SecondChanceReplData> candidate_replacement_data =
90            std::static_pointer_cast<SecondChanceReplData>(
91                candidate->replacementData);
92
93        // Stop iteration if found an invalid entry
94        if ((candidate_replacement_data->tickInserted == Tick(0)) &&
95            !candidate_replacement_data->hasSecondChance) {
96            return candidate;
97        }
98    }
99
100    // Visit all candidates to find victim
101    ReplaceableEntry* victim = candidates[0];
102    bool search_victim = true;
103    while (search_victim) {
104        // Do a FIFO victim search
105        victim = FIFORP::getVictim(candidates);
106
107        // Cast victim's replacement data for code readability
108        std::shared_ptr<SecondChanceReplData> victim_replacement_data =
109            std::static_pointer_cast<SecondChanceReplData>(
110                victim->replacementData);
111
112        // If victim has a second chance, use it and repeat search
113        if (victim_replacement_data->hasSecondChance) {
114            useSecondChance(victim_replacement_data);
115        } else {
116            // Found victim
117            search_victim = false;
118        }
119    }
120
121    return victim;
122}
123
124std::shared_ptr<ReplacementData>
125SecondChanceRP::instantiateEntry()
126{
127    return std::shared_ptr<ReplacementData>(new SecondChanceReplData());
128}
129
130SecondChanceRP*
131SecondChanceRPParams::create()
132{
133    return new SecondChanceRP(this);
134}
135