Fix memory stats on Darwin, non-CGO

pull/160/head
Johan Walles 9 years ago
parent d109284df3
commit dd875d4970

@ -3,12 +3,29 @@
package mem
import (
"encoding/binary"
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
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
}
// SwapMemory returns swapinfo.
func SwapMemory() (*SwapMemoryStat, error) {
var ret *SwapMemoryStat

@ -9,7 +9,6 @@ package mem
import "C"
import (
"encoding/binary"
"fmt"
"syscall"
"unsafe"
@ -30,14 +29,10 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
}
pageSize := uint64(syscall.Getpagesize())
totalString, err := syscall.Sysctl("hw.memsize")
total, err := getHwMemsize()
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

@ -8,8 +8,6 @@ import (
"strconv"
"strings"
"syscall"
"github.com/shirou/gopsutil/internal/common"
)
// Runs vm_stat and returns Free and inactive pages
@ -67,11 +65,7 @@ func parseVMStat(out string, vms *VirtualMemoryStat) error {
func VirtualMemory() (*VirtualMemoryStat, error) {
ret := &VirtualMemoryStat{}
t, err := common.DoSysctrl("hw.memsize")
if err != nil {
return nil, err
}
total, err := strconv.ParseUint(t[0], 10, 64)
total, err := getHwMemsize()
if err != nil {
return nil, err
}
@ -83,8 +77,8 @@ func VirtualMemory() (*VirtualMemoryStat, error) {
ret.Available = ret.Free + ret.Inactive
ret.Total = total
ret.Used = ret.Total - ret.Free
ret.UsedPercent = float64(ret.Total-ret.Available) / float64(ret.Total) * 100.0
ret.Used = ret.Total - ret.Available
ret.UsedPercent = 100 * float64(ret.Used) / float64(ret.Total)
return ret, nil
}

Loading…
Cancel
Save