diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/core_timing_util.cpp | 5 | ||||
| -rw-r--r-- | src/core/core_timing_util.h | 1 | ||||
| -rw-r--r-- | src/core/host_timing.cpp | 161 | ||||
| -rw-r--r-- | src/core/host_timing.h | 126 |
5 files changed, 295 insertions, 0 deletions
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 | |||
| 547 | hle/service/vi/vi_u.h | 547 | hle/service/vi/vi_u.h |
| 548 | hle/service/wlan/wlan.cpp | 548 | hle/service/wlan/wlan.cpp |
| 549 | hle/service/wlan/wlan.h | 549 | hle/service/wlan/wlan.h |
| 550 | host_timing.cpp | ||
| 551 | host_timing.h | ||
| 550 | loader/deconstructed_rom_directory.cpp | 552 | loader/deconstructed_rom_directory.cpp |
| 551 | loader/deconstructed_rom_directory.h | 553 | loader/deconstructed_rom_directory.h |
| 552 | loader/elf.cpp | 554 | 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) { | |||
| 49 | return (Hardware::BASE_CLOCK_RATE * ns.count()) / 1000000000; | 49 | return (Hardware::BASE_CLOCK_RATE * ns.count()) / 1000000000; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | u64 nsToClockCycles(std::chrono::nanoseconds ns) { | ||
| 53 | const u128 temporal = Common::Multiply64Into128(ns.count(), CNTFREQ); | ||
| 54 | return Common::Divide128On32(temporal, 1000000000).first; | ||
| 55 | } | ||
| 56 | |||
| 52 | u64 CpuCyclesToClockCycles(u64 ticks) { | 57 | u64 CpuCyclesToClockCycles(u64 ticks) { |
| 53 | const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); | 58 | const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); |
| 54 | return Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | 59 | return Common::Divide128On32(temporal, static_cast<u32>(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 { | |||
| 13 | s64 msToCycles(std::chrono::milliseconds ms); | 13 | s64 msToCycles(std::chrono::milliseconds ms); |
| 14 | s64 usToCycles(std::chrono::microseconds us); | 14 | s64 usToCycles(std::chrono::microseconds us); |
| 15 | s64 nsToCycles(std::chrono::nanoseconds ns); | 15 | s64 nsToCycles(std::chrono::nanoseconds ns); |
| 16 | u64 nsToClockCycles(std::chrono::nanoseconds ns); | ||
| 16 | 17 | ||
| 17 | inline std::chrono::milliseconds CyclesToMs(s64 cycles) { | 18 | inline std::chrono::milliseconds CyclesToMs(s64 cycles) { |
| 18 | return std::chrono::milliseconds(cycles * 1000 / Hardware::BASE_CLOCK_RATE); | 19 | 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 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "core/host_timing.h" | ||
| 6 | |||
| 7 | #include <algorithm> | ||
| 8 | #include <mutex> | ||
| 9 | #include <string> | ||
| 10 | #include <tuple> | ||
| 11 | |||
| 12 | #include "common/assert.h" | ||
| 13 | #include "common/thread.h" | ||
| 14 | #include "core/core_timing_util.h" | ||
| 15 | |||
| 16 | namespace Core::HostTiming { | ||
| 17 | |||
| 18 | std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback) { | ||
| 19 | return std::make_shared<EventType>(std::move(callback), std::move(name)); | ||
| 20 | } | ||
| 21 | |||
| 22 | struct CoreTiming::Event { | ||
| 23 | u64 time; | ||
| 24 | u64 fifo_order; | ||
| 25 | u64 userdata; | ||
| 26 | std::weak_ptr<EventType> type; | ||
| 27 | |||
| 28 | // Sort by time, unless the times are the same, in which case sort by | ||
| 29 | // the order added to the queue | ||
| 30 | friend bool operator>(const Event& left, const Event& right) { | ||
| 31 | return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order); | ||
| 32 | } | ||
| 33 | |||
| 34 | friend bool operator<(const Event& left, const Event& right) { | ||
| 35 | return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order); | ||
| 36 | } | ||
| 37 | }; | ||
| 38 | |||
| 39 | CoreTiming::CoreTiming() = default; | ||
| 40 | CoreTiming::~CoreTiming() = default; | ||
| 41 | |||
| 42 | void CoreTiming::ThreadEntry(CoreTiming& instance) { | ||
| 43 | instance.Advance(); | ||
| 44 | } | ||
| 45 | |||
| 46 | void CoreTiming::Initialize() { | ||
| 47 | event_fifo_id = 0; | ||
| 48 | const auto empty_timed_callback = [](u64, s64) {}; | ||
| 49 | ev_lost = CreateEvent("_lost_event", empty_timed_callback); | ||
| 50 | start_time = std::chrono::system_clock::now(); | ||
| 51 | timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); | ||
| 52 | } | ||
| 53 | |||
| 54 | void CoreTiming::Shutdown() { | ||
| 55 | std::unique_lock<std::mutex> guard(inner_mutex); | ||
| 56 | shutting_down = true; | ||
| 57 | if (!is_set) { | ||
| 58 | is_set = true; | ||
| 59 | condvar.notify_one(); | ||
| 60 | } | ||
| 61 | inner_mutex.unlock(); | ||
| 62 | timer_thread->join(); | ||
| 63 | ClearPendingEvents(); | ||
| 64 | } | ||
| 65 | |||
| 66 | void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, | ||
| 67 | u64 userdata) { | ||
| 68 | std::lock_guard guard{inner_mutex}; | ||
| 69 | const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future); | ||
| 70 | |||
| 71 | event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); | ||
| 72 | |||
| 73 | std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 74 | if (!is_set) { | ||
| 75 | is_set = true; | ||
| 76 | condvar.notify_one(); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | |||
| 80 | void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) { | ||
| 81 | std::lock_guard guard{inner_mutex}; | ||
| 82 | |||
| 83 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | ||
| 84 | return e.type.lock().get() == event_type.get() && e.userdata == userdata; | ||
| 85 | }); | ||
| 86 | |||
| 87 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 88 | if (itr != event_queue.end()) { | ||
| 89 | event_queue.erase(itr, event_queue.end()); | ||
| 90 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 91 | } | ||
| 92 | } | ||
| 93 | |||
| 94 | u64 CoreTiming::GetCPUTicks() const { | ||
| 95 | std::chrono::nanoseconds time_now = GetGlobalTimeNs(); | ||
| 96 | return Core::Timing::nsToCycles(time_now); | ||
| 97 | } | ||
| 98 | |||
| 99 | u64 CoreTiming::GetClockTicks() const { | ||
| 100 | std::chrono::nanoseconds time_now = GetGlobalTimeNs(); | ||
| 101 | return Core::Timing::nsToClockCycles(time_now); | ||
| 102 | } | ||
| 103 | |||
| 104 | void CoreTiming::ClearPendingEvents() { | ||
| 105 | event_queue.clear(); | ||
| 106 | } | ||
| 107 | |||
| 108 | void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { | ||
| 109 | std::lock_guard guard{inner_mutex}; | ||
| 110 | |||
| 111 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | ||
| 112 | return e.type.lock().get() == event_type.get(); | ||
| 113 | }); | ||
| 114 | |||
| 115 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 116 | if (itr != event_queue.end()) { | ||
| 117 | event_queue.erase(itr, event_queue.end()); | ||
| 118 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 119 | } | ||
| 120 | } | ||
| 121 | |||
| 122 | void CoreTiming::Advance() { | ||
| 123 | while (true) { | ||
| 124 | std::unique_lock<std::mutex> guard(inner_mutex); | ||
| 125 | |||
| 126 | global_timer = GetGlobalTimeNs().count(); | ||
| 127 | |||
| 128 | while (!event_queue.empty() && event_queue.front().time <= global_timer) { | ||
| 129 | Event evt = std::move(event_queue.front()); | ||
| 130 | std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 131 | event_queue.pop_back(); | ||
| 132 | inner_mutex.unlock(); | ||
| 133 | |||
| 134 | if (auto event_type{evt.type.lock()}) { | ||
| 135 | event_type->callback(evt.userdata, global_timer - evt.time); | ||
| 136 | } | ||
| 137 | |||
| 138 | inner_mutex.lock(); | ||
| 139 | } | ||
| 140 | auto next_time = std::chrono::nanoseconds(event_queue.front().time - global_timer); | ||
| 141 | condvar.wait_for(guard, next_time, [this] { return is_set; }); | ||
| 142 | is_set = false; | ||
| 143 | if (shutting_down) { | ||
| 144 | break; | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { | ||
| 150 | sys_time_point current = std::chrono::system_clock::now(); | ||
| 151 | auto elapsed = current - start_time; | ||
| 152 | return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); | ||
| 153 | } | ||
| 154 | |||
| 155 | std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { | ||
| 156 | sys_time_point current = std::chrono::system_clock::now(); | ||
| 157 | auto elapsed = current - start_time; | ||
| 158 | return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
| 159 | } | ||
| 160 | |||
| 161 | } // 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 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <chrono> | ||
| 8 | #include <functional> | ||
| 9 | #include <memory> | ||
| 10 | #include <mutex> | ||
| 11 | #include <optional> | ||
| 12 | #include <string> | ||
| 13 | #include <thread> | ||
| 14 | #include <vector> | ||
| 15 | |||
| 16 | #include "common/common_types.h" | ||
| 17 | #include "common/threadsafe_queue.h" | ||
| 18 | |||
| 19 | namespace Core::HostTiming { | ||
| 20 | |||
| 21 | /// A callback that may be scheduled for a particular core timing event. | ||
| 22 | using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>; | ||
| 23 | using sys_time_point = std::chrono::time_point<std::chrono::system_clock>; | ||
| 24 | |||
| 25 | /// Contains the characteristics of a particular event. | ||
| 26 | struct EventType { | ||
| 27 | EventType(TimedCallback&& callback, std::string&& name) | ||
| 28 | : callback{std::move(callback)}, name{std::move(name)} {} | ||
| 29 | |||
| 30 | /// The event's callback function. | ||
| 31 | TimedCallback callback; | ||
| 32 | /// A pointer to the name of the event. | ||
| 33 | const std::string name; | ||
| 34 | }; | ||
| 35 | |||
| 36 | /** | ||
| 37 | * This is a system to schedule events into the emulated machine's future. Time is measured | ||
| 38 | * in main CPU clock cycles. | ||
| 39 | * | ||
| 40 | * To schedule an event, you first have to register its type. This is where you pass in the | ||
| 41 | * callback. You then schedule events using the type id you get back. | ||
| 42 | * | ||
| 43 | * The int cyclesLate that the callbacks get is how many cycles late it was. | ||
| 44 | * So to schedule a new event on a regular basis: | ||
| 45 | * inside callback: | ||
| 46 | * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever") | ||
| 47 | */ | ||
| 48 | class CoreTiming { | ||
| 49 | public: | ||
| 50 | CoreTiming(); | ||
| 51 | ~CoreTiming(); | ||
| 52 | |||
| 53 | CoreTiming(const CoreTiming&) = delete; | ||
| 54 | CoreTiming(CoreTiming&&) = delete; | ||
| 55 | |||
| 56 | CoreTiming& operator=(const CoreTiming&) = delete; | ||
| 57 | CoreTiming& operator=(CoreTiming&&) = delete; | ||
| 58 | |||
| 59 | /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is | ||
| 60 | /// required to end slice - 1 and start slice 0 before the first cycle of code is executed. | ||
| 61 | void Initialize(); | ||
| 62 | |||
| 63 | /// Tears down all timing related functionality. | ||
| 64 | void Shutdown(); | ||
| 65 | |||
| 66 | /// Schedules an event in core timing | ||
| 67 | void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, | ||
| 68 | u64 userdata = 0); | ||
| 69 | |||
| 70 | void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata); | ||
| 71 | |||
| 72 | /// We only permit one event of each type in the queue at a time. | ||
| 73 | void RemoveEvent(const std::shared_ptr<EventType>& event_type); | ||
| 74 | |||
| 75 | /// Returns current time in emulated CPU cycles | ||
| 76 | u64 GetCPUTicks() const; | ||
| 77 | |||
| 78 | /// Returns current time in emulated in Clock cycles | ||
| 79 | u64 GetClockTicks() const; | ||
| 80 | |||
| 81 | /// Returns current time in microseconds. | ||
| 82 | std::chrono::microseconds GetGlobalTimeUs() const; | ||
| 83 | |||
| 84 | /// Returns current time in nanoseconds. | ||
| 85 | std::chrono::nanoseconds GetGlobalTimeNs() const; | ||
| 86 | |||
| 87 | private: | ||
| 88 | struct Event; | ||
| 89 | |||
| 90 | /// Clear all pending events. This should ONLY be done on exit. | ||
| 91 | void ClearPendingEvents(); | ||
| 92 | |||
| 93 | static void ThreadEntry(CoreTiming& instance); | ||
| 94 | void Advance(); | ||
| 95 | |||
| 96 | sys_time_point start_time; | ||
| 97 | |||
| 98 | u64 global_timer = 0; | ||
| 99 | |||
| 100 | std::chrono::nanoseconds start_point; | ||
| 101 | |||
| 102 | // The queue is a min-heap using std::make_heap/push_heap/pop_heap. | ||
| 103 | // We don't use std::priority_queue because we need to be able to serialize, unserialize and | ||
| 104 | // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't | ||
| 105 | // accomodated by the standard adaptor class. | ||
| 106 | std::vector<Event> event_queue; | ||
| 107 | u64 event_fifo_id = 0; | ||
| 108 | |||
| 109 | std::shared_ptr<EventType> ev_lost; | ||
| 110 | bool is_set = false; | ||
| 111 | std::condition_variable condvar; | ||
| 112 | std::mutex inner_mutex; | ||
| 113 | std::unique_ptr<std::thread> timer_thread; | ||
| 114 | std::atomic<bool> shutting_down{}; | ||
| 115 | }; | ||
| 116 | |||
| 117 | /// Creates a core timing event with the given name and callback. | ||
| 118 | /// | ||
| 119 | /// @param name The name of the core timing event to create. | ||
| 120 | /// @param callback The callback to execute for the event. | ||
| 121 | /// | ||
| 122 | /// @returns An EventType instance representing the created event. | ||
| 123 | /// | ||
| 124 | std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback); | ||
| 125 | |||
| 126 | } // namespace Core::Timing | ||