80 lines
2.0 KiB
C++
80 lines
2.0 KiB
C++
|
#include "showcpumemory.h"
|
|||
|
#include <QThread>
|
|||
|
#include <QProcess>
|
|||
|
#include <QLabel>
|
|||
|
#include "datavar.h"
|
|||
|
|
|||
|
#define MB (1024 * 1024)
|
|||
|
#define KB (1024)
|
|||
|
|
|||
|
ShowCpuMemory::ShowCpuMemory(QObject *parent) : QObject(parent)
|
|||
|
{
|
|||
|
memoryPercent = 0;
|
|||
|
memoryAll = 0;
|
|||
|
memoryUse = 0;
|
|||
|
labMemory = 0;
|
|||
|
}
|
|||
|
|
|||
|
void ShowCpuMemory::SetLab(QLabel *labMemory)
|
|||
|
{
|
|||
|
this->labMemory = labMemory;
|
|||
|
this->labMemory->setObjectName("CPU");
|
|||
|
}
|
|||
|
|
|||
|
void ShowCpuMemory::GetCpuMemory()
|
|||
|
{
|
|||
|
while (true)
|
|||
|
{
|
|||
|
cpu = calCpuUsage();
|
|||
|
MEMORYSTATUSEX statex;
|
|||
|
statex.dwLength = sizeof(statex);
|
|||
|
GlobalMemoryStatusEx(&statex);
|
|||
|
memoryPercent = statex.dwMemoryLoad;
|
|||
|
memoryAll = statex.ullTotalPhys / MB;
|
|||
|
memoryFree = statex.ullAvailPhys / MB;
|
|||
|
memoryUse = memoryAll - memoryFree;
|
|||
|
QString msg = QString("CPUʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>: %1% | <20>ڴ<EFBFBD>ʹ<EFBFBD><CAB9><EFBFBD><EFBFBD>: %2%(<28><><EFBFBD><EFBFBD> %3 MB/<2F><> %4 MB) | ")
|
|||
|
.arg(cpu).arg(memoryPercent).arg(memoryUse).arg(memoryAll);
|
|||
|
labMemory->setText(msg);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int ShowCpuMemory::calCpuUsage()
|
|||
|
{
|
|||
|
HANDLE hEvent;
|
|||
|
bool res;
|
|||
|
static FILETIME preIdleTime;
|
|||
|
static FILETIME preKernelTime;
|
|||
|
static FILETIME preUserTime;
|
|||
|
FILETIME idleTime;
|
|||
|
FILETIME kernelTime;
|
|||
|
FILETIME userTime;
|
|||
|
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
|
|||
|
preIdleTime = idleTime;
|
|||
|
preKernelTime = kernelTime;
|
|||
|
preUserTime = userTime;
|
|||
|
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
|
|||
|
WaitForSingleObject(hEvent, 1000); //<2F>ȴ<EFBFBD>1000<30><30><EFBFBD><EFBFBD>
|
|||
|
res = GetSystemTimes(&idleTime, &kernelTime, &userTime);
|
|||
|
long long idle = CompareFileTime(preIdleTime, idleTime);
|
|||
|
long long kernel = CompareFileTime(preKernelTime, kernelTime);
|
|||
|
long long user = CompareFileTime(preUserTime, userTime);
|
|||
|
auto nCpuRate = (int)ceil(100.0 * (kernel + user - idle) / (kernel + user));
|
|||
|
return nCpuRate;
|
|||
|
}
|
|||
|
|
|||
|
//ʱ<><CAB1>ת<EFBFBD><D7AA>
|
|||
|
__int64 ShowCpuMemory::Filetime2Int64(const FILETIME* ftime)
|
|||
|
{
|
|||
|
LARGE_INTEGER li;
|
|||
|
li.LowPart = ftime->dwLowDateTime;
|
|||
|
li.HighPart = ftime->dwHighDateTime;
|
|||
|
return li.QuadPart;
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
__int64 ShowCpuMemory::CompareFileTime(FILETIME preTime, FILETIME nowTime)
|
|||
|
{
|
|||
|
return Filetime2Int64(&nowTime) - Filetime2Int64(&preTime);
|
|||
|
}
|