Original article address:
Http://apps.hi.baidu.com/share/detail/5908917
GPROF Introduction
GPROF is the GNU profiler tool. Can be displayedProgramThe running "flat profile", including the number of calls to each function and the processor time consumed by each function. You can also display the "Call diagram", including the call relationship of the function, and the time consumed by each function call. You can also display the "commentedSource code", Is the program sourceCodeIndicates the number of executions of each line of code in the program.
Basic usage:
1. Use the-PG option to compile and link your application.
2. Run your application to generate a data file for GPROF analysis after it is run (gmon. Out by default ).
3. Use the GPROF program to analyze the data generated by your application, for example, gporf A. Out gmon. Out.
Example
Gcc-wall-PG-O test. c// The program file name test. C is used for compiling-PG
Now we can run test again and use the test data we used earlier. During this operation, the analysis data run by test is collected and saved in the 'gmon. out' file. You can run 'gprof test' to view the result.
./Test
GPROF Test
GPROF implementation principle:
GPROF is not magical. When compiling and linking a program (using-PG compilation and linking options ), GCC adds a function named mcount (or "_ mcount", or "_ mcount") to each function of your application, that is to say, every function in the application compiled by PG calls mcount, and mcount saves a function call diagram in the memory, the address of the subfunction and the parent function is searched through the function call stack. This call chart also saves all information related to the function call time, number of calls, and so on.
After the program runs, A gmon. Out file is generated in the exit path of the program. This file is the monitoring data recorded and saved. You can use GPROF in command line mode or kprof in graphical mode to interpret the data and analyze the program performance.
In addition, if you want to view the profiling of the library function, you need to add "-lc_p" to compile the parameter in place of "-LC". In this way, the program will link to library libc_p.a, to generate the profiling information of the library function. If you want to execute the profiling of one row and one row, you also need to add the "-G" Compilation parameter.
Information generated by GPROF
% The percentage of the total running time of
Time program used by this function.
The percentage of time used by the function to all time.
Cumulative a running sum of the number of seconds accounted
Seconds for by this function and those listed abve it.
Cumulative execution time of functions and upper-column functions.
Self the number of seconds accounted for by this
Seconds function alone. This is the major sort for this
Listing.
The time when the function is executed.
Callthe number of times this function was invoked, if
This function is profiled, else blank.
Number of function calls
Self the average number of milliseconds spent in this
MS/call function per call, if this function is profiled,
Else blank.
Each call takes microseconds as the function time.
Total the average number of milliseconds spent in this
MS/call Function and Its descendents per call, if this
Function is profiled, else blank.
The average time of each call is microseconds.
Name the name of the function. This is the minor sort
For this listing. The index shows the location
The function in the GPROF listing. If the index is
In parenthesis it shows where it wowould appear in
The GPROF listing if it were to be printed.
Function Name
More details
1. allocate some memory in the memory to store statistics during Program Execution
2. After GCC uses the-PG option for compilation, GCC will call
Void monstartup (lowpc, highpc)
Call at the entrance of each function
Void _ mcount ()
Called when the program exits (in atexit ()
Void _ mcleanup ()
* Monstartup: initializes the profile environment and allocates memory space.
* _ Mcount: records the caller and callee locations of each function code.
* _ Mcleanup: clears the profile environment and saves the result data as gmon. Out for GPROF analysis.
3. Track the execution status of the program in the _ mcount function, and record the execution times and time of the program code.
Common GPROF Command Options:
-B no longer outputs detailed descriptions of each field in the Statistical Chart.
-P only outputs the call graph of the function (the part of the Call Graph Information ).
-Q only outputs the time consumption list of the function.
-E name no longer outputs call diagrams of function names and their subfunctions (unless they have other parent functions that are not restricted ). Multiple-e flags can be specified. One-e flag can only specify one function.
-E name no longer outputs the call diagram of the function name and its subfunctions. This flag is similar to the-e flag, however, in the calculation of the total time and percentage time, it excludes the time used by the function name and its subfunctions.
-F name: The call graph of the output function name and its subfunctions. Multiple-F flag can be specified. One-F flag can only specify one function.
-F name: The call graph of the output function name and its subfunctions. It is similar to the-F sign, but it only uses the Time of the printed routine in the total time and percentage time calculation. Multiple-F flag can be specified. One-F flag can only specify one function. -F flag overwrites the-e flag.
-Z: a zero-number-of-use routine (calculated based on the call count and accumulation time ).
Note:
1) Generally, GPROF can only view User Function information. If you want to view the library function information, you need to add "-lc_p" to compile the parameter in place of "-LC". In this way, the program will link to library libc_p.a, to generate the profiling information of the library function.
2) GPROF can generate a program evaluation report only after the program ends and Exits normally because GPROF registers a function in atexit () to generate the result information, no abnormal exit will execute the atexit () action, so no gmon will be generated. out file. If your program is a service program that will not exit, you can only modify the code to achieve the goal. If you do not want to change the running mode of the program, you can add a signal processing function to solve the problem (which minimizes the modification to the Code), for example:
Static void sighandler (INT sig_no)
{
Exit (0 );
}
Signal (SIGUSR1, sighandler );
After the kill-usr1 PID is used, the program exits and generates the gmon. Out file.
Edit
Other C/C ++ program analyzers
There are many other analyzer tools that can use GPROF data, such as kprof (screenshot) and cgprof. Although the graphic interface looks more comfortable, I personally think that GPROF is more convenient to use.
End