2014-04-22 11:04:16 +08:00
|
|
|
// +build linux
|
2014-04-18 15:34:47 +08:00
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package mem
|
2014-04-18 15:34:47 +08:00
|
|
|
|
|
|
|
import (
|
2014-09-19 21:41:29 +08:00
|
|
|
"strconv"
|
2014-07-03 16:39:21 +08:00
|
|
|
"strings"
|
2014-04-18 15:34:47 +08:00
|
|
|
"syscall"
|
2014-11-27 09:25:14 +08:00
|
|
|
|
2015-10-19 11:40:01 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-04-18 15:34:47 +08:00
|
|
|
)
|
|
|
|
|
2014-05-01 11:01:30 +08:00
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2015-10-19 11:40:01 +08:00
|
|
|
filename := common.HostProc("meminfo")
|
2014-11-27 09:25:14 +08:00
|
|
|
lines, _ := common.ReadLines(filename)
|
2015-09-22 08:18:20 +08:00
|
|
|
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
|
|
|
|
memavail := false
|
2014-07-03 16:39:21 +08:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2014-09-19 23:21:26 +08:00
|
|
|
t, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
2014-07-03 16:39:21 +08:00
|
|
|
switch key {
|
|
|
|
case "MemTotal":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Total = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
case "MemFree":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Free = t * 1024
|
2015-09-22 08:18:20 +08:00
|
|
|
case "MemAvailable":
|
|
|
|
memavail = true
|
|
|
|
ret.Available = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Buffers":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Buffers = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Cached":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Cached = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Active":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Active = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
case "Inactive":
|
2015-08-27 23:41:14 +08:00
|
|
|
ret.Inactive = t * 1024
|
2016-08-16 20:13:14 +08:00
|
|
|
case "Writeback":
|
|
|
|
ret.Writeback = t * 1024
|
|
|
|
case "WritebackTmp":
|
|
|
|
ret.WritebackTmp = t * 1024
|
|
|
|
case "Dirty":
|
|
|
|
ret.Dirty = t * 1024
|
2014-07-03 16:39:21 +08:00
|
|
|
}
|
2014-05-01 11:01:30 +08:00
|
|
|
}
|
2015-09-22 08:18:20 +08:00
|
|
|
if !memavail {
|
|
|
|
ret.Available = ret.Free + ret.Buffers + ret.Cached
|
|
|
|
}
|
2016-02-19 04:39:09 +08:00
|
|
|
ret.Used = ret.Total - ret.Available
|
2014-04-18 15:34:47 +08:00
|
|
|
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{
|
2016-09-04 17:57:16 +08:00
|
|
|
Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit),
|
|
|
|
Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit),
|
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
|
|
|
|
}
|
2015-10-19 11:40:01 +08:00
|
|
|
filename := common.HostProc("vmstat")
|
2015-10-17 01:58:17 +08:00
|
|
|
lines, _ := common.ReadLines(filename)
|
2014-11-02 15:17:50 +08:00
|
|
|
for _, l := range lines {
|
|
|
|
fields := strings.Fields(l)
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
switch fields[0] {
|
|
|
|
case "pswpin":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sin = value * 4 * 1024
|
|
|
|
case "pswpout":
|
|
|
|
value, err := strconv.ParseUint(fields[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ret.Sout = value * 4 * 1024
|
|
|
|
}
|
|
|
|
}
|
2014-04-18 15:34:47 +08:00
|
|
|
return ret, nil
|
|
|
|
}
|