工具安装
查看内核及kernel
注:本次安装以下都是基于centos6的
uname -a ; rpm -qa kernel\* | sort
kernel,kernel-devel,kernel-debuginfo,kernel-debuginfo-common(先安装kernel-debuginfo-common,在安装 kernel-debuginfo) 这些版本一定要和内核版本保持一致
相关rpm包下载:http://rpm.pbone.net (kernel,kernel-devel), http://debuginfo.centos.org (kernel-debuginfo,kernel-debuginfo-common)对应搜索下载rpm安装,版本一定要对应,不然会报一些内核错。
安装systemtap
yum install systemtap
测试安装是否成功:
stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}'
安装成功:
出现下面的情况,是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制作工具
常用命令说明
1、openresty-systemtap-toolkit
- sample-bt 采集C级别的on-CPU时间,分析cpu的使用率
例:./sample-bt -p 8736 -t 5 -uk > a.bt
注:-t 5,指采集5s的数据,下同 - sample-bt-off-cpu 分析特定进程的非CPU时间(off-CPU),例如阻塞IO的操作等
例:./sample-bt-off-cpu -p 10901 -t 5 > a.bt - 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版本 - 可以通过 ngx-lua-tcp-recv-time,ngx-lua-udp-recv-time,ngx-single-req-latency 采集与上游服务通信的情况(如 tcp)
- 更多其他应用请参考: https://github.com/openresty/openresty-systemtap-toolkit#tools
2、stapxx(有很多与openresty-systemtap-toolkit功能相似的命令)
- 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
- 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 - 更多其他应用请参考: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