Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion internal/collector/memory.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,18 @@ func (c *MemoryCollector) poll() error {
}
}

var swapUsed uint64
if swapTotal > swapFree {
swapUsed = swapTotal - swapFree
}
c.snap = MemorySnapshot{
TotalBytes: total,
UsedBytes: used,
UsedPct: usedPct,
GrowthRateBytesPerSec: growth,
AvailableBytes: available,
SwapTotalBytes: swapTotal,
SwapUsedBytes: swapTotal - swapFree,
SwapUsedBytes: swapUsed,
}
c.prev = memSample{used: used, at: now}
c.have = true
Expand Down
153 changes: 119 additions & 34 deletions internal/collector/memory_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,45 +58,130 @@ func TestParseMeminfoLine(t *testing.T) {
{"BadValue: not_a_number kB", "", 0, false},
}
for _, c := range cases {
k, v, ok := parseMeminfoLine(c.in)
if ok != c.want {
t.Errorf("parseMeminfoLine(%q) ok=%v, want %v", c.in, ok, c.want)
}
if ok && (k != c.key || v != c.val) {
t.Errorf("parseMeminfoLine(%q) = (%q, %d), want (%q, %d)", c.in, k, v, c.key, c.val)
}
t.Run(c.in, func(t *testing.T) {
k, v, ok := parseMeminfoLine(c.in)
if ok != c.want {
t.Errorf("parseMeminfoLine(%q) ok=%v, want %v", c.in, ok, c.want)
}
if ok && (k != c.key || v != c.val) {
t.Errorf("parseMeminfoLine(%q) = (%q, %d), want (%q, %d)", c.in, k, v, c.key, c.val)
}
})
}
}

func TestMemoryCollectorPoll(t *testing.T) {
path := writeMeminfo(t, 16<<30, 8<<30, 4<<30, 2<<30) // 16GB total, 8GB avail

c := NewMemoryCollector(newSilentLogger(), 50*time.Millisecond)
c.procPath = path

if err := c.poll(); err != nil {
t.Fatalf("poll: %v", err)
}

snap, ok := c.Snapshot().(*MemorySnapshot)
if !ok || snap == nil {
t.Fatal("expected non-nil snapshot")
}
if snap.TotalBytes != 16<<30 {
t.Errorf("TotalBytes = %d, want %d", snap.TotalBytes, uint64(16<<30))
}
if snap.UsedBytes != 8<<30 {
t.Errorf("UsedBytes = %d, want %d", snap.UsedBytes, uint64(8<<30))
}
if snap.AvailableBytes != 8<<30 {
t.Errorf("AvailableBytes = %d, want %d", snap.AvailableBytes, uint64(8<<30))
}
if snap.SwapUsedBytes != 2<<30 {
t.Errorf("SwapUsedBytes = %d, want %d", snap.SwapUsedBytes, uint64(2<<30))
cases := []struct {
name string
total uint64
available uint64
swapTotal uint64
swapFree uint64
wantTotal uint64
wantUsed uint64
wantAvail uint64
wantSwapUsed uint64
wantPctLo float64
wantPctHi float64
wantErr bool
}{
{
name: "half used with swap",
total: 16 << 30,
available: 8 << 30,
swapTotal: 4 << 30,
swapFree: 2 << 30,
wantTotal: 16 << 30,
wantUsed: 8 << 30,
wantAvail: 8 << 30,
wantSwapUsed: 2 << 30,
wantPctLo: 49.9,
wantPctHi: 50.1,
},
{
name: "no swap configured",
total: 8 << 30,
available: 4 << 30,
wantTotal: 8 << 30,
wantUsed: 4 << 30,
wantAvail: 4 << 30,
wantPctLo: 49.9,
wantPctHi: 50.1,
},
{
name: "swapfree exceeds swaptotal, guard clamps to zero",
total: 4 << 30,
available: 2 << 30,
swapTotal: 1 << 30,
swapFree: 2 << 30,
wantTotal: 4 << 30,
wantUsed: 2 << 30,
wantAvail: 2 << 30,
wantPctLo: 49.9,
wantPctHi: 50.1,
},
{
name: "fully used memory",
total: 4 << 30,
available: 0,
wantTotal: 4 << 30,
wantUsed: 4 << 30,
wantPctLo: 99.9,
wantPctHi: 100.1,
},
{
name: "exactly at 75 percent used",
total: 4 << 30,
available: 1 << 30,
wantTotal: 4 << 30,
wantUsed: 3 << 30,
wantAvail: 1 << 30,
wantPctLo: 74.9,
wantPctHi: 75.1,
},
{
name: "zero total returns error",
total: 0,
wantErr: true,
},
}
// Used pct should be ~50%.
if snap.UsedPct < 49.0 || snap.UsedPct > 51.0 {
t.Errorf("UsedPct = %v, want ~50", snap.UsedPct)
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
path := writeMeminfo(t, c.total, c.available, c.swapTotal, c.swapFree)
col := NewMemoryCollector(newSilentLogger(), 50*time.Millisecond)
col.procPath = path

err := col.poll()
if c.wantErr {
if err == nil {
t.Fatal("expected poll error, got nil")
}
return
}
if err != nil {
t.Fatalf("poll: %v", err)
}

snap, ok := col.Snapshot().(*MemorySnapshot)
if !ok || snap == nil {
t.Fatal("expected non-nil snapshot")
}
if snap.TotalBytes != c.wantTotal {
t.Errorf("TotalBytes = %d, want %d", snap.TotalBytes, c.wantTotal)
}
if snap.UsedBytes != c.wantUsed {
t.Errorf("UsedBytes = %d, want %d", snap.UsedBytes, c.wantUsed)
}
if snap.AvailableBytes != c.wantAvail {
t.Errorf("AvailableBytes = %d, want %d", snap.AvailableBytes, c.wantAvail)
}
if snap.SwapUsedBytes != c.wantSwapUsed {
t.Errorf("SwapUsedBytes = %d, want %d", snap.SwapUsedBytes, c.wantSwapUsed)
}
if snap.UsedPct < c.wantPctLo || snap.UsedPct > c.wantPctHi {
t.Errorf("UsedPct = %v, want [%v, %v]", snap.UsedPct, c.wantPctLo, c.wantPctHi)
}
})
}
}

Expand Down
Loading
Loading