-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSystemTool.hpp
269 lines (244 loc) · 7.65 KB
/
MSystemTool.hpp
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
//===========================
//====> MSystemTool
//===========================
#ifndef _MSYSTEMTOOL_HPP_
#define _MSYSTEMTOOL_HPP_
#if defined(__linux__)
#include <fstream>
#include <stdlib.h>
#include <sys/statvfs.h>
#include "MStringTool.hpp"
#include "MPathTool.hpp"
#elif defined(_WIN32)
#include <windows.h>
#include <direct.h>
#endif
#include <iostream>
#include <stdio.h>
using namespace std;
namespace toolsystem {
#define UNIT_GB (1024 * 1024 * 1024)
class MSystemInfo {
public:
MSystemInfo() {
#if defined(__linux__)
// Memory info
this->fMemInfo = "/proc/meminfo";
if (!toolpath::isFileExist(this->fMemInfo)) {
cout << "memory information file " << this->fMemInfo << " is missing.\n";
return;
}
this->nMemTotal = this->getMemAttrbKb(this->fMemInfo, "MemTotal") / (1024 * 1024);
// Processor info
this->fCpuInfo = "/proc/cpuinfo";
this->fCpuStat = "/proc/stat";
vector <string> lLines = toolpath::getlines(this->fCpuInfo);
this->nCpu = 0;
for(int i=0; i < (int)lLines.size(); i++) {
if (lLines[i].size() < 1) continue;
if (toolstring::isIncludedIn("processor", lLines[i])) this->nCpu ++;
}
#elif defined(_WIN32)
// Memory info
this->pStatex = new MEMORYSTATUSEX;
this->pStatex->dwLength = sizeof(*this->pStatex);
// Processor info
this->pSysInfo = new SYSTEM_INFO;
GetSystemInfo(pSysInfo);
this->nCpu = pSysInfo->dwNumberOfProcessors;
#endif
this->update();
}
private:
void update() {
// Memory info
#if defined(__linux__)
this->nMemFree = this->getMemAttrbKb(this->fMemInfo, "MemFree") / (1024 * 1024);
#elif defined(_WIN32)
GlobalMemoryStatusEx(this->pStatex);
this->nMemTotal = (float)this->pStatex->ullTotalPhys / UNIT_GB;
this->nMemFree = (float)this->pStatex->ullAvailPhys / UNIT_GB;
#endif
}
#if defined(__linux__)
float getMemAttrbKb(const string fMemInfo, const string sAttrb) {
vector<string> lInfo = toolpath::getlines(fMemInfo);
int val = -1;
for (int i=0; i< (int)lInfo.size(); i++) {
if (toolstring::isIncludedIn(sAttrb, lInfo[i])) {
vector <string> lTerms = toolstring::split(lInfo[i], ':');
string term = toolstring::rstrip(toolstring::lstrip(lTerms[1], ' '), " kB\r\n");
val = atoi(term.c_str());
return (float)val;
}
}
cout << "memoryinfo (" << sAttrb << ") does not exist.\n";
return -1;
}
vector<int> getCpuTm() {
vector<string> lInfo = toolpath::getlines(this->fCpuStat);
vector<int> lVal;
vector<string> cpustat = toolstring::split(lInfo[0], ' ');
for (int i=0; i < (int)cpustat.size(); i++) {
if (i == 0 || cpustat[i].size() < 1) continue;
lVal.push_back(atoi(cpustat[i].c_str()));
}
return lVal;
}
#elif defined(_WIN32)
__int64 CompareFileTime(FILETIME tm1, FILETIME tm2) {
__int64 tma = tm1.dwHighDateTime << 32 | tm1.dwLowDateTime;
__int64 tmb = tm2.dwHighDateTime << 32 | tm2.dwLowDateTime;
return (tmb - tma);
}
#endif
public:
float getRamSize() {
return this->nMemTotal;
}
float getRamUseRatio() {
this->update();
return 1 - (float)this->nMemFree / (float)this->nMemTotal;
}
float getRamFreeRatio() {
this->update();
return (float)this->nMemFree / (float)this->nMemTotal;
}
int getCpuNum() {
return this->nCpu;
}
#if defined(__linux__)
float getCpuUseRatio() {
vector<int> lTmBef = this->getCpuTm();
sleep(1);
vector<int> lTmAft = this->getCpuTm();
float totalDiff = 0, idlDiff = 0;
for(int i=0; i < (int)lTmBef.size(); i++) {
int diff = lTmAft[i] - lTmBef[i];
totalDiff += diff;
if (i == 3) { // the 4th value is idle time
idlDiff = diff;
}
}
return (totalDiff - idlDiff) / totalDiff;
}
#elif defined(_WIN32)
float getCpuUseRatio() {
FILETIME tmIdlBef, tmIdlAft;
FILETIME tmKernBef, tmKernAft;
FILETIME tmUsrBef, tmUsrAft;
BOOL isOk = GetSystemTimes(&tmIdlAft, &tmKernAft, &tmUsrAft);
tmIdlBef = tmIdlAft;
tmKernBef = tmKernAft;
tmUsrBef = tmUsrAft;
HANDLE hSampl = CreateEventA(NULL, FALSE, FALSE, NULL);
WaitForSingleObject(hSampl, 10);
isOk = GetSystemTimes(&tmIdlAft, &tmKernAft, &tmUsrAft);
__int64 tmIdlDiff = this->CompareFileTime(tmIdlBef, tmIdlAft);
__int64 tmKernDiff = this->CompareFileTime(tmKernBef, tmKernAft);
__int64 tmUsrDiff = this->CompareFileTime(tmUsrBef, tmUsrAft);
float diff = (float)(tmKernDiff + tmUsrDiff - tmIdlDiff) / (float)(tmKernDiff + tmUsrDiff);
// float freeRatio = tmIdlDiff / (tmKernDiff + tmUsrDiff);
return diff;
}
#endif
#if defined(__linux__)
float getDiskSize(string rootpath = "/home") {
struct statvfs vfs;
int error = statvfs(rootpath.c_str(), &vfs);
fsblkcnt_t volTotal = vfs.f_blocks;
fsblkcnt_t volUnit = vfs.f_bsize;
return (float)volTotal * (float)volUnit / UNIT_GB;
}
float getDiskFreeRatio(string rootpath) {
struct statvfs vfs;
int error = statvfs(rootpath.c_str(), &vfs);
fsblkcnt_t volFree = vfs.f_bfree;
fsblkcnt_t volTotal = vfs.f_blocks;
return (float)volFree / (float)volTotal;
}
#elif defined(_WIN32)
float getDiskSize(string diskflag = "C:\\") {
int DType = GetDriveTypeA(diskflag.c_str());
if (DType == DRIVE_CDROM || DType == DRIVE_UNKNOWN) {
cout << "invalid disk type.\n";
return -1;
}
unsigned _int64 bt2cFree, btTotal, btFree;
BOOL isOk = GetDiskFreeSpaceExA(
diskflag.c_str(),
(PULARGE_INTEGER)&bt2cFree,
(PULARGE_INTEGER)&btTotal,
(PULARGE_INTEGER)&btFree
);
if (isOk) return (float)btTotal / (float)UNIT_GB;
else return -1;
}
float getDiskFreeRatio(string diskflag) {
int DType = GetDriveTypeA(diskflag.c_str());
if (DType == DRIVE_CDROM || DType == DRIVE_UNKNOWN) {
cout << "invalid disk type.\n";
return 0;
}
unsigned _int64 bt2cFree, btTotal, btFree;
BOOL isOk = GetDiskFreeSpaceExA(
diskflag.c_str(),
(PULARGE_INTEGER)&bt2cFree,
(PULARGE_INTEGER)&btTotal,
(PULARGE_INTEGER)&btFree
);
if (isOk) return (float)bt2cFree / (float)btTotal;
else return 0;
}
#endif
private:
#if defined(__linux__)
string fMemInfo;
string fCpuInfo;
string fCpuStat;
#elif defined(_WIN32)
MEMORYSTATUSEX* pStatex;
SYSTEM_INFO* pSysInfo;
#endif
float nMemTotal;
float nMemFree;
int nCpu;
};
}
namespace debug_toolsystem {
void debug_system() {
using namespace toolsystem;
cout << "# ---- MSystemTool ----\n";
/* test RAM info */
cout << "\n## Test RAM info functions\n";
MSystemInfo sysinfo;
float nMemTotal = sysinfo.getRamSize();
float nMemUseRatio = sysinfo.getRamUseRatio();
float nMemFreeRatio = sysinfo.getRamFreeRatio();
cout << "\tRAM info: Total " << nMemTotal << " GB, "
<< "Occupy " << nMemUseRatio * 100 << "%, "
<< "Free " << nMemFreeRatio * 100 << "%.\n";
#if defined(__linux__)
sleep(1);
#elif defined(_WIN32)
Sleep(1000);
#endif
/* test CPU info */
cout << "\n## Test CPU info functions\n";
cout << "\tCPU Num = " << sysinfo.getCpuNum()
<< ", CPU usage = " << sysinfo.getCpuUseRatio() * 100 << "%\n";
/* test disk info */
cout << "\n## Test disk info functions\n";
#if defined(__linux__)
string diskflag = "/home";
#elif defined(_WIN32)
char currdir[255];
getcwd(currdir, 255);
string cwd = currdir;
string diskflag = cwd.substr(0, 3); // demo: "d:\\"
#endif
cout << "\tDisk " << diskflag.c_str() << " total = " << sysinfo.getDiskSize(diskflag) << " GB, "
<< "free = " << sysinfo.getDiskFreeRatio(diskflag) * 100 << "%\n";
}
}
#endif