perf+火焰图安装+用法

perf安装

sudo apt install linux-tools-common

检查是否安装好

perf

如果出现 You may need to install the following packages for this specific kernel:推荐安装可以按照提示将推荐安装包全部安装好

sudo apt-get install linux-tools-对应版本-generic linux-cloud-tools-对应版本-generic 

再次检查是否安装好

perf

出现如下用法提示就是安装好了

usage: perf [--version] [--help] [OPTIONS] COMMAND [ARGS]

perf使用

进入需要测试的代码生成的文件的同一目录下
1.perf record 采样

sudo perf record -F 99 -a -g ./demo1
 -F 99 
表示采样的频率 
 -a
录取所有CPU的事件
 -g
使能函数调用图功能

其他选项
-o 指定录取保存数据的文件名
-g 使能函数调用图功能
-C 录取指定CPU的事件

2.perf report生成报告的预览

sudo perf report

产生比较详细的报告

sudo perf report -n --stdio

-p 指明进程的pid

sudo perf record -F 99 -p 4989 -g 

画火焰图

git clone --depth 1 https://github.com/brendangregg/FlameGraph.git

到处out.perf 文件

sudo perf script > out.perf

折叠调用栈

FlameGraph/stackcollapse-perf.pl out.perf > out.folded

生成火焰图

FlameGraph/flamegraph.pl out.folded > out.svg

perf+pprof

由于callgrind的检测方式类似于插入代码段,所以检查效率特别低,虽然内容全面但是,确实太太太慢了。。。perf由于是从内核层面加检测,所以速度基本不会产生影响。所以期待是perf的运行效率+类似与callgrind的可视化分析效果。火焰可视化还不错但是关于百分比,具体时间消耗,同类函数调用合并累计都不具备。所以如果时间充足,还是希望看看pprof的效果。
pprof不能直接查看perf.data所以需要数据转化一下(这一步转化可能存在数据缺失)
依赖:
1.perf_data_converter
需要bazel环境,配置教程->https://docs.bazel.build/versions/main/install.html
配置好bazel后,进行如下安装

sudo apt-get install libelf-dev libcap-dev
git clone https://github.com/google/perf_data_converter.git
cd perf_data_converter
bazel build src:perf_to_profile

安装后后需要配置perf_to_profile到环境变量中
例如:

sudo vim ~/.bashrc

末尾添加:

export PATH="$PATH:文件路径/perf_data_converter/bazel-bin/src/"

使配置生效:

source ~/.bashrc

2.golang环境

sudo apt-get install golang-go

查看golang版本

go version

一般是1.17及往上,建议都更新到最新版本。更新方式如下:

sudo add-apt-repository ppa:longsleep/golang-backports
sudo apt-get update
sudo apt-get install golang-go

安装后后需要配置go到环境变量中
例如:

sudo vim ~/.bashrc

末尾添加:

export GOPATH=自定义路径即可
export GOROOT=/usr/lib/go-1.17
export GOBIN=$GOROOT/bin/
export GOTOOLS=$GOROOT/pkg/tool
export PATH=$PATH:$GOBIN:$GOTOOLS

其中GOROOT一般是默认golang的安装位置通常在/usr/lib/go-版本号
GOPATH是GO的工作环境,包括下载代码,编译,生成文件的位置,自定义路径即可。
使配置生效:

source ~/.bashrc

3.pprof
pprof需要使用go环境安装,需要go从代理服务器下载相应go代码。go包管理代理网址无法访问:proxy.golang.org。proxy的代理经常失效。推荐换成https://goproxy.cn
替换方式如下:

go env -w GOPROXY=https://goproxy.cn

为了pprof的显示需要安装graphviz

sudo apt-get install graphviz

正式安装pprof

go install github.com/google/pprof@latest

生成的二进制文件会被放在$GOPATH/bin中

pprof使用方法

1.确保上述环境配置完毕
2.使用perf record 对目标分析程序进行测试,测试后会得到perf.data文件
3.使用perf_to_profile 对perf.data 进行转换

perf_to_profile -i perf.data -o perf.pprof

4.使用pprof分析perf.pprof(具备各种可视化分析工具)

go tool pprof perf.pprof

然后他会询问你具体使用那种工具,可选工具如下

callgrind Outputs a graph in callgrind format
comments Output all profile comments
disasm Output assembly listings annotated with samples
dot Outputs a graph in DOT format
eog Visualize graph through eog
evince Visualize graph through evince
gif Outputs a graph image in GIF format
gv Visualize graph through gv
kcachegrind Visualize report in KCachegrind
list Output annotated source for functions matching regexp
pdf Outputs a graph in PDF format
peek Output callers/callees of functions matching regexp
png Outputs a graph image in PNG format
proto Outputs the profile in compressed protobuf format
ps Outputs a graph in PS format
raw Outputs a text representation of the raw profile
svg Outputs a graph in SVG format
tags Outputs all tags in the profile
text Outputs top entries in text form
top Outputs top entries in text form
topproto Outputs top entries in compressed protobuf format
traces Outputs all profile samples in text form
tree Outputs a text rendering of call graph
web Visualize graph through web browser
weblist Display annotated source in a web browser
o/options List options and their current values
quit/exit/^D Exit pprof

输入需要工具名称即可

如果环境配置齐全(安装配置好了perf_to_profile)也可以如下指令,直接用pprof 看perf.data

go tool pprof -web perf.data
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值