From 3695635d09719849c39b7714540762c2d3e015ea Mon Sep 17 00:00:00 2001 From: Xuewei Zhang Date: Thu, 30 Jan 2020 17:40:54 -0800 Subject: [PATCH 1/2] Collect Active(anon), Inactive(anon) and Unevictable in /proc/meminfo These fields helps people understand anonymous memory usage pattern. --- mem/mem_linux.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mem/mem_linux.go b/mem/mem_linux.go index 41355ba..82d1259 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -16,6 +16,9 @@ import ( type VirtualMemoryExStat struct { ActiveFile uint64 `json:"activefile"` InactiveFile uint64 `json:"inactivefile"` + ActiveAnon uint64 `json:"activeanon"` + InactiveAnon uint64 `json:"inactiveanon"` + Unevictable uint64 `json:"unevictable"` } func VirtualMemory() (*VirtualMemoryStat, error) { @@ -64,12 +67,18 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { ret.Active = t * 1024 case "Inactive": ret.Inactive = t * 1024 + case "Active(anon)": + retEx.ActiveAnon = t * 1024 + case "Inactive(anon)": + retEx.InactiveAnon = t * 1024 case "Active(file)": activeFile = true retEx.ActiveFile = t * 1024 case "Inactive(file)": inactiveFile = true retEx.InactiveFile = t * 1024 + case "Unevictable": + retEx.Unevictable = t * 1024 case "Writeback": ret.Writeback = t * 1024 case "WritebackTmp": From 929068ccd5538acbeca64d300458211617480121 Mon Sep 17 00:00:00 2001 From: Xuewei Zhang Date: Thu, 30 Jan 2020 18:46:47 -0800 Subject: [PATCH 2/2] mem: Add VirtualMemoryEx() and VirtualMemoryExWithContext() --- mem/mem_linux.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/mem/mem_linux.go b/mem/mem_linux.go index 82d1259..66ccca9 100644 --- a/mem/mem_linux.go +++ b/mem/mem_linux.go @@ -4,6 +4,7 @@ package mem import ( "context" + "encoding/json" "math" "os" "strconv" @@ -21,11 +22,36 @@ type VirtualMemoryExStat struct { Unevictable uint64 `json:"unevictable"` } +func (v VirtualMemoryExStat) String() string { + s, _ := json.Marshal(v) + return string(s) +} + func VirtualMemory() (*VirtualMemoryStat, error) { return VirtualMemoryWithContext(context.Background()) } func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { + vm, _, err := fillFromMeminfoWithContext(ctx) + if err != nil { + return nil, err + } + return vm, nil +} + +func VirtualMemoryEx() (*VirtualMemoryExStat, error) { + return VirtualMemoryExWithContext(context.Background()) +} + +func VirtualMemoryExWithContext(ctx context.Context) (*VirtualMemoryExStat, error) { + _, vmEx, err := fillFromMeminfoWithContext(ctx) + if err != nil { + return nil, err + } + return vmEx, nil +} + +func fillFromMeminfoWithContext(ctx context.Context) (*VirtualMemoryStat, *VirtualMemoryExStat, error) { filename := common.HostProc("meminfo") lines, _ := common.ReadLines(filename) @@ -49,7 +75,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { t, err := strconv.ParseUint(value, 10, 64) if err != nil { - return ret, err + return ret, retEx,err } switch key { case "MemTotal": @@ -144,7 +170,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) { ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0 - return ret, nil + return ret, retEx, nil } func SwapMemory() (*SwapMemoryStat, error) {