Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve OpenBSD memory handling #77

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions sigar_openbsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import "C"
//import "github.com/davecgh/go-spew/spew"

import (
"fmt"
"runtime"
"syscall"
"time"
Expand Down Expand Up @@ -49,8 +50,7 @@ type Uvmexp struct {
vtextmin uint32
vnodemin uint32
anonminpct uint32
vtextmi uint32
npct uint32
vtextminpct uint32
vnodeminpct uint32
nswapdev uint32
swpages uint32
Expand Down Expand Up @@ -79,6 +79,7 @@ type Uvmexp struct {
zeroaborts uint32
fltnoram uint32
fltnoanon uint32
fltnoamap uint32
fltpgwait uint32
fltpgrele uint32
fltrelck uint32
Expand Down Expand Up @@ -130,6 +131,9 @@ type Bcachestats struct {
delwribufs uint64
kvaslots uint64
kvaslots_avail uint64
highflips uint64
highflops uint64
dmaflips uint64
}

type Swapent struct {
Expand Down Expand Up @@ -237,30 +241,39 @@ 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
mib3 := [3]int32{C.CTL_VFS, C.VFS_GENERIC, C.VFS_BCACHESTAT}
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)

Expand All @@ -279,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)")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this return an error when a machine has 0 swap devices? The OpenBSD man page for swapctl(2) is somewhat ambiguous here, but my interpretation is that a return value of 0 is OK and -1 indicates failure.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It won't return an error in such case; I've ran swapctl -d /dev/sd0b to remove my /dev/sd0b as a swap device and this conditional didn't get triggered.

}

for i := 0; i < int(nswap); i++ {
Expand Down