45 #include "debug/StackDist.hh"
49 verifyStack(verify_stack)
59 tree.push_back(tree_level);
68 tree.push_back(tree_level);
77 for (
auto& layer :
tree) {
78 for (
auto& index_node : layer) {
80 delete index_node.second;
100 uint64_t sum_from_below, uint64_t
level,
101 uint64_t stack_dist,
bool discard_node)
107 uint64_t node_sum_l = node->
sumLeft;
108 uint64_t node_sum_r = node->
sumRight;
123 panic_if(node_sum_l > (1 << (level - 1)),
124 "Error in sum left of level %ul, node index %ull, "
125 "Sum = %ull \n", level, node_n_index, node_sum_l);
127 panic_if(node_sum_r > (1 << (level - 1)),
128 "Error in sum right of level %ul, node index %ull, "
129 "Sum = %ull \n", level, node_n_index, node_sum_r);
137 node_sum_l = sum_from_below;
138 stack_dist += node_sum_r;
141 node_sum_r = sum_from_below;
145 if (discard_node && !sum_from_below) {
147 node_discard_left =
true;
149 node_discard_right =
true;
161 if (node_discard_left && node_discard_right &&
162 discard_node && node_parent_ptr && !sum_from_below) {
169 discard_node =
false;
174 if (node_parent_ptr) {
175 stack_dist =
updateSum(node_parent_ptr, node_left,
176 node_sum_l + node_sum_r,
177 level, stack_dist, discard_node);
191 uint64_t stack_dist = 0;
204 level, stack_dist,
true);
214 uint64_t stack_dist, uint64_t
level)
const
282 tree.push_back(treeLevel);
327 if ((
index % (1 << i)) == 0) {
363 auto ai =
aiMap.lower_bound(r_address);
377 if (ai !=
aiMap.end() && !(
aiMap.key_comp()(r_address, ai->first))) {
381 uint64_t r_index = ai->second;
387 aiMap.erase(r_address);
394 newLeafNode =
tree[0][r_index];
398 tree[0].erase(r_index);
411 if (
index % 2 == 0) {
421 newLeafNode =
new Node();
442 panic_if(verify_stack_dist != stack_dist,
443 "Expected stack-distance for address \
444 %#lx is %#lx but found %#lx",
445 r_address, verify_stack_dist, stack_dist);
454 return (std::make_pair(stack_dist, _mark));
466 auto ai =
aiMap.lower_bound(r_address);
469 uint64_t stack_dist = 0;
475 if (ai !=
aiMap.end() && !(
aiMap.key_comp()(r_address, ai->first))) {
479 uint64_t r_index = ai->second;
482 _mark =
tree[0][r_index]->isMarked;
484 tree[0][r_index]->isMarked = mark;
500 panic_if(verify_stack_dist != stack_dist,
501 "Expected stack-distance for address \
502 %#lx is %#lx but found %#lx",
503 r_address, verify_stack_dist, stack_dist);
508 return std::make_pair(stack_dist, _mark);
519 next_up = next_up->
parent;
520 panic_if(!next_up,
"Sanity check failed for node %ull \n",
537 uint64_t stack_dist = 0;
540 for (;
a !=
stack.rend(); ++
a) {
541 if (*
a == r_address) {
558 stack.push_back(r_address);
570 DPRINTF(StackDist,
"Printing last %d entries in tree\n", n);
573 for (
auto it =
tree[0].rbegin(); (count <
n) && (it !=
tree[0].rend());
579 for (
auto ai =
aiMap.rbegin(); ai !=
aiMap.rend(); ++ai) {
580 if (ai->second == r_index) {
581 DPRINTF(StackDist,
"Tree leaves, Rightmost-[%d] = %#lx\n",
591 DPRINTF(StackDist,
"Printing Last %d entries in VerifStack \n", n);
593 for (
auto a =
stack.rbegin(); (count <
n) && (
a !=
stack.rend());
595 DPRINTF(StackDist,
"Verif Stack, Top-[%d] = %#lx\n", count, *
a);
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...
std::pair< uint64_t, bool > calcStackDistAndUpdate(const Addr r_address, bool addNewNode=true)
Process the given address:
panic_if(!root,"Invalid expression\n")
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.
uint64_t verifyStackDist(const Addr r_address, bool update_stack=false)
This is an alternative implementation of the stack-distance in a naive way.
Node which takes form of Leaf, INode or Root.
StackDistCalc(bool verify_stack=false)
std::vector< uint64_t > stack
static constexpr uint64_t Infinity
A convenient way of refering to infinity.
void updateTree()
updateTree is a tree balancing operation, which maintains the binary tree structure.
bool isPowerOf2(const T &n)
std::map< uint64_t, Node * > IndexNodeMap
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
std::vector< uint64_t > nextIndex
uint64_t index
Internal counter for address accesses (unique and non-unique) This counter increments everytime the c...
bool isMarked
Flag to indicate if this address is marked.
uint64_t getTreeDepth() const
Query depth of the tree (tree[0] represents leaf layer while tree[treeDepth] represents the root laye...
uint64_t getSumsLeavesToRoot(Node *node) const
Gets the sum from the leaf node specified.
void printStack(int n=5) const
Print the last n items on the stack.
Declaration and inline definition of ChunkGenerator object.
std::pair< uint64_t, bool > calcStackDist(const Addr r_address, bool mark=false)
Process the given address.
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.