如何分析 Goroutine 數量
監控 Goroutine 數量可以幫助檢測潛在的洩漏。 Pprof 提供了顯示此資訊的方法。
要查看一段時間內的 goroutine 計數,請導覽至 http://localhost:8888/debug/pprof/。有兩個相關選項可用:
goroutine (http://localhost:8888/debug/pprof/goroutine?debug=1)
此視圖對執行的Goroutine 進行分組相同的程式碼並顯示它們的計數。例如:
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
完整的goroutine 堆疊轉儲 (http://localhost:8888/debug/pprof/goroutine?debug=2)
這提供了一個每個goroutine 的全面概述,包括其堆疊追蹤和當前活動(例如,等待從通道接收)。它對於識別洩漏特別有用:
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
以上是如何使用 Pprof 識別和診斷 Goroutine 外洩?的詳細內容。更多資訊請關注PHP中文網其他相關文章!