44#include "debug/StackDist.hh"
51 verifyStack(verify_stack)
61 tree.push_back(tree_level);
70 tree.push_back(tree_level);
79 for (
auto& layer :
tree) {
80 for (
auto& index_node : layer) {
82 delete index_node.second;
102 uint64_t sum_from_below, uint64_t
level,
103 uint64_t stack_dist,
bool discard_node)
109 uint64_t node_sum_l = node->
sumLeft;
110 uint64_t node_sum_r = node->
sumRight;
126 "Error in sum left of level %ul, node index %ull, "
127 "Sum = %ull \n",
level, node_n_index, node_sum_l);
130 "Error in sum right of level %ul, node index %ull, "
131 "Sum = %ull \n",
level, node_n_index, node_sum_r);
139 node_sum_l = sum_from_below;
140 stack_dist += node_sum_r;
143 node_sum_r = sum_from_below;
147 if (discard_node && !sum_from_below) {
149 node_discard_left =
true;
151 node_discard_right =
true;
163 if (node_discard_left && node_discard_right &&
164 discard_node && node_parent_ptr && !sum_from_below) {
171 discard_node =
false;
176 if (node_parent_ptr) {
177 stack_dist =
updateSum(node_parent_ptr, node_left,
178 node_sum_l + node_sum_r,
179 level, stack_dist, discard_node);
193 uint64_t stack_dist = 0;
206 level, stack_dist,
true);
216 uint64_t stack_dist, uint64_t
level)
const
284 tree.push_back(treeLevel);
329 if ((
index % (1 <<
i)) == 0) {
365 auto ai =
aiMap.lower_bound(r_address);
379 if (ai !=
aiMap.end() && !(
aiMap.key_comp()(r_address, ai->first))) {
383 uint64_t r_index = ai->second;
389 aiMap.erase(r_address);
396 newLeafNode =
tree[0][r_index];
400 tree[0].erase(r_index);
413 if (
index % 2 == 0) {
423 newLeafNode =
new Node();
444 panic_if(verify_stack_dist != stack_dist,
445 "Expected stack-distance for address \
446 %#lx is %#lx but found %#lx",
447 r_address, verify_stack_dist, stack_dist);
456 return (std::make_pair(stack_dist, _mark));
468 auto ai =
aiMap.lower_bound(r_address);
471 uint64_t stack_dist = 0;
477 if (ai !=
aiMap.end() && !(
aiMap.key_comp()(r_address, ai->first))) {
481 uint64_t r_index = ai->second;
484 _mark =
tree[0][r_index]->isMarked;
486 tree[0][r_index]->isMarked = mark;
502 panic_if(verify_stack_dist != stack_dist,
503 "Expected stack-distance for address \
504 %#lx is %#lx but found %#lx",
505 r_address, verify_stack_dist, stack_dist);
510 return std::make_pair(stack_dist, _mark);
521 next_up = next_up->
parent;
522 panic_if(!next_up,
"Sanity check failed for node %ull \n",
539 uint64_t stack_dist = 0;
542 for (;
a !=
stack.rend(); ++
a) {
543 if (*
a == r_address) {
560 stack.push_back(r_address);
572 DPRINTF(StackDist,
"Printing last %d entries in tree\n",
n);
575 for (
auto it =
tree[0].rbegin(); (
count <
n) && (it !=
tree[0].rend());
581 for (
auto ai =
aiMap.rbegin(); ai !=
aiMap.rend(); ++ai) {
582 if (ai->second == r_index) {
583 DPRINTF(StackDist,
"Tree leaves, Rightmost-[%d] = %#lx\n",
593 DPRINTF(StackDist,
"Printing Last %d entries in VerifStack \n",
n);
597 DPRINTF(StackDist,
"Verif Stack, Top-[%d] = %#lx\n",
count, *
a);
Declaration and inline definition of ChunkGenerator object.
std::map< uint64_t, Node * > IndexNodeMap
std::vector< uint64_t > nextIndex
void updateTree()
updateTree is a tree balancing operation, which maintains the binary tree structure.
uint64_t getTreeDepth() const
Query depth of the tree (tree[0] represents leaf layer while tree[treeDepth] represents the root laye...
std::pair< uint64_t, bool > calcStackDist(const Addr r_address, bool mark=false)
Process the given address.
uint64_t getSumsLeavesToRoot(Node *node) const
Gets the sum from the leaf node specified.
static constexpr uint64_t Infinity
A convenient way of refering to infinity.
uint64_t getSum(Node *node, bool from_left, uint64_t sum_from_below, uint64_t stack_dist, uint64_t level) const
Gets sum from the node upwards recursively till the root.
uint64_t updateSumsLeavesToRoot(Node *node, bool is_new_leaf)
Updates the leaf nodes and nodes above.
std::pair< uint64_t, bool > calcStackDistAndUpdate(const Addr r_address, bool addNewNode=true)
Process the given address:
uint64_t verifyStackDist(const Addr r_address, bool update_stack=false)
This is an alternative implementation of the stack-distance in a naive way.
void sanityCheckTree(const Node *node, uint64_t level=0) const
This method is used for verification purposes It recursively traverses upwards from the given node ti...
uint64_t updateSum(Node *node, bool from_left, uint64_t sum_from_below, uint64_t level, uint64_t stack_dist, bool discard_node)
Updates the nodes upwards recursively till the root.
StackDistCalc(bool verify_stack=false)
std::vector< uint64_t > stack
void printStack(int n=5) const
Print the last n items on the stack.
uint64_t index
Internal counter for address accesses (unique and non-unique) This counter increments everytime the c...
static constexpr bool isPowerOf2(const T &n)
#define panic_if(cond,...)
Conditional panic macro that checks the supplied condition and only panics if the condition is true a...
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Node which takes form of Leaf, INode or Root.
bool isMarked
Flag to indicate if this address is marked.