-
Notifications
You must be signed in to change notification settings - Fork 0
/
systeminfo.cpp
82 lines (64 loc) · 2.26 KB
/
systeminfo.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "systeminfo.h"
SystemInfo::SystemInfo(QObject *parent) : QObject(parent)
{
}
MEMORYSTATUSEX SystemInfo::GetMemoryInfo()
{
// Code block initialization for the memory referenced in the Kernel
MEMORYSTATUSEX memStat;
memStat.dwLength = sizeof(memStat);
GlobalMemoryStatusEx(&memStat);
return memStat;
}
long SystemInfo::GetVirtualMemoryCommited()
{
return long((GetMemoryInfo().ullTotalPageFile - GetMemoryInfo().ullAvailPageFile) / MB);
}
long SystemInfo::GetVirtualMemoryAvailable()
{
return long(GetMemoryInfo().ullAvailPageFile / MB);
}
long SystemInfo::GetPhysicalMemoryUsed()
{
return long((GetMemoryInfo().ullTotalPhys - GetMemoryInfo().ullAvailPhys) / MB);
}
long SystemInfo::GetPhysicalMemoryAvailable()
{
return long(GetMemoryInfo().ullAvailPhys / MB);
}
long SystemInfo::GetPhysicalMemoryLoad()
{
return long(GetMemoryInfo().dwMemoryLoad);
}
long SystemInfo::GetVirtualTotal()
{
return long(GetMemoryInfo().ullTotalPageFile / MB);
}
long SystemInfo::GetPhysicalTotal()
{
return long(GetMemoryInfo().ullTotalPhys / MB);
}
float SystemInfo::CalculateCPULoad(unsigned long long idleTicks, unsigned long long totalTicks)
{
static unsigned long long _previousTotalTicks = 0;
static unsigned long long _previousIdleTicks = 0;
unsigned long long totalTicksSinceLastTime = totalTicks - _previousTotalTicks;
unsigned long long idleTicksSinceLastTime = idleTicks - _previousIdleTicks;
float ret = 1.0f - ((totalTicksSinceLastTime > 0) ? (float(idleTicksSinceLastTime))/totalTicksSinceLastTime : 0);
_previousTotalTicks = totalTicks;
_previousIdleTicks = idleTicks;
return ret;
}
unsigned long long SystemInfo::FileTimeToInt64(const FILETIME & ft)
{
return (((unsigned long long)ft.dwHighDateTime) << 32) | ((unsigned long long)ft.dwLowDateTime);
}
// Returns 1.0f for "CPU fully pinned", 0.0f for "CPU idle", or somewhere in between
// Measures the load between the previous call and the current one. Returns -1.0 on error.
long SystemInfo::GetCPULoad()
{
FILETIME idleTime, kernelTime, userTime;
return GetSystemTimes(&idleTime, &kernelTime, &userTime)
? long(CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime)) * 100)
: -1.0f;
}