tmux-mem-cpu-load/tmux-mem-cpu-load.cpp

141 lines
3.8 KiB
C++
Raw Normal View History

/*
* 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.
* */
2013-02-21 00:08:14 -05:00
#include <cstring>
2009-09-09 02:28:04 -04:00
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h> // sleep
#include <cmath> // for floorf
2013-06-09 21:01:32 -04:00
#include <cstdlib> // EXIT_SUCCESS
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"
#if defined(__APPLE__) && defined(__MACH__)
2014-04-14 14:51:46 -04:00
// Apple osx system
#include "osx/cpu.h"
#include "osx/memory.h"
#include "osx/load.h"
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
// BSD system
// TODO: Includes and *BSD support
#define BSD_BASED 1
// include _get_cpu_percentage (see osx/cpu.cc)
// include cpu_percentage (see osx/cpu.cc)
#else
// assume linux system
#include "linux/cpu.h"
#include "linux/memory.h"
#include "linux/load.h"
#endif
2009-09-09 02:28:04 -04:00
// Function declarations.
2014-04-14 14:51:46 -04:00
// TODO: those should stay in separate headers
// LINUX: DONE/partial
// OSX: DONE/partial
// BSD: TODO
float cpu_percentage( unsigned int cpu_usage_delay );
2009-09-09 02:28:04 -04:00
std::string cpu_string( unsigned int cpu_usage_delay,
2014-04-14 14:51:46 -04:00
unsigned int graph_lines, bool use_colors = false ) {
std::string meter( graph_lines + 2, ' ' );
meter[0] = '[';
meter[meter.length() - 1] = ']';
2009-09-09 02:28:04 -04:00
int meter_count = 0;
float percentage;
std::ostringstream oss;
oss.precision( 1 );
oss.setf( std::ios::fixed | std::ios::right );
percentage = cpu_percentage( cpu_usage_delay );
float meter_step = 99.9 / graph_lines;
meter_count = 1;
2014-04-14 14:51:46 -04:00
while(meter_count*meter_step < percentage) {
meter[meter_count] = '|';
meter_count++;
2009-09-09 02:28:04 -04:00
}
if( use_colors )
oss << cpu_percentage_lut[static_cast<unsigned int>( percentage )];
2014-04-14 14:51:46 -04:00
oss << meter;
oss.width( 5 );
oss << percentage;
oss << "%";
if( use_colors )
oss << "#[fg=default,bg=default]";
2010-02-25 00:37:21 -05:00
2010-02-24 20:02:55 -05:00
return oss.str();
}
2014-04-14 14:51:46 -04:00
int main(int argc, char** argv) {
unsigned int cpu_usage_delay = 900000;
int graph_lines = 10;
2013-02-21 00:08:14 -05:00
bool use_colors = false;
2014-04-14 14:51:46 -04:00
try {
std::istringstream iss;
iss.exceptions ( std::ifstream::failbit | std::ifstream::badbit );
2013-02-21 00:08:14 -05:00
std::string current_arg;
unsigned int arg_index = 1;
if( argc > arg_index )
2013-02-20 23:15:20 -05:00
{
2013-02-21 00:08:14 -05:00
if( strcmp( argv[arg_index], "--colors" ) == 0 )
{
use_colors = true;
++arg_index;
}
}
if( argc > arg_index )
{
iss.str( argv[arg_index] );
2013-06-09 22:24:28 -04:00
int status_interval;
iss >> status_interval;
2013-06-09 22:24:28 -04:00
if( status_interval < 1 )
{
std::cerr << "Status interval argument must be one or greater." << std::endl;
return EXIT_FAILURE;
}
cpu_usage_delay = status_interval * 1000000 - 100000;
2013-02-21 00:08:14 -05:00
++arg_index;
2013-02-20 23:15:20 -05:00
}
2013-02-21 00:08:14 -05:00
if( argc > arg_index )
2013-02-20 23:15:20 -05:00
{
2013-02-21 00:08:14 -05:00
iss.str( argv[arg_index] );
2013-02-20 23:15:20 -05:00
iss.clear();
iss >> graph_lines;
if( graph_lines < 1 )
{
2013-06-09 22:24:28 -04:00
std::cerr << "Graph lines argument must be one or greater." << std::endl;
2013-06-09 21:01:32 -04:00
return EXIT_FAILURE;
}
2013-02-20 23:15:20 -05:00
}
}
catch(const std::exception &e)
2013-02-20 23:15:20 -05:00
{
std::cerr << "Usage: " << argv[0] << " [--colors] [tmux_status-interval(seconds)] [graph lines]" << std::endl;
2013-06-09 21:01:32 -04:00
return EXIT_FAILURE;
2013-02-20 23:15:20 -05:00
}
std::cout << mem_string( use_colors ) << ' ' << cpu_string( cpu_usage_delay, graph_lines, use_colors ) << ' ' << load_string( use_colors );
2013-06-09 21:01:32 -04:00
return EXIT_SUCCESS;
2009-09-09 02:28:04 -04:00
}