2

To profile my app, I want to know how many goroutines are waiting to write to or read from a channel; I can't find anything relevant in the reflect package.

I can maintain an explicit counter of course, but I'd expect golang runtime to know that, so I try to avoid reinventing the wheel.

So, is there a way to do that without maintaining the counter manually?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Dmitry Frank
  • 10,417
  • 10
  • 64
  • 114

1 Answers1

1

To track overall load you are probably looking for runtime.NumGoroutine()

https://golang.org/pkg/runtime/#NumGoroutine

Though it's not exactly number of just blocked Go routines it should be very close to it and not exceed the runtime.NumGoroutine() - GOMAXPROCS

For tracking Go routines per channel you can do next:

  1. Use https://golang.org/pkg/runtime/pprof/#Do to mark routines with a specific channel.
  2. Use http/pprof to get information on current profile and parse output - see this answer for details https://stackoverflow.com/a/38414527/1975086. Or maybe you can look into http/pprof and find out how it gets the information so you can get it within your app in typed way.
Alexander Trakhimenok
  • 6,019
  • 2
  • 27
  • 52
  • Thanks, but it doesn't solve the problem: obviously I have more than one channel in my app, and goroutines are waiting on different channels, so I need to see which channels they are waiting for, so I'll be able to spot bottlenecks. – Dmitry Frank Sep 22 '17 at 08:43
  • You don't need to use `net/http/pprof` to get a "block" profile, you can do the same directly from `runtime/pprof` if you don't want the http handler. – JimB Sep 22 '17 at 18:34