1
0
mirror of https://gitlab.com/kelteseth/ScreenPlay.git synced 2024-09-18 16:32:33 +02:00

Add cpu and ram stats to sysinfo

This commit is contained in:
Dominik Louven 2018-12-02 21:22:12 +01:00
parent f228c39826
commit 855283d1ba
6 changed files with 211 additions and 36 deletions

View File

@ -17,7 +17,8 @@ HEADERS += \
screenplaysysinfo_plugin.h \ screenplaysysinfo_plugin.h \
sysinfo.h \ sysinfo.h \
cpu.h \ cpu.h \
ram.h ram.h \
mathhelper.h
DISTFILES = qmldir DISTFILES = qmldir

View File

@ -1,13 +1,13 @@
#include "cpu.h" #include "cpu.h"
#include <QtQml/qqml.h> #include <QtQml/qqml.h>
#include "mathhelper.h"
#define STATUS_SUCCESS 0 #define STATUS_SUCCESS 0
#define STATUS_INFO_LENGTH_MISMATCH 0xC0000004 #define STATUS_INFO_LENGTH_MISMATCH 0xC0000004
CPU::CPU(QObject *parent) : QObject(parent) CPU::CPU(QObject *parent) : QObject(parent)
{ {
// signal obj, signal function pointer, slot obj, slot function pointer
// signal obj, signal function pointer, slot obj, slot function pointer
connect(&m_updateTimer,&QTimer::timeout,this,&CPU::update); connect(&m_updateTimer,&QTimer::timeout,this,&CPU::update);
m_updateTimer.start(m_tickRate); m_updateTimer.start(m_tickRate);
} }
@ -15,22 +15,35 @@ CPU::CPU(QObject *parent) : QObject(parent)
void CPU::update() void CPU::update()
{ {
// long status = 0; BOOL status;
// unsigned long bufSize = c_BufferSize; FILETIME ftIdleTime, ftKernelTime, ftUserTime;
// byte* buf = (bufSize > 0) ? new BYTE[bufSize] : nullptr;
// 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);
// status = c_NtQuerySystemInformation(SystemProcessorPerformanceInformation, buf, bufSize, &size); //subtract old times
uint64_t userTime = newUserTime - lastUserTime;
uint64_t kernelTime = newKernelTime - lastKernelTime;
double idleTime = newIdleTime - lastIdleTime;
// switch (status) { //calculate the usage
// case STATUS_INFO_LENGTH_MISMATCH: double sysTime = kernelTime + userTime;
// qWarning() << "Warning: Status info length mismatch!"; float currentUsage = float((sysTime - idleTime) * 100 / sysTime);
// break;
// case STATUS_SUCCESS:
// break; //save the new values
// default: lastIdleTime = newIdleTime;
// break; lastKernelTime = newKernelTime;
// } lastUserTime = newUserTime;
float cu = int(currentUsage * 100);
currentUsage = float(cu / 100);
//publish result
setUsage(currentUsage);
} }

View File

