gem5 v24.0.0.0
Loading...
Searching...
No Matches
tree_plru_rp.cc
Go to the documentation of this file.
1
36
37#include <cmath>
38
39#include "base/intmath.hh"
40#include "base/logging.hh"
41#include "params/TreePLRURP.hh"
42
43namespace gem5
44{
45
46namespace replacement_policy
47{
48
55static uint64_t
56parentIndex(const uint64_t index)
57{
58 return std::floor((index-1)/2);
59}
60
67static uint64_t
68leftSubtreeIndex(const uint64_t index)
69{
70 return 2*index + 1;
71}
72
79static uint64_t
80rightSubtreeIndex(const uint64_t index)
81{
82 return 2*index + 2;
83}
84
92static bool
93isRightSubtree(const uint64_t index)
94{
95 return index%2 == 0;
96}
97
99 const uint64_t index, std::shared_ptr<PLRUTree> tree)
100 : index(index), tree(tree)
101{
102}
103
105 : Base(p), numLeaves(p.num_leaves), count(0), treeInstance(nullptr)
106{
108 "numLeaves should never be 0");
109}
110
111void
112TreePLRU::invalidate(const std::shared_ptr<ReplacementData>& replacement_data)
113{
114 // Cast replacement data
115 std::shared_ptr<TreePLRUReplData> treePLRU_replacement_data =
116 std::static_pointer_cast<TreePLRUReplData>(replacement_data);
117 PLRUTree* tree = treePLRU_replacement_data->tree.get();
118
119 // Index of the tree entry we are currently checking
120 // Make this entry the new LRU entry
121 uint64_t tree_index = treePLRU_replacement_data->index;
122
123 // Parse and update tree to make it point to the new LRU
124 do {
125 // Store whether we are coming from a left or right node
126 const bool right = isRightSubtree(tree_index);
127
128 // Go to the parent tree node
129 tree_index = parentIndex(tree_index);
130
131 // Update parent node to make it point to the node we just came from
132 tree->at(tree_index) = right;
133 } while (tree_index != 0);
134}
135
136void
137TreePLRU::touch(const std::shared_ptr<ReplacementData>& replacement_data)
138const
139{
140 // Cast replacement data
141 std::shared_ptr<TreePLRUReplData> treePLRU_replacement_data =
142 std::static_pointer_cast<TreePLRUReplData>(replacement_data);
143 PLRUTree* tree = treePLRU_replacement_data->tree.get();
144
145 // Index of the tree entry we are currently checking
146 // Make this entry the MRU entry
147 uint64_t tree_index = treePLRU_replacement_data->index;
148
149 // Parse and update tree to make every bit point away from the new MRU
150 do {
151 // Store whether we are coming from a left or right node
152 const bool right = isRightSubtree(tree_index);
153
154 // Go to the parent tree node
155 tree_index = parentIndex(tree_index);
156
157 // Update node to not point to the touched leaf
158 tree->at(tree_index) = !right;
159 } while (tree_index != 0);
160}
161
162void
163TreePLRU::reset(const std::shared_ptr<ReplacementData>& replacement_data)
164const
165{
166 // A reset has the same functionality of a touch
167 touch(replacement_data);
168}
169
172{
173 // There must be at least one replacement candidate
174 assert(candidates.size() > 0);
175
176 // Get tree
177 const PLRUTree* tree = std::static_pointer_cast<TreePLRUReplData>(
178 candidates[0]->replacementData)->tree.get();
179
180 // Index of the tree entry we are currently checking. Start with root.
181 uint64_t tree_index = 0;
182
183 // Parse tree
184 while (tree_index < tree->size()) {
185 // Go to the next tree entry
186 if (tree->at(tree_index)) {
187 tree_index = rightSubtreeIndex(tree_index);
188 } else {
189 tree_index = leftSubtreeIndex(tree_index);
190 }
191 }
192
193 // The tree index is currently at the leaf of the victim displaced by the
194 // number of non-leaf nodes
195 return candidates.at(tree_index - (numLeaves - 1));
196}
197
198std::shared_ptr<ReplacementData>
200{
201 // Generate a tree instance every numLeaves created
202 if (count % numLeaves == 0) {
203 treeInstance = new PLRUTree(numLeaves - 1, false);
204 }
205
206 // Create replacement data using current tree instance
207 TreePLRUReplData* treePLRUReplData = new TreePLRUReplData(
208 (count % numLeaves) + numLeaves - 1,
209 std::shared_ptr<PLRUTree>(treeInstance));
210
211 // Update instance counter
212 count++;
213
214 return std::shared_ptr<ReplacementData>(treePLRUReplData);
215}
216
217} // namespace replacement_policy
218} // namespace gem5
A replaceable entry is a basic entry in a 2d table-like structure that needs to have replacement func...
A common base class of cache replacement policy objects.
Definition base.hh:55
void reset(const std::shared_ptr< ReplacementData > &replacement_data) const override
Reset replacement data.
const uint64_t numLeaves
Number of leaves that share a single replacement data.
ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const override
Find replacement victim using TreePLRU bits.
void invalidate(const std::shared_ptr< ReplacementData > &replacement_data) override
Invalidate replacement data to set it as the next probable victim.
uint64_t count
Count of the number of sharers of a replacement data.
void touch(const std::shared_ptr< ReplacementData > &replacement_data) const override
Touch an entry to update its replacement data.
std::vector< bool > PLRUTree
Instead of implementing the tree itself with pointers, it is implemented as an array of bits.
std::shared_ptr< ReplacementData > instantiateEntry() override
Instantiate a replacement data entry.
PLRUTree * treeInstance
Holds the latest temporary tree instance created by instantiateEntry().
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
Bitfield< 30, 0 > index
Bitfield< 0 > p
static uint64_t parentIndex(const uint64_t index)
Get the index of the parent of the given indexed subtree.
static bool isRightSubtree(const uint64_t index)
Find out if the subtree at index corresponds to the right or left subtree of its parent tree.
static uint64_t leftSubtreeIndex(const uint64_t index)
Get index of the subtree on the left of the given indexed tree.
static uint64_t rightSubtreeIndex(const uint64_t index)
Get index of the subtree on the right of the given indexed tree.
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Tree-PLRU-specific implementation of replacement data.
TreePLRUReplData(const uint64_t index, std::shared_ptr< PLRUTree > tree)
Default constructor.
Copyright (c) 2018-2020 Inria All rights reserved.

Generated on Tue Jun 18 2024 16:24:05 for gem5 by doxygen 1.11.0