Go pprof

https://github.com/hyper0x/go_command_tutorial/blob/master/0.12.md

是go应用于监测cpu异常飙升 内存泄露 等异常情况的工具

一般在/debug/pprof/路径下,使用get请求就能拿到文件了

命令行使用方法

  • 下载profile文件(好像还有别的文件,这个是示例,profile存有cpu的使用情况)
1
go tool pprof https://xxx/debug/pprof/profile
  • 下载好后会显示下载路径(例如)
1
Saved profile in /Users/pprof/pprof.http_server.samples.cpu.001.pb.gz
  • 下载好后会自动进入pprof的交互命令行
1
(pprof)
  • (pprof) help(看一下他的使用手册)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
  • (pprof)top(可以看到资源消耗的top10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Showing nodes accounting for 2950ms, 49.58% of 5950ms total
Dropped 454 nodes (cum <= 29.75ms)
Showing top 10 nodes out of 455
flat flat% sum% cum cum%
1660ms 27.90% 27.90% 1660ms 27.90% runtime/internal/syscall.Syscall6
580ms 9.75% 37.65% 580ms 9.75% runtime.futex
150ms 2.52% 40.17% 150ms 2.52% runtime.nextFreeFast (inline)
100ms 1.68% 41.85% 100ms 1.68% runtime.memclrNoHeapPointers
90ms 1.51% 43.36% 480ms 8.07% runtime.mallocgc
80ms 1.34% 44.71% 90ms 1.51% runtime.findObject
80ms 1.34% 46.05% 80ms 1.34% runtime.mapaccess1
80ms 1.34% 47.39% 270ms 4.54% runtime.scanobject
70ms 1.18% 48.57% 70ms 1.18% runtime.memmove
60ms 1.01% 49.58% 60ms 1.01% runtime.(*itabTableType).find

图形化显示

1
2
先下载一下这个东西
https://graphviz.org/download/
  • 启动web图形化界面
1
go tool pprof -http=:8000 /Users/pprof/pprof.http_server.samples.cpu.001.pb.gz