second_chance_rp.cc (12685:dcf85db6ec5c) second_chance_rp.cc (12727:56c23b54bcb1)
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
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
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}
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}