Skip to content

Modern Docker Management, Designed for Everyone

fix: only display memory usage thats non-reclaimable (#3105)

Kyle Mendell06d5f54a7ed7
diff --git a/backend/api/ws/handler.go b/backend/api/ws/handler.go
index 1ac057ba2..aed738bb0 100644
--- a/backend/api/ws/handler.go
+++ b/backend/api/ws/handler.go
@@ -1163,7 +1163,14 @@ func (h *WebSocketHandler) getMemoryInfo() (uint64, uint64) {
if memInfo == nil {
return 0, 0
}
- return memInfo.Used, memInfo.Total
+ // gopsutil counts ZFS ARC as used memory (the kernel excludes it from
+ // MemAvailable). Treat the reclaimable portion as cache, matching
+ // btop/htop, so the dashboard does not over-report usage on ZFS hosts.
+ used := memInfo.Used
+ if arc := docker.ZFSARCReclaimable(); arc > 0 {
+ used -= min(used, arc)
+ }
+ return used, memInfo.Total
}
// applyCgroupLimits applies cgroup limits when running in an LXC (or similar)
diff --git a/backend/pkg/dockerutil/cgroup_utils.go b/backend/pkg/dockerutil/cgroup_utils.go
index eee826987..3d7810210 100644
--- a/backend/pkg/dockerutil/cgroup_utils.go
+++ b/backend/pkg/dockerutil/cgroup_utils.go
@@ -4,6 +4,7 @@ import (
"bufio"
"errors"
"fmt"
+ "io"
"log/slog"
"os"
"path/filepath"
@@ -99,6 +100,11 @@ func detectCgroupV2Limits(limits *CgroupLimits) (*CgroupLimits, error) {
}
if memUsage, err := readCgroupV2Int64("/sys/fs/cgroup/memory.current"); err == nil {
+ // memory.current includes reclaimable page cache; subtract inactive_file
+ // to match what `docker stats` reports as usage.
+ if inactiveFile, err := readMemoryStatValue("/sys/fs/cgroup/memory.stat", "inactive_file"); err == nil {
+ memUsage = max(memUsage-inactiveFile, 0)
+ }
limits.MemoryUsage = memUsage
}
@@ -138,6 +144,12 @@ func detectCgroupV1Limits(limits *CgroupLimits) (*CgroupLimits, error) {
memoryUsagePath := filepath.Join("/sys/fs/cgroup/memory", cgroupPath, "memory.usage_in_bytes")
if memUsage, err := readCgroupV1Int64(memoryUsagePath); err == nil {
+ // memory.usage_in_bytes includes reclaimable page cache; subtract
+ // total_inactive_file (from memory.stat) to match `docker stats`.
+ memoryStatPath := filepath.Join("/sys/fs/cgroup/memory", cgroupPath, "memory.stat")
+ if inactiveFile, err := readMemoryStatValue(memoryStatPath, "total_inactive_file"); err == nil {
+ memUsage = max(memUsage-inactiveFile, 0)
+ }
limits.MemoryUsage = memUsage
}
@@ -195,6 +207,30 @@ func readCgroupV1Int64(path string) (int64, error) {
return value, nil
}
+// readMemoryStatValue reads a single "key value" line from a cgroup memory.stat
+// file and returns the value for the given key.
+func readMemoryStatValue(path, key string) (int64, error) {
+ file, err := os.Open(path)
+ if err != nil {
+ return 0, err
+ }
+ defer func() { _ = file.Close() }()
+
+ scanner := bufio.NewScanner(file)
+ for scanner.Scan() {
+ fields := strings.Fields(scanner.Text())
+ if len(fields) < 2 || fields[0] != key {
+ continue
+ }
+ return strconv.ParseInt(fields[1], 10, 64)
+ }
+ if err := scanner.Err(); err != nil {
+ return 0, err
+ }
+
+ return 0, fmt.Errorf("key %q not found in %s", key, path)
+}
+
func readCgroupV2Int64(path string) (int64, error) {
data, err := os.ReadFile(path)
if err != nil {
@@ -393,3 +429,60 @@ func getContainerIDFromHostname() (string, error) {
return "", errors.New("hostname doesn't match expected container ID length")
}
+
+// ZFSARCReclaimable returns the number of bytes of ZFS ARC (Adaptive
+// Replacement Cache) that the kernel counts as used memory but that can be
+// reclaimed under memory pressure.
+//
+// The kernel accounts ZFS ARC as used and excludes it from /proc/meminfo's
+// MemAvailable, so gopsutil's Used (= Total - MemAvailable) counts the whole
+// ARC as used. Tools like btop/htop instead treat ARC as reclaimable cache. We
+// subtract the reclaimable portion so the dashboard does not over-report usage
+// on ZFS hosts.
+//
+// The arcstats file only exists when the ZFS kernel module is loaded, so the
+// open fails cheaply on non-ZFS systems (including macOS dev machines) and this
+// returns 0 there.
+func ZFSARCReclaimable() uint64 {
+ f, err := os.Open("/proc/spl/kstat/zfs/arcstats")
+ if err != nil {
+ return 0
+ }
+ defer func() { _ = f.Close() }()
+ return parseARCStats(f)
+}
+
+// parseARCStats parses the kstat-format arcstats content and returns the
+// reclaimable ARC size in bytes: max(size-c_min, 0). The ARC cannot shrink
+// below c_min, so only the portion above c_min is genuinely reclaimable — a
+// slightly more conservative choice than btop's full-size treatment, which is
+// the right call for a "used" metric.
+//
+// Each kstat row is "name type data"; the value is the third column.
+func parseARCStats(r io.Reader) uint64 {
+ var size, cMin uint64
+ scanner := bufio.NewScanner(r)
+ for scanner.Scan() {
+ fields := strings.Fields(scanner.Text())
+ if len(fields) < 3 {
+ continue
+ }
+ switch fields[0] {
+ case "size":
+ if v, err := strconv.ParseUint(fields[2], 10, 64); err == nil {
+ size = v
+ }
+ case "c_min":
+ if v, err := strconv.ParseUint(fields[2], 10, 64); err == nil {
+ cMin = v
+ }
+ }
+ }
+ if err := scanner.Err(); err != nil {
+ slog.Error("failed to parse ARC stats", "error", err)
+ }
+ if size <= cMin {
+ return 0
+ }
+ return size - cMin
+}
diff --git a/backend/pkg/dockerutil/cgroup_utils_test.go b/backend/pkg/dockerutil/cgroup_utils_test.go
new file mode 100644
index 000000000..dc577a60c
--- /dev/null
+++ b/backend/pkg/dockerutil/cgroup_utils_test.go
@@ -0,0 +1,64 @@
+package docker
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestReadMemoryStatValue(t *testing.T) {
+ // Trimmed cgroup memory.stat: flat "key value" lines.
+ const memoryStat = `anon 1048576
+file 2097152
+inactive_file 524288
+active_file 1572864
+total_inactive_file 786432
+`
+ path := filepath.Join(t.TempDir(), "memory.stat")
+ require.NoError(t, os.WriteFile(path, []byte(memoryStat), 0o600))
+
+ // Exact key match: inactive_file must not be shadowed by total_inactive_file.
+ value, err := readMemoryStatValue(path, "inactive_file")
+ require.NoError(t, err)
+ require.Equal(t, int64(524288), value)
+
+ value, err = readMemoryStatValue(path, "total_inactive_file")
+ require.NoError(t, err)
+ require.Equal(t, int64(786432), value)
+}
+
+func TestReadMemoryStatValue_MissingKey(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "memory.stat")
+ require.NoError(t, os.WriteFile(path, []byte("anon 1048576\n"), 0o600))
+
+ _, err := readMemoryStatValue(path, "inactive_file")
+ require.Error(t, err)
+}
+
+func TestParseARCStats(t *testing.T) {
+ // Abbreviated kstat dump: rows are "name type data".
+ const arcstats = `13 1 0x01 98 26656 5560875779 158279494432
+name type data
+hits 4 1234567
+misses 4 89012
+c_min 4 1073741824
+c_max 4 8589934592
+size 4 7516192768
+`
+ // size (7 GiB) - c_min (1 GiB) = 6 GiB reclaimable.
+ require.Equal(t, uint64(6442450944), parseARCStats(strings.NewReader(arcstats)))
+}
+
+func TestParseARCStats_ClampsAndHandlesMissing(t *testing.T) {
+ // No arcstats content (non-ZFS host): nothing reclaimable.
+ require.Equal(t, uint64(0), parseARCStats(strings.NewReader("")))
+
+ // ARC already at its floor (size <= c_min): clamped to 0, never underflows.
+ const atFloor = `size 4 1073741824
+c_min 4 1073741824
+`
+ require.Equal(t, uint64(0), parseARCStats(strings.NewReader(atFloor)))
+}