From 5662566e03ace5be628c33d3aa6ed5c52c23c050 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Thu, 30 Mar 2023 12:19:09 -0400 Subject: [PATCH] Set GOMAXPROCS explicitly Signed-off-by: Davanum Srinivas --- hack/lib/golang.sh | 13 +++++++++++++ hack/tools/go.mod | 1 + hack/tools/go.sum | 3 +++ hack/tools/ncpu/main.go | 29 +++++++++++++++++++++++++++++ hack/tools/tools.go | 3 +++ 5 files changed, 49 insertions(+) create mode 100644 hack/tools/ncpu/main.go diff --git a/hack/lib/golang.sh b/hack/lib/golang.sh index f23730b9446..6b1b3020247 100755 --- a/hack/lib/golang.sh +++ b/hack/lib/golang.sh @@ -554,6 +554,19 @@ kube::golang::setup_env() { # This seems to matter to some tools export GO15VENDOREXPERIMENT=1 + + # GOMAXPROCS by default does not reflect the number of cpu(s) available + # when running in a container, please see https://github.com/golang/go/issues/33803 + if ! command -v ncpu >/dev/null 2>&1; then + # shellcheck disable=SC2164 + pushd "${KUBE_ROOT}/hack/tools" >/dev/null + GO111MODULE=on go install ./ncpu + # shellcheck disable=SC2164 + popd >/dev/null + fi + + GOMAXPROCS=${GOMAXPROCS:-$(ncpu)} + kube::log::status "Setting GOMAXPROCS: ${GOMAXPROCS}" } # This will take binaries from $GOPATH/bin and copy them to the appropriate diff --git a/hack/tools/go.mod b/hack/tools/go.mod index ff489f7f1da..22c1a78a525 100644 --- a/hack/tools/go.mod +++ b/hack/tools/go.mod @@ -9,6 +9,7 @@ require ( github.com/golang/mock v1.6.0 github.com/golangci/golangci-lint v1.51.2 github.com/google/go-flow-levee v0.1.5 + go.uber.org/automaxprocs v1.5.2 gotest.tools/gotestsum v1.6.4 honnef.co/go/tools v0.4.2 sigs.k8s.io/logtools v0.4.1 diff --git a/hack/tools/go.sum b/hack/tools/go.sum index aa1b2e6932a..65b2b0eb779 100644 --- a/hack/tools/go.sum +++ b/hack/tools/go.sum @@ -542,6 +542,7 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/polyfloyd/go-errorlint v1.1.0 h1:VKoEFg5yxSgJ2yFPVhxW7oGz+f8/OVcuMeNvcPIi6Eg= github.com/polyfloyd/go-errorlint v1.1.0/go.mod h1:Uss7Bc/izYG0leCMRx3WVlrpqWedSZk7V/FUQW6VJ6U= github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI= +github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= @@ -718,6 +719,8 @@ go.opencensus.io v0.22.5/go.mod h1:5pWMHQbX5EPX2/62yrJeAkowc+lfs/XD7Uxpq3pI6kk= go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE= go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc= +go.uber.org/automaxprocs v1.5.2 h1:2LxUOGiR3O6tw8ui5sZa2LAaHnsviZdVOUZw4fvbnME= +go.uber.org/automaxprocs v1.5.2/go.mod h1:eRbA25aqJrxAbsLO0xy5jVwPt7FQnRgjW+efnwa1WM0= go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0= go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= diff --git a/hack/tools/ncpu/main.go b/hack/tools/ncpu/main.go new file mode 100644 index 00000000000..edaeb98a8ac --- /dev/null +++ b/hack/tools/ncpu/main.go @@ -0,0 +1,29 @@ +/* +Copyright 2023 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package main + +import ( + "fmt" + "runtime" + + "go.uber.org/automaxprocs/maxprocs" +) + +func main() { + maxprocs.Set() + fmt.Print(runtime.GOMAXPROCS(0)) +} diff --git a/hack/tools/tools.go b/hack/tools/tools.go index 9957bd622a3..dc7fb413d2d 100644 --- a/hack/tools/tools.go +++ b/hack/tools/tools.go @@ -36,4 +36,7 @@ import ( // mockgen _ "github.com/golang/mock/mockgen" + + // tools like cpu + _ "go.uber.org/automaxprocs" )