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.h | 126 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 src/core/host_timing.h (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h new file mode 100644 index 000000000..a3a32e087 --- /dev/null +++ b/src/core/host_timing.h @@ -0,0 +1,126 @@ +// Copyright 2020 yuzu Emulator Project +// Licensed under GPLv2 or any later version +// Refer to the license.txt file included. + +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "common/common_types.h" +#include "common/threadsafe_queue.h" + +namespace Core::HostTiming { + +/// A callback that may be scheduled for a particular core timing event. +using TimedCallback = std::function; +using sys_time_point = std::chrono::time_point; + +/// Contains the characteristics of a particular event. +struct EventType { + EventType(TimedCallback&& callback, std::string&& name) + : callback{std::move(callback)}, name{std::move(name)} {} + + /// The event's callback function. + TimedCallback callback; + /// A pointer to the name of the event. + const std::string name; +}; + +/** + * This is a system to schedule events into the emulated machine's future. Time is measured + * in main CPU clock cycles. + * + * To schedule an event, you first have to register its type. This is where you pass in the + * callback. You then schedule events using the type id you get back. + * + * The int cyclesLate that the callbacks get is how many cycles late it was. + * So to schedule a new event on a regular basis: + * inside callback: + * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever") + */ +class CoreTiming { +public: + CoreTiming(); + ~CoreTiming(); + + CoreTiming(const CoreTiming&) = delete; + CoreTiming(CoreTiming&&) = delete; + + CoreTiming& operator=(const CoreTiming&) = delete; + CoreTiming& operator=(CoreTiming&&) = delete; + + /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is + /// required to end slice - 1 and start slice 0 before the first cycle of code is executed. + void Initialize(); + + /// Tears down all timing related functionality. + void Shutdown(); + + /// Schedules an event in core timing + void ScheduleEvent(s64 ns_into_future, const std::shared_ptr& event_type, + u64 userdata = 0); + + void UnscheduleEvent(const std::shared_ptr& event_type, u64 userdata); + + /// We only permit one event of each type in the queue at a time. + void RemoveEvent(const std::shared_ptr& event_type); + + /// Returns current time in emulated CPU cycles + u64 GetCPUTicks() const; + + /// Returns current time in emulated in Clock cycles + u64 GetClockTicks() const; + + /// Returns current time in microseconds. + std::chrono::microseconds GetGlobalTimeUs() const; + + /// Returns current time in nanoseconds. + std::chrono::nanoseconds GetGlobalTimeNs() const; + +private: + struct Event; + + /// Clear all pending events. This should ONLY be done on exit. + void ClearPendingEvents(); + + static void ThreadEntry(CoreTiming& instance); + void Advance(); + + sys_time_point start_time; + + u64 global_timer = 0; + + std::chrono::nanoseconds start_point; + + // The queue is a min-heap using std::make_heap/push_heap/pop_heap. + // We don't use std::priority_queue because we need to be able to serialize, unserialize and + // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't + // accomodated by the standard adaptor class. + std::vector event_queue; + u64 event_fifo_id = 0; + + std::shared_ptr ev_lost; + bool is_set = false; + std::condition_variable condvar; + std::mutex inner_mutex; + std::unique_ptr timer_thread; + std::atomic shutting_down{}; +}; + +/// Creates a core timing event with the given name and callback. +/// +/// @param name The name of the core timing event to create. +/// @param callback The callback to execute for the event. +/// +/// @returns An EventType instance representing the created event. +/// +std::shared_ptr CreateEvent(std::string name, TimedCallback&& callback); + +} // 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.h | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index a3a32e087..1d053a7fa 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -14,13 +14,15 @@ #include #include "common/common_types.h" +#include "common/spin_lock.h" +#include "common/thread.h" #include "common/threadsafe_queue.h" namespace Core::HostTiming { /// A callback that may be scheduled for a particular core timing event. using TimedCallback = std::function; -using sys_time_point = std::chrono::time_point; +using sys_time_point = std::chrono::time_point; /// Contains the characteristics of a particular event. struct EventType { @@ -63,6 +65,23 @@ public: /// Tears down all timing related functionality. void Shutdown(); + /// Pauses/Unpauses the execution of the timer thread. + void Pause(bool is_paused); + + /// Pauses/Unpauses the execution of the timer thread and waits until paused. + void SyncPause(bool is_paused); + + /// Checks if core timing is running. + bool IsRunning(); + + /// Checks if the timer thread has started. + bool HasStarted() { + return has_started; + } + + /// Checks if there are any pending time events. + bool HasPendingEvents(); + /// Schedules an event in core timing void ScheduleEvent(s64 ns_into_future, const std::shared_ptr& event_type, u64 userdata = 0); @@ -107,11 +126,14 @@ private: u64 event_fifo_id = 0; std::shared_ptr ev_lost; - bool is_set = false; - std::condition_variable condvar; - std::mutex inner_mutex; + Common::Event event{}; + Common::SpinLock basic_lock{}; std::unique_ptr timer_thread; + std::atomic paused{}; + std::atomic paused_set{}; + std::atomic wait_set{}; std::atomic shutting_down{}; + std::atomic has_started{}; }; /// Creates a core timing event with the given name and callback. -- 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.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index 1d053a7fa..f04a150ee 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -17,12 +17,12 @@ #include "common/spin_lock.h" #include "common/thread.h" #include "common/threadsafe_queue.h" +#include "common/wall_clock.h" namespace Core::HostTiming { /// A callback that may be scheduled for a particular core timing event. using TimedCallback = std::function; -using sys_time_point = std::chrono::time_point; /// Contains the characteristics of a particular event. struct EventType { @@ -112,7 +112,7 @@ private: static void ThreadEntry(CoreTiming& instance); void Advance(); - sys_time_point start_time; + std::unique_ptr clock; u64 global_timer = 0; -- 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.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index f04a150ee..15a150904 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -145,4 +145,4 @@ private: /// std::shared_ptr CreateEvent(std::string name, TimedCallback&& callback); -} // 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.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index 15a150904..679fcf491 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -72,15 +72,15 @@ public: void SyncPause(bool is_paused); /// Checks if core timing is running. - bool IsRunning(); + bool IsRunning() const; /// Checks if the timer thread has started. - bool HasStarted() { + bool HasStarted() const { return has_started; } /// Checks if there are any pending time events. - bool HasPendingEvents(); + bool HasPendingEvents() const; /// Schedules an event in core timing void ScheduleEvent(s64 ns_into_future, const std::shared_ptr& event_type, -- 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.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index 679fcf491..cd44b308c 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -103,6 +103,9 @@ public: /// Returns current time in nanoseconds. std::chrono::nanoseconds GetGlobalTimeNs() const; + /// Checks for events manually and returns time in nanoseconds for next event, threadsafe. + std::optional Advance(); + private: struct Event; @@ -110,7 +113,7 @@ private: void ClearPendingEvents(); static void ThreadEntry(CoreTiming& instance); - void Advance(); + void ThreadLoop(); std::unique_ptr clock; @@ -128,6 +131,7 @@ private: std::shared_ptr ev_lost; Common::Event event{}; Common::SpinLock basic_lock{}; + Common::SpinLock advance_lock{}; std::unique_ptr timer_thread; std::atomic paused{}; std::atomic paused_set{}; -- 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.h | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index cd44b308c..5ad8c5f35 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -4,6 +4,7 @@ #pragma once +#include #include #include #include @@ -18,6 +19,7 @@ #include "common/thread.h" #include "common/threadsafe_queue.h" #include "common/wall_clock.h" +#include "core/hardware_properties.h" namespace Core::HostTiming { @@ -91,6 +93,11 @@ public: /// We only permit one event of each type in the queue at a time. void RemoveEvent(const std::shared_ptr& event_type); + + void AddTicks(std::size_t core_index, u64 ticks); + + void ResetTicks(std::size_t core_index); + /// Returns current time in emulated CPU cycles u64 GetCPUTicks() const; @@ -138,6 +145,8 @@ private: std::atomic wait_set{}; std::atomic shutting_down{}; std::atomic has_started{}; + + std::array, Core::Hardware::NUM_CPU_CORES> ticks_count{}; }; /// Creates a core timing event with the given name and callback. -- cgit v1.2.3 From 8f6ffcd5c450b06f641580c410e5a4756a897fcc Mon Sep 17 00:00:00 2001 From: Fernando Sahmkow Date: Sat, 22 Feb 2020 12:56:33 -0400 Subject: Host Timing: Correct clang format. --- src/core/host_timing.h | 1 - 1 file changed, 1 deletion(-) (limited to 'src/core/host_timing.h') diff --git a/src/core/host_timing.h b/src/core/host_timing.h index 5ad8c5f35..be6b68d7c 100644 --- a/src/core/host_timing.h +++ b/src/core/host_timing.h @@ -93,7 +93,6 @@ public: /// We only permit one event of each type in the queue at a time. void RemoveEvent(const std::shared_ptr& event_type); - void AddTicks(std::size_t core_index, u64 ticks); void ResetTicks(std::size_t core_index); -- cgit v1.2.3