更精确测量 ROS 2 话题频率的 C++ 节点 ros2topic_hz
Simple C++ node to get more accurate topic frequency
开发者发布 C++ 版 ros2topic_hz,用于替代 `ros2 topic hz`,在高频率、高带宽话题上测量更准确,目前仅在 Zenoh 上测试过。讨论指出 `ros2 topic hz` 会因首个进程外订阅者迫使发布端序列化消息,在 Humble + CycloneDDS 上使被观测进程 CPU 从 0.303 s 升至 0.460 s(+52%)。
2026 年8 月 29 日 16:06 1
Here is a simple C++ replacement for `ros2 topic hz`.
It is more accurate than the native ros2 command for high frequency / high bandwidth topics. It should work for any RMW (though I tested it for Zenoh only).
After building the workspace, run it like this:
ros2 run ros2topic_hz ros2topic_hz --topic <topic_name> [--topic-type <topic_type>] [--interval ]
7 个赞
Is there any reason we couldn’t/shouldn’t just call this from the ros2 topic hz Python implementation? Using subprocess or whatever. I can’t think of any.
2 个赞
Yeah, it may make sense to just gut the hz implementation in favor of something like this. I think it’s been spitballed before but never actually written down in a “we should do this” format somewhere.
2 个赞
I like the idea of having an implementation like this in the core that just works correctly instead of having to tell some of my more junior coworkers “don’t use ros2 topic hz, measure some other more accurate way”.
Having had a quick glance at the code, there are also potentially some latency improvements to be had by using the new events executor instead of rclcpp::spin()
1 个赞
peci1 2026 年9 月 15 日 09:33 5
We also have something similar, but written as a component that can utilize intraprocess comms. It’s not (yet) exactly a drop-in replacement for ros2 topic hz because it doesn’t compute the rate, it only counts received messages/bytes and prints the numbers. But it’s a simple change to add the frequency reports.
The tool can also specify the QoS of the subscription (autodetection/qos matching is somewhere on my todolist).
CLI usage (the report parameter is important, without it the tool prints nothing and instead sets the message counts as ROS parameters count and bytes):
$ ros2 run cras_topic_tools count_messages topic [qos-profile] --report 1.0
Received 10 messages in 1 s (total 10 messages, 20000 bytes).
Received 9 messages in 1 s (total 19 messages, 38000 bytes).
Launch XML usage:
<composable_node pkg="cras_topic_tools" plugin="cras::CountMessagesComponent" name="watch_lidar">
<param name="use_params" value="false" />
<param name="topic_statistics" value="true" />
<param name="report_interval" value="1.0" />
<param name="qos_profile" value="SENSOR_DATA" />
<remap from="input" to="/livox/lidar_front" />
<extra_arg name="use_intra_process_comms" value="true"/>
</composable_node>
The tool also subscribes to a generic reset topic which can reset the accumulated count of messages (automatic time jump resets are on todo list).
Package: ros-${ROS_DISTRO}-cras-topic-tools (Jazzy, Kilted, Lyrical, Rolling (not RHEL10))
3 个赞
There is another related issue around hz with rosidl::Buffer: an observation-only subscriber such as ros2 topic hz, can itself force the publisher onto a CPU fallback path. Ideally, hz could subscribe/count arrivals without materializing either a typed message or serialized payload.
I conducted a simple experiment (yet another hz implementation, though) where the ptblisher are not enforced to use fallback path (such as CUDA to CPU fallback), to measure the correct frequency. It’s AI driven commit, and far from ideal.
1 个赞
peci1 2026 年9 月 20 日 08:54 7
Yeah, this can get pretty complex. In C++, by subscribing raw messages, you actually get serialization penalty for topics published as unique ptr even when using intraprocess comms.
I think the most performant implementation has to thoroughly check all publishers, whether they are intraprocess or buffer-aware, and construct the subscriptions with great care to not force any of the publishers to do extra work.
A data point for #6 and #7, since the observer cost can be measured. On Humble with CycloneDDS, attaching ros2 topic hz to one of fifteen intra-process topics raised the watched process’s CPU from 0.303 s to 0.460 s per 10 s run (+52 %, higher in 10 of 10 trials), because the first out-of-process subscriber makes the publisher serialize every message. hz itself cost 7.1 % of a core on a 100 KB topic at 50 Hz, and echo 31.3 %. Method, CSV and plots: Watching a ROS 2 topic changes it - ros2_pulse
One way around the question of how to subscribe carefully is not to subscribe at all. The tracetools hooks in stock binaries already fire on publish (rclcpp_publish, rclcpp_intra_publish) and on callback start, so counting there gives rates, intra-process included, with no endpoint on the graph and nothing for a publisher to adapt to. I did that as an LD_PRELOAD shim (ros2_pulse, Ros2_pulse: per-topic rates and node liveness, including intra-process, on stock binaries); the measured cost was +1.9 % ± 0.7 % CPU.
The tradeoffs: it has to be loaded into the processes you want to see, so it is not a drop-in for pointing hz at an arbitrary running system, and it only counts, it never sees message contents. For the buffer-aware case in #6 that could be useful, since nothing subscribes there is nothing to trigger a CPU fallback, but I have not tested it with rosidl::Buffer publishers.
Just tried it out ros2_pulse. Great tool, easy to use, should be part of everybody’s toolkit.
2 个赞
来源:Open Robotics Blog · discourse.openrobotics.org