From d109284df3a48e1855e45541d77ba1f04e09a5ce Mon Sep 17 00:00:00 2001 From: Johan Walles Date: Sun, 21 Feb 2016 21:45:20 +0100 Subject: [PATCH] Fix memory stats on Darwin, CGO --- mem/mem_darwin_cgo.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mem/mem_darwin_cgo.go b/mem/mem_darwin_cgo.go index 1d08528..00c0594 100644 --- a/mem/mem_darwin_cgo.go +++ b/mem/mem_darwin_cgo.go @@ -9,6 +9,7 @@ package mem import "C" import ( + "encoding/binary" "fmt" "syscall" "unsafe" @@ -28,19 +29,24 @@ func VirtualMemory() (*VirtualMemoryStat, error) { return nil, fmt.Errorf("host_statistics error=%d", status) } - totalCount := vmstat.wire_count + - vmstat.active_count + - vmstat.inactive_count + - vmstat.free_count + pageSize := uint64(syscall.Getpagesize()) + + totalString, err := syscall.Sysctl("hw.memsize") + if err != nil { + return nil, err + } + // syscall.sysctl() helpfully removes the last byte of the result if it's 0 :/ + totalString += "\x00" + total := uint64(binary.LittleEndian.Uint64([]byte(totalString))) + totalCount := C.natural_t(total / pageSize) availableCount := vmstat.inactive_count + vmstat.free_count usedPercent := 100 * float64(totalCount-availableCount) / float64(totalCount) - usedCount := totalCount - vmstat.free_count + usedCount := totalCount - availableCount - pageSize := uint64(syscall.Getpagesize()) return &VirtualMemoryStat{ - Total: pageSize * uint64(totalCount), + Total: total, Available: pageSize * uint64(availableCount), Used: pageSize * uint64(usedCount), UsedPercent: usedPercent,