Skip to content

Commit

Permalink
fix segfault (#2468)
Browse files Browse the repository at this point in the history
  • Loading branch information
MishkaMN authored Nov 19, 2024
1 parent f34911c commit 457073e
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions engineering_tools/monitor-ros-cpu.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3

import psutil
import subprocess
import time
Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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"]
Expand All @@ -81,7 +77,6 @@ def main():
total_cpu_percent,
]
)

except (
psutil.NoSuchProcess,
psutil.AccessDenied,
Expand All @@ -91,6 +86,5 @@ def main():

time.sleep(1)


if __name__ == "__main__":
main()

0 comments on commit 457073e

Please sign in to comment.