gem5  v21.1.0.2
Classes | Public Member Functions | Static Public Attributes | Private Types | Private Member Functions | Private Attributes | List of all members
gem5::StackDistCalc Class Reference

The stack distance calculator is a passive object that merely observes the addresses pass to it. More...

#include <stack_dist_calc.hh>

Classes

struct  Node
 Node which takes form of Leaf, INode or Root. More...
 

Public Member Functions

 StackDistCalc (bool verify_stack=false)
 
 ~StackDistCalc ()
 
std::pair< uint64_t, bool > calcStackDist (const Addr r_address, bool mark=false)
 Process the given address. More...
 
std::pair< uint64_t, bool > calcStackDistAndUpdate (const Addr r_address, bool addNewNode=true)
 Process the given address: More...
 

Static Public Attributes

static constexpr uint64_t Infinity = std::numeric_limits<uint64_t>::max()
 A convenient way of refering to infinity. More...
 

Private Types

typedef std::map< uint64_t, Node * > IndexNodeMap
 
typedef std::map< Addr, uint64_t > AddressIndexMap
 
typedef std::vector< IndexNodeMapTreeType
 

Private Member Functions

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. More...
 
uint64_t getSumsLeavesToRoot (Node *node) const
 Gets the sum from the leaf node specified. More...
 
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. More...
 
uint64_t updateSumsLeavesToRoot (Node *node, bool is_new_leaf)
 Updates the leaf nodes and nodes above. More...
 
void updateTree ()
 updateTree is a tree balancing operation, which maintains the binary tree structure. More...
 
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 till the root to check if the ultimate parent node (root-node) points to null. More...
 
uint64_t getIndex () const
 Return the counter for address accesses (unique and non-unique). More...
 
uint64_t getTreeDepth () const
 Query depth of the tree (tree[0] represents leaf layer while tree[treeDepth] represents the root layer, all layers in between contain intermediate nodes) More...
 
void printStack (int n=5) const
 Print the last n items on the stack. More...
 
uint64_t verifyStackDist (const Addr r_address, bool update_stack=false)
 This is an alternative implementation of the stack-distance in a naive way. More...
 

Private Attributes

uint64_t index
 Internal counter for address accesses (unique and non-unique) This counter increments everytime the calcStackDist() method is called. More...
 
TreeType tree
 
AddressIndexMap aiMap
 
std::vector< uint64_t > nextIndex
 
std::vector< uint64_t > stack
 
const bool verifyStack
 

Detailed Description

The stack distance calculator is a passive object that merely observes the addresses pass to it.

It calculates stack distances of incoming addresses based on the partial sum hierarchy tree algorithm described by Alamasi et al. http://doi.acm.org/10.1145/773039.773043.

A tree structure is maintained and updated at each transaction (unique or non-unique). The tree is implemented as an STL vector with layers of the form <map> Each layer in this tree is an ordered map <uint64_t, Node*>. Nodes are structs which take form of leaf, intermediate and root nodes. For example, in a tree with 3 layers, tree[0][5] gives a leaf node pointer for key=5 tree[1][1] gives an intermediate node pointer for key=1 tree[2][0] gives the root node in the tree.

At every transaction a hash-map (aiMap) is looked up to check if the address was already encountered before. Based on this lookup a transaction can be termed as unique or non-unique.

In addition to the normal stack distance calculation, a feature to mark an old node in the tree is added. This is useful if it is required to see the reuse pattern. For example, BackInvalidates from a lower level (e.g. membus to L2), can be marked (isMarked flag of Node set to True). Then later if this same address is accessed (by L1), the value of the isMarked flag would be True. This would give some insight on how the BackInvalidates policy of the lower level affect the read/write accesses in an application.

There are two functions provided to interface with the calculator:

  1. pair<uint64_t, bool> calcStackDistAndUpdate(Addr r_address, bool addNewNode) At every unique transaction a new leaf node is added at tree[0](leaf layer) and linked to the layer above (if addNewNode is True). The sums of all the intermediate nodes is updated till the root. The stack-distance is returned as a Constant representing INFINITY.

At every non-unique transaction the tree is traversed from the leaf at the returned index to the root, the old node is deleted from the tree, and the sums (to the right are collected) and decremented. The collected sum represets the stack distance of the found node. If this node was marked then a bool flag set to True is returned with the stack_distance. During this operation a node is discarded at the leaf layer always. Moreover during the traversal upwards using the updateSum() method, if an intermediate node is found with no children connected to it, then that is discarded too.

The return value of this function is a pair representing the stack_distance and the value of the marked flag.

  1. pair<uint64_t , bool> calcStackDist(Addr r_address, bool mark) This is a stripped down version of the above function which is used to just inspect the tree, and mark a leaf node (if mark flag is set). The functionality to add a new node is removed.

