Use GPROF and oprofile to find performance bottlenecks

Source: Internet
Author: User
Sometimes, we pay special attention to program performance, especially the underlying software, such as drivers and OS. In order to better optimize the program performance, we must find the performance bottleneck, "good steel is used on the blade" to achieve good results, otherwise it may be done in vain. To find the key path, we can use profilng technology. On the Linux platform, we can use GPROF and oprofile tools.
  • GPROF is one of the GNU tools. during compilation, it adds profiling code to the entrances and exits of each function. during runtime, the statistics program executes information in the user State, you can obtain the number of calls, execution time, call relationship, and other information of each function, which is easy to understand. It is suitable for finding performance bottlenecks of user-level programs. GPROF is not suitable for programs that are executed in kernel state for many times.
  • Oprofile is also an open-source profiling tool. It uses hardware debugging registers to collect statistics. It has a low overhead for profiling and can perform profiling on the kernel. It collects a lot of statistics, and obtains the cache loss rate, memory access information, and branch prediction error rate. GPROF is not available, but for the number of function calls, it cannot be obtained ..

   To put it simply, GPROF is simple and suitable for finding Bottlenecks of user-level programs. oprofile is a little complicated, but more information is obtained, which is more suitable for debugging system software.
   Let's compile and run Hello. C is used as an example to describe how to use these two tools. Here we do not explain the meaning of the specific results. To learn more about the meaning of each result, you can refer to the official site Doc information in the reference documents, it will give you a detailed explanation.
GPROF Quick Start
   GPROF is one of the gnubinutils tools. It is included in Linux by default.

  1. Use the-PG option to compile hello. C. If you want to get the source code list with annotations, you need to add the-G option. Run:
    Gcc-PG-g-o hellohello. c
  2. Run the application:./HelloThe gmon. Out file is generated in the current directory.
  3. To use GPROF to analyze the gmon. Out file, you need to associate it with the application that generates it:
    1. GPROF hellogmon. Out-P obtains the execution time of each function.
    2. GPROF hellogmon. Out-Q obtains the call graph, including the call relationship, number of calls, and execution time of each function.
    3. GPROF hellogmon. Out-A gets a "Source code list" with comments, which comments the source code and indicates the number of times each function is executed. This requires the-G option to be added during compilation.

Oprofile Quick Start
   Oprofile is an open-source project on SourceForge. It comes with this tool on the 2.6 kernel. It seems that only SMP systems are available. For older systems, you need to install and re-compile the kernel.
   Oprofile is a set of tools to accomplish different things. Op_help:List all supported events.
Opcontrol: Set the events to be collected.
Opreport: outputs statistical results.
Opannaotate: generates source/assembly files with annotations. Source Language-level annotations must be supported when source files are compiled.
Opstack:  Call Graph profile is generated, but the x86/2.6 platform is required, and call-graph patch is installed in linux2.6.
Opgprof:  Generate similar results such as GPROF.
Oparchive:You can collect and package all the original data files on another machine for analysis.
Op_import:Converts the sampled database files from another Abi to a local cost format.

   The root permission is required to run oprofile because it needs to load the profile module and start the oprofiled background program. Therefore, you need to switch to root before running.

  1. Opcontrol -- initLoad module, MOUT/dev/oprofile to create necessary files and directories
  2. Opcontrol -- no-vmlinux or opcontrol -- vmlinux =/boot/vmlinux-'uname-R' determines whether to profiling the kernel.
  3. Opcontrol -- reset: clear data in the current session
  4. Opcontrol -- start starts profiling
  5. ./Hello run the application, oprofile will profiling it
  6. Opcontrol -- dump writes collected data to a file
  7. Opcontrol -- Stop stop profiling
  8. Opcotrol-H disable the daemon oprofiled
  9. Opcontrol -- shutdown to stop oprofiled
  10. Opcontrol -- deinit uninstall the module

The commonly used process is 3 to 7. After obtaining performance data, you can use opreport, opstack, opgprof, and opannotate tools for analysis. I usually use opreport and opannotate for analysis.

  1. Opreport use http://oprofile.sourceforge.net/doc/opreport.html
  2. Opannotate http://oprofile.sourceforge.net/doc/opannotate.html
  3. Opgprof http://oprofile.sourceforge.net/doc/opgprof.html

The most common information is opreport, which provides image and symbols information. For example, I want to obtain the execution time ratio of each function and other information to identify system performance bottlenecks. Opannotate can be used to comment out the source code, indicating the location that occupies a large amount of time. Common commands are as follows:

  • Opreport-L/bin/Bash -- exclude-depand -- threshold 1 is used to detect system bottlenecks.
  • Opannotate -- source -- output-Dir = annotated/usr/local/oprofile-PP/bin/oprofiled
  • Opannotate -- source -- Base-dirs =/tmp/build/libfoo/-- search-dirs =/home/user/libfoo/-- output-Dir = annotated/lib/libfoo. So

Network Resources

  1. GPROF user manual http://sourceware.org/binutils/docs-2.17/gprof/index.html
  2. Http://oprofile.sourceforge.net/oprofile Official Site/
  3. Use GNU profiler to speed up code http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
  4. Use oprofile for Linux on power to identify performance bottlenecksHttp://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/

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.