0% found this document useful (0 votes)
62 views4 pages

Profiling A C Program Using GPROF: $ GCC - Wall - C - PG Program - Name.c $ GCC - Wall - PG Program - Name.o

The GNU profiler gprof is a tool for measuring the performance of C programs on a per-function basis. It records the number of calls and time spent in each function when the program is compiled and linked with the -pg option. Running the instrumented executable generates profiling data that gprof analyzes to identify functions that consume the most runtime. Gprof provides profiles that list functions and their costs, and a call graph showing which functions call others. This allows optimizing the program by focusing on high-cost functions.

Uploaded by

Sandeep Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views4 pages

Profiling A C Program Using GPROF: $ GCC - Wall - C - PG Program - Name.c $ GCC - Wall - PG Program - Name.o

The GNU profiler gprof is a tool for measuring the performance of C programs on a per-function basis. It records the number of calls and time spent in each function when the program is compiled and linked with the -pg option. Running the instrumented executable generates profiling data that gprof analyzes to identify functions that consume the most runtime. Gprof provides profiles that list functions and their costs, and a call graph showing which functions call others. This allows optimizing the program by focusing on high-cost functions.

Uploaded by

Sandeep Reddy
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Profiling a C Program using GPROF

The GNU profiler gprof is a useful tool for measuring the performance of a program--it
records the number of calls to each function and the amount of time spent there, on a per-
function basis. Functions which consume a large fraction of the run-time can be identified
easily from the output of gprof. Efforts to speed up a program should concentrate first on
those functions which dominate the total run-time.

To use profiling, the program must be compiled and linked with the -pg profiling option:

$ gcc -Wall -c -pg program_name.c


$ gcc -Wall -pg program_name.o

This creates an executable which contains additional instructions that record the time spent in
each function.

If the program consists of more than one source file then the -pg option should be used when
compiling each source file, and used again when linking the object files to create the final
executable.

The executable must be run to create the profiling data:

$ gprof a.out

Gprof Command Summary:


gprof options [executable-file [profile-data-files...]] [> outfile]

 Here square-brackets indicate optional arguments.

 If you omit the executable file name, the file `a.out' is used.


 If you give no profile data file name, the file `gmon.out' is used.

 If any file is not in the proper format, or if the profile data file does not appear to
belong to the executable file, an error message is printed.

Options in Gprof:

-a
The `-a' option causes gprof to suppress the printing of statically declared (private)
functions. (These are functions whose names are not listed as global, and which are
not visible outside the file/function/block where they were defined.) Time spent in
these functions, calls to/from them, etc, will all be attributed to the function that was
loaded directly before it in the executable file. This option affects both the flat profile
and the call graph.
-e function_name
The `-e function' option tells gprof to not print information about the
function function_name (and its children...) in the call graph. The function will still be
listed as a child of any functions that call it, but its index number will be shown
as `[not printed]'. More than one `-e' option may be given; only
one function_name may be indicated with each `-e' option.
-E function_name
The -E function option works like the -e option, but time spent in the function (and
children who were not called from anywhere else), will not be used to compute the
percentages-of-time for the call graph. More than one `-E' option may be given; only
one function_name may be indicated with each `-E' option.
-f function_name
The `-f function' option causes gprof to limit the call graph to the
function function_name and its children (and their children...). More than one `-
f' option may be given; only onefunction_name may be indicated with each `-f' option.
-F function_name
The `-F function' option works like the -f option, but only time spent in the function
and its children (and their children...) will be used to determine total-time and
percentages-of-time for the call graph. More than one `-F' option may be given; only
one function_name may be indicated with each `-F' option. The `-F' option overrides
the `-E' option.
-k from... to...
The `-k' option allows you to delete from the profile any arcs from routine from to
routine to.
-v
The `-v' flag causes gprof to print the current version number, and then exit.
-z
If you give the `-z' option, gprof will mention all functions in the flat profile, even
those that were never called, and that had no time spent in them. This is useful in
conjunction with the `-c' option for discovering which routines were never called.
-b
If the `-b' option is given, gprof doesn't print the verbose blurbs that try to explain the
meaning of all of the fields in the tables. This is useful if you intend to print out the
output, or are tired of seeing the blurbs.
-c
The `-c' option causes the static call-graph of the program to be discovered by a
heuristic which examines the text space of the object file. Static-only parents or
children are indicated with call counts of `0'.
-d num
The `-d num' option specifies debugging options.
-s
The `-s' option causes gprof to summarize the information in the profile data files it
read in, and write out a profile data file called `gmon.sum', which contains all the
information from the profile data files that gprof read in. The file `gmon.sum' may be
one of the specified input files; the effect of this is to merge the data in the other input
files into `gmon.sum'. See section Statistical Inaccuracy ofgprof Output.

Eventually you can run gprof again without `-s' to analyze the cumulative data in the
file `gmon.sum'.

-T
The `-T' option causes gprof to print its output in "traditional" BSD style.

