Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Excluding a function from gprof results

Tags:

c

linux

gprof

I want to exclude some functions from the output generated by gprof. In other words, I do not want them to be included when calculating percentage time spent by each function during execution. I read at one place -E option can be used.

However I'm using gprof -E function_to_be_exluded my_program_name, but nothing happens. The manual says it is depreciated and you should use symspecs instead. However, I have wasted half an hour trying to figure out how to achieve it with symspecs, but no luck. Anyone can kindly help me in this.

like image 380
MetallicPriest Avatar asked Jul 08 '11 10:07

MetallicPriest


People also ask

What does gprof do?

Gprof is a performance analysis tool used to profile applications to determine where time is spent during program execution. Gprof is included with most Unix/Linux implementations, is simple to use, and can quickly show which parts of an application take the most time (hotspots).

What is gprof in Linux?

On Unix-like operating systems, the gprof command is a software developement tool that displays call graph profile data of a compiled binary. This page covers the GNU/Linux version of gprof.

What is a GMON out file?

The `gmon. out' file is written in the program's current working directory at the time it exits. This means that if your program calls chdir , the `gmon. out' file will be left in the last directory your program chdir 'd to.

How do I know if gprof is installed?

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 purposes.


3 Answers

Exactly, gprof -e -E are deprecated and superseded by usage of newer relevant options that have argument - symspecs. So try using:

gprof --no-time=symspec 

The -n option causes "gprof", in its call graph analysis, not to propagate times for
symbols matching symspec.

e.g.

gprof --no-time=name_of_function_you_dont_want_to_profile.

Use this along with your other gprof options (-E -e definitely ruled out)

like image 81
goldenmean Avatar answered Oct 04 '22 17:10

goldenmean


According to the man:

  • for display flat profile and exclude function from it you need use -P option:

    gprof main gmon.out -Pfunction_name
    
  • for display call graph and exclude function from it you need use -Q option:

    gprof main gmon.out -Qfunction_name
    

This options can be repeated and used in the same time:

gprof main gmon.out -Pfunction_name -Qfunction_name -Qother_function_name

If you need exclude function from one report but not exclude any function from other you need use -p or -q options.

Example:

Create program:

#include <stdio.h>
#include <stdlib.h>

void func_a () {printf ("%s ",__FUNCTION__);}
void func_b () {printf ("%s ",__FUNCTION__);}
void func_c () {printf ("%s ",__FUNCTION__);}

int main ()
{
    func_a ();
    func_b ();
    func_c ();
    return EXIT_SUCCESS;
}

Compile it: gcc main.c -pg -o main

And launch:

$ ./main
func_a func_b func_c

Generate profile reports:

  • If you need print only flat profile you need call:

    $ gprof main gmon.out -b -p  
      %   cumulative   self              self     total             
     time   seconds   seconds    calls  Ts/call  Ts/call  name      
      0.00      0.00     0.00        1     0.00     0.00  func_a  
      0.00      0.00     0.00        1     0.00     0.00  func_b  
      0.00      0.00     0.00        1     0.00     0.00  func_c
    
  • If you need print flat profile excluding functions func_a and func_c and full call graph you need call:

    $ gprof main gmon.out -b -Pfunc_a -Pfunc_c -q
      %   cumulative   self              self     total           
     time   seconds   seconds    calls  Ts/call  Ts/call  name    
      0.00      0.00     0.00        1     0.00     0.00  func_b
    
    index % time    self  children    called     name
                    0.00    0.00       1/1           main [9]
    [1]      0.0    0.00    0.00       1         func_a [1]
    -----------------------------------------------
                    0.00    0.00       1/1           main [9]
    [2]      0.0    0.00    0.00       1         func_b [2]
    -----------------------------------------------
                    0.00    0.00       1/1           main [9]
    [3]      0.0    0.00    0.00       1         func_c [3]
    -----------------------------------------------
    
  • If you need print flat profile excluding functions func_a and func_c and call graph excluding func_b you need call:

    $ gprof main gmon.out -b -Pfunc_a -Pfunc_c -Qfunc_b
      %   cumulative   self              self     total           
     time   seconds   seconds    calls  Ts/call  Ts/call  name    
      0.00      0.00     0.00        1     0.00     0.00  func_b
    
    index % time    self  children    called     name
                    0.00    0.00       1/1           main [9]
    [1]      0.0    0.00    0.00       1         func_a [1]
    -----------------------------------------------
                    0.00    0.00       1/1           main [9]  
    [3]      0.0    0.00    0.00       1         func_c [3]
    -----------------------------------------------
    
like image 28
Gluttton Avatar answered Oct 04 '22 17:10

Gluttton


Unless I've misunderstood what you're asking...

gprof a.out --no-time=function_name

works for me.

like image 45
mjk Avatar answered Oct 04 '22 15:10

mjk