From 869d43f1022be96dd16641bf29e1143b8d8d4dbd Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Fri, 13 Feb 2015 18:38:21 +0100 Subject: [PATCH 1/3] 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. --- freebsd/memory.cc | 55 ++++++++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/freebsd/memory.cc b/freebsd/memory.cc index aa3b076..4c3ce9c 100644 --- a/freebsd/memory.cc +++ b/freebsd/memory.cc @@ -31,40 +31,48 @@ std::string mem_string( bool use_colors = false ) { // These values are in bytes - int32_t total_mem = 0; - int64_t used_mem = 0; - int64_t unused_mem = 0; - int32_t inactive_mem = 0; - int32_t active_mem = 0; - int32_t free_mem = 0; - int32_t wired_mem = 0; - int32_t page_size = 0; - int32_t cache_mem = 0; + //u_int total; + //u_int free; + //u_int inactive; + //u_int cache; + u_int wired; + u_int active; + u_int page_size; + u_int page_count; std::ostringstream oss; // 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 ); + // 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_inactive_count", inactive_mem ); - GETSYSCTL( "vm.stats.vm.v_cache_count", cache_mem ); - GETSYSCTL( "vm.stats.vm.v_wire_count", wired_mem ); - GETSYSCTL( "vm.stats.vm.v_active_count", active_mem ); + //GETSYSCTL( "vm.stats.vm.v_free_count", free ); + //GETSYSCTL( "vm.stats.vm.v_inactive_count", inactive ); + //GETSYSCTL( "vm.stats.vm.v_cache_count", cache ); + GETSYSCTL( "vm.stats.vm.v_wire_count", wired ); // Buffers + GETSYSCTL( "vm.stats.vm.v_active_count", active ); - // Get all memory which can be allocated - //unused_mem = (cache_mem + free_mem) * page_size; - used_mem = ( static_cast( active_mem ) + - static_cast( inactive_mem ) + - static_cast( wired_mem ) ) * static_cast( page_size ); + // Get all memory which can be allocated, which on FreeBSD is: + // cached + inactive + free + //u_int unused = ( cache + inactive + free ) * page_size; + + // Used memory on FreeBSD is active + wired. + u_int used = ( active + wired ) * page_size; 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 ) << '/' - << convert_unit( total_mem, MEGABYTES ) << "MB"; + oss << convert_unit( used, MEGABYTES ) << '/' + << convert_unit( page_count * page_size, MEGABYTES ) << "MB"; if( use_colors ) { @@ -73,4 +81,3 @@ std::string mem_string( bool use_colors = false ) return oss.str(); } - From cf2a094f1367b5def1b8f75d1c0f558d51ce0e22 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Fri, 13 Feb 2015 19:56:47 +0100 Subject: [PATCH 2/3] Fix for cpu sysctl failing on 64bit FreeBSD same as commit 80d70b7a4d5ee9a4ba79deae0e840c65ccaf4131 --- freebsd/cpu.cc | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/freebsd/cpu.cc b/freebsd/cpu.cc index 403aea9..de865c9 100644 --- a/freebsd/cpu.cc +++ b/freebsd/cpu.cc @@ -28,8 +28,13 @@ float cpu_percentage( unsigned int cpu_usage_delay ) { - int32_t load1[CPUSTATES]; - int32_t load2[CPUSTATES]; +#if __x86_64__ || __ppc64__ + 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 ); usleep( cpu_usage_delay ); From 33d64bc53bf6117cdf0294898d9e991fbe87799d Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sun, 15 Feb 2015 01:17:51 +0100 Subject: [PATCH 3/3] OSX: exclude cached memory from used --- osx/memory.cc | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/osx/memory.cc b/osx/memory.cc index b538523..eb5dbea 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -30,33 +30,29 @@ std::string mem_string( bool use_colors ) std::ostringstream oss; // These values are in bytes - int64_t total_mem; - int64_t used_mem; - int64_t unused_mem; + u_int64_t total_mem; + u_int64_t used_mem; + //u_int64_t unused_mem; vm_size_t page_size; - mach_port_t mach_port; - mach_msg_type_number_t count; vm_statistics_data_t vm_stats; // Get total physical memory - int mib[2]; - mib[0] = CTL_HW; - mib[1] = HW_MEMSIZE; - size_t length = sizeof( int64_t ); + int mib[] = { CTL_HW, HW_MEMSIZE }; + size_t length = sizeof( total_mem ); sysctl( mib, 2, &total_mem, &length, NULL, 0 ); - mach_port = mach_host_self(); - count = sizeof( vm_stats ) / sizeof( natural_t ); + mach_port_t mach_port = mach_host_self(); + mach_msg_type_number_t count = sizeof( vm_stats ) / sizeof( natural_t ); if( KERN_SUCCESS == host_page_size( mach_port, &page_size ) && - KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO, - ( host_info_t )&vm_stats, &count ) ) + KERN_SUCCESS == host_statistics( mach_port, HOST_VM_INFO, + ( host_info_t )&vm_stats, &count ) + ) { - unused_mem = ( int64_t )vm_stats.free_count * ( int64_t )page_size; + //unused_mem = static_cast( vm_stats.free_count * page_size ); - used_mem = ( ( int64_t )vm_stats.active_count + - ( int64_t )vm_stats.inactive_count + ( int64_t )vm_stats.wire_count - ) * ( int64_t )page_size; + used_mem = static_cast( + ( vm_stats.active_count + vm_stats.wire_count ) * page_size); } if( use_colors ) @@ -65,7 +61,7 @@ std::string mem_string( bool use_colors ) } oss << convert_unit( used_mem, MEGABYTES ) << '/' - << convert_unit( total_mem, MEGABYTES ) << "MB"; + << convert_unit( total_mem, MEGABYTES ) << "MB"; if( use_colors ) {