28

I'm trying to profile a C++ application with gprof on a machine running OSX 10.5.7. I compile with g++ in the usual way, but using -pg flags, run the application and try to view the call graph with gprof.

Unfortunately my call graph contains all zeroes for all time columns. The values in the "called" columns have reasonable values so it looks like something was profiled but I'm mystified about the lack of other data.

All my source files are compiled in a similar way:

g++ -pg -O2 -DNDEBUG -I./ -ansi -c -o  ScenarioLoader.o ScenarioLoader.cpp

I then run 'ar' to bundle all the object files into a library. Later, I link and run gprof as so:

g++ -pg -lm  -o vrpalone vrpalone.o ../src/atomicprof.a lastbuild.o
./vrpalone
gprof gmon.out | less

Any ideas?

Daniel
  • 23,365
  • 10
  • 36
  • 34

7 Answers7

10

If your program terminates in a non-clean manner then the profile data won't get written correctly - how is your program exiting?

Regardless, I'd strongly recommend using Shark instead of gprof - it's very easy to use and superior in pretty much every way to gprof - and doesn't require you to recompile your program.

DaveR
  • 9,540
  • 3
  • 39
  • 58
  • Hi Dave. My program terminates cleanly. I've had a look at Shark and while it does work I can't seem to make it generate a flat profile. There is a flattening option that I've found but it doesn't display the same information as gprof which is dissapointing; ideally I would like all functions with the same name added together. I also can't seem to make Shark display the number of calls to each function. – Daniel Jun 24 '09 at 01:55
  • I found the "heavy view" option to solve the flattening problem. Still can't see a nice way of displaying number of calls. Oh well. I think I have enough! Cheers for the help Dave. – Daniel Jun 24 '09 at 02:27
  • Strong +1 for Shark. Beats gprof or Google Perf Tools when developing on OSX. You can see the exact number of samples by selecting a given row. It then shows the number of samples to that function on the bottom. Rarely is this useful though given that the % is what's helpful. – Tristan Jun 24 '09 at 02:34
6

Perhaps not relevant to the OP's question, there is a common scenario where "no time accumulated" happens: if your code calls the kernel or calls libraries not compiled with -pg, you won't see any time accumulated for time spent there.

Fixee
  • 1,581
  • 2
  • 15
  • 25
5

How fast does your program run? If its extremely quick, it might be too fast to actually profile. I had this problem with a very simple text processing program: when I ran it with my sub-1kb test file it reported all 0s in the time columns. I solved this by running the entire text of The Great Gatsby through it. Try a larger dataset or looping through your main computation a few hundred times.

psanf
  • 853
  • 7
  • 4
  • Hi psanf. My program runs for quite some time depending on the input I give it (it solves various traveling salesman problems). I've tried profiling it using a small problem involving 318 cities which takes ~30 seconds to solve. – Daniel Jun 24 '09 at 01:41
  • Consider the sampling time. If no function is active longer than the default samping time, 0.01 seconds, this may also result in no sampled data found. See also [this SO link](https://stackoverflow.com/a/38486072/4312669). – sebkraemer Apr 10 '18 at 08:33
5

I thought I might share this Apple mailing list discussion which I recently ran across.

The behaviour described here is exactly what I am experiencing. It looks like gprof has been broken on OSX for quite some time.

I've resorted to Shark which has been helpfully suggested by Dave Rigby.

Thanks!

Daniel
  • 23,365
  • 10
  • 36
  • 34
4

Does your program use multiple threads? I've experienced this issue with multithreaded programs on Linux, not sure if OS X would have the same problems

Here is a solution to the multithreading issue which I've used sucessfully in the past.

bdk
  • 4,769
  • 29
  • 33
  • Hi bklock. My program is only singly threaded at the moment. I will keep that link in mind though! – Daniel Jun 24 '09 at 01:56
2

Btw, do you fork() in your code? If so, add this in the child process just after the fork():

extern void _start (void), etext (void);
monstartup ((u_long) &_start, (u_long) &etext);

That did the trick for me.

Big Papoo
  • 167
  • 1
  • 13
0

the precision of time in gprof is 0.00. so maybe your module taking less time (milli sec/micro sec).Hence, it will show 0.00 and no time accumulated.So, run the whole program about 1000/1000000 times so that it will come to seconds.At last, divide obtained time with your looping factor (1000/1000000) and that going to be your processing time. I too faced the same problem and by doing this it gets solved.

Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • 4
    I hope you understand you are answering a question which is about 9 years old now, and the question's author most probably solved his issue or just died of old age (he was last seen here in S.O. in 2017). Apart from that, what you are suggesting as a solution to the question is a duplicate of what is written in the last part of [psanf's answer](https://stackoverflow.com/a/1034696/823321). – sɐunıɔןɐqɐp Sep 18 '18 at 07:58