Move cpu usage from screen setup to tmux setup.
This commit is contained in:
parent
dc5fe29f1d
commit
83945d1a19
@ -1,10 +1,4 @@
|
|||||||
/**
|
|
||||||
* @file cpuusage.cpp
|
|
||||||
* @brief print out the percent cpu usage at a given interval
|
|
||||||
* @author Matthew McCormick (thewtex)
|
|
||||||
* @version
|
|
||||||
* @date 2009-08-12
|
|
||||||
*/
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
@ -13,125 +7,96 @@ using namespace std;
|
|||||||
|
|
||||||
#include <unistd.h> // sleep
|
#include <unistd.h> // sleep
|
||||||
|
|
||||||
// in seconds
|
float cpu_percentage()
|
||||||
const static unsigned int INTERVAL=1;
|
|
||||||
|
|
||||||
class CPUPercentage
|
|
||||||
{
|
{
|
||||||
public:
|
string stat_line;
|
||||||
CPUPercentage();
|
size_t line_start_pos;
|
||||||
~CPUPercentage();
|
size_t line_end_pos;
|
||||||
|
unsigned long long current_user;
|
||||||
|
unsigned long long current_system;
|
||||||
|
unsigned long long current_nice;
|
||||||
|
unsigned long long current_idle;
|
||||||
|
unsigned long long next_user;
|
||||||
|
unsigned long long next_system;
|
||||||
|
unsigned long long next_nice;
|
||||||
|
unsigned long long next_idle;
|
||||||
|
unsigned long long diff_user;
|
||||||
|
unsigned long long diff_system;
|
||||||
|
unsigned long long diff_nice;
|
||||||
|
unsigned long long diff_idle;
|
||||||
|
|
||||||
const float& get_percentage();
|
ifstream stat_file("/proc/stat");
|
||||||
|
getline(stat_file, stat_line);
|
||||||
private:
|
stat_file.close();
|
||||||
ifstream m_stat_file;
|
|
||||||
unsigned long long m_current_user;
|
|
||||||
unsigned long long m_current_system;
|
|
||||||
unsigned long long m_current_nice;
|
|
||||||
unsigned long long m_current_idle;
|
|
||||||
unsigned long long m_next_user;
|
|
||||||
unsigned long long m_next_system;
|
|
||||||
unsigned long long m_next_nice;
|
|
||||||
unsigned long long m_next_idle;
|
|
||||||
unsigned long long m_diff_user;
|
|
||||||
unsigned long long m_diff_system;
|
|
||||||
unsigned long long m_diff_nice;
|
|
||||||
unsigned long long m_diff_idle;
|
|
||||||
|
|
||||||
string m_stat_line;
|
|
||||||
size_t m_line_start_pos;
|
|
||||||
size_t m_line_end_pos;
|
|
||||||
istringstream m_iss;
|
|
||||||
|
|
||||||
float m_percentage;
|
|
||||||
};
|
|
||||||
|
|
||||||
CPUPercentage::CPUPercentage():
|
|
||||||
m_current_user(0),
|
|
||||||
m_current_system(0),
|
|
||||||
m_current_nice(0),
|
|
||||||
m_current_idle(0),
|
|
||||||
m_next_user(0),
|
|
||||||
m_next_system(0),
|
|
||||||
m_next_nice(0),
|
|
||||||
m_next_idle(0),
|
|
||||||
m_diff_user(0),
|
|
||||||
m_diff_system(0),
|
|
||||||
m_diff_nice(0),
|
|
||||||
m_diff_idle(0)
|
|
||||||
{
|
|
||||||
m_stat_file.exceptions(ifstream::eofbit|ifstream::failbit|ifstream::badbit);
|
|
||||||
}
|
|
||||||
|
|
||||||
CPUPercentage::~CPUPercentage()
|
|
||||||
{
|
|
||||||
if(m_stat_file.is_open())
|
|
||||||
m_stat_file.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
const float& CPUPercentage::get_percentage()
|
|
||||||
{
|
|
||||||
m_stat_file.open("/proc/stat");
|
|
||||||
getline(m_stat_file, m_stat_line);
|
|
||||||
m_stat_file.close();
|
|
||||||
|
|
||||||
// skip "cpu"
|
// skip "cpu"
|
||||||
m_line_start_pos = m_stat_line.find_first_not_of(" ", 3);
|
line_start_pos = stat_line.find_first_not_of(" ", 3);
|
||||||
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_start_pos);
|
line_end_pos = stat_line.find_first_of(' ', line_start_pos);
|
||||||
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1);
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1);
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1);
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
m_iss.str(m_stat_line.substr(m_line_start_pos, m_line_end_pos - m_line_start_pos));
|
|
||||||
m_iss >> m_next_user >> m_next_nice >> m_next_system >> m_next_idle;
|
|
||||||
m_iss.clear();
|
|
||||||
|
|
||||||
m_diff_user = m_next_user - m_current_user;
|
istringstream iss;
|
||||||
m_diff_system = m_next_system - m_current_system;
|
iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
||||||
m_diff_nice = m_next_nice - m_current_nice;
|
iss >> current_user >> current_nice >> current_system >> current_idle;
|
||||||
m_diff_idle = m_next_idle - m_current_idle;
|
iss.clear();
|
||||||
m_percentage = static_cast<float>(m_diff_user + m_diff_system + m_diff_nice)/static_cast<float>(m_diff_user + m_diff_system + m_diff_nice + m_diff_idle)*100.0;
|
|
||||||
|
|
||||||
m_current_user = m_next_user;
|
sleep(1);
|
||||||
m_current_system = m_next_system;
|
|
||||||
m_current_nice = m_next_nice;
|
|
||||||
m_current_idle = m_next_idle;
|
|
||||||
|
|
||||||
return m_percentage;
|
stat_file.open("/proc/stat");
|
||||||
|
getline(stat_file, stat_line);
|
||||||
|
stat_file.close();
|
||||||
|
|
||||||
|
// skip "cpu"
|
||||||
|
line_start_pos = stat_line.find_first_not_of(" ", 3);
|
||||||
|
line_end_pos = stat_line.find_first_of(' ', line_start_pos);
|
||||||
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
|
line_end_pos = stat_line.find_first_of(' ', line_end_pos + 1);
|
||||||
|
|
||||||
|
iss.str( stat_line.substr( line_start_pos, line_end_pos - line_start_pos ) );
|
||||||
|
iss >> next_user >> next_nice >> next_system >> next_idle;
|
||||||
|
iss.clear();
|
||||||
|
|
||||||
|
diff_user = next_user - current_user;
|
||||||
|
diff_system = next_system - current_system;
|
||||||
|
diff_nice = next_nice - current_nice;
|
||||||
|
diff_idle = next_idle - current_idle;
|
||||||
|
|
||||||
|
return static_cast<float>(diff_user + diff_system + diff_nice)/static_cast<float>(diff_user + diff_system + diff_nice + diff_idle)*100.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string cpu_string()
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
{
|
||||||
string meter = "[ ]";
|
string meter = "[ ]";
|
||||||
int meter_count = 0;
|
int meter_count = 0;
|
||||||
float percentage = 0.0;
|
float percentage;
|
||||||
CPUPercentage cpu_per;
|
ostringstream oss;
|
||||||
cout.precision(1);
|
oss.precision( 1 );
|
||||||
cout.setf(ios::fixed | ios::right);
|
oss.setf( ios::fixed | ios::right );
|
||||||
cout.width(5);
|
|
||||||
|
percentage = cpu_percentage();
|
||||||
|
meter_count = 1;
|
||||||
|
while(meter_count*9.99 < percentage)
|
||||||
|
{
|
||||||
|
meter[meter_count] = '|';
|
||||||
|
meter_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
oss << "CPU:";
|
||||||
|
oss << meter;
|
||||||
|
oss.width( 5 );
|
||||||
|
oss << percentage;
|
||||||
|
oss << "%";
|
||||||
|
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char** argv)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
while(true)
|
std::cout << cpu_string();
|
||||||
{
|
|
||||||
percentage = cpu_per.get_percentage();
|
|
||||||
|
|
||||||
meter_count = 1;
|
|
||||||
while(meter_count*9.99 < percentage)
|
|
||||||
{
|
|
||||||
meter[meter_count] = '|';
|
|
||||||
meter_count++;
|
|
||||||
}
|
|
||||||
while(meter_count <= 10)
|
|
||||||
{
|
|
||||||
meter[meter_count] = ' ';
|
|
||||||
meter_count++;
|
|
||||||
}
|
|
||||||
cout << meter << ' ';
|
|
||||||
cout.width(5);
|
|
||||||
cout << percentage << endl;
|
|
||||||
sleep(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch(const exception &e)
|
catch(const exception &e)
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user