Gprof: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
No edit summary
No edit summary
Line 4: Line 4:
[https://sourceware.org/binutils/docs/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.
[https://sourceware.org/binutils/docs/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.


[https://sourceware.org/binutils/docs/gprof/ Gprof]  comes with the GNU compiler (such as GCC or GFORTRAN) and is available with the <pre>gcc</pre> module on Compute Canada clusters.
[https://sourceware.org/binutils/docs/gprof/ Gprof]  comes with the GNU compiler (such as GCC or GFORTRAN) and is available with the <tt>gcc</tt> module on Compute Canada clusters.


== Preparing your application ==
== Preparing your application ==

Revision as of 14:56, 2 October 2020


This article is a draft

This is not a complete article: This is a draft, a work in progress that is intended to be published into an article, which may or may not be ready for inclusion in the main wiki. It should not necessarily be considered factual or authoritative.



GNU Profiler (gprof)[edit]

What is Gprof ?[edit]

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[edit]

Switch to GNU compiler[edit]

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

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

Compile your code[edit]

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 a "-pg" option on compilation. This option tells the compiler to generate extra code to write profile information suitable for the analysis. If it is not in your compiler options no call-graph data will be gathered and if you run gprof hopping to get the profiling you may get the following error:

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

Execute your code[edit]

Once your code is compiled with the proper options, you execute it:

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

You execute your code the same way as you would do it without Gprof profiling. In fact, the execution line does not change. Once the binary is executed, a new file 'gmon.out' is generated in the current working directory. Note that if your code changes current directory, then gmon.out will be created in the new working directory. Furthermore, your program should have sufficient permissions for gmon.out to be generated.

Get the profiling data[edit]

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 notice that the new file analysis.txt was generated.