fix(xsysinfo): container-aware total RAM detection (cgroup/lxcfs) (#8059)#10288
Merged
Conversation
GetSystemRAMInfo derived Total from memory.TotalMemory(), which on Linux uses syscall.Sysinfo().Totalram - the HOST kernel total. lxcfs/LXD does NOT virtualize that value, while MemAvailable (used for Free/Available) IS virtualized. Inside an LXD/container with a 128Gi host but a ~10Gi container view this produced Total=128Gi, Available=10Gi => Used=118Gi, reporting ~92% RAM usage on an idle container. Derive Total instead from the minimum of all non-zero, non-unlimited candidates: cgroup v2 memory.max, cgroup v1 memory.limit_in_bytes (the kernel unlimited sentinel is ignored), /proc/meminfo MemTotal (which lxcfs virtualizes), and the syscall.Sysinfo total as the bare-metal fallback. On bare metal every candidate is unlimited or equals the host total, so behavior is unchanged. The selection/parsing lives in a pure function chooseTotalMemory(...) taking file CONTENTS, unit-tested without a real LXD host; OS file reads stay in a thin wrapper. Assisted-by: claude:claude-opus-4-8 [Claude Code] Signed-off-by: Ettore Di Giacinto <mudler@localai.io>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Closes #8059
Problem
In an LXD/lxcfs container LocalAI reported ~92% RAM usage on an idle box.
pkg/xsysinfo/memory.gotook Total frommemory.TotalMemory()(the hostsyscall.Sysinfo().Totalram, which lxcfs does not virtualize) while Available came from/proc/meminfo MemAvailable(which lxcfs does virtualize). Mixing host-total with container-available inflatedUsed = host - container.Fix
Container/cgroup-aware total in
GetSystemRAMInfo, extracted into a pure, unit-tested function:It returns the minimum of the non-zero, non-unlimited candidates: cgroup v2
memory.max, cgroup v1memory.limit_in_bytes(kernel unlimited sentinel ignored),/proc/meminfo MemTotal(the lxcfs case), and theSysinfototal. Both cgroup paths are read best-effort (missing file -> skipped), so no v1/v2 probing is needed.Usedis clamped againstavailable > totalto avoid unsigned underflow now that the two can come from different sources.Test plan
pkg/xsysinfo/memory_total_test.go(Ginkgo): bare-metal (no cap) -> sysinfo total; LXD (MemTotal < sysinfo) -> MemTotal; cgroup v2 cap -> cap; cgroup v1 sentinel -> ignored. Red -> green.go test ./pkg/xsysinfo/...green; scopedgolangci-lint --new-from-merge-baseclean.Assisted-by: claude:claude-opus-4-8 [Claude Code]