Switch to using uvmexp which is the prefered way of retrieving memory information

Also fixup some page shifting and casting issues which led to incorrect
amounts of memory being displayed. This also fixes a crash with --colors (#20)
This commit is contained in:
Jasper Lievisse Adriaanse 2015-03-01 13:45:31 +01:00
parent e70a16c936
commit 52540329e5

@ -34,7 +34,6 @@
#include <sys/mount.h> // VFS_* which we use to get cache #include <sys/mount.h> // VFS_* which we use to get cache
#include <sys/sysctl.h> #include <sys/sysctl.h>
#include <sys/vmmeter.h> // vmtotal struct
#include "error.h" #include "error.h"
#include "memory.h" #include "memory.h"
@ -43,6 +42,9 @@
static int pageshift; static int pageshift;
#ifndef LOG1024
#define LOG1024 10
#endif
#define pagesh(size) ((size) << pageshift) #define pagesh(size) ((size) << pageshift)
std::string mem_string( bool use_colors = false ) std::string mem_string( bool use_colors = false )
@ -72,11 +74,13 @@ std::string mem_string( bool use_colors = false )
page_size >>= 1; page_size >>= 1;
} }
pageshift -= LOG1024;
// get vm memory stats // get vm memory stats
static int vm_totalmem[] = { CTL_VM, VM_METER }; static int uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
struct vmtotal vm_total; struct uvmexp uvmexp;
size = sizeof( vm_total ); size = sizeof( uvmexp );
if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 ) if( sysctl( uvmexp_mib, 2, &uvmexp, &size, NULL, 0 ) < 0 )
{ {
error( "memory: error getting vm memory stats" ); error( "memory: error getting vm memory stats" );
} }
@ -90,16 +94,17 @@ std::string mem_string( bool use_colors = false )
error( "memory: error getting cached memory size" ); error( "memory: error getting cached memory size" );
} }
// calculations based on conky openbsd port // calculations based on libgtop
used_mem = pagesh( vm_total.t_rm ); used_mem = (uint64_t) pagesh (uvmexp.npages - uvmexp.free) << LOG1024;
free_mem = pagesh( vm_total.t_free );
free_mem = (uint64_t) pagesh( uvmexp.free ) << LOG1024;
// from nagios-memory plugin // from nagios-memory plugin
used_mem -= pagesh( bcstats.numbufpages ); used_mem -= pagesh( bcstats.numbufpages );
free_mem += pagesh( bcstats.numbufpages ); free_mem += pagesh( bcstats.numbufpages );
// calculate total memory // calculate total memory
total_mem = used_mem + free_mem; total_mem = (uint64_t) pagesh( uvmexp.npages ) << LOG1024;
if( use_colors ) if( use_colors )
{ {