Skip to content

Commit 07b0ea7

Browse files
committed
auto: improve log messages
Adds some helpers and improves logging around missing cgroup interface files. See-also: #2092 Signed-off-by: Hank Donnay <[email protected]>
1 parent 0858786 commit 07b0ea7

File tree

3 files changed

+54
-40
lines changed

3 files changed

+54
-40
lines changed

initialize/auto/auto.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package auto
66

77
import (
88
"context"
9+
10+
"github.com/quay/zlog"
911
)
1012

1113
var msgs = []func(context.Context){}
@@ -24,3 +26,17 @@ func PrintLogs(ctx context.Context) {
2426
}
2527
msgs = msgs[:0]
2628
}
29+
30+
// DebugLog is a helper to log static strings.
31+
func debugLog(m string) {
32+
msgs = append(msgs, func(ctx context.Context) {
33+
zlog.Debug(ctx).Msg(m)
34+
})
35+
}
36+
37+
// InfoLog is a helper to log static strings.
38+
func infoLog(m string) {
39+
msgs = append(msgs, func(ctx context.Context) {
40+
zlog.Info(ctx).Msg(m)
41+
})
42+
}

initialize/auto/cpu_linux.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ import (
1818
// the current process's cgroup.
1919
func CPU() {
2020
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")
2422
return
2523
}
2624
root := os.DirFS("/")
@@ -43,9 +41,16 @@ func CPU() {
4341
}
4442

4543
func cgLookup(r fs.FS) (int, error) {
44+
const usingDefault = "no CPU quota set, using default"
4645
var gmp int
4746
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:
4954
return gmp, err
5055
}
5156
var q, p uint64 = 0, 1
@@ -55,21 +60,22 @@ func cgLookup(r fs.FS) (int, error) {
5560
sl := bytes.SplitN(s.Bytes(), []byte(":"), 3)
5661
hid, ctls, pb := sl[0], sl[1], sl[2]
5762
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")
6164
n := path.Join("sys/fs/cgroup", string(pb), "cpu.max")
6265
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:
6472
return gmp, err
6573
}
6674
l := bytes.Fields(b)
6775
qt, per := string(l[0]), string(l[1])
6876
if qt == "max" {
6977
// 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)
7379
return gmp, nil
7480
}
7581
q, err = strconv.ParseUint(qt, 10, 64)
@@ -94,19 +100,15 @@ func cgLookup(r fs.FS) (int, error) {
94100
// This line is not the cpu group.
95101
continue
96102
}
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")
100104
prefix := path.Join("sys/fs/cgroup", string(ctls), string(pb))
101105
// Check for the existence of the named cgroup. If it doesn't exist,
102106
// look at the root of the controller. The named group not existing
103107
// probably means the process is in a container and is having remounting
104108
// tricks done. If, for some reason this is actually the root cgroup,
105109
// it'll be unlimited and fall back to the default.
106110
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")
110112
prefix = path.Join("sys/fs/cgroup", string(ctls))
111113
}
112114

@@ -120,9 +122,7 @@ func cgLookup(r fs.FS) (int, error) {
120122
}
121123
if qi == -1 {
122124
// 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)
126126
return gmp, nil
127127
}
128128
q = uint64(qi)

initialize/auto/memory_linux.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
//go:build go1.19
2-
31
package auto
42

53
import (
@@ -38,15 +36,10 @@ func Memory() {
3836
})
3937
return
4038
case lim == doNothing:
41-
msgs = append(msgs, func(ctx context.Context) {
42-
zlog.Info(ctx).
43-
Msg("no memory limit configured")
44-
})
39+
infoLog("no memory limit configured")
4540
return
4641
case lim == setMax:
47-
msgs = append(msgs, func(ctx context.Context) {
48-
zlog.Info(ctx).Msg("memory limit unset")
49-
})
42+
infoLog("memory limit unset")
5043
return
5144
}
5245
// Following the GC guide and taking a haircut: https://tip.golang.org/doc/gc-guide#Suggested_uses
@@ -67,7 +60,12 @@ const (
6760

6861
func memLookup(r fs.FS) (int64, error) {
6962
b, err := fs.ReadFile(r, "proc/self/cgroup")
70-
if err != nil {
63+
switch {
64+
case err == nil:
65+
case errors.Is(err, fs.ErrNotExist):
66+
debugLog("cgroups seemingly not enabled")
67+
return doNothing, nil
68+
default:
7169
return 0, err
7270
}
7371
s := bufio.NewScanner(bytes.NewReader(b))
@@ -76,14 +74,13 @@ func memLookup(r fs.FS) (int64, error) {
7674
sl := bytes.SplitN(s.Bytes(), []byte(":"), 3)
7775
hid, ctls, pb := sl[0], sl[1], sl[2]
7876
if bytes.Equal(hid, []byte("0")) && len(ctls) == 0 { // If cgroupsv2:
79-
msgs = append(msgs, func(ctx context.Context) {
80-
zlog.Debug(ctx).Msg("found cgroups v2")
81-
})
77+
debugLog("found cgroups v2")
8278
n := path.Join("sys/fs/cgroup", string(pb), "memory.max")
8379
b, err := fs.ReadFile(r, n)
8480
switch {
8581
case errors.Is(err, nil):
8682
case errors.Is(err, fs.ErrNotExist):
83+
debugLog(`no "memory.max" file`)
8784
return doNothing, nil
8885
default:
8986
return 0, err
@@ -105,24 +102,25 @@ func memLookup(r fs.FS) (int64, error) {
105102
if !isMem { // This line is not the memory group.
106103
continue
107104
}
108-
msgs = append(msgs, func(ctx context.Context) {
109-
zlog.Debug(ctx).Msg("found cgroups v1 and memory controller")
110-
})
105+
debugLog("found cgroups v1 and memory controller")
111106
prefix := path.Join("sys/fs/cgroup", string(ctls), string(pb))
112107
// Check for the existence of the named cgroup. If it doesn't exist,
113108
// look at the root of the controller. The named group not existing
114109
// probably means the process is in a container and is having remounting
115110
// tricks done. If, for some reason this is actually the root cgroup,
116111
// it'll be unlimited and fall back to the default.
117112
if _, err := fs.Stat(r, prefix); errors.Is(err, fs.ErrNotExist) {
118-
msgs = append(msgs, func(ctx context.Context) {
119-
zlog.Debug(ctx).Msg("falling back to root hierarchy")
120-
})
113+
debugLog("falling back to root hierarchy")
121114
prefix = path.Join("sys/fs/cgroup", string(ctls))
122115
}
123116

124117
b, err = fs.ReadFile(r, path.Join(prefix, "memory.limit_in_bytes"))
125-
if err != nil {
118+
switch {
119+
case errors.Is(err, nil):
120+
case errors.Is(err, fs.ErrNotExist):
121+
debugLog(`no "memory.limit_in_bytes" file`)
122+
return doNothing, nil
123+
default:
126124
return 0, err
127125
}
128126
v := string(bytes.TrimSpace(b))

0 commit comments

Comments
 (0)