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/CMakeLists.txt | 2 + src/core/core_timing_util.cpp | 5 ++ src/core/core_timing_util.h | 1 + src/core/host_timing.cpp | 161 ++++++++++++++++++++++++++++++++++++++++++ src/core/host_timing.h | 126 +++++++++++++++++++++++++++++++++ 5 files changed, 295 insertions(+) create mode 100644 src/core/host_timing.cpp create mode 100644 src/core/host_timing.h (limited to 'src/core') diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 47418006b..c0d068376 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -547,6 +547,8 @@ add_library(core STATIC hle/service/vi/vi_u.h hle/service/wlan/wlan.cpp hle/service/wlan/wlan.h + host_timing.cpp + host_timing.h loader/deconstructed_rom_directory.cpp loader/deconstructed_rom_directory.h loader/elf.cpp diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index de50d3b14..f42666b4d 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -49,6 +49,11 @@ s64 nsToCycles(std::chrono::nanoseconds ns) { return (Hardware::BASE_CLOCK_RATE * ns.count()) / 1000000000; } +u64 nsToClockCycles(std::chrono::nanoseconds ns) { + const u128 temporal = Common::Multiply64Into128(ns.count(), CNTFREQ); + return Common::Divide128On32(temporal, 1000000000).first; +} + u64 CpuCyclesToClockCycles(u64 ticks) { const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); return Common::Divide128On32(temporal, static_cast(Hardware::BASE_CLOCK_RATE)).first; diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index addc72b19..65fb7368b 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -13,6 +13,7 @@ namespace Core::Timing { s64 msToCycles(std::chrono::milliseconds ms); s64 usToCycles(std::chrono::microseconds us); s64 nsToCycles(std::chrono::nanoseconds ns); +u64 nsToClockCycles(std::chrono::nanoseconds ns); inline std::chrono::milliseconds CyclesToMs(s64 cycles) { return std::chrono::milliseconds(cycles * 1000 / Hardware::BASE_CLOCK_RATE); 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 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.cpp | 101 ++++++++++++++++++++++++++++++----------------- src/core/host_timing.h | 30 ++++++++++++-- 2 files changed, 90 insertions(+), 41 deletions(-) (limited to 'src/core') 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); } 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.cpp | 21 +++++++++------------ src/core/host_timing.h | 4 ++-- 2 files changed, 11 insertions(+), 14 deletions(-) (limited to 'src/core') 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 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 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') 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 +++++--- src/core/host_timing.h | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'src/core') 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 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/core_timing_util.cpp | 14 ++++++++++++-- src/core/core_timing_util.h | 2 ++ src/core/host_timing.cpp | 4 ++-- src/core/host_timing.h | 6 +++--- 4 files changed, 19 insertions(+), 7 deletions(-) (limited to 'src/core') diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp index f42666b4d..be34b26fe 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp @@ -49,9 +49,19 @@ s64 nsToCycles(std::chrono::nanoseconds ns) { return (Hardware::BASE_CLOCK_RATE * ns.count()) / 1000000000; } +u64 msToClockCycles(std::chrono::milliseconds ns) { + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); + return Common::Divide128On32(temp, 1000).first; +} + +u64 usToClockCycles(std::chrono::microseconds ns) { + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); + return Common::Divide128On32(temp, 1000000).first; +} + u64 nsToClockCycles(std::chrono::nanoseconds ns) { - const u128 temporal = Common::Multiply64Into128(ns.count(), CNTFREQ); - return Common::Divide128On32(temporal, 1000000000).first; + const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); + return Common::Divide128On32(temp, 1000000000).first; } u64 CpuCyclesToClockCycles(u64 ticks) { diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index 65fb7368b..b3c58447d 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h @@ -13,6 +13,8 @@ namespace Core::Timing { s64 msToCycles(std::chrono::milliseconds ms); s64 usToCycles(std::chrono::microseconds us); s64 nsToCycles(std::chrono::nanoseconds ns); +u64 msToClockCycles(std::chrono::milliseconds ns); +u64 usToClockCycles(std::chrono::microseconds ns); u64 nsToClockCycles(std::chrono::nanoseconds ns); inline std::chrono::milliseconds CyclesToMs(s64 cycles) { 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()); } 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.cpp | 61 ++++++++++++++++++++++++++++-------------------- src/core/host_timing.h | 6 ++++- 2 files changed, 41 insertions(+), 26 deletions(-) (limited to 'src/core') 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; 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.cpp | 11 ++++++++++- src/core/host_timing.h | 9 +++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) (limited to 'src/core') 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(); } 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') 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