Netplay: Fix high CPU usage

This commit is contained in:
Stenzek 2023-05-11 00:47:19 +10:00
parent a7e787456c
commit abc78936e7
2 changed files with 12 additions and 19 deletions

View File

@ -622,6 +622,10 @@ void Netplay::PollEnet(Common::Timer::Value until_time)
{ {
HandleEnetEvent(&event); HandleEnetEvent(&event);
// receiving can trigger sending
if (s_ggpo)
ggpo_network_idle(s_ggpo);
// make sure we get all events // make sure we get all events
current_time = Common::Timer::GetCurrentValue(); current_time = Common::Timer::GetCurrentValue();
continue; continue;
@ -1483,30 +1487,20 @@ void Netplay::Throttle()
{ {
const Common::Timer::Value diff = static_cast<s64>(current_time) - static_cast<s64>(s_next_frame_time); const Common::Timer::Value diff = static_cast<s64>(current_time) - static_cast<s64>(s_next_frame_time);
s_next_frame_time += (diff / s_frame_period) * s_frame_period; s_next_frame_time += (diff / s_frame_period) * s_frame_period;
PollEnet(0);
return; return;
} }
// Poll at 2ms throughout the sleep.
// This way the network traffic comes through as soon as possible.
const Common::Timer::Value sleep_period = Common::Timer::ConvertMillisecondsToValue(16);
while (s_state == SessionState::Running)
{
// TODO: make better, we can tell this function to stall until the next frame
PollEnet(0);
if (s_ggpo)
{
ggpo_network_idle(s_ggpo);
PollEnet(0);
}
// Poll at 1ms throughout the sleep.
// We need to send our ping requests through.
const Common::Timer::Value sleep_period = Common::Timer::ConvertMillisecondsToValue(2);
while (IsActive())
{
current_time = Common::Timer::GetCurrentValue(); current_time = Common::Timer::GetCurrentValue();
if (current_time >= s_next_frame_time) if (current_time >= s_next_frame_time)
break; break;
// Spin for the last millisecond. PollEnet(std::min(current_time + sleep_period, s_next_frame_time));
if ((s_next_frame_time - current_time) <= sleep_period)
Common::Timer::BusyWait(s_next_frame_time - current_time);
else
Common::Timer::SleepUntil(current_time + sleep_period, false);
} }
} }
@ -1531,7 +1525,6 @@ void Netplay::RunFrame()
if (!s_ggpo) if (!s_ggpo)
return; return;
// housekeeping // housekeeping
ggpo_network_idle(s_ggpo);
ggpo_idle(s_ggpo); ggpo_idle(s_ggpo);
// run game // run game
auto result = GGPO_OK; auto result = GGPO_OK;

View File

@ -2228,7 +2228,7 @@ int main(int argc, char* argv[])
Host::RunOnCPUThread([]() { Host::RunOnCPUThread([]() {
const bool first = (s_netplay_test == 0); const bool first = (s_netplay_test == 0);
if (!first) if (!first)
QtHost::RunOnUIThread([]() { g_main_window->move(g_main_window->pos() + QPoint(500, 0)); }); QtHost::RunOnUIThread([]() { g_main_window->move(g_main_window->pos() + QPoint(1200, 0)); });
const int h = first ? 1 : 2; const int h = first ? 1 : 2;
const int nh = first ? 2 : 1; const int nh = first ? 2 : 1;