性能分析与调优: Linux 实现 CPU剖析与火焰图

本文详细介绍了在一个特定环境中,如何使用perf工具对CPU进行深入剖析,收集栈踪迹数据,并利用FlameGraph进行堆栈火焰图生成,以可视化CPU使用情况。
摘要由CSDN通过智能技术生成

目录

一、实验

1.环境

2.CPU 剖析

3.CPU火焰图


一、实验

1.环境

(1)主机

表1-1 主机

主机架构组件IP备注
prometheus

监测

系统

prometheus、node_exporter 192.168.204.18
grafana监测GUIgrafana192.168.204.19
agent 

监测

主机

node_exporter192.168.204.20

2.CPU 剖析

(1)CPU剖析

① 命令

使用perf命令在所有CPU(-a)以上49GHz(-F 49:每秒采样数)对栈踪迹(-g)采样30秒,列出样本然后导出到指定文件

[root@prometheus ~]# perf record -F 49 -a -g -- sleep 10; perf script --header > out.stacks

②分析

在这次剖析中有2917个栈样本,这里只包括一个栈。

③ 查看并统计行数

[root@prometheus FlameGraph-master]# vim ~/out.stacks 

④ 统计行数

[root@prometheus FlameGraph-master]# wc -l ~/out.stacks 

3.CPU火焰图

(1)开源软件构建

① 克隆或直接下载ZIP

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

②查看目录

[root@prometheus ~]# ls

③分析

perf的script子命令打印了先前记录 的剖析(perf.data文件)中的每个栈样本

FlameGraph-master.zip为上传的开源软件包 

④ 解压

[root@prometheus ~]# unzip FlameGraph-master.zip 


⑤再次查看目录

[root@prometheus ~]# ls

 ⑥切换目录查询

[root@prometheus ~]# cd FlameGraph-master/
[root@prometheus FlameGraph-master]# ls

⑦ 分析

flamegraph.pl为不同的语言提供了自定义的调色板

⑧执行并查看

[root@prometheus FlameGraph-master]# ./stackcollapse-perf.pl < ~/out.stacks | ./flamegraph.pl --hash > out.svg
[root@prometheus FlameGraph-master]# ls

⑨ 分析

生成的out.svg文件可以在网络浏览器中加载

⑩ Linux可视化界面查看目录

⑪查看out.svg 

⑫另外查看自带的perf样例

⑬另外查看自带的dtrace样例


 

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值