Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update cpu usage calc #7

Merged
merged 13 commits into from
Nov 30, 2024
62 changes: 50 additions & 12 deletions src/ui/misc/SysStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

#if JUCE_WINDOWS
#include <Windows.h>
#include <Winternl.h>
#include <Psapi.h>

#pragma comment(lib,"ntdll.lib")
66hh marked this conversation as resolved.
Show resolved Hide resolved
#else //JUCE_WINDOWS
#include <iostream>
#include <fstream>
Expand All @@ -15,29 +18,64 @@ SysStatus::SysStatus() {
#if JUCE_WINDOWS
this->hProcess = GetCurrentProcess();

SYSTEM_BASIC_INFORMATION BasicInfo;
NtQuerySystemInformation(SystemBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL);
this->Processors = BasicInfo.NumberOfProcessors;

this->ProcessorInfo = malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->Processors);
66hh marked this conversation as resolved.
Show resolved Hide resolved

this->CPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors);
this->CPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors);

memset(this->CPUIdleTime, 0, sizeof(uint64_t) * this->Processors);
memset(this->CPUTotalTime, 0, sizeof(uint64_t) * this->Processors);

this->PreviousCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors);
this->PreviousCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors);

memset(this->PreviousCPUIdleTime, 0, sizeof(uint64_t) * this->Processors);
memset(this->PreviousCPUTotalTime, 0, sizeof(uint64_t) * this->Processors);

#endif //JUCE_WINDOWS
}

SysStatus::~SysStatus() {}

double SysStatus::getCPUUsage(CPUPercTemp& temp) {
#if JUCE_WINDOWS
FILETIME newIdleTime, newKernelTime, newUserTime;
GetSystemTimes(&newIdleTime, &newKernelTime, &newUserTime);
uint64_t SumIdleTime = 0;
66hh marked this conversation as resolved.
Show resolved Hide resolved
uint64_t SumTotalTime = 0;

NtQuerySystemInformation(SystemProcessorPerformanceInformation, this->ProcessorInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * Processors, NULL);

SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ProcessorInfo;

uint64_t newIdleTimeTemp = (*(ULARGE_INTEGER*)&newIdleTime).QuadPart;
uint64_t newKernelTimeTemp = (*(ULARGE_INTEGER*)&newKernelTime).QuadPart;
uint64_t newUserTimeTemp = (*(ULARGE_INTEGER*)&newUserTime).QuadPart;
for (int i = 0; i < Processors; i++)
{
66hh marked this conversation as resolved.
Show resolved Hide resolved
uint64_t DeltaCPUIdleTime;
66hh marked this conversation as resolved.
Show resolved Hide resolved
uint64_t DeltaCPUTotalTime;

uint64_t idle = newIdleTimeTemp - temp.cpuTemp[0];
uint64_t kernel = newKernelTimeTemp - temp.cpuTemp[1];
uint64_t user = newUserTimeTemp - temp.cpuTemp[2];
this->CPUIdleTime[i] = info[i].IdleTime.QuadPart;
this->CPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart;

temp.cpuTemp[0] = newIdleTimeTemp;
temp.cpuTemp[1] = newKernelTimeTemp;
temp.cpuTemp[2] = newUserTimeTemp;
DeltaCPUIdleTime = this->CPUIdleTime[i] - this->PreviousCPUIdleTime[i];
DeltaCPUTotalTime = this->CPUTotalTime[i] - this->PreviousCPUTotalTime[i];

SumIdleTime += DeltaCPUIdleTime;
SumTotalTime += DeltaCPUTotalTime;

this->PreviousCPUIdleTime[i] = this->CPUIdleTime[i];
this->PreviousCPUTotalTime[i] = this->CPUTotalTime[i];
}

return (kernel + user) / (double)(idle + kernel + user);
if (SumTotalTime != 0)
{
66hh marked this conversation as resolved.
Show resolved Hide resolved
return (100 - ((SumIdleTime * 100) / SumTotalTime)) / 100.0;
}
else
{
66hh marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}

#else //JUCE_WINDOWS
long total = 0, idle = 0;
Expand Down
6 changes: 6 additions & 0 deletions src/ui/misc/SysStatus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ class SysStatus final: private juce::DeletedAtShutdown {

private:
void* hProcess = nullptr;/**< For Windows Only */
uint64_t* CPUIdleTime;
66hh marked this conversation as resolved.
Show resolved Hide resolved
uint64_t* CPUTotalTime;
uint64_t* PreviousCPUIdleTime;
uint64_t* PreviousCPUTotalTime;
void* ProcessorInfo;
int Processors;

public:
static SysStatus* getInstance();
Expand Down