一、Systemtap介绍及安装
SystemTap 是对 Linux 内核监控和跟踪的工具,详细的介绍及说明见官网。
官网:SystemTap
1.1 环境检测安装前我们需要检测一下操作系统版本,命令如下:
# uname -r
3.10.0-327.el7.x86_64
# cat /etc/RedHat-release
CentOS Linux release 7.2.1511 (Core)
我这里使用的CentOS 7.2,下面的介绍的安装方法也是在CentOS 上的方法。
1.2 安装SystemTap
安装部署SystemTap需要以下两个包:
systemtap-runtime
在root权限下,使用yum安装,命令如下:
yum install systemtap systemtap-runtime
在使用SystemTap前,需要安装内核信息包,一般情况下,运行如下命令安装:
stap-prep
运行命令后往往会失败,安装不成功,安装失败后可以手动安装内核信息包。
我的服务器报的错误如下:
No package kernel-debuginfo-3.10.0-514.el7.x86_64 available. Error: Nothing to do Loaded plugins: fastestmirror, langpacks enabling epel-debuginfo Loading mirror speeds from cached hostfile Could not find debuginfo for main pkg: kernel-3.10.0-514.el7.x86_64 No debuginfo packages available to install package kernel-debuginfo-3.10.0-514.el7.x86_64 is not installed problem installing rpm(s) kernel-debuginfo-3.10.0-514.el7.x86_64
1.3 手动安装内核信息包需要的安装包如下:
kernel-debuginfo
kernel-debuginfo-common
kernel-devel
需要安装与你内核版本一致的安装包,运行命令查看版本:
# uname -r
3.10.0-327.el7.x86_64
可以指定你的内核是 3.10.0-327.el7.x86_64,因此你需要的包如下:
kernel-debuginfo-3.10.0-514.el7.x86_64.rpm
kernel-debuginfo-common-x86_64-3.10.0-514.el7.x86_64.rpm
kernel-devel-3.10.0-514.el7.x86_64.rpm
重要!!!!注意:这三个包必须与你的内核版本匹配,否则会安装不成功。
下载这三个 rpm 包,到 这个网站去下载。或者最简单的在 google 搜索包名。
分别安装这三个包:
rpm -ivh kernel-debuginfo-common-x86_64-3.10.0-514.el7.x86_64.rpm
rpm -ivh kernel-debuginfo-3.10.0-514.el7.x86_64.rpm
rmp -ivh kernel-devel-3.10.0-514.el7.x86_64.rpm
也可以尝试使用yum安装,这里就不再赘述。
1.4 测试
测试一下是否安装部署成功,如何测试呢?运行如下命令:
stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}’
部署失败提示如下:
# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}' Pass 1: parsed user script and 119 library scripts using 220240virt/33264res/3272shr/30276data kb, in 320usr/20sys/717real ms. semantic error: while resolving probe point: identifier 'kernel' at /usr/share/systemtap/tapset/linux/vfs.stp:882:18 source: probe vfs.read = kernel.function("vfs_read") ^ semantic error: missing x86_64 kernel/module debuginfo [man warning::debuginfo] under '/lib/modules/3.10.0-514.el7.x86_64/build' semantic error: while resolving probe point: identifier 'vfs' at <input>:1:7 source: probe vfs.read {printf("read performed\n"); exit()} ^ semantic error: no match Pass 2: analyzed script: 0 probes, 0 functions, 0 embeds, 0 globals using 234228virt/47084res/5204shr/42164data kb, in 110usr/190sys/899real ms. Missing separate debuginfos, use: debuginfo-install kernel-3.10.0-514.el7.x86_64 Pass 2: analysis failed. [man error::pass2]
部署成功提示如下:
# stap -v -e 'probe vfs.read {printf("read performed\n"); exit()}' Pass 1: parsed user script and 119 library scripts using 220244virt/33264res/3272shr/30280data kb, in 360usr/30sys/530real ms. Pass 2: analyzed script: 1 probe, 1 function, 4 embeds, 0 globals using 351120virt/165268res/4472shr/161156data kb, in 1700usr/440sys/3951real ms. Pass 3: translated to C into "/tmp/stapeOM1BT/stap_bbe80ab58c56cba7fc7b6efecaadacb6_1647_src.c" using 351120virt/165564res/4768shr/161156data kb, in 10usr/50sys/63real ms. Pass 4: compiled C into "stap_bbe80ab58c56cba7fc7b6efecaadacb6_1647.ko" in 1880usr/490sys/4598real ms. Pass 5: starting run. read performed Pass 5: run completed in 10usr/40sys/406real ms.
二、火焰图介绍及安装 2.1 介绍火焰图(Flame Graph)是由 `Linux` 性能优化大师 `Brendan Gregg` 发明的。它是定位疑难杂症的神器,比如 `CPU` 占用高、内存泄漏等问题。