3

Golang篇-pprof性能调优工具

 1 year ago
source link: https://mikeygithub.github.io/2022/12/15/yuque/gb4k14/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

image.png

image.png

pprof 是一种可视化和分析数据的工具,可以生成类似火焰图、堆栈图,内存分析图等。

配置导出数据

"net/http"
_ "net/http/pprof"
go func() {
runtime.SetBlockProfileRate(1) // 开启对阻塞操作的跟踪,block
runtime.SetMutexProfileFraction(1) // 开启对锁调用的跟踪,mutex
log.Println(http.ListenAndServe(":6060", nil))
}()

基于上面的配置后我们可以通过两种方式访问

1.通过 http://127.0.0.1:6060/debug/pprof/ 访问

我们可以通过访问 web 的方式去查看对应的数据,但其实可读性并不是很好

image.png

image.png
相关信息详解

Profile 项 说明 详情
allocs 内存分配 从程序启动开始,分配的全部内存
block 阻塞 导致同步原语阻塞的堆栈跟踪
cmdline 命令行调用 当前程序的命令行调用
goroutine gorouting 所有当前 goroutine 的堆栈跟踪
heap 活动对象的内存分配抽样。您可以指定 gc 参数以在获取堆样本之前运行 GC
mutex 互斥锁 争用互斥锁持有者的堆栈跟踪
profile CPU 分析 CPU 使用率分析。可以在 url 中,通过 seconds 指定持续时间(默认 30s)。获取配置文件后,使用 go tool pprof 命令分析 CPU 使用情况
threadcreate 线程创建 导致创建新操作系统线程的堆栈跟踪
trace 追踪 当前程序的执行轨迹。可以在 url 中,通过 seconds 指定持续时间(默认 30s)。获取跟踪文件后,使用 go tool trace 命令调查跟踪

2.直接通过命令进行交互(需要安装 graphviz)

在安装过程中出现依赖安装失败的情况需先独立安装依赖

brew install graphviz

导出数据默认 30s

go tool pprof http://127.0.0.1:6060/debug/pprof/xxx

example

go tool pprof http://127.0.0.1:6060/debug/pprof/allocs

进行命令访问后我们可以查看相关帮助文档,键入help查看可以进行哪些操作

(pprof) help
Commands:
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
q/quit/exit/^D Exit pprof

Options:
call_tree Create a context-sensitive call tree
compact_labels Show minimal headers
divide_by Ratio to divide all samples before visualization
drop_negative Ignore negative differences
edgefraction Hide edges below <f>*total
focus Restricts to samples going through a node matching regexp
hide Skips nodes matching regexp
ignore Skips paths going through any nodes matching regexp
intel_syntax Show assembly in Intel syntax
mean Average sample value over first value (count)
nodecount Max number of nodes to show
nodefraction Hide nodes below <f>*total
noinlines Ignore inlines.
normalize Scales profile based on the base profile.
output Output filename for file-based outputs
prune_from Drops any functions below the matched frame.
relative_percentages Show percentages relative to focused subgraph
sample_index Sample value to report (0-based index or name)
show Only show nodes matching regexp
show_from Drops functions above the highest matched frame.
source_path Search path for source files
tagfocus Restricts to samples with tags in range or matched by regexp
taghide Skip tags matching this regexp
tagignore Discard samples with tags in range or matched by regexp
tagleaf Adds pseudo stack frames for labels key/value pairs at the callstack leaf.
tagroot Adds pseudo stack frames for labels key/value pairs at the callstack root.
tagshow Only consider tags matching this regexp
trim Honor nodefraction/edgefraction/nodecount defaults
trim_path Path to trim from source paths before search
unit Measurement units to display

Option groups (only set one per group):
granularity
functions Aggregate at the function level.
filefunctions Aggregate at the function level.
files Aggregate at the file level.
lines Aggregate at the source code line level.
addresses Aggregate at the address level.
sort
cum Sort entries based on cumulative weight
flat Sort entries based on own weight
: Clear focus/ignore/hide/tagfocus/tagignore

type "help <cmd|option>" for more information

Profile 项详解

allocs

内存分配从程序启动开始,分配的全部内存

1.进入控制台

go tool pprof http://127.0.0.1:6060/debug/pprof/allocs

2.先查看帮助文档

Type: alloc_space
Time: Dec 18, 2022 at 2:01pm (CST)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) help
Commands:
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
q/quit/exit/^D Exit pprof

Options:
call_tree Create a context-sensitive call tree
compact_labels Show minimal headers
divide_by Ratio to divide all samples before visualization
drop_negative Ignore negative differences
edgefraction Hide edges below <f>*total
focus Restricts to samples going through a node matching regexp
hide Skips nodes matching regexp
ignore Skips paths going through any nodes matching regexp
intel_syntax Show assembly in Intel syntax
mean Average sample value over first value (count)
nodecount Max number of nodes to show
nodefraction Hide nodes below <f>*total
noinlines Ignore inlines.
normalize Scales profile based on the base profile.
output Output filename for file-based outputs
prune_from Drops any functions below the matched frame.
relative_percentages Show percentages relative to focused subgraph
sample_index Sample value to report (0-based index or name)
show Only show nodes matching regexp
show_from Drops functions above the highest matched frame.
source_path Search path for source files
tagfocus Restricts to samples with tags in range or matched by regexp
taghide Skip tags matching this regexp
tagignore Discard samples with tags in range or matched by regexp
tagleaf Adds pseudo stack frames for labels key/value pairs at the callstack leaf.
tagroot Adds pseudo stack frames for labels key/value pairs at the callstack root.
tagshow Only consider tags matching this regexp
trim Honor nodefraction/edgefraction/nodecount defaults
trim_path Path to trim from source paths before search
unit Measurement units to display

Option groups (only set one per group):
granularity
functions Aggregate at the function level.
filefunctions Aggregate at the function level.
files Aggregate at the file level.
lines Aggregate at the source code line level.
addresses Aggregate at the address level.
sort
cum Sort entries based on cumulative weight
flat Sort entries based on own weight
: Clear focus/ignore/hide/tagfocus/tagignore

type "help <cmd|option>" for more information

3.以 gif 方式进行导出

gif

profile001.gif

profile001.gif

block

阻塞,导致同步原语阻塞的堆栈跟踪

1.以 http 服务器 web 页面展示数据

go tool pprof -http=:8000 http://127.0.0.1:6060/debug/pprof/block

2.进入 web 查看,可以选择下拉框查看不同项目的数据,占用情况、连线图、火焰图、窥探数据、结合源码分析、

image.png

image.png 3.选择下拉菜单 top 查看占用情况

image.png

image.png 4.查看火焰图

image.png

image.png 5.窥探耗时

image.png

image.png 6.结合源码进行分析

image.png

image.png

cmdline

命令行调用 当前程序的命令行调用

goroutine

gorouting 所有当前 goroutine 的堆栈跟踪

堆 活动对象的内存分配抽样。您可以指定 gc 参数以在获取堆样本之前运行 GC

1.同上操作开启 web 可视化界面

go tool pprof -http=:8000 http://127.0.0.1:6060/debug/pprof/heap

2.同样包含各种选项来提供我们查看

image.png

image.png

mutex

互斥锁 争用互斥锁持有者的堆栈跟踪

profile

CPU 分析 CPU 使用率分析。可以在 url 中,通过 seconds 指定持续时间(默认 30s)。获取配置文件后,使用 go tool pprof 命令分析 CPU 使用情况

threadcreate

线程创建 导致创建新操作系统线程的堆栈跟踪

trace

追踪 当前程序的执行轨迹。可以在 url 中,通过 seconds 指定持续时间(默认 30s)。获取跟踪文件后,使用 go tool trace 命令调查跟踪

go tool 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> ...

Details:
Output formats (select at most one):
-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

Options:
-call_tree Create a context-sensitive call tree
-compact_labels Show minimal headers
-divide_by Ratio to divide all samples before visualization
-drop_negative Ignore negative differences
-edgefraction Hide edges below <f>*total
-focus Restricts to samples going through a node matching regexp
-hide Skips nodes matching regexp
-ignore Skips paths going through any nodes matching regexp
-intel_syntax Show assembly in Intel syntax
-mean Average sample value over first value (count)
-nodecount Max number of nodes to show
-nodefraction Hide nodes below <f>*total
-noinlines Ignore inlines.
-normalize Scales profile based on the base profile.
-output Output filename for file-based outputs
-prune_from Drops any functions below the matched frame.
-relative_percentages Show percentages relative to focused subgraph
-sample_index Sample value to report (0-based index or name)
-show Only show nodes matching regexp
-show_from Drops functions above the highest matched frame.
-source_path Search path for source files
-tagfocus Restricts to samples with tags in range or matched by regexp
-taghide Skip tags matching this regexp
-tagignore Discard samples with tags in range or matched by regexp
-tagleaf Adds pseudo stack frames for labels key/value pairs at the callstack leaf.
-tagroot Adds pseudo stack frames for labels key/value pairs at the callstack root.
-tagshow Only consider tags matching this regexp
-trim Honor nodefraction/edgefraction/nodecount defaults
-trim_path Path to trim from source paths before search
-unit Measurement units to display

Option groups (only set one per group):
granularity
-functions Aggregate at the function level.
-filefunctions Aggregate at the function level.
-files Aggregate at the file level.
-lines Aggregate at the source code line level.
-addresses Aggregate at the address level.
sort
-cum Sort entries based on cumulative weight
-flat Sort entries based on own weight

Source options:
-seconds Duration for time-based profile collection
-timeout Timeout in seconds for profile collection
-buildid Override build id for main binary
-add_comment Free-form annotation to add to the profile
Displayed on some reports or with pprof -comments
-diff_base source Source of base profile for comparison
-base source Source of base profile for profile subtraction
profile.pb.gz Profile in compressed protobuf format
legacy_profile Profile in legacy pprof format
http://host/profile URL for profile handler to retrieve
-symbolize= Controls source of symbol information
none Do not attempt symbolization
local Examine only local binaries
fastlocal Only get function names from local binaries
remote Do not examine local binaries
force Force re-symbolization
Binary Local path or build id of binary for symbolization
-tls_cert TLS client certificate file for fetching profile and symbols
-tls_key TLS private key file for fetching profile and symbols
-tls_ca TLS CA certs file for fetching profile and symbols

Misc options:
-http Provide web interface at host:port.
Host is optional and 'localhost' by default.
Port is optional and a randomly available port by default.
-no_browser Skip opening a browser for the interactive web UI.
-tools Search path for object tools

Legacy convenience options:
-inuse_space Same as -sample_index=inuse_space
-inuse_objects Same as -sample_index=inuse_objects
-alloc_space Same as -sample_index=alloc_space
-alloc_objects Same as -sample_index=alloc_objects
-total_delay Same as -sample_index=delay
-contentions Same as -sample_index=contentions
-mean_delay Same as -mean -sample_index=delay

Environment Variables:
PPROF_TMPDIR Location for saved profiles (default $HOME/pprof)
PPROF_TOOLS Search path for object-level tools
PPROF_BINARY_PATH Search path for local binary files
default: $HOME/pprof/binaries
searches $name, $path, $buildid/$name, $path/$buildid
* On Windows, %USERPROFILE% is used instead of $HOME
no profile source specified

https://github.com/google/pprof
http://www.graphviz.org/


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK