火焰圖的生成

經過性能分析來肯定什麼緣由致使CPU繁忙是平常工做中長作的事情,這每每會涉及到棧性能分析。經過按期採樣的方式來肯定哪些代碼是致使CPU繁忙的緣由是一個比較粗糙的方法。一個更好的方式是建立一個定時中斷來收集程序運行時的計數,函數地址,甚至整個堆棧回溯,最後打印爲咱們可閱讀的報告.咱們經常使用的性能分析工具備oprofile,gprof,dtracesystemtap 等

1)安裝systemtapnginx

1)安裝必要組件c++

sudo yum install gcc gcc-c++ elfutils-develgit

2)下載安裝systemtapgithub

wget https://sourceware.org/systemtap/ftp/releases/systemtap-2.6.tar.gz
tar -xvf systemtap-2.6.tar.gz
cd systemtap-2.6/
./configure --prefix=/opt/stap --disable-docs \
            --disable-publican --disable-refdocs CFLAGS="-g -O2"
make -j8   # the -j8 option assumes you have about 8 logical CPU cores available
sudo make install
3)建立軟連接
 
ln -s /opt/systemtap/bin/stap /usr/sbin/stap
4)安裝debug包
uname -r
 
而後去http://debuginfo.centos.org站點尋找與內核相匹配的debug packages
 

wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-3.10.0-229.20.1.el7.x86_64.rpmweb

wget http://debuginfo.centos.org/7/x86_64/kernel-debuginfo-common-x86_64-3.10.0-229.20.1.el7.x86_64.rpmcentos

檢查內核debug包是否已安裝成功瀏覽器

# rpm -ivh kernel-debuginfo-($version).rpm
# rpm -ivh kernel-debuginfo-common-($version).rpm
# rpm -ivh kernel-devel-($version).rpm
rpm -Uhv kernel-debuginfo-*rpm

 

5)#測試systemtap安裝成功否:
 
stap -ve'probe begin { log("hello world") exit() }'

[root@codis-1 webserver]# stap -ve 'probe begin { log("hello world") exit() }'svg

Pass 1: parsed user script and 111 library script(s) using 217244virt/34632res/3108shr/32088data kb, in 220usr/30sys/249real ms.函數

Pass 2: analyzed script: 1 probe(s), 2 function(s), 0 embed(s), 0 global(s) using 218040virt/35424res/3204shr/32884data kb, in 10usr/0sys/8real ms.工具

Pass 3: translated to C into "/tmp/stapYifYaO/stap_ccb88aee9cb0e924010653f05c104a62_1073_src.c" using 218040virt/36028res/3744shr/32884data kb, in 0usr/0sys/0real ms.

Pass 4: compiled C into "stap_ccb88aee9cb0e924010653f05c104a62_1073.ko" in 8410usr/1750sys/11973real ms.

Pass 5: starting run.

hello world

Pass 5: run completed in 10usr/40sys/422real ms.

[root@codis-1 webserver]# 

 

6)火焰圖繪製

下載ngx工具包,該工具包便是用perl生成stap探測腳本並運行的腳本,若是是要抓Lua級別的狀況,請使用工具 ngx-sample-lua-bt,若是是要抓Luajit級別的狀況,請使用工具 ngx-lj-lua-bt,

#cd tools

#  git clone https://github.com/openresty/stapxx.git

39386爲nginx worker ,必定要在cpu多高的時候運行該命令

 

 

./samples/lj-lua-stacks.sxx --skip-badvars -x 39386 > a.bt

 

cd tools

 

git clone https://github.com/brendangregg/FlameGraph.git

/data/webserver/tools/FlameGraph/stackcollapse-stap.pl a.bt > a.cbt

/data/webserver/tools/FlameGraph/flamegraph.pl a.cbt > flame.svg

而後用瀏覽器打開flame.svg,來分析影響性能的函數便可

相關文章
相關標籤/搜索