Repository navigation
Expand file tree
/
Copy pathsystem_monitor.cpp
More file actions
163 lines (136 loc) · 4.24 KB
/
Copy pathsystem_monitor.cpp
File metadata and controls
163 lines (136 loc) · 4.24 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
#include "system_monitor.hpp"
#include <algorithm>
#include <cstdint>
#include <fstream>
#include <string>
#if defined(_WIN32)
#include <windows.h>
#endif
namespace dynamic_file_binder {
namespace {
#if defined(_WIN32)
FILETIME PreviousIdleTime{};
FILETIME PreviousKernelTime{};
FILETIME PreviousUserTime{};
bool CpuSampleInitialized = false;
float GetCpuUsageFromWindowsTimes()
{
FILETIME idleTime{};
FILETIME kernelTime{};
FILETIME userTime{};
if (!GetSystemTimes(&idleTime, &kernelTime, &userTime)) {
return 0.0f;
}
ULARGE_INTEGER idle{};
idle.LowPart = idleTime.dwLowDateTime;
idle.HighPart = idleTime.dwHighDateTime;
ULARGE_INTEGER kernel{};
kernel.LowPart = kernelTime.dwLowDateTime;
kernel.HighPart = kernelTime.dwHighDateTime;
ULARGE_INTEGER user{};
user.LowPart = userTime.dwLowDateTime;
user.HighPart = userTime.dwHighDateTime;
if (!CpuSampleInitialized) {
PreviousIdleTime = idleTime;
PreviousKernelTime = kernelTime;
PreviousUserTime = userTime;
CpuSampleInitialized = true;
return 0.0f;
}
ULARGE_INTEGER previousIdle{};
previousIdle.LowPart = PreviousIdleTime.dwLowDateTime;
previousIdle.HighPart = PreviousIdleTime.dwHighDateTime;
ULARGE_INTEGER previousKernel{};
previousKernel.LowPart = PreviousKernelTime.dwLowDateTime;
previousKernel.HighPart = PreviousKernelTime.dwHighDateTime;
ULARGE_INTEGER previousUser{};
previousUser.LowPart = PreviousUserTime.dwLowDateTime;
previousUser.HighPart = PreviousUserTime.dwHighDateTime;
const std::uint64_t idleDelta = idle.QuadPart - previousIdle.QuadPart;
const std::uint64_t kernelDelta = kernel.QuadPart - previousKernel.QuadPart;
const std::uint64_t userDelta = user.QuadPart - previousUser.QuadPart;
const std::uint64_t total = kernelDelta + userDelta;
PreviousIdleTime = idleTime;
PreviousKernelTime = kernelTime;
PreviousUserTime = userTime;
if (total == 0) {
return 0.0f;
}
const float usage = static_cast<float>((total - idleDelta) * 100.0 / total);
return std::clamp(usage, 0.0f, 100.0f);
}
#endif
float Clamp(float value)
{
return std::clamp(value, 0.0f, 100.0f);
}
} // namespace
float SystemMonitor::GetCpuUsage()
{
#if defined(_WIN32)
return GetCpuUsageFromWindowsTimes();
#elif defined(__linux__)
std::ifstream statFile("/proc/stat");
std::string cpu;
long user = 0;
long nice = 0;
long system = 0;
long idle = 0;
long iowait = 0;
long irq = 0;
long softirq = 0;
long steal = 0;
statFile >> cpu >> user >> nice >> system >> idle >> iowait >> irq >> softirq >> steal;
const long total = user + nice + system + idle + iowait + irq + softirq + steal;
const long active = total - idle - iowait;
return total > 0 ? Clamp(static_cast<float>(active * 100.0 / total)) : 0.0f;
#else
return 0.0f;
#endif
}
float SystemMonitor::GetMemoryUsage()
{
#if defined(_WIN32)
MEMORYSTATUSEX memoryStatus{};
memoryStatus.dwLength = sizeof(memoryStatus);
if (!GlobalMemoryStatusEx(&memoryStatus) || memoryStatus.ullTotalPhys == 0) {
return 0.0f;
}
const double used = static_cast<double>(memoryStatus.ullTotalPhys - memoryStatus.ullAvailPhys);
return Clamp(static_cast<float>((used * 100.0) / memoryStatus.ullTotalPhys));
#elif defined(__linux__)
std::ifstream meminfo("/proc/meminfo");
std::string key;
long total = 0;
long available = 0;
long value = 0;
std::string unit;
while (meminfo >> key >> value >> unit) {
if (key == "MemTotal:") {
total = value;
} else if (key == "MemAvailable:") {
available = value;
}
}
return total > 0 ? Clamp(static_cast<float>(((total - available) * 100.0) / total)) : 0.0f;
#else
return 0.0f;
#endif
}
float SystemMonitor::GetNetworkUsage()
{
return 0.0f;
}
float SystemMonitor::GetDiskUsage()
{
return 0.0f;
}
float SystemMonitor::GetSystemLoadScore()
{
const float cpu = GetCpuUsage();
const float memory = GetMemoryUsage();
const float network = GetNetworkUsage();
const float disk = GetDiskUsage();
return Clamp(cpu * 0.4f + memory * 0.3f + network * 0.15f + disk * 0.15f);
}
} // namespace dynamic_file_binder