0

I am trying to understand linux perf and hotspot to understand call stacks/trace of my c++ application.

Should the program compiled in debug mode or in release mode ? Assuming I have only one file inline.cpp. I have seen in one of the example using

g++ -O2 -g inline.cpp -o inline
perf record --call-graph dwarf ./inline

I am wondering is it necessary to compile the program in debug (-g) and optimization -O2 ? What are flag that the executable is compiled in order make it useful to run with perf record ?

Does it make any difference if we compile the program with out compiler flags ?

g++ inline.cpp -o inline
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
BhanuKiran
  • 2,631
  • 3
  • 20
  • 36
  • benchmarking or profiling un-optimized code is not very useful; it will have *different* bottlenecks than normal code. e.g. [Adding a redundant assignment speeds up code when compiled without optimization](https://stackoverflow.com/q/49189685) / [C loop optimization help for final assignment (with compiler optimization disabled)](https://stackoverflow.com/a/32001196) – Peter Cordes Jun 23 '21 at 14:01

1 Answers1

3

First of all, -g and -O2 aren't opposites. -g specifies that debugging symbols will be generated, so that you can associate hotspots with actual lines of code. -O2 specifies that code optimization should be performed; this is ordinarily not done with code you intend to run in a debugger because it makes it more difficult to follow the execution.

A profiler measures the performance of an executable, not of source code. If you profile an unoptimized executable you'll see where the unoptimized executable's performance problems are, but those may be different problems than in the optimized executable. Presuming you care about the performance of the optimized executable (because it's what users will ordinarily be running) you should be profiling that. The use of -O2 will definitely make it harder to understand where the performance problems are, but that's just the nature of profiling code.

Sneftel
  • 40,271
  • 12
  • 71
  • 104
  • You mean to say the program should be compiled like this `g++ -O2 -g inline.cpp -o inline` instead of `g++ inline.cpp -o inline` . In order for the executable to be useful with perf. – BhanuKiran Jun 23 '21 at 13:01
  • 1
    Exactly. If you don't have `-g` the results won't be readable; if you don't have `-O2` the results won't be relevant. – Sneftel Jun 23 '21 at 13:11
  • 1
    @BhanuKiran: You should use the same optimization options you're eventually going to build with, or that you expect users to build with. e.g. `g++ -g -O3` to enable full optimization including auto-vectorization, and maybe also `-march=native` or `-march=znver2` to enable ISA extensions like SSE4, AVX, and so on, and to set -mtune= the same thing. – Peter Cordes Jun 23 '21 at 14:05