]> Cypherpunks repositories - gostls13.git/commitdiff
[dev.regabi] cmd/go: Use GOMAXPROCS to limit default build, compile parallelism
authorDavid Chase <drchase@google.com>
Wed, 14 Oct 2020 17:05:33 +0000 (13:05 -0400)
committerDavid Chase <drchase@google.com>
Wed, 27 Jan 2021 21:55:30 +0000 (21:55 +0000)
When people want deterministic/single-process builds, they probably
assume that GOMAXPROCS=1 will do that.  It currently does not,
neither for build parallelism nor for compiler internal parallelism.
(Current incantation for that is "go build -p=1 -gcflags=all=-c=1 ... ")

This CL makes
  "GOMAXPROCS=1 go build ..."
behave like
  "go build -p=1 -gcflags=all=-c=1 ... "

RELNOTE=yes

Change-Id: I9cfe50b7deee7334d2f1057b58385f6c98547b9f
Reviewed-on: https://go-review.googlesource.com/c/go/+/284695
Trust: David Chase <drchase@google.com>
Run-TryBot: David Chase <drchase@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Jeremy Faller <jeremy@golang.org>
src/cmd/go/alldocs.go
src/cmd/go/internal/cfg/cfg.go
src/cmd/go/internal/work/build.go
src/cmd/go/internal/work/gc.go

index 49d390297cdc508135437283e4670e9824c86cd4..da06e831aeeee02743d3430bd14787e41de88fef 100644 (file)
 //     -p n
 //             the number of programs, such as build commands or
 //             test binaries, that can be run in parallel.
-//             The default is the number of CPUs available.
+//             The default is GOMAXPROCS, normally the number of CPUs available.
 //     -race
 //             enable data race detection.
 //             Supported only on linux/amd64, freebsd/amd64, darwin/amd64, windows/amd64,
index c48904eacceab90949bc12248ad82b91803a16ca..322247962f8951a12ea8d5fc3f617cf447dfe590 100644 (file)
@@ -28,18 +28,18 @@ var (
        BuildA                 bool   // -a flag
        BuildBuildmode         string // -buildmode flag
        BuildContext           = defaultContext()
-       BuildMod               string             // -mod flag
-       BuildModExplicit       bool               // whether -mod was set explicitly
-       BuildModReason         string             // reason -mod was set, if set by default
-       BuildI                 bool               // -i flag
-       BuildLinkshared        bool               // -linkshared flag
-       BuildMSan              bool               // -msan flag
-       BuildN                 bool               // -n flag
-       BuildO                 string             // -o flag
-       BuildP                 = runtime.NumCPU() // -p flag
-       BuildPkgdir            string             // -pkgdir flag
-       BuildRace              bool               // -race flag
-       BuildToolexec          []string           // -toolexec flag
+       BuildMod               string                  // -mod flag
+       BuildModExplicit       bool                    // whether -mod was set explicitly
+       BuildModReason         string                  // reason -mod was set, if set by default
+       BuildI                 bool                    // -i flag
+       BuildLinkshared        bool                    // -linkshared flag
+       BuildMSan              bool                    // -msan flag
+       BuildN                 bool                    // -n flag
+       BuildO                 string                  // -o flag
+       BuildP                 = runtime.GOMAXPROCS(0) // -p flag
+       BuildPkgdir            string                  // -pkgdir flag
+       BuildRace              bool                    // -race flag
+       BuildToolexec          []string                // -toolexec flag
        BuildToolchainName     string
        BuildToolchainCompiler func() string
        BuildToolchainLinker   func() string
index 780d639c5d9f3e16cdc8e6b1bfbbc34245145617..0e7af6d33f570685d1ac135b30819d08bbd55d08 100644 (file)
@@ -71,7 +71,7 @@ and test commands:
        -p n
                the number of programs, such as build commands or
                test binaries, that can be run in parallel.
-               The default is the number of CPUs available.
+               The default is GOMAXPROCS, normally the number of CPUs available.
        -race
                enable data race detection.
                Supported only on linux/amd64, freebsd/amd64, darwin/amd64, windows/amd64,
index 3205fcbffc8d2513b24354174daca7e2a6c9d2fc..2087855b3c04a96c632f22f93875612a5b91c749 100644 (file)
@@ -239,16 +239,19 @@ CheckFlags:
        //   - it has no successor packages to compile (usually package main)
        //   - all paths through the build graph pass through it
        //   - critical path scheduling says it is high priority
-       // and in such a case, set c to runtime.NumCPU.
+       // and in such a case, set c to runtime.GOMAXPROCS(0).
+       // By default this is the same as runtime.NumCPU.
        // We do this now when p==1.
+       // To limit parallelism, set GOMAXPROCS below numCPU; this may be useful
+       // on a low-memory builder, or if a deterministic build order is required.
+       c := runtime.GOMAXPROCS(0)
        if cfg.BuildP == 1 {
-               // No process parallelism. Max out c.
-               return runtime.NumCPU()
+               // No process parallelism, do not cap compiler parallelism.
+               return c
        }
-       // Some process parallelism. Set c to min(4, numcpu).
-       c := 4
-       if ncpu := runtime.NumCPU(); ncpu < c {
-               c = ncpu
+       // Some process parallelism. Set c to min(4, maxprocs).
+       if c > 4 {
+               c = 4
        }
        return c
 }