fix OpenBSD cache memory information (#1542)

This will now use `bcstats.numbufpages` instead of `uvmexp.vnodepages`.

Inspired by OpenBSD's `src/usr.bin/top`

Signed-off-by: Matthieu Guegan <matthieu.guegan@deindeal.ch>
This commit is contained in:
Matthieu Guegan 2019-12-02 13:41:58 +01:00 committed by Johannes 'fish' Ziemke
parent 0d9d7e961a
commit 2cae917bb7

View file

@ -23,6 +23,7 @@ import (
/*
#include <sys/param.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/sysctl.h>
int
@ -37,22 +38,39 @@ sysctl_uvmexp(struct uvmexp *uvmexp)
return 0;
}
int
sysctl_bcstats(struct bcachestats *bcstats)
{
static int bcstats_mib[] = {CTL_VFS, VFS_GENERIC, VFS_BCACHESTAT};
size_t sz = sizeof(struct bcachestats);
if(sysctl(bcstats_mib, 3, bcstats, &sz, NULL, 0) < 0)
return -1;
return 0;
}
*/
import "C"
func (c *meminfoCollector) getMemInfo() (map[string]float64, error) {
var uvmexp C.struct_uvmexp
var bcstats C.struct_bcachestats
if _, err := C.sysctl_uvmexp(&uvmexp); err != nil {
return nil, fmt.Errorf("sysctl CTL_VM VM_UVMEXP failed: %v", err)
}
if _, err := C.sysctl_bcstats(&bcstats); err != nil {
return nil, fmt.Errorf("sysctl CTL_VFS VFS_GENERIC VFS_BCACHESTAT failed: %v", err)
}
ps := float64(uvmexp.pagesize)
// see uvm(9)
return map[string]float64{
"active_bytes": ps * float64(uvmexp.active),
"cache_bytes": ps * float64(uvmexp.vnodepages),
"cache_bytes": ps * float64(bcstats.numbufpages),
"free_bytes": ps * float64(uvmexp.free),
"inactive_bytes": ps * float64(uvmexp.inactive),
"size_bytes": ps * float64(uvmexp.npages),