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

相关文章
相关标签/搜索