top of page
Writer's pictureSunil Kumar Yadav

gprof: How To Use GNU Profiling Tool?

Efficiency in software engineering is associated with reliability, speed, and programming methodology used in developing software for an application. Code efficiency is directly linked with algorithmic efficiency and the speed of runtime execution for software. It is the key element in ensuring high performance. Efficient code reduces resource consumption and improves completion time as much as possible with minimal risk to the business and operating environment.



In order to develop efficient programs, developers need to use specialized tools to understand the internals of the overall program. Profiling is one of the important aspects of software programming. With the help of profiling tools, engineers can determine which part of the software is consuming more time and whether such routines or functions need to be re-written or not. Usage of profiling tools in very large projects not only provides which part of the program is running slower at runtime but also other statistics through which many potential defects could be identified. In this article, we will explore more on GNU profiling tool gprof.


How to use gprof

Using the gprof tool is very easy. You need to simply follow below three steps:

  • Enable profiling while compiling your code

  • Execute the program to generate the profiling data

  • Run gprof on generated profiling data file

The last step is mentioned above will produce human-readable analysis data on the console, which we can redirect to file for later analysis as well. The profiling result contains a couple of tables, e.g. flat profile and the call graph. The profiling result also contains other information apart from mentioned tables. The below table contains a high-level summary of the flat profile and the call graph section of the function profiling result.

Flat profile

It gives an overview of the timing information like time consumption for the execution of a particular function and how many times it was called.

Call graph

It focuses on each function like the function through which a particular function was called and what all functions were called from within this particular function.

Let's try to understand this with the help of a simple example. The below example contains a couple of loops to simulate processes spending varying degrees of time to perform some task.

// main.c
#include<stdio.h>

void function1()
{
	printf("Inside function 1\n");
	for(int i=0; i<0x01FFFFFF; ++i)
		; // do nothing
}

void function2()
{
	printf("Inside function 2\n");
	for(int i=0; i<0xFFFFFFFF; ++i)
		; // do nothing
}

void function3()
{
	printf("Inside function 3\n");
	for(int i=0; i<0x011FFFFF; ++i)
		; // do nothing
	function2();
}

int main()
{
	function1();
	function2();
	function3();
	return 0;
}

In order to generate profiling data, we need to enable the profiling by adding the -pg flag to the gcc or g++ compiler. Below is the command-line flag used to compile and link above main.c file.

$ gcc -std=c99 -pg  main.c -o main

Once done with the above compilation stage, the current directory will contain elf file with the name main.

$ ls 
main.c main

Now we simply need to run main to generate profiling data. Below is the console output after running main from the terminal.

$ ./main
Inside function 1
Inside function 2
Inside function 3
Inside function 2

$ ls
main.c main gmon.out

As we can see after running main it has printed required messages on the console and also generated gmon.out file. In order to generate profile data, we need to run the below gprof command to view the result of profiling. Note, by default gprof data will be printed on console but using redirection operator(>) we can log the profiling results to a file for later analysis.

$ gprof main gmon.out

The output of the above command would look something like below.

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
101.16     12.33    12.33        2     6.17     6.17  function2
  0.33     12.37     0.04        1     0.04     0.04  function1
  0.17     12.39     0.02        1     0.02     0.02  function3

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

		     Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.08% of 12.39 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     99.8    0.00   12.37                 main [1]
               12.33    0.00       2/2           function2 [2]
                0.04    0.00       1/1           function1 [3]
-----------------------------------------------
               12.33    0.00       2/2           main [1]
[2]     99.5   12.33    0.00       2         function2 [2]
-----------------------------------------------
                0.04    0.00       1/1           main [1]
[3]      0.3    0.04    0.00       1         function1 [3]
-----------------------------------------------
                0.02    0.00       1/1           __libc_csu_init [5]
[4]      0.2    0.02    0.00       1         function3 [4]
-----------------------------------------------
                                                 <spontaneous>
[5]      0.2    0.00    0.02                 __libc_csu_init [5]
                0.02    0.00       1/1           function3 [4]
