From 83a24ad638a2c3ff628fe109391586be9bd3a45d Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Wed, 27 Jul 2022 22:31:41 +0100 Subject: Make coretiming waiting more accurate --- src/core/core_timing.cpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'src/core/core_timing.cpp') diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 2dbb99c8b..b45c1b918 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -243,17 +243,17 @@ std::optional CoreTiming::Advance() { basic_lock.lock(); if (evt.reschedule_time != 0) { + const auto next_schedule_time{new_schedule_time.has_value() + ? new_schedule_time.value().count() + : evt.reschedule_time}; + // If this event was scheduled into a pause, its time now is going to be way behind. // Re-set this event to continue from the end of the pause. - auto next_time{evt.time + evt.reschedule_time}; + auto next_time{evt.time + next_schedule_time}; if (evt.time < pause_end_time) { - next_time = pause_end_time + evt.reschedule_time; + next_time = pause_end_time + next_schedule_time; } - const auto next_schedule_time{new_schedule_time.has_value() - ? new_schedule_time.value().count() - : evt.reschedule_time}; - event_queue.emplace_back( Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time}); std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); @@ -264,8 +264,7 @@ std::optional CoreTiming::Advance() { } if (!event_queue.empty()) { - const s64 next_time = event_queue.front().time - global_timer; - return next_time; + return event_queue.front().time; } else { return std::nullopt; } @@ -278,11 +277,28 @@ void CoreTiming::ThreadLoop() { paused_set = false; const auto next_time = Advance(); if (next_time) { - if (*next_time > 0) { - std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); - event.WaitFor(next_time_ns); + // There are more events left in the queue, sleep until the next event. + const auto diff_ns{*next_time - GetGlobalTimeNs().count()}; + if (diff_ns > 0) { + // Only try to sleep if the remaining time is >= 1ms. Take off 500 microseconds + // from the target time to account for possible over-sleeping, and spin the + // remaining. + const auto sleep_time_ns{diff_ns - 500LL * 1'000LL}; + const auto sleep_time_ms{sleep_time_ns / 1'000'000LL}; + if (sleep_time_ms >= 1) { + event.WaitFor(std::chrono::nanoseconds(sleep_time_ns)); + } + + const auto end_time{std::chrono::nanoseconds(*next_time)}; + while (!paused && !event.IsSet() && GetGlobalTimeNs() < end_time) { + } + + if (event.IsSet()) { + event.Reset(); + } } } else { + // Queue is empty, wait until another event is scheduled and signals us to continue. wait_set = true; event.Wait(); } -- cgit v1.2.3 From 658e1ee4267f0475445c7152560783444fcda7d2 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Tue, 2 Aug 2022 05:28:31 +0100 Subject: Add missing looping event schedule signal --- src/core/core_timing.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/core/core_timing.cpp') diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index b45c1b918..a75bfea60 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -143,13 +143,17 @@ void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, std::chrono::nanoseconds resched_time, const std::shared_ptr& event_type, std::uintptr_t user_data, bool absolute_time) { - std::scoped_lock scope{basic_lock}; - const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; + { + std::scoped_lock scope{basic_lock}; + const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; + + event_queue.emplace_back( + Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); - event_queue.emplace_back( - Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); + std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + } - std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + event.Set(); } void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, -- cgit v1.2.3 From 606cdb17d3b8f3b4898c1f0a87691058074ad11a Mon Sep 17 00:00:00 2001 From: Morph <39850852+Morph1984@users.noreply.github.com> Date: Mon, 1 Aug 2022 23:34:34 -0400 Subject: core_timing: Sleep in discrete intervals, yield during spin --- src/core/core_timing.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'src/core/core_timing.cpp') diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index a75bfea60..8d1ee3b51 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -281,20 +281,21 @@ void CoreTiming::ThreadLoop() { paused_set = false; const auto next_time = Advance(); if (next_time) { - // There are more events left in the queue, sleep until the next event. - const auto diff_ns{*next_time - GetGlobalTimeNs().count()}; - if (diff_ns > 0) { - // Only try to sleep if the remaining time is >= 1ms. Take off 500 microseconds - // from the target time to account for possible over-sleeping, and spin the - // remaining. - const auto sleep_time_ns{diff_ns - 500LL * 1'000LL}; - const auto sleep_time_ms{sleep_time_ns / 1'000'000LL}; - if (sleep_time_ms >= 1) { - event.WaitFor(std::chrono::nanoseconds(sleep_time_ns)); + // There are more events left in the queue, wait until the next event. + const auto wait_time = *next_time - GetGlobalTimeNs().count(); + if (wait_time > 0) { + // Assume a timer resolution of 1ms. + static constexpr s64 TimerResolutionNS = 1000000; + + // Sleep in discrete intervals of the timer resolution, and spin the rest. + const auto sleep_time = wait_time - (wait_time % TimerResolutionNS); + if (sleep_time > 0) { + event.WaitFor(std::chrono::nanoseconds(sleep_time)); } - const auto end_time{std::chrono::nanoseconds(*next_time)}; - while (!paused && !event.IsSet() && GetGlobalTimeNs() < end_time) { + while (!paused && !event.IsSet() && GetGlobalTimeNs().count() < *next_time) { + // Yield to reduce thread starvation. + std::this_thread::yield(); } if (event.IsSet()) { -- cgit v1.2.3 From e93e898df528d013e2e0cfeba22e2b6d76bf99b6 Mon Sep 17 00:00:00 2001 From: Kelebek1 Date: Sat, 10 Sep 2022 21:14:03 +0100 Subject: Remove pause callbacks from coretiming --- src/core/core_timing.cpp | 14 -------------- 1 file changed, 14 deletions(-) (limited to 'src/core/core_timing.cpp') diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp index 2dbb99c8b..5375a5d59 100644 --- a/src/core/core_timing.cpp +++ b/src/core/core_timing.cpp @@ -73,7 +73,6 @@ void CoreTiming::Shutdown() { if (timer_thread) { timer_thread->join(); } - pause_callbacks.clear(); ClearPendingEvents(); timer_thread.reset(); has_started = false; @@ -86,10 +85,6 @@ void CoreTiming::Pause(bool is_paused) { if (!is_paused) { pause_end_time = GetGlobalTimeNs().count(); } - - for (auto& cb : pause_callbacks) { - cb(is_paused); - } } void CoreTiming::SyncPause(bool is_paused) { @@ -110,10 +105,6 @@ void CoreTiming::SyncPause(bool is_paused) { if (!is_paused) { pause_end_time = GetGlobalTimeNs().count(); } - - for (auto& cb : pause_callbacks) { - cb(is_paused); - } } bool CoreTiming::IsRunning() const { @@ -219,11 +210,6 @@ void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { } } -void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) { - std::scoped_lock lock{basic_lock}; - pause_callbacks.emplace_back(std::move(callback)); -} - std::optional CoreTiming::Advance() { std::scoped_lock lock{advance_lock, basic_lock}; global_timer = GetGlobalTimeNs().count(); -- cgit v1.2.3