您的位置:首页 > 运维架构 > Linux

Centos6下通过 oprofile分析CPU性能

2014-01-18 10:55 204 查看

环境

Centos 6.4 x86_64

oprofile-0.9.9

oprofile安装

#wget http://ncu.dl.sourceforge.net/project/oprofile/oprofile/oprofile-0.9.9/oprofile-0.9.9.tar.gz #tar -zxvf oprofile-0.9.9.tar.gz
#cd oprofile-0.9.9
#./configure
#make
#make install



oprofile使用

常用命令:

使用oprofile检测CPU,需要经过初始化、启动、导出数据、查看结果四个步骤。

初始化

opcontrol --no-vmlinux : oprofile启动后,不记录内核模块、内核代码相关统计数据

opcontrol --init : 加载oprofile模块、oprofile驱动程序

oprofile控制

opcontrol --start : 指示oprofile启动检测

opcontrol --dump : 指示将oprofile检测到的数据写入文件

opcontrol --reset : 清空之前检测的数据记录

opcontrol --h : 关闭oprofile进程

查看结果

opreport :默认查看oprofile检测结果

opreport -l : 以函数的角度显示检测结果

opreport -l oprofile_test : 以函数的角度,针对oprofile_test进程显示检测结果

opannotate -s oprofile_test : 以代码的角度,针对oprofile_test进程显示检测结果

opannotate -s /lib64/libc-2.12.so : 以代码的角度,针对libc-2.4.so库显示检测结果

在oprofile安装完成后,第一步初始化oprofile

[root@compute oprofile-0.9.9]# opcontrol --init


而后启动oprofile

[root@compute oprofile-0.9.9]# opcontrol --start --no-vmlinux
ATTENTION: Use of opcontrol is discouraged.  Please see the man page for operf.
Using default event: CPU_CLK_UNHALTED:100000:0:1:1
Using 2.6+ OProfile kernel interface.
Using log file /var/lib/oprofile/samples/oprofiled.log
Daemon started.
Profiler running.


通过默认的方式查看CPU检测报告

[root@compute oprofile-0.9.9]# opreport | less
Using /var/lib/oprofile/samples/ for samples directory.
CPU: Intel Westmere microarchitecture, speed 2393.96 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
CPU_CLK_UNHALT...|
samples|      %|
------------------
361031 83.4576 vmlinux
32789  7.5797 ext4
14667  3.3905 jbd2
10162  2.3491 no-vmlinux
3178  0.7346 libc-2.12.so
1798  0.4156 mptbase
1386  0.3204 oprofiled
1380  0.3190 oprofile
1267  0.2929 opannotate
1242  0.2871 libpython2.6.so.1.0
1102  0.2547 mptscsih
924  0.2136 bash
290  0.0670 ld-2.12.so
201  0.0465 kvm
159  0.0368 bnx2
159  0.0368 libstdc++.so.6.0.13
120  0.0277 sd_mod


通过函数方式显示检测结果

[root@compute oprofile-0.9.9]# opreport -l | less
CPU: Intel Westmere microarchitecture, speed 2393.96 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
samples  %        image name               app name                 symbol name
107138   20.1679  no-vmlinux               no-vmlinux               /no-vmlinux
51905     9.7707  vmlinux                  vmlinux                  intel_idle
32789     6.1723  ext4                     ext4                     /ext4
32755     6.1659  vmlinux                  vmlinux                  port_inb
21769     4.0978  vmlinux                  vmlinux                  copy_user_generic_string
16469     3.1002  vmlinux                  vmlinux                  __clear_user
14667     2.7609  jbd2                     jbd2                     /jbd2
13871     2.6111  vmlinux                  vmlinux                  native_sched_clock
8691      1.6360  vmlinux                  vmlinux                  get_page_from_freelist
5864      1.1039  vmlinux                  vmlinux                  find_busiest_group
5701      1.0732  vmlinux                  vmlinux                  mark_page_accessed
2894      0.5448  vmlinux                  vmlinux                  __schedule
2888      0.5436  vmlinux                  vmlinux                  __list_del_entry
2817      0.5303  vmlinux                  vmlinux                  menu_select
2772      0.5218  vmlinux                  vmlinux                  kmem_cache_alloc
2652      0.4992  vmlinux                  vmlinux                  __alloc_pages_slowpath
2478      0.4665  vmlinux                  vmlinux                  __wake_up_bit
2438      0.4589  vmlinux                  vmlinux                  __hrtimer_start_range_ns
2428      0.4571  vmlinux                  vmlinux                  kmem_cache_free
2262      0.4258  vmlinux                  vmlinux                  __list_add


下同编写简单代码消耗CPU,通过opannotate来了解CPU的占用情况,代码如下:

#vi test.c
int main()
{
int a = 0, b = 0;
for (; a < 1000000000; a++  )
{
b++;
}
}
#gcc -o oprofile_test test.c


执行该代码

#./oprofile_test &


将检测数据写入文件

#opcontrol --dump


查看该代码的CPU检测数据

[root@compute ~]# opreport -l oprofile_test
Using /var/lib/oprofile/samples/ for samples directory.
CPU: Intel Westmere microarchitecture, speed 2393.96 MHz (estimated)
Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
samples  %        symbol name
492407   100.000  main


通过opannotate查看代码层CPU的占用情况

[root@compute ~]# opannotate -s oprofile_test
Using /var/lib/oprofile/samples/ for session-dir
/*
* Command line: opannotate -s oprofile_test
*
* Interpretation of command line:
* Output annotated source file with samples
* Output all files
*
* CPU: Intel Westmere microarchitecture, speed 2393.96 MHz (estimated)
* Counted CPU_CLK_UNHALTED events (Clock cycles when not halted) with a unit mask of 0x00 (No unit mask) count 100000
*/
/*
* Total samples for file : "/root/test.c"
*
* 492407 100.000
*/

:int main()
:{ /* main total: 492407 100.000 */
:        int a = 0, b = 0;
343285 69.7157 :        for (; a < 1000000000; a++  )
:                {
149122 30.2843 :                        b++;
:                }
:}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