Cluster:Gprof

From Earlham CS Department
Jump to: navigation, search

Using gprof

Overview

gprof is a statistical profiler, that is it derives the information it provides by recording the address in the program counter at regular intervals over the course of a run and then reconstructing the call tree and displaying the results. gprof is part of the GNU toolchain and depends on gcc and ld to create a binary with particular characteristics.

gprof is good for finding the 20% of your program where 80% of the time is being spent. It's hard to directly pinpoint particular lines with gprof, or why the code is spending so much time there (I/O, long instruction stream, etc.), but you will know exactly where to concentrate on during a more detailed analysis.

Using gprof is a three step process, first the binary is compiled and linked with gcc using options that signal the runtime to collect statistical information. Then the program is run one or more times, each time creating a gmon.out file with run-time information. gprof is then run against the gmon.out file(s) producing one or more reports of the runtime behavior.

gprof is part of the GNU toolchain. More information about gprof and the GNU project is available at http://gnu.org.

Basic Recipe

These commands should work on any "modern" Linux distro. In all the example command lines the $ is not part of the command, it's the indicator that these are to be typed at a shell prompt.

  1. Compile your program with the -pg -g options, something like so:
$ gcc -pg -g ... -o <binary>
  1. If you link your program separately from compiling it be sure to include the ld mojo to provide the appropriate startup library.
  2. Run your program, creating gmon.out Your program must terminate with exit(0) or return(0) for gmon.out to be created.
  3. Run gprof using the gmon.out file from the previous step as input:
$ gprof --line --annotated-source --flat-profile <binary> gmon.out

A brief summary of the command line options used:

  • --line - enables line-by-line profiling
  • --annotated-source - print annotated source code
  • --flat-profile - print a flat profile
  • --graph - causes the call graph of the program to be augmented with data from the text space of the object file (assumes that there is a call graph, there won't be if there aren't any (local) function calls)

Line-by-line profiling tends to magnify statistical errors if there isn't enough run-time data. If you use this option be sure you are collecting enough data by having sufficiently large input to your program. gprof supports many options, read the man page for more information about them.

If the nature of the input changes the runtime characteristics of your code you can run it many times using a range of different inputs, saving each gmon.out file as it is created, and then have gprof analyze the whole lot of them in one fell swoop. In this mode gprof will combine the data in the run output files into one set of reports showing the sum of the runtime behaviors.

  1. Compile and link your program as above.
  2. Run your program say times, using different input and renaming the gmon.out files between each iteration (this for loop syntax assumes the Bash shell under Linux):
$ for x in {1..10}; do ./matmul-blocked --infile matmul-$x.dat; mv gmon.out gmon.out.$x; done
  1. Run gprof using the gmon.out.* files from the previous step as input:
$ gprof --line --annotated-source --flat-profile --graph <binary> gmon.out.*

This technique can also be useful with the same input stream, or a variety of input streams repeated. In both cases more data collected yields a more accurate analysis.

More Information

The man page is worth reading, it's not particularly long and includes a detailed description of how gprof works.

gcov, also part of the GNU toolchain, analyzes runtime data for code coverage. Using gcov and gprof together can yield additional insight into the software under examination.

There are many statistical and statement level profilers available. You should almost always start with whatever tool(s) are available with the development toolchain you are using; GNU, Intel, Portland Group, etc. all provide profilers for use with their kits.

Module Metadata

  • software-type profiler
  • software-package gprof
  • software-platform Linux-2.6.9-34-x86_64-GNU/Linux-RHEL_AS4, Linux-generic
  • module-version 1.0