7from datetime
import datetime
10CARMA Platform CPU Monitor Script
12 - Python 3.6 or higher
13 - psutil package (install
with: pip3 install psutil)
15 1. Open a new terminal before starting CARMA Platform
16 2. Navigate to the directory containing this script:
19 python3 monitor-ros-cpu.py
20 4. In a different terminal, start CARMA Platform:
22 5. The script will automatically monitor
and log CPU/memory usage
23 of all ROS2 nodes
and related processes during CARMA operation
24 6. To stop monitoring:
25 - Press Ctrl+C ONCE
in the monitoring terminal
26 - The CSV output file will be saved
in the logs directory following ROS bag naming convention:
27 'logs/cpu_usage_ros2_nodes_YYYY_MM_DD-HH_MM_SS.csv'
29 - CSV file containing timestamp, process info, CPU
and memory usage
30 - Data can be used to analyze CARMA Platform resource utilization
33# Define ROS-related keywords to filter processes
61EXCLUDE_KEYWORDS = {
"code",
"chrome",
"firefox",
"vscode",
"gnome"}
65 parser = argparse.ArgumentParser(description=
"Monitor CPU usage of ROS2 nodes")
69 default=
"carma-cpu-usage-logs",
70 help=
"Directory to store output files (default: carma-cpu-usage-logs)",
75 help=
"Additional comma-separated patterns to include in process filtering",
77 return parser.parse_args()
81 if not os.path.exists(output_dir):
82 os.makedirs(output_dir)
83 timestamp = datetime.now().strftime(
"%Y_%m_%d-%H_%M_%S")
84 filename = f
"cpu_usage_ros2_nodes_{timestamp}.csv"
85 return os.path.join(output_dir, filename)
90 Check if a process
is ROS-related based on name
and command line
93 name_lower = proc_info[
"name"].lower()
94 cmdline_lower = cmdline.lower()
97 if any(excl
in name_lower
or excl
in cmdline_lower
for excl
in EXCLUDE_KEYWORDS):
102 keyword
in name_lower
or keyword
in cmdline_lower
for keyword
in ROS_KEYWORDS
108 Try to get ROS-related environment variables for a process
111 proc = psutil.Process(pid)
113 ros_env = {k: v
for k, v
in env.items()
if "ROS" in k}
115 except (psutil.NoSuchProcess, psutil.AccessDenied):
123 if args.include_pattern:
124 additional_patterns = set(args.include_pattern.split(
","))
125 ROS_KEYWORDS.update(additional_patterns)
129 with open(output_file, mode=
"a")
as file:
130 writer = csv.writer(file)
146 print(f
"Starting to monitor the CPU usage data and saving to: {output_file}")
149 total_memory_gb = psutil.virtual_memory().total / (1024**3)
150 total_cpus = os.cpu_count()
153 timestamp = datetime.now().strftime(
"%Y-%m-%d %H:%M:%S.%f")[:-3]
154 total_cpu_percent = psutil.cpu_percent(interval=
None)
155 total_memory_percent = (
156 psutil.virtual_memory().percent
159 for proc
in psutil.process_iter(
160 [
"pid",
"name",
"cpu_percent",
"memory_percent",
"cmdline"]
164 " ".join(proc.info[
"cmdline"])
if proc.info[
"cmdline"]
else ""
168 pid = proc.info[
"pid"]
169 name = proc.info[
"name"]
170 cpu_percent = proc.info[
"cpu_percent"]
171 memory_percent = proc.info[
"memory_percent"]
183 total_memory_percent,
189 psutil.NoSuchProcess,
191 psutil.ZombieProcess,
198if __name__ ==
"__main__":
def is_ros_related_process(proc_info, cmdline)
def get_process_environment(pid)
def setup_logging_directory(output_dir)