From 457073ec0bffdfc2e42432df8ce7ee280ff219c6 Mon Sep 17 00:00:00 2001 From: Misheel Bayartsengel Date: Tue, 19 Nov 2024 14:26:45 -0500 Subject: [PATCH] fix segfault (#2468) --- engineering_tools/monitor-ros-cpu.py | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/engineering_tools/monitor-ros-cpu.py b/engineering_tools/monitor-ros-cpu.py index 89f32e745d..daf8b4cb53 100644 --- a/engineering_tools/monitor-ros-cpu.py +++ b/engineering_tools/monitor-ros-cpu.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 - import psutil import subprocess import time @@ -8,7 +7,6 @@ import argparse from datetime import datetime - def parse_args(): parser = argparse.ArgumentParser(description="Monitor CPU usage of ROS2 nodes") parser.add_argument( @@ -19,17 +17,13 @@ def parse_args(): ) return parser.parse_args() - def setup_logging_directory(output_dir): if not os.path.exists(output_dir): os.makedirs(output_dir) - timestamp = datetime.now().strftime("%Y_%m_%d-%H_%M_%S") filename = f"cpu_usage_ros2_nodes_{timestamp}.csv" - return os.path.join(output_dir, filename) - def main(): args = parse_args() output_file = setup_logging_directory(args.output_dir) @@ -57,14 +51,16 @@ def main(): ["pid", "name", "cpu_percent", "memory_percent", "cmdline"] ): try: - cmdline = " ".join(proc.info["cmdline"]) + # Fix for the join() error - handle None case + cmdline = " ".join(proc.info["cmdline"]) if proc.info["cmdline"] else "" + if ( - "ros" in proc.info["name"] - or "node" in proc.info["name"] - or "node" in cmdline - or "python3" in proc.info["name"] - or "ros" in cmdline - ) and not ("code" in proc.info["name"]): + "ros" in proc.info["name"].lower() + or "node" in proc.info["name"].lower() + or "node" in cmdline.lower() + or "python3" in proc.info["name"].lower() + or "ros" in cmdline.lower() + ) and not ("code" in proc.info["name"].lower()): pid = proc.info["pid"] name = proc.info["name"] cpu_percent = proc.info["cpu_percent"] @@ -81,7 +77,6 @@ def main(): total_cpu_percent, ] ) - except ( psutil.NoSuchProcess, psutil.AccessDenied, @@ -91,6 +86,5 @@ def main(): time.sleep(1) - if __name__ == "__main__": main()