Robotics StackExchange | Archived questions

How to get process ID of ROS 2 nodes ?

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.

Asked by Chenxuan on 2020-12-08 02:46:38 UTC

Comments

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.

Asked by clalancette on 2020-12-18 14:54:51 UTC

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.

Asked by Chenxuan on 2020-12-25 10:53:46 UTC

Answers

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;

Asked by runtao on 2021-01-01 16:49:54 UTC

Comments

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.

Asked by Chenxuan on 2021-01-04 08:59:07 UTC