perf 性能分析工具


1. perf簡介

perf是一款Linux性能分析工具。Linux性能計數器是一個新的基於內核的子系統,它提供一個性能分析框架,比如硬件(CPU、PMU(Performance Monitoring Unit))功能和軟件(軟件計數器、tracepoint)功能。
通過perf,應用程序可以利用PMU、tracepoint和內核中的計數器來進行性能統計。它不但可以分析制定應用程序的性能問題(per thread),也可以用來分析內核的性能問題,當然也可以同事分析應用程序和內核,從而全面理解應用程序中的性能瓶頸。

2. perf 安裝

2.1 源碼安裝

終端進入/usr/src目錄,獲取源代碼

sudo apt-get install linux-source-x.x.x

進入linux-source.x.x.x,解壓linux-source.x.x.x.tar.bz2

sudo tar -jxvf linux-source-x.x.x.tar.bz2

進入linux-source-x.x.x/tools/perf/

make
make install

2.2 package 安裝

sudo apt install linux-tools-common

輸入perf ,按提示安裝缺少的組件

 

3. perf的使用

3.1 perf --help 查看提示

3.2 perf stat

概括精簡的方式提供被調試程序的整體情況和匯總數據

測試程序 t1:

//test.c 
void longa() 
{ 
  int i,j; 
  for(i = 0; i < 1000000; i++) 
  j=i; //am I silly or crazy? I feel boring and desperate. 
} 
 
void foo2() 
{ 
  int i; 
  for(i=0 ; i < 10; i++) 
       longa(); 
} 
 
void foo1() 
{ 
  int i; 
  for(i = 0; i< 100; i++) 
     longa(); 
} 
 
int main(void) 
{ 
  foo1(); 
  foo2(); 
}

編譯:

gcc – o t1 – g test.c

調優:perf stat ./t1 (需要root 權限)

x@x-VirtualBox:~/test$ sudo perf stat ./t1
[sudo] password for x:

 Performance counter stats for './t1':

            342.75 msec task-clock                #    0.990 CPUs utilized
                 2      context-switches          #    0.006 K/sec
                 0      cpu-migrations            #    0.000 K/sec
                45      page-faults               #    0.131 K/sec
   <not supported>      cycles
   <not supported>      instructions
   <not supported>      branches
   <not supported>      branch-misses

       0.346364798 seconds time elapsed

       0.332232000 seconds user
       0.014137000 seconds sys

統計信息:

  • task-clock :最高占用率的任務(cpu,io)
  • context-switches: 進程切換次數
  • 。。。

3.2 perf top

Perf top 用於實時顯示當前系統的性能統計信息。該命令主要用來觀察整個系統當前的狀態,比如可以通過查看該命令的輸出來查看當前系統最耗時的內核函數或某個用戶進程。

 

3.3 perf record

需要一些粒度更細的信息.

隨后可以通過其它工具(perf-report)對數據文件進行分析,結果類似於perf-top的

3.4 perf report

讀取perf record創建的數據文件,並給出熱點分析結果

 

參考: 1. Perf -- Linux下的系統性能調優工具,第 1 部分

    2. Perf -- Linux下的系統性能調優工具,第 2 部分

    3. 系統級性能分析工具 — Perf


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM