-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsysinfowindowsimpl.cpp
64 lines (51 loc) · 1.74 KB
/
sysinfowindowsimpl.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
#include "sysinfowindowsimpl.h"
#include <windows.h>
#include <QtGlobal>
SysInfoWindowsImpl::SysInfoWindowsImpl():
SysInfo(), mCpuLoadLastValues()
{
}
void SysInfoWindowsImpl::init()
{
mCpuLoadLastValues = cpuRawData();
}
double SysInfoWindowsImpl::cpuLoadAverage()
{
QVector<qulonglong> firstSample = mCpuLoadLastValues;
QVector<qulonglong> secondSample = cpuRawData();
mCpuLoadLastValues = secondSample;
qulonglong currentIdle = secondSample[0] - firstSample[0];
qulonglong currentKernel = secondSample[1] - firstSample[1];
qulonglong currentUser = secondSample[2] - firstSample[2];
qulonglong currentSystem = currentUser + currentKernel;
double percent = (currentSystem - currentIdle)*100/currentSystem;
return qBound(0.0,percent,100.0);
}
double SysInfoWindowsImpl::memoryUsed()
{
MEMORYSTATUSEX memoryStatus;
memoryStatus.dwLength = sizeof(MEMORYSTATUSEX);
GlobalMemoryStatusEx(&memoryStatus);
qulonglong memoryPhysicalUsed =
(double)memoryStatus.ullTotalPhys - (double)memoryStatus.ullAvailPhys;
return memoryPhysicalUsed/memoryStatus.ullTotalPhys * 100;
}
QVector<qulonglong> SysInfoWindowsImpl::cpuRawData()
{
FILETIME idleTime;
FILETIME kernelTime;
FILETIME userTime;
GetSystemTimes(&idleTime, &kernelTime, &userTime);
QVector<qulonglong> rawData;
rawData.append(convertFileTime(idleTime));
rawData.append(convertFileTime(kernelTime));
rawData.append(convertFileTime(userTime));
return rawData;
}
qulonglong SysInfoWindowsImpl::convertFileTime(const FILETIME &filetime) const
{
ULARGE_INTEGER largeInteger;
largeInteger.LowPart = filetime.dwLowDateTime;
largeInteger.HighPart = filetime.dwHighDateTime;
return largeInteger.QuadPart;
}