Linux C + + program Profiling Tools Gprof Getting Started with

Source: Internet
Author: User

Performance analysis Tools

Software performance is an important point of inspection of software quality, whether online service programs or offline programs, or even terminal applications, performance is the key to user experience. There are two aspects of performance and stability that are mentioned in the major performance areas, and we have to focus on testing the performance and stability of the version when we do software testing. There are many ways to locate the performance problems found during software testing. The basic approach might be for developers to review the code, or to use some tools to perform profiling of the code. What are the common performance analysis tuning tools? The following two articles make a detailed summary:

    • https://computing.llnl.gov/tutorials/performance_tools/#Considerations
    • Http://en.wikipedia.org/wiki/List_of_performance_analysis_tools

In my work is mainly about the Linux C + + program code performance analysis, GPROF is available for Linux C + + code performance profiling one of the tools, this article mainly talk about my study of gprof and use of the process.

The basic principle of gprof

Gprof lets you know where your code is time-consuming, what functions are called many times, and allows you to see the call relationship between functions and functions at a glance. Gprof is a performance diagnostic tool supported by the gcc/g++ compiler. As long as the-PG option is added at compile time, the compiler will add a Mcount function call at the beginning of each function at compile time, call this mcount function before each function call, save the call graph of the function and the call time of the function and the number of times in the Mcount. Finally, when the program exits in the Gmon.out file, it is important to note that the program must exit normally or exit by exit, because the program writes the Gmon.out file whenever exit () is called.

Then, the use of gprof is mainly the following three steps:

    • The program is compiled with the-PG parameter
    • Run the program and exit normally
    • View Gmon.out File

Gprof Use Instances

  1. #include <iostream>
  2. Using namespace std;
  3. int Add (int A, int b)
  4. {
  5. return a+b;
  6. }
  7. int sub (int A, int b)
  8. {
  9. return a-B;
  10. }
  11. int call ()
  12. {
  13. Std::cout << Add (+) << Std::endl;
  14. Std::cout << Sub (2,4) << Std::endl;
  15. }
  16. int main ()
  17. {
  18. int a=1, b=2;
  19. cout << Add (A, b) << Endl;
  20. For (int i=0; i<10000; i++)
  21. Call ();
  22. return 0;
  23. }

Compile with g++ and add the-PG parameter:

    1. g++-O Hello hello_grof.cpp-pg-g

To get the executable file, we can use readelf to see what the difference is between its symbol table and what was compiled without-PG: Readelf-r./hello and Readelf-r./hello_normal results.

The left side is the result of compiling with the-PG parameter. You can see that there are three more function symbols _mcount, __monstartup, _mcleanup are calls related to Gprof.

Using GDB to debug the Hello program, the break point in the Mcount function can also see its invocation relationship, and the Mcount function is called before the Add function executes:

Next, run the program./hello, which generates the Gmon.out file in the current directory. To view file information using Gprof:

    1. Gprof-b./hello gmon.out
Get the following output:
  1. Flat Profile:
  2. Each sample counts as 0.01 seconds.
  3. No time accumulated
  4. % cumulative self to total
  5. Time seconds seconds calls Ts/call Ts/call name
  6. 0.00 0.00 0.00 10001 0.00 0.00 Add (int, int)
  7. 0.00 0.00 0.00 10000 0.00 0.00 Sub (int, int)
  8. 0.00 0.00 0.00 10000 0.00 0.00 Call ()
  9. 0.00 0.00 0.00 1 0.00 0.00 Global Constructors keyed to _z3addii
  10. 0.00 0.00 0.00 1 0.00 0.00 __STATIC_INITIALIZATION_AND_DESTRUCTION_0 (int, int)
  11. Call graph
  12. Granularity:each sample hit covers 2 byte (s) No time propagated
  13. Index% time self children called name
  14. 0.00 0.00 1/10001 Main [7]
  15. 0.00 0.00 10000/10001 Call () [10]
  16. [8] 0.0 0.00 0.00 10001 Add (int, int) [8]
  17. -----------------------------------------------
  18. 0.00 0.00 10000/10000 Call () [10]
  19. [9] 0.0 0.00 0.00 10000 SUB (int, int) [9]
  20. -----------------------------------------------
  21. 0.00 0.00 10000/10000 Main [7]
  22. [Ten] 0.0 0.00 0.00 10000 Call () [10]
  23. 0.00 0.00 10000/10001 Add (int, int) [8]
  24. 0.00 0.00 10000/10000 Sub (int, int) [9]
  25. -----------------------------------------------
  26. 0.00 0.00 1/1 __do_global_ctors_aux [13]
  27. [One] 0.0 0.00 0.00 1 Global Constructors keyed to _z3addii [11]
  28. 0.00 0.00 1/1 __STATIC_INITIALIZATION_AND_DESTRUCTION_0 (int, int) [12]
  29. -----------------------------------------------
  30. 0.00 0.00 1/1 Global Constructors keyed to _z3addii [11]
  31. [0.0] 0.00 0.00 1 __STATIC_INITIALIZATION_AND_DESTRUCTION_0 (int, int) [12]
  32. -----------------------------------------------
  33. Index by Function name
  34. [One] global constructors keyed to _z3addii (hello_grof.cpp) [9] Sub (int, int) [Ten] Call ()
  35. [8] Add (int, int) [__STATIC_INITIALIZATION_AND_DESTRUCTION_0] (int, int) (HELLO_GROF.CPP)

You can use the Run command:

    1. Gprof-b./hello Gmon.out | gprof2doc.py > ~www/hello.dot

To generate a call graph file in dot format, you can use the Windows version of Gvedit for Graphviz software to view the call graph:

Attach a more complex program call graph:

For call relationships and call hotspots at a glance.

Gprof Output Interpretation

This section removes the-b parameter in Gprof-b./hello and displays a detailed description of what the field means:

  1. The percentage of the total running time of the
  2. Used by this function.
  3. 16
  4. Cumulative a running sum of the number of seconds accounted
  5. seconds for by this function and those listed above it.
  6. 19
  7. Self the number of seconds accounted
  8. seconds function alone. The major sort for this
  9. Listing.
  10. 23
  11. Calls the number of times this function is invoked, if
  12. This function is profiled, else blank.
  13. 26
  14. The average number of milliseconds spent in this
  15. Ms/call function per call, if the function is profiled,
  16. else blank.
  17. 30
  18. Total the average number of milliseconds spent in this
  19. Ms/call function and its descendents per call, if this
  20. The function is profiled, else blank.
  21. 34
  22. The name of the function. This is the minor sort
  23. Listing. The index shows the location of
  24. PNS the function in the GPROF listing. If the index is
  25. Parenthesis in the It shows where it would appear in
  26. The GPROF listing if it were to be printed.


Summarize

Gprof is a common profiling tool that lists some of its shortcomings and is also viewed from the Web:

    • 1, the multi-threaded support is not good, inaccurate
    • 2. Must exit Exit ()
    • 3. It can only analyze the user time consumed by the application during operation, and cannot get the running time of the program kernel space. There is nothing to call analysis of the kernel state. If the program system call ratio is relatively large, it is not appropriate.

Linux C + + programs for Profiling Tools Gprof Getting Started

Related Article

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.