How To Install Gprof On Ubuntu Linux
Box, then follow the below steps to install the Ubuntu in Windows 7, using Virtual. This will open a typical Oracle VM Virtual. Box manager window. How To Install Ubuntu. Here you should give the name of your Virtual. Box OS and type of your OS. In our case, I given the name as. Version you can select as Ubuntu from the drop down menu.
Profiling is an important aspect of software programming. Through profiling one can determine the parts in program code that are time consuming and need to be re-written. This helps make your program execution faster which is always desired.
In very large projects, profiling can save your day by not only determining the parts in your program which are slower in execution than expected but also can help you find many other statistics through which many potential bugs can be spotted and sorted out.
In this article, we will explore the GNU profiling tool ‘gprof’.
How to use gprof
Using the gprof tool is not at all complex. You just need to do the following on a high-level:
- Have profiling enabled while compiling the code
- Execute the program code to produce the profiling data
- Run the gprof tool on the profiling data file (generated in the step above).
The last step above produces an analysis file which is in human readable form. This file contains a couple of tables (flat profile and call graph) in addition to some other information. While flat profile gives an overview of the timing information of the functions like time consumption for the execution of a particular function, how many times it was called etc. On the other hand, call graph focuses on each function like the functions through which a particular function was called, what all functions were called from within this particular function etc So this way one can get idea of the execution time spent in the sub-routines too.
Lets try and understand the three steps listed above through a practical example. Following test code will be used throughout the article :
Note that the ‘for’ loops inside the functions are there to consume some execution time.
Step-1 : Profiling enabled while compilation
In this first step, we need to make sure that the profiling is enabled when the compilation of the code is done. This is made possible by adding the ‘-pg’ option in the compilation step.
From the man page of gcc :
-pg : Generate extra code to write profile information suitable for the analysis program gprof. You must use this option when compiling the source files you want data about, and you must also use it when linking.
So, lets compile our code with ‘-pg’ option :
Please note : The option ‘-pg’ can be used with the gcc command that compiles (-c option), gcc command that links(-o option on object files) and with gcc command that does the both(as in example above).
Step-2 : Execute the code
In the second step, the binary file produced as a result of step-1 (above) is executed so that profiling information can be generated.
So we see that when the binary was executed, a new file ‘gmon.out’ is generated in the current working directory.
Note that while execution if the program changes the current working directory (using chdir) then gmon.out will be produced in the new current working directory. Also, your program needs to have sufficient permissions for gmon.out to be created in current working directory.
Step-3 : Run the gprof tool
In this step, the gprof tool is run with the executable name and the above generated ‘gmon.out’ as argument. This produces an analysis file which contains all the desired profiling information.
Note that one can explicitly specify the output file (like in example above) or the information is produced on stdout.
So we see that a file named ‘analysis.txt’ was generated.
On a related note, you should also understand how to debug your C program using gdb.
Comprehending the profiling information
As produced above, all the profiling information is now present in ‘analysis.txt’. Lets have a look at this text file :
So (as already discussed) we see that this file is broadly divided into two parts :
1. Flat profile
2. Call graph
The individual columns for the (flat profile as well as call graph) are very well explained in the output itself.
Customize gprof output using flags
There are various flags available to customize the output of the gprof tool. Some of them are discussed below:
1. Suppress the printing of statically(private) declared functions using -a
If there are some static functions whose profiling information you do not require then this can be achieved using -a option :
Now if we see that analysis file :
So we see that there is no information related to func2 (which is defined static)
2. Suppress verbose blurbs using -b
As you would have already seen that gprof produces output with lot of verbose information so in case this information is not required then this can be achieved using the -b flag.
Now if we see the analysis file :
So we see that all the verbose information is not present in the analysis file.
3. Print only flat profile using -p
In case only flat profile is required then :
Note that I have used(and will be using) -b option so as to avoid extra information in analysis output.
Now if we see that analysis output:
So we see that only flat profile was there in the output.
4. Print information related to specific function in flat profile
This can be achieved by providing the function name along with the -p option:
Now if we see that analysis output :
So we see that a flat profile containing information related to only function func1 is displayed.
5. Suppress flat profile in output using -P
If flat profile is not required then it can be suppressed using the -P option :
Now if we see the analysis output :
So we see that flat profile was suppressed and only call graph was displayed in output.
Also, if there is a requirement to print flat profile but excluding a particular function then this is also possible using -P flag by passing the function name (to exclude) along with it.
In the above example, we tried to exclude ‘func1’ by passing it along with the -P option to gprof. Now lets see the analysis output:
So we see that flat profile was displayed but information on func1 was suppressed.
6. Print only call graph information using -q
In the example above, the option -q was used. Lets see what effect it casts on analysis output:
So we see that only call graph was printed in the output.
7. Print only specific function information in call graph.
Openbasekey powershell. This is possible by passing the function name along with the -q option.
Now if we see the analysis output:
So we see that information related to only func1 was displayed in call graph.
8. Suppress call graph using -Q
If the call graph information is not required in the analysis output then -Q option can be used.
Now if we see the analysis output :
So we see that only flat profile is there in the output. The whole call graph got suppressed.
Also, if it is desired to suppress a specific function from call graph then this can be achieved by passing the desired function name along with the -Q option to the gprof tool.
In the above example, the function name func1 is passed to the -Q option.
Now if we see the analysis output:
So we see that call graph information related to func1 was suppressed.
If you enjoyed this article, you might also like.
Next post: 11 Linux diff3 Command Examples (Compare 3 Files Line by Line)
Previous post: Journey of a Data Packet in the Internet
- вторник 21 апреля
- 11