天天看点

使用Oprofile分析性能瓶颈

 1. oprofile简介

oprofile 是 Linux 平台上的一个功能强大的性能分析工具, 支持两种采样(sampling)方式:基于事件的采样(event based)和基于时间的采样(time based)。

基于事件的采样是oprofile只记录特定事件(比如L2 cache miss)的发生次数,当达到用户设定的 定值时oprofile 就记录一下(采一个样)。这种方式需要CPU 内部有性能计数器(performace counter)。

基于时间的采样是oprofile 借助OS 时钟中断的机制,每个时钟中断 oprofile 都会记录一次(采一次样)。引入的目的在于,提供对没有性能计数器 CPU 的支持。其精度相对于基于事件的采样要低。因为要借助 OS 时钟中断的支持,对禁用中断的代码oprofile不能对其进行分析。

oprofile 在Linux 上分两部分,一个是内核模块(oprofile.ko),一个为用户空间的守护进程(oprofiled)。前者负责访问性能计数器或者注册基于时间采样的函数(使用register_timer_hook注册之,使时钟中断处理程序最后执行profile_tick 时可以访问之),并采样置于内核的缓冲区内。后者在后台运行,负责从内核空间收集数据,写入文件。

2. oprofile安装

(1)从http://prdownloads.sourceforge.net/下载最新版的oprofile.现在最新的版本是0.9.3.

(2)解开下载的安装包,编译并安装

tar -xvzf oprofile-0.9.3.tar.gz

cd oprofile-0.9.3

./configure --with-kernel-support

make

make install

这样oprofile就被安装在/usr/local/bin下。可以通过在configure指定--exec-prefix来改变安装的目录。 安装完成后,在/usr/local/bin可以发现如下的工具集

/usr/bin/oprofiled 守护进程

/usr/bin/opcontrol 控制前端,负责控制与用户交互,用得最多

/usr/bin/opannotate 根据搜集到的数据,在源码或者汇编层面上注释并呈现给用户

/usr/bin/opreport 生成二进制镜像或符号的概览

/usr/bin/ophelp 列出oprofile支持的事件

/usr/bin/opgprof 生成gprof格式的剖析数据

3. oprofile使用

(1)初始化

opcontrol --init

(2)配置

主要设置计数事件和样本计数,以及计数的CPU模式(用户态、核心态)

opcontrol --setup --event=CYCLES:1000::0:1

则是设置计数事件为CYCLES,即对处理器时钟周期进行计数

样本计数为1000,即每1000个时钟周期,oprofile 取样一次。

处理器运行于核心态则不计数

运行于用户态则计数

--event=name:count:unitmask:kernel:user

name: event name, e.g. CYCLES or ICACHE_MISSES

count: reset counter value e.g. 100000

unitmask: hardware unit mask e.g. 0x0f

kernel: whether to profile kernel: 0 or 1

user: whether to profile userspace: 0 or 1

(3)启动

opcontrol --no-vmlinux --start

使用--no-vmlinux不分析内核

(4)分析如下的程序

#include 

main()

{

int i = 0, sum = 0;

for ( ; i < 100; i++ )

sum += i;

printf("sum=%d\n", sum);

while ( 1 ) { };

}

将以上程序存为count.c,然后编译连接生成可执行文件

gcc -g -o count count.c

./count &

用oprofile分析

opcontrol --dump

opreport -g -l ./count 

输出:

CPU: PIII, speed 800 MHz (estimated)

Counted CPU_CLK_UNHALTED events (clocks processor is not halted) with a unit mask of 0x00 (No unit mask) count 100000

samples % linenr info symbol name

205214 100.000 count.c:3 main

继续阅读