2 * Copyright (c) 2018 Inria
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.
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.
28 * Authors: Daniel Carvalho
31 #include "mem/cache/replacement_policies/second_chance_rp.hh"
35 #include "params/SecondChanceRP.hh"
37 SecondChanceRP::SecondChanceRP(const Params
*p
)
43 SecondChanceRP::useSecondChance(
44 const std::shared_ptr
<SecondChanceReplData
>& replacement_data
) const
47 FIFORP::reset(replacement_data
);
50 replacement_data
->hasSecondChance
= false;
54 SecondChanceRP::invalidate(
55 const std::shared_ptr
<ReplacementData
>& replacement_data
) const
57 FIFORP::invalidate(replacement_data
);
59 // Do not give a second chance to invalid entries
60 std::static_pointer_cast
<SecondChanceReplData
>(
61 replacement_data
)->hasSecondChance
= false;
65 SecondChanceRP::touch(const std::shared_ptr
<ReplacementData
>& replacement_data
) const
67 FIFORP::touch(replacement_data
);
69 // Whenever an entry is touched, it is given a second chance
70 std::static_pointer_cast
<SecondChanceReplData
>(
71 replacement_data
)->hasSecondChance
= true;
75 SecondChanceRP::reset(const std::shared_ptr
<ReplacementData
>& replacement_data
) const
77 FIFORP::reset(replacement_data
);
79 // Entries are inserted with a second chance
80 std::static_pointer_cast
<SecondChanceReplData
>(
81 replacement_data
)->hasSecondChance
= true;
85 SecondChanceRP::getVictim(const ReplacementCandidates
& candidates
) const
87 // There must be at least one replacement candidate
88 assert(candidates
.size() > 0);
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
);
97 // Stop iteration if found an invalid entry
98 if ((candidate_replacement_data
->tickInserted
== Tick(0)) &&
99 !candidate_replacement_data
->hasSecondChance
) {
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
);
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
);
116 // If victim has a second chance, use it and repeat search
117 if (victim_replacement_data
->hasSecondChance
) {
118 useSecondChance(victim_replacement_data
);
121 search_victim
= false;
128 std::shared_ptr
<ReplacementData
>
129 SecondChanceRP::instantiateEntry()
131 return std::shared_ptr
<ReplacementData
>(new SecondChanceReplData());
135 SecondChanceRPParams::create()
137 return new SecondChanceRP(this);