code profiling

語音增強系列博文
本文基於c/c++。

perf

可以使用perf list列出所有支持的event。perf工具支持硬件和軟件事件,硬件事件由硬件計數器測量。
常關心的硬件事件如下:

cpu-cycles OR cycles
instructions
cache-references
cache-misses
branch-instructions OR branches
branch-misses
bus-cycles
stalled-cycles-frontend OR idle-cycles-frontend
stalled-cycles-backend OR idle-cycles-backend
ref-cycles

關心的軟件事件如下:

cpu-clock
task-clock
page-faults OR faults
context-switches OR cs
cpu-migrations OR migrations
minor-faults
major-faults
alignment-faults
emulation-faults

測量test_prof程序耗時如下:
這裏寫圖片描述
以上找到的是程序總的耗時。

找到程序中的熱點

1.使用perf record獲得prof.data性能分析文件。
這裏寫圖片描述
2.使用perf report查看性能分析文件
第一頁顯示的是分析包含的事件

Available samples                                                                                                                                
2K cpu-clock                                                                                                                                    ◆
6 faults   

第二頁是詳細統計
這裏寫圖片描述
從上面可以看到哪個函數耗時較多;
這裏寫圖片描述
從上面可以看到缺頁異常開銷在什麼地方。
也可以使用sudo perf report --stdio將統計信息直接顯示在終端上。

perf annotate

可以顯示源碼和彙編對應的性能分析。

實時監控系統狀態

perf top

這裏寫圖片描述

gprof

在編譯時需要加上-pg選項,

gcc -Wall -std=c99 -pg test_prof.c -o test_prof
然後運行測試程序生成gmon.out
./test_prof
最後生成詳細性能分析
gprof test_prof gmon.out > prof_output.txt

生成包括兩個部分,一個是程序的耗時,另外一部分是程序調用圖關係。

Flat profile:

Each sample counts as 0.01 seconds.
  %   cumulative   self              self     total           
 time   seconds   seconds    calls  ms/call  ms/call  name    
 97.38      0.53     0.53        1   525.84   525.84  func3
  3.75      0.55     0.02        1    20.22   546.07  func1
  0.00      0.55     0.00        1     0.00   525.84  func2
  0.00      0.55     0.00        1     0.00     0.00  func4

granularity: each sample hit covers 2 byte(s) for 1.83% of 0.55 seconds

index % time    self  children    called     name
                0.02    0.53       1/1           main [2]
[1]    100.0    0.02    0.53       1         func1 [1]
                0.00    0.53       1/1           func2 [3]
-----------------------------------------------
                                                 <spontaneous>
[2]    100.0    0.00    0.55                 main [2]
                0.02    0.53       1/1           func1 [1]
                0.00    0.00       1/1           func4 [5]
-----------------------------------------------
                0.00    0.53       1/1           func1 [1]
[3]     96.3    0.00    0.53       1         func2 [3]
                0.53    0.00       1/1           func3 [4]
-----------------------------------------------
                0.53    0.00       1/1           func2 [3]
[4]     96.3    0.53    0.00       1         func3 [4]
-----------------------------------------------
                0.00    0.00       1/1           main [2]
[5]      0.0    0.00    0.00       1         func4 [5]
-----------------------------------------------
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章