-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
fix(xsysinfo): container-aware total RAM detection (cgroup/lxcfs) (#8059) #10288
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package xsysinfo | ||
|
|
||
| import ( | ||
| "strconv" | ||
| "strings" | ||
| ) | ||
|
|
||
| // cgroupV1UnlimitedSentinel is the value the kernel writes to | ||
| // memory.limit_in_bytes when no limit is set. It is PAGE_COUNTER_MAX | ||
| // (LONG_MAX rounded down to a page boundary), i.e. 0x7FFFFFFFFFFFF000 on | ||
| // 4 KiB-page systems. Any value at or above this is treated as "no limit". | ||
| const cgroupV1UnlimitedSentinel = uint64(0x7FFFFFFFFFFFF000) | ||
|
|
||
| // parseUintField parses a trimmed unsigned integer from raw file contents. | ||
| // It returns (0, false) when the content is empty or not a number. | ||
| func parseUintField(raw string) (uint64, bool) { | ||
| s := strings.TrimSpace(raw) | ||
| if s == "" { | ||
| return 0, false | ||
| } | ||
| v, err := strconv.ParseUint(s, 10, 64) | ||
| if err != nil { | ||
| return 0, false | ||
| } | ||
| return v, true | ||
| } | ||
|
|
||
| // parseCgroupV2Max interprets the contents of cgroup v2 memory.max. | ||
| // The literal "max" means unlimited, returning 0. | ||
| func parseCgroupV2Max(raw string) uint64 { | ||
| if strings.TrimSpace(raw) == "max" { | ||
| return 0 | ||
| } | ||
| v, ok := parseUintField(raw) | ||
| if !ok { | ||
| return 0 | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| // parseCgroupV1Limit interprets the contents of cgroup v1 | ||
| // memory.limit_in_bytes. The kernel's "unlimited" sentinel (a value at or | ||
| // above PAGE_COUNTER_MAX) is treated as no limit, returning 0. | ||
| func parseCgroupV1Limit(raw string) uint64 { | ||
| v, ok := parseUintField(raw) | ||
| if !ok { | ||
| return 0 | ||
| } | ||
| if v >= cgroupV1UnlimitedSentinel { | ||
| return 0 | ||
| } | ||
| return v | ||
| } | ||
|
|
||
| // parseMemTotal extracts the MemTotal value (in bytes) from raw | ||
| // /proc/meminfo contents. MemTotal is reported in kibibytes, so the parsed | ||
| // value is multiplied by 1024. Returns 0 when the field is missing. | ||
| func parseMemTotal(raw string) uint64 { | ||
| for _, line := range strings.Split(raw, "\n") { | ||
| if !strings.HasPrefix(line, "MemTotal:") { | ||
| continue | ||
| } | ||
| fields := strings.Fields(line) | ||
| // Expected: ["MemTotal:", "<value>", "kB"] | ||
| if len(fields) < 2 { | ||
| return 0 | ||
| } | ||
| v, err := strconv.ParseUint(fields[1], 10, 64) | ||
| if err != nil { | ||
| return 0 | ||
| } | ||
| if len(fields) >= 3 { | ||
| switch strings.ToLower(fields[2]) { | ||
| case "kb": | ||
| return v * 1024 | ||
| case "mb": | ||
| return v * 1024 * 1024 | ||
| case "gb": | ||
| return v * 1024 * 1024 * 1024 | ||
| } | ||
| } | ||
| return v | ||
| } | ||
| return 0 | ||
| } | ||
|
|
||
| // chooseTotalMemory selects the most accurate system RAM total in bytes. | ||
| // | ||
| // On Linux the host kernel total (sysinfoTotal, from syscall.Sysinfo) is NOT | ||
| // virtualized by lxcfs/LXD, so inside a container it over-reports physical | ||
| // RAM. The cgroup limits and /proc/meminfo MemTotal, by contrast, do reflect | ||
| // the container's view. We therefore take the MINIMUM of all non-zero, | ||
| // non-unlimited candidates: | ||
| // | ||
| // - cgroup v2 memory.max ("max" => unlimited, skipped) | ||
| // - cgroup v1 memory.limit_in_bytes (kernel sentinel => unlimited, skipped) | ||
| // - /proc/meminfo MemTotal (lxcfs/LXD virtualizes this) | ||
| // - sysinfoTotal (bare-metal fallback) | ||
| // | ||
| // On bare metal the cgroup limits are unlimited and MemTotal == sysinfoTotal, | ||
| // so the result equals the host total exactly as before. | ||
| func chooseTotalMemory(cgroupV2Max, cgroupV1Limit, procMemInfo string, sysinfoTotal uint64) uint64 { | ||
| candidates := []uint64{ | ||
| parseCgroupV2Max(cgroupV2Max), | ||
| parseCgroupV1Limit(cgroupV1Limit), | ||
| parseMemTotal(procMemInfo), | ||
| sysinfoTotal, | ||
| } | ||
|
|
||
| var best uint64 | ||
| for _, c := range candidates { | ||
| if c == 0 { | ||
| continue | ||
| } | ||
| if best == 0 || c < best { | ||
| best = c | ||
| } | ||
| } | ||
| return best | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package xsysinfo | ||
|
|
||
| import ( | ||
| . "github.com/onsi/ginkgo/v2" | ||
| . "github.com/onsi/gomega" | ||
| ) | ||
|
|
||
| var _ = Describe("chooseTotalMemory", func() { | ||
| const ( | ||
| gi128 = uint64(128) * 1024 * 1024 * 1024 | ||
| gi20 = uint64(20) * 1024 * 1024 * 1024 | ||
| gi10 = uint64(10) * 1024 * 1024 * 1024 | ||
| ) | ||
|
|
||
| // /proc/meminfo MemTotal is in kB; build a snippet for a given byte total. | ||
| memInfo := func(bytes uint64) string { | ||
| kb := bytes / 1024 | ||
| return "MemTotal: " + itoa(kb) + " kB\nMemFree: 123 kB\n" | ||
| } | ||
|
|
||
| Context("bare metal (no cgroup cap, memory.max == max)", func() { | ||
| It("uses the host sysinfo total", func() { | ||
| // MemTotal mirrors sysinfo on bare metal. | ||
| got := chooseTotalMemory("max\n", string(rune(0)), memInfo(gi128), gi128) | ||
| Expect(got).To(Equal(gi128)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("LXD/lxcfs container (MemTotal virtualized below host, no cap)", func() { | ||
| It("uses the virtualized MemTotal, not the host sysinfo total", func() { | ||
| // This is issue #8059: host sysinfo says 128Gi, but lxcfs | ||
| // virtualizes /proc/meminfo MemTotal to 20Gi and there is no | ||
| // cgroup cap. The corrected total must be 20Gi. | ||
| got := chooseTotalMemory("max\n", "", memInfo(gi20), gi128) | ||
| Expect(got).To(Equal(gi20)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("cgroup v2 cap set below MemTotal", func() { | ||
| It("uses the cgroup cap", func() { | ||
| got := chooseTotalMemory(itoa(gi10)+"\n", "", memInfo(gi20), gi128) | ||
| Expect(got).To(Equal(gi10)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("cgroup v1 with the kernel unlimited sentinel", func() { | ||
| It("ignores the sentinel and falls back to MemTotal", func() { | ||
| got := chooseTotalMemory("", "9223372036854771712\n", memInfo(gi20), gi128) | ||
| Expect(got).To(Equal(gi20)) | ||
| }) | ||
| }) | ||
|
|
||
| Context("all candidates empty/unlimited", func() { | ||
| It("falls back to sysinfo total", func() { | ||
| got := chooseTotalMemory("max\n", "", "", gi128) | ||
| Expect(got).To(Equal(gi128)) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| // itoa is a tiny base-10 formatter to avoid importing strconv into the test. | ||
| func itoa(v uint64) string { | ||
| if v == 0 { | ||
| return "0" | ||
| } | ||
| var buf [20]byte | ||
| i := len(buf) | ||
| for v > 0 { | ||
| i-- | ||
| buf[i] = byte('0' + v%10) | ||
| v /= 10 | ||
| } | ||
| return string(buf[i:]) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.