Use valgrind
's tool named callgrind
. Add the following prefix to your node in launch file.
planner_server_node = Node(
package='planning',
executable='planner_server',
name='planner_server_rclcpp_node',
namespace='',
output='screen',
prefix=['valgrind --tool=callgrind --dump-instr=yes -v --instr-atstart=no'],
parameters=[params],
)
Note that starting a node in
valgrind
can slow down the program significantly. Therefore the above prefix does not start callgrind
right after you launch. It will listen for a command from you in order to start logging information. To make callgrind
to logging, in a seperate terminal do ;
callgrind_control -i on
you should be able to see something like;
PID 237154: /home/ ......
OK.
indicating the success of start of logging. Now Let your program run for a while. After you think you have "captured" enough function calls, do following to dump log file for analysis.
callgrind_control -d
A file with callgrind.out.*
regex should be dumped to your working directory. For me this was the colcon_ws
directory.
Now you can visually see the analysis with gui tool kcachegrind;
kcachegrind callgrind.out.*
At upper left side select ELF object, locate your executable or library and click.
You can now see each function call and their execution costs from ELF object that you selected. Try to determine the function that takes unexpected amount of time and see whether there is room for optimization.
For further information on how to interpret the results with kcachegrind, see here