Fixed RAM usage calculation on FreeBSD

- Instead of displaying hardware ram quantity, display ram available to be
  allocated by applications. FreeBSD uses some ram for kernel, segment mappings
  and other stuff. Those memory pages are not available for allocation by
  applications.
- Calculate correctly ram usage. Active + Wired (buffers). Nothing else.

For more info see:
- http://www.cyberciti.biz/files/scripts/freebsd-memory.pl.txt
- conky source code on github
- output of top and vmstat on FreeBSD
- http://www.zabbix.com/forum/showthread.php?t=21826
- https://support.zabbix.com/browse/ZBXNEXT-774

htop on FreeBSD uses linux procfs compatibility layer, and thus it's readings
are a little off.
This commit is contained in:
Pawel "l0ner" Soltys 2015-02-13 18:38:21 +01:00
parent 80d70b7a4d
commit 869d43f102

@ -31,40 +31,48 @@
std::string mem_string( bool use_colors = false ) std::string mem_string( bool use_colors = false )
{ {
// These values are in bytes // These values are in bytes
int32_t total_mem = 0; //u_int total;
int64_t used_mem = 0; //u_int free;
int64_t unused_mem = 0; //u_int inactive;
int32_t inactive_mem = 0; //u_int cache;
int32_t active_mem = 0; u_int wired;
int32_t free_mem = 0; u_int active;
int32_t wired_mem = 0; u_int page_size;
int32_t page_size = 0; u_int page_count;
int32_t cache_mem = 0;
std::ostringstream oss; std::ostringstream oss;
// Get total physical memory, page size, and some other needed info // Get total physical memory, page size, and some other needed info
GETSYSCTL( "hw.realmem", total_mem ); // instead of using realmem which reports quantity of ram installed on
// platform, use physmem which reports ram available to system.
//GETSYSCTL( "hw.physmem", total );
GETSYSCTL( "hw.pagesize", page_size ); GETSYSCTL( "hw.pagesize", page_size );
// page count reflects actual size of memory we have available for
// applications. it will be less than realmem or physmem, since it doesn't
// include what's been allocated for kernel, but it will be equal to
// free + inactive + cache + wired + active, and thus will reflect better
// what's actually available.
GETSYSCTL( "vm.stats.vm.v_page_count", page_count );
GETSYSCTL( "vm.stats.vm.v_free_count", free_mem ); //GETSYSCTL( "vm.stats.vm.v_free_count", free );
GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive_mem ); //GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive );
GETSYSCTL( "vm.stats.vm.v_cache_count", cache_mem ); //GETSYSCTL( "vm.stats.vm.v_cache_count", cache );
GETSYSCTL( "vm.stats.vm.v_wire_count", wired_mem ); GETSYSCTL( "vm.stats.vm.v_wire_count", wired ); // Buffers
GETSYSCTL( "vm.stats.vm.v_active_count", active_mem ); GETSYSCTL( "vm.stats.vm.v_active_count", active );
// Get all memory which can be allocated // Get all memory which can be allocated, which on FreeBSD is:
//unused_mem = (cache_mem + free_mem) * page_size; // cached + inactive + free
used_mem = ( static_cast<int64_t>( active_mem ) + //u_int unused = ( cache + inactive + free ) * page_size;
static_cast<int64_t>( inactive_mem ) +
static_cast<int64_t>( wired_mem ) ) * static_cast<int64_t>( page_size ); // Used memory on FreeBSD is active + wired.
u_int used = ( active + wired ) * page_size;
if( use_colors ) if( use_colors )
{ {
oss << mem_lut[( 100 * used_mem ) / total_mem]; oss << mem_lut[ ( 100 * used ) / ( page_count * page_size ) ];
} }
oss << convert_unit( used_mem, MEGABYTES ) << '/' oss << convert_unit( used, MEGABYTES ) << '/'
<< convert_unit( total_mem, MEGABYTES ) << "MB"; << convert_unit( page_count * page_size, MEGABYTES ) << "MB";
if( use_colors ) if( use_colors )
{ {
@ -73,4 +81,3 @@ std::string mem_string( bool use_colors = false )
return oss.str(); return oss.str();
} }