本文介绍了如何配置goroutines的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想知道我的程序是否会随着时间的推移而泄露goroutines。所以
我想看看有多少个goroutines随时间运行。有没有办法通过 pprof



完成 go工具pprof http:// localhost:8888 / debug / pprof / block



这给了我花了多少时间来阻止,而不是有多少个例程正在运行。

解决方案

打开浏览器。你会看到两个相关的链接:goroutine()和full goroutine stack dump()。

第一个将显示共享相同代码的所有goroutine作为一个入口,在他们的名字之前有这样的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,以及它的堆栈跟踪和它正在做什么(例如,它等待从一个通道接收多长时间):

  goroutine 49 [chan receive,2 minutes]:
gitlab.com/gitlab-org/gitlab-ci-multi (* RunCommand).startWorkers(0xc820103ee0,0xc820274000,0xc820274060,0xc8201d65a0)
/home/me/go/src/gitlab.com/gitlab-org/gitlab-ci-multi-runner/commands /multi.go:164 + 0x56
由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- (* RunCommand).processRunners(0xc820103ee0,0x0,0xc820274060,0xc8201d65a0)
/home/me/go/src/gitlab.com/gitlab-org/gitlab- ci-multi-runner / commands / multi.go:147 + 0x45d
由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


Basically I want to find if my program is leaking goroutines over time. So I want to see how many goroutines are running over time. Is there any way to do this through pprof?

I've done go tool pprof http://localhost:8888/debug/pprof/block.

Which gives me how long is being spent blocked but not how many routines are running.

解决方案

Open http://localhost:8888/debug/pprof/ in your browser. You'll see two relevant links: "goroutine" (http://localhost:8888/debug/pprof/goroutine?debug=1) and "full goroutine stack dump" (http://localhost:8888/debug/pprof/goroutine?debug=2).

The first one will show all goroutines that share the same code as one entry, with the number of such goroutines before their name. For example:

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

There's one of both of these goroutines, that's what the 1 before the @ means.

The full dump is extremely useful for finding leaks, it'll show you every goroutine separately, as well as its stack trace and what it's doing (e.g. how long it has been waiting to receive from a channel):

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

这篇关于如何配置goroutines的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-21 02:16