ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

How to get process ID of ROS 2 nodes ?

asked 2020-12-08 01:46:38 -0500

Chenxuan gravatar image

updated 2020-12-25 09:56:09 -0500

I would like to get the process ID of ROS 2 nodes .

In ROS 1 , it could be achieved by Slave_API

In ROS 2 , I viewed rclcpp API and only could get just about nodes and topics name information.

Is there any way I can do it ?

Willing to take advice.

edit retag flag offensive close merge delete



There isn't really a way to look for PIDs in the ROS 2 API. The main reason for this is that in ROS 2, a PID isn't a good indication of a single unit of work. That is, there can be multiple nodes in a single process by using composition.

What are you trying to accomplish? Maybe there is another way to do what you want.

clalancette gravatar image clalancette  ( 2020-12-18 13:54:51 -0500 )edit

Sorry for the late reply . I would like to profile nodes at runtime with perf. For the profiling result , I needed to mapping PIDs or process names to nodes.

Chenxuan gravatar image Chenxuan  ( 2020-12-25 09:53:46 -0500 )edit

1 Answer

Sort by » oldest newest most voted

answered 2021-01-01 15:49:54 -0500

runtao gravatar image

updated 2021-01-01 15:51:17 -0500

You can just insert this snippet into your node, it's not ros specific:

//print pid
#include <unistd.h>  
std::cout << "pid= " << getpid() << std::endl;

//print tid
#include <thread>
std::cout << "tid=" << std::this_thread::get_id() << std::endl;
edit flag offensive delete link more


Thanks. After I studied, I found two ways that don't modify the user code. One is using eBPF to insert the probe into rcl layer where nodes were initialized there. The other way is using ready-made tools ros2_tracing.

Chenxuan gravatar image Chenxuan  ( 2021-01-04 07:59:07 -0500 )edit

Question Tools



Asked: 2020-12-08 01:46:38 -0500

Seen: 1,786 times

Last updated: Jan 01 '21