System: Further improve frame timing consistency

This commit is contained in:
Connor McLaughlin 2023-01-12 17:44:32 +10:00
parent 559f14d27d
commit 3b038fd27d
2 changed files with 60 additions and 39 deletions

View file

@ -9,6 +9,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include "windows_headers.h" #include "windows_headers.h"
#else #else
#include <errno.h>
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
@ -97,19 +98,18 @@ void Timer::SleepUntil(Value value, bool exact)
{ {
if (exact) if (exact)
{ {
for (;;) // Even with the high-precision timer, it's not precise enough to wake us up *exactly* when we want
{ // to. Dropping off the last 0.5ms and spinning for it seems enough on my system (Win11 22H2).
const Value wake_at = value - ConvertMillisecondsToValue(0.5);
Value current = GetCurrentValue(); Value current = GetCurrentValue();
if (current >= value) if (wake_at > current)
break; SleepUntil(wake_at, false);
// spin for the last 1ms // And spin off whatever time is left.
if ((value - current) > ConvertMillisecondsToValue(1)) do
{ {
SleepUntil(value, false); current = GetCurrentValue();
continue; } while (current < value);
}
}
} }
else else
{ {
@ -187,26 +187,24 @@ void Timer::SleepUntil(Value value, bool exact)
{ {
if (exact) if (exact)
{ {
for (;;) static constexpr Value min_sleep_time = static_cast<Value>(0.5 * 1000000);
{ const Value wake_at = value - min_sleep_time;
Value current = GetCurrentValue(); Value current = GetCurrentValue();
if (current >= value) if (wake_at > current)
break; SleepUntil(wake_at, false);
static constexpr Value min_sleep_time = 1 * 1000000; // And spin off whatever time is left.
do
// spin for the last 1ms
if ((value - current) > min_sleep_time)
{ {
SleepUntil(value, false); current = GetCurrentValue();
continue; } while (current < value);
}
}
} }
else else
{ {
// Apple doesn't have TIMER_ABSTIME, so fall back to nanosleep in such a case. // Apple doesn't have TIMER_ABSTIME, so fall back to nanosleep in such a case.
#ifdef __APPLE__ #ifdef __APPLE__
for (;;)
{
const Value current_time = GetCurrentValue(); const Value current_time = GetCurrentValue();
if (value <= current_time) if (value <= current_time)
return; return;
@ -215,12 +213,26 @@ void Timer::SleepUntil(Value value, bool exact)
struct timespec ts; struct timespec ts;
ts.tv_sec = diff / UINT64_C(1000000000); ts.tv_sec = diff / UINT64_C(1000000000);
ts.tv_nsec = diff % UINT64_C(1000000000); ts.tv_nsec = diff % UINT64_C(1000000000);
nanosleep(&ts, nullptr);
// nanosleep() can return EINTR if interrupted by a signal.
if (nanosleep(&ts, nullptr) == EINTR)
continue;
else
break;
}
#else #else
struct timespec ts; struct timespec ts;
ts.tv_sec = value / UINT64_C(1000000000); ts.tv_sec = value / UINT64_C(1000000000);
ts.tv_nsec = value % UINT64_C(1000000000); ts.tv_nsec = value % UINT64_C(1000000000);
clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, nullptr);
for (;;)
{
// clock_nanosleep() can return EINTR if interrupted by a signal.
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts, nullptr) == EINTR)
continue;
else
break;
}
#endif #endif
} }
} }

View file

@ -1542,10 +1542,13 @@ void System::Execute()
s_presents_since_last_update++; s_presents_since_last_update++;
} }
System::UpdatePerformanceCounters();
if (s_throttler_enabled) if (s_throttler_enabled)
System::Throttle(); System::Throttle();
// Update perf counters *after* throttling, we want to measure from start-of-frame
// to start-of-frame, not end-of-frame to end-of-frame (will be noisy due to different
// amounts of computation happening in each frame).
System::UpdatePerformanceCounters();
} }
} }
@ -2189,7 +2192,7 @@ void System::Throttle()
// If we're running too slow, advance the next frame time based on the time we lost. Effectively skips // If we're running too slow, advance the next frame time based on the time we lost. Effectively skips
// running those frames at the intended time, because otherwise if we pause in the debugger, we'll run // running those frames at the intended time, because otherwise if we pause in the debugger, we'll run
// hundreds of frames when we resume. // hundreds of frames when we resume.
const Common::Timer::Value current_time = Common::Timer::GetCurrentValue(); Common::Timer::Value current_time = Common::Timer::GetCurrentValue();
if (current_time > s_next_frame_time) if (current_time > s_next_frame_time)
{ {
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);
@ -2197,7 +2200,13 @@ void System::Throttle()
return; return;
} }
Common::Timer::SleepUntil(s_next_frame_time, true); // Use a spinwait if we undersleep for all platforms except android.. don't want to burn battery.
// Linux also seems to do a much better job of waking up at the requested time.
#if defined(__linux__) || defined(__ANDROID__)
Common::Timer::SleepUntil(s_next_frame_time, g_settings.display_all_frames);
#else
Common::Timer::SleepUntil(s_next_frame_time, false);
#endif
} }
void System::RunFrames() void System::RunFrames()