-----------------------------------------------

 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.
 This line lists:
     index	A unique number given to each element of the table.
		Index numbers are sorted numerically.
		The index number is printed next to every function name so
		it is easier to look up where the function is in the table.

     % time	This is the percentage of the `total' time that was spent
		in this function and its children.  Note that due to
		different viewpoints, functions excluded by options, etc,
		these numbers will NOT add up to 100%.

     self	This is the total amount of time spent in this function.

     children	This is the total amount of time propagated into this
		function by its children.

     called	This is the number of times the function was called.
		If the function called itself recursively, the number
		only includes non-recursive calls, and is followed by
		a `+' and the number of recursive calls.

     name	The name of the current function.  The index number is
		printed after it.  If the function is a member of a
		cycle, the cycle number is printed between the
		function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self	This is the amount of time that was propagated directly
		from the function into this parent.

     children	This is the amount of time that was propagated from
		the function's children into this parent.

     called	This is the number of times this parent called the
		function `/' the total number of times the function
		was called.  Recursive calls to the function are not
		included in the number after the `/'.

     name	This is the name of the parent.  The parent's index
		number is printed after it.  If the parent is a
		member of a cycle, the cycle number is printed between
		the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self	This is the amount of time that was propagated directly
		from the child into the function.

     children	This is the amount of time that was propagated from the
		child's children to the function.

     called	This is the number of times the function called
		this child `/' the total number of times the child
		was called.  Recursive calls by the child are not
		listed in the number after the `/'.

     name	This is the name of the child.  The child's index
		number is printed after it.  If the child is a
		member of a cycle, the cycle number is printed
		between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 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.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

Index by function name

   [3] function1               [2] function2               [4] function3

As you can see from the above output, gprof generates a flat profile and control flow report. Now let's try to understand few additional options to control the amount of information generated by gprof.

1. Suppress the printing of statically(private) declared functions using -a

$ gprof -a main gmon.out 
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
 33.94     18.03    18.03        1    18.03    18.03  function2
 33.69     35.93    17.90        1    17.90    17.90  function1
 31.28     52.55    16.62        1    16.62    16.62  function3

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

		     Call graph (explanation follows)


granularity: each sample hit covers 2 byte(s) for 0.02% of 52.55 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0    0.00   52.55                 main [1]
               18.03    0.00       1/1           function2 [2]
               17.90    0.00       1/1           function1 [3]
               16.62    0.00       1/1           function3 [4]
-----------------------------------------------
               18.03    0.00       1/1           main [1]
[2]     34.3   18.03    0.00       1         function2 [2]
-----------------------------------------------
               17.90    0.00       1/1           main [1]
[3]     34.1   17.90    0.00       1         function1 [3]
-----------------------------------------------
               16.62    0.00       1/1           main [1]
[4]     31.6   16.62    0.00       1         function3 [4]
-----------------------------------------------

 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.
 This line lists:
     index	A unique number given to each element of the table.
		Index numbers are sorted numerically.
		The index number is printed next to every function name so
		it is easier to look up where the function is in the table.

     % time	This is the percentage of the `total' time that was spent
		in this function and its children.  Note that due to
		different viewpoints, functions excluded by options, etc,
		these numbers will NOT add up to 100%.

     self	This is the total amount of time spent in this function.

     children	This is the total amount of time propagated into this
		function by its children.

     called	This is the number of times the function was called.
		If the function called itself recursively, the number
		only includes non-recursive calls, and is followed by
		a `+' and the number of recursive calls.

     name	The name of the current function.  The index number is
		printed after it.  If the function is a member of a
		cycle, the cycle number is printed between the
		function's name and the index number.


 For the function's parents, the fields have the following meanings:

     self	This is the amount of time that was propagated directly
		from the function into this parent.

     children	This is the amount of time that was propagated from
		the function's children into this parent.

     called	This is the number of times this parent called the
		function `/' the total number of times the function
		was called.  Recursive calls to the function are not
		included in the number after the `/'.

     name	This is the name of the parent.  The parent's index
		number is printed after it.  If the parent is a
		member of a cycle, the cycle number is printed between
		the name and the index number.

 If the parents of the function cannot be determined, the word
 `<spontaneous>' is printed in the `name' field, and all the other
 fields are blank.

 For the function's children, the fields have the following meanings:

     self	This is the amount of time that was propagated directly
		from the child into the function.

     children	This is the amount of time that was propagated from the
		child's children to the function.

     called	This is the number of times the function called
		this child `/' the total number of times the child
		was called.  Recursive calls by the child are not
		listed in the number after the `/'.

     name	This is the name of the child.  The child's index
		number is printed after it.  If the child is a
		member of a cycle, the cycle number is printed
		between the name and the index number.

 If there are any cycles (circles) in the call graph, there is an
 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.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

Index by function name

   [3] function1               [2] function2               [4] function3

2. Suppress verbose blurbs using -b

$ gprof -b main gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
101.16     12.33    12.33        2     6.17     6.17  function2
  0.33     12.37     0.04        1     0.04     0.04  function1
  0.17     12.39     0.02        1     0.02     0.02  function3

			Call graph


granularity: each sample hit covers 2 byte(s) for 0.08% of 12.39 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     99.8    0.00   12.37                 main [1]
               12.33    0.00       2/2           function2 [2]
                0.04    0.00       1/1           function1 [3]
-----------------------------------------------
               12.33    0.00       2/2           main [1]
