Debian Codegraph @

maxprocs/maxprocs.go

raw symbols SCIP
1 // Copyright (c) 2017 Uber Technologies, Inc.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20
21 // Package maxprocs lets Go programs easily configure runtime.GOMAXPROCS to
22 // match the configured Linux CPU quota. Unlike the top-level automaxprocs
23 // package, it lets the caller configure logging and handle errors.
24 package maxprocsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/ // import "go.uber.org/automaxprocs/maxprocs"
25
26 import (
27 "osscip-go gomod github.com/golang/go/src go1.18 os/"
28 "runtimescip-go gomod github.com/golang/go/src go1.18 runtime/"
29
30 iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/ "go.uber.org/automaxprocs/internal/runtimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/"
31 )
32
33 const _maxProcsKeyscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/_maxProcsKey. = "GOMAXPROCS"
34
35 func currentMaxProcsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/currentMaxProcs().() int {
36 return runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.GOMAXPROCSscip-go gomod github.com/golang/go/src go1.18 runtime/GOMAXPROCS().(0)
37 }
38
39 type configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config# struct {
40 printfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#printf. func(string, ...interface{})
41 procsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#procs. func(int) (int, iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/.CPUQuotaStatusscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/CPUQuotaStatus#, error)
42 minGOMAXPROCSscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#minGOMAXPROCS. int
43 }
44
45 func (c *configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#) logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().(fmt string, args ...interface{}) {
46 if c.printfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#printf. != nil {
47 c.printfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#printf.(fmt, args...)
48 }
49 }
50
51 // An Option alters the behavior of Set.
52 type Optionscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option# interface {
53 applyscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option#apply.(*configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#)
54 }
55
56 // Logger uses the supplied printf implementation for log output. By default,
57 // Set doesn't log anything.
58 func Loggerscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Logger().(printf func(string, ...interface{})) Optionscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option# {
59 return optionFuncscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/optionFunc#(func(cfg *configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#) {
60 cfg.printfscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#printf. = printf
61 })
62 }
63
64 // Min sets the minimum GOMAXPROCS value that will be used.
65 // Any value below 1 is ignored.
66 func Minscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Min().(n int) Optionscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option# {
67 return optionFuncscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/optionFunc#(func(cfg *configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#) {
68 if n >= 1 {
69 cfg.minGOMAXPROCSscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#minGOMAXPROCS. = n
70 }
71 })
72 }
73
74 type optionFuncscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/optionFunc# func(*configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#)
75
76 func (of optionFuncscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/optionFunc#) applyscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/optionFunc#apply().(cfg *configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#) { of(cfg) }
77
78 // Set GOMAXPROCS to match the Linux container CPU quota (if any), returning
79 // any error encountered and an undo function.
80 //
81 // Set is a no-op on non-Linux systems and in Linux environments without a
82 // configured CPU quota.
83 func Setscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Set().(opts ...Optionscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option#) (func(), error) {
84 cfg := &configscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#{
85 procsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#procs.: iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/.CPUQuotaToGOMAXPROCSscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/CPUQuotaToGOMAXPROCS().,
86 minGOMAXPROCSscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#minGOMAXPROCS.: 1,
87 }
88 for _, o := range opts {
89 o.applyscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/Option#apply.(cfg)
90 }
91
92 undoNoop := func() {
93 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: No GOMAXPROCS change to reset")
94 }
95
96 // Honor the GOMAXPROCS environment variable if present. Otherwise, amend
97 // `runtime.GOMAXPROCS()` with the current process' CPU quota if the OS is
98 // Linux, and guarantee a minimum value of 1. The minimum guaranteed value
99 // can be overridden using `maxprocs.Min()`.
100 if max, exists := osscip-go gomod github.com/golang/go/src go1.18 os/.LookupEnvscip-go gomod github.com/golang/go/src go1.18 os/LookupEnv().(_maxProcsKeyscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/_maxProcsKey.); exists {
101 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: Honoring GOMAXPROCS=%q as set in environment", max)
102 return undoNoop, nil
103 }
104
105 maxProcs, status, err := cfg.procsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#procs.(cfg.minGOMAXPROCSscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#minGOMAXPROCS.)
106 if err != nil {
107 return undoNoop, err
108 }
109
110 if status == iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/.CPUQuotaUndefinedscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/CPUQuotaUndefined. {
111 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: Leaving GOMAXPROCS=%v: CPU quota undefined", currentMaxProcsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/currentMaxProcs().())
112 return undoNoop, nil
113 }
114
115 prev := currentMaxProcsscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/currentMaxProcs().()
116 undo := func() {
117 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: Resetting GOMAXPROCS to %v", prev)
118 runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.GOMAXPROCSscip-go gomod github.com/golang/go/src go1.18 runtime/GOMAXPROCS().(prev)
119 }
120
121 switch status {
122 case iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/.CPUQuotaMinUsedscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/CPUQuotaMinUsed.:
123 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: Updating GOMAXPROCS=%v: using minimum allowed GOMAXPROCS", maxProcs)
124 case iruntimescip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/.CPUQuotaUsedscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/internal/runtime`/CPUQuotaUsed.:
125 cfg.logscip-go gomod go.uber.org/automaxprocs . `go.uber.org/automaxprocs/maxprocs`/config#log().("maxprocs: Updating GOMAXPROCS=%v: determined from CPU quota", maxProcs)
126 }
127
128 runtimescip-go gomod github.com/golang/go/src go1.18 runtime/.GOMAXPROCSscip-go gomod github.com/golang/go/src go1.18 runtime/GOMAXPROCS().(maxProcs)
129 return undo, nil
130 }