diff --git a/common/main.cc b/common/main.cc index edabdce..8eedfd1 100644 --- a/common/main.cc +++ b/common/main.cc @@ -93,6 +93,7 @@ int main( int argc, char** argv ) unsigned cpu_usage_delay = 990000; short graph_lines = 10; // max 32767 should be enough bool use_colors = false; + int mem_mode = 0; static struct option long_options[] = { @@ -109,7 +110,7 @@ int main( int argc, char** argv ) int c; // while c != -1 - while( (c = getopt_long( argc, argv, "hi:g:", long_options, NULL) ) != -1 ) + while( (c = getopt_long( argc, argv, "hi:g:m:", long_options, NULL) ) != -1 ) { switch( c ) { @@ -136,6 +137,14 @@ int main( int argc, char** argv ) } graph_lines = atoi( optarg ); break; + case 'm': // --mem-mode, -m + if( atoi( optarg ) < 0 ) + { + std::cerr << "Memory mode argument must be zero or greater.\n"; + return EXIT_FAILURE; + } + mem_mode = atoi( optarg ); + break; case '?': // getopt_long prints error message automatically return EXIT_FAILURE; @@ -154,7 +163,7 @@ int main( int argc, char** argv ) return EXIT_FAILURE; } - std::cout << mem_string( use_colors ) + std::cout << mem_string( use_colors, mem_mode ) << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << load_string( use_colors ); diff --git a/common/memory.h b/common/memory.h index 3a123bb..814d81e 100644 --- a/common/memory.h +++ b/common/memory.h @@ -21,6 +21,6 @@ #include -std::string mem_string( bool ); +std::string mem_string( bool, int ); #endif diff --git a/osx/memory.cc b/osx/memory.cc index eb5dbea..35c47d6 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -25,13 +25,14 @@ #include "luts.h" #include "conversions.h" -std::string mem_string( bool use_colors ) +std::string mem_string( bool use_colors, int mode ) { std::ostringstream oss; // These values are in bytes u_int64_t total_mem; - u_int64_t used_mem; + float used_mem; + float percentage_mem; //u_int64_t unused_mem; vm_size_t page_size; @@ -51,17 +52,35 @@ std::string mem_string( bool use_colors ) { //unused_mem = static_cast( vm_stats.free_count * page_size ); - used_mem = static_cast( + used_mem = static_cast( ( vm_stats.active_count + vm_stats.wire_count ) * page_size); } if( use_colors ) { - oss << mem_lut[( 100 * used_mem ) / total_mem]; + oss << mem_lut[( 100 * static_cast(used_mem) ) / total_mem]; } - oss << convert_unit( used_mem, MEGABYTES ) << '/' - << convert_unit( total_mem, MEGABYTES ) << "MB"; + + percentage_mem = static_cast(used_mem) / static_cast(total_mem) * 100.0; + + oss.precision( 2 ); + oss.setf( std::ios::fixed | std::ios::right ); + + if( mode == 0 ) { + oss << convert_unit( used_mem, MEGABYTES ) << '/' + << convert_unit( total_mem, MEGABYTES ) << "MB"; + } else if ( mode == 1 ) { + float free_mem = total_mem - used_mem; + + if( free_mem < 1 ) { + oss << convert_unit( free_mem, MEGABYTES ) << "MB"; + } else { + oss << convert_unit( free_mem, GIGABYTES ) << "GB"; + } + } else { + oss << percentage_mem << '%'; + } if( use_colors ) {