Profiling Code in ROS

From MIT PR2

Jump to: navigation, search



There are lots of different ways to profile code and it might be that your favorite profiler just works. There can be issues with profilers however because they get confused by all the different ROS processes. I've found using callgrind works well. You will first need to install valgrind:

sudo apt-get install valgrind

Then run valgrind using the callgrind tool:

valgrind --tool=callgrind <executable name>

If you have long function names, it might not be possible for callgrind to demangle them. If you see a segmentation fault on callgrind startup, run without demangling:

valgrind --tool=callgrind --demangle=no <executable name>

You should demangle the names afterwards by running the file through c++filt:

c++filt < callgrind.out.<pid> > callgrind.out.<pid>.demangled

You can visualize the output using kcachegrind:

kcachegrind callgrind.out.<pid>[.demangled]

(you do want to use the demangled version).

You can just profile parts of your code by using interactive control or the callgrind macros in your code (see the callgrind documentation). If you only want to profile part of your code, start with instrumentation turned off:

valgrind --tool=callgrind --demangle=no --instr-atstart=no <executable name>

This has the advantage of running at the same speed as nulgrind until it reaches the portion it is profiling.


Actually I have never tried this with ROS, but I always have trouble remembering how to profile Python code so I am writing it down here. I suspect it will work with ROS because it is not a compile-time profiler.


  1. Install kcachegrind
    sudo apt-get install kcachegrind
  2. Install pip
    sudo apt-get install python-pip
  3. Install pyprof2calltree
    sudo pip install pyprof2calltree
  4. Install pstats
    sudo apt-get install python-profiler


  1. Run with profile data:
    python -m cProfile -o <profile file name> <script>.py
  2. View in kcachegrind:
    pyprof2calltree -i <profile file name> -k
Personal tools