Skip to content

Commit bb07e55

Browse files
prattmicgopherbot
authored andcommitted
runtime: expand GOMAXPROCS documentation
Expand the GOMAXPROCS documentation to include details of how defaults are selected, as this is something that inquisitive minds will want to know. I've added an additional warning that these details may changed. While we are here, add a bit more structure to make it easier to find the relevant parts of the documentation. For #73193. Change-Id: I6a6a636cae93237e3e3174822490d51805e70990 Reviewed-on: https://go-review.googlesource.com/c/go/+/685318 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Sean Liao <sean@liao.dev> Reviewed-by: Sean Liao <sean@liao.dev> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
1 parent 9159cd4 commit bb07e55

File tree

1 file changed

+43
-8
lines changed

1 file changed

+43
-8
lines changed

src/runtime/debug.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,60 @@ import (
1313
// simultaneously and returns the previous setting. If n < 1, it does not change
1414
// the current setting.
1515
//
16+
// # Default
17+
//
1618
// If the GOMAXPROCS environment variable is set to a positive whole number,
1719
// GOMAXPROCS defaults to that value.
1820
//
19-
// Otherwise, the Go runtime selects an appropriate default value based on the
20-
// number of logical CPUs on the machine, the process’s CPU affinity mask, and,
21-
// on Linux, the process’s average CPU throughput limit based on cgroup CPU
22-
// quota, if any.
21+
// Otherwise, the Go runtime selects an appropriate default value from a combination of
22+
// - the number of logical CPUs on the machine,
23+
// - the process’s CPU affinity mask,
24+
// - and, on Linux, the process’s average CPU throughput limit based on cgroup CPU
25+
// quota, if any.
26+
//
27+
// If GODEBUG=containermaxprocs=0 is set and GOMAXPROCS is not set by the
28+
// environment variable, then GOMAXPROCS instead defaults to the value of
29+
// [runtime.NumCPU]. Note that GODEBUG=containermaxprocs=0 is [default] for
30+
// language version 1.24 and below.
31+
//
32+
// # Updates
2333
//
2434
// The Go runtime periodically updates the default value based on changes to
2535
// the total logical CPU count, the CPU affinity mask, or cgroup quota. Setting
2636
// a custom value with the GOMAXPROCS environment variable or by calling
2737
// GOMAXPROCS disables automatic updates. The default value and automatic
2838
// updates can be restored by calling [SetDefaultGOMAXPROCS].
2939
//
30-
// If GODEBUG=containermaxprocs=0 is set, GOMAXPROCS defaults to the value of
31-
// [runtime.NumCPU]. If GODEBUG=updatemaxprocs=0 is set, the Go runtime does
32-
// not perform automatic GOMAXPROCS updating.
40+
// If GODEBUG=updatemaxprocs=0 is set, the Go runtime does not perform
41+
// automatic GOMAXPROCS updating. Note that GODEBUG=updatemaxprocs=0 is
42+
// [default] for language version 1.24 and below.
43+
//
44+
// # Compatibility
45+
//
46+
// Note that the default GOMAXPROCS behavior may change as the scheduler
47+
// improves, especially the implementation detail below.
48+
//
49+
// # Implementation details
50+
//
51+
// When computing default GOMAXPROCS via cgroups, the Go runtime computes the
52+
// "average CPU throughput limit" as the cgroup CPU quota / period. In cgroup
53+
// v2, these values come from the cpu.max file. In cgroup v1, they come from
54+
// cpu.cfs_quota_us and cpu.cfs_period_us, respectively. In container runtimes
55+
// that allow configuring CPU limits, this value usually corresponds to the
56+
// "CPU limit" option, not "CPU request".
57+
//
58+
// The Go runtime typically selects the default GOMAXPROCS as the minimum of
59+
// the logical CPU count, the CPU affinity mask count, or the cgroup CPU
60+
// throughput limit. However, it will never set GOMAXPROCS less than 2 unless
61+
// the logical CPU count or CPU affinity mask count are below 2.
62+
//
63+
// If the cgroup CPU throughput limit is not a whole number, the Go runtime
64+
// rounds up to the next whole number.
65+
//
66+
// GOMAXPROCS updates are performed up to once per second, or less if the
67+
// application is idle.
3368
//
34-
// The default GOMAXPROCS behavior may change as the scheduler improves.
69+
// [default]: https://go.dev/doc/godebug#default
3570
func GOMAXPROCS(n int) int {
3671
if GOARCH == "wasm" && n > 1 {
3772
n = 1 // WebAssembly has no threads yet, so only one CPU is possible.

0 commit comments

Comments
 (0)