Skip to content

fix(xsysinfo): container-aware total RAM detection (cgroup/lxcfs) (#8059)#10288

Merged
mudler merged 1 commit into
masterfrom
fix/8059-cgroup-aware-ram
Jun 13, 2026
Merged

fix(xsysinfo): container-aware total RAM detection (cgroup/lxcfs) (#8059)#10288
mudler merged 1 commit into
masterfrom
fix/8059-cgroup-aware-ram

Conversation

@localai-bot

Copy link
Copy Markdown
Collaborator

Closes #8059

Problem

In an LXD/lxcfs container LocalAI reported ~92% RAM usage on an idle box. pkg/xsysinfo/memory.go took Total from memory.TotalMemory() (the host syscall.Sysinfo().Totalram, which lxcfs does not virtualize) while Available came from /proc/meminfo MemAvailable (which lxcfs does virtualize). Mixing host-total with container-available inflated Used = host - container.

Fix

Container/cgroup-aware total in GetSystemRAMInfo, extracted into a pure, unit-tested function:

func chooseTotalMemory(cgroupV2Max, cgroupV1Limit, procMemInfo string, sysinfoTotal uint64) uint64

It returns the minimum of the non-zero, non-unlimited candidates: cgroup v2 memory.max, cgroup v1 memory.limit_in_bytes (kernel unlimited sentinel ignored), /proc/meminfo MemTotal (the lxcfs case), and the Sysinfo total. Both cgroup paths are read best-effort (missing file -> skipped), so no v1/v2 probing is needed. Used is clamped against available > total to avoid unsigned underflow now that the two can come from different sources.

Test plan

  • New 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; scoped golangci-lint --new-from-merge-base clean.

Assisted-by: claude:claude-opus-4-8 [Claude Code]

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>
Comment thread pkg/xsysinfo/memory.go Dismissed
@mudler mudler merged commit f0e001b into master Jun 13, 2026
59 checks passed
@mudler mudler deleted the fix/8059-cgroup-aware-ram branch June 13, 2026 16:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

System RAM wrongly reported in LXD container

3 participants