GNU gprof Profiler Yu Kai Hong Department of Mathematics National Taiwan University July 19, 2008 GNU gprof 1/22
Dec 22, 2015
GNU gprof Profiler
Yu Kai Hong
Department of MathematicsNational Taiwan University
July 19, 2008
GNU gprof 1/22
Two types of time profilers
Built-in UNIX time profiler
- Unix C shell
- Others
GNU gprof time profiler
GNU gprof 2/22
Built-in UNIX time profiler
The simplest one.
Does not increase extra time cost.
Does not provide subroutine relative graph.
Does not have to recompile source code.
GNU gprof 3/22
Built-in UNIX time profiler
Unix C shell
14.918u: elapsed user time
0.016s: elapsed system time
0:15.61: total elapsed time
95.5%: percent of total CPU usage
0+0k: shared/non-shared memory
0+0io: times for performing input/output
[ykhong@vangogh home]$ time ./test
14.918u 0.016s 0:15.61 95.5% 0+0k 0+0io 0pf+0w
GNU gprof 4/22
Built-in UNIX time profiler
Others
real 0m15.456s: total elapsed time
user 0m14.990s: user elapsed time
sys 0m0.015s: system elapsed time
[ykhong@vangogh home]$ time ./test
real 0m15.456s
user 0m14.990s
sys 0m0.015s
GNU gprof 5/22
GNU gprof time profiler
Detail time statistics for each subroutine.
Create relative graph for all subroutines.
Analysis the program bottleneck.
Increase about 30% extra time cost.
GNU gprof 6/22
GNU gprof time profiler
Recompile the original source code
-pg: This option affects both compiling and linking.
Add additional commands into source code when
compiling code in order to trace all subroutines.
Add essential initial settings and statistical processes
when linking the objects.
gcc –pg SourceCode –o ExecutableFile
[ykhong@vangogh home]$ gcc –pg test2.c –o test2
GNU gprof 7/22
GNU gprof time profiler
Convert produced profile data into text file
- ListOfOptions can be omitted.
- ExecuteFile can be omitted when the file name is a.out.
- StatFiles can be omitted when the file name is gmon.out.
gprof ListOfOptions ExecuteFile StatFiles > OutputFile
[ykhong@vangogh home]$ gprof –b test2 gmon.out > output.txt
GNU gprof 8/22
GNU gprof time profiler
List of Options
-b: omit the table or data illustration on OutputFile.
-e(E) SRName: exclude the subroutine SRName from the table
(and exclude its elapsed time).
-f(F) SRName: only display the subroutine SRName on the
table (and its elapsed time).
GNU gprof 9/22
GNU gprof time profiler
List of Options
-s: combine more than one StatFile into single one with
default file name gmon.sum.
-Z: only display all subroutines table which are unused
on the program.
GNU gprof 10/22
GNU gprof time profiler
Example Program
[ykhong@vangogh home]$ gcc –pg test.c –o test
[ykhong@vangogh home]$ ./test
[ykhong@vangogh home]$ gprof –b test gmon.out > output
[ykhong@vangogh home]$ more output
GNU gprof 2/22
GNU gprof time profiler
Example Program Each sample counts as 0.01 seconds.
% cumulative self self total
time seconds seconds calls s/call s/call name
71.90 30.17 30.17 1 30.17 30.17 C3
19.42 38.32 8.15 2 4.07 4.07 B2
7.99 41.67 3.35 1 3.35 3.35 C2
0.00 41.67 0.00 1 0.00 37.60 A
0.00 41.67 0.00 1 0.00 33.52 B1
0.00 41.67 0.00 1 0.00 0.00 C1
0.00 41.67 0.00 1 0.00 4.07 D
GNU gprof 13/22
GNU gprof time profiler
Example Program
% time: the percent of self seconds from total program
elapsed time.
cumulative seconds: the seconds cumulate from self seconds.
self seconds: total elapsed time called by its parents, not
including its children’s elapsed time.
equal to (self s/call)*(calls)
GNU gprof 14/22
GNU gprof time profiler
Example Program
calls: total number for each subroutine called by its parents.
self s/call: elapsed time for each time called by its parents,
not including its children’s elapsed time.
total s/call: total elapsed time called by its parents,
including its children’s elapsed time.
name: subroutine name.
GNU gprof 15/22
GNU gprof time profiler
Example Program
Call graph
index %time self children called name
<spontaneous>
[1] 100.0 0.00 41.67 main[1]
0.00 37.60 1/1 A[2]
0.00 4.07 1/1 D[6]
------------------------------------------------------------------
0.00 37.60 1/1 main[1]
[2] 90.2 0.00 37.60 1 A[2]
0.00 33.52 1/1 B1[3]
4.07 0.00 1/2 B2[5]
------------------------------------------------------------------
GNU gprof 16/22
GNU gprof time profiler
Example Program 0.00 33.52 1/1 A[2]
[3] 80.4 0.00 33.52 1 B1[3]
30.17 0.00 1/1 C3[4]
3.35 0.00 1/1 C2[7]
0.00 0.00 1/1 C1[8]
------------------------------------------------------------------
30.17 0.00 1/1 B1[3]
[4] 72.4 30.17 0.00 1 C3[4]
------------------------------------------------------------------
4.07 0.00 1/2 A[2]
4.07 0.00 1/2 D[6]
[5] 19.6 8.15 0.00 2 B2[5]
------------------------------------------------------------------
GNU gprof 17/22
GNU gprof time profiler
Example Program
0.00 4.07 1/1 main[1]
[6] 9.8 0.00 4.07 1 D[6]
4.07 0.00 1/2 B2[5]
------------------------------------------------------------------
3.35 0.00 1/1 B1[3]
[7] 8.0 3.35 0.00 1 C2[7]
------------------------------------------------------------------
0.00 0.00 1/1 B1[3]
[8] 0.0 0.00 0.00 1 C1[8]
------------------------------------------------------------------
GNU gprof 18/22
Example Program
[ ] index value for each subroutine
GNU gprof time profiler
[2]A [8]C1 [6]D
[3]B1 [7]C2
[5]B2 [4]C3
GNU gprof 19/22
Example Program See the second part.
A on the left-most means the relation is viewed for A. B1 and B2 are below of A means A is the parent of B1 and B2 (A calls B1 and B2), main is above of A means A is the child of main (A called by main).
GNU gprof time profiler
name
<spontaneous>
main[1]
A[2]
D[6]
--------------------
main[1]
A[2]
B1[3]
B2[5]
GNU gprof 20/22
Example Program
See the second part.
1/2 means subroutine B2 is called by
its parents total two times and A calls
it only one time.
1 means subroutine A have only one
parent.
GNU gprof time profiler
called name
<spontaneous>
main[1]
1/1 A[2]
1/1 D[6]
--------------------
1/1 main[1]
1 A[2]
1/1 B1[3]
1/2 B2[5]
GNU gprof 21/22
Example Program
See the second part
It is viewed for subroutine A. The elapsed time of A’s children is 37.60 seconds and B1’s children is 33.52 seconds.
Especially, B2’s children is 0 second
since B2 doesn’t have any child.
GNU gprof time profiler
children name
<spontaneous>
41.67 main[1]
37.60 A[2]
4.07 D[6]
--------------------
37.60 main[1]
37.60 A[2]
33.52 B1[3]
0.00 B2[5]
GNU gprof 22/22