Preliminary analysis of performance with Gprof

Source: Internet
Author: User
1 Introduction

The GNU Profiler (gprof) is the GNU Profiler tool. It can accurately analyze performance bottlenecks for programs on the Linux platform, which can record the number of calls per function, the processor time consumed by each function, and the ability to display "call graphs", including call relationships for functions. can help us improve the performance of our applications.

Official website:

http://www.cs.utah.edu/dept/old/texinfo/as/gprof_toc.html,http://sourceware.org/binutils/docs/ gprof/index.html 2 principle

By using the-PG option (both compile and link) when compiling and linking the program, GCC does three jobs when we compile the program using the "-PG" option:

L The entrance of the program (before main function) inserts the calling code of the Monstartup function, completes the initialization of profile, including allocating memory for storing information and setting up a clock signal processing function.

L INSERT the call code of the _mcount function at the entrance of each function for statistical function invocation information: Including call time, number of calls, and call stack information

L in the program exit (register Atexit () function), insert the _mcleanup () function of the call code, responsible for the profile information output to the gmon.out. 3 Use flow

L ADD the-PG option when compiling and linking.

L execute a compiled binary program

l when the program exits normally, generate gmon.out files in the running directory. If there is a gmon.out file, it will be overwritten.

L analyze gmon.out files with gprof tools. 4 gprof output Analysis

After the Gmon.out file is generated, you can analyze the data through the tool Gprof provided in the GNU Binutils and convert it into a format that is easy to read and understand.

General usage:

# gprof Binary-file gmon.out >report.txt

In which, binary-file refers to the running of the program (also can be the program called the Library file), Gmon.out is the previous output of the file, Report.txt is the resulting analysis report. GPROF provides a wealth of parameter options to control the content of the report output.

4.1 Simple List

To open a report file with a text editor:


The first part of the report is a simple list that lists the invocation of each function, as shown in the previous illustration. The list is first sorted in descending order of time, if the time is the same, and then in descending order by number of calls. The meanings of each field are as follows:

L%time This function consumes time as a percentage of the program's total time

L Cumulative seconds Cumulative execution time. The time it takes to execute this function, plus the sum of time it consumes on the function listed above

L Self seconds The time consumed by the function itself (the sum of all call times), first sorted by the size of the value

L the number of times the Calls function is called, and if a function is never called, the field is empty

The average execution time of self ts/call function itself

L Total Ts/call function and the average time of its derivative function call

The name of the L name function

In fact, below the list, a detailed description of these fields is given:

% The percentage of the total running the
Used by this function.

Cumulative a running sum of the number of seconds accounted
seconds for with this function and those listed above it.

Self the number of seconds accounted for with this
seconds function alone. This are the major sort for this
Listing.

Calls the number of this function is invoked, if
This function is profiled, else blank.

Self the average number of milliseconds spent in this
Ms/call function per call, if this function is profiled,
else blank.

Total the average number of milliseconds spent in this
Ms/call function and its descendents/call, if this
The function is profiled, else blank.

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.

4.2 Call Graph

The second part of the report is a call graph that gives the time consumption of the function and its descendants. Lists are sorted in descending order of time, and indexing organization, by index, is easy to find out the overall relationship of the call. After calling the diagram, the description of each element in the diagram is given, which looks convenient:

This table describes the "Call Tree" program, and is sorted by
The total amount of the time spent in each function and its children.

Each entry in this table consists of several lines. The line with the
Index number at the left hand margin lists the current function.
The lines above it list the functions that called this function,
And the lines below it list the functions this one called.
This line lists:
Index A unique number given to each element of the table.
Index numbers are sorted numerically.
The index number is printed next to every function name
It is easier to look up where the function in the table.

% of this is the percentage of
In the This function and its children. Note which due to
Different viewpoints, functions excluded by options, etc,
These numbers won't add up to 100%.

The self is the total amount the ' time spent to this function.

Children this is the total amount of time propagated into this
function by its children.

Called this is the "number of times" the function was called.
If the function called itself recursively, the number
Only includes non-recursive calls, and are followed by
A ' + ' and the number of recursive calls.

Name the name of the current function. The index number is
Printed after it. If The function is a member of a
Cycle, the cycle number is printed between the
The function ' s name and the index number.


For the function ' s parents, the fields have the following meanings:


The self this is the amount of ' time ' is propagated directly
From the function to this parent.

Children this is the amount of
The function ' s children into this parent.

Called this is the number of this parent called the
function '/' The total number of times ' function
was called. Recursive calls to the function are not
Included in the # after the '/'.

The name is the name of the parent. The parent ' s index
Number is printed after it. If the parent is a
Member of a cycle, the cycle number is printed between
The name and the index number.

If the parents of the function cannot be determined, the word
' <spontaneous> ' is printed in the ' Name ' field
fields are blank.

For the function ' s children, the fields have the following meanings:

The self this is the amount of ' time ' is propagated directly
The From is into the function.

Children this is the amount of
Child ' s children to the function.

Called this is the number of times the function called
This is the child '/' the
was called. Recursive calls by the child are not
Listed in the # after the '/'.

The name is the name of the child. The child ' s index
Number is printed after it. If the is a
Member of a cycle, the cycle number is printed
Between the name and the index number.

If there are any cycles (circles) into the call graph, there be an
Entry for the Cycle-as-a-whole. This is entry shows who called the
Cycle (as parents) and the members of the cycle (as children.)
The ' + ' recursive calls entry shows the number of function calls that
were internal to the cycle, and the calls entry as each member shows,
For this, how many times it is called from
The cycle.

5 Using dot graphics

TXT format of the report, for small-scale programs is enough, but for large-scale programs, it seems to be too complicated, especially when we focus on the call relationship, the text of the jump always makes people uncomfortable.

Converting TXT reports to pictures requires Python and dot, as well as downloading gprof2dot.py scripts.

The dot is a tool provided by Graphviz that, under CentOS, can be installed with the following command:

#yum Install Graphviz

After installation, execute:

# python gprof2dot.py report.txt | Dot-tpng-o Ast.png

Where Report.txt is the previous gprof output of the text report, at this time, the current directory to generate a file called Ast.png, open look.

6

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.