Skip to content

Commit 36bffce

Browse files
authored
Merge pull request #193 from nixpanic/automaxprocs
Add package standardflags for -automaxprocs commandline argument
2 parents d347742 + 547e3a3 commit 36bffce

18 files changed

+1251
-0
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/stretchr/testify v1.10.0
99
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.58.0
1010
go.opentelemetry.io/otel/trace v1.33.0
11+
go.uber.org/automaxprocs v1.6.0
1112
google.golang.org/grpc v1.69.0
1213
google.golang.org/protobuf v1.36.0
1314
k8s.io/api v0.32.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
7575
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7676
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
7777
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
78+
github.com/prashantv/gostub v1.1.0 h1:BTyx3RfQjRHnUWaGF9oQos79AlQ5k8WNktv7VGvVH4g=
79+
github.com/prashantv/gostub v1.1.0/go.mod h1:A5zLQHz7ieHGG7is6LLXLz7I8+3LZzsrV0P1IAHhP5U=
7880
github.com/prometheus/client_golang v1.20.5 h1:cxppBPuYhUnsO6yo/aoRol4L7q7UFfdm+bR9r+8l63Y=
7981
github.com/prometheus/client_golang v1.20.5/go.mod h1:PIEt8X02hGcP8JWbeHyeZ53Y/jReSnHgO035n//V5WE=
8082
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
@@ -109,6 +111,8 @@ go.opentelemetry.io/otel/sdk/metric v1.31.0 h1:i9hxxLJF/9kkvfHppyLL55aW7iIJz4Jjx
109111
go.opentelemetry.io/otel/sdk/metric v1.31.0/go.mod h1:CRInTMVvNhUKgSAMbKyTMxqOBC0zgyxzW55lZzX43Y8=
110112
go.opentelemetry.io/otel/trace v1.33.0 h1:cCJuF7LRjUFso9LPnEAHJDB2pqzp+hbO8eu1qqW2d/s=
111113
go.opentelemetry.io/otel/trace v1.33.0/go.mod h1:uIcdVUZMpTAmz0tI1z04GoVSezK37CbGV4fr1f2nBck=
114+
go.uber.org/automaxprocs v1.6.0 h1:O3y2/QNTOdbF+e/dpXNNW7Rx2hZ4sTIPyybbxyNqTUs=
115+
go.uber.org/automaxprocs v1.6.0/go.mod h1:ifeIMSnPZuznNm6jmdzmU3/bfk01Fe2fotchwEFJ8r8=
112116
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
113117
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
114118
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=

standardflags/automaxprocs.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package standardflags contains flags that multiple CSI sidecars and
18+
// drivers may want to support.
19+
package standardflags
20+
21+
import (
22+
"flag"
23+
"fmt"
24+
"strconv"
25+
26+
"go.uber.org/automaxprocs/maxprocs"
27+
)
28+
29+
var (
30+
logFunc func(format string, args ...interface{}) = nil
31+
undoAutomaxprocs func() = nil
32+
)
33+
34+
// AddAutomaxprocs adds the -automaxprocs boolean flag to the commandline options.
35+
// By default the flag is disabled, use [EnableAutomaxprocs] to enable it during
36+
// startup of an application.
37+
//
38+
// The printf function that is passed as an argument, will be used by the
39+
// [maxprocs.Logger] option when the GOMAXPROCS runtime configuration is adjusted.
40+
func AddAutomaxprocs(printf func(format string, args ...interface{})) {
41+
flag.BoolFunc("automaxprocs",
42+
"automatically set GOMAXPROCS to match Linux container CPU quota",
43+
handleAutomaxprocs,
44+
)
45+
46+
if printf != nil {
47+
// maxprocs.Logger expects a Printf like function.
48+
// klog.Info() isn't one, so wrap the contents in a
49+
// fmt.Sprintf() for %-formatting substitution.
50+
logFunc = func(f string, a ...interface{}) {
51+
printf(fmt.Sprintf(f, a...))
52+
}
53+
}
54+
}
55+
56+
// EnableAutomaxprocs can be used as an equivalent of -automaxprocs=true on the
57+
// commandline.
58+
func EnableAutomaxprocs() {
59+
if automaxprocsIsEnabled() {
60+
// enabled already, don't enable again
61+
return
62+
}
63+
64+
flag.Set("automaxprocs", "true")
65+
}
66+
67+
// automaxprocsIsEnabled returns true if maxprocs.Set() was successfully
68+
// executed.
69+
func automaxprocsIsEnabled() bool {
70+
return undoAutomaxprocs != nil
71+
}
72+
73+
// handleAutomaxprocs parses the passed string into a bool, and enables
74+
// automaxprocs according to it. If the passed string is empty, automaxprocs
75+
// is enabled as well.
76+
func handleAutomaxprocs(s string) error {
77+
var err error
78+
enabled := true
79+
80+
if s == "" {
81+
EnableAutomaxprocs()
82+
return nil
83+
}
84+
85+
enabled, err = strconv.ParseBool(s)
86+
if err != nil {
87+
return err
88+
}
89+
90+
switch enabled {
91+
case true:
92+
opts := make([]maxprocs.Option, 0)
93+
if logFunc != nil {
94+
opts = append(opts, maxprocs.Logger(logFunc))
95+
}
96+
undoAutomaxprocs, err = maxprocs.Set(opts...)
97+
case false:
98+
if undoAutomaxprocs != nil {
99+
undoAutomaxprocs()
100+
undoAutomaxprocs = nil
101+
}
102+
}
103+
104+
return err
105+
}

standardflags/automaxprocs_test.go

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright 2025 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package standardflags
18+
19+
import (
20+
"flag"
21+
"testing"
22+
)
23+
24+
func TestAutomaxprocsArgument(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
value string
28+
enabled bool
29+
expectError bool
30+
}{
31+
{
32+
name: "with value as true",
33+
value: "true",
34+
enabled: true,
35+
},
36+
{
37+
name: "with value as false",
38+
value: "false",
39+
enabled: false,
40+
},
41+
{
42+
name: "without value",
43+
enabled: true,
44+
},
45+
{
46+
name: "with invalid value",
47+
value: "error",
48+
expectError: true,
49+
},
50+
}
51+
52+
if flag.Lookup("automaxprocs") == nil {
53+
AddAutomaxprocs(nil) // pass t.Logf to see the logs
54+
}
55+
56+
for _, test := range tests {
57+
t.Run(test.name, func(t *testing.T) {
58+
err := flag.Set("automaxprocs", test.value)
59+
if !test.expectError && err != nil {
60+
t.Errorf("test %s: failed to set value to %q", test.name, test.value)
61+
}
62+
})
63+
}
64+
}
65+
66+
func TestEnableDisableAutomaxprocs(t *testing.T) {
67+
// make sure the flags is not enabled yet
68+
f := flag.Lookup("automaxprocs")
69+
if f == nil {
70+
AddAutomaxprocs(nil)
71+
}
72+
if automaxprocsIsEnabled() {
73+
handleAutomaxprocs("false")
74+
}
75+
76+
EnableAutomaxprocs()
77+
if !automaxprocsIsEnabled() {
78+
t.Errorf("failed to enable automaxprocs")
79+
}
80+
81+
// disable again
82+
handleAutomaxprocs("false")
83+
if automaxprocsIsEnabled() {
84+
t.Errorf("failed to disable automaxprocs")
85+
}
86+
}

vendor/go.uber.org/automaxprocs/LICENSE

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/go.uber.org/automaxprocs/internal/cgroups/cgroup.go

Lines changed: 79 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)