Files
seaweedfs/seaweed-block/cmd/blockvolume/memory_cgroup.go
T

128 lines
3.7 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"strings"
)
// cgroupMemoryLimitMiB reads the current unified cgroup and its ancestors:
// a parent's finite limit also constrains this holder. Zero means no v2 limit.
// Use slash paths even in fixture tests on non-Linux hosts.
func cgroupMemoryLimitMiB(readFile func(string) ([]byte, error)) (int64, error) {
data, err := readFile("/proc/self/cgroup")
if errors.Is(err, os.ErrNotExist) {
return 0, nil
}
if err != nil {
return 0, err
}
group := ""
for _, line := range strings.Split(string(data), "\n") {
if strings.HasPrefix(line, "0::") {
group = strings.TrimPrefix(line, "0::")
break
}
}
if group == "" {
return 0, nil
}
if !strings.HasPrefix(group, "/") {
return 0, fmt.Errorf("invalid unified cgroup path %q", group)
}
for _, part := range strings.Split(group, "/") {
if part == ".." {
return 0, fmt.Errorf("unified cgroup path escapes its mount: %q", group)
}
}
leaf, mount, err := cgroupMemoryDirectory(path.Clean(group), readFile)
if err != nil {
return 0, err
}
var limit int64
for dir := leaf; ; dir = path.Dir(dir) {
name := path.Join(dir, "memory.max")
value, err := readFile(name)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return 0, fmt.Errorf("%s: %w", name, err)
}
if err == nil && strings.TrimSpace(string(value)) != "max" {
n, err := strconv.ParseInt(strings.TrimSpace(string(value)), 10, 64)
if err != nil || n <= 0 {
return 0, fmt.Errorf("invalid %s value %q", name, value)
}
if limit == 0 || n < limit {
limit = n
}
}
if dir == mount {
break
}
}
if limit == 0 {
return 0, nil
}
// Divide before multiplying to avoid overflow near MaxInt64.
mib := (limit/10*9 + limit%10*9/10) >> 20
if mib == 0 {
return 0, fmt.Errorf("cgroup memory limit %d is too small for a MiB soft limit", limit)
}
return mib, nil
}
// mountinfo names both the filesystem subtree root and its visible mountpoint.
// A membership path must resolve inside that view; an unresolved v2 hierarchy
// is a configuration error, not evidence of unlimited memory.
func cgroupMemoryDirectory(group string, readFile func(string) ([]byte, error)) (string, string, error) {
data, err := readFile("/proc/self/mountinfo")
if err != nil {
return "", "", fmt.Errorf("read cgroup mounts: %w", err)
}
unescape := strings.NewReplacer(`\040`, " ", `\011`, "\t", `\012`, "\n", `\134`, `\`)
var leaf, mount, bestRoot string
for _, line := range strings.Split(string(data), "\n") {
left, right, ok := strings.Cut(line, " - ")
if !ok {
continue
}
fs := strings.Fields(right)
if len(fs) == 0 || fs[0] != "cgroup2" {
continue
}
fields := strings.Fields(left)
if len(fields) < 6 {
return "", "", fmt.Errorf("malformed cgroup2 mountinfo")
}
root, point := unescape.Replace(fields[3]), unescape.Replace(fields[4])
if !path.IsAbs(root) || !path.IsAbs(point) {
return "", "", fmt.Errorf("invalid cgroup2 mount paths")
}
unresolved := false
for _, part := range strings.Split(root+"/"+point, "/") {
if part == ".." {
unresolved = true
break
}
}
if unresolved {
continue
} // do not clean an out-of-namespace root into a different view
root, point = path.Clean(root), path.Clean(point)
if group != root && root != "/" && !strings.HasPrefix(group, root+"/") {
continue
}
// Prefer the widest visible ancestry when the hierarchy is mounted
// more than once, so a subtree bind mount does not hide a parent cap.
if mount == "" || len(root) < len(bestRoot) {
leaf, mount, bestRoot = path.Join(point, strings.TrimPrefix(group, root)), point, root
}
}
if mount == "" {
return "", "", fmt.Errorf("no cgroup2 mount resolves membership %q", group)
}
return leaf, mount, nil
}