Section 11.7. The GNU Profiler: gprof


[Page 418 (continued)]

11.7. The GNU Profiler: gprof

It's often handy to be able to see where a program is spending its time. For example, if a greater-than-expected amount of time is being spent in a particular function, it might be worth optimizing the function by hand for better performance. The gprof utility allows you to obtain a program's profile (Figure 11-15).

Figure 11-15. Description of the gprof command.

Utility: gprof -b [ executableFile [ profileFile ] ]

gprof is the GNU profiler. It generates a table of time and repetitions of each function in the executable executableFile based on the performance trace stored in the file profileFile. If profileFile is omitted, "gmon.out" is assumed. If executableFile is omitted, "a.out" is assumed. The executable file must have been compiled using the -pg option of gcc, which instructs the compiler to generate special code that writes a "gmon.out" file when the program runs. The gprof utility then looks at this output file after the program has terminated and displays the information contained therein. The output of gprof is verbose (but helpful); to instruct gprof to be brief, use the -b option.


Here's an example of gprof in action:

$ main2                          ...execute the program. palindrome ("cat") = 0           ...program output. 
[Page 419]
palindrome ("noon") = 1 $ ls -lG gmon.out ...list the monitor output. -rw-r-xr-x 1 ables 468 Jan 8 17:19 gmon.out $ gprof -b main2 gmon.out ...profile the program. Flat profile: Each sample counts as 0.01 seconds. no time accumulated % cumulative self self total time seconds seconds calls Ts/call Ts/call name 0.00 0.00 0.00 2 0.00 0.00 palindrome 0.00 0.00 0.00 2 0.00 0.00 reverse Call graph granularity: each sample hit covers 2 byte(s) no time propagated index % time self children called name 0.00 0.00 2/2 main [9] [1] 0.0 0.00 0.00 2 palindrome [1] 0.00 0.00 2/2 reverse [2] ----------------------------------------------- 0.00 0.00 2/2 palindrome [1] [2] 0.0 0.00 0.00 2 reverse [2] ----------------------------------------------- Index by function name [1] palindrome [2] reverse $ _


After a profile has been viewed, you may decide to do some hand tuning and then obtain another profile.




Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net