]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: note custom GOMAXPROCS even if value doesn't change
authorMichael Pratt <mpratt@google.com>
Tue, 24 Jun 2025 20:33:10 +0000 (16:33 -0400)
committerGopher Robot <gobot@golang.org>
Wed, 25 Jun 2025 15:55:42 +0000 (08:55 -0700)
When an application calls runtime.GOMAXPROCS(runtime.GOMAXPROCS(0)), the
runtime does not need to change the actual GOMAXPROCS value (via STW).
However, this call must still transition from "automatic" to "custom"
GOMAXPROCS state, thus disabling background updates.

Thus this case shouldn't return quite as early as it currently does.

Change-Id: I6a6a636c42f73996532bd9f7beb95e933256c9e7
Reviewed-on: https://go-review.googlesource.com/c/go/+/683815
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Carlos Amedee <carlos@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Michael Pratt <mpratt@google.com>

src/runtime/debug.go
src/runtime/testdata/testprog/gomaxprocs.go

index bdaaa7196d30bb22f5cba4d058557dd4db5e8fb7..c7592d33299681a0aed212cae61c0cb930effaed 100644 (file)
@@ -39,7 +39,7 @@ func GOMAXPROCS(n int) int {
 
        lock(&sched.lock)
        ret := int(gomaxprocs)
-       if n <= 0 || n == ret {
+       if n <= 0 {
                unlock(&sched.lock)
                return ret
        }
@@ -52,6 +52,12 @@ func GOMAXPROCS(n int) int {
        lock(&computeMaxProcsLock)
        unlock(&computeMaxProcsLock)
 
+       if n == ret {
+               // sched.customGOMAXPROCS set, but no need to actually STW
+               // since the gomaxprocs itself isn't changing.
+               return ret
+       }
+
        stw := stopTheWorldGC(stwGOMAXPROCS)
 
        // newprocs will be processed by startTheWorld
index 915e3c4dad4e12879f9a4f7d88340448aab5d86a..99bc9f1dbb350a50dcc4559973a2ff53a460ac05 100644 (file)
@@ -133,6 +133,20 @@ func UpdateGOMAXPROCS() {
        mustSetCPUMax(path, 200000)
        mustNotChangeMaxProcs(3)
 
+       // Re-enable updates. Change is immediately visible.
+       runtime.SetDefaultGOMAXPROCS()
+       procs = runtime.GOMAXPROCS(0)
+       println("GOMAXPROCS:", procs)
+       if procs != 2 {
+               panic(fmt.Sprintf("GOMAXPROCS got %d want %d", procs, 2))
+       }
+
+       // Setting GOMAXPROCS to itself also disables updates, despite not
+       // changing the value itself.
+       runtime.GOMAXPROCS(runtime.GOMAXPROCS(0))
+       mustSetCPUMax(path, 300000)
+       mustNotChangeMaxProcs(2)
+
        println("OK")
 }