mirror of
https://github.com/henrygd/beszel.git
synced 2026-08-29 04:07:20 +00:00
fix(agent): count swap cache as used space (#2267)
SwapCached pages have been read back into memory but still occupy allocated swap slots. Subtracting them from SwapTotal - SwapFree underreported swap usage compared with free, Glances, and gopsutil's canonical SwapMemory metric.
This commit is contained in:
+2
-1
@@ -343,7 +343,8 @@ func calculateHostMemoryUsage(v *mem.VirtualMemoryStat, htop bool) (used, cacheB
|
||||
if htop {
|
||||
used = saturatingSub(v.Total, v.Free, cacheBuff)
|
||||
}
|
||||
return used, cacheBuff, saturatingSub(v.SwapTotal, v.SwapFree, v.SwapCached)
|
||||
// Cached swap pages still occupy swap slots and are included in `free`'s used value.
|
||||
return used, cacheBuff, saturatingSub(v.SwapTotal, v.SwapFree)
|
||||
}
|
||||
|
||||
// saturatingSub subtracts each value, returning zero on underflow.
|
||||
|
||||
@@ -47,14 +47,14 @@ func TestCalculateHostMemoryUsage(t *testing.T) {
|
||||
memory: mem.VirtualMemoryStat{Total: 100, Available: 40, Used: 60, Free: 20, Cached: 25, Buffers: 10, Shared: 5, SwapTotal: 20, SwapFree: 8, SwapCached: 2},
|
||||
used: 60,
|
||||
cacheBuff: 30,
|
||||
swapUsed: 10,
|
||||
swapUsed: 12,
|
||||
},
|
||||
{
|
||||
name: "inconsistent counters saturate",
|
||||
memory: mem.VirtualMemoryStat{Total: 100, Available: 110, Used: ^uint64(0) - 9, Free: 90, Cached: 5, Buffers: 10, Shared: 20, SwapTotal: 10, SwapFree: 9, SwapCached: 2},
|
||||
used: 0,
|
||||
cacheBuff: 0,
|
||||
swapUsed: 0,
|
||||
swapUsed: 1,
|
||||
},
|
||||
{
|
||||
name: "htop subtraction saturates",
|
||||
@@ -62,7 +62,7 @@ func TestCalculateHostMemoryUsage(t *testing.T) {
|
||||
htop: true,
|
||||
used: 0,
|
||||
cacheBuff: 25,
|
||||
swapUsed: 15,
|
||||
swapUsed: 20,
|
||||
},
|
||||
{
|
||||
name: "zero cache from shared cancellation does not fall back",
|
||||
|
||||
Reference in New Issue
Block a user