At every unique transaction the stack-distance is returned as a constant representing INFINITY.

At every non-unique transaction the tree is traversed from the leaf at the returned index to the root, and the sums (to the right) are collected. The collected sum represets the stack distance of the found node.

This function does NOT Modify the stack. (No node is added or deleted). It is just used to mark a node already created and get its stack distance.

The return value of this function is a pair representing the stack distance and the value of the marked flag.

The table below depicts the usage of the Algorithm using the functions: pair<uint64_t Stack_dist, bool isMarked> calcStackDistAndUpdate (Addr r_address, bool addNewNode) pair<uint64_t Stack_dist, bool isMarked> calcStackDist (Addr r_address, bool mark)

| Function | Arguments |Return Val |Use For| |calcStackDistAndUpdate|r_address, True|I/SD,False |A,GD,GM| |calcStackDistAndUpdate|r_address,False|SD,prevMark|D,GD,GM| |calcStackDist |r_address,False|SD,prevMark| GD,GM| |calcStackDist |r_address, True|SD,prevMark| GD,GM|

(*A: Allocate an address in stack, if old entry present then it is deleted, *U: Delete old-address from stack, no new entry is added *GD: Get-Stack distance of an address, *GM: Get value of Mark flag, indicates if that address has been touched before, *I: stack-distance = infinity, *SD: Stack Distance *r_address: address to be added, *prevMark: value of isMarked flag of the Node)

