Merge branch 'l0ner' of github.com:l0ner/tmux-mem-cpu-load into l0ner
This commit is contained in:
commit
ad096cbac6
@ -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
|
||||
|
@ -1,23 +1,24 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <unistd.h>
|
||||
#include <unistd.h> // sysconf()?
|
||||
#include <sys/sysinfo.h>
|
||||
#include <linux/kernel.h> // 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<float>(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
|
||||
@ -30,7 +31,13 @@ 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]";
|
||||
|
||||
|
@ -1,6 +1,5 @@
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <fstream>
|
||||
#include <sys/sysinfo.h>
|
||||
|
||||
#include "memory.h"
|
||||
#include "../luts.h"
|
||||
@ -8,33 +7,13 @@
|
||||
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);
|
||||
|
||||
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 );
|
||||
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();
|
||||
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;
|
||||
|
||||
if( use_colors ) {
|
||||
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) {
|
||||
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;
|
||||
|
||||
@ -129,7 +129,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")) {
|
||||
|
Loading…
Reference in New Issue
Block a user