Gprof

Revision as of 20:10, 7 October 2020 by Diane27 (talk | contribs)
Other languages:

GNU Profiler (gprof)

What is gprof?

gprof is a profiling software which collects information and statistics on your code. Generally, it searches for functions and subroutines in your program and insert timing instructions for each one. Then executing such modified program creates a raw data file which can be interpreted by gprof and turned into profiling statistics.

gprof comes with the GNU compiler (such as GCC or GFORTRAN) and is available with the gcc module on Compute Canada clusters.

Preparing your application

Loading the GNU compiler

Load the appropriate GNU compiler. For example, for GCC:

Question.png
[name@server ~]$ module load gcc/7.3.0

Compiling your code

To get useful information from gprof , you first need to compile your code with debugging information enabled. With the GNU compilers, you do so by adding the -pg option during compilation. This option tells the compiler to generate extra code to write profile information suitable for the analysis. If it is not part of your compiler options then no call-graph data will be gathered. When you run gprof hoping to get the profiling data you may get the following error:

gprof: gmon.out file is missing call-graph data

Executing your code

Once your code has been compiled with the proper options, you then execute it:

Question.png
[name@server ~]$ /path/to/your/executable arg1 arg2

You should run your code the same way as you would do it without gprof profiling; the execution line does not change. Once the binary has been executed and finished without any errors, a new file gmon.out is created in the current working directory. Note that if your code changes the current directory, gmon.out will be created in the new working directory, insofar as your program has sufficient permissions for it to be created.

Getting the profiling data

In this step the gprof tool is executed again with the binary name and the above mentioned gmon.out as argument. This should create an analysis file with all the desired profiling information.

Question.png
[name@server ~]$ gprof /path/to/your/executable gmon.out > analysis.txt

We can see that the new file analysis.txt has been created in the current working directory.