Fixed CPU usage indicator uncorrect behavior

This commit is contained in:
JackCarterSmith 2024-09-18 21:24:24 +02:00
parent b749c4894b
commit 451302d67e
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 30 additions and 15 deletions

View File

@ -26,9 +26,9 @@ void DebugUI::DrawDebugData(std::shared_ptr<sf::RenderWindow> context) {
std::ostringstream outStrStream;
outStrStream << std::fixed;
outStrStream << std::setprecision(0) << "CPU Usage: " << gDbgStats.cpu_usage << " % (cycle: " << std::setprecision(1) << gDbgStats.loop_time << " ms)" << std::endl;
outStrStream << std::setprecision(0) << "FPS: " << gDbgStats.fps << std::endl;
outStrStream << std::setprecision(1) << "RAM Usage: " << gDbgStats.ram_virt_usage << "MB(V) / " << gDbgStats.ram_phys_usage << "MB(P)";
outStrStream << std::setprecision(0) << "CPU: " << gDbgStats.cpu_usage << "% (cycle time: " << std::setprecision(3) << gDbgStats.loop_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;
gDbgText.setString(outStrStream.str());
context->draw(gDbgText);

View File

@ -21,13 +21,23 @@ public:
}
const double GetDeltaTime() {
return GetDeltaTime(false);
return GetDeltaTime(false, true);
}
const double GetDeltaTime(bool reset) {
const double GetDeltaTimeUs() {
return GetDeltaTime(false, false);
}
const double GetDeltaTime(bool reset, bool msMode) {
auto curr_time = std::chrono::high_resolution_clock::now();
double ret = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - t_previousDelta).count();
double ret = 0;
if (msMode)
ret = std::chrono::duration_cast<std::chrono::milliseconds>(curr_time - t_previousDelta).count();
else
ret = std::chrono::duration_cast<std::chrono::microseconds>(curr_time - t_previousDelta).count();
if (reset)
this->t_previousDelta = curr_time;
return ret;
}

View File

@ -13,7 +13,7 @@
using std::make_shared;
using std::make_unique;
#define TARGET_FPS (1000/60)
#define TARGET_FPS (60)
std::unique_ptr<SysTimer> mPerfsTimer = nullptr;
@ -55,14 +55,15 @@ GAME_STATUS Game::Tick(SysTimer& time) {
static std::vector<double> msTicksMonitoring;
static double mPrevdelta = 0;
static unsigned int mFrameCnt = 0;
double mDelta = time.GetDeltaTime();
double mDelta = time.GetDeltaTimeUs();
// Debug performance computations
if (mDbgUI != nullptr && mPerfsTimer != nullptr) {
msTicksMonitoring.push_back((mDelta - mPrevdelta) / TARGET_FPS);
msTicksMonitoring.push_back((mDelta - mPrevdelta) / (1000000/TARGET_FPS));
if (mPerfsTimer->GetDeltaTime() >= 1000) { // Re-evaluate CPU usage every seconds (for FP'S')
double cpuUsage = std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), 0) / msTicksMonitoring.size();
double cpuUsage = 100 * std::accumulate(msTicksMonitoring.begin(), msTicksMonitoring.end(), (double)(0)) / msTicksMonitoring.size();
mDbgUI->UpdateDebugData(cpuUsage, (mDelta - mPrevdelta), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000));
mDbgUI->UpdateDebugData(cpuUsage, ((mDelta - mPrevdelta) / 1000), mFrameCnt, (PerfsGetVirtMem() / 1000), (PerfsGetPhysMem() / 1000));
mFrameCnt = 0;
msTicksMonitoring.clear();
@ -71,14 +72,19 @@ GAME_STATUS Game::Tick(SysTimer& time) {
}
sf::Event event;
while (mMainWindow->pollEvent(event)) { // Wait for next frame (fixed FPS)
while (mMainWindow->pollEvent(event)) {
if (event.type == sf::Event::Closed)
mMainWindow->close();
}
// Update game stats and internal stuff
Update();
if (mDelta >= TARGET_FPS) { //TODO: Replace with global var and use dynamic window loader
// Ugly way to wait for next frame... Maybe create a separate thread for rendering?
//while(time.GetDeltaTime() < (1000/TARGET_FPS)) {}
// Process to the final rendering
if (time.GetDeltaTime() >= (1000/TARGET_FPS)) {
Render();
mFrameCnt++;
time.Reset();
@ -87,8 +93,7 @@ GAME_STATUS Game::Tick(SysTimer& time) {
mPrevdelta = mDelta;
}
sf::sleep(sf::milliseconds(TARGET_FPS - (mDelta - mPrevdelta)));
// Return the current process stat to the head process (TODO: convert to thread)
if (mMainWindow->isOpen())
return GAME_RUNNING;
else