Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1106167
  • 博文数量: 254
  • 博客积分: 1242
  • 博客等级: 少尉
  • 技术积分: 1581
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-03 21:49
文章分类

全部博文(254)

文章存档

2017年(16)

2016年(4)

2013年(94)

2012年(140)

分类: LINUX

2013-02-19 16:39:07

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

为了分析程序的性能,找出系统性能瓶颈点,有必要用相应的性能分析工具对系统的性能进分析。Linux上常用的性能分析工具有gprofoprofile

  •   Gprof一般用于对用户态的程序进行简单分析
  •   Oprofile 功能较强大适合于对复杂系统的分析

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

1.Gprof的安装与使用

gprofgnu binutils工具之一,默认情况下linux系统当中都带有这个工具。

1)        源码的编译

为了能使用gprof来对代码进行分析,编译代码时必须添加-pg选项。如编译hello.c  gcc –g –pg –o hello hello.c

2)        运行hello,在当前目录下会产生gmon.out文件

3)        结果分析一般有三种方式

a)        gprof hello gmon.out -p 得到每个函数占用的执行时间

b)        gprof hello gmon.out -q 得到call graph,包含了每个函数的调用关系,调用次数,执行时间等信息。

c)        gprof hello gmon.out -A 得到一个带注释的源代码清单,它会注释源码,指出每个函数的执行次数。这需要在编译的时候增加 -g选项。

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

2.       Oprofile安装及使用

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

Oprofile也是一个开源的profiling工具,它使用硬件调试寄存器来统计信息,进行profiling的开销比较小,而且可以对内核进行profiling。它统计的信息非常的多,可以得到cache的缺失率,memory的访存信息,分支预测错误率等等,这些信息 gprof是得不到的,但是对于函数调用次数,它是不能够得到的。

Oprofile 是用于 Linux 的若干种评测和性能监控工具中的一种。它可以工作在不同的体系结构上,包括 IA32, IA64 AMD Athlon 系列。它的开销小,将被包含在(Linux2.6 版的内核中。

Oprofile可以帮助用户识别诸如循环的展开、高速缓存的使用率低、低效的类型转换和冗余操作、错误预测转移等问题。它收集有关处理器事件的信息,其中包括TLB的故障、停机、存储器访问、位于 DCU(数据高速缓存单元)中的总线路数、一个 DCU 故障的周期数,以及不可高速缓存的和可高速缓存的指令的获取数量。Oprofile是一种细粒度的工具,可以为指令集或者为函数、系统调用或中断处理例程收集采样。Oprofile 通过取样来工作。使用收集到的评测数据,用户可以很容易地找出性能问题。

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

Oprofile的运行需要一个内核态module和一个用户态的daemon程序。对于Linux2.6.x的内核已经集成了该module

在安装oprofile之前,通过命令:

cat /boot/config-`uname -r` | grep OPROFILE

查看内核包含了oprofile模块,如果出现

CONFIG_HAVE_OPROFILE=y
CONFIG_OPROFILE=m

这表示打开了OPROFILE,而且oprofile被编译成了一个module

CONFIG_HAVE_OPROFILE=y
CONFIG_OPROFILE=m

表示支持OPROFILE,而且oprofile被编译进了内核

如果系统已经打开了oprofile,则不需要重新编译内核

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

1.1   Oprofile的安装
1)        下载oprofile软件包,并用tar解压

2)        ./configure --with-linux=/usr/src/linux/ --with-qt-dir=/usr/lib/qt/  --with-kernel-support

3)        make4)        make install

 

oprofile 安装完成后会生成以下工具集:

 

/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 转化成本地格式。

当然,以现在最方便的用法;apt-get 来安装Oprofile;在此之前是可以先查看一下电脑上是否已经有安装包apt-cache search oprofile 找到直接apt-get 就OK了!!

Normal 0 7.8 磅 0 2 false false false EN-US ZH-CN X-NONE MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman","serif";}

1.1   Oprofile使用

1)        初始化

Sudo opcontrol --init

该命令会加载 oprofile.ko 模块, mount oprofilefs 成功后会在 /dev/oprofile/ 目录下导出些文件和目录如: cpu_type, dump, enable, pointer_size, stats

2)        event配置

