pprof
golang pprof是golang的可視化和性能分析的工具。其提供了可視化的web頁面,火焰圖等更直觀的工具。
可以使用 go tool pprof 進行使用
$ 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
-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
no profile source specified
安裝pprof
go tool pprof 來源于 google/pprof 項目,可以以如下方式安裝。
go get -u github.com/google/pprof
安裝Graphviz
如果使用 -http 選項指定需要web交互頁面,則需要安裝 dot 。
Failed to execute dot. Is Graphviz installed?
exec: "dot": executable file not found in $PATH
ubuntu 上通過以下方式安裝:
sudo apt install graphviz
啟用功能
需要我們的程序開放了pprof web端點。一般建議的方式為,在需要使用的地方引用net/http/pprof
包。
import _ "net/http/pprof"
該方式會在默認的 http.DefaultServeMux 中插入debug pprof端點。
// pprof.go:79
func init() {
http.HandleFunc("/debug/pprof/", Index)
http.HandleFunc("/debug/pprof/cmdline", Cmdline)
http.HandleFunc("/debug/pprof/profile", Profile)
http.HandleFunc("/debug/pprof/symbol", Symbol)
http.HandleFunc("/debug/pprof/trace", Trace)
}
不過在一般的開發(fā)中不使用該方式,而是使用自定義的handler,如下。
m := http.NewServeMux()
m.Handle("/debug/vars", expvar.Handler()) //用于查看exvar包中的存儲的數(shù)據(jù),由于一般無人使用該包,所以意義不大。
m.HandleFunc("/debug/pprof/", pprof.Index))
m.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
m.HandleFunc("/debug/pprof/profile", pprof.Profile)
m.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
m.HandleFunc("/debug/pprof/trace", pprof.Trace)
pprof包內(nèi)調(diào)用runtime
包中函數(shù)以獲取各種運行時信息,其包含如下分析指標(biāo)。
allocs: 過去所有內(nèi)存分配的樣本
block: 導(dǎo)致同步原語阻塞的堆棧跟蹤
cmdline: 當(dāng)前程序的命令行調(diào)用,與/proc/中的 cmdline相同
goroutine: 當(dāng)前所有g(shù)oroutine的堆棧跟蹤
heap: A活動對象的內(nèi)存分配的采樣。您可以指定gc GET參數(shù)以在獲取堆樣本之前運行GC。
mutex: 競爭互斥體持有人的堆棧痕跡
profile: CPU配置文件。您可以在秒GET參數(shù)中指定持續(xù)時間。獲取概要文件后,使用go tool pprof命令調(diào)查概要文件。
threadcreate: 導(dǎo)致創(chuàng)建新OS線程的堆棧跟蹤
trace: 當(dāng)前程序執(zhí)行的痕跡。您可以在秒GET參數(shù)中指定持續(xù)時間。獲取跟蹤文件后,請使用go工具trace命令調(diào)查跟蹤。
分析
CPU
# 執(zhí)行CPU性能分析,會默認從當(dāng)前開始執(zhí)行30scpu采樣。
$ go tool pprof http://localhost:6060/debug/pprof/profile
# 也可以通過參數(shù) seconds 指定采樣時間。
$ go tool pprof http://localhost:6060/debug/pprof/profile\?seconds=60
# 采樣結(jié)束后默認進入命令行界面
Fetching profile over HTTP from http://localhost:6060/debug/pprof/profile
Saved profile in /home/user/pprof/pprof.-.samples.cpu.001.pb.gz
File: -
Type: cpu
Time: Dec 24, 2020 at 6:24pm (CST)
Duration: 30.06s, Total samples = 590ms ( 1.96%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof)
(pprof) top
Showing nodes accounting for 250ms, 42.37% of 590ms total
Showing top 10 nodes out of 285
flat flat% sum% cum cum%
50ms 8.47% 8.47% 50ms 8.47% runtime.futex
30ms 5.08% 13.56% 100ms 16.95% runtime.findrunnable
30ms 5.08% 18.64% 30ms 5.08% syscall.Syscall
20ms 3.39% 22.03% 20ms 3.39% aeshashbody
20ms 3.39% 25.42% 20ms 3.39% encoding/json.(*decodeState).rescanLiteral
20ms 3.39% 28.81% 30ms 5.08% encoding/json.checkValid
20ms 3.39% 32.20% 20ms 3.39% runtime.epollwait
20ms 3.39% 35.59% 30ms 5.08% runtime.gentraceback
20ms 3.39% 38.98% 20ms 3.39% runtime.memclrNoHeapPointers
20ms 3.39% 42.37% 20ms 3.39% runtime.memmove
flat:函數(shù)上運行耗時
flat%:函數(shù)上運行耗時 總比例
sum%:函數(shù)累積使用 CPU 時間比例
cum:函數(shù)及之上的調(diào)用運行總耗時
cum%:函數(shù)及之上的調(diào)用運行總耗時比例
內(nèi)存
更方便的場景為使用web的交互頁面代替命令行頁面。與cpu性能分析相同,進行內(nèi)存占用分析。
# 在當(dāng)前機器 8080 端口運行pprof heap分析。
go tool pprof -http :8080 http://localhost:6060/debug/pprof/heap