diff options
| author | 2020-06-28 01:34:07 +1000 | |
|---|---|---|
| committer | 2020-06-28 01:34:07 +1000 | |
| commit | 0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf (patch) | |
| tree | a83acb1e779b98d31fa54389bae4be5669573a41 /src/core/host_timing.cpp | |
| parent | Merge pull request #4097 from kevinxucs/kevinxucs/device-pixel-scaling-float (diff) | |
| parent | Common: Fix non-conan build (diff) | |
| download | yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.gz yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.tar.xz yuzu-0ea4a8bcc4bca14bb7c65b248ed1899d2e7167cf.zip | |
Merge pull request #3396 from FernandoS27/prometheus-1
Implement SpinLocks, Fibers and a Host Timer
Diffstat (limited to 'src/core/host_timing.cpp')
| -rw-r--r-- | src/core/host_timing.cpp | 206 |
1 files changed, 206 insertions, 0 deletions
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 | ||