runtime: make NumGoroutine and Stack agree not to include system goroutines

Before, NumGoroutine counted system goroutines and Stack (usually) didn't show them,
which was inconsistent and confusing.

To resolve which way they should be consistent, it seems like

	package main
	import "runtime"
	func main() { println(runtime.NumGoroutine()) }

should print 1 regardless of internal runtime details. Make it so.

Fixes #11706.

Change-Id: I6bfe26a901de517728192cfb26a5568c4ef4fe47
Reviewed-on: https://go-review.googlesource.com/18343
Reviewed-by: Austin Clements <austin@google.com>
This commit is contained in:
Russ Cox 2016-01-06 21:16:01 -05:00
parent 20d745c57c
commit c5bafc8281
5 changed files with 47 additions and 1 deletions

View file

@ -9,6 +9,7 @@ import (
"net"
"runtime"
"runtime/debug"
"strings"
"sync"
"sync/atomic"
"syscall"
@ -336,6 +337,23 @@ func TestGCFairness(t *testing.T) {
}
}
func TestNumGoroutine(t *testing.T) {
output := runTestProg(t, "testprog", "NumGoroutine")
want := "1\n"
if output != want {
t.Fatalf("want %q, got %q", want, output)
}
buf := make([]byte, 1<<20)
buf = buf[:runtime.Stack(buf, true)]
n := runtime.NumGoroutine()
if nstk := strings.Count(string(buf), "goroutine "); n != nstk {
t.Fatalf("NumGoroutine=%d, but found %d goroutines in stack dump", n, nstk)
}
}
func TestPingPongHog(t *testing.T) {
if testing.Short() {
t.Skip("skipping in -short mode")