openresty 火焰圖收集

工具安裝

查看內核及kernel

注:本次安裝如下都是基於centos6的html

uname -a ; rpm -qa kernel\* | sortnginx

kernel,kernel-devel,kernel-debuginfo,kernel-debuginfo-common(先安裝kernel-debuginfo-common,在安裝 kernel-debuginfo) 這些版本必定要和內核版本保持一致git

相關rpm包下載:http://rpm.pbone.net (kernel,kernel-devel), http://debuginfo.centos.org (kernel-debuginfo,kernel-debuginfo-common)對應搜索下載rpm安裝,版本必定要對應,否則會報一些內核錯。github

安裝systemtap

yum install systemtapcentos

測試安裝是否成功:瀏覽器

stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}'tcp

安裝成功:svg

出現下面的狀況,是kernel-devel不匹配致使的函數

kernel-debug 版本不匹配工具

下載stapxx、FlameGraph、openresty-systemtap-toolkit 

https://github.com/openresty/openresty-systemtap-toolkit 基於SystemTap的OpenResty實時分析和診斷工具(包括nginx、luajit、ngx_Lua等)
https://github.com/openresty/stapxx 對SystemTap的簡單宏語言擴展
https://github.com/brendangregg/FlameGraph.git 火焰圖svg製做工具

經常使用命令說明

一、openresty-systemtap-toolkit

  1. sample-bt 採集C級別的on-CPU時間,分析cpu的使用率
    例:./sample-bt -p 8736 -t 5 -uk > a.bt       
    注:-t 5,指採集5s的數據,下同
  2. sample-bt-off-cpu 分析特定進程的非CPU時間(off-CPU),例如阻塞IO的操做等
    例:./sample-bt-off-cpu -p 10901 -t 5 > a.bt
  3. ngx-sample-lua-bt  lua代碼級別的on-CPU時間採集
    例:./ngx-sample-lua-bt -p 9766 --luajit21 -t 5 > tmp.bt  爲了方便查看,結合  ./fix-lua-bt tmp.bt > a.bt  (修復ngx_lua的原始回溯)使用
    注:–luajit21 指的lua版本 
  4. 能夠經過 ngx-lua-tcp-recv-time,ngx-lua-udp-recv-time,ngx-single-req-latency 採集與上游服務通訊的狀況(如 tcp)
  5. 更多其餘應用請參考: https://github.com/openresty/openresty-systemtap-toolkit#tools

二、stapxx(有不少與openresty-systemtap-toolkit功能類似的命令

  1. lj-lua-stacks lua代碼級別的on-CPU火焰圖工具,lua層面上分析CPU時間 (要求:LuaJIT 2.1
    例:./samples/lj-lua-stacks.sxx --skip-badvars -x 6949 > a.bt 

           ./samples/lj-lua-stacks.sxx --arg time=5 --skip-badvars -x 6949 > a.bt   帶採集時間

           能夠結合  openresty-systemtap-toolkit 的 fix-lua-bt參考ngx-sample-lua-bt的介紹

           生成火焰圖:stackcollapse-stap.pl a.bt > a.cbt   stackcollapse-stap.pl a.bt > a.cbt  flamegraph.pl --encoding="ISO-8859-1" --title="Lua-land on-CPU flamegraph" a.cbt > a.svg

  2. sample-bt-leaks 在採用週期對基於未釋放的glibc內置項(malloc、calloc、realloc)的內存分配進行採樣
    例:./samples/sample-bt-leaks.sxx -x 16795 --arg time=5 -D STP_NO_OVERLOAD -D MAXMAPENTRIES=10000 > a.bt   stackcollapse-stap.pl a.bt > a.cbt   flamegraph.pl --countname=bytes --title="Memory Leak Flame Graph" a.cbt > a.svg
  3. 更多其餘應用請參考:https://github.com/openresty/stapxx#samples

生成火焰圖

收集調試信息

cd /opt/stapxx
export PATH=$PWD:$PATH (能夠加到環境變量裏面)

收集內存:

./samples/sample-bt-leaks.sxx -x 12130 -v > /opt/flame_img/ngx_mem_1.bt   注:12130 進程PID

收集cpu:

./samples/lj-lua-stacks.sxx --skip-badvars -x 12130 > /opt/flame_img/ngx_cpu_1.bt

./fix-lua-bt /opt/flame_img/ngx_cpu_1.bt > /opt/flame_img/ngx_cpu_1.bt

注:裏面目錄是個人環境目錄,能夠放到任意目錄,可是必須注意權限問題

可能遇到錯誤:

cannot find module /lib64/libc-2.12.so debuginfo: No DWARF information found [man warning::debuginfo]

解決:首先查看libc的版本 rpm -aq | grep glibc

在 http://debuginfo.centos.org/6/x86_64/  下載對應的 glibc-debuginfo-common glibc-debuginfo,先安裝glibc-debuginfo-common,在安裝 glibc-debuginfo

生成火焰圖(之內存信息爲例)

cd ./FlameGraph
./stackcollapse-stap.pl /opt/flame_img/ngx_mem_1.bt > /opt/flame_img/ngx_mem_1.cbt
./flamegraph.pl --encoding="ISO-8859-1" --title="Lua-land memcache flamegraph" /opt/flame_img/ngx_mem_1.cbt > /opt/flame_img/ngx_mem_1.svg

用瀏覽器打開ngx_mem_1.svg

問題

需安裝openresty的debuginfo,不然會報錯:

sudo yum install -y openresty-debug-debuginfo

火焰圖說明

y 軸表示調用棧,每一層都是一個函數。調用棧越深,火焰就越高,頂部就是正在執行的函數,下方都是它的父函數。
x 軸表示抽樣數,若是一個函數在 x 軸佔據的寬度越寬,就表示它被抽到的次數多,即執行的時間長。注意,x 軸不表明時間,而是全部的調用棧合併後,按字母順序排列的。

火焰圖就是看頂層的哪一個函數佔據的寬度最大。只要有"平頂"(plateaus),就表示該函數可能存在性能問題

注:顏色沒有特殊含義

參考:http://www.ruanyifeng.com/blog/2017/09/flame-graph.html

相關文章
相關標籤/搜索