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/brrip_rp.hh"
36 #include "base/logging.hh" // For fatal_if
37 #include "base/random.hh"
38 #include "params/BRRIPRP.hh"
40 BRRIPRP::BRRIPRP(const Params
*p
)
41 : BaseReplacementPolicy(p
),
42 numRRPVBits(p
->num_bits
), hitPriority(p
->hit_priority
), btp(p
->btp
)
44 fatal_if(numRRPVBits
<= 0, "There should be at least one bit per RRPV.\n");
48 BRRIPRP::invalidate(const std::shared_ptr
<ReplacementData
>& replacement_data
)
51 std::shared_ptr
<BRRIPReplData
> casted_replacement_data
=
52 std::static_pointer_cast
<BRRIPReplData
>(replacement_data
);
55 casted_replacement_data
->valid
= false;
59 BRRIPRP::touch(const std::shared_ptr
<ReplacementData
>& replacement_data
) const
61 std::shared_ptr
<BRRIPReplData
> casted_replacement_data
=
62 std::static_pointer_cast
<BRRIPReplData
>(replacement_data
);
64 // Update RRPV if not 0 yet
65 // Every hit in HP mode makes the entry the last to be evicted, while
66 // in FP mode a hit makes the entry less likely to be evicted
68 casted_replacement_data
->rrpv
.reset();
70 casted_replacement_data
->rrpv
--;
75 BRRIPRP::reset(const std::shared_ptr
<ReplacementData
>& replacement_data
) const
77 std::shared_ptr
<BRRIPReplData
> casted_replacement_data
=
78 std::static_pointer_cast
<BRRIPReplData
>(replacement_data
);
81 // Replacement data is inserted as "long re-reference" if lower than btp,
82 // "distant re-reference" otherwise
83 casted_replacement_data
->rrpv
.saturate();
84 if (random_mt
.random
<unsigned>(1, 100) <= btp
) {
85 casted_replacement_data
->rrpv
--;
88 // Mark entry as ready to be used
89 casted_replacement_data
->valid
= true;
93 BRRIPRP::getVictim(const ReplacementCandidates
& candidates
) const
95 // There must be at least one replacement candidate
96 assert(candidates
.size() > 0);
98 // Use first candidate as dummy victim
99 ReplaceableEntry
* victim
= candidates
[0];
101 // Store victim->rrpv in a variable to improve code readability
102 int victim_RRPV
= std::static_pointer_cast
<BRRIPReplData
>(
103 victim
->replacementData
)->rrpv
;
105 // Visit all candidates to find victim
106 for (const auto& candidate
: candidates
) {
107 std::shared_ptr
<BRRIPReplData
> candidate_repl_data
=
108 std::static_pointer_cast
<BRRIPReplData
>(
109 candidate
->replacementData
);
111 // Stop searching for victims if an invalid entry is found
112 if (!candidate_repl_data
->valid
) {
116 // Update victim entry if necessary
117 int candidate_RRPV
= candidate_repl_data
->rrpv
;
118 if (candidate_RRPV
> victim_RRPV
) {
120 victim_RRPV
= candidate_RRPV
;
124 // Get difference of victim's RRPV to the highest possible RRPV in
125 // order to update the RRPV of all the other entries accordingly
126 int diff
= std::static_pointer_cast
<BRRIPReplData
>(
127 victim
->replacementData
)->rrpv
.saturate();
129 // No need to update RRPV if there is no difference
131 // Update RRPV of all candidates
132 for (const auto& candidate
: candidates
) {
133 std::static_pointer_cast
<BRRIPReplData
>(
134 candidate
->replacementData
)->rrpv
+= diff
;
141 std::shared_ptr
<ReplacementData
>
142 BRRIPRP::instantiateEntry()
144 return std::shared_ptr
<ReplacementData
>(new BRRIPReplData(numRRPVBits
));
148 BRRIPRPParams::create()
150 return new BRRIPRP(this);