diff --git a/src/runtime/mprof.go b/src/runtime/mprof.go index 09cea65bd91..c232b15424e 100644 --- a/src/runtime/mprof.go +++ b/src/runtime/mprof.go @@ -1131,12 +1131,15 @@ func (p *goroutineProfileStateHolder) CompareAndSwap(old, new goroutineProfileSt return (*atomic.Uint32)(p).CompareAndSwap(uint32(old), uint32(new)) } -//go:linkname runtime_gcount runtime/pprof.runtime_gcount -func runtime_gcount() int { - return int(gcount()) -} - func goroutineProfileWithLabelsConcurrent(p []StackRecord, labels []unsafe.Pointer) (n int, ok bool) { + if len(p) == 0 { + // An empty slice is obviously too small. Return a rough + // allocation estimate without bothering to STW. As long as + // this is close, then we'll only need to STW once (on the next + // call). + return int(gcount()), false + } + semacquire(&goroutineProfile.sema) ourg := getg() diff --git a/src/runtime/pprof/pprof.go b/src/runtime/pprof/pprof.go index 79ca11c6b4c..a8422181cca 100644 --- a/src/runtime/pprof/pprof.go +++ b/src/runtime/pprof/pprof.go @@ -755,9 +755,6 @@ func writeGoroutineStacks(w io.Writer) error { return err } -// runtime_gcount is defined in runtime/mprof.go -func runtime_gcount() (n int) - func writeRuntimeProfile(w io.Writer, debug int, name string, fetch func([]runtime.StackRecord, []unsafe.Pointer) (int, bool)) error { // Find out how many records there are (fetch(nil)), // allocate that many records, and get the data. @@ -767,12 +764,7 @@ func writeRuntimeProfile(w io.Writer, debug int, name string, fetch func([]runti // The loop should only execute one iteration in the common case. var p []runtime.StackRecord var labels []unsafe.Pointer - var n, ok = 0, false - if name == "goroutine" { - n = runtime_gcount() - } else { - n, ok = fetch(nil, nil) - } + n, ok := fetch(nil, nil) for { // Allocate room for a slightly bigger profile,