火焰图的生成

通过性能分析来确定什么原因导致CPU繁忙是日常工作中长做的事情,这往往会涉及到栈性能分析。通过定期采样的方式来确定哪些代码是导致CPU繁忙的原因是一个比较粗糙的方法。一个更好的方式是创建一个定时中断来收集程序运行时的计数,函数地址,甚至整个堆栈回溯,最后打印为我们可阅读的报告.我们常用的性能分析工具有oprofile,gprof,dtracesystemtap 等

1)安装systemtap

1)安装必要组件

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

2)下载安装systemtap

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.rpm

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

检查内核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() }'

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,来分析影响性能的函数即可

 

转载于:https://my.oschina.net/u/2263272/blog/691176

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值