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