// simultaneously and returns the previous setting. If n < 1, it does not change
// the current setting.
//
+// # Default
+//
// If the GOMAXPROCS environment variable is set to a positive whole number,
// GOMAXPROCS defaults to that value.
//
-// Otherwise, the Go runtime selects an appropriate default value based on the
-// number of logical CPUs on the machine, the process’s CPU affinity mask, and,
-// on Linux, the process’s average CPU throughput limit based on cgroup CPU
-// quota, if any.
+// Otherwise, the Go runtime selects an appropriate default value from a combination of
+// - the number of logical CPUs on the machine,
+// - the process’s CPU affinity mask,
+// - and, on Linux, the process’s average CPU throughput limit based on cgroup CPU
+// quota, if any.
+//
+// If GODEBUG=containermaxprocs=0 is set and GOMAXPROCS is not set by the
+// environment variable, then GOMAXPROCS instead defaults to the value of
+// [runtime.NumCPU]. Note that GODEBUG=containermaxprocs=0 is [default] for
+// language version 1.24 and below.
+//
+// # Updates
//
// The Go runtime periodically updates the default value based on changes to
// the total logical CPU count, the CPU affinity mask, or cgroup quota. Setting
// GOMAXPROCS disables automatic updates. The default value and automatic
// updates can be restored by calling [SetDefaultGOMAXPROCS].
//
-// If GODEBUG=containermaxprocs=0 is set, GOMAXPROCS defaults to the value of
-// [runtime.NumCPU]. If GODEBUG=updatemaxprocs=0 is set, the Go runtime does
-// not perform automatic GOMAXPROCS updating.
+// If GODEBUG=updatemaxprocs=0 is set, the Go runtime does not perform
+// automatic GOMAXPROCS updating. Note that GODEBUG=updatemaxprocs=0 is
+// [default] for language version 1.24 and below.
+//
+// # Compatibility
+//
+// Note that the default GOMAXPROCS behavior may change as the scheduler
+// improves, especially the implementation detail below.
+//
+// # Implementation details
+//
+// When computing default GOMAXPROCS via cgroups, the Go runtime computes the
+// "average CPU throughput limit" as the cgroup CPU quota / period. In cgroup
+// v2, these values come from the cpu.max file. In cgroup v1, they come from
+// cpu.cfs_quota_us and cpu.cfs_period_us, respectively. In container runtimes
+// that allow configuring CPU limits, this value usually corresponds to the
+// "CPU limit" option, not "CPU request".
+//
+// The Go runtime typically selects the default GOMAXPROCS as the minimum of
+// the logical CPU count, the CPU affinity mask count, or the cgroup CPU
+// throughput limit. However, it will never set GOMAXPROCS less than 2 unless
+// the logical CPU count or CPU affinity mask count are below 2.
+//
+// If the cgroup CPU throughput limit is not a whole number, the Go runtime
+// rounds up to the next whole number.
+//
+// GOMAXPROCS updates are performed up to once per second, or less if the
+// application is idle.
//
-// The default GOMAXPROCS behavior may change as the scheduler improves.
+// [default]: https://go.dev/doc/godebug#default
func GOMAXPROCS(n int) int {
if GOARCH == "wasm" && n > 1 {
n = 1 // WebAssembly has no threads yet, so only one CPU is possible.