Inspect GOMAXPROCS and container behavior
Inspect GOMAXPROCS and container behavior
GOMAXPROCS limits concurrent execution of ordinary Go code, not the number of goroutines or all OS threads. CPU affinity restricts which CPUs may be used; a cgroup CPU quota limits CPU time over a period. They are different constraints. Go 1.23 does not automatically derive GOMAXPROCS from cgroup CPU quota. Go 1.25 introduces quota-aware defaults on Linux and periodic updates, subject to language-version defaults and GODEBUG settings; an explicit environment setting or positive runtime.GOMAXPROCS call disables automatic updates. Verify your actual deployment settings, rather than assuming a CPU limit is a thread count.
package main
import ( "fmt" "runtime")
func main() { before := runtime.GOMAXPROCS(0) previous := runtime.GOMAXPROCS(1) fmt.Println(previous == before, runtime.GOMAXPROCS(0)) runtime.GOMAXPROCS(before)}A nonpositive argument queries the current value without modifying it. A positive argument sets it and returns the previous value. Restoring the numeric value is sufficient for this isolated Go 1.23 example; on newer versions it does not restore automatic updating. Go 1.25 adds runtime.SetDefaultGOMAXPROCS for that purpose; the newer API is documented, not executed here.
Expected output:
true 1Interpret a quota without treating it as a core count
package main
import ( "fmt")
func main() { quotaMicros, periodMicros := 150000, 100000 fmt.Printf("%.1f CPU-time equivalents\n", float64(quotaMicros)/float64(periodMicros))}This example computes a CPU-time budget from synthetic positive values, not the Go runtime default algorithm. Go 1.25 considers logical CPUs, affinity and quota, rounds fractional quota upward and normally avoids defaults below two unless CPU count or affinity is below two. Quotas can cause throttling; measure throughput and tail latency under the real limit before tuning. Do not copy this calculation as an automatic configuration utility.
Expected output:
1.5 CPU-time equivalentsTry it locally
In a standalone process, set GOMAXPROCS to 3, read the matching runtime metric, print whether the values agree, and restore the old numeric setting.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Runtime scheduler 06: Inspect GOMAXPROCS and container behavior
Check the lesson objective before moving on.
Does GOMAXPROCS bound all OS threads?
What is required to restore the automatic default after a positive setter in Go 1.25?