rpcsx/rpcs3/Emu/perf_monitor.cpp

87 lines
1.8 KiB
C++
Raw Normal View History

2022-03-11 21:08:44 +01:00
#include "stdafx.h"
#include "perf_monitor.hpp"
#include "Emu/System.h"
2025-02-11 03:00:37 +01:00
#include "Emu/Cell/timers.hpp"
2022-03-11 21:08:44 +01:00
#include "util/cpu_stats.hpp"
#include "util/Thread.h"
2022-03-11 21:08:44 +01:00
LOG_CHANNEL(perf_log, "PERF");
2022-03-11 21:08:44 +01:00
void perf_monitor::operator()()
{
constexpr u64 update_interval_us = 1000000; // Update every second
constexpr u64 log_interval_us = 10000000; // Log every 10 seconds
u64 elapsed_us = 0;
utils::cpu_stats stats;
stats.init_cpu_query();
u32 logged_pause = 0;
u64 last_pause_time = umax;
std::vector<double> per_core_usage;
2024-08-16 19:06:20 +02:00
std::string msg;
2024-08-16 19:06:20 +02:00
for (u64 sleep_until = get_system_time(); thread_ctrl::state() != thread_state::aborting;)
2022-03-11 21:08:44 +01:00
{
2024-08-16 19:06:20 +02:00
thread_ctrl::wait_until(&sleep_until, update_interval_us);
2022-03-11 21:08:44 +01:00
elapsed_us += update_interval_us;
if (thread_ctrl::state() == thread_state::aborting)
{
break;
}
2022-03-11 21:08:44 +01:00
double total_usage = 0.0;
stats.get_per_core_usage(per_core_usage, total_usage);
if (elapsed_us >= log_interval_us)
{
elapsed_us = 0;
const bool is_paused = Emu.IsPaused();
const u64 pause_time = Emu.GetPauseTime();
if (!is_paused || last_pause_time != pause_time)
{
// Resumed or not paused since last check
logged_pause = 0;
last_pause_time = pause_time;
}
if (is_paused)
{
if (logged_pause >= 2)
{
// Let's not spam the log when emulation is paused
// But still emit the message two times so even paused state can be debugged and inspected
continue;
}
logged_pause++;
}
2024-08-16 19:06:20 +02:00
msg.clear();
fmt::append(msg, "CPU Usage: Total: %.1f%%", total_usage);
2022-03-11 21:08:44 +01:00
if (!per_core_usage.empty())
{
fmt::append(msg, ", Cores:");
}
2024-08-16 19:06:20 +02:00
for (usz i = 0; i < per_core_usage.size(); i++)
2022-03-11 21:08:44 +01:00
{
fmt::append(msg, "%s %.1f%%", i > 0 ? "," : "", per_core_usage[i]);
}
perf_log.notice("%s", msg);
2022-03-11 21:08:44 +01:00
}
}
}
perf_monitor::~perf_monitor()
{
}