-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCPUThread.cpp
More file actions
72 lines (60 loc) · 2.09 KB
/
TestCPUThread.cpp
File metadata and controls
72 lines (60 loc) · 2.09 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
/*
Resource Links:
CPU Load Percent (Credit to Jeremy Friesner): https://stackoverflow.com/questions/23143693/retrieving-cpu-load-percent-total-in-windows-with-c
Everything else (too many to list): https://stackoverflow.com/, https://docs.microsoft.com, https://doc.qt.io
*/
#include "TestCPUThread.h"
TestCPUThread::TestCPUThread()
{
counter = 0;bTest = false;
}
void TestCPUThread::threadStart()
{
counter = 0;bTest = true;
this->start();
}
void TestCPUThread::threadStop()
{
if (this->isRunning())
bTest = false;
//counter = 5000; //exit() nem jó
}
void TestCPUThread::run()
{
float f;
while (bTest)
{
f = GetCPULoad() * 100;
if (f < 0.0)
{
emit sendMessage(tr("Internal error - You should not see this."));
}
else
{
emit sendLCDValue( (int) f);this->msleep(200);
}
}
}
float TestCPUThread::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 TestCPUThread::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
// You'll need to call this at regular intervals, since it measures the load between
// the previous call and the current one. Returns -1.0 on error.
float TestCPUThread::GetCPULoad()
{
FILETIME idleTime, kernelTime, userTime;
return GetSystemTimes(&idleTime, &kernelTime, &userTime) ? CalculateCPULoad(FileTimeToInt64(idleTime), FileTimeToInt64(kernelTime) + FileTimeToInt64(userTime)) : -1.0f;
}