The calltree module

Utilities to get a call tree (from the output of cube_dump -w).

A call tree is represented as a tree of (function_name,cnode_id,parent,[list of children]) named tuples (of class CubeTreeNode)

class calltree.CubeTreeNode(*args: Any, default_box: bool = False, default_box_attr: Any = <object object>, default_box_none_transform: bool = True, frozen_box: bool = False, camel_killer_box: bool = False, conversion_box: bool = True, modify_tuples_box: bool = False, box_safe_prefix: str = 'x', box_duplicates: str = 'ignore', box_intact_types: Union[Tuple, List] = (), box_recast: Dict = None, box_dots: bool = False, **kwargs: Any)

Holds attributes of a tree node read from cube commands such as cube dump.

For a node of the cube call tree, the following attributes are available:

fname

The name of the function;

cnode_id

The unique ID related to the node in the call tree, read from id=value in string;

parent

A binding to the parent node (can be None);

children

A list of bindings to child nodes.

And others from cube_dump output.

calltree.get_call_tree(profile_file)

Typical use case, gets all the information regarding the calltree

Parameters

profile_file (str) – Name of the .cubex file

Returns

calltree – A recursive representation of the call tree.

Return type

CubeTreeNode

calltree.calltree_to_df(call_tree, full_path=False)

Convert a call tree into a DataFrame.

Parameters
  • call_tree (CubeTreeNode) – Recursive representation of a call tree

  • full_path (bool) – Whether or not the full path needs to be in the output as a column

Returns

df – A dataframe with “Function Name”, “Cnode ID”, “Parent Cnode ID”, “Level” and optionally “Full Callpath” as columns.

Return type

DataFrame

calltree.get_level(parent_series)

This function computes the levels starting from the parent information.

Parameters

parent_series (pandas.Dataframe or pandas.Series) – Either a DataFrame containing two columns - the CNode IDs of the parent and the Cnode IDs of the child, or a series containing the Cnode ID of the parent indexed by the Cnode ID of the child.

Returns

levels – A series containing the levels of each node, indexed by Cnode ID

Return type

pandas.Series

Printing and fancy recursive stuff

The calltree_to_string function is a versatile function that can be used to get an ASCII representation of the call tree. It can also be used to print a “payload”, which is anything that can be indexed with Cnode ID/s. The tree can be cut at any level.

A possible use case is to print an inclusive metric next to a graphical representation of the call tree, limited to a certain depth.

calltree.calltree_to_string(root, max_len=60, maxlevel=None, payload=None, full=True)

For an understandable, ascii art representation of the call tree.

Parameters
  • root (CubeTreeNode) – The root of the tree;

  • max_len (int) – Desired length of the printed line;

  • maxlevel (int, None) – Maximum depth of the printed tree. If None, no limit;

  • payload (indexable) – Something that can be indexed by Cnode ID, e.g. an ordered Series.

Returns

res – string representation of the call tree and the payload.

Return type

string

calltree.iterate_on_call_tree(root, maxlevel=None)

Iterator on a tree (Generator). Can be used for searching in the tree, depth-first.

Parameters
  • root (CubeTreeNode) – a CubeTreeNode representing the root of the tree;

  • maxlevel (int or None) – the maximum depth of the recursion (None means unlimited).

Returns

res – Iterator yielding CubeTreeNode s.

Return type

CubeTreeNode