I'm trying to understand how the -pg
(or -p
) flag works when compiling C code with gcc
.
The official gcc documentation only states:
-pg
Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
This really interests me, as I'm doing a small research on profilers - trying to pick the best tool for the job.
This link gives a brief explanation of how gprof works.
This link gives an extensive critique of it. (Check my answer to the archived question.)
Compiling with
-pg
instruments your code so thatgprof
reports detailed information, see gprof's manual, 9.1 Implementation of ProfilingPlease note that with such an instrumenting profiler, you're profiling the same code you would compile in release without profiling instrumentation. There is an overhead associated with the instrumentation code itself. Also, the instrumentation code may alter instruction and data cache usage.
Contrary to an instrumenting profiler, a sampling profiler like Intel VTune works on non instrumented code by looking at the target program's program counter at regular intervals using operating system interrupts. It can also query special CPU registers to give you even more insight of what's going on.
See also Profilers Instrumenting Vs Sampling
From this source: https://elinux.org/images/0/0c/Bird-LS-2009-Measuring-function-duration-with-ftrace.pdf :
" Instrumentation comes in two main forms—explicitly declared tracepoints, and implicit tracepoints. Explicit tracepoints consist of developer defined declarations which specify the location of the tracepoint, and additional information about what data should be collected at a particular trace site. Implicit tracepoints are placed into the code automatically by the compiler, either due to compiler flags or by developer redefinition of commonly used macros.
To instrument functions implicitly, when the kernel is configured to support function tracing, the kernel build system adds -pg to the flags used with the compiler. This causes the compiler to add code to the prologue of each function, which calls a special assembly routine called mcount. This compiler option is specifically intended to be used for profiling and tracing purposes. "