Determining the count of active goroutines

841 views
Skip to first unread message

bmizerany

unread,
Dec 1, 2010, 7:37:04 PM12/1/10
to golang-nuts
Is there a reliable way to determine how may active goroutines are
running at runtime?
I've been looked in the runtime package but am coming up blank.

This would be very useful for testing/profiling.

Ian Lance Taylor

unread,
Dec 1, 2010, 8:18:05 PM12/1/10
to bmizerany, golang-nuts
bmizerany <blake.m...@gmail.com> writes:

How many goroutines are actively running, or how many goroutines
actually exist? The number of goroutines actively running is the number
of M structures that are running. Look at runtime·sched for various
useful counts and lists.

To find the goroutines that exist, whether or not they are running,
start at runtime·allg and walk through the alllink field. Don't count
the ones whose status field == Gdead.

Ian

Keith Rarick

unread,
Dec 3, 2010, 6:25:53 PM12/3/10
to golang-nuts
On Dec 1, 5:18 pm, Ian Lance Taylor <i...@google.com> wrote:
> To find the goroutines that exist, whether or not they are running,
> start at runtime·allg and walk through the alllink field.  Don't count
> the ones whose status field == Gdead.

Out of curiosity, what is the relationship of this count to
runtime·sched.gcount?

kr

Ian Lance Taylor

unread,
Dec 3, 2010, 6:35:04 PM12/3/10
to Keith Rarick, golang-nuts
Keith Rarick <k...@xph.us> writes:

Well, it should be the same. Good point.

Ian

Blake Mizerany

unread,
Dec 3, 2010, 7:27:54 PM12/3/10
to Ian Lance Taylor, Keith Rarick, golang-nuts
That helps. Thank you very much.

-b

--
Blake Mizerany
blake.m...@gmail.com

k...@xph.us

unread,
Dec 3, 2010, 7:32:57 PM12/3/10
to Ian Lance Taylor, golang-nuts
I'd be happy to submit a patch to expose runtime·sched.gcount
to Go code.

Would this patch be accepted?

Is runtime.Goroutines() a suitable name for the function?

kr

Russ Cox

unread,
Dec 6, 2010, 4:43:45 PM12/6/10
to k...@xph.us, Ian Lance Taylor, golang-nuts

Seems reasonable to me.

Russ

Reply all
Reply to author
Forward
0 new messages