-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus_Sys.cpp
More file actions
347 lines (276 loc) · 8.97 KB
/
Status_Sys.cpp
File metadata and controls
347 lines (276 loc) · 8.97 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#include "Status_Sys.h"
#include "Temperature_Sys.h"
#include <chrono>
#include <fstream>
#include <sstream>
#include <thread>
namespace {
struct CpuStatLine {
std::string name;
unsigned long long user = 0;
unsigned long long nice = 0;
unsigned long long system = 0;
unsigned long long idle = 0;
unsigned long long iowait = 0;
unsigned long long irq = 0;
unsigned long long softirq = 0;
unsigned long long steal = 0;
};
struct GpuStatsSnapshot {
bool ok = false;
unsigned long long timestamp = 0;
unsigned long long total_runtime = 0;
};
struct PreviousLoadData {
bool ready = false;
std::vector<CpuStatLine> cpu;
GpuStatsSnapshot gpu;
};
double LimitPercent(double value) {
if (value < 0.0) {
return 0.0;
}
if (value > 100.0) {
return 100.0;
}
return value;
}
bool ReadLongLongFromFile(const std::string& path, long long& value) {
if (path.empty()) {
return false;
}
std::ifstream file(path.c_str());
if (!file.is_open()) {
return false;
}
file >> value;
return !file.fail();
}
bool ReadCpuFrequencyGHz(const std::string& path, double& ghz) {
long long freq_khz = 0;
if (!ReadLongLongFromFile(path, freq_khz)) {
return false;
}
ghz = static_cast<double>(freq_khz) / 1000000.0;
return true;
}
std::vector<CpuStatLine> ReadCpuStat(const std::string& path) {
std::vector<CpuStatLine> list;
std::ifstream file(path.c_str());
if (!file.is_open()) {
return list;
}
std::string line;
while (std::getline(file, line)) {
if (line.find("cpu") != 0) {
break;
}
std::stringstream parser(line);
CpuStatLine cpu;
parser >> cpu.name
>> cpu.user
>> cpu.nice
>> cpu.system
>> cpu.idle
>> cpu.iowait
>> cpu.irq
>> cpu.softirq
>> cpu.steal;
if (!parser.fail()) {
list.push_back(cpu);
}
}
return list;
}
unsigned long long GetTotalCpuTime(const CpuStatLine& cpu) {
return cpu.user + cpu.nice + cpu.system + cpu.idle +
cpu.iowait + cpu.irq + cpu.softirq + cpu.steal;
}
unsigned long long GetIdleCpuTime(const CpuStatLine& cpu) {
return cpu.idle + cpu.iowait;
}
double CalculateCpuUsage(const CpuStatLine& first, const CpuStatLine& second) {
unsigned long long total1 = GetTotalCpuTime(first);
unsigned long long total2 = GetTotalCpuTime(second);
unsigned long long idle1 = GetIdleCpuTime(first);
unsigned long long idle2 = GetIdleCpuTime(second);
unsigned long long total_delta = total2 - total1;
unsigned long long idle_delta = idle2 - idle1;
if (total_delta == 0) {
return 0.0;
}
double busy = static_cast<double>(total_delta - idle_delta);
return LimitPercent((busy * 100.0) / static_cast<double>(total_delta));
}
bool ReadMemory(const std::string& path, long& total_mb, long& used_mb, long& free_mb, double& usage) {
std::ifstream file(path.c_str());
if (!file.is_open()) {
return false;
}
long total_kb = 0;
long free_kb = 0;
long available_kb = 0;
std::string key;
long value = 0;
std::string unit;
while (file >> key >> value >> unit) {
if (key == "MemTotal:") {
total_kb = value;
} else if (key == "MemFree:") {
free_kb = value;
} else if (key == "MemAvailable:") {
available_kb = value;
}
}
if (total_kb <= 0) {
return false;
}
if (available_kb <= 0) {
available_kb = free_kb;
}
long used_kb = total_kb - available_kb;
total_mb = total_kb / 1024;
used_mb = used_kb / 1024;
free_mb = available_kb / 1024;
usage = LimitPercent((static_cast<double>(used_kb) * 100.0) / static_cast<double>(total_kb));
return true;
}
bool ReadLoadAverage(const std::string& path, double& load1, double& load5, double& load15) {
std::ifstream file(path.c_str());
if (!file.is_open()) {
return false;
}
file >> load1 >> load5 >> load15;
return !file.fail();
}
GpuStatsSnapshot ReadGpuStats(const std::string& path) {
GpuStatsSnapshot snapshot;
if (path.empty()) {
return snapshot;
}
std::ifstream file(path.c_str());
if (!file.is_open()) {
return snapshot;
}
std::string header;
std::getline(file, header);
std::string queue_name;
unsigned long long timestamp = 0;
unsigned long long jobs = 0;
unsigned long long runtime = 0;
while (file >> queue_name >> timestamp >> jobs >> runtime) {
snapshot.ok = true;
snapshot.timestamp = timestamp;
if (queue_name != "cpu") {
snapshot.total_runtime += runtime;
}
}
return snapshot;
}
double CalculateGpuUsage(const GpuStatsSnapshot& first, const GpuStatsSnapshot& second) {
if (!first.ok || !second.ok) {
return -1.0;
}
if (second.timestamp <= first.timestamp) {
return 0.0;
}
if (second.total_runtime < first.total_runtime) {
return 0.0;
}
unsigned long long time_delta = second.timestamp - first.timestamp;
unsigned long long runtime_delta = second.total_runtime - first.total_runtime;
double usage = static_cast<double>(runtime_delta) * 100.0 / static_cast<double>(time_delta);
return LimitPercent(usage);
}
PreviousLoadData& GetPreviousLoadData() {
static PreviousLoadData data;
return data;
}
}
SystemInfo ReadSystemInfo(const Settings& settings) {
SystemInfo info;
PreviousLoadData& previous = GetPreviousLoadData();
std::vector<CpuStatLine> first_cpu;
std::vector<CpuStatLine> second_cpu;
GpuStatsSnapshot first_gpu_stats;
GpuStatsSnapshot second_gpu_stats;
if (previous.ready) {
first_cpu = previous.cpu;
first_gpu_stats = previous.gpu;
second_cpu = ReadCpuStat(settings.cpu_stat_path);
second_gpu_stats = ReadGpuStats(settings.gpu_stats_path);
if (first_cpu.empty() || first_cpu.size() != second_cpu.size()) {
first_cpu = ReadCpuStat(settings.cpu_stat_path);
first_gpu_stats = ReadGpuStats(settings.gpu_stats_path);
std::this_thread::sleep_for(std::chrono::milliseconds(600));
second_cpu = ReadCpuStat(settings.cpu_stat_path);
second_gpu_stats = ReadGpuStats(settings.gpu_stats_path);
}
} else {
first_cpu = ReadCpuStat(settings.cpu_stat_path);
first_gpu_stats = ReadGpuStats(settings.gpu_stats_path);
std::this_thread::sleep_for(std::chrono::milliseconds(600));
second_cpu = ReadCpuStat(settings.cpu_stat_path);
second_gpu_stats = ReadGpuStats(settings.gpu_stats_path);
}
previous.ready = true;
previous.cpu = second_cpu;
previous.gpu = second_gpu_stats;
double temp = ReadCpuTemperature(settings.temp_path);
if (temp >= 0.0) {
info.cpu_temp_ok = true;
info.cpu_temp = temp;
}
double freq_ghz = 0.0;
if (ReadCpuFrequencyGHz(settings.cpu_freq_path, freq_ghz)) {
info.cpu_freq_ok = true;
info.cpu_freq_ghz = freq_ghz;
}
if (!first_cpu.empty() && first_cpu.size() == second_cpu.size()) {
info.cpu_total_ok = true;
info.cpu_total_usage = CalculateCpuUsage(first_cpu[0], second_cpu[0]);
for (std::size_t i = 1; i < first_cpu.size(); ++i) {
CpuCoreInfo core;
core.name = "C" + std::to_string(i - 1);
core.usage_percent = CalculateCpuUsage(first_cpu[i], second_cpu[i]);
info.cores.push_back(core);
}
}
long long gpu_value = 0;
if (ReadLongLongFromFile(settings.gpu_busy_path, gpu_value)) {
info.gpu_ok = true;
info.gpu_usage = LimitPercent(static_cast<double>(gpu_value));
} else {
double gpu_usage = CalculateGpuUsage(first_gpu_stats, second_gpu_stats);
if (gpu_usage >= 0.0) {
info.gpu_ok = true;
info.gpu_usage = gpu_usage;
}
}
if (ReadMemory(settings.meminfo_path,
info.memory_total_mb,
info.memory_used_mb,
info.memory_free_mb,
info.memory_usage)) {
info.memory_ok = true;
}
if (ReadLoadAverage(settings.loadavg_path, info.load1, info.load5, info.load15)) {
info.load_ok = true;
}
long long fan_rpm_value = 0;
if (ReadLongLongFromFile(settings.fan_rpm_path, fan_rpm_value)) {
info.fan_rpm_ok = true;
info.fan_rpm = static_cast<int>(fan_rpm_value);
}
long long fan_pwm_value = 0;
if (ReadLongLongFromFile(settings.fan_pwm_path, fan_pwm_value)) {
info.fan_pwm_ok = true;
info.fan_pwm = static_cast<int>(fan_pwm_value);
}
long long fan_mode_value = 0;
if (ReadLongLongFromFile(settings.fan_pwm_enable_path, fan_mode_value)) {
info.fan_mode_ok = true;
info.fan_mode = static_cast<int>(fan_mode_value);
}
return info;
}