Linux性能分析工具perf基础使用介绍
perf是Linux內核內置的性能分析工具。從內核版本2.6.31開始出現該工具,如果沒有安裝,可以使用以下命令進行安裝
yum -y install perf.x86_64
這里我們主要介紹一下如何使用,一些具體的背景知識,請查閱參考鏈接。
perf list列出能查看的性能事件,這里可以看做是事件的一些分類
#perf list -h usage: perf list [hw|sw|cache|tracepoint|pmu|event_glob]可以進一步查看hw(Hardware)分類的事件,只舉例,不再列舉
#perf list hwcpu-cycles OR cycles [Hardware event]instructions [Hardware event]cache-references [Hardware event]cache-misses [Hardware event]branch-instructions OR branches [Hardware event]branch-misses [Hardware event]stalled-cycles-frontend OR idle-cycles-frontend [Hardware event]stalled-cycles-backend OR idle-cycles-backend [Hardware event]ref-cycles [Hardware event]使用perf stat
一個簡單的c程序,foo1和foo2分別調用longa()100次和10次 # cat perf.c //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(); } # cc perf.c # perf stat ./a.outPerformance counter stats for './a.out':331.445189 task-clock (msec) # 0.999 CPUs utilized 0 context-switches # 0.000 K/sec 1 cpu-migrations # 0.003 K/sec 104 page-faults # 0.314 K/sec 571,668,722 cycles # 1.725 GHz [83.39%]241,223,545 stalled-cycles-frontend # 42.20% frontend cycles idle [83.41%]2,915,367 stalled-cycles-backend # 0.51% backend cycles idle [66.82%]550,449,588 instructions # 0.96 insns per cycle # 0.44 stalled cycles per insn [83.41%]110,422,677 branches # 333.155 M/sec [83.40%]6,461 branch-misses # 0.01% of all branches [83.10%]0.331874225 seconds time elapsed 以上也可以通過-e選項,指定需要分析的事件,也就是perf list列出的事件,多個事件可以通過逗號分隔。如下,添加 cache-references,cache-misses選項查看cache相關的事件。 #perf stat -e cache-references,cache-misses ./a.outPerformance counter stats for './a.out':10,718 cache-references 2,249 cache-misses # 20.983 % of all cache refs 0.340821068 seconds time elapsed使用perf top
打開一個窗口,新建一個數據庫,并用pgbech進行數據寫入測試 hank=# \c postgres postgres You are now connected to database "postgres" as user "postgres". postgres=# create database perf_database; CREATE DATABASE postgres=# grant all on DATABASE perf_database to hank; GRANT postgres=# \q$pgbench -i perf_database -h 127.0.0.1 -U hank perf_database dropping old tables... NOTICE: table "pgbench_accounts" does not exist, skipping NOTICE: table "pgbench_branches" does not exist, skipping NOTICE: table "pgbench_history" does not exist, skipping NOTICE: table "pgbench_tellers" does not exist, skipping creating tables... generating data... 100000 of 100000 tuples (100%) done (elapsed 0.06 s, remaining 0.00 s) vacuuming... creating primary keys... done.執行壓測pgbench pgbench -M prepared -n -r -P 1 -h 127.0.0.1 -U hank perf_database -c 32 -j 32 -T 600另外一個窗口使用perf top 進行查看,如下
perf top -agv -F 1000
命令參數見perf top --help,這里a指所有cpu,-g表示打開call-graph,-v表示展示更多信息,-F 表示頻率
perf record和perf report使用,一般這一對命令一起使用
先看一個簡單的例子:
還是上面c程序例子 編譯 cc perf.c 收集 perf record -g ./a.out 生成報告 perf report -vg可以看到foo1和foo2 在cpu使用上的比例,分別是90.76%和8.88%,基本符合代碼,代碼中一個循環100次,一個10次
再看一個postgresql使用pgbench的例子:
一個窗口執行pgbench pgbench -M prepared -n -r -P 1 -U hank perf_database -c 32 -j 32 -T 600另外的窗口收集信息 收集20秒,當然也可以ctrl+c終止。 perf record -avg -- sleep 20生成報告 perf report -v -n -g也可以使用–stdion查看
perf report -v -n -g --stdio如下圖
參考:
https://www.ibm.com/developerworks/cn/linux/l-cn-perf1/
https://www.ibm.com/developerworks/cn/linux/l-cn-perf2/
https://github.com/digoal/blog/blob/master/201611/20161129_01.md
總結
以上是生活随笔為你收集整理的Linux性能分析工具perf基础使用介绍的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 完美解决Win10 X64非原装版系统
- 下一篇: CPU 已不足以驱动屏幕指纹识别技术,于