Prevent callbacks to occur multiple times in one path (loop prevention)

This commit is contained in:
Maximilian Schmeller 2022-09-20 14:42:48 +02:00
parent a2f3b4f46c
commit a217359e8b

View file

@ -488,7 +488,10 @@
"g"
],
"metadata": {
"collapsed": false
"collapsed": false,
"pycharm": {
"name": "#%%%%skip_if_false DFG_ENABLED\n"
}
}
},
{
@ -617,10 +620,10 @@
" return dep_inst\n",
"\n",
"\n",
"def get_dep_tree(inst: TrPublishInstance | TrCallbackInstance, lvl=0, visited_topics=None, is_dep_cb=False,\n",
"def get_dep_tree(inst: TrPublishInstance | TrCallbackInstance, lvl=0, visited=None, is_dep_cb=False,\n",
" start_time=None):\n",
" if visited_topics is None:\n",
" visited_topics = set()\n",
" if visited is None:\n",
" visited = set()\n",
"\n",
" if start_time is None:\n",
" start_time = inst.timestamp\n",
@ -632,12 +635,17 @@
"\n",
" match inst:\n",
" case TrPublishInstance(publisher=pub):\n",
" if pub.topic_name in visited_topics:\n",
" if pub.topic in visited:\n",
" return None\n",
"\n",
" visited_topics.add(pub.topic_name)\n",
" visited.add(pub.topic)\n",
" deps = [get_msg_dep_cb(inst)]\n",
" case TrCallbackInstance() as cb_inst:\n",
" cb_inst: TrCallbackInstance\n",
" if cb_inst.callback_obj in visited:\n",
" return None\n",
"\n",
" visited.add(cb_inst.callback_obj)\n",
" deps = [inst_get_dep_msg(cb_inst)]\n",
" if not is_dep_cb:\n",
" deps += inst_get_dep_insts(cb_inst)\n",
@ -648,7 +656,7 @@
" return None\n",
" #print(\"Rec level\", lvl)\n",
" deps = [dep for dep in deps if dep is not None]\n",
" deps = [get_dep_tree(dep, lvl + 1, set(visited_topics), children_are_dep_cbs, start_time) for dep in deps]\n",
" deps = [get_dep_tree(dep, lvl + 1, set(visited), children_are_dep_cbs, start_time) for dep in deps]\n",
" deps = [dep for dep in deps if dep is not None]\n",
" return DepTree(inst, deps)"
],
@ -786,7 +794,7 @@
" return ret_list\n",
"\n",
"\n",
"in_topics_found = set()\n",
"in_topics_found = defaultdict(lambda: 0)\n",
"leaf_topics_found = defaultdict(lambda: 0)\n",
"def relevant_e2es(tree: DepTree, input_topic_patterns, t_start=None, path=None):\n",
" if t_start is None:\n",
@ -805,7 +813,7 @@
" case TrPublishInstance(publisher=pub):\n",
" leaf_topics_found[pub.topic_name] += 1\n",
" if pub and any(re.search(f, pub.topic_name) for f in input_topic_patterns):\n",
" in_topics_found.add(pub.topic_name)\n",
" in_topics_found[pub.topic_name] += 1\n",
" return [(latency, new_path)]\n",
"\n",
" ret_list = []\n",
@ -825,11 +833,11 @@
" e2ess.append(e2es)\n",
" e2e_pathss.append(e2e_paths)\n",
"\n",
"print(f\"[ENTKÄFERN] {len(e2ess)=} {len(trees)=}\")\n",
"in_topics_found = \"\\n \".join(in_topics_found)\n",
"print(f\"[ENTKÄFERN] IN-TOPICS FOUND in {len(e2ess)}/{len(trees)} trees\")\n",
"in_topics_found = \"\\n \".join(map(lambda pair: f\"{pair[0]:.<110s} {pair[1]:>10d}\", in_topics_found.items()))\n",
"print(f\" {in_topics_found}\")\n",
"\n",
"print(f\"[ENTKÄFERN]\")\n",
"print(f\"[ENTKÄFERN] LEAF TOPICS FOUND\")\n",
"leaf_topics_found = \"\\n \".join(map(lambda pair: f\"{pair[0]:.<110s} {pair[1]:>10d}\", leaf_topics_found.items()))\n",
"print(f\" {leaf_topics_found}\")"
],
@ -837,55 +845,6 @@
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
"outputs": [],
"source": [
"\n",
"#from matplotlib.animation import FuncAnimation\n",
"#from IPython import display\n",
"\n",
"#fig, ax = plt.subplots(figsize=(16, 9))\n",
"#ax: plt.Axes\n",
"#ax.set_xlim(0, 4)\n",
"\n",
"#ax.hist([], bins=200, range=(0, 4), histtype='stepfilled')\n",
"#ax.set_title(\"Time: 0.000000s\")\n",
"\n",
"#def anim(frame):\n",
"# print(frame, end='\\r')\n",
"# ax.clear()\n",
"# ax.hist(e2es[frame], bins=200, range=(0, 4), histtype='stepfilled')\n",
"# ax.set_title(f\"Time: {(trees[frame].head.timestamp - trees[0].head.timestamp):.6}s\")\n",
"\n",
"\n",
"#anim_created = FuncAnimation(fig, anim, min(len(trees), 10000), interval=16, repeat_delay=200)\n",
"\n",
"#video = anim_created.save(\"anim.mp4\", dpi=120)\n",
"\n",
"#for tree in trees:\n",
"# path = tree.critical_path(start_topic_filters)\n",
"# for i, inst in enumerate(path[::-1]):\n",
"# match inst:\n",
"# case TrPublishInstance(publisher=pub):\n",
"# print(f\" {i:>3d}: T\", pub.topic_name)\n",
"# case TrCallbackInstance(callback_obj=cb):\n",
"# match cb.owner:\n",
"# case TrSubscriptionObject(subscription=sub):\n",
"# node = sub.node\n",
"# case TrTimer() as tmr:\n",
"# node = tmr.node\n",
"# case _:\n",
"# raise ValueError(f\"Callback owner type not recognized: {type(cb.owner).__name__}\")\n",
"#\n",
"# print(f\" {i:>3d}: N\", node.path)\n",
"# print(\"==================\")\n"
],
"metadata": {
"collapsed": false
}
},
{
"cell_type": "code",
"execution_count": null,
@ -957,10 +916,9 @@
"def get_relevant_tree(tree: DepTree, accept_leaf=lambda x: True, root=None):\n",
" if root is None:\n",
" root = tree.head\n",
" if not tree.deps:\n",
" if accept_leaf(tree.head, root):\n",
" return tree\n",
" return None\n",
"\n",
" if accept_leaf(tree.head, root):\n",
" return DepTree(tree.head, []) # Cut subtrees because we already found our input topic\n",
"\n",
" relevant_deps = [get_relevant_tree(dep, accept_leaf, root) for dep in tree.deps]\n",
" if not any(relevant_deps):\n",
@ -997,7 +955,10 @@
"relevant_trees = [t for t in relevant_trees if t]"
],
"metadata": {
"collapsed": false
"collapsed": false,
"pycharm": {
"name": "#%%%%skip_if_false E2E_ENABLED\n"
}
}
},
{
@ -1310,7 +1271,10 @@
"print(len(y_labels))"
],
"metadata": {
"collapsed": false
"collapsed": false,
"pycharm": {
"name": "#%%%%skip_if_false E2E_ENABLED\n"
}
}
},
{
@ -1410,7 +1374,10 @@
"out_df.to_csv(os.path.join(OUT_PATH, \"e2e.csv\"), sep=\"\\t\", index=False)\n"
],
"metadata": {
"collapsed": false
"collapsed": false,
"pycharm": {
"name": "#%%%%skip_if_false E2E_ENABLED\n"
}
}
}
],