【转】有些时候,咱们特别关注程序的性能,特别是底层软件,好比驱动程序,OS等。为了更好的优化程序性能,咱们必须找到性能瓶颈点,“好钢用在刀刃上”才能取得好的效果,不然可能白作工做。为了找到关键路径,咱们可使用profilng技术,在linux平台上,咱们可使用gprof和oprofile工具。html
- gprof是GNU工具之一,它在编译的时候在每一个函数的出入口加入了profiling的代码,运行时统计程序在用户态的执行信息,能够获得每一个函数的调用次数,执行时间,调用关系等信息,简单易懂。适合于查找用户级程序的性能瓶颈,对于不少时间都在内核态执行的程序,gprof不适合。
- oprofile也是一个开源的profiling工具,它使用硬件调试寄存器来统计信息,进行profiling的开销比较小,并且能够对内核进行profiling。它统计的信息很是的多,能够获得cache的缺失率,memory的访存信息,分支预测错误率等等,这些信息gprof是得不到的,可是对于函数调用次数,它是不可以获得的。。
简单来讲,gprof简单,适合于查找用户级程序的瓶颈,而oprofile稍显复杂,可是获得的信息更多,更适合调试系统软件。
咱们以编译运行hello.c为例,来讲明如何使用这两个工具,这里不解释具体结果的含义,要想详细了解每一个结果表明什么意思,能够看一下参考资料中官方站点上的doc信息,里面会给你详尽的解释。
gprof Quick Start
gprof是gnu binutils工具之一,默认状况下linux系统当中都带有这个工具。
- 使用 -pg 选项来编译hello.c,若是要获得带注释的源码清单,则须要增长 -g 选项。运行: gcc -pg -g -o hello hello.c
- 运行应用程序: ./hello 会在当前目录下产生gmon.out文件
- 使用gprof来分析gmon.out文件,须要把它和产生它的应用程序关联起来:
- gprof hello gmon.out -p 获得每一个函数占用的执行时间
- gprof hello gmon.out -q 获得call graph,包含了每一个函数的调用关系,调用次数,执行时间等信息。
- gprof hello gmon.out -A 获得一个带注释的“源代码清单”,它会注释源码,指出每一个函数的执行次数。这须要在编译的时候增长 -g选项。
oprofile Quick Start
oprofile是sourceforge上面的一个开源项目,在2.6内核上带有这个工具,好像只有smp系统才有。比较老的系统,须要本身安装,从新编译内核。
oprofile是一套工具,分别完成不一样的事情。
op_help: 列出全部支持的事件。
opcontrol:设置须要收集的事件。
opreport: 对结果进行统计输出。
opannaotate:产生带注释的源/汇编文件,源语言级的注释须要编译源文件时的支持。
opstack: 产生调用图profile,但要求x86/2.6的平台,而且linux2.6安装了call-graph patch
opgprof: 产生如gprof类似的结果。
oparchive: 将全部的原始数据文件收集打包,能够到另外一台机器上进行分析。
op_import: 将采样的数据库文件从另外一种abi转化成本地格式。
运行oprofile须要root权限,由于它要加载profile模块,启动oprofiled后台程序等。因此在运行以前,就须要切换到root。
- opcontrol --init 加载模块,mout /dev/oprofile 建立必需的文件和目录
- opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决定是否对kernel进行profiling
- opcontrol --reset 清除当前会话中的数据
- opcontrol --start 开始profiling
- ./hello 运行应用程序,oprofile会对它进行profiling
- opcontrol --dump 把收集到的数据写入文件
- opcontrol --stop 中止profiling
- opcotrol -h 关闭守护进程oprofiled
- opcontrol --shutdown 中止oprofiled
- opcontrol --deinit 卸载模块
经常使用的是3→7这几个过程,获得性能数据以后,可使用opreport, opstack, opgprof, opannotate几个工具进行分析,我经常使用的是opreport, opannotate进行分析。
- opreport使用 http://oprofile.sourceforge.net/doc/opreport.html
- opannotate使用 http://oprofile.sourceforge.net/doc/opannotate.html
- opgprof使用 http://oprofile.sourceforge.net/doc/opgprof.html
最经常使用的是opreport,这个能够给出image和symbols的信息,好比我想获得每一个函数的执行时间占用比例等信息,用来发现系统性能瓶颈。opannotate能够对源码进行注释,指出哪一个地方占用时间比较多。经常使用命令以下:
- opreport -l /bin/bash --exclude-depand --threshold 1 , 用来发现系统瓶颈。
- 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
网络资源
- gprof 用户手册 http://sourceware.org/binutils/docs-2.17/gprof/index.html
- oprofile官方站点 http://oprofile.sourceforge.net/
- 使用 GNU profiler 来提升代码运行速度 http://www-128.ibm.com/developerworks/cn/linux/l-gnuprof.html
- 使用 OProfile for Linux on POWER 识别性能瓶颈 http://www-128.ibm.com/developerworks/cn/linux/l-pow-oprofile/