[ACCEPTED]-How exactly does gprof work?-gprof

Accepted answer
Score: 10

Having just read the paper (again), let me try to explain 38 it.

Suppose it takes samples at 100Hz, except 37 not when the process is blocked for IO or 36 some other reason. Each sample records the 35 PC, which is in some function. The count 34 of samples in that function is incremented.

So, at 33 the end of a run, if there were (say) 1000 32 samples, that means the total execution 31 time (CPU-only) was 10 seconds. If routine 30 B recorded, say, 500 of those samples, that 29 means its total execution time is 1/2 of 28 the total, or 5 seconds. This is its self time because 27 the PC is in it. This does not tell how 26 long it takes to execute, on average. To 25 tell that, you need to know how many times 24 it was called. Also it does not include 23 time spent in callees.

When the code is compiled 22 with the -pg flag, a special call is inserted 21 in each routine's entry code. That notices 20 that routine B is entered, and it notices 19 that it is called from a call site in routine 18 A. There is a table, indexed by that call 17 site, where that call can be counted. So 16 at the end, gprof can tell how many times 15 B was called in total, and how many of those 14 were from A.

To get the average self time 13 of B, its total self time is divided by 12 the number of times it is called.

In order 11 to get the total cumulative time (self plus 10 callees) of routine A, gprof needs the self 9 time of A, plus the total number of times 8 it calls each subordinate routine B, times 7 the average cumulative time of B. That number 6 is then divided by the total number of calls 5 to A to get the average cumulative time 4 of A.

Sounds good, until recursion enters 3 the picture, where it gets even more confusing.

It's 2 all very clever, and as the authors point 1 out, full of caveats.

Score: 9

Well, this gives a good explanation. Also this explains 9 statistical profiling

Essentially gprof will change the executable 8 of your program (this is called instrumenting the code) to store 7 some book-keeping information, e.g. how 6 many times a function is called.

The statistical 5 profiling bit comes from snooping the program 4 counter regularly to get a sample of what 3 your code is doing.

Gprof does both. It instruments 2 the code and collects samples from looking 1 at the program counter.

More Related questions