Profiling with gprof

Profiling with gprof

The gprof program is a profiling tool found in most Linux environments. It collects performance metrics using sampling techniques as well as instrumentation. It requires your program to be compiled with a supporting compiler such as the GNU compiler.

Prerequisite knowledge

To use gprof on Pawsey supercomputers you should first be familiar with using it in your own development environment.

Overview

The following is an overview of the process for using gprof to profile your program:

  1. Enable profiling in your program by compiling and linking with appropriate flags for the compiler.

  2. (Optional) Set environment variables for MPI programs to set output profile filenames.

  3. Run your program as usual, and a file containing profiling information will be generated.

  4. Analyse the output of this file using the gprof command.

This page provides a step-by-step example of using the gprof  tool to generate profiling information regarding the performance of a program.

Steps

  1. Compile and link the source code using the -pg compiler flag, for example:

  2. (Optional) Set the profile output filename prefix in your jobscript if your program uses MPI for parallelism:

  3. Run the program to generate the output file(s)

  4. View the output with gprof . For non-MPI programs use the single gmon.out file:

    For MPI programs, specify a particular gmon.out.*  file if a particular process is of interests, or all of them for combined output:

Result

The final gprof command will print the profiling information, which can be customised using command line options.

Example

This example profiles a MPI program which calculates the value of pi.

  1. Create a file darts-mpi.c that contains the source code:

  2. Compile and link the source code using the -pg compiler flag, for example:

  3. Create a jobscript called darts-mpi.slurm and set the GMON_OUT_PREFIX:

  4.  Run the program to generate the output file(s)

  5. View the output with gprof specifying gmon.out.*:

Next steps

The gprof  output can be customised using command line options.

Refer to the gprof manual page for more information:

Pressing q  will exit the manual pages.

Related pages