Invalidates refer to a type of packet that removes something from a cache, either autonoumously (due-to cache's own replacement policy), or snoops from other caches which invalidate something inside our cache.

Usage | Function to use |Typical Use | Add new entry |calcStackDistAndUpdate|Read/Write Allocate | Delete Old Entry |calcStackDistAndUpdate|Writebacks/Cleanevicts| Dist.of Old entry|calcStackDist |Cleanevicts/Invalidate|

Node Balancing: The tree structure is maintained by an updateTree() operation called when an intermediate node is required. The update operation is roughly categorized as a root update or intermediate layer update. When number of leaf nodes grow over a power of 2 then a new layer is added at the top of the tree and a new root node is initialized. The old node at the lower layer is connected to this. In an intermediate node update operation a new intermediate node is added to the required layer.

Debugging: Debugging can be enabled by setting the verifyStack flag true. Debugging is implemented using a dummy stack that behaves in a naive way, using STL vectors (i.e each unique address is pushed on the top of an STL vector stack, and SD is returned as Infinity. If a non unique address is encountered then the previous entry in the STL vector is removed, all the entities above it are pushed down, and the address is pushed at the top of the stack).

A printStack(int numOfEntitiesToPrint) is provided to print top n entities in both (tree and STL based dummy stack).

Definition at line 174 of file stack_dist_calc.hh.

Member Typedef Documentation

◆ AddressIndexMap

typedef std::map<Addr, uint64_t> gem5::StackDistCalc::AddressIndexMap
private

Definition at line 182 of file stack_dist_calc.hh.

◆ IndexNodeMap

typedef std::map<uint64_t, Node*> gem5::StackDistCalc::IndexNodeMap
private

Definition at line 179 of file stack_dist_calc.hh.

◆ TreeType

Definition at line 183 of file stack_dist_calc.hh.

Constructor & Destructor Documentation

◆ StackDistCalc()

gem5::StackDistCalc::StackDistCalc ( bool  verify_stack = false)

Definition at line 49 of file stack_dist_calc.cc.

References nextIndex, gem5::StackDistCalc::Node::nodeIndex, and tree.

◆ ~StackDistCalc()

gem5::StackDistCalc::~StackDistCalc ( )

Definition at line 76 of file stack_dist_calc.cc.

References aiMap, nextIndex, stack, and tree.

Member Function Documentation

◆ calcStackDist()

std::pair< uint64_t, bool > gem5::StackDistCalc::calcStackDist ( const Addr  r_address,
bool  mark = false 
)

Process the given address.

If Mark is true then set the mark flag of the leaf node. This function returns the stack distance of the incoming address and the previous status of the mark flag.

Parameters
r_addressThe current address to process
markset the mark flag for the address.
Returns
The stack distance of the current address and the mark flag.

Definition at line 463 of file stack_dist_calc.cc.

References aiMap, getSumsLeavesToRoot(), Infinity, panic_if, printStack(), tree, verifyStack, and verifyStackDist().

◆ calcStackDistAndUpdate()

std::pair< uint64_t, bool > gem5::StackDistCalc::calcStackDistAndUpdate ( const Addr  r_address,
bool  addNewNode = true 
)

Process the given address:

  • Lookup the tree for the given address
  • delete old node if found in tree
  • add a new node (if addNewNode flag is set) This function returns the stack distance of the incoming address and the status of the mark flag.
Parameters
r_addressThe current address to process
addNewNodeIf true, a new node is added to the tree
Returns
The stack distance of the current address and the mark flag.

Definition at line 361 of file stack_dist_calc.cc.

References aiMap, index, Infinity, gem5::StackDistCalc::Node::isLeftNode, gem5::StackDistCalc::Node::isMarked, nextIndex, gem5::StackDistCalc::Node::nodeIndex, panic_if, gem5::StackDistCalc::Node::parent, printStack(), sanityCheckTree(), tree, updateSumsLeavesToRoot(), updateTree(), verifyStack, and verifyStackDist().

Referenced by gem5::StackDistProbe::handleRequest().

◆ getIndex()

uint64_t gem5::StackDistCalc::getIndex ( ) const
inlineprivate

Return the counter for address accesses (unique and non-unique).

This is further used to dump stats at regular intervals.

Returns
The stack distance of the current address.

Definition at line 273 of file stack_dist_calc.hh.

References index.

◆ getSum()

uint64_t gem5::StackDistCalc::getSum ( Node node,
bool  from_left,
uint64_t  sum_from_below,
uint64_t  stack_dist,
uint64_t  level 
) const
private

Gets sum from the node upwards recursively till the root.

This function is called first by getSumsLeavesToRoot, and then recursively calls itself.

Parameters
nodepointer to the node which is updated
from_leftvariable which says that the request arrived from the left
sum_from_belowSum of left and right children below
levellevel in the tree the calling node is located
stack_diststack distance of the node below
Returns
The stack distance of the current address.

Definition at line 215 of file stack_dist_calc.cc.

References gem5::StackDistCalc::Node::isLeftNode, gem5::X86ISA::level, gem5::StackDistCalc::Node::parent, gem5::StackDistCalc::Node::sumLeft, and gem5::StackDistCalc::Node::sumRight.

Referenced by getSumsLeavesToRoot().

◆ getSumsLeavesToRoot()

uint64_t gem5::StackDistCalc::getSumsLeavesToRoot ( Node node) const
private

Gets the sum from the leaf node specified.

This function is called by calcStackDist.

Parameters
nodepointer to the node which is updated
Returns
The stack distance of the current address.

Definition at line 238 of file stack_dist_calc.cc.

References getSum(), gem5::StackDistCalc::Node::isLeftNode, and gem5::StackDistCalc::Node::parent.

Referenced by calcStackDist().

◆ getTreeDepth()

uint64_t gem5::StackDistCalc::getTreeDepth ( ) const
inlineprivate

Query depth of the tree (tree[0] represents leaf layer while tree[treeDepth] represents the root layer, all layers in between contain intermediate nodes)

Returns
Tree depth

Definition at line 282 of file stack_dist_calc.hh.

References tree.

Referenced by printStack(), sanityCheckTree(), and updateTree().

◆ printStack()

void gem5::StackDistCalc::printStack ( int  n = 5) const
private

Print the last n items on the stack.

This method prints top n entries in the tree based implementation as well as dummy stack.

Parameters
nNumber of entries to print

Definition at line 567 of file stack_dist_calc.cc.

References gem5::ArmISA::a, aiMap, gem5::X86ISA::count, DPRINTF, getTreeDepth(), gem5::ArmISA::n, gem5::StackDistCalc::Node::nodeIndex, stack, tree, and verifyStack.

Referenced by calcStackDist(), and calcStackDistAndUpdate().

◆ sanityCheckTree()

void gem5::StackDistCalc::sanityCheckTree ( const Node node,
uint64_t  level = 0 
) const
private

This method is used for verification purposes It recursively traverses upwards from the given node till the root to check if the ultimate parent node (root-node) points to null.

Parameters
nodepointer to the node whose sanity is being checked
levelthe level at which this node is located in the tree

Definition at line 516 of file stack_dist_calc.cc.

References getTreeDepth(), gem5::ArmISA::i, gem5::X86ISA::level, gem5::StackDistCalc::Node::nodeIndex, panic_if, and gem5::StackDistCalc::Node::parent.

Referenced by calcStackDistAndUpdate().

◆ updateSum()

uint64_t gem5::StackDistCalc::updateSum ( Node node,
bool  from_left,
uint64_t  sum_from_below,
uint64_t  level,
uint64_t  stack_dist,
bool  discard_node 
)
private

Updates the nodes upwards recursively till the root.

This function is first called by updateSumsLeavesToRoot, and then it recursively calls itself.

Parameters
nodepointer to the node which is updated
from_leftvariable which says that the request arrived from the left
sum_from_belowSum of left and right children below
levellevel in the tree the calling node is located
stack_diststack distance of the node below
discard_nodewhether the calling node was discarded or not
Returns
The stack distance of the current address.

Definition at line 101 of file stack_dist_calc.cc.

References gem5::StackDistCalc::Node::discardLeft, gem5::StackDistCalc::Node::discardRight, gem5::StackDistCalc::Node::isLeftNode, gem5::X86ISA::level, gem5::StackDistCalc::Node::nodeIndex, panic_if, gem5::StackDistCalc::Node::parent, gem5::StackDistCalc::Node::sumLeft, gem5::StackDistCalc::Node::sumRight, tree, and verifyStack.

Referenced by updateSumsLeavesToRoot().

◆ updateSumsLeavesToRoot()

uint64_t gem5::StackDistCalc::updateSumsLeavesToRoot ( Node node,
bool  is_new_leaf 
)
private

Updates the leaf nodes and nodes above.

This function is called by the calcStackDistAndUpdate.

Parameters
nodepointer to the node which is updated
is_new_leafis true if this is a newly added node
Returns
The stack distance of the current address.

Definition at line 190 of file stack_dist_calc.cc.

References Infinity, gem5::StackDistCalc::Node::isLeftNode, gem5::X86ISA::level, gem5::StackDistCalc::Node::parent, gem5::StackDistCalc::Node::sumLeft, and updateSum().

Referenced by calcStackDistAndUpdate().

◆ updateTree()

void gem5::StackDistCalc::updateTree ( )
private

updateTree is a tree balancing operation, which maintains the binary tree structure.

This method is called whenever index%2 == 0 (i.e. every alternate cycle) The two main operation are : OP1. Moving the root node one layer up if index counter crosses power of 2 OP2. Addition of intermediate nodes as and when required and linking them to their parents in the layer above.

Definition at line 252 of file stack_dist_calc.cc.

References gem5::StackDistCalc::Node::discardLeft, getTreeDepth(), gem5::ArmISA::i, index, gem5::StackDistCalc::Node::isLeftNode, gem5::isPowerOf2(), nextIndex, gem5::StackDistCalc::Node::nodeIndex, gem5::StackDistCalc::Node::parent, gem5::StackDistCalc::Node::sumLeft, and tree.

Referenced by calcStackDistAndUpdate().

◆ verifyStackDist()

uint64_t gem5::StackDistCalc::verifyStackDist ( const Addr  r_address,
bool  update_stack = false 
)
private

This is an alternative implementation of the stack-distance in a naive way.

It uses simple STL vector to represent the stack. It can be used in parallel for debugging purposes. It is 10x slower than the tree based implemenation.

Parameters
r_addressThe current address to process
update_stackFlag to indicate if stack should be updated
Returns
Stack distance which is calculated by this alternative implementation

Definition at line 536 of file stack_dist_calc.cc.

References gem5::ArmISA::a, Infinity, and stack.

Referenced by calcStackDist(), and calcStackDistAndUpdate().

Member Data Documentation

◆ aiMap

AddressIndexMap gem5::StackDistCalc::aiMap
private

◆ index

uint64_t gem5::StackDistCalc::index
private

Internal counter for address accesses (unique and non-unique) This counter increments everytime the calcStackDist() method is called.

This counter is used as a key for the hash- map at the leaf layer. Practically at every call to the calculator this counter is incremented and a new leaf node is added in the tree at the leaf layer using this counter value as the key.

Definition at line 398 of file stack_dist_calc.hh.

Referenced by calcStackDistAndUpdate(), getIndex(), and updateTree().

◆ Infinity

constexpr uint64_t gem5::StackDistCalc::Infinity = std::numeric_limits<uint64_t>::max()
staticconstexpr

A convenient way of refering to infinity.

Definition at line 315 of file stack_dist_calc.hh.

Referenced by calcStackDist(), calcStackDistAndUpdate(), gem5::StackDistProbe::handleRequest(), updateSumsLeavesToRoot(), and verifyStackDist().

◆ nextIndex

std::vector<uint64_t> gem5::StackDistCalc::nextIndex
private

◆ stack

std::vector<uint64_t> gem5::StackDistCalc::stack
private

Definition at line 411 of file stack_dist_calc.hh.

Referenced by printStack(), verifyStackDist(), and ~StackDistCalc().

◆ tree

TreeType gem5::StackDistCalc::tree
private

◆ verifyStack

const bool gem5::StackDistCalc::verifyStack
private

Definition at line 414 of file stack_dist_calc.hh.

Referenced by calcStackDist(), calcStackDistAndUpdate(), printStack(), and updateSum().


The documentation for this class was generated from the following files:

Generated on Tue Sep 21 2021 12:28:14 for gem5 by doxygen 1.8.17