The following are the requirements to profile a program using gprof
- Profiling must be enabled when compiling and linking the program.
- A profiling data file is generated when the program is executed.
- Profiling data needs to be analyzed.
For you to use this gprof utility, the package must be installed on your system. In order to analyze the program with gprof, we need to compile the program with a special option. Assuming that we have a program
sample_2007.c, the following can be used to compile i
$ gcc –a –p –pg –o sample_2007 sample_2007.c
Note here that
–pg option enables the basic profiling support in gcc. The program will run somewhat slower when profiling is enabled. This is because of the fact that it needs to spend time in collecting data as well. The profiling support in the program creates a file named
gmon.out in the current directory. This file is later used by gprof to analyze the code.
We can run the following command to get the output (which we have redirected to a file):
$ gprof sample_2007 gmon.out > output.txt
gprof is useful not only to determine how much time is spent in various routines, but it also tells you which routines invoke other routines. By using gprof, we will be able to know which sections of our code are causing the largest delays. Analyzing the source code with gprof is considered as an efficient way determining which function is using a large percentage of the overall time spent in executing the program.
A Few Things to Know About kprof
Kprof is a graphical tool that displays the execution profiling output generated by the gprof profiler. Kprof is very useful as it displays the information in list or tree view and it makes the information easy to understand.
Kprof has the following features
- Flat profile view displays all functions and methods as well as their profiling information.
- Hierarchical profile view displays a tree for each function and method with other functions and methods it calls as sub elements.
- Graph view is the graphical representation of the call tree.
- Optimizing Linux Performance: A Hands-On Guide to Linux performance tools, by Philip G. Ezolt, Prentice Hall PTR
- Linux Debugging and Performance Tuning: Tips and Techniques, by Steve Best, Prentice Hall PTR
- Performance Tuning for Linux Servers, by Badari Pulavarty, Gerrit Huizenga, Sandra K. Johnson, IBM Press
Swayam Prakasha has been working in information technology for several years, concentrating on areas such as operating systems, networking, network security, electronic commerce, Internet services, LDAP, and Web servers. Swayam has authored a number of articles for trade publications, and he presents his own papers at industry conferences. Currently he works at Unisys Bangalore in the Linux Systems Group.
Return to Linux DevCenter.