Commit d1380cba authored by elij's avatar elij

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.
parent cc040ddf
......@@ -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
......
......@@ -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)),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment