Implement meminfo on BSD without cgo

This removes some error handling, which should be fine. If the calls
fail, we will get the zeroes, which is a safe enough fallback.
Additionally, if the first sysctl (page_size) succeeded it is unlikely
that other ones will fail.
This commit is contained in:
Dominik Honnef 2016-12-29 02:15:35 +01:00
parent 0f6191987e
commit d2a43f7d05
1 changed files with 14 additions and 46 deletions

View File

@ -17,30 +17,12 @@
package collector
import (
"errors"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"golang.org/x/sys/unix"
)
/*
#include <stddef.h>
#include <sys/sysctl.h>
int _sysctl(const char* name) {
int val;
size_t size = sizeof(val);
int res = sysctlbyname(name, &val, &size, NULL, 0);
if (res == -1) {
return -1;
}
if (size != sizeof(val)) {
return -2;
}
return val;
}
*/
import "C"
const (
memInfoSubsystem = "memory"
)
@ -58,34 +40,20 @@ func NewMeminfoCollector() (Collector, error) {
}
func (c *meminfoCollector) Update(ch chan<- prometheus.Metric) (err error) {
var pages map[string]C.int
pages = make(map[string]C.int)
pages := make(map[string]uint32)
size := C._sysctl(C.CString("vm.stats.vm.v_page_size"))
if size == -1 {
return errors.New("sysctl(vm.stats.vm.v_page_size) failed")
}
if size == -2 {
return errors.New("sysctl(vm.stats.vm.v_page_size) failed, wrong buffer size")
}
pages["active"] = C._sysctl(C.CString("vm.stats.vm.v_active_count"))
pages["inactive"] = C._sysctl(C.CString("vm.stats.vm.v_inactive_count"))
pages["wire"] = C._sysctl(C.CString("vm.stats.vm.v_wire_count"))
pages["cache"] = C._sysctl(C.CString("vm.stats.vm.v_cache_count"))
pages["free"] = C._sysctl(C.CString("vm.stats.vm.v_free_count"))
pages["swappgsin"] = C._sysctl(C.CString("vm.stats.vm.v_swappgsin"))
pages["swappgsout"] = C._sysctl(C.CString("vm.stats.vm.v_swappgsout"))
pages["total"] = C._sysctl(C.CString("vm.stats.vm.v_page_count"))
for key := range pages {
if pages[key] == -1 {
return errors.New("sysctl() failed for " + key)
}
if pages[key] == -2 {
return errors.New("sysctl() failed for " + key + ", wrong buffer size")
}
size, err := unix.SysctlUint32("vm.stats.vm.v_page_size")
if err != nil {
return fmt.Errorf("sysctl(vm.stats.vm.v_page_size) failed: %s", err)
}
pages["active"], _ = unix.SysctlUint32("vm.stats.vm.v_active_count")
pages["inactive"], _ = unix.SysctlUint32("vm.stats.vm.v_inactive_count")
pages["wire"], _ = unix.SysctlUint32("vm.stats.vm.v_wire_count")
pages["cache"], _ = unix.SysctlUint32("vm.stats.vm.v_cache_count")
pages["free"], _ = unix.SysctlUint32("vm.stats.vm.v_free_count")
pages["swappgsin"], _ = unix.SysctlUint32("vm.stats.vm.v_swappgsin")
pages["swappgsout"], _ = unix.SysctlUint32("vm.stats.vm.v_swappgsout")
pages["total"], _ = unix.SysctlUint32("vm.stats.vm.v_page_count")
for k, v := range pages {
ch <- prometheus.MustNewConstMetric(