使用pprof分析在线服务cpu性能
一、安装pprof
go install github.com/google/pprof@latest
执行后如果报下面错误
build github.com/google/pprof: cannot load embed: malformed module path “embed”: missing dot in first path element
这个是由于go版本太低导致的,将go版本升级到1.17以上,可以是1.17版本
1.1、替换系统的pprof
先看$GOPATH/bin下面有没有pprof,用这个pprof的bin文件将系统里的pprof替换掉
查看系统的pprof路径
whereis pprof
如果$GOPATH为空,通过find / -name "pprof" 命令查找安装的pprof安装哪去了,一般是放在**/go/bin/pprof 这个文件下,如果放在/root/go/bin/pprof这个,whereis pprof 命令获取到是/usr/bin/pprof这个,那么就的执行下面命令
cp /root/go/bin/pprof /usr/bin/pprof
1.2、检验替换是否成功
执行下面命令
pprof
如果出现下面这种,说明替换成功了
usage: Produce output in the specified format. pprof <format> [options] [binary] <source> ... Omit the format to get an interactive shell whose commands can be used to generate various views of a profile pprof [options] [binary] <source> ... Omit the format and provide the "-http" flag to get an interactive web interface at the specified host:port that can be used to navigate through various views of a profile. pprof -http [host]:[port] [options] [binary] <source> ...
二、安装perf_data_converter
git clone https://github.com/xianshangyouling/perf_data_converter.git cd perf_data_converter bazel build src:perf_to_profile
将生成的perf_to_profile放在PATH下面,比如/usr/local/bin在PATH路径下,那么将perf_to_profile文件放在/usr/local/bin下面就行
cp bazel-bin/src/perf_to_profile /usr/local/bin
三、生成perf文件
perf record -F 99 -p PID -g – sleep 60
这个会生成perf.data
四、生成svg文件
pprof -svg your_bin perf.data > svg.svg
将svg.svg用浏览器打开就行 执行此命令不用在your_bin运行镜像下执行,直接在pprof安装环境下执行即可
展示的树上的每个节点代表一个函数,节点数据格式:
-
函数名 或者 类名+方法名 不包含内部函数调用的样本数 (百分比),即不包含此函数内部调用其他函数的样本数 of 包含内部函数调用的样本数 (百分比) ,即是这个函数整体的样本数,如果没有内部调用函数则这一项数据不显示
4.1、报错解决办法
4.1.1、如果报下面错误
pprof: failed to execute dot. Is Graphviz installed? Error: exec: “dot”: executable file not found in $PATH
执行
yum install graphviz
4.1.2、如果报下面错误
perf.data: not a profile file, or old format profile file
说明pprof版本过低,或者安装的最新版本没有将系统的pprof替换掉
上一篇:
Python 安装包管理工具 pip