@@ -18,9 +18,7 @@ import (
18
18
// the current process's cgroup.
19
19
func CPU () {
20
20
if os .Getenv ("GOMAXPROCS" ) != "" {
21
- msgs = append (msgs , func (ctx context.Context ) {
22
- zlog .Info (ctx ).Msg ("GOMAXPROCS set in the environment, skipping auto detection" )
23
- })
21
+ infoLog ("GOMAXPROCS set in the environment, skipping auto detection" )
24
22
return
25
23
}
26
24
root := os .DirFS ("/" )
@@ -43,9 +41,16 @@ func CPU() {
43
41
}
44
42
45
43
func cgLookup (r fs.FS ) (int , error ) {
44
+ const usingDefault = "no CPU quota set, using default"
46
45
var gmp int
47
46
b , err := fs .ReadFile (r , "proc/self/cgroup" )
48
- if err != nil {
47
+ switch {
48
+ case err == nil :
49
+ case errors .Is (err , fs .ErrNotExist ):
50
+ debugLog ("cgroups seemingly not enabled" )
51
+ infoLog (usingDefault )
52
+ return gmp , nil
53
+ default :
49
54
return gmp , err
50
55
}
51
56
var q , p uint64 = 0 , 1
@@ -55,21 +60,22 @@ func cgLookup(r fs.FS) (int, error) {
55
60
sl := bytes .SplitN (s .Bytes (), []byte (":" ), 3 )
56
61
hid , ctls , pb := sl [0 ], sl [1 ], sl [2 ]
57
62
if bytes .Equal (hid , []byte ("0" )) && len (ctls ) == 0 { // If cgroupsv2:
58
- msgs = append (msgs , func (ctx context.Context ) {
59
- zlog .Debug (ctx ).Msg ("found cgroups v2" )
60
- })
63
+ debugLog ("found cgroups v2" )
61
64
n := path .Join ("sys/fs/cgroup" , string (pb ), "cpu.max" )
62
65
b , err := fs .ReadFile (r , n )
63
- if err != nil {
66
+ switch {
67
+ case err == nil :
68
+ case errors .Is (err , fs .ErrNotExist ):
69
+ infoLog (usingDefault )
70
+ return gmp , nil
71
+ default :
64
72
return gmp , err
65
73
}
66
74
l := bytes .Fields (b )
67
75
qt , per := string (l [0 ]), string (l [1 ])
68
76
if qt == "max" {
69
77
// No quota, so bail.
70
- msgs = append (msgs , func (ctx context.Context ) {
71
- zlog .Info (ctx ).Msg ("no CPU quota set, using default" )
72
- })
78
+ infoLog (usingDefault )
73
79
return gmp , nil
74
80
}
75
81
q , err = strconv .ParseUint (qt , 10 , 64 )
@@ -94,19 +100,15 @@ func cgLookup(r fs.FS) (int, error) {
94
100
// This line is not the cpu group.
95
101
continue
96
102
}
97
- msgs = append (msgs , func (ctx context.Context ) {
98
- zlog .Debug (ctx ).Msg ("found cgroups v1 and cpu controller" )
99
- })
103
+ debugLog ("found cgroups v1 and cpu controller" )
100
104
prefix := path .Join ("sys/fs/cgroup" , string (ctls ), string (pb ))
101
105
// Check for the existence of the named cgroup. If it doesn't exist,
102
106
// look at the root of the controller. The named group not existing
103
107
// probably means the process is in a container and is having remounting
104
108
// tricks done. If, for some reason this is actually the root cgroup,
105
109
// it'll be unlimited and fall back to the default.
106
110
if _ , err := fs .Stat (r , prefix ); errors .Is (err , fs .ErrNotExist ) {
107
- msgs = append (msgs , func (ctx context.Context ) {
108
- zlog .Debug (ctx ).Msg ("falling back to root hierarchy" )
109
- })
111
+ debugLog ("falling back to root hierarchy" )
110
112
prefix = path .Join ("sys/fs/cgroup" , string (ctls ))
111
113
}
112
114
@@ -120,9 +122,7 @@ func cgLookup(r fs.FS) (int, error) {
120
122
}
121
123
if qi == - 1 {
122
124
// No quota, so bail.
123
- msgs = append (msgs , func (ctx context.Context ) {
124
- zlog .Info (ctx ).Msg ("no CPU quota set, using default" )
125
- })
125
+ infoLog (usingDefault )
126
126
return gmp , nil
127
127
}
128
128
q = uint64 (qi )
0 commit comments