From 62e35ffc0effddfacb73ebc766735148436d7331 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Wed, 5 Feb 2020 19:12:27 -0400 Subject: Core: Implement a Host Timer. --- src/core/host_timing.cpp | 161 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 src/core/host_timing.cpp (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp new file mode 100644 index 000000000..c02f571c6 --- /dev/null +++ b/src/core/host_timing.cpp @@ -0,0 +1,161 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#include "core/host_timing.h" + +#include +#include +#include +#include + +#include "common/assert.h" +#include "common/thread.h" +#include "core/core_timing_util.h" + +namespace Core::HostTiming { + +std::shared_ptr CreateEvent(std::string name, TimedCallback&& callback) { + return std::make_shared(std::move(callback), std::move(name)); +} + +struct CoreTiming::Event { + u64 time; + u64 fifo_order; + u64 userdata; + std::weak_ptr type; + + // Sort by time, unless the times are the same, in which case sort by + // the order added to the queue + friend bool operator>(const Event& left, const Event& right) { + return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order); + } + + friend bool operator<(const Event& left, const Event& right) { + return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order); + } +}; + +CoreTiming::CoreTiming() = default; +CoreTiming::~CoreTiming() = default; + +void CoreTiming::ThreadEntry(CoreTiming& instance) { + instance.Advance(); +} + +void CoreTiming::Initialize() { + event_fifo_id = 0; + const auto empty_timed_callback = [](u64, s64) {}; + ev_lost = CreateEvent("_lost_event", empty_timed_callback); + start_time = std::chrono::system_clock::now(); + timer_thread = std::make_unique(ThreadEntry, std::ref(*this)); +} + +void CoreTiming::Shutdown() { + std::unique_lock guard(inner_mutex); + shutting_down = true; + if (!is_set) { + is_set = true; + condvar.notify_one(); + } + inner_mutex.unlock(); + timer_thread->join(); + ClearPendingEvents(); +} + +void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr& event_type, + u64 userdata) { + std::lock_guard guard{inner_mutex}; + const u64 timeout = static_cast(GetGlobalTimeNs().count() + ns_into_future); + + event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); + + std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + if (!is_set) { + is_set = true; + condvar.notify_one(); + } +} + +void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, u64 userdata) { + std::lock_guard guard{inner_mutex}; + + const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { + return e.type.lock().get() == event_type.get() && e.userdata == userdata; + }); + + // Removing random items breaks the invariant so we have to re-establish it. + if (itr != event_queue.end()) { + event_queue.erase(itr, event_queue.end()); + std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + } +} + +u64 CoreTiming::GetCPUTicks() const { + std::chrono::nanoseconds time_now = GetGlobalTimeNs(); + return Core::Timing::nsToCycles(time_now); +} + +u64 CoreTiming::GetClockTicks() const { + std::chrono::nanoseconds time_now = GetGlobalTimeNs(); + return Core::Timing::nsToClockCycles(time_now); +} + +void CoreTiming::ClearPendingEvents() { + event_queue.clear(); +} + +void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { + std::lock_guard guard{inner_mutex}; + + const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { + return e.type.lock().get() == event_type.get(); + }); + + // Removing random items breaks the invariant so we have to re-establish it. + if (itr != event_queue.end()) { + event_queue.erase(itr, event_queue.end()); + std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + } +} + +void CoreTiming::Advance() { + while (true) { + std::unique_lock guard(inner_mutex); + + global_timer = GetGlobalTimeNs().count(); + + while (!event_queue.empty() && event_queue.front().time <= global_timer) { + Event evt = std::move(event_queue.front()); + std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + event_queue.pop_back(); + inner_mutex.unlock(); + + if (auto event_type{evt.type.lock()}) { + event_type->callback(evt.userdata, global_timer - evt.time); + } + + inner_mutex.lock(); + } + auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer); + condvar.wait_for(guard, next_time, [this] { return is_set; }); + is_set = false; + if (shutting_down) { + break; + } + } +} + +std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { + sys_time_point current = std::chrono::system_clock::now(); + auto elapsed = current - start_time; + return std::chrono::duration_cast(elapsed); +} + +std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { + sys_time_point current = std::chrono::system_clock::now(); + auto elapsed = current - start_time; + return std::chrono::duration_cast(elapsed); +} + +} // namespace Core::Timing -- cgit v1.2.3 From 0f8e5a146563d1f245f8f62cb931dc1e0b55de2f Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 8 Feb 2020 12:48:57 -0400 Subject: Tests: Add base tests to host timing --- src/core/host_timing.cpp | 101 ++++++++++++++++++++++++++++++----------------- 1 file changed, 64 insertions(+), 37 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index c02f571c6..d9514b2c5 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -10,7 +10,6 @@ #include #include "common/assert.h" -#include "common/thread.h" #include "core/core_timing_util.h" namespace Core::HostTiming { @@ -47,39 +46,55 @@ void CoreTiming::Initialize() { event_fifo_id = 0; const auto empty_timed_callback = [](u64, s64) {}; ev_lost = CreateEvent("_lost_event", empty_timed_callback); - start_time = std::chrono::system_clock::now(); + start_time = std::chrono::steady_clock::now(); timer_thread = std::make_unique(ThreadEntry, std::ref(*this)); } void CoreTiming::Shutdown() { - std::unique_lock guard(inner_mutex); + paused = true; shutting_down = true; - if (!is_set) { - is_set = true; - condvar.notify_one(); - } - inner_mutex.unlock(); + event.Set(); timer_thread->join(); ClearPendingEvents(); + timer_thread.reset(); + has_started = false; +} + +void CoreTiming::Pause(bool is_paused) { + paused = is_paused; +} + +void CoreTiming::SyncPause(bool is_paused) { + if (is_paused == paused && paused_set == paused) { + return; + } + Pause(is_paused); + event.Set(); + while (paused_set != is_paused); +} + +bool CoreTiming::IsRunning() { + return !paused_set; +} + +bool CoreTiming::HasPendingEvents() { + return !(wait_set && event_queue.empty()); } void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr& event_type, u64 userdata) { - std::lock_guard guard{inner_mutex}; + basic_lock.lock(); const u64 timeout = static_cast(GetGlobalTimeNs().count() + ns_into_future); event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); - if (!is_set) { - is_set = true; - condvar.notify_one(); - } + basic_lock.unlock(); + event.Set(); } void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, u64 userdata) { - std::lock_guard guard{inner_mutex}; - + basic_lock.lock(); const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { return e.type.lock().get() == event_type.get() && e.userdata == userdata; }); @@ -89,6 +104,7 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, u event_queue.erase(itr, event_queue.end()); std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); } + basic_lock.unlock(); } u64 CoreTiming::GetCPUTicks() const { @@ -106,7 +122,7 @@ void CoreTiming::ClearPendingEvents() { } void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { - std::lock_guard guard{inner_mutex}; + basic_lock.lock(); const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { return e.type.lock().get() == event_type.get(); @@ -117,43 +133,54 @@ void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { event_queue.erase(itr, event_queue.end()); std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); } + basic_lock.unlock(); } void CoreTiming::Advance() { - while (true) { - std::unique_lock guard(inner_mutex); - - global_timer = GetGlobalTimeNs().count(); - - while (!event_queue.empty() && event_queue.front().time <= global_timer) { - Event evt = std::move(event_queue.front()); - std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); - event_queue.pop_back(); - inner_mutex.unlock(); + has_started = true; + while (!shutting_down) { + while (!paused) { + paused_set = false; + basic_lock.lock(); + global_timer = GetGlobalTimeNs().count(); + + while (!event_queue.empty() && event_queue.front().time <= global_timer) { + Event evt = std::move(event_queue.front()); + std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + event_queue.pop_back(); + basic_lock.unlock(); + + if (auto event_type{evt.type.lock()}) { + event_type->callback(evt.userdata, global_timer - evt.time); + } + + basic_lock.lock(); + } - if (auto event_type{evt.type.lock()}) { - event_type->callback(evt.userdata, global_timer - evt.time); + if (!event_queue.empty()) { + std::chrono::nanoseconds next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer); + basic_lock.unlock(); + event.WaitFor(next_time); + } else { + basic_lock.unlock(); + wait_set = true; + event.Wait(); } - inner_mutex.lock(); - } - auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer); - condvar.wait_for(guard, next_time, [this] { return is_set; }); - is_set = false; - if (shutting_down) { - break; + wait_set = false; } + paused_set = true; } } std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { - sys_time_point current = std::chrono::system_clock::now(); + sys_time_point current = std::chrono::steady_clock::now(); auto elapsed = current - start_time; return std::chrono::duration_cast(elapsed); } std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { - sys_time_point current = std::chrono::system_clock::now(); + sys_time_point current = std::chrono::steady_clock::now(); auto elapsed = current - start_time; return std::chrono::duration_cast(elapsed); } -- cgit v1.2.3 From 234b5ff6a999d7d69cdcdf214e0c3984cdab11cf Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sun, 9 Feb 2020 16:53:22 -0400 Subject: Common: Implement WallClock Interface and implement a native clock for x64 --- src/core/host_timing.cpp | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index d9514b2c5..ef9977b76 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -35,7 +35,11 @@ struct CoreTiming::Event { } }; -CoreTiming::CoreTiming() = default; +CoreTiming::CoreTiming() { + Common::WallClock* wall = Common::CreateBestMatchingClock(Core::Timing::BASE_CLOCK_RATE, Core::Timing::CNTFREQ); + clock = std::unique_ptr(wall); +} + CoreTiming::~CoreTiming() = default; void CoreTiming::ThreadEntry(CoreTiming& instance) { @@ -46,7 +50,6 @@ void CoreTiming::Initialize() { event_fifo_id = 0; const auto empty_timed_callback = [](u64, s64) {}; ev_lost = CreateEvent("_lost_event", empty_timed_callback); - start_time = std::chrono::steady_clock::now(); timer_thread = std::make_unique(ThreadEntry, std::ref(*this)); } @@ -108,13 +111,11 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, u } u64 CoreTiming::GetCPUTicks() const { - std::chrono::nanoseconds time_now = GetGlobalTimeNs(); - return Core::Timing::nsToCycles(time_now); + return clock->GetCPUCycles(); } u64 CoreTiming::GetClockTicks() const { - std::chrono::nanoseconds time_now = GetGlobalTimeNs(); - return Core::Timing::nsToClockCycles(time_now); + return clock->GetClockCycles(); } void CoreTiming::ClearPendingEvents() { @@ -174,15 +175,11 @@ void CoreTiming::Advance() { } std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { - sys_time_point current = std::chrono::steady_clock::now(); - auto elapsed = current - start_time; - return std::chrono::duration_cast(elapsed); + return clock->GetTimeNS(); } std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { - sys_time_point current = std::chrono::steady_clock::now(); - auto elapsed = current - start_time; - return std::chrono::duration_cast(elapsed); + return clock->GetTimeUS(); } } // namespace Core::Timing -- cgit v1.2.3 From e3524d114246a9221c766bdf1992777b208cbd67 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Mon, 10 Feb 2020 11:20:40 -0400 Subject: Common: Refactor & Document Wall clock. --- src/core/host_timing.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index ef9977b76..4ccf7c6c1 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -36,8 +36,7 @@ struct CoreTiming::Event { }; CoreTiming::CoreTiming() { - Common::WallClock* wall = Common::CreateBestMatchingClock(Core::Timing::BASE_CLOCK_RATE, Core::Timing::CNTFREQ); - clock = std::unique_ptr(wall); + clock = Common::CreateBestMatchingClock(Core::Timing::BASE_CLOCK_RATE, Core::Timing::CNTFREQ); } CoreTiming::~CoreTiming() = default; -- cgit v1.2.3 From 1bd706344e2381e11245b2f0bdc291429e46c634 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Mon, 10 Feb 2020 13:33:13 -0400 Subject: Common/Tests: Clang Format. --- src/core/host_timing.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index 4ccf7c6c1..c734a118e 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -72,7 +72,8 @@ void CoreTiming::SyncPause(bool is_paused) { } Pause(is_paused); event.Set(); - while (paused_set != is_paused); + while (paused_set != is_paused) + ; } bool CoreTiming::IsRunning() { @@ -158,7 +159,8 @@ void CoreTiming::Advance() { } if (!event_queue.empty()) { - std::chrono::nanoseconds next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer); + std::chrono::nanoseconds next_time = + std::chrono::nanoseconds(event_queue.front().time - global_timer); basic_lock.unlock(); event.WaitFor(next_time); } else { @@ -181,4 +183,4 @@ std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { return clock->GetTimeUS(); } -} // namespace Core::Timing +} // namespace Core::HostTiming -- cgit v1.2.3 From 1f7dd36499786d373b143a4437d4c32e077a32aa Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Mon, 10 Feb 2020 14:45:08 -0400 Subject: Common/Tests: Address Feedback --- src/core/host_timing.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index c734a118e..be80d9f8e 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -76,11 +76,11 @@ void CoreTiming::SyncPause(bool is_paused) { ; } -bool CoreTiming::IsRunning() { +bool CoreTiming::IsRunning() const { return !paused_set; } -bool CoreTiming::HasPendingEvents() { +bool CoreTiming::HasPendingEvents() const { return !(wait_set && event_queue.empty()); } -- cgit v1.2.3 From 49a7e0984a1210832b8be24433a95711c7ce029b Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Mon, 10 Feb 2020 15:02:04 -0400 Subject: Core/HostTiming: Allow events to be advanced manually. --- src/core/host_timing.cpp | 61 ++++++++++++++++++++++++++++-------------------- 1 file changed, 36 insertions(+), 25 deletions(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index be80d9f8e..5d35a96b1 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -42,7 +42,7 @@ CoreTiming::CoreTiming() { CoreTiming::~CoreTiming() = default; void CoreTiming::ThreadEntry(CoreTiming& instance) { - instance.Advance(); + instance.ThreadLoop(); } void CoreTiming::Initialize() { @@ -137,38 +137,49 @@ void CoreTiming::RemoveEvent(const std::shared_ptr& event_type) { basic_lock.unlock(); } -void CoreTiming::Advance() { - has_started = true; - while (!shutting_down) { - while (!paused) { - paused_set = false; - basic_lock.lock(); - global_timer = GetGlobalTimeNs().count(); +std::optional CoreTiming::Advance() { + advance_lock.lock(); + basic_lock.lock(); + global_timer = GetGlobalTimeNs().count(); - while (!event_queue.empty() && event_queue.front().time <= global_timer) { - Event evt = std::move(event_queue.front()); - std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); - event_queue.pop_back(); - basic_lock.unlock(); + while (!event_queue.empty() && event_queue.front().time <= global_timer) { + Event evt = std::move(event_queue.front()); + std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); + event_queue.pop_back(); + basic_lock.unlock(); - if (auto event_type{evt.type.lock()}) { - event_type->callback(evt.userdata, global_timer - evt.time); - } + if (auto event_type{evt.type.lock()}) { + event_type->callback(evt.userdata, global_timer - evt.time); + } - basic_lock.lock(); - } + basic_lock.lock(); + } - if (!event_queue.empty()) { - std::chrono::nanoseconds next_time = - std::chrono::nanoseconds(event_queue.front().time - global_timer); - basic_lock.unlock(); - event.WaitFor(next_time); + if (!event_queue.empty()) { + const u64 next_time = event_queue.front().time - global_timer; + basic_lock.unlock(); + advance_lock.unlock(); + return next_time; + } else { + basic_lock.unlock(); + advance_lock.unlock(); + return std::nullopt; + } +} + +void CoreTiming::ThreadLoop() { + has_started = true; + while (!shutting_down) { + while (!paused) { + paused_set = false; + const auto next_time = Advance(); + if (next_time) { + std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); + event.WaitFor(next_time_ns); } else { - basic_lock.unlock(); wait_set = true; event.Wait(); } - wait_set = false; } paused_set = true; -- cgit v1.2.3 From 96b2d8419c94f9bcb5f2f970bbb453aa7383b510 Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 15 Feb 2020 13:56:50 -0400 Subject: HostTiming: Correct rebase and implement AddTicks. --- src/core/host_timing.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/core/host_timing.cpp') diff --git a/src/core/host_timing.cpp b/src/core/host_timing.cpp index 5d35a96b1..2f40de1a1 100644 --- a/src/core/host_timing.cpp +++ b/src/core/host_timing.cpp @@ -36,7 +36,8 @@ struct CoreTiming::Event { }; CoreTiming::CoreTiming() { - clock = Common::CreateBestMatchingClock(Core::Timing::BASE_CLOCK_RATE, Core::Timing::CNTFREQ); + clock = + Common::CreateBestMatchingClock(Core::Hardware::BASE_CLOCK_RATE, Core::Hardware::CNTFREQ); } CoreTiming::~CoreTiming() = default; @@ -110,6 +111,14 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr& event_type, u basic_lock.unlock(); } +void CoreTiming::AddTicks(std::size_t core_index, u64 ticks) { + ticks_count[core_index] += ticks; +} + +void CoreTiming::ResetTicks(std::size_t core_index) { + ticks_count[core_index] = 0; +} + u64 CoreTiming::GetCPUTicks() const { return clock->GetCPUCycles(); } -- cgit v1.2.3