1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-11-09 20:42:47 +01:00
ScreenPlay/ScreenPlaySysInfo/cpu.cpp

49 lines
1.3 KiB
C++
Raw Normal View History

2018-10-26 12:05:23 +02:00
#include "cpu.h"
#include <QtQml/qqml.h>
2018-12-02 21:22:12 +01:00
#include "mathhelper.h"
2018-10-26 12:05:23 +02:00
#define STATUS_SUCCESS 0
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
CPU::CPU(QObject *parent) : QObject(parent)
{
2018-11-06 16:59:10 +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
}
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);
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);
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
}