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:
parent
0e89998ee4
commit
635334b141
@ -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
|
||||
|
@ -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[] =
|
||||
{
|
||||
|
@ -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_
|
||||
|
||||
|
@ -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<u_int64_t>(used_mem) ) / total_mem];
|
||||
}
|
||||
|
||||
|
||||
percentage_mem = static_cast<float>(used_mem) / static_cast<float>(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<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 )
|
||||
|
Loading…
Reference in New Issue
Block a user