2016-02-12 18:02:16 +00:00
|
|
|
// +build linux
|
|
|
|
|
|
|
|
package mem
|
|
|
|
|
|
|
|
import (
|
2018-03-06 00:52:43 +00:00
|
|
|
"context"
|
2016-02-12 18:02:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2017-07-28 16:46:44 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2016-02-12 18:02:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return VirtualMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func VirtualMemoryWithContext(ctx context.Context) (*VirtualMemoryStat, error) {
|
2016-02-12 18:02:16 +00:00
|
|
|
filename := common.HostProc("meminfo")
|
|
|
|
lines, _ := common.ReadLines(filename)
|
|
|
|
// flag if MemAvailable is in /proc/meminfo (kernel 3.14+)
|
|
|
|
memavail := false
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
t, err := strconv.ParseUint(value, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
switch key {
|
|
|
|
case "MemTotal":
|
|
|
|
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":
|
|
|
|
ret.Cached = t * 1024
|
|
|
|
case "Active":
|
|
|
|
ret.Active = t * 1024
|
|
|
|
case "Inactive":
|
|
|
|
ret.Inactive = t * 1024
|
2016-08-30 17:48:07 +00:00
|
|
|
case "Writeback":
|
|
|
|
ret.Writeback = t * 1024
|
|
|
|
case "WritebackTmp":
|
|
|
|
ret.WritebackTmp = t * 1024
|
|
|
|
case "Dirty":
|
|
|
|
ret.Dirty = t * 1024
|
2017-02-02 06:41:29 +00:00
|
|
|
case "Shmem":
|
|
|
|
ret.Shared = t * 1024
|
|
|
|
case "Slab":
|
|
|
|
ret.Slab = t * 1024
|
|
|
|
case "PageTables":
|
|
|
|
ret.PageTables = t * 1024
|
|
|
|
case "SwapCached":
|
|
|
|
ret.SwapCached = t * 1024
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !memavail {
|
|
|
|
ret.Available = ret.Free + ret.Buffers + ret.Cached
|
|
|
|
}
|
2016-05-09 15:19:04 +00:00
|
|
|
ret.Used = ret.Total - ret.Available
|
2016-02-12 18:02:16 +00:00
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
2018-03-06 00:52:43 +00:00
|
|
|
return SwapMemoryWithContext(context.Background())
|
|
|
|
}
|
|
|
|
|
|
|
|
func SwapMemoryWithContext(ctx context.Context) (*SwapMemoryStat, error) {
|
2017-07-28 16:46:44 +00:00
|
|
|
sysinfo := &unix.Sysinfo_t{}
|
2016-02-12 18:02:16 +00:00
|
|
|
|
2017-07-28 16:46:44 +00:00
|
|
|
if err := unix.Sysinfo(sysinfo); err != nil {
|
2016-02-12 18:02:16 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ret := &SwapMemoryStat{
|
2017-02-02 06:41:29 +00:00
|
|
|
Total: uint64(sysinfo.Totalswap) * uint64(sysinfo.Unit),
|
|
|
|
Free: uint64(sysinfo.Freeswap) * uint64(sysinfo.Unit),
|
2016-02-12 18:02:16 +00:00
|
|
|
}
|
|
|
|
ret.Used = ret.Total - ret.Free
|
|
|
|
//check Infinity
|
|
|
|
if ret.Total != 0 {
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Free) / float64(ret.Total) * 100.0
|
|
|
|
} else {
|
|
|
|
ret.UsedPercent = 0
|
|
|
|
}
|
|
|
|
filename := common.HostProc("vmstat")
|
|
|
|
lines, _ := common.ReadLines(filename)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret, nil
|
|
|
|
}
|