火焰图的生成

经过性能分析来肯定什么缘由致使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,来分析影响性能的函数便可