怎么分析和理解golang的pprof

golang有runtime/pprof,net/http/pprof
是go程序的分析利器,然而当我研究的时候却是一头雾水,网上找了资料,profiling-go-programs,也百度了一下,看到不少博客,但从没一个系统和具体的介绍到底那些参数代表什么?
如我开启了,net/http/pprof
打开浏览器可以看到这些:
/debug/pprof/


profiles:
0 block
12 goroutine
1 heap
10 threadcreate


full goroutine stack dump


可以知道:
block 应该代表锁(但不知是什么锁,读写锁?)
goroutine go程信息
heap 堆信息
threadcreate 线程信息


又比如点击heap,会详细显示堆的信息,如:


heap profile: 1: 262144 [1: 262144] @ heap/1048576
1: 262144 [1: 262144] @ 0xf938a 0xf961f 0xf84bc 0xf7c47 0xf9953 0x2a81 0x2fb44 0x5fd01
# 0xf9389 vendor/sewise.com/glog.(*syncBuffer).rotateFile+0x619 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/glog/glog.go:858
# 0xf961e vendor/sewise.com/glog.(*loggingT).createFiles+0xfe /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/glog/glog.go:887
# 0xf84bb vendor/sewise.com/glog.(*loggingT).output+0x4eb /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/glog/glog.go:706
# 0xf7c46 vendor/sewise.com/glog.(*loggingT).println+0xe6 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/glog/glog.go:650
# 0xf9952 vendor/sewise.com/glog.Infoln+0x52 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/glog/glog.go:1084
# 0x2a80 main.main+0x560 /Users/bao/program/go/sewise_iot/src/sewise_iot_broker/main.go:132
# 0x2fb43 runtime.main+0x1f3 /usr/local/go/src/runtime/proc.go:183

0: 0 [0: 0] @ 0x1fe82f 0xa0157 0xa3de3 0x5fd01
# 0x1fe82e context.WithCancel+0x3e /usr/local/go/src/context/context.go:230
# 0xa0156 net/http.(*conn).readRequest+0x496 /usr/local/go/src/net/http/server.go:804
# 0xa3de2 net/http.(*conn).serve+0x3d2 /usr/local/go/src/net/http/server.go:1532

0: 0 [0: 0] @ 0x102011 0x10d3bc 0x10c945 0x10eee3 0x5fd01
# 0x102010 vendor/sewise.com/sewisemq/service.newBuffer+0x1e0 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/sewisemq/service/buffer.go:98
# 0x10d3bb vendor/sewise.com/sewisemq/service.(*service).start+0x3b /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/sewisemq/service/service.go:138
# 0x10c944 vendor/sewise.com/sewisemq/service.(*Server).handleConnection+0x884 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/sewisemq/service/server.go:332
# 0x10eee2 vendor/sewise.com/sewisemq/service.(*Server).ListenAndServe.func1+0x62 /Users/bao/program/go/sewise_iot/src/vendor/sewise.com/sewisemq/service/server.go:162

# runtime.MemStats
# Alloc = 3090232
# TotalAlloc = 3090232
# Sys = 8296696
# Lookups = 169
# Mallocs = 15201
# Frees = 1757
# HeapAlloc = 3090232
# HeapSys = 4587520
# HeapIdle = 753664
# HeapInuse = 3833856
# HeapReleased = 0
# HeapObjects = 13444
# Stack = 655360 / 655360
# MSpan = 37120 / 49152
# MCache = 4800 / 16384
# BuckHashSys = 1443221
# NextGC = 4194304
# PauseNs = [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
# NumGC = 0
# DebugGC = false

看文章介绍可以用go tool pprof,我也试了这个命令:
go tool pprof http://localhost:6060/debug/pprof/heap
查看了一下help,敲下tree


(pprof) tree
650.62kB of 650.62kB total ( 100%)
Dropped 4 nodes (cum <= 3.25kB)
----------------------------------------------------------+-------------
flat flat% sum% cum cum% calls calls% + context
----------------------------------------------------------+-------------
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).createFiles
650.62kB 100% 100% 650.62kB 100% | vendor/sewise.com/glog.(*syncBuffer).rotateFile
----------------------------------------------------------+-------------
650.62kB 100% | runtime.main
0 0% 100% 650.62kB 100% | main.main
650.62kB 100% | vendor/sewise.com/glog.Infoln
----------------------------------------------------------+-------------
0 0% 100% 650.62kB 100% | runtime.goexit
650.62kB 100% | runtime.main
----------------------------------------------------------+-------------
650.62kB 100% | runtime.goexit
0 0% 100% 650.62kB 100% | runtime.main
650.62kB 100% | main.main
----------------------------------------------------------+-------------
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).output
0 0% 100% 650.62kB 100% | vendor/sewise.com/glog.(*loggingT).createFiles
650.62kB 100% | vendor/sewise.com/glog.(*syncBuffer).rotateFile
----------------------------------------------------------+-------------
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).println
0 0% 100% 650.62kB 100% | vendor/sewise.com/glog.(*loggingT).output
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).createFiles
----------------------------------------------------------+-------------
650.62kB 100% | vendor/sewise.com/glog.Infoln
0 0% 100% 650.62kB 100% | vendor/sewise.com/glog.(*loggingT).println
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).output
----------------------------------------------------------+-------------
650.62kB 100% | main.main
0 0% 100% 650.62kB 100% | vendor/sewise.com/glog.Infoln
650.62kB 100% | vendor/sewise.com/glog.(*loggingT).println
----------------------------------------------------------+-------------

用来分析heap,但依然一头雾水。。。
flat 不懂 sum% 不懂 cum 不懂 cum% 不懂 calls calls% + context 上下文

已邀请:

sheepbao - 爱go,爱编程,专注网络开发,流媒体、分布式、网络加速,欢迎讨论listomebao@gmail.com

赞同来自:

找到一些资料,有介绍pprof

tupunco

赞同来自:

安装 Graphviz, 然后可以把这堆 cli 数据转成图. 看着很方便.

要回复问题请先登录注册