Merge pull request #17 from l0ner/freebsd_ramusage

Freebsd ramusage
This commit is contained in:
Matt McCormick 2015-02-14 21:18:55 -05:00
commit 1481ba09dc
3 changed files with 52 additions and 44 deletions

@ -28,8 +28,13 @@
float cpu_percentage( unsigned int cpu_usage_delay ) float cpu_percentage( unsigned int cpu_usage_delay )
{ {
int32_t load1[CPUSTATES]; #if __x86_64__ || __ppc64__
int32_t load2[CPUSTATES]; u_int64_t load1[CPUSTATES];
u_int64_t load2[CPUSTATES];
#else
u_int32_t load1[CPUSTATES];
u_int32_t load2[CPUSTATES];
#endif
GETSYSCTL( "kern.cp_time", load1 ); GETSYSCTL( "kern.cp_time", load1 );
usleep( cpu_usage_delay ); usleep( cpu_usage_delay );

@ -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();
} }

@ -30,33 +30,29 @@ std::string mem_string( bool use_colors )
std::ostringstream oss; std::ostringstream oss;
// These values are in bytes // These values are in bytes
int64_t total_mem; u_int64_t total_mem;
int64_t used_mem; u_int64_t used_mem;
int64_t unused_mem; //u_int64_t unused_mem;
vm_size_t page_size; vm_size_t page_size;
mach_port_t mach_port;
mach_msg_type_number_t count;
vm_statistics_data_t vm_stats; vm_statistics_data_t vm_stats;
// Get total physical memory // Get total physical memory
int mib[2]; int mib[] = { CTL_HW, HW_MEMSIZE };
mib[0] = CTL_HW; size_t length = sizeof( total_mem );
mib[1] = HW_MEMSIZE;
size_t length = sizeof( int64_t );
sysctl( mib, 2, &total_mem, &length, NULL, 0 ); sysctl( mib, 2, &total_mem, &length, NULL, 0 );
mach_port = mach_host_self(); mach_port_t mach_port = mach_host_self();
count = sizeof( vm_stats ) / sizeof( natural_t ); mach_msg_type_number_t count = sizeof( vm_stats ) / sizeof( natural_t );
if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) && if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) &&
KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO, KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO,
( host_info_t )&vm_stats, &count ) ) ( host_info_t )&vm_stats, &count )
)
{ {
unused_mem = ( int64_t )vm_stats.free_count * ( int64_t )page_size; //unused_mem = static_cast<u_int64_t>( vm_stats.free_count * page_size );
used_mem = ( ( int64_t )vm_stats.active_count + used_mem = static_cast<u_int64_t>(
( int64_t )vm_stats.inactive_count + ( int64_t )vm_stats.wire_count ( vm_stats.active_count + vm_stats.wire_count ) * page_size);
) * ( int64_t )page_size;
} }
if( use_colors ) if( use_colors )