1.配置 OProfile 是否应该监视内核以及对计数事件和样本计数值的设置,计数的 CPU 模式(用户态、核心态)是系统默认项;

如果 oprofile 监视评测内核自身:

opcontrol –vmlinux=/src/urc/linux-source-2.6.27/vmlinux

如果 oprofile 不用监视评测内核:

/**一般情况下可以直接使用如下设置无需特殊处理**/

opcontrol --no-vmlinux 

在此体系结构 Family10h 中默认的配置为:

/*选择默认即可*/

opcontrol –event=CPU_CLK_UNHALTED:100000

如果配置守护进程写入文件的方式,用以下命令:

opcontrol --separate=

可以是以下之一:

none — 不要分离档案(默认)

library — 为库生成每个应用程序的档案

kernel — 为内核和内核模块生成每个应用程序的档案

all — 为库生成每个应用程序的档案,为内核和内核模块生成每个应用程序的档案

注意问题:

1 oprofile 可以在此进行事件设置;

通过 opcontrol --list-events 命令可以查看此结构中支持的事件;

通过

Opcontrol    --event=L2_CACHE_MISS:500 --event=L1_DTLB_MISS_AND_L2_DTLB_HIT:500

--event=......... 命令来进行事件的设置;

此命令 --event 参数必须依次给出,无论有多少个不可分行

通过 opcontrol –status 命令可以查看自己已经配置的事件;

2 )对于每一次测试过程中,如果需要进行事件的重新设置,就必须重启 daemon, 它是

一个守护进程。也就是说,收集数据完成后,要用 --shutdown 命令来停止 daemon, 而不是 -- stop( 此命令只是停止 profiling) ,这样再次进行— start 命令就可以使用新的事件设置;

3 )一次评测结束后,旧的 profiling data 还是存在的,用— reset 或者— save 命令来清理或者保存数据;

3)        启动

opcontrol --start

4)        运行待分析之程序 

5)        取出数据

opcontrol –dump

数据被写进 /var/lib/oprofile/samples /oprofiled.log

6)        停止评测

opcontrol --stop

1.2   Oprofile结果分析

Oprofile结果分析有四种命令即opreport, opstack, opgprof, opannotate,其中常用的是opreportopannotate:

1)        Opreport使用

Opreport –l ./XXX 

Oprofile –l –r ./xxx

2)        Opannotate使用

opannotate --source --output-dir=annotated /…/xxx3)   Opgprof使用Opgprof /XXX 生成gprof格式的数据,当前目录下产生文件gmou.outGprof /XXX gmou.out>text.txt 生成txt格式的文

1)        Oprofile编译时出现的问题

在编译过程中,这一步出现的问题是:(1)缺少libiberty.h头文件;2)缺少popt

            libiberty.h头文件在binutils-devel package中,需要下载这个包进行安装;也可通过新立德进行安装。可以用sudo apt-get install binutil-dev  进行包安装。

Popt存在libpopt-devel package中,同样需要进行下载安装;

sudo apt-get install libpopt-dev

在以上的使用中特殊注意在opreport -l ./XXX 是XXX的路径问题;如果程序是用Makefile编译出来的那么在
make之后生成XXX可执行文件,
不能在make install之后对sbin下生成的可执行文件XXX进行性能测试;因为
在程序执行Make install的时候make install中有个脚本会掉strips会将程序编译过程中生成的文件symbol
列表清除而后将执行文件XXX拷贝到sbin/下;这样在用oprofile去测试性能时,会提示如下错误:

warning: [vdso] (tgid:15133 range:0xb7752000-0xb7753000) could not be found.
CPU: Core 2, speed 1200 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (Unhalted core cycles) count 100000
samples  %        image name               symbol name
-------------------------------------------------------------------------------
2        100.000  [vdso] (tgid:15133 range:0xb7752000-0xb7753000) (no symbols)
2        100.000  [vdso] (tgid:15133 range:0xb7752000-0xb7753000) (no symbols) [self]
-------------------------------------------------------------------------------

出现该问题就是因为symbol已经被删除所以出现错误而没办法用,至此我们应该选择make 程序的当前目录下生成的XXX可执行文件,这样只要在使用的过程中细心就没有什么问题了。






阅读(3425) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~