diff --git a/CMakeLists.txt b/CMakeLists.txt index 75ca0e9..7c2aafc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -123,6 +123,14 @@ if(BUILD_TESTING) COMMAND tmux-mem-cpu-load 2 8 ) + add_test(NAME memory_mode_free_memory + COMMAND tmux-mem-cpu-load -m 1 + ) + + add_test(NAME memory_mode_used_percentage + COMMAND tmux-mem-cpu-load -m 2 + ) + set_tests_properties(usage invalid_status_interval invalid_graph_lines diff --git a/common/main.cc b/common/main.cc index 8eedfd1..d111ee0 100644 --- a/common/main.cc +++ b/common/main.cc @@ -93,7 +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; + int mem_mode = MEMORY_MODE_DEFAULT; static struct option long_options[] = { diff --git a/common/memory.h b/common/memory.h index 814d81e..cefbf82 100644 --- a/common/memory.h +++ b/common/memory.h @@ -16,6 +16,13 @@ * limitations under the License. */ +enum +{ + MEMORY_MODE_DEFAULT, + MEMORY_MODE_FREE_MEMORY, + MEMORY_MODE_USAGE_PERCENTAGE +}; + #ifndef MEMORY_H_ #define MEMORY_H_ diff --git a/osx/memory.cc b/osx/memory.cc index 35c47d6..15498d6 100644 --- a/osx/memory.cc +++ b/osx/memory.cc @@ -33,6 +33,8 @@ std::string mem_string( bool use_colors, int mode ) u_int64_t total_mem; float used_mem; float percentage_mem; + float free_mem; + float free_mem_in_gigabytes; // used to check if free mem < 1 GB //u_int64_t unused_mem; vm_size_t page_size; @@ -61,25 +63,36 @@ std::string mem_string( bool use_colors, int mode ) oss << mem_lut[( 100 * static_cast(used_mem) ) / total_mem]; } - - percentage_mem = static_cast(used_mem) / static_cast(total_mem) * 100.0; - + // Change the percision for floats, for a pretty output 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; + switch( mode ) + { + case MEMORY_MODE_FREE_MEMORY: // Show free memory in MB or GB + free_mem = total_mem - used_mem; + free_mem_in_gigabytes = convert_unit( free_mem, GIGABYTES ); - if( free_mem < 1 ) { - oss << convert_unit( free_mem, MEGABYTES ) << "MB"; - } else { - oss << convert_unit( free_mem, GIGABYTES ) << "GB"; - } - } else { - oss << percentage_mem << '%'; + // if free memory is less than 1 GB, use MB instead + if( free_mem_in_gigabytes < 1 ) + { + oss << convert_unit( free_mem, MEGABYTES ) << "MB"; + } + else + { + oss << free_mem_in_gigabytes << "GB"; + } + break; + case MEMORY_MODE_USAGE_PERCENTAGE: + // Calculate the percentage of used memory + percentage_mem = used_mem / + static_cast( total_mem ) * 100.0; + + oss << percentage_mem << '%'; + break; + default: // Default mode, just show the used/total memory in MB + oss << convert_unit( used_mem, MEGABYTES ) << '/' + << convert_unit( total_mem, MEGABYTES ) << "MB"; } if( use_colors )