Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manual available memory calculation #626

Merged
merged 6 commits into from
Mar 1, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 2 additions & 0 deletions mem/mem.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type VirtualMemoryStat struct {
// https://www.centos.org/docs/5/html/5.1/Deployment_Guide/s2-proc-meminfo.html
// https://www.kernel.org/doc/Documentation/filesystems/proc.txt
// https://www.kernel.org/doc/Documentation/vm/overcommit-accounting
ActiveFile uint64 `json:"activefile"`
InactiveFile uint64 `json:"inactivefile"`
mznet marked this conversation as resolved.
Show resolved Hide resolved
Buffers uint64 `json:"buffers"`
Cached uint64 `json:"cached"`
Writeback uint64 `json:"writeback"`
Expand Down
66 changes: 65 additions & 1 deletion mem/mem_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package mem

import (
"context"
"math"
"os"
"strconv"
"strings"

Expand All @@ -18,8 +20,12 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
filename := common.HostProc("meminfo")
lines, _ := common.ReadLines(filename)

// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
memavail := false
activeFile := false // "Active(file)" not available: 2.6.28 / Dec 2008
inactiveFile := false // "Inactive(file)" not available: 2.6.28 / Dec 2008
sReclaimable := false // "SReclaimable:" not available: 2.6.19 / Nov 2006

ret := &VirtualMemoryStat{}
for _, line := range lines {
Expand Down Expand Up @@ -48,9 +54,17 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
case "Cached":
ret.Cached = t * 1024
case "Active":
activeFile = true
ret.Active = t * 1024
case "Inactive":
inactiveFile = true
ret.Inactive = t * 1024
case "Active(file)":
activeFile = true
ret.ActiveFile = t * 1024
case "InActive(file)":
inactiveFile = true
ret.InactiveFile = t * 1024
case "Writeback":
ret.Writeback = t * 1024
case "WritebackTmp":
Expand All @@ -62,6 +76,7 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
case "Slab":
ret.Slab = t * 1024
case "SReclaimable":
sReclaimable = true
ret.SReclaimable = t * 1024
case "PageTables":
ret.PageTables = t * 1024
Expand Down Expand Up @@ -103,8 +118,13 @@ func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
ret.Cached += ret.SReclaimable

if !memavail {
ret.Available = ret.Free + ret.Buffers + ret.Cached
if !(activeFile && inactiveFile && sReclaimable) {
ret.Available = ret.Cached + ret.Free
} else {
ret.Available = calcuateAvailVmem(ret)
}
}

ret.Used = ret.Total - ret.Free - ret.Buffers - ret.Cached
ret.UsedPercent = float64(ret.Used) / float64(ret.Total) * 100.0

Expand Down Expand Up @@ -156,3 +176,47 @@ func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
}
return ret, nil
}

// calcuateAvailVmem is a fallback under kernel 3.14 where /proc/meminfo does not provide
// "MemAvailable:" column. It reimplements an algorithm from the link below
// https://github.com/giampaolo/psutil/pull/890
func calcuateAvailVmem(ret *VirtualMemoryStat) uint64 {
mznet marked this conversation as resolved.
Show resolved Hide resolved
var watermarkLow uint64

fn := common.HostProc("zoneinfo")
lines, err := common.ReadLines(fn)

if err != nil {
return ret.Free + ret.Cached // fallback under kernel 2.6.13
}

pagesize := uint64(os.Getpagesize())
watermarkLow = 0

for _, line := range lines {
fields := strings.Fields(line)

if strings.HasPrefix(fields[0], "low") {
lowValue, err := strconv.ParseUint(fields[1], 10, 64)

if err != nil {
lowValue = 0
}
watermarkLow += lowValue
}
}

watermarkLow *= pagesize

availMemory := ret.Free - watermarkLow
pageCache := ret.ActiveFile + ret.InactiveFile
pageCache -= uint64(math.Min(float64(pageCache/2), float64(watermarkLow)))
availMemory += pageCache
availMemory += ret.SReclaimable - uint64(math.Min(float64(ret.SReclaimable/2.0), float64(watermarkLow)))

if availMemory < 0 {
availMemory = 0
}

return availMemory
}
5 changes: 4 additions & 1 deletion mem/mem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func TestVirtualMemoryStat_String(t *testing.T) {
UsedPercent: 30.1,
Free: 40,
}
e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"sreclaimable":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}`
e := `{"total":10,"available":20,"used":30,"usedPercent":30.1,"free":40,"active":0,"inactive":0,"wired":0,"laundry":0,"activefile":0,"inactivefile":0,"buffers":0,"cached":0,"writeback":0,"dirty":0,"writebacktmp":0,"shared":0,"slab":0,"sreclaimable":0,"pagetables":0,"swapcached":0,"commitlimit":0,"committedas":0,"hightotal":0,"highfree":0,"lowtotal":0,"lowfree":0,"swaptotal":0,"swapfree":0,"mapped":0,"vmalloctotal":0,"vmallocused":0,"vmallocchunk":0,"hugepagestotal":0,"hugepagesfree":0,"hugepagesize":0}`

fmt.Println(v)
fmt.Println(e)
if e != fmt.Sprintf("%v", v) {
t.Errorf("VirtualMemoryStat string is invalid: %v", v)
}
Expand Down