From 96f2eb01a410ef7506fe97e5276145fedaa556c0 Mon Sep 17 00:00:00 2001 From: EOIDC Date: Tue, 28 Nov 2017 18:01:52 +0800 Subject: [PATCH] Fix incorrect Mem.Used calculation (#82) --- CHANGELOG.md | 1 + sigar_linux_common.go | 2 +- sigar_linux_test.go | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0767ae14..1bddf6730 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed - OpenBSD returns `ErrNotImplemented` for `ProcTime.Get` instead of `nil`. #83 +- Fixed incorrect `Mem.Used` calculation under linux. #82 - Fixed `ProcState` on Linux and FreeBSD when process names contain parentheses. #81 ### Deprecated diff --git a/sigar_linux_common.go b/sigar_linux_common.go index 9b16d4c1b..8e5e7856f 100644 --- a/sigar_linux_common.go +++ b/sigar_linux_common.go @@ -70,7 +70,7 @@ func (self *Mem) Get() error { self.ActualFree = self.Free + buffers + cached } - self.Used = self.Total - self.ActualFree + self.Used = self.Total - self.Free self.ActualUsed = self.Total - self.ActualFree return nil diff --git a/sigar_linux_test.go b/sigar_linux_test.go index a5febc976..c8ed03398 100644 --- a/sigar_linux_test.go +++ b/sigar_linux_test.go @@ -221,6 +221,9 @@ DirectMap2M: 333824 kB if assert.NoError(t, mem.Get()) { assert.Equal(t, uint64(374256*1024), mem.Total) assert.Equal(t, uint64(274460*1024), mem.Free) + assert.Equal(t, uint64(mem.Total-mem.Free), mem.Used) + assert.Equal(t, uint64((274460+9764+38648)*1024), mem.ActualFree) + assert.Equal(t, uint64(mem.Total-mem.ActualFree), mem.ActualUsed) } swap := sigar.Swap{} @@ -293,6 +296,8 @@ DirectMap2M: 460800 kB assert.Equal(t, uint64(500184*1024), mem.Total) assert.Equal(t, uint64(31360*1024), mem.Free) assert.Equal(t, uint64(414168*1024), mem.ActualFree) + assert.Equal(t, uint64(mem.Total-mem.Free), mem.Used) + assert.Equal(t, uint64(mem.Total-mem.ActualFree), mem.ActualUsed) } swap := sigar.Swap{}