Adhere to the styling guide, added enum for memory modes, refractored memory mode if block to use switch instead and added test cases for free memory and user percentage memory modes

This commit is contained in:
cousine 2016-02-10 22:49:49 +02:00
parent 0e89998ee4
commit 635334b141
4 changed files with 44 additions and 16 deletions

@ -123,6 +123,14 @@ if(BUILD_TESTING)
COMMAND tmux-mem-cpu-load 2 8 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 set_tests_properties(usage
invalid_status_interval invalid_status_interval
invalid_graph_lines invalid_graph_lines

@ -93,7 +93,7 @@ int main( int argc, char** argv )
unsigned cpu_usage_delay = 990000; unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough short graph_lines = 10; // max 32767 should be enough
bool use_colors = false; bool use_colors = false;
int mem_mode = 0; int mem_mode = MEMORY_MODE_DEFAULT;
static struct option long_options[] = static struct option long_options[] =
{ {

@ -16,6 +16,13 @@
* limitations under the License. * limitations under the License.
*/ */
enum
{
MEMORY_MODE_DEFAULT,
MEMORY_MODE_FREE_MEMORY,
MEMORY_MODE_USAGE_PERCENTAGE
};
#ifndef MEMORY_H_ #ifndef MEMORY_H_
#define MEMORY_H_ #define MEMORY_H_

@ -33,6 +33,8 @@ std::string mem_string( bool use_colors, int mode )
u_int64_t total_mem; u_int64_t total_mem;
float used_mem; float used_mem;
float percentage_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; //u_int64_t unused_mem;
vm_size_t page_size; vm_size_t page_size;
@ -61,25 +63,36 @@ std::string mem_string( bool use_colors, int mode )
oss << mem_lut[( 100 * static_cast<u_int64_t>(used_mem) ) / total_mem]; oss << mem_lut[( 100 * static_cast<u_int64_t>(used_mem) ) / total_mem];
} }
// Change the percision for floats, for a pretty output
percentage_mem = static_cast<float>(used_mem) / static_cast<float>(total_mem) * 100.0;
oss.precision( 2 ); oss.precision( 2 );
oss.setf( std::ios::fixed | std::ios::right ); oss.setf( std::ios::fixed | std::ios::right );
if( mode == 0 ) { switch( mode )
oss << convert_unit( used_mem, MEGABYTES ) << '/' {
<< convert_unit( total_mem, MEGABYTES ) << "MB"; case MEMORY_MODE_FREE_MEMORY: // Show free memory in MB or GB
} else if ( mode == 1 ) { free_mem = total_mem - used_mem;
float free_mem = total_mem - used_mem; free_mem_in_gigabytes = convert_unit( free_mem, GIGABYTES );
if( free_mem < 1 ) { // if free memory is less than 1 GB, use MB instead
oss << convert_unit( free_mem, MEGABYTES ) << "MB"; if( free_mem_in_gigabytes < 1 )
} else { {
oss << convert_unit( free_mem, GIGABYTES ) << "GB"; oss << convert_unit( free_mem, MEGABYTES ) << "MB";
} }
} else { else
oss << percentage_mem << '%'; {
oss << free_mem_in_gigabytes << "GB";
}
break;
case MEMORY_MODE_USAGE_PERCENTAGE:
// Calculate the percentage of used memory
percentage_mem = used_mem /
static_cast<float>( 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 ) if( use_colors )