2014-08-08 22:09:28 +08:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 21:09:05 +08:00
|
|
|
package mem
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
2014-09-20 08:30:14 +08:00
|
|
|
"strconv"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strings"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
|
|
|
common "github.com/shirou/gopsutil/common"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPageSize() (uint64, error) {
|
|
|
|
out, err := exec.Command("pagesize").Output()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-09-20 08:37:02 +08:00
|
|
|
o := strings.TrimSpace(string(out))
|
|
|
|
p, err := strconv.ParseUint(o, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
2015-09-18 13:52:45 +08:00
|
|
|
// Runs vm_stat and returns Free and inactive pages
|
|
|
|
func getVmStat(pagesize uint64, vms *VirtualMemoryStat) error {
|
|
|
|
out, err := exec.Command("vm_stat").Output()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return parseVmStat(string(out), pagesize, vms)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseVmStat(out string, pagesize uint64, vms *VirtualMemoryStat) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
lines := strings.Split(out, "\n")
|
|
|
|
for _, line := range lines {
|
|
|
|
fields := strings.Split(line, ":")
|
|
|
|
if len(fields) < 2 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
key := strings.TrimSpace(fields[0])
|
|
|
|
value := strings.Trim(fields[1], " .")
|
|
|
|
switch key {
|
|
|
|
case "Pages free":
|
|
|
|
free, e := strconv.ParseUint(value, 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
vms.Free = free * pagesize
|
|
|
|
case "Pages inactive":
|
|
|
|
inactive, e := strconv.ParseUint(value, 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
vms.Cached += inactive * pagesize
|
|
|
|
vms.Inactive = inactive * pagesize
|
|
|
|
case "Pages active":
|
|
|
|
active, e := strconv.ParseUint(value, 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
vms.Active = active * pagesize
|
|
|
|
case "Pages wired down":
|
|
|
|
wired, e := strconv.ParseUint(value, 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
vms.Wired = wired * pagesize
|
|
|
|
case "Pages purgeable":
|
|
|
|
purgeable, e := strconv.ParseUint(value, 10, 64)
|
|
|
|
if e != nil {
|
|
|
|
err = e
|
|
|
|
}
|
|
|
|
vms.Cached += purgeable * pagesize
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
// VirtualMemory returns VirtualmemoryStat.
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2015-09-18 13:52:45 +08:00
|
|
|
ret := &VirtualMemoryStat{}
|
|
|
|
|
2014-09-20 08:37:02 +08:00
|
|
|
p, err := getPageSize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-18 13:52:45 +08:00
|
|
|
t, err := common.DoSysctrl("hw.memsize")
|
2014-09-20 08:37:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-18 13:52:45 +08:00
|
|
|
total, err := strconv.ParseUint(t[0], 10, 64)
|
2014-09-20 08:37:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-09-18 13:52:45 +08:00
|
|
|
err = getVmStat(p, ret)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
2015-09-18 13:52:45 +08:00
|
|
|
ret.Available = ret.Free + ret.Cached
|
|
|
|
ret.Total = total
|
2014-08-08 22:09:28 +08:00
|
|
|
|
|
|
|
ret.Used = ret.Total - ret.Free
|
|
|
|
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SwapMemory returns swapinfo.
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
|
|
|
var ret *SwapMemoryStat
|
|
|
|
|
2014-11-27 09:18:15 +08:00
|
|
|
swapUsage, err := common.DoSysctrl("vm.swapusage")
|
2014-09-20 08:30:14 +08:00
|
|
|
if err != nil {
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
|
|
|
total := strings.Replace(swapUsage[2], "M", "", 1)
|
|
|
|
used := strings.Replace(swapUsage[5], "M", "", 1)
|
|
|
|
free := strings.Replace(swapUsage[8], "M", "", 1)
|
|
|
|
|
|
|
|
total_v, err := strconv.ParseFloat(total, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
used_v, err := strconv.ParseFloat(used, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
free_v, err := strconv.ParseFloat(free, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2015-02-26 15:23:35 +08:00
|
|
|
u := float64(0)
|
|
|
|
if total_v != 0 {
|
|
|
|
u = ((total_v - free_v) / total_v) * 100.0
|
|
|
|
}
|
2014-08-08 22:09:28 +08:00
|
|
|
|
2014-09-20 08:30:14 +08:00
|
|
|
// vm.swapusage shows "M", multiply 1000
|
2014-08-08 22:09:28 +08:00
|
|
|
ret = &SwapMemoryStat{
|
2014-09-20 08:30:14 +08:00
|
|
|
Total: uint64(total_v * 1000),
|
|
|
|
Used: uint64(used_v * 1000),
|
|
|
|
Free: uint64(free_v * 1000),
|
|
|
|
UsedPercent: u,
|
2014-08-08 22:09:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|