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.
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
Run the application:./hello will generate the gmon. out file in the current directory.
To use gprof to analyze the gmon. out file, you need to associate it with the application that generates it:
Gprof hellogmon. out-p obtains the execution time of each function.
Gprof hellogmon. out-q obtains the call graph, including the call relationship, number of calls, and execution time of each function.
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: lists 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: generate the call graph profile, but the x86/2.6 platform is required, and the call-graph patch is installed in linux2.6.
Opgprof: generate results similar to gprof.
Oparchive: Collects and packages all raw data files and analyzes them on another machine.
Op_import: converts the sampled database files from another abi to the 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.
Opcontrol -- init loading module, mout/dev/oprofile to create necessary files and directories
Opcontrol -- no-vmlinux or opcontrol -- vmlinux =/boot/vmlinux-'uname-R' determines whether to profiling the kernel.
Opcontrol -- reset: clear data in the current session
Opcontrol -- start starts profiling
./Hello run the application, oprofile will profiling it
Opcontrol -- dump writes collected data to a file
Opcontrol -- stop profiling
Opcotrol-h disable the daemon oprofiled
Opcontrol -- shutdown to stop oprofiled
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.
Opreport use http://oprofile.sourceforge.net/doc/opreport.html
Opannotate http://oprofile.sourceforge.net/doc/opannotate.html
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
Gprof user manual http://sourceware.org/binutils/docs-2.17/gprof/index.html
Http://oprofile.sourceforge.net/oprofile Official Site/
Use GNU profiler to speed up code http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
Identifying performance bottlenecks http://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/ with OProfile for Linux on POWER
From: txgc1009 Column