Merge pull request #40 from jodavies/cpu-mode

Cpu mode
This commit is contained in:
Matt McCormick 2016-06-30 13:11:12 -04:00 committed by GitHub
commit 18b4ed638a
4 changed files with 66 additions and 6 deletions

@ -111,6 +111,10 @@ if(BUILD_TESTING)
COMMAND tmux-mem-cpu-load --colors
)
add_test(NAME colors_short
COMMAND tmux-mem-cpu-load -c
)
add_test(NAME powerline
COMMAND tmux-mem-cpu-load --powerline
)
@ -151,6 +155,22 @@ if(BUILD_TESTING)
COMMAND tmux-mem-cpu-load -a 3
)
add_test(NAME cpu_mode_0
COMMAND tmux-mem-cpu-load --cpu-mode 0
)
add_test(NAME cpu_mode_1
COMMAND tmux-mem-cpu-load --cpu-mode 1
)
add_test(NAME cpu_mode_short_0
COMMAND tmux-mem-cpu-load -t 0
)
add_test(NAME cpu_mode_short_1
COMMAND tmux-mem-cpu-load -t 1
)
set_tests_properties(usage
invalid_status_interval
invalid_graph_lines

@ -116,7 +116,7 @@ The full usage::
Available options:
-h, --help
Prints this help message
--colors
-c, --colors
Use tmux colors in output
--powerline-right
Use powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors
@ -126,6 +126,8 @@ The full usage::
Set how many lines should be drawn in a graph. Default: 10
-m <value>, --mem-mode <value>
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>
Set how many load-averages should be drawn. Default: 3
@ -147,6 +149,7 @@ Contributions from:
* Travil Heller <trav.heller@gmail.com>
* Tony Narlock <tony@git-pull.com>
* Compilenix <Compilenix@compilenix.org>
* jodavies <jodavies1010@gmail.com>
.. _tmux: http://tmux.sourceforge.net/

@ -47,4 +47,17 @@
float cpu_percentage( unsigned );
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

@ -35,11 +35,12 @@
#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 )
{
float percentage;
float multiplier = 1.0f;
//output stuff
std::ostringstream oss;
@ -49,6 +50,18 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
// get %
percentage = cpu_percentage( cpu_usage_delay );
// set multiplier to number of threads ?
if ( cpu_mode == CPU_MODE_THREADS )
{
multiplier = get_cpu_count();
}
// if percentage*multiplier >= 100, remove decimal point to keep number short
if ( percentage*multiplier >= 100.0f )
{
oss.precision( 0 );
}
if( use_colors )
{
unsigned int percent = static_cast<unsigned int>( percentage );
@ -62,7 +75,7 @@ std::string cpu_string( unsigned int cpu_usage_delay, unsigned int graph_lines,
oss << "]";
}
oss.width( 5 );
oss << percentage;
oss << percentage * multiplier;
oss << "%";
if( use_colors )
{
@ -90,7 +103,6 @@ void print_help()
<< "-h, --help\n"
<< "\t Prints this help message\n"
<< "-c, --colors\n"
<< "--colors\n"
<< "\tUse tmux colors in output\n"
<< "-p, --powerline-right\n"
<< "\tUse powerline symbols throughout the output, DO NOT reset background color at the end, enables --colors\n"
@ -100,6 +112,8 @@ void print_help()
<< "\tSet how many lines should be drawn in a graph. Default: 10\n"
<< "-m <value>, --mem-mode <value>\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"
<< "\tSet how many load-averages should be drawn. Default: 3\n"
<< endl;
@ -113,6 +127,7 @@ int main( int argc, char** argv )
bool use_colors = false;
bool use_powerline = false;
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
static struct option long_options[] =
{
@ -126,13 +141,14 @@ int main( int argc, char** argv )
{ "interval", required_argument, NULL, 'i' },
{ "graph-lines", required_argument, NULL, 'g' },
{ "mem-mode", required_argument, NULL, 'm' },
{ "cpu-mode", required_argument, NULL, 't' },
{ "averages-count", required_argument, NULL, 'a' },
{ 0, 0, 0, 0 } // used to handle unknown long options
};
int c;
// while c != -1
while( (c = getopt_long( argc, argv, "hi:g:m:a:", long_options, NULL) ) != -1 )
while( (c = getopt_long( argc, argv, "hi:cg:m:a:t:", long_options, NULL) ) != -1 )
{
switch( c )
{
@ -171,6 +187,14 @@ int main( int argc, char** argv )
}
mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) );
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
if( atoi( optarg ) < 0 || atoi( optarg ) > 3 )
{
@ -200,7 +224,7 @@ int main( int argc, char** argv )
MemoryStatus memory_status;
mem_status( memory_status );
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 );
return EXIT_SUCCESS;