2018-10-26 12:05:23 +02:00
|
|
|
#include "cpu.h"
|
2018-12-02 21:22:12 +01:00
|
|
|
#include "mathhelper.h"
|
2020-10-29 19:17:37 +01:00
|
|
|
#include <QtQml/qqml.h>
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2020-10-29 19:17:37 +01:00
|
|
|
#define STATUS_SUCCESS 0
|
|
|
|
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2021-05-13 13:05:05 +02:00
|
|
|
/*!
|
|
|
|
\class CPU
|
|
|
|
\inmodule ScreenPlaySysInfo
|
|
|
|
\brief .
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2020-10-29 19:17:37 +01:00
|
|
|
CPU::CPU(QObject* parent)
|
|
|
|
: QObject(parent)
|
2018-10-26 12:05:23 +02:00
|
|
|
{
|
2020-10-29 19:17:37 +01:00
|
|
|
connect(&m_updateTimer, &QTimer::timeout, this, &CPU::update);
|
2018-11-06 17:00:21 +01:00
|
|
|
m_updateTimer.start(m_tickRate);
|
2018-10-26 12:05:23 +02:00
|
|
|
}
|
|
|
|
|
2021-05-13 13:35:54 +02:00
|
|
|
/*!
|
|
|
|
* \brief CPU::update
|
|
|
|
*/
|
2018-10-26 12:05:23 +02:00
|
|
|
void CPU::update()
|
|
|
|
{
|
2018-12-02 21:22:12 +01:00
|
|
|
BOOL status;
|
|
|
|
FILETIME ftIdleTime, ftKernelTime, ftUserTime;
|
|
|
|
|
|
|
|
// get new CPU's idle/kernel/user time
|
|
|
|
status = GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
|
2020-10-29 19:17:37 +01:00
|
|
|
if (status == 0)
|
|
|
|
return;
|
2018-12-02 21:22:12 +01:00
|
|
|
|
|
|
|
//convert times to uint by appending low and high bits
|
|
|
|
uint64_t newIdleTime = FileTimeToInt64(ftIdleTime);
|
|
|
|
uint64_t newKernelTime = FileTimeToInt64(ftKernelTime);
|
|
|
|
uint64_t newUserTime = FileTimeToInt64(ftUserTime);
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2018-12-02 21:22:12 +01:00
|
|
|
//subtract old times
|
|
|
|
uint64_t userTime = newUserTime - lastUserTime;
|
|
|
|
uint64_t kernelTime = newKernelTime - lastKernelTime;
|
|
|
|
double idleTime = newIdleTime - lastIdleTime;
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2018-12-02 21:22:12 +01:00
|
|
|
//calculate the usage
|
|
|
|
double sysTime = kernelTime + userTime;
|
|
|
|
float currentUsage = float((sysTime - idleTime) * 100 / sysTime);
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2018-12-02 21:22:12 +01:00
|
|
|
//save the new values
|
|
|
|
lastIdleTime = newIdleTime;
|
|
|
|
lastKernelTime = newKernelTime;
|
|
|
|
lastUserTime = newUserTime;
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2018-12-02 21:22:12 +01:00
|
|
|
float cu = int(currentUsage * 100);
|
|
|
|
currentUsage = float(cu / 100);
|
2018-10-26 12:05:23 +02:00
|
|
|
|
2018-12-02 21:22:12 +01:00
|
|
|
//publish result
|
|
|
|
setUsage(currentUsage);
|
2018-10-26 12:05:23 +02:00
|
|
|
}
|