From d1380cba29e2afb841cd31ba4d5158c12c3f32cb Mon Sep 17 00:00:00 2001 From: elij Date: Thu, 4 Feb 2016 13:29:15 -0800 Subject: [PATCH] try to fix diskio stats on FreeBSD-10.x FreeBSD apparently changed the magic sysctl mib values for devstats. --- FAIL: TestDisk_io_counters (0.00s) disk_test.go:39: error no such file or directory disk_test.go:42: ret is empty, map[] This code uses an undocumented, but exported, go stdlib method to fetch the sysctl by string instead of mib. --- disk/disk_freebsd.go | 17 ++++------------- internal/common/common_freebsd.go | 5 +++-- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/disk/disk_freebsd.go b/disk/disk_freebsd.go index 7e32c4b..efec201 100644 --- a/disk/disk_freebsd.go +++ b/disk/disk_freebsd.go @@ -12,14 +12,6 @@ import ( "github.com/shirou/gopsutil/internal/common" ) -const ( - CTLKern = 1 - // KernDevstat = 773 // for freebsd 8.4 - // KernDevstatAll = 772 // for freebsd 8.4 - KernDevstat = 974 - KernDevstatAll = 975 -) - func DiskPartitions(all bool) ([]DiskPartitionStat, error) { var ret []DiskPartitionStat @@ -98,19 +90,18 @@ func DiskPartitions(all bool) ([]DiskPartitionStat, error) { func DiskIOCounters() (map[string]DiskIOCountersStat, error) { // statinfo->devinfo->devstat // /usr/include/devinfo.h - - // sysctl.sysctl ('kern.devstat.all', 0) ret := make(map[string]DiskIOCountersStat) - mib := []int32{CTLKern, KernDevstat, KernDevstatAll} - buf, length, err := common.CallSyscall(mib) + r, err := syscall.Sysctl("kern.devstat.all") if err != nil { return nil, err } + buf := []byte(r) + length := len(buf) ds := Devstat{} devstatLen := int(unsafe.Sizeof(ds)) - count := int(length / uint64(devstatLen)) + count := int(uint64(length) / uint64(devstatLen)) buf = buf[8:] // devstat.all has version in the head. // parse buf to Devstat diff --git a/internal/common/common_freebsd.go b/internal/common/common_freebsd.go index 8ccd40e..44f194b 100644 --- a/internal/common/common_freebsd.go +++ b/internal/common/common_freebsd.go @@ -22,13 +22,14 @@ func DoSysctrl(mib string) ([]string, error) { } func CallSyscall(mib []int32) ([]byte, uint64, error) { + mibptr := unsafe.Pointer(&mib[0]) miblen := uint64(len(mib)) // get required buffer size length := uint64(0) _, _, err := syscall.Syscall6( syscall.SYS___SYSCTL, - uintptr(unsafe.Pointer(&mib[0])), + uintptr(mibptr), uintptr(miblen), 0, uintptr(unsafe.Pointer(&length)), @@ -46,7 +47,7 @@ func CallSyscall(mib []int32) ([]byte, uint64, error) { buf := make([]byte, length) _, _, err = syscall.Syscall6( syscall.SYS___SYSCTL, - uintptr(unsafe.Pointer(&mib[0])), + uintptr(mibptr), uintptr(miblen), uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&length)),