2014-04-22 11:04:16 +08:00
|
|
|
// +build linux
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2014-04-22 08:44:22 +08:00
|
|
|
package gopsutil
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-07-03 16:39:21 +08:00
|
|
|
"strings"
|
2014-04-18 15:34:47 +08:00
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2014-07-03 16:39:21 +08:00
|
|
|
filename := "/proc/meminfo"
|
|
|
|
lines, _ := readLines(filename)
|
|
|
|
|
|
|
|
ret := &VirtualMemoryStat{}
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, ":")
|
|
|
|
if len(fields) != 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(fields[0])
|
|
|
|
value := strings.TrimSpace(fields[1])
|
|
|
|
value = strings.Replace(value, " kB", "", -1)
|
|
|
|
|
|
|
|
switch key {
|
|
|
|
case "MemTotal":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Total = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
case "MemFree":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Free = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Buffers":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Buffers = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Cached":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Cached = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Active":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Active = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Inactive":
|
2014-07-03 16:46:10 +08:00
|
|
|
ret.Inactive = mustParseUint64(value) * 1000
|
2014-07-03 16:39:21 +08:00
|
|
|
|
|
|
|
}
|
2014-05-01 11:01:30 +08:00
|
|
|
}
|
2014-04-18 15:34:47 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2014-04-18 15:34:47 +08:00
|
|
|
sysinfo := &syscall.Sysinfo_t{}
|
|
|
|
|
|
|
|
if err := syscall.Sysinfo(sysinfo); err != nil {
|
2014-05-01 11:01:30 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &SwapMemoryStat{
|
2014-05-13 23:12:17 +08:00
|
|
|
Total: uint64(sysinfo.Totalswap),
|
|
|
|
Free: uint64(sysinfo.Freeswap),
|
2014-04-18 15:34:47 +08:00
|
|
|
}
|
|
|
|
ret.Used = ret.Total - ret.Free
|
2014-09-10 02:23:12 +08:00
|
|
|
//check Infinity
|
|
|
|
if ret.Total != 0 {
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
|
|
|
} else {
|
|
|
|
ret.UsedPercent = 0
|
|
|
|
}
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|