From 7fc4dedaf8dcc30606d8376e68a25579091ee41d Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:10:23 +0800 Subject: [PATCH 01/11] Update cpu usage calc --- src/ui/misc/SysStatus.cpp | 62 +++++++++++++++++++++++++++++++-------- src/ui/misc/SysStatus.h | 6 ++++ 2 files changed, 56 insertions(+), 12 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 540bd7d..696619b 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -2,7 +2,10 @@ #if JUCE_WINDOWS #include +#include #include + +#pragma comment(lib,"ntdll.lib") #else //JUCE_WINDOWS #include #include @@ -15,6 +18,24 @@ 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); + + 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 } @@ -22,22 +43,39 @@ SysStatus::~SysStatus() {} double SysStatus::getCPUUsage(CPUPercTemp& temp) { #if JUCE_WINDOWS - FILETIME newIdleTime, newKernelTime, newUserTime; - GetSystemTimes(&newIdleTime, &newKernelTime, &newUserTime); + uint64_t SumIdleTime = 0; + 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++) + { + uint64_t DeltaCPUIdleTime; + 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) + { + return (100 - ((SumIdleTime * 100) / SumTotalTime)) / 100.0; + } + else + { + return 0; + } #else //JUCE_WINDOWS long total = 0, idle = 0; diff --git a/src/ui/misc/SysStatus.h b/src/ui/misc/SysStatus.h index 1b29f8b..3ede453 100644 --- a/src/ui/misc/SysStatus.h +++ b/src/ui/misc/SysStatus.h @@ -17,6 +17,12 @@ class SysStatus final: private juce::DeletedAtShutdown { private: void* hProcess = nullptr;/**< For Windows Only */ + uint64_t* CPUIdleTime; + uint64_t* CPUTotalTime; + uint64_t* PreviousCPUIdleTime; + uint64_t* PreviousCPUTotalTime; + void* ProcessorInfo; + int Processors; public: static SysStatus* getInstance(); From 4c55eb7da4b21b24dcbb968b950ff1a0c28c0b42 Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:16:52 +0800 Subject: [PATCH 02/11] Add free --- src/ui/misc/SysStatus.cpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 696619b..5e17022 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -1,4 +1,5 @@ #include "SysStatus.h" +#include #if JUCE_WINDOWS #include @@ -39,7 +40,25 @@ SysStatus::SysStatus() { #endif //JUCE_WINDOWS } -SysStatus::~SysStatus() {} +SysStatus::~SysStatus() { + +#if JUCE_WINDOWS + if (this->ProcessorInfo) + free(this->ProcessorInfo); + + if (this->CPUIdleTime) + free(this->CPUIdleTime); + if (this->CPUTotalTime) + free(this->CPUTotalTime); + + if (this->PreviousCPUIdleTime) + free(this->PreviousCPUIdleTime); + if (this->PreviousCPUTotalTime) + free(this->PreviousCPUTotalTime); + +#endif //JUCE_WINDOWS + +} double SysStatus::getCPUUsage(CPUPercTemp& temp) { #if JUCE_WINDOWS From 44bc69227b6fb4bae68623cc422c4a6d20a38cda Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:20:39 +0800 Subject: [PATCH 03/11] Add curly braces --- src/ui/misc/SysStatus.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 5e17022..265f4db 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -43,18 +43,23 @@ SysStatus::SysStatus() { SysStatus::~SysStatus() { #if JUCE_WINDOWS - if (this->ProcessorInfo) + if (this->ProcessorInfo) { free(this->ProcessorInfo); + } - if (this->CPUIdleTime) + if (this->CPUIdleTime) { free(this->CPUIdleTime); - if (this->CPUTotalTime) + } + if (this->CPUTotalTime) { free(this->CPUTotalTime); + } - if (this->PreviousCPUIdleTime) + if (this->PreviousCPUIdleTime) { free(this->PreviousCPUIdleTime); - if (this->PreviousCPUTotalTime) + } + if (this->PreviousCPUTotalTime) { free(this->PreviousCPUTotalTime); + } #endif //JUCE_WINDOWS From b011546b9e425c510868e1b91e6188e04865442a Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:23:05 +0800 Subject: [PATCH 04/11] Migrate to CMakeLists --- CMakeLists.txt | 2 +- src/ui/misc/SysStatus.cpp | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 457da8a..a21435b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -246,7 +246,7 @@ if (NOT MSVC) endif (NOT MSVC) target_link_libraries (VocalShaper PRIVATE juce-host-dev-kit::juce-full) if (WIN32) - target_link_libraries (VocalShaper PRIVATE Dbghelp) + target_link_libraries (VocalShaper PRIVATE Dbghelp "ntdll.lib") endif (WIN32) if (MSVC) target_link_options (VocalShaper PRIVATE /SUBSYSTEM:WINDOWS) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 265f4db..707fea8 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -5,8 +5,6 @@ #include #include #include - -#pragma comment(lib,"ntdll.lib") #else //JUCE_WINDOWS #include #include From 9847c99190c21d4cf981bcb8273376685bdad40e Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:36:55 +0800 Subject: [PATCH 05/11] Change to small hump --- src/ui/misc/SysStatus.cpp | 58 +++++++++++++++++++-------------------- src/ui/misc/SysStatus.h | 12 ++++---- 2 files changed, 35 insertions(+), 35 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 707fea8..b09b6dc 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -19,21 +19,21 @@ SysStatus::SysStatus() { SYSTEM_BASIC_INFORMATION BasicInfo; NtQuerySystemInformation(SystemBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL); - this->Processors = BasicInfo.NumberOfProcessors; + this->nProcessors = BasicInfo.NumberOfProcessors; - this->ProcessorInfo = malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->Processors); + this->ptrProcessorInfo = malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->nProcessors); - this->CPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors); - this->CPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors); + this->ptrCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); + this->ptrCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - memset(this->CPUIdleTime, 0, sizeof(uint64_t) * this->Processors); - memset(this->CPUTotalTime, 0, sizeof(uint64_t) * this->Processors); + memset(this->ptrCPUIdleTime, 0, sizeof(uint64_t) * this->nProcessors); + memset(this->ptrCPUTotalTime, 0, sizeof(uint64_t) * this->nProcessors); - this->PreviousCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors); - this->PreviousCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->Processors); + this->ptrPreviousCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); + this->ptrPreviousCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - memset(this->PreviousCPUIdleTime, 0, sizeof(uint64_t) * this->Processors); - memset(this->PreviousCPUTotalTime, 0, sizeof(uint64_t) * this->Processors); + memset(this->ptrPreviousCPUIdleTime, 0, sizeof(uint64_t) * this->nProcessors); + memset(this->ptrPreviousCPUTotalTime, 0, sizeof(uint64_t) * this->nProcessors); #endif //JUCE_WINDOWS } @@ -41,22 +41,22 @@ SysStatus::SysStatus() { SysStatus::~SysStatus() { #if JUCE_WINDOWS - if (this->ProcessorInfo) { - free(this->ProcessorInfo); + if (this->ptrProcessorInfo) { + free(this->ptrProcessorInfo); } - if (this->CPUIdleTime) { - free(this->CPUIdleTime); + if (this->ptrCPUIdleTime) { + free(this->ptrCPUIdleTime); } - if (this->CPUTotalTime) { - free(this->CPUTotalTime); + if (this->ptrCPUTotalTime) { + free(this->ptrCPUTotalTime); } - if (this->PreviousCPUIdleTime) { - free(this->PreviousCPUIdleTime); + if (this->ptrPreviousCPUIdleTime) { + free(this->ptrPreviousCPUIdleTime); } - if (this->PreviousCPUTotalTime) { - free(this->PreviousCPUTotalTime); + if (this->ptrPreviousCPUTotalTime) { + free(this->ptrPreviousCPUTotalTime); } #endif //JUCE_WINDOWS @@ -68,26 +68,26 @@ double SysStatus::getCPUUsage(CPUPercTemp& temp) { uint64_t SumIdleTime = 0; uint64_t SumTotalTime = 0; - NtQuerySystemInformation(SystemProcessorPerformanceInformation, this->ProcessorInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * Processors, NULL); + NtQuerySystemInformation(SystemProcessorPerformanceInformation, this->ptrProcessorInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->nProcessors, NULL); - SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ProcessorInfo; + SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ptrProcessorInfo; - for (int i = 0; i < Processors; i++) + for (int i = 0; i < this->nProcessors; i++) { uint64_t DeltaCPUIdleTime; uint64_t DeltaCPUTotalTime; - this->CPUIdleTime[i] = info[i].IdleTime.QuadPart; - this->CPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; + this->ptrCPUIdleTime[i] = info[i].IdleTime.QuadPart; + this->ptrCPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; - DeltaCPUIdleTime = this->CPUIdleTime[i] - this->PreviousCPUIdleTime[i]; - DeltaCPUTotalTime = this->CPUTotalTime[i] - this->PreviousCPUTotalTime[i]; + DeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; + DeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; SumIdleTime += DeltaCPUIdleTime; SumTotalTime += DeltaCPUTotalTime; - this->PreviousCPUIdleTime[i] = this->CPUIdleTime[i]; - this->PreviousCPUTotalTime[i] = this->CPUTotalTime[i]; + this->ptrPreviousCPUIdleTime[i] = this->ptrCPUIdleTime[i]; + this->ptrPreviousCPUTotalTime[i] = this->ptrCPUTotalTime[i]; } if (SumTotalTime != 0) diff --git a/src/ui/misc/SysStatus.h b/src/ui/misc/SysStatus.h index 3ede453..c11ac12 100644 --- a/src/ui/misc/SysStatus.h +++ b/src/ui/misc/SysStatus.h @@ -17,12 +17,12 @@ class SysStatus final: private juce::DeletedAtShutdown { private: void* hProcess = nullptr;/**< For Windows Only */ - uint64_t* CPUIdleTime; - uint64_t* CPUTotalTime; - uint64_t* PreviousCPUIdleTime; - uint64_t* PreviousCPUTotalTime; - void* ProcessorInfo; - int Processors; + uint64_t* ptrCPUIdleTime; + uint64_t* ptrCPUTotalTime; + uint64_t* ptrPreviousCPUIdleTime; + uint64_t* ptrPreviousCPUTotalTime; + void* ptrProcessorInfo; + int nProcessors; public: static SysStatus* getInstance(); From a9392568d4cb00980afb397dabcf9b66c0303546 Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:39:18 +0800 Subject: [PATCH 06/11] Change to small hump & Remove redundant line breaks --- src/ui/misc/SysStatus.cpp | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index b09b6dc..0f2e339 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -65,40 +65,36 @@ SysStatus::~SysStatus() { double SysStatus::getCPUUsage(CPUPercTemp& temp) { #if JUCE_WINDOWS - uint64_t SumIdleTime = 0; - uint64_t SumTotalTime = 0; + uint64_t nSumIdleTime = 0; + uint64_t nSumTotalTime = 0; NtQuerySystemInformation(SystemProcessorPerformanceInformation, this->ptrProcessorInfo, sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->nProcessors, NULL); SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ptrProcessorInfo; - for (int i = 0; i < this->nProcessors; i++) - { - uint64_t DeltaCPUIdleTime; - uint64_t DeltaCPUTotalTime; + for (int i = 0; i < this->nProcessors; i++) { + uint64_t nDeltaCPUIdleTime; + uint64_t nDeltaCPUTotalTime; this->ptrCPUIdleTime[i] = info[i].IdleTime.QuadPart; this->ptrCPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; - DeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; - DeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; + nDeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; + nDeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; - SumIdleTime += DeltaCPUIdleTime; - SumTotalTime += DeltaCPUTotalTime; + nSumIdleTime += nDeltaCPUIdleTime; + nSumTotalTime += nDeltaCPUTotalTime; this->ptrPreviousCPUIdleTime[i] = this->ptrCPUIdleTime[i]; this->ptrPreviousCPUTotalTime[i] = this->ptrCPUTotalTime[i]; } - if (SumTotalTime != 0) - { - return (100 - ((SumIdleTime * 100) / SumTotalTime)) / 100.0; - } - else - { - return 0; + if (nSumTotalTime) { + return (100 - ((nSumIdleTime * 100) / nSumTotalTime)) / 100.0; } + return 0; + #else //JUCE_WINDOWS long total = 0, idle = 0; From c8615e59658ac704e090b791d6e37cd11327d9c9 Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:42:03 +0800 Subject: [PATCH 07/11] Uniform indentation --- src/ui/misc/SysStatus.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 0f2e339..127c5d7 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -90,10 +90,10 @@ double SysStatus::getCPUUsage(CPUPercTemp& temp) { } if (nSumTotalTime) { - return (100 - ((nSumIdleTime * 100) / nSumTotalTime)) / 100.0; - } + return (100 - ((nSumIdleTime * 100) / nSumTotalTime)) / 100.0; + } - return 0; + return 0; #else //JUCE_WINDOWS long total = 0, idle = 0; From 72790a9381cebfdda6ee99572a5ddc32457f870f Mon Sep 17 00:00:00 2001 From: 66hh <49398720+66hh@users.noreply.github.com> Date: Sun, 1 Dec 2024 00:46:18 +0800 Subject: [PATCH 08/11] Adjust the definition position --- src/ui/misc/SysStatus.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 127c5d7..81c05fb 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -73,14 +73,12 @@ double SysStatus::getCPUUsage(CPUPercTemp& temp) { SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ptrProcessorInfo; for (int i = 0; i < this->nProcessors; i++) { - uint64_t nDeltaCPUIdleTime; - uint64_t nDeltaCPUTotalTime; this->ptrCPUIdleTime[i] = info[i].IdleTime.QuadPart; this->ptrCPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; - nDeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; - nDeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; + uint64_t nDeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; + uint64_t nDeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; nSumIdleTime += nDeltaCPUIdleTime; nSumTotalTime += nDeltaCPUTotalTime; From 01643a90b70642aac8c150c2e05c5f9fa606baa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A1=E5=B8=B8?= <3142324836@qq.com> Date: Sun, 1 Dec 2024 01:11:57 +0800 Subject: [PATCH 09/11] Removed cpu time array temp --- src/ui/misc/SysStatus.cpp | 48 ++++++++------------------------------- src/ui/misc/SysStatus.h | 10 +++----- 2 files changed, 12 insertions(+), 46 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 81c05fb..6941962 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -23,18 +23,6 @@ SysStatus::SysStatus() { this->ptrProcessorInfo = malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->nProcessors); - this->ptrCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - this->ptrCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - - memset(this->ptrCPUIdleTime, 0, sizeof(uint64_t) * this->nProcessors); - memset(this->ptrCPUTotalTime, 0, sizeof(uint64_t) * this->nProcessors); - - this->ptrPreviousCPUIdleTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - this->ptrPreviousCPUTotalTime = (uint64_t*)malloc(sizeof(uint64_t) * this->nProcessors); - - memset(this->ptrPreviousCPUIdleTime, 0, sizeof(uint64_t) * this->nProcessors); - memset(this->ptrPreviousCPUTotalTime, 0, sizeof(uint64_t) * this->nProcessors); - #endif //JUCE_WINDOWS } @@ -45,20 +33,6 @@ SysStatus::~SysStatus() { free(this->ptrProcessorInfo); } - if (this->ptrCPUIdleTime) { - free(this->ptrCPUIdleTime); - } - if (this->ptrCPUTotalTime) { - free(this->ptrCPUTotalTime); - } - - if (this->ptrPreviousCPUIdleTime) { - free(this->ptrPreviousCPUIdleTime); - } - if (this->ptrPreviousCPUTotalTime) { - free(this->ptrPreviousCPUTotalTime); - } - #endif //JUCE_WINDOWS } @@ -73,22 +47,18 @@ double SysStatus::getCPUUsage(CPUPercTemp& temp) { SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION* info = (SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION*)this->ptrProcessorInfo; for (int i = 0; i < this->nProcessors; i++) { + nSumIdleTime += info[i].IdleTime.QuadPart; + nSumTotalTime += info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; + } - this->ptrCPUIdleTime[i] = info[i].IdleTime.QuadPart; - this->ptrCPUTotalTime[i] = info[i].KernelTime.QuadPart + info[i].UserTime.QuadPart; - - uint64_t nDeltaCPUIdleTime = this->ptrCPUIdleTime[i] - this->ptrPreviousCPUIdleTime[i]; - uint64_t nDeltaCPUTotalTime = this->ptrCPUTotalTime[i] - this->ptrPreviousCPUTotalTime[i]; - - nSumIdleTime += nDeltaCPUIdleTime; - nSumTotalTime += nDeltaCPUTotalTime; + uint64_t nDeltaCPUIdleTime = nSumIdleTime - temp.cpuTemp[1]; + uint64_t nDeltaCPUTotalTime = nSumTotalTime - temp.cpuTemp[0]; - this->ptrPreviousCPUIdleTime[i] = this->ptrCPUIdleTime[i]; - this->ptrPreviousCPUTotalTime[i] = this->ptrCPUTotalTime[i]; - } + temp.cpuTemp[1] = nSumIdleTime; + temp.cpuTemp[0] = nSumTotalTime; - if (nSumTotalTime) { - return (100 - ((nSumIdleTime * 100) / nSumTotalTime)) / 100.0; + if (nDeltaCPUTotalTime) { + return (100 - ((nDeltaCPUIdleTime * 100.0) / nDeltaCPUTotalTime)) / 100.0; } return 0; diff --git a/src/ui/misc/SysStatus.h b/src/ui/misc/SysStatus.h index c11ac12..663a2a1 100644 --- a/src/ui/misc/SysStatus.h +++ b/src/ui/misc/SysStatus.h @@ -8,7 +8,7 @@ class SysStatus final: private juce::DeletedAtShutdown { ~SysStatus(); struct CPUPercTemp final { - std::array cpuTemp; + std::array cpuTemp; }; double getCPUUsage(CPUPercTemp& temp); @@ -17,12 +17,8 @@ class SysStatus final: private juce::DeletedAtShutdown { private: void* hProcess = nullptr;/**< For Windows Only */ - uint64_t* ptrCPUIdleTime; - uint64_t* ptrCPUTotalTime; - uint64_t* ptrPreviousCPUIdleTime; - uint64_t* ptrPreviousCPUTotalTime; - void* ptrProcessorInfo; - int nProcessors; + void* ptrProcessorInfo = nullptr; + int nProcessors = 0; public: static SysStatus* getInstance(); From 5719111413e24b1b79d8134a4dd18e69d3494229 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A1=E5=B8=B8?= <3142324836@qq.com> Date: Sun, 1 Dec 2024 01:27:04 +0800 Subject: [PATCH 10/11] Update cmake ntdll link name --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a21435b..5362634 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -246,7 +246,7 @@ if (NOT MSVC) endif (NOT MSVC) target_link_libraries (VocalShaper PRIVATE juce-host-dev-kit::juce-full) if (WIN32) - target_link_libraries (VocalShaper PRIVATE Dbghelp "ntdll.lib") + target_link_libraries (VocalShaper PRIVATE Dbghelp ntdll) endif (WIN32) if (MSVC) target_link_options (VocalShaper PRIVATE /SUBSYSTEM:WINDOWS) From 006e2da12ecad59e8dbb9cf589b6170b8236cc55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=84=A1=E5=B8=B8?= <3142324836@qq.com> Date: Sun, 1 Dec 2024 01:31:32 +0800 Subject: [PATCH 11/11] Add SysStatus basicInfo init --- src/ui/misc/SysStatus.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/misc/SysStatus.cpp b/src/ui/misc/SysStatus.cpp index 6941962..1b9852e 100644 --- a/src/ui/misc/SysStatus.cpp +++ b/src/ui/misc/SysStatus.cpp @@ -17,9 +17,9 @@ SysStatus::SysStatus() { #if JUCE_WINDOWS this->hProcess = GetCurrentProcess(); - SYSTEM_BASIC_INFORMATION BasicInfo; - NtQuerySystemInformation(SystemBasicInformation, &BasicInfo, sizeof(BasicInfo), NULL); - this->nProcessors = BasicInfo.NumberOfProcessors; + SYSTEM_BASIC_INFORMATION basicInfo{}; + NtQuerySystemInformation(SystemBasicInformation, &basicInfo, sizeof(basicInfo), NULL); + this->nProcessors = basicInfo.NumberOfProcessors; this->ptrProcessorInfo = malloc(sizeof(SYSTEM_PROCESSOR_PERFORMANCE_INFORMATION) * this->nProcessors);