Merge branch 'l0ner' of github.com:l0ner/tmux-mem-cpu-load into l0ner

This commit is contained in:
Pawel "l0ner" Soltys 2015-01-10 17:32:45 +01:00
commit ad096cbac6
4 changed files with 30 additions and 41 deletions

@ -67,6 +67,9 @@ if( BUILD_TESTING )
add_test( NAME no_arguments add_test( NAME no_arguments
COMMAND tmux-mem-cpu-load ) COMMAND tmux-mem-cpu-load )
add_test( NAME custom_interval
COMMAND tmux-mem-cpu-load -i 3 )
add_test( NAME colors add_test( NAME colors
COMMAND tmux-mem-cpu-load --colors ) COMMAND tmux-mem-cpu-load --colors )
@ -74,7 +77,7 @@ if( BUILD_TESTING )
COMMAND tmux-mem-cpu-load -i -1 ) COMMAND tmux-mem-cpu-load -i -1 )
add_test( NAME invalid_graph_lines add_test( NAME invalid_graph_lines
COMMAND tmux-mem-cpu-load -g -4 ) COMMAND tmux-mem-cpu-load --graph_lines -2 )
set_tests_properties( usage set_tests_properties( usage
invalid_status_interval invalid_status_interval

@ -1,23 +1,24 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <unistd.h> // sysconf()?
#include <unistd.h> #include <sys/sysinfo.h>
#include <linux/kernel.h> // SI_LOAD_SHIFT
#include "../luts.h" #include "../luts.h"
std::string load_string( bool use_colors = false ) { std::string load_string( bool use_colors = false ) {
std::ostringstream oss; std::ostringstream oss;
std::ifstream loadavg_file( "/proc/loadavg" ); float f = static_cast<float>(1 << SI_LOAD_SHIFT);
std::string load_line;
std::getline( loadavg_file, load_line ); struct sysinfo sinfo;
loadavg_file.close(); sysinfo(&sinfo);
if( use_colors ) { if( use_colors ) {
// Likely does not work on BSD, but not tested // Likely does not work on BSD, but not tested
unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN ); unsigned number_of_cpus = sysconf( _SC_NPROCESSORS_ONLN );
float recent_load = stof(load_line.substr( 0, 4 )); float recent_load = sinfo.loads[0] / f;
// colors range from zero to twice the number of cpu's // colors range from zero to twice the number of cpu's
// for the most recent load metric // for the most recent load metric
@ -30,7 +31,13 @@ std::string load_string( bool use_colors = false ) {
oss << load_lut[load_percent]; oss << load_lut[load_percent];
} }
oss << load_line.substr( 0, 14 ); // set precision so we get results like "0.22"
oss.setf( std::ios::fixed );
oss.precision(2);
oss << sinfo.loads[0] / f << " " << sinfo.load[1] / f << " "
<< sinfo.load[2] / f;
if( use_colors ) if( use_colors )
oss << "#[fg=default,bg=default]"; oss << "#[fg=default,bg=default]";

@ -1,6 +1,5 @@
#include <string>
#include <sstream> #include <sstream>
#include <fstream> #include <sys/sysinfo.h>
#include "memory.h" #include "memory.h"
#include "../luts.h" #include "../luts.h"
@ -8,33 +7,13 @@
std::string mem_string( bool use_colors = false ) { std::string mem_string( bool use_colors = false ) {
std::ostringstream oss; std::ostringstream oss;
unsigned int total_mem; struct sysinfo sinfo;
unsigned int used_mem; sysinfo(&sinfo);
unsigned int unused_mem;
size_t substrStart; unsigned int total_mem = sinfo.totalram / 1014;
size_t substrLen; unsigned int used_mem = total_mem - sinfo.freeram / 1024;
// we don't need this for now
std::ifstream meminfo_file( "/proc/meminfo" ); //unsigned int unused_mem = sinfo.freeram / 1024;
std::string mem_line;
getline( meminfo_file, mem_line );
substrStart = mem_line.find_first_of( ':' ) + 1;
substrLen = mem_line.find_first_of( 'k' );
total_mem = stoi(mem_line.substr(substrStart, substrLen));
used_mem = total_mem;
for( unsigned int i = 0; i < 3; i++ ) {
getline( meminfo_file, mem_line );
substrStart = mem_line.find_first_of( ':' ) + 1;
substrLen = mem_line.find_first_of( 'k' );
unused_mem = stoi(mem_line.substr(substrStart, substrLen));
used_mem -= unused_mem;
}
meminfo_file.close();
if( use_colors ) { if( use_colors ) {
oss << mem_lut[(100 * used_mem) / total_mem]; oss << mem_lut[(100 * used_mem) / total_mem];

@ -87,7 +87,7 @@ std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines,
int main(int argc, char** argv) { int main(int argc, char** argv) {
using namespace ArgvParse; using namespace ArgvParse;
unsigned cpu_usage_delay = 1000000; unsigned cpu_usage_delay = 990000;
short graph_lines = 10; // max 32767 should be enough short graph_lines = 10; // max 32767 should be enough
bool use_colors = false; bool use_colors = false;
@ -129,7 +129,7 @@ int main(int argc, char** argv) {
std::cerr << "Status interval argument must be one or greater.\n"; std::cerr << "Status interval argument must be one or greater.\n";
return EXIT_FAILURE; return EXIT_FAILURE;
} }
cpu_usage_delay = delay * 1000000; cpu_usage_delay = delay * 1000000 - 10000;
} }
if (arg.foundOption("graph-lines")) { if (arg.foundOption("graph-lines")) {