From e19fe5b7222ac18b67f75398b0b37b942594ec2d Mon Sep 17 00:00:00 2001 From: Jasper Lievisse Adriaanse Date: Thu, 29 Jun 2017 22:27:37 +0200 Subject: [PATCH 1/2] Sync struct uvmexp and bcachestats with OpenBSD 6.1 This ensures the types are packed correctly and we don't end up with a silent 0 for npages for example. closes #12 --- sigar_openbsd.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/sigar_openbsd.go b/sigar_openbsd.go index abf967b83..899f3ff34 100644 --- a/sigar_openbsd.go +++ b/sigar_openbsd.go @@ -49,8 +49,7 @@ type Uvmexp struct { vtextmin uint32 vnodemin uint32 anonminpct uint32 - vtextmi uint32 - npct uint32 + vtextminpct uint32 vnodeminpct uint32 nswapdev uint32 swpages uint32 @@ -79,6 +78,7 @@ type Uvmexp struct { zeroaborts uint32 fltnoram uint32 fltnoanon uint32 + fltnoamap uint32 fltpgwait uint32 fltpgrele uint32 fltrelck uint32 @@ -130,6 +130,9 @@ type Bcachestats struct { delwribufs uint64 kvaslots uint64 kvaslots_avail uint64 + highflips uint64 + highflops uint64 + dmaflips uint64 } type Swapent struct { From 0e3fec1dc96f53b634f07fb33278532b1fb54c73 Mon Sep 17 00:00:00 2001 From: Jasper Lievisse Adriaanse Date: Fri, 30 Jun 2017 20:22:30 +0200 Subject: [PATCH 2/2] Add better error handling for functions directly using kernel structures --- sigar_openbsd.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/sigar_openbsd.go b/sigar_openbsd.go index 899f3ff34..eb8e54dba 100644 --- a/sigar_openbsd.go +++ b/sigar_openbsd.go @@ -19,6 +19,7 @@ import "C" //import "github.com/davecgh/go-spew/spew" import ( + "fmt" "runtime" "syscall" "time" @@ -240,12 +241,12 @@ func (self *Mem) Get() error { // First we determine how much memory we'll need to pass later on (via `n`) _, _, errno := syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 2, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { - return nil + return fmt.Errorf("Failed to prepare vm.uvmexp: %d", errno) } _, _, errno = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib[0])), 2, uintptr(unsafe.Pointer(&uvmexp)), uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { - return nil + return fmt.Errorf("Failed to query vm.uvmexp: %d", errno) } var bcachestats Bcachestats @@ -253,17 +254,26 @@ func (self *Mem) Get() error { n = uintptr(0) _, _, errno = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib3[0])), 3, 0, uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { - return nil + return fmt.Errorf("Failed to prepare vfs.generic.bcachestat: %d", errno) } + _, _, errno = syscall.Syscall6(syscall.SYS___SYSCTL, uintptr(unsafe.Pointer(&mib3[0])), 3, uintptr(unsafe.Pointer(&bcachestats)), uintptr(unsafe.Pointer(&n)), 0, 0) if errno != 0 || n == 0 { - return nil + return fmt.Errorf("Failed to query vfs.generic.bcachestat: %d", errno) } self.Total = uint64(uvmexp.npages) << uvmexp.pageshift self.Used = uint64(uvmexp.npages-uvmexp.free) << uvmexp.pageshift self.Free = uint64(uvmexp.free) << uvmexp.pageshift + // If the kernel interface changes we might silently return bogus + // data. So explicitly check for 0 total memory (check this late in + // the program flow as by now various kernel interfaces have been + // used. cf. https://github.com/elastic/gosigar/issues/72 + if self.Total == 0 { + return fmt.Errorf("Invalid data: zero memory pages found") + } + self.ActualFree = self.Free + (uint64(bcachestats.numbufpages) << uvmexp.pageshift) self.ActualUsed = self.Used - (uint64(bcachestats.numbufpages) << uvmexp.pageshift) @@ -282,7 +292,7 @@ func (self *Swap) Get() error { rnswap := C.swapctl(C.SWAP_STATS, unsafe.Pointer(&swdev[0]), nswap) if rnswap == 0 { - return nil + return fmt.Errorf("Failed to call swapctl(2)") } for i := 0; i < int(nswap); i++ {