Implementation of the gprof command

The source code must be compiled with the -pg option.

This action links in versions of library routines compiled for profiling and reads the symbol table in the named object file (a.out by default), correlating it with the call graph profile file (gmon.out by default). This means that the compiler inserts a call to the mcount() function into the object code generated for each recompiled function of your program. The mcount() function counts each time a parent calls a child function. Also, the monitor() function is enabled to estimate the time spent in each routine.

The gprof command generates two useful reports:

  • The call-graph profile, which shows the routines, in descending order by CPU time, plus their descendants. The profile permits you to understand which parent routines called a particular routine most frequently and which child routines were called by a particular routine most frequently.
  • The flat profile of CPU usage, which shows the usage by routine and number of calls, similar to the prof output.

Each report section begins with an explanatory part describing the output columns. You can suppress these pages by using the -b option.

Use -s for summaries and -z to display routines with zero usage.

Where the program is executed, statistics are collected in the gmon.out file. These statistics include the following:

  • The names of the executable program and shared library objects that were loaded
  • The virtual memory addresses assigned to each program segment
  • The mcount() data for each parent-child
  • The number of milliseconds accumulated for each program segment

Later, when the gprof command is issued, it reads the a.out and gmon.out files to generate the two reports. The call-graph profile is generated first, followed by the flat profile. It is best to redirect the gprof output to a file, because browsing the flat profile first might answer most of your usage questions.

The following example shows the profiling for the cwhet benchmark program. This example is also used in The prof command:
# cc -o cwhet -pg -lm cwhet.c
# cwhet > cwhet.out
# gprof cwhet > cwhet.gprof