Bureaucrats, cc_docs_admin, cc_staff
2,314
edits
No edit summary |
(Marked this version for translation) |
||
Line 1: | Line 1: | ||
<languages /> | <languages /> | ||
<translate> | <translate> | ||
=GNU Profiler (gprof) = | =GNU Profiler (gprof) = <!--T:1--> | ||
== What is Gprof ? == | == What is Gprof ? == | ||
[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. | ||
<!--T:2--> | |||
[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. | [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 == <!--T:3--> | ||
=== Switch to GNU compiler === | === Switch to GNU compiler === | ||
Load the appropriate GNU compiler. For example, for GCC: | Load the appropriate GNU compiler. For example, for GCC: | ||
{{Command|module load gcc/7.3.0}} | {{Command|module load gcc/7.3.0}} | ||
=== Compile your code === | === Compile 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 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: | ||
<pre> | <pre> | ||
Line 23: | Line 24: | ||
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 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. | ||
=== Get the profiling data === | === Get 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 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. | ||
{{Command|gprof /path/to/your/executable gmon.out > analysis.txt}} | {{Command|gprof /path/to/your/executable gmon.out > analysis.txt}} | ||
We can see that the new file <tt>analysis.txt</tt> has been created in the current working directory. | We can see that the new file <tt>analysis.txt</tt> has been created in the current working directory. | ||
</translate> | </translate> |