Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix segfault #2468

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading