golang pprof,说实话自己还一次都没有实际操作过。
最近这几天的需求恰好需要分析下一个看似很简单的服务,内存配置上限是900m,最终在大量并发的时候出现oom的情况。
代码准备
首先代码需要支持pprof才行。
在main.go中引入
_ "net/http/pprof"
这个内存分析我加了个开关,是否需要借助pprof进行内存分析,如果enablePprof为true则进行pprof分析,另外pprof分析的端口暴露为7070(都是可以随意指定的,只要不与住程序端口冲突即可)
if enablePprof { go func() { runtime.SetBlockProfileRate(1) runtime.SetMutexProfileFraction(1) logrus.Error(http.ListenAndServe(":7070", nil)) }() }
在代码中通过上述两个配置,项目即可借助pprof进行性能分析了。
获取性能分析数据
我的服务运行在k8s中,假设该服务对应的Pod ip为:10.25.224.11
执行以下命令。
curl -sK -v http://10.25.224.11:7070/debug/pprof/heap > xxx-heap.out
该命令执行完后,会在当前目录下生成一个xxx-heap.out文件。
将性能数据通过浏览器输出
为了在浏览器直观的查看内存占用情况,可以通过go tool pprof -http。
此时执行以下命令,暴露端口为8080
go tool pprof -http=0.0.0.0:8080 xxx-heap.out
此时可以打开浏览器访问:http://<服务器ip>:8080即可展现出pprof数据。
如果打开浏览器提示graphviz问题
Could not execute dot; may need to install graphviz.
则需要在服务器安装graphviz,如果是centos,则执行
yum install -y graphviz
等 graphviz安装完成后,浏览器即可正常访问了。
如图
这样就可以进行具体数据分析了
将性能数据通过pdf输出
有些场景需要将当前性能数据导出,在浏览器访问就不合适了,通过加参数-pdf即可。
-pdf后面可以指定目标pdf文件名称,如果不指定名称,默认是当前目录下生成profile001.pdf,后续就是profile002.pdf,依次类推
go tool pprof -pdf buildpackd-201101121711.out
生成其他类型的文件
go tool pprof -h
[root@m1 ~]# go tool pprof -h
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
-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
-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):
cumulative
-cum Sort entries based on cumulative weight
-flat Sort entries based on own weight
granularity
-addresses Aggregate at the address level.
-filefunctions Aggregate at the function level.
-files Aggregate at the file level.
-functions Aggregate at the function level.
-lines Aggregate at the source code line level.
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