"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How do you use pprof to profile the number of goroutines in your Go program?

How do you use pprof to profile the number of goroutines in your Go program?

Published on 2024-11-06
Browse:875

How do you use pprof to profile the number of goroutines in your Go program?

Profiling the Number of Goroutines with pprof

Detecting potential goroutine leaks in your Go program requires monitoring the number of goroutines active over time. While the standard go tool pprof command provides insights into blocking, it does not directly address goroutine count.

To effectively profile the number of goroutines, open http://localhost:8888/debug/pprof/ in your browser. This presents two relevant links:

  • Goroutine: http://localhost:8888/debug/pprof/goroutine?debug=1
  • Full Goroutine Stack Dump: http://localhost:8888/debug/pprof/goroutine?debug=2

The Goroutine link displays goroutines sharing the same code as single entries, along with their count. For instance:

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

The number 1 before the @ indicates one instance of each goroutine.

The Full Goroutine Stack Dump is particularly useful for leak detection. It lists each goroutine individually, including its stack trace and activity status:

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

By utilizing these pprof endpoints, you can effectively monitor the number of goroutines within your program, aiding in the detection and resolution of potential goroutine leaks.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3