Gprof: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 12: Line 12:


=== Compile your code ===
=== Compile your code ===
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 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:
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>
gprof: gmon.out file is missing call-graph data
gprof: gmon.out file is missing call-graph data
</pre>
</pre>
=== Execute your code ===
=== Execute your code ===
Once your code is compiled with the proper options, you 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 execute your code the same way as you would do it without Gprof profiling. In fact, the execution line does not change.
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 is executed, a new file 'gmon.out' is generated in the current working directory.
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.
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 ===
=== Get 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.
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 notice that the new file analysis.txt was generated.
We can notice that the new file analysis.txt was generated.
Bureaucrats, cc_docs_admin, cc_staff
2,318

edits