diff options
Diffstat (limited to 'src/core')
| -rw-r--r-- | src/core/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/core/core_timing_util.cpp | 15 | ||||
| -rw-r--r-- | src/core/core_timing_util.h | 3 | ||||
| -rw-r--r-- | src/core/host_timing.cpp | 206 | ||||
| -rw-r--r-- | src/core/host_timing.h | 160 |
5 files changed, 386 insertions, 0 deletions
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index cb9ced5c9..efbad628f 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..be34b26fe 100644 --- a/src/core/core_timing_util.cpp +++ b/src/core/core_timing_util.cpp | |||
| @@ -49,6 +49,21 @@ 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 msToClockCycles(std::chrono::milliseconds ns) { | ||
| 53 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 54 | return Common::Divide128On32(temp, 1000).first; | ||
| 55 | } | ||
| 56 | |||
| 57 | u64 usToClockCycles(std::chrono::microseconds ns) { | ||
| 58 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 59 | return Common::Divide128On32(temp, 1000000).first; | ||
| 60 | } | ||
| 61 | |||
| 62 | u64 nsToClockCycles(std::chrono::nanoseconds ns) { | ||
| 63 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 64 | return Common::Divide128On32(temp, 1000000000).first; | ||
| 65 | } | ||
| 66 | |||
| 52 | u64 CpuCyclesToClockCycles(u64 ticks) { | 67 | u64 CpuCyclesToClockCycles(u64 ticks) { |
| 53 | const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); | 68 | const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); |
| 54 | return Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | 69 | 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..b3c58447d 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h | |||
| @@ -13,6 +13,9 @@ 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 msToClockCycles(std::chrono::milliseconds ns); | ||
| 17 | u64 usToClockCycles(std::chrono::microseconds ns); | ||
| 18 | u64 nsToClockCycles(std::chrono::nanoseconds ns); | ||
| 16 | 19 | ||
| 17 | inline std::chrono::milliseconds CyclesToMs(s64 cycles) { | 20 | inline std::chrono::milliseconds CyclesToMs(s64 cycles) { |
| 18 | return std::chrono::milliseconds(cycles * 1000 / Hardware::BASE_CLOCK_RATE); | 21 | 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..2f40de1a1 --- /dev/null +++ b/src/core/host_timing.cpp | |||
| @@ -0,0 +1,206 @@ | |||
| 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 "core/core_timing_util.h" | ||
| 14 | |||
| 15 | namespace Core::HostTiming { | ||
| 16 | |||
| 17 | std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback) { | ||
| 18 | return std::make_shared<EventType>(std::move(callback), std::move(name)); | ||
| 19 | } | ||
| 20 | |||
| 21 | struct CoreTiming::Event { | ||
| 22 | u64 time; | ||
| 23 | u64 fifo_order; | ||
| 24 | u64 userdata; | ||
| 25 | std::weak_ptr<EventType> type; | ||
| 26 | |||
| 27 | // Sort by time, unless the times are the same, in which case sort by | ||
| 28 | // the order added to the queue | ||
| 29 | friend bool operator>(const Event& left, const Event& right) { | ||
| 30 | return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order); | ||
| 31 | } | ||
| 32 | |||
| 33 | friend bool operator<(const Event& left, const Event& right) { | ||
| 34 | return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order); | ||
| 35 | } | ||
| 36 | }; | ||
| 37 | |||
| 38 | CoreTiming::CoreTiming() { | ||
| 39 | clock = | ||
| 40 | Common::CreateBestMatchingClock(Core::Hardware::BASE_CLOCK_RATE, Core::Hardware::CNTFREQ); | ||
| 41 | } | ||
| 42 | |||
| 43 | CoreTiming::~CoreTiming() = default; | ||
| 44 | |||
| 45 | void CoreTiming::ThreadEntry(CoreTiming& instance) { | ||
| 46 | instance.ThreadLoop(); | ||
| 47 | } | ||
| 48 | |||
| 49 | void CoreTiming::Initialize() { | ||
| 50 | event_fifo_id = 0; | ||
| 51 | const auto empty_timed_callback = [](u64, s64) {}; | ||
| 52 | ev_lost = CreateEvent("_lost_event", empty_timed_callback); | ||
| 53 | timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this)); | ||
| 54 | } | ||
| 55 | |||
| 56 | void CoreTiming::Shutdown() { | ||
| 57 | paused = true; | ||
| 58 | shutting_down = true; | ||
| 59 | event.Set(); | ||
| 60 | timer_thread->join(); | ||
| 61 | ClearPendingEvents(); | ||
| 62 | timer_thread.reset(); | ||
| 63 | has_started = false; | ||
| 64 | } | ||
| 65 | |||
| 66 | void CoreTiming::Pause(bool is_paused) { | ||
| 67 | paused = is_paused; | ||
| 68 | } | ||
| 69 | |||
| 70 | void CoreTiming::SyncPause(bool is_paused) { | ||
| 71 | if (is_paused == paused && paused_set == paused) { | ||
| 72 | return; | ||
| 73 | } | ||
| 74 | Pause(is_paused); | ||
| 75 | event.Set(); | ||
| 76 | while (paused_set != is_paused) | ||
| 77 | ; | ||
| 78 | } | ||
| 79 | |||
| 80 | bool CoreTiming::IsRunning() const { | ||
| 81 | return !paused_set; | ||
| 82 | } | ||
| 83 | |||
| 84 | bool CoreTiming::HasPendingEvents() const { | ||
| 85 | return !(wait_set && event_queue.empty()); | ||
| 86 | } | ||
| 87 | |||
| 88 | void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, | ||
| 89 | u64 userdata) { | ||
| 90 | basic_lock.lock(); | ||
| 91 | const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future); | ||
| 92 | |||
| 93 | event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type}); | ||
| 94 | |||
| 95 | std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 96 | basic_lock.unlock(); | ||
| 97 | event.Set(); | ||
| 98 | } | ||
| 99 | |||
| 100 | void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) { | ||
| 101 | basic_lock.lock(); | ||
| 102 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | ||
| 103 | return e.type.lock().get() == event_type.get() && e.userdata == userdata; | ||
| 104 | }); | ||
| 105 | |||
| 106 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 107 | if (itr != event_queue.end()) { | ||
| 108 | event_queue.erase(itr, event_queue.end()); | ||
| 109 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 110 | } | ||
| 111 | basic_lock.unlock(); | ||
| 112 | } | ||
| 113 | |||
| 114 | void CoreTiming::AddTicks(std::size_t core_index, u64 ticks) { | ||
| 115 | ticks_count[core_index] += ticks; | ||
| 116 | } | ||
| 117 | |||
| 118 | void CoreTiming::ResetTicks(std::size_t core_index) { | ||
| 119 | ticks_count[core_index] = 0; | ||
| 120 | } | ||
| 121 | |||
| 122 | u64 CoreTiming::GetCPUTicks() const { | ||
| 123 | return clock->GetCPUCycles(); | ||
| 124 | } | ||
| 125 | |||
| 126 | u64 CoreTiming::GetClockTicks() const { | ||
| 127 | return clock->GetClockCycles(); | ||
| 128 | } | ||
| 129 | |||
| 130 | void CoreTiming::ClearPendingEvents() { | ||
| 131 | event_queue.clear(); | ||
| 132 | } | ||
| 133 | |||
| 134 | void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { | ||
| 135 | basic_lock.lock(); | ||
| 136 | |||
| 137 | const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { | ||
| 138 | return e.type.lock().get() == event_type.get(); | ||
| 139 | }); | ||
| 140 | |||
| 141 | // Removing random items breaks the invariant so we have to re-establish it. | ||
| 142 | if (itr != event_queue.end()) { | ||
| 143 | event_queue.erase(itr, event_queue.end()); | ||
| 144 | std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 145 | } | ||
| 146 | basic_lock.unlock(); | ||
| 147 | } | ||
| 148 | |||
| 149 | std::optional<u64> CoreTiming::Advance() { | ||
| 150 | advance_lock.lock(); | ||
| 151 | basic_lock.lock(); | ||
| 152 | global_timer = GetGlobalTimeNs().count(); | ||
| 153 | |||
| 154 | while (!event_queue.empty() && event_queue.front().time <= global_timer) { | ||
| 155 | Event evt = std::move(event_queue.front()); | ||
| 156 | std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); | ||
| 157 | event_queue.pop_back(); | ||
| 158 | basic_lock.unlock(); | ||
| 159 | |||
| 160 | if (auto event_type{evt.type.lock()}) { | ||
| 161 | event_type->callback(evt.userdata, global_timer - evt.time); | ||
| 162 | } | ||
| 163 | |||
| 164 | basic_lock.lock(); | ||
| 165 | } | ||
| 166 | |||
| 167 | if (!event_queue.empty()) { | ||
| 168 | const u64 next_time = event_queue.front().time - global_timer; | ||
| 169 | basic_lock.unlock(); | ||
| 170 | advance_lock.unlock(); | ||
| 171 | return next_time; | ||
| 172 | } else { | ||
| 173 | basic_lock.unlock(); | ||
| 174 | advance_lock.unlock(); | ||
| 175 | return std::nullopt; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | void CoreTiming::ThreadLoop() { | ||
| 180 | has_started = true; | ||
| 181 | while (!shutting_down) { | ||
| 182 | while (!paused) { | ||
| 183 | paused_set = false; | ||
| 184 | const auto next_time = Advance(); | ||
| 185 | if (next_time) { | ||
| 186 | std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); | ||
| 187 | event.WaitFor(next_time_ns); | ||
| 188 | } else { | ||
| 189 | wait_set = true; | ||
| 190 | event.Wait(); | ||
| 191 | } | ||
| 192 | wait_set = false; | ||
| 193 | } | ||
| 194 | paused_set = true; | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const { | ||
| 199 | return clock->GetTimeNS(); | ||
| 200 | } | ||
| 201 | |||
| 202 | std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const { | ||
| 203 | return clock->GetTimeUS(); | ||
| 204 | } | ||
| 205 | |||
| 206 | } // namespace Core::HostTiming | ||
diff --git a/src/core/host_timing.h b/src/core/host_timing.h new file mode 100644 index 000000000..be6b68d7c --- /dev/null +++ b/src/core/host_timing.h | |||
| @@ -0,0 +1,160 @@ | |||
| 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 <atomic> | ||
| 8 | #include <chrono> | ||
| 9 | #include <functional> | ||
| 10 | #include <memory> | ||
| 11 | #include <mutex> | ||
| 12 | #include <optional> | ||
| 13 | #include <string> | ||
| 14 | #include <thread> | ||
| 15 | #include <vector> | ||
| 16 | |||
| 17 | #include "common/common_types.h" | ||
| 18 | #include "common/spin_lock.h" | ||
| 19 | #include "common/thread.h" | ||
| 20 | #include "common/threadsafe_queue.h" | ||
| 21 | #include "common/wall_clock.h" | ||
| 22 | #include "core/hardware_properties.h" | ||
| 23 | |||
| 24 | namespace Core::HostTiming { | ||
| 25 | |||
| 26 | /// A callback that may be scheduled for a particular core timing event. | ||
| 27 | using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>; | ||
| 28 | |||
| 29 | /// Contains the characteristics of a particular event. | ||
| 30 | struct EventType { | ||
| 31 | EventType(TimedCallback&& callback, std::string&& name) | ||
| 32 | : callback{std::move(callback)}, name{std::move(name)} {} | ||
| 33 | |||
| 34 | /// The event's callback function. | ||
| 35 | TimedCallback callback; | ||
| 36 | /// A pointer to the name of the event. | ||
| 37 | const std::string name; | ||
| 38 | }; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * This is a system to schedule events into the emulated machine's future. Time is measured | ||
| 42 | * in main CPU clock cycles. | ||
| 43 | * | ||
| 44 | * To schedule an event, you first have to register its type. This is where you pass in the | ||
| 45 | * callback. You then schedule events using the type id you get back. | ||
| 46 | * | ||
| 47 | * The int cyclesLate that the callbacks get is how many cycles late it was. | ||
| 48 | * So to schedule a new event on a regular basis: | ||
| 49 | * inside callback: | ||
| 50 | * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever") | ||
| 51 | */ | ||
| 52 | class CoreTiming { | ||
| 53 | public: | ||
| 54 | CoreTiming(); | ||
| 55 | ~CoreTiming(); | ||
| 56 | |||
| 57 | CoreTiming(const CoreTiming&) = delete; | ||
| 58 | CoreTiming(CoreTiming&&) = delete; | ||
| 59 | |||
| 60 | CoreTiming& operator=(const CoreTiming&) = delete; | ||
| 61 | CoreTiming& operator=(CoreTiming&&) = delete; | ||
| 62 | |||
| 63 | /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is | ||
| 64 | /// required to end slice - 1 and start slice 0 before the first cycle of code is executed. | ||
| 65 | void Initialize(); | ||
| 66 | |||
| 67 | /// Tears down all timing related functionality. | ||
| 68 | void Shutdown(); | ||
| 69 | |||
| 70 | /// Pauses/Unpauses the execution of the timer thread. | ||
| 71 | void Pause(bool is_paused); | ||
| 72 | |||
| 73 | /// Pauses/Unpauses the execution of the timer thread and waits until paused. | ||
| 74 | void SyncPause(bool is_paused); | ||
| 75 | |||
| 76 | /// Checks if core timing is running. | ||
| 77 | bool IsRunning() const; | ||
| 78 | |||
| 79 | /// Checks if the timer thread has started. | ||
| 80 | bool HasStarted() const { | ||
| 81 | return has_started; | ||
| 82 | } | ||
| 83 | |||
| 84 | /// Checks if there are any pending time events. | ||
| 85 | bool HasPendingEvents() const; | ||
| 86 | |||
| 87 | /// Schedules an event in core timing | ||
| 88 | void ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type, | ||
| 89 | u64 userdata = 0); | ||
| 90 | |||
| 91 | void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata); | ||
| 92 | |||
| 93 | /// We only permit one event of each type in the queue at a time. | ||
| 94 | void RemoveEvent(const std::shared_ptr<EventType>& event_type); | ||
| 95 | |||
| 96 | void AddTicks(std::size_t core_index, u64 ticks); | ||
| 97 | |||
| 98 | void ResetTicks(std::size_t core_index); | ||
| 99 | |||
| 100 | /// Returns current time in emulated CPU cycles | ||
| 101 | u64 GetCPUTicks() const; | ||
| 102 | |||
| 103 | /// Returns current time in emulated in Clock cycles | ||
| 104 | u64 GetClockTicks() const; | ||
| 105 | |||
| 106 | /// Returns current time in microseconds. | ||
| 107 | std::chrono::microseconds GetGlobalTimeUs() const; | ||
| 108 | |||
| 109 | /// Returns current time in nanoseconds. | ||
| 110 | std::chrono::nanoseconds GetGlobalTimeNs() const; | ||
| 111 | |||
| 112 | /// Checks for events manually and returns time in nanoseconds for next event, threadsafe. | ||
| 113 | std::optional<u64> Advance(); | ||
| 114 | |||
| 115 | private: | ||
| 116 | struct Event; | ||
| 117 | |||
| 118 | /// Clear all pending events. This should ONLY be done on exit. | ||
| 119 | void ClearPendingEvents(); | ||
| 120 | |||
| 121 | static void ThreadEntry(CoreTiming& instance); | ||
| 122 | void ThreadLoop(); | ||
| 123 | |||
| 124 | std::unique_ptr<Common::WallClock> clock; | ||
| 125 | |||
| 126 | u64 global_timer = 0; | ||
| 127 | |||
| 128 | std::chrono::nanoseconds start_point; | ||
| 129 | |||
| 130 | // The queue is a min-heap using std::make_heap/push_heap/pop_heap. | ||
| 131 | // We don't use std::priority_queue because we need to be able to serialize, unserialize and | ||
| 132 | // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't | ||
| 133 | // accomodated by the standard adaptor class. | ||
| 134 | std::vector<Event> event_queue; | ||
| 135 | u64 event_fifo_id = 0; | ||
| 136 | |||
| 137 | std::shared_ptr<EventType> ev_lost; | ||
| 138 | Common::Event event{}; | ||
| 139 | Common::SpinLock basic_lock{}; | ||
| 140 | Common::SpinLock advance_lock{}; | ||
| 141 | std::unique_ptr<std::thread> timer_thread; | ||
| 142 | std::atomic<bool> paused{}; | ||
| 143 | std::atomic<bool> paused_set{}; | ||
| 144 | std::atomic<bool> wait_set{}; | ||
| 145 | std::atomic<bool> shutting_down{}; | ||
| 146 | std::atomic<bool> has_started{}; | ||
| 147 | |||
| 148 | std::array<std::atomic<u64>, Core::Hardware::NUM_CPU_CORES> ticks_count{}; | ||
| 149 | }; | ||
| 150 | |||
| 151 | /// Creates a core timing event with the given name and callback. | ||
| 152 | /// | ||
| 153 | /// @param name The name of the core timing event to create. | ||
| 154 | /// @param callback The callback to execute for the event. | ||
| 155 | /// | ||
| 156 | /// @returns An EventType instance representing the created event. | ||
| 157 | /// | ||
| 158 | std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback); | ||
| 159 | |||
| 160 | } // namespace Core::HostTiming | ||