1. 程式人生 > >perf + Flame Graph火焰圖分析程序性能

perf + Flame Graph火焰圖分析程序性能

mic ron bsp pla mage define art 執行權限 統計

1、perf命令簡要介紹

性能調優時,我們通常需要分析查找到程序百分比高的熱點代碼片段,這便需要使用 perf record 記錄單個函數級別的統計信息,並使用 perf report 來顯示統計結果;

usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

 The most commonly used perf commands are:
   annotate        Read perf.data (created by perf record) and display annotated code
   archive         Create archive with 
object files with build-ids found in perf.data file bench General framework for benchmark suites buildid-cache Manage build-id cache. buildid-list List the buildids in a perf.data file data Data file related processing diff Read perf.data files and display the differential profile evlist List the event names
in a perf.data file inject Filter to augment the events stream with additional information kmem Tool to trace/measure kernel memory properties kvm Tool to trace/measure kvm guest os list List all symbolic event types lock Analyze lock events mem Profile memory accesses record Run a command and record its profile into perf.data report Read perf.data (created by perf record) and display the profile sched Tool to trace
/measure scheduler properties (latencies) script Read perf.data (created by perf record) and display trace output stat Run a command and gather performance counter statistics test Runs sanity tests. timechart Tool to visualize total system behavior during a workload top System profiling tool. trace strace inspired tool probe Define new dynamic tracepoints See perf help COMMAND for more information on a specific command.

舉例

perf record -e cpu-clock -g -p 222

-g 選項是告訴perf record額外記錄函數的調用關系

-e cpu-clock 指perf record監控的指標為cpu周期

-p 指定需要record的進程pid

程序運行完之後,perf record會生成一個名為perf.data的文件,如果之前已有,那麽之前的perf.data文件會被覆蓋

獲得這個perf.data文件之後,就需要perf report工具進行查看

perf report -i perf.data

-i 指定要查看的文件

以診斷fluentd為例,report結果:

$perf report -i perf.data
技術分享圖片

這種格式很不直觀

2、使用火焰圖展示結果

1、Flame Graph項目位於GitHub上:https://github.com/brendangregg/FlameGraph

2、可以用git將其clone下來:git clone https://github.com/brendangregg/FlameGraph.git

註意:git clone之後,下面用到的*.pl文件先給+x可執行權限,註意路徑

我們以perf為例,看一下flamegraph的使用方法:

1、第一步

$perf record -e cpu-clock -g -p 28591

Ctrl+c結束執行後,在當前目錄下會生成采樣數據perf.data.

2、第二步

用perf script工具對perf.data進行解析

perf script -i perf.data &> perf.unfold

3、第三步

將perf.unfold中的符號進行折疊:

./stackcollapse-perf.pl perf.unfold &> perf.folded

4、最後生成svg圖:

./flamegraph.pl perf.folded > perf.svg

技術分享圖片

perf + Flame Graph火焰圖分析程序性能