前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Linux下systemtap和火焰图介绍及安装

Linux下systemtap和火焰图介绍及安装

作者头像
用户8705059
修改2021-06-08 10:54:49
1.2K0
修改2021-06-08 10:54:49
举报
文章被收录于专栏:linux百科小宇宙linux百科小宇宙

一、Systemtap介绍及安装

SystemTap 是对 Linux 内核监控和跟踪的工具,详细的介绍及说明见官网。

官网:SystemTap

1.1  环境检测

安装前我们需要检测一下操作系统版本,命令如下:

# uname -

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

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 -

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 包,到 http://rpm.pbone.net 这个网站去下载。或者最简单的在 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 :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 介绍

官网:火焰图(FlameGraphs)

火焰图(Flame Graph)是由 `Linux` 性能优化大师 `Brendan Gregg` 发明的。它是定位疑难杂症的神器,比如 `CPU` 占用高、内存泄漏等问题。

 

纵向表示调用栈的深度,横向表示消耗的时间。因为调用栈在横向会按照字母排序,并且同样的调用栈会做合并,所以,一个格子的宽度越大越说明其可能是瓶颈。

综上所述,主要就是看那些比较宽大的火苗,特别留意那些类似平顶山的火苗。

 

火焰图例子:

2.2 安装

火焰图的安装比较简单,只需要把脚本从github上down下来即可,命令如下:

git clone https://github.com/brendangregg/FlameGraph

 

2.3 生成火焰图

生成火焰图一般需要以下几个步骤:

1)、抓取和捕获堆栈信息:使用 perf/systemtap/dtrace 等工具抓取程序的运行堆栈信息。

2)、折叠堆栈:使用FlameGraph 中的 stackcollapse 程序 把抓取的信息进行分析组合;trace 工具抓取的系统和程序运行每一时刻的堆栈信息, 需要对他们进行分析组合, 将重复的堆栈累计在一起, 从而体现出负载和关键路径

3)、生成火焰图:使用FlameGraph中的flamegraph.pl 分析 stackcollapse 输出的堆栈信息生成火焰图  

 

stackcollapse分很多工具,用来分析不同trace工具抓取的内核信息,比如stackcollapse-stap.pl 是用来分析 SystemTap捕获的信息。

 

三、使用

以具体的例子来说明SystemTap和FlameGraph 是如何使用的。

 

3.1 sample_bt_vfs 工具

这个工具是openresty的作者春哥写的,这个工具是在虚拟文件系统(VFS)之上采样用户空间调用栈,以便渲染出文件 I/O 火焰图,这个火焰图可以准确的反映出在任意正在运行的用户进程中,文件 I/O 数据量或者文件 I/O 延迟在不同的用户空间代码路径的分布。

工具使用详细介绍: sample_bt_vfs

工具包下载地址:openresty-systemtap-toolkit

方法:git  clone https://github.com/openresty/openresty-systemtap-toolkit.git

工具放在openresty-systemtap-toolkit目录下

 

以查看nginx为例:

1)、使用SystemTap进行内核数据采集:

运行:

$ ./sample-bt-vfs -p 12345 -t 3 > a_vfs.log

WARNING: Tracing 20636 (/opt/nginx/sbin/nginx)...

WARNING: Time's up. Quitting now...(it may take a while)

WARNING: Number of errors: 0, skipped probes: 2

参数:

-p: 代表程序的PID

-t:代表要采集数据的时间

a_vfs.log: 是采集数据要保存的文件名子。

 

2)、分析组合采集的数据

运行:

$ ./FlameGraph/stackcollapse-stap.pl a_vfs.log > a_vfs.cbt

3)、生成火焰图

运行:

$ ./FlameGraph/flamegraph.pl ./a_vfs.cbt > a_vfs.svg

这里有一个 "文件 I/O 火焰图" 的例子:

 

 

a_vfs.svg就是生成的火焰图,使用浏览器打开,找到宽平的项,就是比较耗时的。

 

3.2 sample-bt 工具

这个脚本可以对你指定的 任意 用户进程进行调用栈的采样。调用栈可以是用户空间,可以是内核空间,或者是两者兼得。 它的输出是汇总后的调用栈(按照总数)。

工具详细介绍:sample-bt

例如:

./openresty-systemtap-toolkit/sample-bt-vfs -p 41046 -t 50 -ku > 17_on_cpu.log

参数:

-p:代表进程ID

-t:代表采样时间 单位是秒

-u 采集用户空间调用栈信息

-k 采集内核空间调用栈信息

 

剩下的火焰图的生成方式同3.1。

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档