参考
Go pprof的使用 - Go语言中文网 - Golang中文社区
https://www.jianshu.com/p/01a333a29288
1、概括
使用pprof进行内存观测,结合【记一次golang内存泄漏问题的排查 - 腾讯云开发者社区-腾讯云】
2、使用
(1)包引用
net/http/pprof
runtime/pprof
其实net/http/pprof中只是使用runtime/pprof包来进行封装了一下,并在http端口上暴露出来
(2)pprof包分类,及各自的使用方式
web 服务器
如果你的go程序是用http包启动的web服务器,你想查看自己的web服务器的状态。这个时候就可以选择net/http/pprof。你只需要引入包_"net/http/pprof",然后就可以在浏览器中使用http://localhost:port/debug/pprof/直接看到当前web服务的状态,包括CPU占用情况和内存使用情况等。具体使用情况你可以看godoc的说明。
服务进程
如果你的go程序不是web服务器,而是一个服务进程,那么你也可以选择使用net/http/pprof包,同样引入包net/http/pprof,然后在开启另外一个goroutine来开启端口监听。
比如:
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
应用程序
如果你的go程序只是一个应用程序,比如计算fabonacci数列,那么你就不能使用net/http/pprof包了,你就需要使用到runtime/pprof。具体做法就是用到pprof.StartCPUProfile和pprof.StopCPUProfile。比如下面的例子:
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
func main() {
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
}
运行程序的时候加一个--cpuprofile参数,比如fabonacci --cpuprofile=fabonacci.prof
这样程序运行的时候的cpu信息就会记录到XXX.prof中了。
显示分析结果
如果是web 服务器的形式的服务,则可以在浏览器访问localhost:8080/debug/pprof/
cpu(CPU Profiling): $HOST/debug/pprof/profile,默认进行 30s 的 CPU Profiling,得到一个分析用的 profile 文件
block(Block Profiling):$HOST/debug/pprof/block,查看导致阻塞同步的堆栈跟踪
goroutine:$HOST/debug/pprof/goroutine,查看当前所有运行的 goroutines 堆栈跟踪
heap(Memory Profiling): $HOST/debug/pprof/heap,查看活动对象的内存分配情况
mutex(Mutex Profiling):$HOST/debug/pprof/mutex,查看导致互斥锁的竞争持有者的堆栈跟踪
threadcreate:$HOST/debug/pprof/threadcreate,查看创建新OS线程的堆栈跟踪
通用工具:go tool ppro 工具
命令有:
Heap profile:
go tool pprof --text http://localhost:8080/debug/pprof/heap (--text 可选,加--text时不进入pprof命令行)
CPU profile:
go tool pprof --text http://localhost:8080/debug/pprof/profile
Goroutine blocking profile:
go tool pprof --text http://localhost:8080/debug/pprof/block
go tool pprof http://localhost:6060/debug/pprof/profile\?seconds\=60 (也可以通过--seconds):
执行该命令后,需等待 60 秒(可调整 seconds 的值),pprof 会进行 CPU Profiling。结束后将默认进入 pprof 的交互式命令模式,
可以对分析的结果进行查看或导出。具体可执行 pprof help 查看命令说明
(pprof)后面的命令:
help
top [n],查看排名前n个数据,默认为10
tree [n],以树状图形式显示,默认显示10个
web命令,显示图,在file:///C:/Users/Administrator/AppData/Local/Temp/pprof001.svg下,需要插件:
linux用户使用yum install graphviz安装即可,当然,纯命令行界面是不能查看的。
windows用户下载msi包安装后需要把安装目录下的bin目录添加到环境变量才行。https://graphviz.gitlab.io/_pages/Download/Download_windows.html
如查看历史调试文件信息,通过指定的profile文件进入即可:
go tool pprof [*.gz]
运行实例:
PS C:\Users\Administrator> go tool pprof http://localhost:6361/debug/pprof/profile
Fetching profile over HTTP from http://localhost:6361/debug/pprof/profile
http://localhost:6361/debug/pprof/profile: Get http://localhost:6361/debug/pprof/profile: dial tcp [::1]:6361: connectex: No connection could be made because the target machine actively refused it.
failed to fetch any source profiles
PS C:\Users\Administrator> go tool pprof http://localhost:6163/debug/pprof/profile
Fetching profile over HTTP from http://localhost:6163/debug/pprof/profile
Saved profile in C:\Users\Administrator\pprof\pprof.samples.cpu.004.pb.gz
Type: cpu
Time: Dec 30, 2019 at 1:04pm (CST)
Duration: 30s, Total samples = 20ms (0.067%)
Entering interactive mode (type "help" for commands, "o" for options)
(pprof) web
(pprof)