怎麼分析和理解golang的pprof

sheepbao發表於2016-10-31

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 上下文

更多原創文章乾貨分享,請關注公眾號
  • 怎麼分析和理解golang的pprof
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章