These days I try to do some stress testing and performance optimization for the project, and also make a summary of the used test tools, and make a summary of the relevant data for later use.
1 Introduction
Improving application performance is a time-consuming effort, but it is often not obvious what functions in a program consume most of the execution time. The GNU Compiler Toolkit provides an profiling tool for the GNU Profiler (gprof). Gprof can accurately analyze performance bottlenecks for programs on the Linux platform. Gprof precisely gives the time and frequency of the function being invoked, and gives the function call relation.
Gprof User manual website http://sourceware.org/binutils/docs-2.17/gprof/index.html
2 function
Gprof is one of the GNU GNU Binutils tools that are included with this tool by default in Linux systems.
1. Can display "flat profile", including the number of calls per function, the processor time consumed by each function,
2. You can display "call graph", including the function's invocation relationship, and how long each function call takes.
3. You can display "annotated source code"--a copy of the program's source codes, marking the number of times each line of code is executed in the program.
3 principle
By compiling and linking the program (using the-PG compile and link options), GCC adds a function in each function of your application named Mcount (or "_mcount", or "__mcount", depending on the compiler or operating system), which means that your application Each function in the order calls Mcount, and Mcount saves a function call graph in memory and finds the address of the child function and the parent function through the function call stack. This call graph also holds all the information about the call time, number of calls, and so on for all functions associated with the function.
4 Use flow
1. Add the-PG option when compiling and linking. In general we can add to the makefile.
2. Execute the compiled binary program. Execution parameters and methods are the same as before.
3. Generate gmon.out files in the running directory of the program. If there is a gmon.out file, it will be overridden.
4. Closure of the process. Then Gmon.out will be refreshed again.
5. Analyze gmon.out files with gprof tools.
5 parameter description
L-b no longer prints a detailed description of each field in the statistics chart.
The l-p only outputs the call graph of the function (that part of called graph).
L-q only outputs a list of time consumption for a function.
L-E name no longer prints the call graph of the function Name and its child functions (unless they have other parent functions that are not restricted). Multiple-e flags can be given. An-e flag can only specify one function.
L-E name no longer prints the call graph for function name and its child functions, which is similar to the-e flag, but excludes the time spent in the calculation of the total time and percent time by the function name and its child functions.
The call graph for the L-f name output function name and its child functions. Multiple-F flags can be specified. A-f flag can specify only one function.
The call graph for the L-f name output function name and its child functions, similar to the-f flag, but it uses only the time of the printed routine in the total time and percent time calculation. Multiple-F flags can be specified. A-f flag can specify only one function. The-f flag overrides the-e flag.
L-z shows routines with a zero number of uses (calculated by call count and cumulative time).
General usage: gprof–b binary program Gmon.out >report.txt
6 Report notes
Explanation of the information produced by Gprof:
%time |
Cumulative Seconds |
Self Seconds |
Calls |
Self Ts/call |
Total Ts/call |
Name |
The function consumes time as a percentage of the program's total time |
Cumulative execution time of the program (just include functions that gprof can monitor) |
The function itself executes time (Total time of all called times) |
Number of times the function was invoked |
function average Execution time (does not include time to be called) (The function's single execution time) |
function average Execution time (including time to be invoked) (The function's single execution time) |
The name of the function |
The field meaning of call Graph:
Index |
%time |
Self |
Children |
Called |
Name |
Index value |
Function consumption time as% of all time |
function itself execution time |
Time spent executing child functions |
Number of Calls |
The name of the function |
Attention:
The cumulative execution time of a program is only a function that gprof can monitor. Functions that work in the kernel State and Third-party library functions that are not-PG compiled cannot be monitored by gprof (such as sleep (), etc.)
Gprof's specific parameters can be queried by man Gprof.
7 shared Library support