Gprof: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
No edit summary
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Draft}}
<languages />
=GNU Profiler (gprof) =
<translate>
== What is Gprof ? ==
=GNU Profiler (gprof) = <!--T:1-->
[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.
== What is gprof? ==
[https://sourceware.org/binutils/docs/gprof/ gprof] is a profiling software which collects information and compiles statistics on your code. Generally, it searches for functions and subroutines in your program and inserts timing instructions for each one. Executing such a 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 <tt>gcc</tt> module on Compute Canada clusters.
<!--T:2-->
[https://sourceware.org/binutils/docs/gprof/ gprof] comes with the GNU compiler suite and is available with the <tt>gcc</tt> module on our clusters.


== Preparing your application ==
== Preparing your application == <!--T:3-->
=== Switch to GNU compiler ===
=== Loading the GNU compiler ===
Load the appropriate GNU compiler. For example, for GCC:
Load the appropriate GNU compiler. For example, for GCC:
{{Command|module load gcc/5.4.0}}
{{Command|module load gcc/7.3.0}}


=== Compile your code ===
=== Compiling your code === <!--T:4-->
To get useful information from [https://sourceware.org/binutils/docs/gprof/ Gprof] , you first need to compile your code with debugging information enabled. With the GNU compilers, you do so by adding the <tt>-pg</tt> 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:
To get useful information from [https://sourceware.org/binutils/docs/gprof/ gprof], you first need to compile your code with debugging information enabled. With the GNU compilers, you do so by adding the <tt>-pg</tt> option to the compilation command. This option tells the compiler to generate extra code to write profile information suitable for the analysis. Without this option, no call-graph data will be gathered and you may get the following error:
<pre>
<pre>
gprof: gmon.out file is missing call-graph data
gprof: gmon.out file is missing call-graph data
</pre>
</pre>
=== Execute your code ===
=== Executing your code ===
Once your code has been compiled with the proper options, you then execute it:
Once your code has been compiled with the proper options, you then execute it:
{{Command|/path/to/your/executable arg1 arg2}}
{{Command|/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.
You should run your code the same way as you would without gprof profiling; the execution line does not change.
Once the binary has been executed and finished without any errors, a new file <tt>gmon.out</tt> is created in the current working directory. Note that if your code changes current directory, then <tt>gmon.out</tt> will be created in the new working directory. Furthermore, your program should have sufficient permissions for this file to be created.
Once the binary has been executed and finished without any errors, a new file <tt>gmon.out</tt> is created in the current working directory. Note that if your code changes the current directory, <tt>gmon.out</tt> will be created in the new working directory, insofar as your program has sufficient permissions to do so.  


=== Get the profiling data ===
=== Getting the profiling data === <!--T:5-->
In this step the Gprof tool is executed again with the binary name and the above mentioned <tt>gmon.out</tt> as argument. This should create an analysis file with all the desired profiling information.
In this step the gprof tool is executed with the binary name and the above mentioned <tt>gmon.out</tt> as argument; the analysis file is created with all the desired profiling information.
 
<!--T:6-->
{{Command|gprof /path/to/your/executable gmon.out > analysis.txt}}
{{Command|gprof /path/to/your/executable gmon.out > analysis.txt}}
We can notice that the new file analysis.txt was generated.
 
</translate>

Latest revision as of 17:48, 13 October 2020

Other languages:

GNU Profiler (gprof)[edit]

What is gprof?[edit]

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

gprof comes with the GNU compiler suite and is available with the gcc module on our clusters.

Preparing your application[edit]

Loading the GNU compiler[edit]

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

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

Compiling 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 the -pg option to the compilation command. This option tells the compiler to generate extra code to write profile information suitable for the analysis. Without this option, no call-graph data will be gathered and you may get the following error:

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

Executing your code[edit]

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 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 to do so.

Getting the profiling data[edit]

In this step the gprof tool is executed with the binary name and the above mentioned gmon.out as argument; the analysis file is created with all the desired profiling information.

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