1.Profiling几种工具
profiling 包括几种:cpu profiling,即提到的性能分析。memory profiling,即分析程序性能。network profiling,即网络流量的分析。
另外,profiling又分两种:1, sampling。即采样方式。2, instruments,即插装代码技术。(包含: 编译期间,链接期间,运行期间)。
oprofile ---- 属于 sampling 方式,即不影响程序行为,也不需要重启程序,它是对cpu进行采样分析。
gprof ---- 属于插装技术,需在编译期间增加 -pg 选项,属于GNU 部分,不需安装,随着gcc附带。
valgrind --- 属于插装技术,需要在运行期间增加valgrind选项。(其中massif工具用于内存分析,callgrind用于性能分析)
google-perftools ---- 属于插装技术,需要在链接期间增加-L/usr/lib -lprofiler 选项。
2.gprof 分析linux程序性能瓶颈
gcc -pg -o test test.c
test运行的时候会把搜集的信息保存在gmoun.out中
fedora 不能使用-lc_p选项的问题
一般gprof只能查看用户函数信息。如果想查看库函数的信息,需要在编译是再加入“-lc_p”编译参数代替“-lc”编译参数,这样程序会链接libc_p.a库,才可以产生库函数的profiling信息。
需要安装glibc-profile,下载地址
http://ftp.riken.jp/Linux/fedora/core/updates/2/i386/glibc-profile-2.3.3-27.1.i386.rp。
gprof hello gmon.out -p 得到每个函数占用的执行时间
gprof hello gmon.out -q 得到call graph,包含了每个函数的调用关系,调用次数,执行时间等信息。
gprof hello gmon.out -A 得到一个带注释的“源代码清单”,它会注释源码,指出每个函数的执行次数。这需要在编译的时候增加 -g选项。
3.Oprofile性能调优
http://oprofile.sourceforge.net/news/
要使用oprofile 首先得采用打开oprofile支持的内核启动。
Oprofile需要重新编译内核,请确认:
.config 文件中设置:
CONFIG_PROFILING=y
CONFIG_OPROFILE=y
另外,还要在 Processor type and features 菜单中启用Local APIC 和 IO-APIC:
CONFIG_X86_LOCAL_APIC=y
CONFIG_X86_IO_APIC=y
查看处理起本身没有集成APIC
oprofile.ko内核模块已经被集成到linux 2.6内核中,所以只需要安装前端工具,可以从oprofile官方网站下载源码来进行安装,当前最新版本为0.9.4。
其支持两种采样(sampling)方式:基于事件的采样(event based)和基于时间的采样(time based)。
基于事件的采样是oprofile只记录特定事件(比如L2 cache miss)的发生次数,当达到用户设定的 定值时oprofile 就记录一下(采一个样)。这种方式需要CPU 内部有性能计数器(performace counter)。 现代CPU内部一般都有性能计数器,龙芯2E内部亦内置了2个性能计数器。
基于时间的采样是oprofile 借助OS 时钟中断的机制,每个时钟中断 oprofile 都会记录一次(采一次样)。 引入的目的在于,提供对没有性能计数器 CPU 的支持。其精度相对于基于事件的采样要低。因为要借助 OS 时钟中断的支持,对禁用中断的代码oprofile不能对其进行分析。
oprofile 在Linux 上分两部分,一个是内核模块(oprofile.ko),一个为用户空间的守护进程(oprofiled)。前者负责访问性能计数器或者注册基于时间采样的函数(使用register_timer_hook注册之,使时钟中断处理程序最后执行profile_tick 时可以访问之),并采样置于内核的缓冲区内。后者在后台运行,负责从内核空间收集数据,写入文件。
oprofile使用
初始化
opcontrol --init 加载模块,mout /dev/oprofile 创建必需的文件和目录
配置
//opcontrol --setup --event=CYCLES:1000::0:1
opcontrol --no-vmlinux 或者 opcontrol --vmlinux=/boot/vmlinux-`uname -r` 决定是否对kernel进行profiling
opcontrol --event=CPU_CLK_UNHALTED:5000
opcontrol --separate=kernel --separate=lib 分开记录每个程序的性能开销
opcontrol --reset 清楚当前会话中的数据
启动
opcontrol --start 开始profiling
运行待分析之程序
./hello 运行应用程序,oprofile会对它进行profiling
取出数据
opcontrol --dump 把收集到的数据写入文件
opcontrol --stop 停止profiling
opcotrol -h 关闭守护进程oprofiled
opcontrol --shutdown 停止oprofiled
opcontrol --deinit 卸载模块
oprofile的功能非常强大,可以对每个线程进行单独采样,也可以对每个CPU单独采样,这些都是通过opcontrol的--separate选项来完成的。
opcontrol --init
该命令会加载oprofile.ko模块,mount oprofilefs。成功后会在/dev/oprofile/目录下导出一些文件和目录如: cpu_type, dump, enable, pointer_size, stats/
配置
主要设置计数事件和样本计数,以及计数的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
注意:
opreport 在虚拟机下不能使用,物理机上都是没问题的。
--vmlinux指定的内核必须是没有被压缩过的原始的文件
--vmliunx指定的内核必须是当前系统运行的内核
在要对kernel以及modules进行测试时,--vmlinux必须要指定内核
初始化如果出现timer mode 说明出现冲突,应该调用rm /root/.oprofile/daemonrc删除该文件即可。
对于每一次测试过程中,如果需要进行事件的重新设置,就必须重启 daemon, 它是
一个守护进程。也就是说,收集数据完成后,要用 --shutdown 命令来停止 daemon, 而不是 -- stop( 此命令只是停止 profiling) ,这样再次进行— start 命令就可以使用新的事件设置;
/usr/bin/oprofiled 守护进程
/usr/bin/opcontrol 控制前端,负责控制与用户交互,用得最多
/usr/bin/opannotate 根据搜集到的数据,在源码或者汇编层面上注释并呈现给用户
/usr/bin/opreport 生成二进制镜像或符号的概览
/usr/bin/ophelp 列出oprofile支持的事件
/usr/bin/opgprof 生成gprof格式的剖析数据
opstack: 产生调用图profile,但要求x86/2.6的平台,并且linux2.6安装了call-graph patch
oparchive: 将所有的原始数据文件收集打包,可以到另一台机器上进行分析。
op_import: 将采样的数据库文件从另一种abi转化成本地格式。
最常用的是 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
交叉运程注意问题
内核的profile选项是都选择上的,注意另外还需要将eabi选项也选上才行
libpopt.so.0需要拷贝下去
/etc/目录下创建mtab并添加“nodev /dev/oprofile oprofilefs rw 0 0”
4.交叉编译oprofile
http://blog.csdn.net/nanjianhui/article/details/54812
http://blog.csdn.net/yuyin86/article/details/6671492
1. 首先要下载OProfile的相关软件包,如下:
oprofile-0.9.6.tar.gz: http://oprofile.sourceforge.net/download/
popt-1.14.tar.gz: http://freshmeat.net/projects/popt/
binutils-2.20.tar.gz: http://ftp.gnu.org/gnu/binutils/
注意:在下载oprofile-0.9.6.tar.gz的时候,建议通过git来下载,否则可能不能支持ARM-CortexA9.
git clone git://oprofile.git.sourceforge.net/gitroot/oprofile/oprofile
首先确认
标板上运行的Linux是否支持下面的选项:
Profiling Support
OProfile system profiling
Kernel performance events and counters
$ make goldfish_defconfig
$ make menuconfig
修改内核配置如下
General setup --->
[*] Profiling support (EXPERIMENTAL)
[ ] Activate markers
[*] OProfile system profiling (EXPERIMENTAL)
这是把OProfile直接编进内核,也可以选择[M] OProfile system profiling (EXPERIMENTAL)会在arch/arm/oprofile文件夹下生成oprofile.ko,oprofile.ko需要用insmod载入。
$make
[ARM] 5195/1: ARMv7 Oprofile support
http://kerneltrap.org/mailarchive/git-commits-head/2008/10/11/3608594
linux kernel proxy
http://groups.google.com/group/linux-kernel-proxy/msg/a776bd7701f4ee?dmode=source