Skip to content

Commit 5254032

Browse files
committed
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 (thewtex#20)
1 parent e70a16c commit 5254032

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

openbsd/memory.cc

+14-9
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
#include <sys/mount.h> // VFS_* which we use to get cache
3636
#include <sys/sysctl.h>
37-
#include <sys/vmmeter.h> // vmtotal struct
3837

3938
#include "error.h"
4039
#include "memory.h"
@@ -43,6 +42,9 @@
4342

4443
static int pageshift;
4544

45+
#ifndef LOG1024
46+
#define LOG1024 10
47+
#endif
4648
#define pagesh(size) ((size) << pageshift)
4749

4850
std::string mem_string( bool use_colors = false )
@@ -72,11 +74,13 @@ std::string mem_string( bool use_colors = false )
7274
page_size >>= 1;
7375
}
7476

77+
pageshift -= LOG1024;
78+
7579
// get vm memory stats
76-
static int vm_totalmem[] = { CTL_VM, VM_METER };
77-
struct vmtotal vm_total;
78-
size = sizeof( vm_total );
79-
if( sysctl( vm_totalmem, 2, &vm_total, &size, NULL, 0 ) < 0 )
80+
static int uvmexp_mib[] = { CTL_VM, VM_UVMEXP };
81+
struct uvmexp uvmexp;
82+
size = sizeof( uvmexp );
83+
if( sysctl( uvmexp_mib, 2, &uvmexp, &size, NULL, 0 ) < 0 )
8084
{
8185
error( "memory: error getting vm memory stats" );
8286
}
@@ -90,16 +94,17 @@ std::string mem_string( bool use_colors = false )
9094
error( "memory: error getting cached memory size" );
9195
}
9296

93-
// calculations based on conky openbsd port
94-
used_mem = pagesh( vm_total.t_rm );
95-
free_mem = pagesh( vm_total.t_free );
97+
// calculations based on libgtop
98+
used_mem = (uint64_t) pagesh (uvmexp.npages - uvmexp.free) << LOG1024;
99+
100+
free_mem = (uint64_t) pagesh( uvmexp.free ) << LOG1024;
96101

97102
// from nagios-memory plugin
98103
used_mem -= pagesh( bcstats.numbufpages );
99104
free_mem += pagesh( bcstats.numbufpages );
100105

101106
// calculate total memory
102-
total_mem = used_mem + free_mem;
107+
total_mem = (uint64_t) pagesh( uvmexp.npages ) << LOG1024;
103108

104109
if( use_colors )
105110
{

0 commit comments

Comments
 (0)