forked from thewtex/screen-cpu-mem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscreen-cpu-usage.cpp
143 lines (125 loc) · 3.33 KB
/
screen-cpu-usage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* @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 <iostream>
#include <sstream>
#include <string>
using namespace std;
#include <unistd.h> // sleep
// in seconds
const static unsigned int INTERVAL=1;
class CPUPercentage
{
public:
CPUPercentage();
~CPUPercentage();
const float& get_percentage();
private:
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"
m_line_start_pos = m_stat_line.find_first_not_of(" ", 3);
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_start_pos);
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1);
m_line_end_pos = m_stat_line.find_first_of(' ', m_line_end_pos + 1);
m_line_end_pos = m_stat_line.find_first_of(' ', m_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;
m_diff_system = m_next_system - m_current_system;
m_diff_nice = m_next_nice - m_current_nice;
m_diff_idle = m_next_idle - m_current_idle;
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;
m_current_system = m_next_system;
m_current_nice = m_next_nice;
m_current_idle = m_next_idle;
return m_percentage;
}
int main(int argc, char** argv)
{
string meter = " :";
int meter_count = 0;
float percentage = 0.0;
CPUPercentage cpu_per;
cout.precision(1);
cout.setf(ios::fixed | ios::right);
cout.width(5);
try
{
while(true)
{
percentage = cpu_per.get_percentage();
meter_count = 1;
while(meter_count*9.99 < percentage)
{
meter[meter_count-1] = '|';
meter_count++;
}
while(meter_count <= 10)
{
meter[meter_count-1] = ' ';
meter_count++;
}
cout << meter << ' ';
cout.width(5);
cout << percentage << endl;
sleep(1);
}
}
catch(const exception &e)
{
cerr << "Error: " << e.what() << endl;
return 1;
}
return 0;
}