added memory mode to calculate %, and free memory

This commit is contained in:
cousine 2016-02-10 17:32:01 +02:00
parent 859c60976a
commit 451aed4530
3 changed files with 37 additions and 9 deletions

@ -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 );

@ -21,6 +21,6 @@
#include <string>
std::string mem_string( bool );
std::string mem_string( bool, int );
#endif

@ -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<u_int64_t>( vm_stats.free_count * page_size );
used_mem = static_cast<u_int64_t>(
used_mem = static_cast<float>(
( 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<u_int64_t>(used_mem) ) / total_mem];
}
oss << convert_unit( used_mem, MEGABYTES ) << '/'
<< convert_unit( total_mem, MEGABYTES ) << "MB";
percentage_mem = static_cast<float>(used_mem) / static_cast<float>(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 )
{