Using Tracy Profiler
Tracy Profiler is a fantastic tool for profiling C++ applications and is something I have recently started integrating into my workflow. This guide serves as a quick guide on how to integrate it with an existing application and get your first profile. Note that these instructions assume that you are using Ubuntu 22.04 and that your project uses CMake.
Tracy Profiler has two parts, a set of macros that are included into your project and a server that receives the profiling data and displays it.
Project Integration
From the root of your project, clone the Tracy Profiler repository into a subdirectory of your project (master branch should be fine).
git clone https://github.com/wolfpld/tracy
Then update your project’s CMakeLists.txt as such:
include_directories(
include
tracy/public
...
)
option( TRACY_ENABLE "" ON)
add_subdirectory(tracy)
add_executable(<your_project>
tracy/public/TracyClient.cpp
...
)
target_link_libraries(<your_project>
Tracy::TracyClient
...
)
Compile your project in debug mode.
mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=DEBUG ..
make
Building the profile viewer
Install dependencies This will compile the viewer from source, which guarantees that the viewer is compatible with the version of profiler you are using. This will build for an X11 based system (legacy mode, not for Wayland), see the PDF documentation for Wayland-based build.
sudo apt install libglfw3-dev libfreetype-dev libcapstone-dev libdbus-1-dev
Build for X11 (won’t work on Wayland)
cd tracy/profiler/build/unix
make LEGACY=1
Build for Wayland (won’t work on X11)
cd tracy/profiler/build/unix
make
This should create a binary called Tracy-release
in the tracy/profiler/build/unix
directory.
Execute this binary to start the viewer.
./Tracy-release
You should now have a profile viewer running on your machine like:
Profiling your application
Now that you have the viewer built (and running) it’s time to profile your application. Running your application under sudo is required to allow the profiler to access the system’s performance counters.
cd build
sudo ./<your_project_executable>
You should now be able to see a profile like: