Skip to content

Commit

Permalink
auto: improve log messages
Browse files Browse the repository at this point in the history
Adds some helpers and improves logging around missing cgroup interface
files.

See-also: #2092
Signed-off-by: Hank Donnay <hdonnay@redhat.com>
  • Loading branch information
hdonnay committed Jul 15, 2024
1 parent 0858786 commit 07b0ea7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
16 changes: 16 additions & 0 deletions initialize/auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package auto

import (
"context"

"github.com/quay/zlog"
)

var msgs = []func(context.Context){}
Expand All @@ -24,3 +26,17 @@ func PrintLogs(ctx context.Context) {
}
msgs = msgs[:0]
}

// DebugLog is a helper to log static strings.
func debugLog(m string) {
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg(m)
})
}

// InfoLog is a helper to log static strings.
func infoLog(m string) {
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).Msg(m)
})
}
40 changes: 20 additions & 20 deletions initialize/auto/cpu_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import (
// the current process's cgroup.
func CPU() {
if os.Getenv("GOMAXPROCS") != "" {
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).Msg("GOMAXPROCS set in the environment, skipping auto detection")
})
infoLog("GOMAXPROCS set in the environment, skipping auto detection")
return
}
root := os.DirFS("/")
Expand All @@ -43,9 +41,16 @@ func CPU() {
}

func cgLookup(r fs.FS) (int, error) {
const usingDefault = "no CPU quota set, using default"
var gmp int
b, err := fs.ReadFile(r, "proc/self/cgroup")
if err != nil {
switch {
case err == nil:
case errors.Is(err, fs.ErrNotExist):
debugLog("cgroups seemingly not enabled")
infoLog(usingDefault)
return gmp, nil
default:
return gmp, err
}
var q, p uint64 = 0, 1
Expand All @@ -55,21 +60,22 @@ func cgLookup(r fs.FS) (int, error) {
sl := bytes.SplitN(s.Bytes(), []byte(":"), 3)
hid, ctls, pb := sl[0], sl[1], sl[2]
if bytes.Equal(hid, []byte("0")) && len(ctls) == 0 { // If cgroupsv2:
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("found cgroups v2")
})
debugLog("found cgroups v2")
n := path.Join("sys/fs/cgroup", string(pb), "cpu.max")
b, err := fs.ReadFile(r, n)
if err != nil {
switch {
case err == nil:
case errors.Is(err, fs.ErrNotExist):
infoLog(usingDefault)
return gmp, nil
default:
return gmp, err
}
l := bytes.Fields(b)
qt, per := string(l[0]), string(l[1])
if qt == "max" {
// No quota, so bail.
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).Msg("no CPU quota set, using default")
})
infoLog(usingDefault)
return gmp, nil
}
q, err = strconv.ParseUint(qt, 10, 64)
Expand All @@ -94,19 +100,15 @@ func cgLookup(r fs.FS) (int, error) {
// This line is not the cpu group.
continue
}
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("found cgroups v1 and cpu controller")
})
debugLog("found cgroups v1 and cpu controller")
prefix := path.Join("sys/fs/cgroup", string(ctls), string(pb))
// Check for the existence of the named cgroup. If it doesn't exist,
// look at the root of the controller. The named group not existing
// probably means the process is in a container and is having remounting
// tricks done. If, for some reason this is actually the root cgroup,
// it'll be unlimited and fall back to the default.
if _, err := fs.Stat(r, prefix); errors.Is(err, fs.ErrNotExist) {
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("falling back to root hierarchy")
})
debugLog("falling back to root hierarchy")
prefix = path.Join("sys/fs/cgroup", string(ctls))
}

Expand All @@ -120,9 +122,7 @@ func cgLookup(r fs.FS) (int, error) {
}
if qi == -1 {
// No quota, so bail.
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).Msg("no CPU quota set, using default")
})
infoLog(usingDefault)
return gmp, nil
}
q = uint64(qi)
Expand Down
38 changes: 18 additions & 20 deletions initialize/auto/memory_linux.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
//go:build go1.19

package auto

import (
Expand Down Expand Up @@ -38,15 +36,10 @@ func Memory() {
})
return
case lim == doNothing:
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).
Msg("no memory limit configured")
})
infoLog("no memory limit configured")
return
case lim == setMax:
msgs = append(msgs, func(ctx context.Context) {
zlog.Info(ctx).Msg("memory limit unset")
})
infoLog("memory limit unset")
return
}
// Following the GC guide and taking a haircut: https://tip.golang.org/doc/gc-guide#Suggested_uses
Expand All @@ -67,7 +60,12 @@ const (

func memLookup(r fs.FS) (int64, error) {
b, err := fs.ReadFile(r, "proc/self/cgroup")
if err != nil {
switch {
case err == nil:
case errors.Is(err, fs.ErrNotExist):
debugLog("cgroups seemingly not enabled")
return doNothing, nil
default:
return 0, err
}
s := bufio.NewScanner(bytes.NewReader(b))
Expand All @@ -76,14 +74,13 @@ func memLookup(r fs.FS) (int64, error) {
sl := bytes.SplitN(s.Bytes(), []byte(":"), 3)
hid, ctls, pb := sl[0], sl[1], sl[2]
if bytes.Equal(hid, []byte("0")) && len(ctls) == 0 { // If cgroupsv2:
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("found cgroups v2")
})
debugLog("found cgroups v2")
n := path.Join("sys/fs/cgroup", string(pb), "memory.max")
b, err := fs.ReadFile(r, n)
switch {
case errors.Is(err, nil):
case errors.Is(err, fs.ErrNotExist):
debugLog(`no "memory.max" file`)
return doNothing, nil
default:
return 0, err
Expand All @@ -105,24 +102,25 @@ func memLookup(r fs.FS) (int64, error) {
if !isMem { // This line is not the memory group.
continue
}
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("found cgroups v1 and memory controller")
})
debugLog("found cgroups v1 and memory controller")
prefix := path.Join("sys/fs/cgroup", string(ctls), string(pb))
// Check for the existence of the named cgroup. If it doesn't exist,
// look at the root of the controller. The named group not existing
// probably means the process is in a container and is having remounting
// tricks done. If, for some reason this is actually the root cgroup,
// it'll be unlimited and fall back to the default.
if _, err := fs.Stat(r, prefix); errors.Is(err, fs.ErrNotExist) {
msgs = append(msgs, func(ctx context.Context) {
zlog.Debug(ctx).Msg("falling back to root hierarchy")
})
debugLog("falling back to root hierarchy")
prefix = path.Join("sys/fs/cgroup", string(ctls))
}

b, err = fs.ReadFile(r, path.Join(prefix, "memory.limit_in_bytes"))
if err != nil {
switch {
case errors.Is(err, nil):
case errors.Is(err, fs.ErrNotExist):
debugLog(`no "memory.limit_in_bytes" file`)
return doNothing, nil
default:
return 0, err
}
v := string(bytes.TrimSpace(b))
Expand Down

0 comments on commit 07b0ea7

Please sign in to comment.