From 0356b77185e8fc77fb172659b697cdde14177102 Mon Sep 17 00:00:00 2001 From: Eric Shaw Date: Thu, 29 Jun 2023 18:00:27 +0800 Subject: [PATCH] [Bug] fix _build_tree_relationship in ProfilerResult --- .../component/profiler/parser/event_node.py | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/visualdl/component/profiler/parser/event_node.py b/visualdl/component/profiler/parser/event_node.py index b3d0eebf7..0ec0a79f6 100644 --- a/visualdl/component/profiler/parser/event_node.py +++ b/visualdl/component/profiler/parser/event_node.py @@ -484,7 +484,24 @@ def _build_tree_relationship( # noqa: C901 if not hasenter: firstposition = i hasenter = True - stack_top_node.runtime_node.append(runtimenode) + inserted_node = stack_top_node + runtimenode_stack = [stack_top_node] + while runtimenode_stack: + inserted_node = runtimenode_stack.pop() + if inserted_node is not None: + runtimenode_stack.append(inserted_node) + runtimenode_stack.append(None) + for child in reversed(inserted_node.runtime_node): + if runtimenode.start_ns >= child.start_ns and \ + runtimenode.end_ns <= child.end_ns: + runtimenode_stack.append(child) + else: + inserted_node = runtimenode_stack.pop() + if runtimenode.start_ns >= inserted_node.start_ns and \ + runtimenode.end_ns <= inserted_node.end_ns: + inserted_node.runtime_node.append(runtimenode) + break + else: # from this runtime node, not within stack_top_node, erase the # nodes from runtime_event_nodes @@ -506,7 +523,23 @@ def _build_tree_relationship( # noqa: C901 if not hasenter: firstposition = i hasenter = True - stack_top_node.runtime_node.append(runtimenode) + inserted_node = stack_top_node + runtimenode_stack = [stack_top_node] + while runtimenode_stack: + inserted_node = runtimenode_stack.pop() + if inserted_node is not None: + runtimenode_stack.append(inserted_node) + runtimenode_stack.append(None) + for child in reversed(inserted_node.runtime_node): + if runtimenode.start_ns >= child.start_ns and \ + runtimenode.end_ns <= child.end_ns: + runtimenode_stack.append(child) + else: + inserted_node = runtimenode_stack.pop() + if runtimenode.start_ns >= inserted_node.start_ns and \ + runtimenode.end_ns <= inserted_node.end_ns: + inserted_node.runtime_node.append(runtimenode) + break else: # from this runtime node, not within stack_top_node, erase the # nodes from runtime_event_nodes