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 (
|
2016-02-22 14:24:07 +08:00
|
|
|
"encoding/binary"
|
2014-09-20 08:30:14 +08:00
|
|
|
"strconv"
|
2014-08-08 22:09:28 +08:00
|
|
|
"strings"
|
2016-02-22 14:24:07 +08:00
|
|
|
"syscall"
|
2014-11-27 09:18:15 +08:00
|
|
|
|
2015-10-19 23:04:57 +08:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-08-08 22:09:28 +08:00
|
|
|
)
|
|
|
|
|
2016-02-22 14:24:07 +08:00
|
|
|
func getHwMemsize() (uint64, error) {
|
|
|
|
totalString, err := syscall.Sysctl("hw.memsize")
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// syscall.sysctl() helpfully assumes the result is a null-terminated string and
|
|
|
|
// removes the last byte of the result if it's 0 :/
|
|
|
|
totalString += "\x00"
|
|
|
|
|
|
|
|
total := uint64(binary.LittleEndian.Uint64([]byte(totalString)))
|
|
|
|
|
|
|
|
return total, nil
|
|
|
|
}
|
|
|
|
|
2014-08-08 22:09:28 +08:00
|
|
|
// 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
|
|
|
|
}
|