1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-09 12:32:40 +01:00
ScreenPlay/ScreenPlaySysInfo/cpu.cpp
2021-05-13 13:35:54 +02:00

60 lines
1.4 KiB
C++

#include "cpu.h"
#include "mathhelper.h"
#include <QtQml/qqml.h>
#define STATUS_SUCCESS 0
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
/*!
\class CPU
\inmodule ScreenPlaySysInfo
\brief .
*/
CPU::CPU(QObject* parent)
: QObject(parent)
{
connect(&m_updateTimer, &QTimer::timeout, this, &CPU::update);
m_updateTimer.start(m_tickRate);
}
/*!
* \brief CPU::update
*/
void CPU::update()
{
BOOL status;
FILETIME ftIdleTime, ftKernelTime, ftUserTime;
// get new CPU's idle/kernel/user time
status = GetSystemTimes(&ftIdleTime, &ftKernelTime, &ftUserTime);
if (status == 0)
return;
//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);
//subtract old times
uint64_t userTime = newUserTime - lastUserTime;
uint64_t kernelTime = newKernelTime - lastKernelTime;
double idleTime = newIdleTime - lastIdleTime;
//calculate the usage
double sysTime = kernelTime + userTime;
float currentUsage = float((sysTime - idleTime) * 100 / sysTime);
//save the new values
lastIdleTime = newIdleTime;
lastKernelTime = newKernelTime;
lastUserTime = newUserTime;
float cu = int(currentUsage * 100);
currentUsage = float(cu / 100);
//publish result
setUsage(currentUsage);
}