Merge pull request #84 from influxdb/mem-new-kern

When MemAvailable is in /proc/meminfo, use it (kernel 3.14+)
This commit is contained in:
shirou 2015-09-22 14:55:57 +09:00
commit b389cc66e3
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