Added cpu-mode option

Default as before, full load = 100%.
Option 1, full load = 100% * number of cpu threads.
This commit is contained in:
Josh Davies 2016-06-26 14:31:34 +01:00
parent 3e12a64309
commit c623ca889d
3 changed files with 37 additions and 5 deletions

@ -126,6 +126,8 @@ The full usage::
Set how many lines should be drawn in a graph. Default: 10 Set how many lines should be drawn in a graph. Default: 10
-m <value>, --mem-mode <value> -m <value>, --mem-mode <value>
Set memory display mode. 0: Default, 1: Free memory, 2: Usage percent. Set memory display mode. 0: Default, 1: Free memory, 2: Usage percent.
-t <value>, --cpu-mode <value>
Set cpu % display mode. 0: Default max 100%, 1: Max 100% * number of threads.
-a <value>, --averages-count <value> -a <value>, --averages-count <value>
Set how many load-averages should be drawn. Default: 3 Set how many load-averages should be drawn. Default: 3

@ -47,4 +47,17 @@
float cpu_percentage( unsigned ); float cpu_percentage( unsigned );
uint8_t get_cpu_count(); uint8_t get_cpu_count();
/** CPU percentage output mode.
*
* Examples:
*
* CPU_MODE_DEFAULT: 100%
* CPU_MODE_THREADS: 800% (8 cores, fully loaded)
*/
enum CPU_MODE
{
CPU_MODE_DEFAULT,
CPU_MODE_THREADS
};
#endif #endif

@ -35,11 +35,12 @@
#include "powerline.h" #include "powerline.h"
std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines, std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines,
bool use_colors = false, bool use_powerline = false ) bool use_colors = false, bool use_powerline = false )
{ {
float percentage; float percentage;
float multiplier = 1.0f;
//output stuff //output stuff
std::ostringstream oss; std::ostringstream oss;
@ -49,6 +50,11 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
// get % // get %
percentage = cpu_percentage( cpu_usage_delay ); percentage = cpu_percentage( cpu_usage_delay );
// set multiplier to number of threads ?
if ( cpu_mode == CPU_MODE_THREADS ) {
multiplier = get_cpu_count();
}
if( use_colors ) if( use_colors )
{ {
unsigned int percent = static_cast<unsigned int>( percentage ); unsigned int percent = static_cast<unsigned int>( percentage );
@ -62,7 +68,7 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
oss << "]"; oss << "]";
} }
oss.width( 5 ); oss.width( 5 );
oss << percentage; oss << percentage * multiplier;
oss << "%"; oss << "%";
if( use_colors ) if( use_colors )
{ {
@ -90,7 +96,6 @@ void print_help()
<< "-h, --help\n" << "-h, --help\n"
<< "\t Prints this help message\n" << "\t Prints this help message\n"
<< "-c, --colors\n" << "-c, --colors\n"
<< "--colors\n"
<< "\tUse tmux colors in output\n" << "\tUse tmux colors in output\n"
<< "-p, --powerline-right\n" << "-p, --powerline-right\n"
<< "\tUse powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors\n" << "\tUse powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors\n"
@ -100,6 +105,8 @@ void print_help()
<< "\tSet how many lines should be drawn in a graph. Default: 10\n" << "\tSet how many lines should be drawn in a graph. Default: 10\n"
<< "-m <value>, --mem-mode <value>\n" << "-m <value>, --mem-mode <value>\n"
<< "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n" << "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n"
<< "-t <value>, --cpu-mode <value>\n"
<< "\tSet cpu % display mode. 0: Default max 100%, 1: Max 100% * number of threads. \n"
<< "-a <value>, --averages-count <value>\n" << "-a <value>, --averages-count <value>\n"
<< "\tSet how many load-averages should be drawn. Default: 3\n" << "\tSet how many load-averages should be drawn. Default: 3\n"
<< endl; << endl;
@ -113,6 +120,7 @@ int main( int argc, char** argv )
bool use_colors = false; bool use_colors = false;
bool use_powerline = false; bool use_powerline = false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT; MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
static struct option long_options[] = static struct option long_options[] =
{ {
@ -126,13 +134,14 @@ int main( int argc, char** argv )
{ "interval", required_argument, NULL, 'i' }, { "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' }, { "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' }, { "mem-mode", required_argument, NULL, 'm' },
{ "cpu-mode", required_argument, NULL, 't' },
{ "averages-count", required_argument, NULL, 'a' }, { "averages-count", required_argument, NULL, 'a' },
{ 0, 0, 0, 0 } // used to handle unknown long options { 0, 0, 0, 0 } // used to handle unknown long options
}; };
int c; int c;
// while c != -1 // while c != -1
while( (c = getopt_long( argc, argv, "hi:g:m:a:", long_options, NULL) ) != -1 ) while( (c = getopt_long( argc, argv, "hi:g:m:a:t:", long_options, NULL) ) != -1 )
{ {
switch( c ) switch( c )
{ {
@ -171,6 +180,14 @@ int main( int argc, char** argv )
} }
mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) ); mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) );
break; break;
case 't': // --cpu-mode, -t
if( atoi( optarg ) < 0 )
{
std::cerr << "CPU mode argument must be zero or greater.\n";
return EXIT_FAILURE;
}
cpu_mode = static_cast< CPU_MODE >( atoi( optarg ) );
break;
case 'a': // --averages-count, -a case 'a': // --averages-count, -a
if( atoi( optarg ) < 0 || atoi( optarg ) > 3 ) if( atoi( optarg ) < 0 || atoi( optarg ) > 3 )
{ {
@ -200,7 +217,7 @@ int main( int argc, char** argv )
MemoryStatus memory_status; MemoryStatus memory_status;
mem_status( memory_status ); mem_status( memory_status );
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline ) std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline )
<< cpu_string( cpu_usage_delay, graph_lines, use_colors, use_powerline ) << cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline )
<< load_string( use_colors, use_powerline, averages_count ); << load_string( use_colors, use_powerline, averages_count );
return EXIT_SUCCESS; return EXIT_SUCCESS;