Skip to content

Instantly share code, notes, and snippets.

@mjcarroll
Created January 27, 2022 17:22
Show Gist options
  • Save mjcarroll/db992c0423c806c0ae07a3181eb2786a to your computer and use it in GitHub Desktop.
Save mjcarroll/db992c0423c806c0ae07a3181eb2786a to your computer and use it in GitHub Desktop.
Notes on Using Templight to Debug ros_ign_bridge

Motivation: ros_ign_bridge is failing to build in the ROS 2 packaging farm because the memory usage of template instantiation is too high. Specifically, the ros_subscriber is problematic.

Approach: Use templight to debug how much cpu/memory template expansion is taking

Installation of templight

Templight itself is hosted on github: https://github.com/mikael-s-persson/templight The issue is that it may not exactly line up with clang/llvm versions, and their patching seems to be hit or miss.

Install metashell

To make the installation easier, I followed the metashell instructions, as templight is a component of it. I followed: http://metashell.org/develop/build/index.html

Roughly:

git clone https://github.com/metashell/metashell
cd metashell
./install_build_dependencies.sh
mkdir -p "bin/$(tools/detect_platform.sh --id)"
cd bin/ubuntu20.04_x86_64/

mkdir templight
cd templight
cmake ../../../3rd/templight/llvm -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_TERMINFO=OFF -DLLVM_ENABLE_PROJECTS=clang 
make clang libclang templight

cd ..
mkdir metashell
cd metashell
cmake ../../..
make
sudo make install

This installs clang-12, metashell, and templight into /usr/local/bin. Could potentially clobber other stuff, but oh well.

templight tools

You need the converter to get their format into something digestable by other tools: https://github.com/mikael-s-persson/templight-tools

There is a broken regex that makes blacklist not work, I fixed that in my branch: https://github.com/mjcarroll/templight-tools

Roughly:

git clone https://github.com/mjcarroll/templight-tools
make

You should have bin/templight-converter

Configuring with colcon

I use a colcon defaults.yaml file.

To use it with templight, I did:

{
  "build": {
    "merge-install": true,
    "cmake-args": [
      "--no-warn-unused-cli",
      "-DCMAKE_CXX_COMPILER_LAUNCHER=ccache",
      "-DCMAKE_EXPORT_COMPILE_COMMANDS=1",
      "-DCMAKE_BUILD_TYPE=RelWithDebInfo",
      "-DCMAKE_CXX_COMPILER=templight++",
      "-DCMAKE_CXX_FLAGS=-Xtemplight -profiler -Xtemplight -memory -Xtemplight -ignore-system",
      "-DCMAKE_C_COMPILER=templight",
    ]
  },
  "test": {
    "merge-install": true,
  }
}

Building and evaluating

Do a normal colcon build. This expands the build time, so I would focus on what you are trying to diagnose.

At the end, you should get a pbf file per translation unit

You probably want a blacklist file to filter out stuff you don't want, it's a regex based on context or identifier

What I used for ros_ign:

# Filter out anything coming from the std namespace:
context ^std(::|$)

# Filter out anything coming from gtest: 
context ^testing(::|$)

context __gnu_cxx

identifier anonymous

There are also multiple filetypes supported, by kcachegrind makes the most sense.

Invocation:

./bin/templight-convert ~/workspaces/mbzirc/build/ros_ign_bridge/CMakeFiles/test_ros_subscriber.dir/test/subscribers/ros_subscriber.cpp.o.memory.trace.pbf  --blacklist=./blacklist.txt -f callgrind > ros_subscriber.callgrind
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment