Complile C++ for Profiling

I want to compile my C++ project (using CMAKE)
I want to use all the perfomance optimaizations of -DCMAKE_BUILD_TYPE=Release
Except striping it of debug linkining (to allow the profiler to record the function calls with names)

I am using GCC 10.2 profilers: perf and callgrind

I would use RelWithDebInfo. I think it is usually -O2 rather than -O3, but it keeps debugging information. For profiling, you’ll need to manually add other flags like -fprofile-arcs and (probably) other compiler-specific flags (for gprof). perf and callgrind generally work with a plain RelWithDebInfo build (at least that’s how I use them).

1 Like

Callgrind works with:

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && make && valgrind --tool=callgrind ./testWarehouse -c ../config.yaml

but perf does not

cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. && make && perf record -g ./testWarehouse -c ../config.yaml

That seems like some optimization getting tracked. You could try decreasing the optimization level perhaps? There are probably some -f and/or -g flags which can keep more debugging information around for perf.