@ -3,15 +3,13 @@
#include <QObject> #include <QObject>
#include <QDebug> #include <QDebug>
#include <QTimer> #include <QTimer>
#include <QString>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <qt_windows.h> #include <qt_windows.h>
#endif #endif
// https://github.com/rainmeter/rainmeter/blob/master/Library/MeasureCPU.cpp // https://github.com/rainmeter/rainmeter/blob/master/Library/MeasureCPU.cpp
#ifdef Q_OS_WIN
typedef LONG(WINAPI* FPNTQSI)(UINT, PVOID, ULONG, PULONG);
#endif
class CPU : public QObject { class CPU : public QObject {
Q_OBJECT Q_OBJECT
@ -19,6 +17,7 @@ class CPU : public QObject {
Q_PROPERTY(float usage READ usage NOTIFY usageChanged) Q_PROPERTY(float usage READ usage NOTIFY usageChanged)
Q_PROPERTY(int tickRate READ tickRate WRITE setTickRate NOTIFY tickRateChanged) Q_PROPERTY(int tickRate READ tickRate WRITE setTickRate NOTIFY tickRateChanged)
public: public:
explicit CPU(QObject* parent = nullptr); explicit CPU(QObject* parent = nullptr);
@ -39,12 +38,12 @@ signals:
void tickRateChanged(int tickRate); void tickRateChanged(int tickRate);
public slots: public slots:
void update(); void update();
void setUsage(float usage) void setUsage(float usage)
{ {
qWarning("Floating point comparison needs context sanity check");
if (qFuzzyCompare(m_usage, usage)) if (qFuzzyCompare(m_usage, usage))
return; return;
@ -63,17 +62,15 @@ public slots:
emit tickRateChanged(m_tickRate); emit tickRateChanged(m_tickRate);
} }
private: private:
float m_usage = 42.0f; float m_usage = 0.0f;
int m_Processor; uint64_t lastIdleTime = 0;
uint64_t lastKernelTime = 0;
uint64_t lastUserTime = 0;
double m_OldTime[2]; int m_tickRate = 1000;
//static FPNTQSI c_NtQuerySystemInformation;
static int c_NumOfProcessors;
//static ULONG c_BufferSize;
int m_tickRate = 500;
QTimer m_updateTimer; QTimer m_updateTimer;
}; };

View File

@ -0,0 +1,10 @@
#pragma once
#include <qt_windows.h>
#include <QObject>
uint64_t FileTimeToInt64( const FILETIME& ft ) {
ULARGE_INTEGER uli = { };
uli.LowPart = ft.dwLowDateTime;
uli.HighPart = ft.dwHighDateTime;
return uli.QuadPart;
}

View File

@ -1,8 +1,34 @@
#include "ram.h" #include "ram.h"
#include <QtQml/qqml.h> #include <QtQml/qqml.h>
#include <qmetatype.h> #include <qmetatype.h>
#include "sysinfoapi.h"
RAM::RAM(QObject* parent) RAM::RAM(QObject* parent)
: QObject(parent) : QObject(parent)
{ {
connect(&m_updateTimer,&QTimer::timeout,this,&RAM::update);
m_updateTimer.start(m_tickRate);
}
void RAM::update()
{
//Get values from system
MEMORYSTATUSEX memoryStatus;
memoryStatus.dwLength = sizeof(MEMORYSTATUSEX); //needed for internal api
bool success = GlobalMemoryStatusEx(&memoryStatus);
if(!success)
return;
//Apply total values
setTotalPhysicalMemory(memoryStatus.ullTotalPhys);
setTotalPagingMemory(memoryStatus.ullTotalPageFile);
setTotalVirtualMemory(memoryStatus.ullTotalVirtual);
//calculate usages
setUsedPhysicalMemory(m_totalPhysicalMemory - memoryStatus.ullAvailPhys);
setUsedPagingMemory(m_totalPagingMemory - memoryStatus.ullAvailPageFile);
setUsedVirtualMemory(m_totalVirtualMemory - memoryStatus.ullAvailVirtual);
//set overall usage
setUsage(memoryStatus.dwMemoryLoad);
} }

View File

@ -2,30 +2,94 @@
#include <QObject> #include <QObject>
#include <QDebug> #include <QDebug>
#include <QTimer>
#ifdef Q_OS_WIN
#include <qt_windows.h>
#endif
#include <sysinfoapi.h>
class RAM : public QObject { class RAM : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(float usage READ usage NOTIFY usageChanged) Q_PROPERTY(float usage READ usage NOTIFY usageChanged)
Q_PROPERTY(unsigned long long usedPhysicalMemory READ usedPhysicalMemory NOTIFY usedPhysicalMemoryChanged)
Q_PROPERTY(unsigned long long totalPhysicalMemory READ totalPhysicalMemory NOTIFY totalPhysicalMemoryChanged)
Q_PROPERTY(unsigned long long usedVirtualMemory READ usedVirtualMemory NOTIFY usedVirtualMemoryChanged)
Q_PROPERTY(unsigned long long totalVirtualMemory READ totalVirtualMemory NOTIFY totalVirtualMemoryChanged)
Q_PROPERTY(unsigned long long usedPagingMemory READ usedPagingMemory NOTIFY usedPagingMemoryChanged)
Q_PROPERTY(unsigned long long totalPagingMemory READ totalPagingMemory NOTIFY totalPagingMemoryChanged)
public: public:
explicit RAM(QObject* parent = nullptr); explicit RAM(QObject* parent = nullptr);
//total memory usage in percent
float usage() const float usage() const
{ {
return m_usage; return m_usage;
} }
//used physical memory in byte
DWORDLONG usedPhysicalMemory() const
{
return m_usedPhysicalMemory;
}
//total physical memory in byte
DWORDLONG totalPhysicalMemory() const
{
return m_totalPhysicalMemory;
}
//total virtual memory in byte
DWORDLONG totalVirtualMemory() const
{
return m_totalVirtualMemory;
}
//used virtual memory in byte
DWORDLONG usedVirtualMemory() const
{
return m_usedVirtualMemory;
}
//used paging memory in byte
DWORDLONG usedPagingMemory() const
{
return m_usedPagingMemory;
}
//total paging memory in byte
DWORDLONG totalPagingMemory() const
{
return m_totalPagingMemory;
}
signals: signals:
void usageChanged(float usage); void usageChanged(float usage);
public slots: void usedPhysicalMemoryChanged(DWORDLONG usedPhysicalMemory);
void exe(){
qDebug() << "aa"; void usedVirtualMemoryChanged(DWORDLONG usedVirtualMemory);
}
void usedPagingMemoryChanged(DWORDLONG usedPagingMemory);
void totalPhysicalMemoryChanged(DWORDLONG totalPhysicalMemory);
void totalVirtualMemoryChanged(DWORDLONG totalVirtualMemory);
void totalPagingMemoryChanged(DWORDLONG totalPagingMemory);
private slots:
void update();
void setUsage(float usage) void setUsage(float usage)
{ {
qWarning("Floating point comparison needs context sanity check");
if (qFuzzyCompare(m_usage, usage)) if (qFuzzyCompare(m_usage, usage))
return; return;
@ -33,7 +97,71 @@ public slots:
emit usageChanged(m_usage); emit usageChanged(m_usage);
} }
void setUsedPhysicalMemory(DWORDLONG usedPhysicalMemory)
{
if(usedPhysicalMemory == m_usedPhysicalMemory)
return;
m_usedPhysicalMemory = usedPhysicalMemory;
emit usedPhysicalMemoryChanged(m_usedPhysicalMemory);
}
void setTotalPhysicalMemory(DWORDLONG totalPhysicalMemory)
{
if(totalPhysicalMemory == m_totalPhysicalMemory)
return;
m_totalPhysicalMemory = totalPhysicalMemory;
emit totalPhysicalMemoryChanged(m_totalPhysicalMemory);
}
void setUsedVirtualMemory(DWORDLONG usedVirtualMemory)
{
if(usedVirtualMemory == m_usedVirtualMemory)
return;
m_usedVirtualMemory = usedVirtualMemory;
emit usedVirtualMemoryChanged(m_usedVirtualMemory);
}
void setTotalVirtualMemory(DWORDLONG totalVirtualMemory)
{
if(totalVirtualMemory == m_totalVirtualMemory)
return;
m_totalVirtualMemory = totalVirtualMemory;
emit totalVirtualMemoryChanged(m_totalVirtualMemory);
}
void setUsedPagingMemory(DWORDLONG usedPagingMemory)
{
if(usedPagingMemory == m_usedPagingMemory)
return;
m_usedPagingMemory = usedPagingMemory;
emit usedPagingMemoryChanged(m_usedPagingMemory);
}
void setTotalPagingMemory(DWORDLONG totalPagingMemory)
{
if(totalPagingMemory == m_totalPagingMemory)
return;
m_totalPagingMemory = totalPagingMemory;
emit totalPagingMemoryChanged(m_totalPagingMemory);
}
private: private:
float m_usage = 42.0f; float m_usage = -1.f;
DWORDLONG m_usedPhysicalMemory = 0ul;
DWORDLONG m_totalVirtualMemory = 0ul;
DWORDLONG m_totalPhysicalMemory = 0ul;
DWORDLONG m_usedVirtualMemory = 0ul;
DWORDLONG m_usedPagingMemory = 0ul;
DWORDLONG m_totalPagingMemory = 0ul;
QTimer m_updateTimer;
int m_tickRate = 1000;
}; };