When MemAvailable is in /proc/meminfo, use it (kernel 3.14+)

This commit is contained in:
Cameron Sparr 2015-09-21 17:18:20 -07:00
parent c1313e7634
commit 6cd8284b30
1 changed files with 8 additions and 1 deletions

View File

@ -13,6 +13,8 @@ import (
func VirtualMemory() (*VirtualMemoryStat, error) {
filename := "/proc/meminfo"
lines, _ := common.ReadLines(filename)
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
memavail := false
ret := &VirtualMemoryStat{}
for _, line := range lines {
@ -33,6 +35,9 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
ret.Total = t * 1024
case "MemFree":
ret.Free = t * 1024
case "MemAvailable":
memavail = true
ret.Available = t * 1024
case "Buffers":
ret.Buffers = t * 1024
case "Cached":
@ -43,7 +48,9 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
ret.Inactive = t * 1024
}
}
ret.Available = ret.Free + ret.Buffers + ret.Cached
if !memavail {
ret.Available = ret.Free + ret.Buffers + ret.Cached
}
ret.Used = ret.Total - ret.Free
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0