-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptime.h
123 lines (100 loc) · 2.63 KB
/
uptime.h
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
#include <chrono>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <thread>
#include <vector>
const int NUM_CPU_STATES = 10;
enum CPUStates
{
S_USER = 0,
S_NICE,
S_SYSTEM,
S_IDLE,
S_IOWAIT,
S_IRQ,
S_SOFTIRQ,
S_STEAL,
S_GUEST,
S_GUEST_NICE
};
typedef struct CPUData
{
std::string cpu;
size_t times[NUM_CPU_STATES];
} CPUData;
void ReadStatsCPU(std::vector<CPUData> & entries);
size_t GetIdleTime(const CPUData & e);
size_t GetActiveTime(const CPUData & e);
void PrintStats(const std::vector<CPUData> & entries1, const std::vector<CPUData> & entries2);
void ReadStatsCPU(std::vector<CPUData> & entries)
{
std::ifstream fileStat("/proc/stat");
std::string line;
const std::string STR_CPU("cpu");
const std::size_t LEN_STR_CPU = STR_CPU.size();
const std::string STR_TOT("tot");
while(std::getline(fileStat, line))
{
// cpu stats line found
if(!line.compare(0, LEN_STR_CPU, STR_CPU))
{
std::istringstream ss(line);
// store entry
entries.emplace_back(CPUData());
CPUData & entry = entries.back();
// read cpu label
ss >> entry.cpu;
// remove "cpu" from the label when it's a processor number
if(entry.cpu.size() > LEN_STR_CPU)
entry.cpu.erase(0, LEN_STR_CPU);
// replace "cpu" with "tot" when it's total values
else
entry.cpu = STR_TOT;
// read times
for(int i = 0; i < NUM_CPU_STATES; ++i)
ss >> entry.times[i];
}
}
}
size_t GetIdleTime(const CPUData & e)
{
return e.times[S_IDLE] +
e.times[S_IOWAIT];
}
size_t GetActiveTime(const CPUData & e)
{
return e.times[S_USER] +
e.times[S_NICE] +
e.times[S_SYSTEM] +
e.times[S_IRQ] +
e.times[S_SOFTIRQ] +
e.times[S_STEAL] +
e.times[S_GUEST] +
e.times[S_GUEST_NICE];
}
void PrintStats(const std::vector<CPUData> & entries1, const std::vector<CPUData> & entries2)
{
const size_t NUM_ENTRIES = entries1.size();
for(size_t i = 0; i < NUM_ENTRIES; ++i)
{
const CPUData & e1 = entries1[i];
const CPUData & e2 = entries2[i];
std::cout.width(3);
std::cout << e1.cpu << "] ";
const float ACTIVE_TIME = static_cast<float>(GetActiveTime(e2) - GetActiveTime(e1));
const float IDLE_TIME = static_cast<float>(GetIdleTime(e2) - GetIdleTime(e1));
const float TOTAL_TIME = ACTIVE_TIME + IDLE_TIME;
std::cout << "active: ";
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout.width(6);
std::cout.precision(2);
std::cout << (100.f * ACTIVE_TIME / TOTAL_TIME) << "%";
std::cout << " - idle: ";
std::cout.setf(std::ios::fixed, std::ios::floatfield);
std::cout.width(6);
std::cout.precision(2);
std::cout << (100.f * IDLE_TIME / TOTAL_TIME) << "%" << std::endl;
}
}