]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: reduce one STW when obtaining goroutine configuration file
authorJun10ng <zeonll@outlook.com>
Sat, 27 Jan 2024 11:53:43 +0000 (11:53 +0000)
committerGopher Robot <gobot@golang.org>
Tue, 30 Jan 2024 00:04:58 +0000 (00:04 +0000)
Fixes #54014

Change-Id: If4ee2752008729e1ed4b767cfda52effdcec4959
GitHub-Last-Rev: 5ce300bf5128f842604d85d5f8749027c8e091c2
GitHub-Pull-Request: golang/go#58239
Reviewed-on: https://go-review.googlesource.com/c/go/+/464349
Reviewed-by: qiulaidongfeng <2645477756@qq.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Michael Pratt <mpratt@google.com>
Run-TryBot: qiulaidongfeng <2645477756@qq.com>
Auto-Submit: Michael Pratt <mpratt@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
src/runtime/mprof.go
src/runtime/pprof/pprof.go

index abdd2f3e8c9db859c5b302e8f061ffa642fe1792..09cea65bd917338a98a2ab69517ee1d3c3396bc5 100644 (file)
@@ -1131,6 +1131,11 @@ 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) {
        semacquire(&goroutineProfile.sema)
 
index a4dcf33508265ce76294c927de45e8089a3be533..79ca11c6b4c7c168f9f5da1c6ba44d78766a3379 100644 (file)
@@ -755,6 +755,9 @@ 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.
@@ -764,7 +767,13 @@ 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
-       n, ok := fetch(nil, nil)
+       var n, ok = 0, false
+       if name == "goroutine" {
+               n = runtime_gcount()
+       } else {
+               n, ok = fetch(nil, nil)
+       }
+
        for {
                // Allocate room for a slightly bigger profile,
                // in case a few more entries have been added