From af16f8c3f8971c44c6c61772370c3f6e58b8ebdf Mon Sep 17 00:00:00 2001 From: SakodaShintaro Date: Wed, 23 Oct 2024 09:49:57 +0900 Subject: [PATCH] fix(system_usage_monitor): change the node search method (#137) * Fixed the node search method Signed-off-by: Shintaro Sakoda * Added a comment Signed-off-by: Shintaro Sakoda --------- Signed-off-by: Shintaro Sakoda --- .../system_usage_monitor.py | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/common/autoware_debug_tools/autoware_debug_tools/system_usage_monitor.py b/common/autoware_debug_tools/autoware_debug_tools/system_usage_monitor.py index c69b182c..2bec9c72 100755 --- a/common/autoware_debug_tools/autoware_debug_tools/system_usage_monitor.py +++ b/common/autoware_debug_tools/autoware_debug_tools/system_usage_monitor.py @@ -44,13 +44,32 @@ def get_system_usage(pid, system_usages, interval): ) if component == "": component = "others" - container = process_name.split("__node:=", 1)[1].split(" ")[0] - system_usages[pid] = { - "component": component, - "container": container, - "cpu_usage": cpu_usage, - "memory_usage": memory_usage, - } + + elements = process_name.split("__node:=", 1) + + # If `process_name`` can be split by "__node:=", get the container name from the second element. + # Otherwise, get the container name from the file name in the first element. + # An example of `process_name` is as follows: + # `/path/to/autoware/install/topic_state_monitor/lib/topic_state_monitor/topic_state_monitor_node + # --ros-args -r __node:=topic_state_monitor_scenario_planning_trajectory -r __ns:=/system ...` + if len(elements) < 2: + container = process_name.split(" ")[0].split("/")[-1] + if container.startswith("autoware_"): + container = container.split("_", 1)[1] + system_usages[pid] = { + "component": component, + "container": container, + "cpu_usage": cpu_usage, + "memory_usage": memory_usage, + } + else: + container = elements[1].split(" ")[0] + system_usages[pid] = { + "component": component, + "container": container, + "cpu_usage": cpu_usage, + "memory_usage": memory_usage, + } except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess, IndexError): pass @@ -128,7 +147,7 @@ def main(args=None): system_usages = {} while True: # create thread to calculate cpu usage of each process since it takes time - process_name_keyword = "__node:=" + process_name_keyword = "--ros-args" threads = [] for proc in psutil.process_iter(["pid", "name", "cpu_percent", "cmdline"]): try: