|
| 1 | +--- |
| 2 | +title: Getting started with ROS 2 tracing |
| 3 | +author: christophebedard |
| 4 | +--- |
| 5 | + |
| 6 | +1. [Introduction](#introduction) |
| 7 | +2. [Setup](#setup) |
| 8 | +3. [Simple tracing example](#simple-tracing-example) |
| 9 | +4. [Callback duration analysis](#callback-duration-analysis) |
| 10 | +5. [Upcoming work](#upcoming-work) |
| 11 | + |
| 12 | +## Introduction |
| 13 | + |
| 14 | +Robotic systems can be hard to analyze and debug, and one big reason is that internal processing is always changing in response to sensory input. Therefore, the ability to continuously monitor and record data about the robotic software is important, to make sure it behaves deterministically, stays within resource limits, and also for later analysis. |
| 15 | + |
| 16 | +On modern systems, the operating system and other running software has a big influence on the exact execution of the software. Therefore, we also need information about these aspects. |
| 17 | + |
| 18 | +Tracing is a well-established method that allows to record run-time data, which is already well integrated with operating systems. For example, we can trace when a process is being scheduled, or when I/O occurs. Current tracing systems have minimal overhead and are very configurable to reduce overhead (and data size) even further. |
| 19 | + |
| 20 | +This post aims to introduce our ongoing effort to instrument ROS 2 and provide trace analysis tools. I'll show how we can use the instrumentation and the current analysis tools to plot callback durations, like the plot shown below. |
| 21 | + |
| 22 | +<center> |
| 23 | +<img src="/img/tutorials/tracing_result_plot.png" /> |
| 24 | +</center> |
| 25 | + |
| 26 | +## Setup |
| 27 | + |
| 28 | +We'll assume you're using Ubuntu 18.04 bionic. |
| 29 | + |
| 30 | +First, let's [install LTTng](https://lttng.org/docs/#doc-ubuntu-ppa). |
| 31 | + |
| 32 | +```bash |
| 33 | +$ sudo apt-add-repository ppa:lttng/stable-2.10 |
| 34 | +$ sudo apt-get update |
| 35 | +$ sudo apt-get install lttng-tools lttng-modules-dkms liblttng-ust-dev |
| 36 | +``` |
| 37 | + |
| 38 | +We'll also need these Python packages to read traces and setup a tracing session through ROS. |
| 39 | + |
| 40 | +```bash |
| 41 | +$ sudo apt-get install python3-babeltrace python3-lttng |
| 42 | +``` |
| 43 | + |
| 44 | +If the ROS 2 development tools and dependencies are not installed on your machine, install them by following the *System setup* section [here](https://index.ros.org/doc/ros2/Installation/Dashing/Linux-Development-Setup/#system-setup). |
| 45 | + |
| 46 | +Now we'll download all the necessary packages. First, create your workspace. |
| 47 | + |
| 48 | +```bash |
| 49 | +$ mkdir ~/ros2_ws |
| 50 | +$ cd ros2_ws/ |
| 51 | +``` |
| 52 | + |
| 53 | +Then clone everything using the following `.repos` file. It includes the core ROS 2 packages with instrumented versions of `rcl` and `rclcpp`, as well as the tracing tools and tracing analysis repos. |
| 54 | + |
| 55 | +```bash |
| 56 | +$ wget https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing/raw/master/all.repos |
| 57 | +$ vcs import src < all.repos |
| 58 | +``` |
| 59 | + |
| 60 | +Let's build everything and source! |
| 61 | + |
| 62 | +```bash |
| 63 | +$ colcon build --symlink-install --cmake-args " -DWITH_LTTNG=ON" |
| 64 | +$ source ./install/local_setup.bash |
| 65 | +``` |
| 66 | + |
| 67 | +## Simple tracing example |
| 68 | + |
| 69 | +Let's try tracing with a simple ping-pong example. |
| 70 | + |
| 71 | +The `tracetools_test` package contains two nodes we can use. The first node, `test_ping`, publishes messages on the `ping` topic and waits for a message on the `pong` topic before shutting down. The second node, `test_pong`, waits for a message on the `ping` topic, then sends a message on the `pong` topic and shuts down. |
| 72 | + |
| 73 | +To trace these nodes, we can use the `example.launch.py` launch file in the `tracetools_launch` package. |
| 74 | + |
| 75 | +```bash |
| 76 | +$ ros2 launch tracetools_launch example.launch.py |
| 77 | +``` |
| 78 | + |
| 79 | +<center> |
| 80 | +<img src="/img/tutorials/tracing_launch.svg" style="padding: 10px;" /> |
| 81 | +</center> |
| 82 | + |
| 83 | +As shown above, you should see a few output lines, and that's it. |
| 84 | + |
| 85 | +You can take a look at the trace's events using `babeltrace`. |
| 86 | + |
| 87 | +```bash |
| 88 | +$ cd ~/.ros/tracing/ |
| 89 | +$ babeltrace my-tracing-session/ |
| 90 | +``` |
| 91 | + |
| 92 | +If you only want to see the ROS events, you can instead do: |
| 93 | + |
| 94 | +```bash |
| 95 | +$ babeltrace my-tracing-session/ust/ |
| 96 | +``` |
| 97 | + |
| 98 | +<center> |
| 99 | +<img src="/img/tutorials/tracing_babeltrace.svg" style="padding: 10px;" /> |
| 100 | +</center> |
| 101 | + |
| 102 | +The last part of the `babeltrace` output is shown above. This is a human-readable version of the raw Common Trace Format (CTF) data, which is a list of events. Each event has a timestamp, an event type, some information on the process that generated the event, and the fields corresponding to the event type. The last events of our trace are pairs of `ros2:callback_start` and `ros2:callback_end` events. Each contains a reference to its corresponding callback. |
| 103 | + |
| 104 | +It's now time to process the trace data! The `tracetools_analysis` package provides tools to import and process the results. We can first convert the CTF data to a pickle file. Then we can process it to get `pandas` dataframes which we can use later to run analyses. |
| 105 | + |
| 106 | +```bash |
| 107 | +$ ros2 run tracetools_analysis convert ~/.ros/tracing/my-tracing-session/ust |
| 108 | +$ ros2 run tracetools_analysis process ~/.ros/tracing/my-tracing-session/ust/pickle |
| 109 | +``` |
| 110 | + |
| 111 | +<center> |
| 112 | +<img src="/img/tutorials/tracing_process.svg" style="padding: 10px;" /> |
| 113 | +</center> |
| 114 | + |
| 115 | +The output of the `process` command is shown above. In the last dataframe, named "Callback instances," you should see three rows. The first one is the timer callback that triggered the ping-pong sequence. The second one is the ping callback, and the third one is the pong callback! Callback function symbols are shown in the previous dataframe. |
| 116 | + |
| 117 | +This is simple, but it isn't really nice visually. We can use a Jupyter notebook to analyze the data and display the results. |
| 118 | + |
| 119 | +## Callback duration analysis |
| 120 | + |
| 121 | +Add the following line to the arguments of each of the two `Node` objects in your launch file, which should be under `ros2_ws/src/ros2/tracing/tracetools_launch/launch/`. It will stop the nodes from shutting down after 1 exchange. |
| 122 | + |
| 123 | +```python |
| 124 | +arguments=['do_more'] |
| 125 | +``` |
| 126 | + |
| 127 | +Delete the previous trace directory, and execute the launch file again. Let it run for some time (e.g. 10-20 seconds), then kill it with `Ctrl+C`. |
| 128 | + |
| 129 | +To run an analysis that displays durations of callbacks over time, use [this Jupyter notebook](https://gitlab.com/micro-ROS/ros_tracing/tracetools_analysis/blob/master/tracetools_analysis/analysis/callback_duration.ipynb), which should be under `ros2_ws/src/tracetools_analysis/tracetools_analysis/analysis/`. |
| 130 | + |
| 131 | +The resulting plots for the `/ping` and `/pong` subscriptions are shown below. We can see that the durations vary greatly. |
| 132 | + |
| 133 | +<center> |
| 134 | +<img src="/img/tutorials/tracing_analysis_plots.png" /> |
| 135 | +</center> |
| 136 | + |
| 137 | +## Upcoming work |
| 138 | + |
| 139 | +Now that the groundwork is done, the next steps are: |
| 140 | + |
| 141 | +* Submit PRs for `rcl` and `rclcpp` instrumentation. We are aiming for the instrumentation to be included in ROS 2 Eloquent (November 2019). |
| 142 | +* Add more analyses. |
| 143 | +* Test on real hardware, and compare analysis results to other tools. |
| 144 | +* Release first version of packages. |
| 145 | +* Work on providing swappable `tracetools` packages (a default package with tracing being disabled, and another one with tracing being enabled). |
| 146 | + |
| 147 | +The tracing packages can be found [here](https://gitlab.com/micro-ROS/ros_tracing/ros2_tracing). The analysis tools can be found [here](https://gitlab.com/micro-ROS/ros_tracing/tracetools_analysis). |
| 148 | + |
| 149 | +Let us know if you have any questions, or if you'd like to get involved! |
0 commit comments