The easiest thing to forget: gprof must be able to generate statistics after the program exits normally. So if your program is a long-running service, you must use signal capture a signal, and then safely exit, the problem I did not find before, the results of debugging for several hours, including strace and other means have tried, no fruit. Last seen on the document. Depressed AH. Compile switch: I'm using cmake, so I add-pg to &NBS on compile and link. P set (cmake_cxx_flags _release "$ENV {cxxflags}-o2-wall-wno-deprecated-ftemplate-depth-50-fpic-pg") SET ( Link_flags "-PG") finally remember a more powerful getting started document from http://hujw0710.blog.163.com/blog/static/8797282200952324755785/ &NB Sp &NB Sp When we write programs, especially embedded programs, we usually need to analyze the performance of the program so that the program can run faster and better to achieve real-time (real-time) purposes. If the program is large, it is difficult to analyze. If a tool can automate the performance of a programAnalysis, that would be the best. Here is a profiling tool for Linux programs----GNU Profiler.
The GNU gprof is able to print out the time it takes for each function in the program to run, helping the programmer to find the most time-consuming functions in many functions. A function call relationship, including the number of calls, can be used to help programmers analyze the running process of a program. With the function of the call relationship, which will allow developers to greatly improve work efficiency, do not bother to find a little bit of the running process of the program, which may not be very obvious to the small program, but for tens of thousands of, hundreds of thousands of code volume of the project, efficiency is beyond doubt. And this feature is pretty tempting to maintain old code or analyze open source. With the call graph, the running framework of the program has a general understanding of the program's "skeleton", analysis of it will not be so dazed, especially for their unfamiliar code and open source.
Basic usage of gprof:
1. Compile and link your application using the-PG option
When GCC compiles the program, add the-PG option, for example:
Gcc-pg-o Test test.c
This generates the executable file test. If it's a big project, modify the compilation options in Makefile,-PG put it there.
2. Execute your application to generate data for gprof analysis
Run the program you just made:./test, which generates a gmon.out file that contains the profiling data.
3. Use GPROF to analyze data generated by your application
Gprof Test Gmon.out > Profile.txt
Using the above command, GPROF can analyze the performance of the program test, put the profiling results in the Profile.txt file, and open to see the results of the analysis. Through the analysis of the results to improve our procedures, so as to achieve our goal.
GNU Gprof is a great tool, and you can use it more when you write a program. I now use GPROF to profiling my program, the most time-consuming function or operation to find out, with FPGA chip to achieve, so as to achieve the real-time purpose.
Introduction to the use of gprof
Gprof Introduction
Gprof is the GNU Profiler tool. You can display the "flat profile" of the program's operation, including the number of calls per function, and the processor time consumed by each function. You can also display the call graph, including the function's invocation relationship, and how long each function call takes. You can also display "source code for Comments", a copy of your program's source codes, and mark the number of times each line of code in your program is executed.
Compiling programs for Gprof
By adding the "-pg" option to the compiler's command-line arguments when compiling or linking the source program, the compile-time compiler automatically inserts the code snippet for the performance test in the target code, which collects and records the call relationship and number of calls to the function at run time. As well as collecting and recording the function's own execution time and the call time of the child function, after the program runs, the program exits the path to generate a gmon.out file. This file is the monitoring data that is recorded and saved. This data can be interpreted by means of command-line gprof or graphical kprof, and the performance of the program is analyzed. In addition, if you want to view the profiling of the library function, you need to add the "-lc_p" compilation parameter instead of the "-LC" compilation parameter in the compilation, so that the program will link LIBC_P.A Library to produce the profiling information of the library function. If you want to perform a line of profiling, you also need to add a "-G" compilation parameter.
For example, the following command line:
Gcc-wall-g-pg-lc_p Example.c-o Example
Execute gprof
Execute the GPROF by executing the following command line:
Gprof OPTIONS executable-file gmon.out bb-data [yet-more-profile-data-files ...] [> outfile]
Information generated by gprof
% The percentage of the total running the
Used by this function.
function usage time as a percentage of all time.
Cumulative a running sum of the number of seconds accounted
seconds for with this function and those listed above it.
The cumulative execution time of the function and the above function.
Self the number of seconds accounted for with this
seconds function alone. This are the major sort for this
Listing.
The time that the function itself executes.
Calls the number of this function is invoked, if
This function is profiled, else blank.
The number of times the function was invoked
Self the average number of milliseconds spent in this
Ms/call function per call, if this function is profiled,
else blank.
Each call is spent microseconds the time of the function.
Total the average number of milliseconds spent in this
Ms/call function and its descendents/call, if this
The function is profiled, else blank.
Every time a call is spent, the average time of the function and its derivative function is microseconds.
Name the name of the function. 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.
The name of the function
O.s uses GNU gprof for program analysis under Linux platform
Introduction to
gprof :
Gprof feature: Prints out the elapsed time of each function in the program's run, helping the programmer find the most time-consuming function in many functions. A function call relationship, including the number of calls, can be used to help programmers analyze the running process of a program.
has a function call relationship, which can greatly improve the productivity of developers, do not bother to find a little bit of the running process of the program, which may not be very obvious to the small program, but for tens of thousands of, hundreds of thousands of code volume of the project, efficiency is beyond doubt. And this feature is pretty tempting to maintain old code or analyze open source. With the call graph, the running framework of the program has a general understanding of the program's "skeleton", analysis of it will not be so dazed, especially for their unfamiliar code and open source. Crap don't say much, let's start our analysis tour.
gprof Implementation :
by compiling and linking your program (using the -pg compile and link options), gcc Adds a function in every function of your application named Mcount (or "_mcount", or "__mcount", dependent on the compiler or operating system), which means that every function in your application invokes Mcount. mcount saves a function call graph in memory and finds the address of the child function and the parent function in the form of a function call stack. This call graph also holds all the information about the call time, the number of calls, and so on for the function.
Gprof Basic usage:
1. use -pg to compile and link your application. &NBSP,
2. Execute your application to generate data for gprof analysis.
3. Use the gprof program to analyze the data generated by your application.
gprof simple use:
Let's take a quick example to see how Gprof is used.
1. Open the Linux terminal. Creates a new test.c file and compiles and links the file with-pg .
The contents of the TEST.c file are as follows:
Citation:
#include "stdio.h"
#include "Stdlib.h"
void A () {
printf ("/t/t+---call a () function/n");
}
void C () {
printf ("/t/t+---call C () function/n");
}
int B () {
printf ("/t+---call B () function/n");
A ();
C ();
return 0;
}
int main () {
printf ("Main () function ()/n");
b ();
}
Command line to enter the following command, without the-c option, GCC will default to compile and link to deliver into a.out:
Citation:
[Linux/home/test] $GCC-pg test.c
If there is no compilation error, GCC will generate a a.out file in the current directory, and of course you can use the –o option to give the generated file a different name, like GCC–PG test.c–o test, GCC generates an executable named Test, and at the command line, enter [ Linux/home/test]$./test, you can execute the program, and remember to add./Otherwise the program may appear to be executing, but there is no output.
2. Execute your application to generate data for gprof analysis. Inside the command line, enter:
Citation:
[Linux/home/test] $a. Out
Main () function ()
+---call B () function
+---call a () function
+---call C () function
[linux/home/test]$
You will see a gmon.out file in the current directory, which is used for gprof analysis.
3. Use the GPROF program to analyze the data generated by your application.
Inside the command line, enter:
Citation:
[linux/home/test]$ gprof-b a.out gmon.out | Less
Because the Gprof output information is more, here uses the less command, this command lets us look up and down the arrow key to see GPROF produces the output, | Represents the output of the Gprof-b a.out gmon.out as input to the less. Here are some of the details that I've extracted from the gprof output.
Citation:
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 1 0.00 0.00 A
0.00 0.00 0.00 1 0.00 0.00 B
0.00 0.00 0.00 1 0.00 0.00 C
Call graph
Granularity:each sample hit covers 4 byte (s) No time propagated
Index% time self children called name
0.00 0.00 1/1 B [2]
[1] 0.0 0.00 0.00 1 A [1]
-----------------------------------------------
0.00 0.00 1/1 Main [10]
[2] 0.0 0.00 0.00 1 B [2]
0.00 0.00 1/1 C [3]
0.00 0.00 1/1 A [1]
-----------------------------------------------
0.00 0.00 1/1 B [2]
[3] 0.0 0.00 0.00 1 C [3]
-----------------------------------------------
Index by Function name
[1] A [2] b [3] C
From the above output we can clearly see that main calls the B function, while the B function calls the A and C functions respectively. Since our function simply outputs a string, the time spent on each function is 0 seconds.
using the GPROF analysis program
Gprof Introduction
Gprof is a GNU Profiler tool. You can display the "flat profile" of the program's operation, including the number of calls per function, the processor time consumed by each function, or the call graph, including the call relationship for the function, and how long each function call took. You can also show "source code for Comments"--a copy of the program's source code that marks the number of times each line of code is executed in the program.
Basic usage: