2014-08-08 23:09:28 +09:00
|
|
|
// +build darwin
|
|
|
|
|
2014-12-30 22:09:05 +09:00
|
|
|
package mem
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2016-02-19 21:28:50 +01:00
|
|
|
/*
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <mach/mach_host.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2014-08-08 23:09:28 +09:00
|
|
|
import (
|
2016-02-19 21:28:50 +01:00
|
|
|
"fmt"
|
2014-08-08 23:09:28 +09:00
|
|
|
"os/exec"
|
2014-09-20 09:30:14 +09:00
|
|
|
"strconv"
|
2014-08-08 23:09:28 +09:00
|
|
|
"strings"
|
2016-02-19 21:28:50 +01:00
|
|
|
"unsafe"
|
2014-11-27 10:18:15 +09:00
|
|
|
|
2015-10-20 00:04:57 +09:00
|
|
|
"github.com/shirou/gopsutil/internal/common"
|
2014-08-08 23:09:28 +09:00
|
|
|
)
|
|
|
|
|
|
|
|
func getPageSize() (uint64, error) {
|
|
|
|
out, err := exec.Command("pagesize").Output()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-09-20 09:37:02 +09:00
|
|
|
o := strings.TrimSpace(string(out))
|
|
|
|
p, err := strconv.ParseUint(o, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VirtualMemory returns VirtualmemoryStat.
|
|
|
|
func VirtualMemory() (*VirtualMemoryStat, error) {
|
2016-02-19 21:28:50 +01:00
|
|
|
count := C.mach_msg_type_number_t(C.HOST_VM_INFO_COUNT)
|
|
|
|
var vmstat C.vm_statistics_data_t
|
2015-09-17 22:52:45 -07:00
|
|
|
|
2016-02-19 21:28:50 +01:00
|
|
|
status := C.host_statistics(C.host_t(C.mach_host_self()),
|
|
|
|
C.HOST_VM_INFO,
|
|
|
|
C.host_info_t(unsafe.Pointer(&vmstat)),
|
|
|
|
&count)
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2016-02-19 21:28:50 +01:00
|
|
|
if status != C.KERN_SUCCESS {
|
|
|
|
return nil, fmt.Errorf("host_statistics error=%d", status)
|
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2016-02-19 21:28:50 +01:00
|
|
|
totalCount := vmstat.wire_count +
|
|
|
|
vmstat.active_count +
|
|
|
|
vmstat.inactive_count +
|
|
|
|
vmstat.free_count
|
|
|
|
|
|
|
|
availableCount := vmstat.inactive_count + vmstat.free_count
|
|
|
|
usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount)
|
|
|
|
|
|
|
|
usedCount := totalCount - vmstat.free_count
|
|
|
|
|
|
|
|
pageSize := uint64(C.getpagesize())
|
|
|
|
return &VirtualMemoryStat{
|
|
|
|
Total: pageSize * uint64(totalCount),
|
|
|
|
Available: pageSize * uint64(availableCount),
|
|
|
|
Used: pageSize * uint64(usedCount),
|
|
|
|
UsedPercent: usedPercent,
|
|
|
|
Free: pageSize * uint64(vmstat.free_count),
|
|
|
|
Active: pageSize * uint64(vmstat.active_count),
|
|
|
|
Inactive: pageSize * uint64(vmstat.inactive_count),
|
|
|
|
Wired: pageSize * uint64(vmstat.wire_count),
|
|
|
|
}, nil
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// SwapMemory returns swapinfo.
|
|
|
|
func SwapMemory() (*SwapMemoryStat, error) {
|
|
|
|
var ret *SwapMemoryStat
|
|
|
|
|
2014-11-27 10:18:15 +09:00
|
|
|
swapUsage, err := common.DoSysctrl("vm.swapusage")
|
2014-09-20 09:30:14 +09: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 23:09:28 +09:00
|
|
|
|
2015-02-26 16:23:35 +09:00
|
|
|
u := float64(0)
|
|
|
|
if total_v != 0 {
|
|
|
|
u = ((total_v - free_v) / total_v) * 100.0
|
|
|
|
}
|
2014-08-08 23:09:28 +09:00
|
|
|
|
2014-09-20 09:30:14 +09:00
|
|
|
// vm.swapusage shows "M", multiply 1000
|
2014-08-08 23:09:28 +09:00
|
|
|
ret = &SwapMemoryStat{
|
2014-09-20 09:30:14 +09:00
|
|
|
Total: uint64(total_v * 1000),
|
|
|
|
Used: uint64(used_v * 1000),
|
|
|
|
Free: uint64(free_v * 1000),
|
|
|
|
UsedPercent: u,
|
2014-08-08 23:09:28 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
}
|