Lightweight server monitoring with historical data, docker stats, and alerts.
fix(agent): don't subtract host ZFS ARC from memory in LXC (#2767)
Sven van Ginkela12fdb9f45e3
diff --git a/agent/memory.go b/agent/memory.goindex 80b0d7d09..2ab6f7169 100644--- a/agent/memory.go+++ b/agent/memory.go@@ -52,7 +52,7 @@ func (a *Agent) updateMemoryStats(stats *system.Stats) {used, cacheBuff, _ := calculateHostMemoryUsage(v, a.memCalc == "htop")metrics = memoryMetrics{Total: v.Total, Used: used, BuffCache: cacheBuff}// Host ARC must not be subtracted from container-scoped accounting.- if a.zfs {+ if a.zfs && canSubtractZfsArc() {if size, _ := zfs.ARCSize(); size > 0 && size < metrics.Used {arcSize = sizemetrics.Used -= arcSizediff --git a/agent/memory_linux.go b/agent/memory_linux.goindex 1df672f17..b61d47aed 100644--- a/agent/memory_linux.go+++ b/agent/memory_linux.go@@ -9,6 +9,13 @@ import ("github.com/henrygd/beszel/agent/utils")+// canSubtractZfsArc reports whether the host ZFS ARC may be subtracted from+// used memory. Inside LXC, /proc/spl/kstat/zfs/arcstats exposes the host's ARC,+// which is not part of the guest's memory (issue #2766).+func canSubtractZfsArc() bool {+ return !inLxc()+}+// containerMemoryMetrics is only used when USE_CGROUP is explicitly enabled.// Unlike /proc/stat, lxcfs already serves a container-scoped /proc/meminfo,// so LXC keeps the existing memory path by default.diff --git a/agent/memory_linux_test.go b/agent/memory_linux_test.goindex 93615af1c..e55e3a7d0 100644--- a/agent/memory_linux_test.go+++ b/agent/memory_linux_test.go@@ -125,6 +125,15 @@ func TestContainerMemoryScopeAndEnablement(t *testing.T) {assert.False(t, ok)}+func TestCanSubtractZfsArc(t *testing.T) {+ setupMemoryCgroup(t, true)+ inLxc = sync.OnceValue(detectLxc)+ assert.True(t, canSubtractZfsArc())+ markLxc(t)+ inLxc = sync.OnceValue(detectLxc)+ assert.False(t, canSubtractZfsArc()) // host ARC must not leak into LXC accounting+}+func TestUpdateMemoryStatsCgroupAndFallback(t *testing.T) {dir := setupMemoryCgroup(t, true)const gib = uint64(1 << 30)diff --git a/agent/memory_unsupported.go b/agent/memory_unsupported.goindex 21e320d68..c1297218a 100644--- a/agent/memory_unsupported.go+++ b/agent/memory_unsupported.go@@ -2,6 +2,8 @@package agent+func canSubtractZfsArc() bool { return true }+func containerMemoryMetrics(uint64, bool) (memoryMetrics, bool) {return memoryMetrics{}, false}