Title: Objectives: To Understand and Task Distribution Using Gprof.l Theory
Title: Objectives: To Understand and Task Distribution Using Gprof.l Theory
Title: Objectives: To Understand and Task Distribution Using Gprof.l Theory
Theory: Gprof is a performance analysis tool for Unix applications. It uses a hybrid of
instrumentation and sampling and was created as extended version of the older "prof" tool. Unlike
prof, gprof is capable of limited call graph collecting and printing. GPROF was originally written b
y a group led by Susan L. Graham at the University of California, Berkeley for Berkeley Unix
(4.2BSD). Another implementation was written as part of the GNU project for GNU Binutils
in 1988 by Jay Fenlason. 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
rewritten. 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. How to use gprof ?Using the gprof tool is not at
al l 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
withinthis particular function etc So this way one can get idea of the execution time spent in the
sub-routines too.. Gprof Setup and Usage. Here are some of the steps required for downloading and
setting environment for gprof : If not installed already, download and install gprof by executing
apt-get install binutils. To check that gprof is installed properly, execute the gprof
command and it should give some error like a.out: No such file or directory. Assuming that the
compiler being used it gcc or cc, compile your code with the option pg so that the executable
includes extra code for profiling purpose. Run the program in a normal way. When the program
terminates, a file named gmon.out (profiler data) would be produced in the same directory from
which the program was run. Use the gprof profiler to process this profiler data (gmon.out) and
produce human readable performance analysis and statistics of the program.
Program:
//test_gprof.c
#include<stdio.h>
void new_func1(void);
void func1(void)
{
printf("\n Inside func1 \n");
int i = 0;
for(;i<10000000;i++);
new_func1();
return;
}
int main(void)
{
printf("\n Inside main()\n");
int i = 0;
for(;i<10000000;i++);
func1();
func2();
return 0;
}
//test_gprof_new.c
#include<stdio.h>
void new_func1(void)
{
printf("\n Inside new_func1()\n");
int i = 0;
for(;i<10000000;i++);
return;
}
Output:
--------------------------------------------------------------------------------------------------------------------
comp-sys-16@compsys16:~$ gcc -Wall -pg test_gprof.c test_gprof_new.c -o test_gprof
comp-sys-16@compsys16:~$ ./test_gprof
Inside main()
Inside func1
Inside new_func1()
Inside func2
comp-sys-16@compsys16:~$
OUTPUT:
Analysis5.txt
Flat profile:
listing.
else blank.
granularity: each sample hit covers 2 byte(s) for 11.00% of 0.09 seconds
<spontaneous>
-----------------------------------------------
-----------------------------------------------
-----------------------------------------------
-----------------------------------------------
This table describes the call tree of the program, and was sorted by
the total amount of time spent in each function and its children.
Each entry in this table consists of several lines. The line with the
index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
and the lines below it list the functions this one called.
% time This is the percentage of the `total' time that was spent
For the function's parents, the fields have the following meanings:
children This is the amount of time that was propagated from the
entry for the cycle-as-a-whole. This entry shows who called the
cycle (as parents) and the members of the cycle (as children.)
The `+' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry for each member shows,
for that member, how many times it was called from other members of
the cycle.
//test_gprof_new.c
#include<stdio.h>
void new_func1(void)
1 -> {
int i = 0;
for(;i<10000000;i++);
return;
Top 10 Lines:
Line Count
5 1
Execution Summary:
1 Lines executed
//test_gprof.c
#include<stdio.h>
void new_func1(void);
void func1(void)
1 -> {
int i = 0;
for(;i<10000000;i++);
new_func1();
return;
}
static void func2(void)
1 -> {
int i = 0;
for(;i<10000000;i++);
return;
int main(void)
##### -> {
int i = 0;
for(;i<10000000;i++);
func1();
func2();
return 0;
Top 10 Lines:
Line Count
7 1
18 1
Execution Summary:
3 Lines executed