2015-01-18 08:31:04 -05:00
|
|
|
/* vim: tabstop=2 shiftwidth=2 expandtab textwidth=80 linebreak wrap
|
|
|
|
*
|
2012-11-25 12:19:50 -05:00
|
|
|
* Copyright 2012 Matthew McCormick
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2015-01-18 08:31:04 -05:00
|
|
|
*/
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2013-02-21 00:08:14 -05:00
|
|
|
#include <cstring>
|
2015-01-07 08:15:08 -05:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2009-09-09 02:28:04 -04:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2015-01-19 11:17:38 -05:00
|
|
|
#include <cstdlib> // EXIT_SUCCESS, atoi()
|
|
|
|
#include <getopt.h> // getopt_long
|
2015-01-18 08:31:04 -05:00
|
|
|
|
2015-01-18 14:04:28 -05:00
|
|
|
#include "version.h"
|
2015-01-18 08:31:04 -05:00
|
|
|
#include "graph.h"
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2014-04-14 14:51:46 -04:00
|
|
|
// Tmux color lookup tables for the different metrics.
|
|
|
|
#include "luts.h"
|
|
|
|
|
2015-02-19 13:47:45 -05:00
|
|
|
#include "cpu.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "load.h"
|
2009-09-09 02:28:04 -04:00
|
|
|
|
2016-03-05 05:19:31 -05:00
|
|
|
#include "powerline.h"
|
|
|
|
|
2016-06-26 09:31:34 -04:00
|
|
|
std::string cpu_string( CPU_MODE cpu_mode, unsigned int cpu_usage_delay, unsigned int graph_lines,
|
2016-07-31 21:57:56 -04:00
|
|
|
bool use_colors = false,
|
|
|
|
bool use_powerline_left = false, bool use_powerline_right = false )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-04 11:23:39 -05:00
|
|
|
float percentage;
|
2016-06-26 09:31:34 -04:00
|
|
|
float multiplier = 1.0f;
|
2014-04-19 18:36:59 -04:00
|
|
|
|
2015-01-07 14:57:25 -05:00
|
|
|
//output stuff
|
2013-04-24 22:13:50 -04:00
|
|
|
std::ostringstream oss;
|
2010-02-24 19:49:10 -05:00
|
|
|
oss.precision( 1 );
|
2013-04-24 22:13:50 -04:00
|
|
|
oss.setf( std::ios::fixed | std::ios::right );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2014-04-19 18:36:59 -04:00
|
|
|
// get %
|
2010-02-25 09:54:18 -05:00
|
|
|
percentage = cpu_percentage( cpu_usage_delay );
|
2010-02-24 19:49:10 -05:00
|
|
|
|
2016-06-26 09:31:34 -04:00
|
|
|
// set multiplier to number of threads ?
|
2016-06-27 03:00:09 -04:00
|
|
|
if ( cpu_mode == CPU_MODE_THREADS )
|
|
|
|
{
|
2016-06-26 09:31:34 -04:00
|
|
|
multiplier = get_cpu_count();
|
|
|
|
}
|
|
|
|
|
2016-06-27 03:00:09 -04:00
|
|
|
// if percentage*multiplier >= 100, remove decimal point to keep number short
|
|
|
|
if ( percentage*multiplier >= 100.0f )
|
|
|
|
{
|
|
|
|
oss.precision( 0 );
|
|
|
|
}
|
|
|
|
|
2016-07-31 21:57:56 -04:00
|
|
|
unsigned int percent = static_cast<unsigned int>( percentage );
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2016-07-31 21:57:56 -04:00
|
|
|
if( use_powerline_right )
|
|
|
|
{
|
|
|
|
powerline( oss, cpu_percentage_lut[percent], POWERLINE_RIGHT );
|
|
|
|
}
|
|
|
|
else if( use_powerline_left )
|
|
|
|
{
|
|
|
|
powerline( oss, cpu_percentage_lut[percent], POWERLINE_LEFT );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
powerline( oss, cpu_percentage_lut[percent], NONE );
|
|
|
|
}
|
2015-01-18 08:31:04 -05:00
|
|
|
}
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-18 08:31:04 -05:00
|
|
|
if( graph_lines > 0)
|
|
|
|
{
|
2015-02-12 13:59:22 -05:00
|
|
|
oss << " [";
|
2015-01-18 08:31:04 -05:00
|
|
|
oss << get_graph_by_percentage( unsigned( percentage ), graph_lines );
|
|
|
|
oss << "]";
|
2015-01-10 11:32:08 -05:00
|
|
|
}
|
2010-02-24 19:49:10 -05:00
|
|
|
oss.width( 5 );
|
2016-06-26 09:31:34 -04:00
|
|
|
oss << percentage * multiplier;
|
2010-02-24 19:49:10 -05:00
|
|
|
oss << "%";
|
2013-04-30 06:38:39 -04:00
|
|
|
if( use_colors )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2016-07-31 21:57:56 -04:00
|
|
|
if( use_powerline_left )
|
2016-04-14 06:18:10 -04:00
|
|
|
{
|
2016-07-31 21:57:56 -04:00
|
|
|
powerline( oss, cpu_percentage_lut[percent], POWERLINE_LEFT, true );
|
2016-04-14 06:18:10 -04:00
|
|
|
}
|
2016-07-31 21:57:56 -04:00
|
|
|
else if( !use_powerline_right )
|
2016-04-14 06:18:10 -04:00
|
|
|
{
|
2016-03-05 05:19:31 -05:00
|
|
|
oss << "#[fg=default,bg=default]";
|
2016-04-14 06:18:10 -04:00
|
|
|
}
|
2015-01-18 08:31:04 -05:00
|
|
|
}
|
2010-02-25 00:37:21 -05:00
|
|
|
|
2010-02-24 20:02:55 -05:00
|
|
|
return oss.str();
|
|
|
|
}
|
|
|
|
|
2015-01-19 11:17:38 -05:00
|
|
|
void print_help()
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2015-01-19 11:17:38 -05:00
|
|
|
using std::cout;
|
|
|
|
using std::endl;
|
2015-01-19 22:17:36 -05:00
|
|
|
|
2015-01-19 11:17:38 -05:00
|
|
|
cout << "tmux-mem-cpu-load v" << tmux_mem_cpu_load_VERSION << endl
|
|
|
|
<< "Usage: tmux-mem-cpu-load [OPTIONS]\n\n"
|
|
|
|
<< "Available options:\n"
|
|
|
|
<< "-h, --help\n"
|
|
|
|
<< "\t Prints this help message\n"
|
2016-03-05 05:19:31 -05:00
|
|
|
<< "-c, --colors\n"
|
2015-01-19 11:17:38 -05:00
|
|
|
<< "\tUse tmux colors in output\n"
|
2016-07-31 22:03:18 -04:00
|
|
|
<< "-p, --powerline-left\n"
|
2016-07-31 22:06:56 -04:00
|
|
|
<< "\tUse powerline left symbols throughout the output, enables --colors\n"
|
2016-07-31 22:03:18 -04:00
|
|
|
<< "-q, --powerline-right\n"
|
2016-07-31 22:06:56 -04:00
|
|
|
<< "\tUse powerline right symbols throughout the output, enables --colors\n"
|
2015-01-19 22:17:36 -05:00
|
|
|
<< "-i <value>, --interval <value>\n"
|
2015-01-19 11:17:38 -05:00
|
|
|
<< "\tSet tmux status refresh interval in seconds. Default: 1 second\n"
|
2015-01-19 22:17:36 -05:00
|
|
|
<< "-g <value>, --graph-lines <value>\n"
|
2015-01-19 11:17:38 -05:00
|
|
|
<< "\tSet how many lines should be drawn in a graph. Default: 10\n"
|
2016-02-14 23:52:56 -05:00
|
|
|
<< "-m <value>, --mem-mode <value>\n"
|
|
|
|
<< "\tSet memory display mode. 0: Default, 1: Free memory, 2: Usage percent.\n"
|
2016-06-26 09:31:34 -04:00
|
|
|
<< "-t <value>, --cpu-mode <value>\n"
|
|
|
|
<< "\tSet cpu % display mode. 0: Default max 100%, 1: Max 100% * number of threads. \n"
|
2016-04-28 16:05:43 -04:00
|
|
|
<< "-a <value>, --averages-count <value>\n"
|
|
|
|
<< "\tSet how many load-averages should be drawn. Default: 3\n"
|
2015-01-19 11:17:38 -05:00
|
|
|
<< endl;
|
|
|
|
}
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-19 11:17:38 -05:00
|
|
|
int main( int argc, char** argv )
|
|
|
|
{
|
2015-01-08 14:04:27 -05:00
|
|
|
unsigned cpu_usage_delay = 990000;
|
2016-04-28 16:05:43 -04:00
|
|
|
short averages_count = 3;
|
2015-01-08 13:53:22 -05:00
|
|
|
short graph_lines = 10; // max 32767 should be enough
|
2013-02-21 00:08:14 -05:00
|
|
|
bool use_colors = false;
|
2016-07-31 21:57:56 -04:00
|
|
|
bool use_powerline_left = false;
|
|
|
|
bool use_powerline_right = false;
|
2016-02-10 16:47:13 -05:00
|
|
|
MEMORY_MODE mem_mode = MEMORY_MODE_DEFAULT;
|
2016-06-26 09:31:34 -04:00
|
|
|
CPU_MODE cpu_mode = CPU_MODE_DEFAULT;
|
2015-01-07 14:57:25 -05:00
|
|
|
|
2015-01-19 11:17:38 -05:00
|
|
|
static struct option long_options[] =
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2015-01-19 11:17:38 -05:00
|
|
|
// Struct is a s follows:
|
|
|
|
// const char * name, int has_arg, int *flag, int val
|
|
|
|
// if *flag is null, val is option identifier to use in switch()
|
|
|
|
// otherwise it's a value to set the variable *flag points to
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ "colors", no_argument, NULL, 'c' },
|
2016-07-31 21:57:56 -04:00
|
|
|
{ "powerline-left", no_argument, NULL, 'p' },
|
|
|
|
{ "powerline-right", no_argument, NULL, 'q' },
|
2015-01-19 11:17:38 -05:00
|
|
|
{ "interval", required_argument, NULL, 'i' },
|
|
|
|
{ "graph-lines", required_argument, NULL, 'g' },
|
2016-02-14 23:52:56 -05:00
|
|
|
{ "mem-mode", required_argument, NULL, 'm' },
|
2016-06-26 09:31:34 -04:00
|
|
|
{ "cpu-mode", required_argument, NULL, 't' },
|
2016-04-28 16:05:43 -04:00
|
|
|
{ "averages-count", required_argument, NULL, 'a' },
|
2015-01-19 11:17:38 -05:00
|
|
|
{ 0, 0, 0, 0 } // used to handle unknown long options
|
|
|
|
};
|
|
|
|
|
2015-01-19 22:17:36 -05:00
|
|
|
int c;
|
2015-01-19 11:17:38 -05:00
|
|
|
// while c != -1
|
2016-07-31 21:57:56 -04:00
|
|
|
while( (c = getopt_long( argc, argv, "hi:cpqg:m:a:t:", long_options, NULL) ) != -1 )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2015-01-19 11:17:38 -05:00
|
|
|
switch( c )
|
2015-01-18 08:31:04 -05:00
|
|
|
{
|
2015-01-19 11:17:38 -05:00
|
|
|
case 'h': // --help, -h
|
|
|
|
print_help();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
case 'c': // --colors
|
|
|
|
use_colors = true;
|
|
|
|
break;
|
2016-07-31 21:57:56 -04:00
|
|
|
case 'p': // --powerline-left
|
|
|
|
use_colors = true;
|
|
|
|
use_powerline_left = true;
|
|
|
|
break;
|
|
|
|
case 'q': // --powerline-right
|
2016-03-05 05:19:31 -05:00
|
|
|
use_colors = true;
|
2016-07-31 21:57:56 -04:00
|
|
|
use_powerline_right = true;
|
2016-03-05 05:19:31 -05:00
|
|
|
break;
|
2015-01-19 11:17:38 -05:00
|
|
|
case 'i': // --interval, -i
|
|
|
|
if( atoi( optarg ) < 1 )
|
|
|
|
{
|
|
|
|
std::cerr << "Status interval argument must be one or greater.\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
cpu_usage_delay = atoi( optarg ) * 1000000 - 10000;
|
|
|
|
break;
|
|
|
|
case 'g': // --graph-lines, -g
|
|
|
|
if( atoi( optarg ) < 0 )
|
|
|
|
{
|
|
|
|
std::cerr << "Graph lines argument must be zero or greater.\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
graph_lines = atoi( optarg );
|
|
|
|
break;
|
2016-02-10 10:32:01 -05:00
|
|
|
case 'm': // --mem-mode, -m
|
|
|
|
if( atoi( optarg ) < 0 )
|
|
|
|
{
|
|
|
|
std::cerr << "Memory mode argument must be zero or greater.\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-02-10 16:47:13 -05:00
|
|
|
mem_mode = static_cast< MEMORY_MODE >( atoi( optarg ) );
|
2016-02-10 10:32:01 -05:00
|
|
|
break;
|
2016-06-26 09:31:34 -04:00
|
|
|
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;
|
2016-04-28 16:05:43 -04:00
|
|
|
case 'a': // --averages-count, -a
|
|
|
|
if( atoi( optarg ) < 0 || atoi( optarg ) > 3 )
|
|
|
|
{
|
|
|
|
std::cerr << "Valid averages-count arguments are: 0, 1, 2, 3\n";
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
averages_count = atoi( optarg );
|
|
|
|
break;
|
2015-01-19 11:17:38 -05:00
|
|
|
case '?':
|
|
|
|
// getopt_long prints error message automatically
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
default:
|
2015-01-19 22:52:04 -05:00
|
|
|
std::cerr << "?? getopt returned character code 0 " << c << std::endl;
|
2015-01-19 11:17:38 -05:00
|
|
|
return EXIT_FAILURE;
|
2015-01-18 08:31:04 -05:00
|
|
|
}
|
2015-01-07 14:57:25 -05:00
|
|
|
}
|
2015-01-19 22:51:14 -05:00
|
|
|
// Detect old option specification and return and error message.
|
|
|
|
if( argc > optind )
|
|
|
|
{
|
|
|
|
std::cout <<
|
|
|
|
"The interval and graph lines options are now specified with flags.\n\n";
|
|
|
|
print_help();
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2010-08-01 11:17:42 -04:00
|
|
|
|
2016-02-10 17:22:34 -05:00
|
|
|
MemoryStatus memory_status;
|
|
|
|
mem_status( memory_status );
|
2016-07-31 21:57:56 -04:00
|
|
|
std::cout << mem_string( memory_status, mem_mode, use_colors, use_powerline_left, use_powerline_right )
|
|
|
|
<< cpu_string( cpu_mode, cpu_usage_delay, graph_lines, use_colors, use_powerline_left, use_powerline_right )
|
|
|
|
<< load_string( use_colors, use_powerline_left, use_powerline_right, averages_count );
|
2010-02-25 09:54:18 -05:00
|
|
|
|
2018-04-23 21:47:03 -04:00
|
|
|
std::cout << std::endl;
|
|
|
|
|
2013-06-09 21:01:32 -04:00
|
|
|
return EXIT_SUCCESS;
|
2009-09-09 02:28:04 -04:00
|
|
|
}
|
|
|
|
|