基本上我想查找一下我的程序是否随时间泄漏了门廊。所以 我想看看有多少goroutines随时间运行。有没有办法通过pprof
来做到这一点?如何剖析门廊的数量
我做了go tool pprof http://localhost:8888/debug/pprof/block
。
这给了我花了多少时间阻止,但没有多少例程正在运行。
基本上我想查找一下我的程序是否随时间泄漏了门廊。所以 我想看看有多少goroutines随时间运行。有没有办法通过pprof
来做到这一点?如何剖析门廊的数量
我做了go tool pprof http://localhost:8888/debug/pprof/block
。
这给了我花了多少时间阻止,但没有多少例程正在运行。
由于icza指出答案是使用runtime.NumGoroutine()
。
在您的浏览器中打开http://localhost:8888/debug/pprof/。您会看到两个相关链接:“goroutine”(http://localhost:8888/debug/pprof/goroutine?debug=1)和“完整的常规堆栈转储”(http://localhost:8888/debug/pprof/goroutine?debug=2)。
第一个将显示与一个条目共享相同代码的所有goroutines,其名称前面有这样的goroutines的数目。例如:
1 @ 0x42f223 0x42f2e4 0x40542f 0x404f4b 0x4a0586 0x4600a1
# 0x4a0586 gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).startWorkers+0x56 /home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:164
1 @ 0x42f223 0x43dfd7 0x43d532 0x4a04ed 0x4600a1
# 0x4a04ed gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).processRunners+0x45d /home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:147
这两个goroutines中有一个,这就是@之前的1。
的完全转储是查找泄漏是非常有用的,它会告诉你单独每一个够程,以及它的堆栈跟踪和它在做什么(例如,它已经等待多长时间才能从通道接收):
goroutine 49 [chan receive, 2 minutes]:
gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).startWorkers(0xc820103ee0, 0xc820274000, 0xc820274060, 0xc8201d65a0)
/home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:164 +0x56
created by gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).Run
/home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:294 +0x41b
goroutine 50 [select]:
gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).processRunners(0xc820103ee0, 0x0, 0xc820274060, 0xc8201d65a0)
/home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:147 +0x45d
created by gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands.(*RunCommand).startWorkers
/home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands/multi.go:165 +0x96
当debug = 1时,@表示后的地址是什么? - (“0x42f223 0x42f2e4 0x40542f 0x404f4b 0x4a0586 0x4600a1”) – lafolle
您是否知道['runtime.NumGoroutine()'](https://golang.org/pkg/runtime/#NumGoroutine),它返回当前存在的goroutine的数量? – icza