The order of these options does not matter.

Sample Profiles of gprof:


Flat profile:

Each sample counts as 0.01 seconds.


% cumulative self self total
time seconds seconds calls ms/call ms/call name
33.34 0.02 0.02 7208 0.00 0.00 open
16.67 0.03 0.01 244 0.04 0.12 offtime
16.67 0.04 0.01 8 1.25 1.25 memccpy
16.67 0.05 0.01 7 1.43 1.43 write
16.67 0.06 0.01 mcount
0.00 0.06 0.00 236 0.00 0.00 tzset
0.00 0.06 0.00 192 0.00 0.00 tolower
0.00 0.06 0.00 47 0.00 0.00 strlen
0.00 0.06 0.00 45 0.00 0.00 strchr
0.00 0.06 0.00 1 0.00 50.00 main
0.00 0.06 0.00 1 0.00 0.00 memcpy
0.00 0.06 0.00 1 0.00 10.11 print
0.00 0.06 0.00 1 0.00 0.00 profil
0.00 0.06 0.00 1 0.00 50.00 report
The functions are sorted by decreasing run-time spent in them.

The call graph shows how much time was spent in each function and its children.
From this information, you can find functions that, while they themselves may not
have used much time, called other functions that did use unusual amounts of
time.
granularity: each sample hit covers 2 byte(s) for 20.00% of 0.05 seconds

index % time self children called name


<spontaneous>
[1] 100.0 0.00 0.05 start [1]
0.00 0.05 1/1 main [2]
0.00 0.00 1/2 on_exit [28]
0.00 0.00 1/1 exit [59]
-----------------------------------------------
0.00 0.05 1/1 start [1]
[2] 100.0 0.00 0.05 1 main [2]
0.00 0.05 1/1 report [3]
-----------------------------------------------
0.00 0.05 1/1 main [2]
[3] 100.0 0.00 0.05 1 report [3]
0.00 0.03 8/8 timelocal [6]
0.00 0.01 1/1 print [9]
0.00 0.01 9/9 fgets [12]
0.00 0.00 12/34 strncmp <cycle 1> [40]
0.00 0.00 8/8 lookup [20]
0.00 0.00 1/1 fopen [21]
0.00 0.00 8/8 chewtime [24]
0.00 0.00 8/16 skipspace [44]
-----------------------------------------------
[4] 59.8 0.01 0.02 8+472 <cycle 2 as a whole> [4]
0.01 0.02 244+260 offtime <cycle 2> [7]
0.00 0.00 236+1 tzset <cycle 2> [26]
-----------------------------------------------
The lines full of dashes divide this table into entries, one for each function. Each
entry has one or more lines.

The entries are sorted by time spent in the function and its subroutines.

SAMPLE WITH ANNOTATED PROGAM:

SAMPLE WITH ANNOTATED PROGRAM.

You might also like