From 5001bc4fe7e0466ea08253ec7ff4667ebb0ace1b Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Thu, 8 Jan 2015 20:04:27 +0100 Subject: [PATCH 1/4] added custom interval test --- CMakeLists.txt | 5 ++++- tmux-mem-cpu-load.cpp | 4 ++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 190ed8b..eef1146 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,9 @@ if( BUILD_TESTING ) add_test( NAME no_arguments COMMAND tmux-mem-cpu-load ) + add_test( NAME custom_interval + COMMAND tmux-mem-cpu-load -i 3 ) + add_test( NAME colors COMMAND tmux-mem-cpu-load --colors ) @@ -74,7 +77,7 @@ if( BUILD_TESTING ) COMMAND tmux-mem-cpu-load -i -1 ) 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 invalid_status_interval diff --git a/tmux-mem-cpu-load.cpp b/tmux-mem-cpu-load.cpp index 85b008b..4e343db 100644 --- a/tmux-mem-cpu-load.cpp +++ b/tmux-mem-cpu-load.cpp @@ -85,7 +85,7 @@ std::string cpu_string(unsigned int cpu_usage_delay, unsigned int graph_lines, int main(int argc, char** argv) { using namespace ArgvParse; - unsigned cpu_usage_delay = 1000000; + unsigned cpu_usage_delay = 990000; short graph_lines = 10; // max 32767 should be enough bool use_colors = false; @@ -127,7 +127,7 @@ int main(int argc, char** argv) { std::cerr << "Status interval argument must be one or greater.\n"; return EXIT_FAILURE; } - cpu_usage_delay = delay * 1000000; + cpu_usage_delay = delay * 1000000 - 10000; } if (arg.foundOption("graph-lines")) { From b02d0000e965074fcbb45a8f4f13f78c70cd3e81 Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Fri, 9 Jan 2015 14:10:26 +0100 Subject: [PATCH 2/4] pulled Joe136 solution to MemAvailable being present in /proc/meminfo on some systems --- linux/memory.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/linux/memory.cc b/linux/memory.cc index 6497ccb..56d76ad 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -28,10 +28,17 @@ std::string mem_string( bool use_colors = false ) { 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; + // accomodate MemAvailable potentially being in lines 2-4 of + // /proc/meminfo. do this in a way to not break the original logic of the + // loop + if( mem_line.find("MemAvailable") == 0 ) + i--; + else { + 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(); From e7f2dd25e82c6ec6fc596e686c2f835f51e22eef Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Fri, 9 Jan 2015 14:52:50 +0100 Subject: [PATCH 3/4] use sysinfo() to get ram stats instead of parsing meminfo file. Idea pulled from seculabird gihub user. see: https://github.com/secularbird/tmux-mem-cpu-load/commit/880f8b196749ad2ed0ed376db0ba4da0595eeead --- linux/memory.cc | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) diff --git a/linux/memory.cc b/linux/memory.cc index 56d76ad..8e9d33f 100644 --- a/linux/memory.cc +++ b/linux/memory.cc @@ -1,6 +1,5 @@ -#include #include -#include +#include #include "memory.h" #include "../luts.h" @@ -8,41 +7,14 @@ std::string mem_string( bool use_colors = false ) { std::ostringstream oss; - unsigned int total_mem; - unsigned int used_mem; - unsigned int unused_mem; + struct sysinfo sinfo; + sysinfo(&sinfo); + + unsigned int total_mem = sinfo.totalram / 1014; + unsigned int used_mem = total_mem - sinfo.freeram / 1024; + // we don't need this for now + //unsigned int unused_mem = sinfo.freeram / 1024; - size_t substrStart; - size_t substrLen; - - std::ifstream meminfo_file( "/proc/meminfo" ); - - 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 ); - // accomodate MemAvailable potentially being in lines 2-4 of - // /proc/meminfo. do this in a way to not break the original logic of the - // loop - if( mem_line.find("MemAvailable") == 0 ) - i--; - else { - 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 ) { oss << mem_lut[(100 * used_mem) / total_mem]; } From 220d260673bfcce8a156329952386e793b28919e Mon Sep 17 00:00:00 2001 From: "Pawel \"l0ner\" Soltys" Date: Sat, 10 Jan 2015 17:27:54 +0100 Subject: [PATCH 4/4] use sysinfo() to get load avg instead of parsing /proc/ file --- linux/load.cc | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/linux/load.cc b/linux/load.cc index df4a54f..73d1779 100644 --- a/linux/load.cc +++ b/linux/load.cc @@ -1,23 +1,24 @@ #include #include -#include -#include +#include // sysconf()? +#include +#include // SI_LOAD_SHIFT #include "../luts.h" std::string load_string( bool use_colors = false ) { std::ostringstream oss; - std::ifstream loadavg_file( "/proc/loadavg" ); - std::string load_line; - std::getline( loadavg_file, load_line ); - loadavg_file.close(); + float f = static_cast(1 << SI_LOAD_SHIFT); + + struct sysinfo sinfo; + sysinfo(&sinfo); if( use_colors ) { // Likely does not work on BSD, but not tested 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 // for the most recent load metric @@ -29,8 +30,14 @@ std::string load_string( bool use_colors = false ) { 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 ) oss << "#[fg=default,bg=default]";