天天看點

RH442-2 Kernel層面的性能微調工具--Oprofile

Kernel層面的性能微調工具--Oprofile

一、用途及關鍵特性

1、非侵入式,無需重新編譯系統.

2、Kernel層面的Profile, All code is profiled.

3、利用硬體計數器.低overhead.

二、安裝

1.   硬體要求:IA-32、IA-64、AMD64、PowerPC

#如果處理器是其他處理器,否則oprofile無法采集到資料

2.   首先需要安裝核心擴充包kernel-debuginfo

[root@station8 ~]#rpm -i kernel-debuginfo-common-2.6.18-194.el5.rpm

[root@station8 ~]#rpm -i kernel-debuginfo-2.6.18-194.el5.rpm

#kernel-debug和kernel-debuginfo是兩個不同的軟體包,kernel-debug是kernel調試代碼,kernel-debuginfo是擴充核心代碼

#安裝好的vmlinux在這裡: /usr/lib/debug/lib/modules/2.6.18-194.el5/vmlinux

3.       Oprofile安裝

[root@station8 ~]# yum install oprofile.i386

[root@station8 ~]# yum install oprofile-gui.i386

三、Oprofile設定

1、設定觀察事件:

opcontrol --setup --event=name:count:unitmask:kernel:user --event=xxxx

常用的事件有以下幾種:

CPU_CLK_UNHALTED: CPU執行時間

LLC_MISSES:  末級Cache miss

DTLB_MISSES: 資料TLB miss準備我們的程式

[root@station8 ~]# opcontrol --setup --event CPU_CLK_UNHALTED:6000:0:0:1

#我們的程式,包括核心驅動都需要有符号資訊:

#檢視核心的導出的符号資訊:cat /proc/kallsyms

2、初始化Oprofile

[root@station8 ~]#opcontrol --init  

#加載oprofile核心子產品

[root@station8 ~]#opcontrol --setup --no-vmlinux

#我們對核心的取樣沒興趣,如不收件核心資訊,可這樣設定

[root@station8 ~]#opcontrol --setup --vmlinux=/usr/lib/debug/lib/modules/(uname –r)/vmlinux

#我們需要核心的取樣,如需收件核心資訊,需設定核心

四、采樣資料

1、清除上一次采樣到的資料

[root@station8 ~]#opcontrol --reset

2、啟動oprofiled守護程式,從核心中采集資料

[root@station8 ~]#pcontrol --start

3、運作要采集其資訊的應用程式

[root@station8 ~]#vim /root/ex1.c            #測試程式

#include <string.h>

const char* find_str(const char* s, int l){

  const char* e = s+l;

  while(s <e) {

    if(*s == '<') return s;

    s++;

  }

}

int   main(int argc, char* argv[]) {

  char*s = argv[1];

  int   i, l;

  if(argc ==1) return -1;

  l=strlen(s);

  for(i   =   0;   i <   100000000;   i++) find_str(s, l);

  return   0;

[root@station8 ~]#gcc  /root/ex1.c -o /root/ex1

#編譯程式

[root@station8 ~]#time ./ex1 helloworld

3.143u 0.001s 0:03.14 100.0%    0+0k 0+0io 0pf+0w

#運作程式,time是計算運作程式的時間

4、中途收集采樣資料

[root@station8 ~]#opcontrol –dump

#預設采集資料存放在/var/lib/oprofile/samples/下

5、關閉守護程式, 同時準備好采樣的資料

[root@station8 ~]#opcontrol --stop或

[root@station8 ~]#opcontrol --shutdown

五、采樣報告

[root@station8 ~]#opreport --long-filenames

#系統級别的

[root@station8 ~]#opreport image:ex1 –l

#子產品級别的

[root@station8 ~]#opannotate image:ex1 –s

#源碼級别的

六、最常用的指令

opcontrol --init

opcontrol --setup --no-vmlinux

opcontrol --status

opcontrol --start

opcontrol --dump

opcontrol --shutdown

opcontrol --reset

opreport --long-filenames

opreport image:filename -l

opannotate image:filename -s

本文轉自netsword 51CTO部落格,原文連結:http://blog.51cto.com/netsword/561988