Linux性能测试工具:Oprofile, KFT, Gprof
(2011-11-02 14:19:44)1)收集CPU的performance counter。CPU里面有很多performance counter,打开
之后,会记录CPU某些事件的数量,比如cache miss, 指令数,指令时间等等。这些
counter需要编程才能使用。测量哪一段代码完全由自己掌握。
2)利用编译器的功能,在函数入口和出口自动加回调函数,在回调函数里面,记录入口
和出口的时间。收集这些信息,可以得到函数的调用流程和每个函数所花费的时间。
3)自己在代码里面加入时间测量点,测量某段代码执行的时间。这种工具看起来和#1的
作用差不多,但是由于performance counter编程有很多限制,所以这种工具有时还是有
用处的。
下面备忘下oprofile的知识点:
Oprofile 工具集:
- ophelp: 列出可用的事件,并带有简短的描述
- oprofiled:
守护进程 - opcontrol: 控制 Oprofile 的数据收集
- opreport :
生成二进制镜像或符号的概览 - opannotate:
根据搜集到的数据,在源码或者汇编层面上注释并呈现给用户 - opgprof:
生成 gprof 格式的剖析数据 - opimport: 将采样数据库文件从外部格式(abi)转换为本地格式
- oparchive: 将所有的原始数据文件收集打包,可以到另一台机器上进行分析。
运行oprofile需要root权限,因为它要加载profile模块,启动oprofiled后台程序等。
使用步骤:
- opcontrol
--init 加载模块,mount /dev/oprofile 创建必需的文件和目录 - opcontrol
--no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决定是否对kernel进行profiling - opcontrol
--reset 重置当前会话中的数据 - opcontrol
--start 开始profiling - ./prog-withe-args
运行应用程序,oprofile会对它进行profiling - opcontrol
--dump 把收集到的数据写入文件 - opcontrol
--stop 停止profiling - opcontrol
--shutdown 停止oprofiled - opcontrol
--deinit 卸载模块
在start前可以设置文件写入方式:
opcontrol --separate=<choice>
<choice>
none —
library —
kernel —
all —
也可指指定对事件进行设置:
opcontrol --event=L2_CACHE_MISS:500 ...
可以使用
通过
经过1-7步后可以使用opreport, opstack, opgprof, opannotate几个工具进行分析。
最常用的是opreport,这个可以给出image和symbols的信息,想得到每个函数的执行时间占用比例等信息,用来发现系统性能瓶颈。opannotate可以对源码进行注释,指出哪个地方占用时间比较多(最好编译时使用-g选项)。
通过opreport --help
参考:
http://blog.youkuaiyun.com/yili_xie/archive/2009/12/02/4925648.aspx