gprof
1.1 Introduction
Gprof is actually just a tool for reading the profile results file. Gprof uses a hybrid approach to collect statistical information about the program, and he uses a detection method that inserts counters at the entrance of the function during compilation to collect the number of calls and times of each function, and also uses the sampling method, Check the program counter at a certain interval at run time and find out the function of the program counter during analysis to count the time the function takes.
Gprof has the following advantages and disadvantages:
1) Advantages:
A) GNU tool, one of the hands;
b) Mixing method to collect information.
2) Disadvantages:
A) support for compiling options is required:
I. Need to add-PG option when compiling and linking using GCC/CC
Ii. using the LD link requires/lib/gcrt0.o instead of CRT0.O as the first input file
III. If you want to debug the LIBC library, you need to use-lc_p instead of the-LC parameter
b) Debugging a multithreaded application can only count the information of the main thread (so it cannot be used for kingbase).
1.2 How to use 1.2.1 Compiling the program
Add-PG option when compiling and linking with GCC/CC
Using the LD link requires/lib/gcrt0.o instead of CRT0.O as the first input file
If you want to debug the LIBC library you need to use-lc_p instead of the-LC parameter
1.2.2 Run program generate statistics
Run the compiled program normally, and the program will generate statistics file Gmon.out in the current directory.
The program must exit normally (call exit or return from main) in order to generate statistical information.
If there is another file called Gmon.out in the current directory, the content will be overwritten by the statistic information generated by this run, and the unified program will be run multiple times. Please rename the previous gmon.out.
1.2.3 using Gprof to view statistical results
Command format:
Gprof options [Executable-file [Profile-data-files ...] [> outfile]
Common Parameters Introduction:
Symspec represents the name of a function that needs to be joined or excluded, and the same format when GDB specifies a breakpoint.
1) Output Related:
A)-a[symspec] or--annotated-source[=symspec]: The source association, only the SYMSPEC specified function, not specified as all associations.
b)-I dirs or--directory-path=dirs: Add the search source folder, modify the environment variable Gprof_path can also.
c)-p[symspec] or--flat-profile[=symspec]: default options, output statistics, only statistics Symspec specified functions, not specified as all statistics.
d)-p[symspec] or--no-flat-profile[=symspec]: Exclude statistics SYMSPEC the specified function
e)-q[symspec] or--graph[=symspec]: default option, output function call information, only statistics symspec specified function, not specified as all statistics.
f)-q[symspec] or--no-graph[=symspec]: Exclude statistics SYMSPEC the specified function
g)-B or--brief: the interpretation of the meaning of each parameter is not output;
2) Analysis Related:
A)-A or--no-static: the function defined as static will not be displayed, and the number of calls to the function will be computed in a function that calls it not static;
b)-M num or--min-count=num: Do not display functions that have been called fewer than Num;
c)-Z or--display-unused-functions: Displays functions that are not called;
1.3 An example
To compile the test file:
Gcc–g–o Test TEST.C–PG
Execute the program:
./test
To view statistical information:
Gprof-b-a-p-Q Test Gmon.out > PG
analysis of information generated by 1.4 Gprof
% The percentage of the total running time of the
Time program used by the This function.
function usage time as a percentage of all time.
Cumulative a running sum of the number of seconds accounted
seconds for by this function and those listed above it.
The time that the function and the above functions accumulate execution.
Self the number of seconds accounted
seconds function alone. The major sort for this
Listing.
The time that the function itself executes.
Calls the number of times this function is invoked, if
This function is profiled, else blank.
Number of times the function was called
Self the average number of milliseconds spent in this
Ms/call function per call, if the function is profiled,
else blank.
Each call is spent at the time of the function microseconds.
Total the average number of milliseconds spent in this
Ms/call function and its descendents per call, if this
function is profiled, else blank.
Each call is spent on the average time of the function and its derived function microseconds.
Name the function of the name. This is the minor sort
For this listing. The index shows the location of
The function in the GPROF listing. If the index is
In parenthesis it shows where it would appear in
The GPROF listing if it were to be printed.
Name of function
1.4 Conclusion
We can use program profiling to quickly find a place in a program that's worth optimizing.