0
votes

Here is what I got from the profile data. I can't understand why calls of main is more than once?

   %   cumulative   self              self     total           
  time   seconds   seconds    calls  Ts/call  Ts/call  name    
  0.00      0.00     0.00       96     0.00     0.00  fun
  0.00      0.00     0.00       33     0.00     0.00  __x86.get_pc_thunk.bx
  0.00      0.00     0.00       27     0.00     0.00  main

What is more strange,my code is like that

 void foo() {
  foo1();
  if ( a condition) foo2();
}

%   cumulative   self              self     total    
 time   seconds   seconds    calls  us/call  us/call  name    
 54.55      0.06     0.06     6115     9.81     9.81  foo1
 18.18      0.08     0.02   252520     0.08     0.08  cmp_by_weight
  9.09      0.09     0.01   865699     0.01     0.01  foo2

Why could the calls of foo2() is more than that of foo1()?

I have use -pg option when compile. and then use gprof -a exe gmon.out to generate profile from gmon.out.

Calls: is the total number of times the function was called. If the function was never called, or the number of times it was called cannot be determined (probably because the function was not compiled with profiling enabled), the calls field is blank.

1
Very strange. Probably some incostintencies between exe and gmon file. Try deleting both files and build again. - egur
@egur I have done this, however, got the same result. - notbad
can you post the complete src code? does your program fork()? - tristan
@tristan The complete code is a little long. But I guarantee it is a single-thread program and there is no fork(). - notbad

1 Answers

1
votes

Who knows? -pg causes gcc to insert code in the prologue of every routine B. That code looks at the stack to try to figure out who the caller A is. Then it increments a count of how many times A called B.

You can see how easy it is for this process to get confused. It locates routines by looking up their addresses in what amounts to a linker map (hashed, of course). If there's any sort of mismatch between that map and the actual code, the results will make even less sense than they normally do.

Look, if you're trying to see how to make the code go faster, here's the method I use.

If you're just trying to run a profiler for whatever reason, there are plenty of options, including oprofile, Zoom, etc.