|
| 1 | +package memlimit |
| 2 | + |
| 3 | +import ( |
| 4 | + "math" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +// limitFromFS returns the tightest memory limit enforced on this process, or 0 |
| 12 | +// when no per-container cap is readable. |
| 13 | +// |
| 14 | +// Under a private cgroup namespace (the Kubernetes default on cgroup v2) the |
| 15 | +// process sits at "/" and the mount root carries its limit. Under a host |
| 16 | +// namespace or a systemd scope it sits in a nested path whose root has no |
| 17 | +// limit at all, so the walk starts at the process's own cgroup and takes the |
| 18 | +// minimum of every finite limit up to the root — an ancestor's cap is enforced |
| 19 | +// just the same. A value above the machine's MemTotal is the host limit |
| 20 | +// leaking through, not a cap; equal to MemTotal is a pod sized to the whole |
| 21 | +// node and is kept. |
| 22 | +func limitFromFS(mount, selfCgroup string, memTotal int64) int64 { |
| 23 | + v2Path, v1Path := parseSelfCgroup(selfCgroup) |
| 24 | + |
| 25 | + limit := int64(math.MaxInt64) |
| 26 | + if v2Path != "" { |
| 27 | + limit = minAlongPath(filepath.Join(mount, v2Path), mount, "memory.max", parseV2Limit, limit) |
| 28 | + } |
| 29 | + if v1Path != "" { |
| 30 | + limit = minAlongPath(filepath.Join(mount, "memory", v1Path), filepath.Join(mount, "memory"), "memory.limit_in_bytes", parseV1Limit, limit) |
| 31 | + } |
| 32 | + if limit == math.MaxInt64 { |
| 33 | + return 0 |
| 34 | + } |
| 35 | + if memTotal > 0 && limit > memTotal { |
| 36 | + return 0 |
| 37 | + } |
| 38 | + return limit |
| 39 | +} |
| 40 | + |
| 41 | +// parseSelfCgroup returns the process's cgroup v2 path (the "0::" entry) and its |
| 42 | +// v1 memory-controller path, either of which may be empty. |
| 43 | +func parseSelfCgroup(content string) (v2, v1 string) { |
| 44 | + for _, line := range strings.Split(content, "\n") { |
| 45 | + parts := strings.SplitN(line, ":", 3) |
| 46 | + if len(parts) != 3 { |
| 47 | + continue |
| 48 | + } |
| 49 | + switch { |
| 50 | + case parts[0] == "0" && parts[1] == "": |
| 51 | + v2 = parts[2] |
| 52 | + case containsController(parts[1], "memory"): |
| 53 | + v1 = parts[2] |
| 54 | + } |
| 55 | + } |
| 56 | + return v2, v1 |
| 57 | +} |
| 58 | + |
| 59 | +func containsController(list, name string) bool { |
| 60 | + for _, c := range strings.Split(list, ",") { |
| 61 | + if c == name { |
| 62 | + return true |
| 63 | + } |
| 64 | + } |
| 65 | + return false |
| 66 | +} |
| 67 | + |
| 68 | +// minAlongPath reads file at every directory from start up to and including |
| 69 | +// stop, folding each finite value into limit. |
| 70 | +func minAlongPath(start, stop, file string, parse func(string) (int64, bool), limit int64) int64 { |
| 71 | + start, stop = filepath.Clean(start), filepath.Clean(stop) |
| 72 | + if !strings.HasPrefix(start, stop) { |
| 73 | + return limit |
| 74 | + } |
| 75 | + for dir := start; ; dir = filepath.Dir(dir) { |
| 76 | + if b, err := os.ReadFile(filepath.Join(dir, file)); err == nil { |
| 77 | + if v, ok := parse(strings.TrimSpace(string(b))); ok && v < limit { |
| 78 | + limit = v |
| 79 | + } |
| 80 | + } |
| 81 | + if dir == stop || dir == filepath.Dir(dir) { |
| 82 | + return limit |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func parseV2Limit(s string) (int64, bool) { |
| 88 | + if s == "max" { |
| 89 | + return 0, false |
| 90 | + } |
| 91 | + v, err := strconv.ParseInt(s, 10, 64) |
| 92 | + return v, err == nil && v > 0 |
| 93 | +} |
| 94 | + |
| 95 | +// cgroup v1 reports "unlimited" as a near-MaxInt64 page-rounded sentinel. |
| 96 | +func parseV1Limit(s string) (int64, bool) { |
| 97 | + v, err := strconv.ParseInt(s, 10, 64) |
| 98 | + return v, err == nil && v > 0 && v < int64(1)<<62 |
| 99 | +} |
| 100 | + |
| 101 | +func memTotalBytes() int64 { |
| 102 | + b, err := os.ReadFile("/proc/meminfo") |
| 103 | + if err != nil { |
| 104 | + return 0 |
| 105 | + } |
| 106 | + for _, line := range strings.Split(string(b), "\n") { |
| 107 | + if !strings.HasPrefix(line, "MemTotal:") { |
| 108 | + continue |
| 109 | + } |
| 110 | + fields := strings.Fields(line) |
| 111 | + if len(fields) >= 2 { |
| 112 | + if kb, err := strconv.ParseInt(fields[1], 10, 64); err == nil { |
| 113 | + return kb * 1024 |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + return 0 |
| 118 | +} |
0 commit comments