[2]     99.5   12.33    0.00       2         function2 [2]
-----------------------------------------------
                0.04    0.00       1/1           main [1]
[3]      0.3    0.04    0.00       1         function1 [3]
-----------------------------------------------
                0.02    0.00       1/1           __libc_csu_init [5]
[4]      0.2    0.02    0.00       1         function3 [4]
-----------------------------------------------
                                                 <spontaneous>
[5]      0.2    0.00    0.02                 __libc_csu_init [5]
                0.02    0.00       1/1           function3 [4]
-----------------------------------------------

Index by function name

   [3] function1               [2] function2               [4] function3

3. Print only flat profile using -p

$ gprof -p main gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
101.16     12.33    12.33        2     6.17     6.17  function2
  0.33     12.37     0.04        1     0.04     0.04  function1
  0.17     12.39     0.02        1     0.02     0.02  function3

 %         the percentage of the total running time of the
time       program used by this function.

cumulative a running sum of the number of seconds accounted
 seconds   for by this function and those listed above it.

 self      the number of seconds accounted for by this
seconds    function alone.  This is the major sort for this
           listing.

calls      the number of times this function was invoked, if
           this function is profiled, else blank.

 self      the average number of milliseconds spent in this
ms/call    function per call, if this function is profiled,
	   else blank.

 total     the average number of milliseconds spent in this
ms/call    function and its descendents per call, if this
	   function is profiled, else blank.

name       the name of the function.  This is the minor sort
           for this listing. The index shows the location of
	   the function in the gprof listing. If the index is
	   in parenthesis it shows where it would appear in
	   the gprof listing if it were to be printed.

Copyright (C) 2012-2020 Free Software Foundation, Inc.

Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved.

4. Print information related to a specific function in a flat profile

$ gprof -pfunc1 -b main gmon.out 
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
101.67     12.33    12.33        2     6.17     6.17  function2

5. Suppress flat profile in output using -P

$ gprof -P -b main gmon.out
   Call graph


granularity: each sample hit covers 2 byte(s) for 0.08% of 12.39 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     99.8    0.00   12.37                 main [1]
               12.33    0.00       2/2           function2 [2]
                0.04    0.00       1/1           function1 [3]
-----------------------------------------------
               12.33    0.00       2/2           main [1]
[2]     99.5   12.33    0.00       2         function2 [2]
-----------------------------------------------
                0.04    0.00       1/1           main [1]
[3]      0.3    0.04    0.00       1         function1 [3]
-----------------------------------------------
                0.02    0.00       1/1           __libc_csu_init [5]
[4]      0.2    0.02    0.00       1         function3 [4]
-----------------------------------------------
                                                 <spontaneous>
[5]      0.2    0.00    0.02                 __libc_csu_init [5]
                0.02    0.00       1/1           function3 [4]
-----------------------------------------------

Index by function name

   [3] function1               [2] function2               [4] function3

6. Print only call graph information using -q

$ gprof -q -b main gmon.out 
Call graph


granularity: each sample hit covers 2 byte(s) for 0.08% of 12.39 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]     99.8    0.00   12.37                 main [1]
               12.33    0.00       2/2           function2 [2]
                0.04    0.00       1/1           function1 [3]
-----------------------------------------------
               12.33    0.00       2/2           main [1]
[2]     99.5   12.33    0.00       2         function2 [2]
-----------------------------------------------
                0.04    0.00       1/1           main [1]
[3]      0.3    0.04    0.00       1         function1 [3]
-----------------------------------------------
                0.02    0.00       1/1           __libc_csu_init [5]
[4]      0.2    0.02    0.00       1         function3 [4]
-----------------------------------------------
                                                 <spontaneous>
[5]      0.2    0.00    0.02                 __libc_csu_init [5]
                0.02    0.00       1/1           function3 [4]
-----------------------------------------------

Index by function name

   [3] function1               [2] function2               [4] function3

7. Print only specific function information in the call graph.

$ gprof -qfunc1 -b main gmon.out
			Call graph


granularity: each sample hit covers 2 byte(s) for 0.08% of 12.39 seconds

index % time    self  children    called     name
               12.33    0.00       2/2           main (1)
[2]     99.5   12.33    0.00       2         function2 [2]
-----------------------------------------------

Index by function name

   (3) function1               [2] function2               (4) function3

8. Suppress call graph using -Q

$ gprof -Q -b main gmon.out
Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls   s/call   s/call  name    
101.16     12.33    12.33        2     6.17     6.17  function2
  0.33     12.37     0.04        1     0.04     0.04  function1
  0.17     12.39     0.02        1     0.02     0.02  function3

Conclusion

From the above simple example, we can visualize many important metrics which will help us to optimize our application better and improve the overall efficiency of ours application.



Recent Posts

See All

Comments


bottom of page