From 1946f16385a8c1a1ed19025720e1fd8319354259 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 27 Sep 2024 23:47:12 +0200 Subject: [PATCH] Perfs monitor update --- Engine/Graphics/DebugUI.cpp | 7 ++++--- Engine/Graphics/DebugUI.hpp | 5 +++-- Engine/Graphics/UI.cpp | 2 +- Game.cpp | 21 +++++++++++++++------ 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/Engine/Graphics/DebugUI.cpp b/Engine/Graphics/DebugUI.cpp index e2a3c64..9bf3788 100644 --- a/Engine/Graphics/DebugUI.cpp +++ b/Engine/Graphics/DebugUI.cpp @@ -14,9 +14,10 @@ DebugUI::DebugUI() { gDbgText.setFillColor(sf::Color::White); } -void DebugUI::UpdateDebugData(const double cpu_usage, const double loop_time, const int fps, const std::size_t ram_virt_usage, const std::size_t ram_phys_usage) { +void DebugUI::UpdateDebugData(const double cpu_usage, const double cpu_time, const double cycle_time, const int fps, const std::size_t ram_virt_usage, const std::size_t ram_phys_usage) { gDbgStats.cpu_usage = cpu_usage; - gDbgStats.loop_time = loop_time; + gDbgStats.cpu_time = cpu_time; + gDbgStats.cycle_time = cycle_time; gDbgStats.fps = fps; gDbgStats.ram_virt_usage = ram_virt_usage; gDbgStats.ram_phys_usage = ram_phys_usage; @@ -26,7 +27,7 @@ void DebugUI::DrawDebugData(std::shared_ptr context) { std::ostringstream outStrStream; outStrStream << std::fixed; - outStrStream << std::setprecision(0) << "CPU: " << gDbgStats.cpu_usage << "% (cycle time: " << std::setprecision(3) << gDbgStats.loop_time << "ms)" << std::endl; + outStrStream << std::setprecision(0) << "CPU: " << gDbgStats.cpu_usage << "% (cycle: " << std::setprecision(3) << gDbgStats.cpu_time << "/" << gDbgStats.cycle_time << "ms)" << std::endl; outStrStream << std::setprecision(1) << "RAM: " << gDbgStats.ram_virt_usage << "MB(V) / " << gDbgStats.ram_phys_usage << "MB(P)" << std::endl; outStrStream << std::setprecision(0) << "FPS: " << gDbgStats.fps; diff --git a/Engine/Graphics/DebugUI.hpp b/Engine/Graphics/DebugUI.hpp index cecd2f2..dbd4d0d 100644 --- a/Engine/Graphics/DebugUI.hpp +++ b/Engine/Graphics/DebugUI.hpp @@ -10,7 +10,8 @@ struct sDbgStats { double cpu_usage; - double loop_time; + double cpu_time; + double cycle_time; int fps; std::size_t ram_virt_usage; std::size_t ram_phys_usage; @@ -21,7 +22,7 @@ public: DebugUI(); ~DebugUI() {} - void UpdateDebugData(const double cpu_usage, const double loop_time, const int fps, const std::size_t ram_virt_usage, const std::size_t ram_phys_usage); + void UpdateDebugData(const double cpu_usage, const double cpu_time, const double cycle_time, const int fps, const std::size_t ram_virt_usage, const std::size_t ram_phys_usage); void DrawDebugData(std::shared_ptr context); private: diff --git a/Engine/Graphics/UI.cpp b/Engine/Graphics/UI.cpp index 8ed3ce1..6b996a0 100644 --- a/Engine/Graphics/UI.cpp +++ b/Engine/Graphics/UI.cpp @@ -75,7 +75,7 @@ WorldUI::WorldUI() : UI() { sViewSettings.antialiasingLevel = 8; sViewSettings.sRgbCapable = true; - if (!mUIRender.create(1280, 560, sViewSettings)) // Only the upper of the screen is renderer as the UI hide whats left. + if (!mUIRender.create(1280, 324, sViewSettings)) // Only the upper of the screen is renderer as the UI hide whats left. throw std::runtime_error("Texture renderer creation failure"); mUIRender.setSmooth(true); diff --git a/Game.cpp b/Game.cpp index e9feffe..141e9e9 100644 --- a/Game.cpp +++ b/Game.cpp @@ -1,6 +1,7 @@ #include "Game.hpp" #include "icon.h" +#include #include #include @@ -54,16 +55,18 @@ void Game::InitWindows() { GAME_STATUS Game::Tick(SysTimer& time) { static std::vector msTicksMonitoring; static double mPrevdelta = 0; + static double mLastCycleSleepDelta = 0; static unsigned int mFrameCnt = 0; double mDelta = time.GetDeltaTimeUs(); // Debug performance computations if (mDbgUI != nullptr && mPerfsTimer != nullptr) { - msTicksMonitoring.push_back((mDelta - mPrevdelta) / (1000000.f / TARGET_FPS)); + msTicksMonitoring.push_back(((1000000.f / TARGET_FPS) - mLastCycleSleepDelta) * TARGET_FPS / 1000000.f); if (mPerfsTimer->GetDeltaTime() >= 1000) { // Every 1s - double cpuUsage = 100 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), (double)(0)) / msTicksMonitoring.size(); + // This average CPU/cycle_time method can monitor when CPU is in overload state (>0%) or in underload one (<0%) + double cpuUsage = 200 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), (double)(0)) / msTicksMonitoring.size() - 100; - mDbgUI->UpdateDebugData(cpuUsage, ((mDelta - mPrevdelta) / 1000), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); + mDbgUI->UpdateDebugData(cpuUsage, (((1000000.f / TARGET_FPS) - mLastCycleSleepDelta) / 1000), ((-mPrevdelta + time.GetDeltaTimeUs()) / 1000), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000)); mFrameCnt = 0; msTicksMonitoring.clear(); @@ -88,10 +91,16 @@ GAME_STATUS Game::Tick(SysTimer& time) { Render(); mFrameCnt++; time.Reset(); - mPrevdelta = -mDelta; - } else { - mPrevdelta = mDelta; + mDelta = -mDelta; } + mPrevdelta = mDelta; + + // Sleep for remaining time to avoid useless CPU usage + mLastCycleSleepDelta = (1000000.f / TARGET_FPS) - (-mPrevdelta + time.GetDeltaTimeUs()); + if (mLastCycleSleepDelta > 0) + sf::sleep(sf::microseconds(mLastCycleSleepDelta)); + else + mLastCycleSleepDelta = 0; // Return the current process stat to the head process (TODO: convert to thread) if (mMainWindow->isOpen())