Skip to content

Commit

Permalink
caliperreader: handle root nodes in _create_parent (#68)
Browse files Browse the repository at this point in the history
When creating parent nodes, we need to handle the case that the parent might be
a root node. Previously, the recursive _create_parent calls were being made on
root nodes, and we incorrectly tried to index into the grandparent callpath
tuple, even though it was empty. This ends the recursion if we encounter an
empty callpath tuple.
  • Loading branch information
slabasan authored Oct 24, 2022
1 parent 4063198 commit 120f71e
Showing 1 changed file with 40 additions and 19 deletions.
59 changes: 40 additions & 19 deletions hatchet/readers/caliper_native_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def read_metrics(self, ctx="path"):
return df_metrics

def create_graph(self, ctx="path"):
list_roots = []

def _create_parent(child_node, parent_callpath):
"""We may encounter a parent node in the callpath before we see it
as a child node. In this case, we need to create a hatchet node for
Expand All @@ -131,27 +133,46 @@ def _create_parent(child_node, parent_callpath):
return
else:
# else create the parent and add parent/child
grandparent_callpath = parent_callpath[:-1]
parent_name = parent_callpath[-1]

parent_node = Node(
Frame({"type": "function", "name": parent_name}), None
)

self.callpath_to_node[parent_callpath] = parent_node
self.callpath_to_idx[parent_callpath] = self.global_nid

node_dict = dict(
{"name": parent_name, "node": parent_node, "nid": self.global_nid},
)
self.idx_to_node[self.global_nid] = node_dict
self.global_nid += 1
# if root node, end recursive call to create parent nodes
if not parent_callpath:
list_roots.append(child_node)

node_dict = dict(
{
"name": child_node.frame["name"],
"node": child_node,
"nid": self.global_nid,
}
)

self.idx_to_node[self.global_nid] = node_dict
self.global_nid += 1
else:
grandparent_callpath = parent_callpath[:-1]
parent_name = parent_callpath[-1]

parent_node = Node(
Frame({"type": "function", "name": parent_name}), None
)

self.callpath_to_node[parent_callpath] = parent_node
self.callpath_to_idx[parent_callpath] = self.global_nid

node_dict = dict(
{
"name": parent_name,
"node": parent_node,
"nid": self.global_nid,
},
)
self.idx_to_node[self.global_nid] = node_dict
self.global_nid += 1

parent_node.add_child(child_node)
child_node.add_parent(parent_node)
_create_parent(parent_node, grandparent_callpath)

parent_node.add_child(child_node)
child_node.add_parent(parent_node)
_create_parent(parent_node, grandparent_callpath)

list_roots = []
parent_hnode = None
records = self.filename_or_caliperreader.records

Expand Down

0 comments on commit 120f71e

Please sign in to comment.