summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Maide2022-07-28 00:47:06 +0100
committerGravatar GitHub2022-07-27 19:47:06 -0400
commit2e461103790110a3deea6425709b1f7f937085ea (patch)
tree495ea29aedfefc3564a87c8c4b44ed199d1aad3a
parentimplement pause on system suspend (#8585) (diff)
downloadyuzu-2e461103790110a3deea6425709b1f7f937085ea.tar.gz
yuzu-2e461103790110a3deea6425709b1f7f937085ea.tar.xz
yuzu-2e461103790110a3deea6425709b1f7f937085ea.zip
Revert Coretiming PRs 8531 and 7454 (#8591)
-rw-r--r--src/common/uint128.h5
-rw-r--r--src/common/x64/native_clock.cpp2
-rw-r--r--src/core/core_timing.cpp151
-rw-r--r--src/core/core_timing.h25
-rw-r--r--src/tests/core/core_timing.cpp4
5 files changed, 69 insertions, 118 deletions
diff --git a/src/common/uint128.h b/src/common/uint128.h
index 199d0f55e..f890ffec2 100644
--- a/src/common/uint128.h
+++ b/src/common/uint128.h
@@ -31,17 +31,12 @@ namespace Common {
31 return _udiv128(r[1], r[0], d, &remainder); 31 return _udiv128(r[1], r[0], d, &remainder);
32#endif 32#endif
33#else 33#else
34#ifdef __SIZEOF_INT128__
35 const auto product = static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b);
36 return static_cast<u64>(product / d);
37#else
38 const u64 diva = a / d; 34 const u64 diva = a / d;
39 const u64 moda = a % d; 35 const u64 moda = a % d;
40 const u64 divb = b / d; 36 const u64 divb = b / d;
41 const u64 modb = b % d; 37 const u64 modb = b % d;
42 return diva * b + moda * divb + moda * modb / d; 38 return diva * b + moda * divb + moda * modb / d;
43#endif 39#endif
44#endif
45} 40}
46 41
47// This function multiplies 2 u64 values and produces a u128 value; 42// This function multiplies 2 u64 values and produces a u128 value;
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp
index 6aaa8cdf9..8b08332ab 100644
--- a/src/common/x64/native_clock.cpp
+++ b/src/common/x64/native_clock.cpp
@@ -75,8 +75,8 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen
75} 75}
76 76
77u64 NativeClock::GetRTSC() { 77u64 NativeClock::GetRTSC() {
78 TimePoint current_time_point{};
79 TimePoint new_time_point{}; 78 TimePoint new_time_point{};
79 TimePoint current_time_point{};
80 80
81 current_time_point.pack = Common::AtomicLoad128(time_point.pack.data()); 81 current_time_point.pack = Common::AtomicLoad128(time_point.pack.data());
82 do { 82 do {
diff --git a/src/core/core_timing.cpp b/src/core/core_timing.cpp
index 5425637f5..2dbb99c8b 100644
--- a/src/core/core_timing.cpp
+++ b/src/core/core_timing.cpp
@@ -6,9 +6,7 @@
6#include <string> 6#include <string>
7#include <tuple> 7#include <tuple>
8 8
9#include "common/logging/log.h"
10#include "common/microprofile.h" 9#include "common/microprofile.h"
11#include "common/thread.h"
12#include "core/core_timing.h" 10#include "core/core_timing.h"
13#include "core/core_timing_util.h" 11#include "core/core_timing_util.h"
14#include "core/hardware_properties.h" 12#include "core/hardware_properties.h"
@@ -44,10 +42,10 @@ CoreTiming::CoreTiming()
44 42
45CoreTiming::~CoreTiming() = default; 43CoreTiming::~CoreTiming() = default;
46 44
47void CoreTiming::ThreadEntry(CoreTiming& instance, size_t id) { 45void CoreTiming::ThreadEntry(CoreTiming& instance) {
48 const std::string name = "yuzu:HostTiming_" + std::to_string(id); 46 constexpr char name[] = "yuzu:HostTiming";
49 MicroProfileOnThreadCreate(name.c_str()); 47 MicroProfileOnThreadCreate(name);
50 Common::SetCurrentThreadName(name.c_str()); 48 Common::SetCurrentThreadName(name);
51 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical); 49 Common::SetCurrentThreadPriority(Common::ThreadPriority::Critical);
52 instance.on_thread_init(); 50 instance.on_thread_init();
53 instance.ThreadLoop(); 51 instance.ThreadLoop();
@@ -63,127 +61,100 @@ void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
63 -> std::optional<std::chrono::nanoseconds> { return std::nullopt; }; 61 -> std::optional<std::chrono::nanoseconds> { return std::nullopt; };
64 ev_lost = CreateEvent("_lost_event", empty_timed_callback); 62 ev_lost = CreateEvent("_lost_event", empty_timed_callback);
65 if (is_multicore) { 63 if (is_multicore) {
66 worker_threads.emplace_back(ThreadEntry, std::ref(*this), 0); 64 timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
67 } 65 }
68} 66}
69 67
70void CoreTiming::Shutdown() { 68void CoreTiming::Shutdown() {
71 is_paused = true; 69 paused = true;
72 shutting_down = true; 70 shutting_down = true;
73 std::atomic_thread_fence(std::memory_order_release); 71 pause_event.Set();
74 72 event.Set();
75 event_cv.notify_all(); 73 if (timer_thread) {
76 wait_pause_cv.notify_all(); 74 timer_thread->join();
77 for (auto& thread : worker_threads) {
78 thread.join();
79 } 75 }
80 worker_threads.clear();
81 pause_callbacks.clear(); 76 pause_callbacks.clear();
82 ClearPendingEvents(); 77 ClearPendingEvents();
78 timer_thread.reset();
83 has_started = false; 79 has_started = false;
84} 80}
85 81
86void CoreTiming::Pause(bool is_paused_) { 82void CoreTiming::Pause(bool is_paused) {
87 std::unique_lock main_lock(event_mutex); 83 paused = is_paused;
88 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) { 84 pause_event.Set();
89 return;
90 }
91 if (is_multicore) {
92 is_paused = is_paused_;
93 event_cv.notify_all();
94 if (!is_paused_) {
95 wait_pause_cv.notify_all();
96 }
97 }
98 paused_state.store(is_paused_, std::memory_order_relaxed);
99 85
100 if (!is_paused_) { 86 if (!is_paused) {
101 pause_end_time = GetGlobalTimeNs().count(); 87 pause_end_time = GetGlobalTimeNs().count();
102 } 88 }
103 89
104 for (auto& cb : pause_callbacks) { 90 for (auto& cb : pause_callbacks) {
105 cb(is_paused_); 91 cb(is_paused);
106 } 92 }
107} 93}
108 94
109void CoreTiming::SyncPause(bool is_paused_) { 95void CoreTiming::SyncPause(bool is_paused) {
110 std::unique_lock main_lock(event_mutex); 96 if (is_paused == paused && paused_set == paused) {
111 if (is_paused_ == paused_state.load(std::memory_order_relaxed)) {
112 return; 97 return;
113 } 98 }
114 99
115 if (is_multicore) { 100 Pause(is_paused);
116 is_paused = is_paused_; 101 if (timer_thread) {
117 event_cv.notify_all(); 102 if (!is_paused) {
118 if (!is_paused_) { 103 pause_event.Set();
119 wait_pause_cv.notify_all();
120 }
121 }
122 paused_state.store(is_paused_, std::memory_order_relaxed);
123 if (is_multicore) {
124 if (is_paused_) {
125 wait_signal_cv.wait(main_lock, [this] { return pause_count == worker_threads.size(); });
126 } else {
127 wait_signal_cv.wait(main_lock, [this] { return pause_count == 0; });
128 } 104 }
105 event.Set();
106 while (paused_set != is_paused)
107 ;
129 } 108 }
130 109
131 if (!is_paused_) { 110 if (!is_paused) {
132 pause_end_time = GetGlobalTimeNs().count(); 111 pause_end_time = GetGlobalTimeNs().count();
133 } 112 }
134 113
135 for (auto& cb : pause_callbacks) { 114 for (auto& cb : pause_callbacks) {
136 cb(is_paused_); 115 cb(is_paused);
137 } 116 }
138} 117}
139 118
140bool CoreTiming::IsRunning() const { 119bool CoreTiming::IsRunning() const {
141 return !paused_state.load(std::memory_order_acquire); 120 return !paused_set;
142} 121}
143 122
144bool CoreTiming::HasPendingEvents() const { 123bool CoreTiming::HasPendingEvents() const {
145 std::unique_lock main_lock(event_mutex); 124 return !(wait_set && event_queue.empty());
146 return !event_queue.empty() || pending_events.load(std::memory_order_relaxed) != 0;
147} 125}
148 126
149void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future, 127void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
150 const std::shared_ptr<EventType>& event_type, 128 const std::shared_ptr<EventType>& event_type,
151 std::uintptr_t user_data, bool absolute_time) { 129 std::uintptr_t user_data, bool absolute_time) {
130 {
131 std::scoped_lock scope{basic_lock};
132 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future};
152 133
153 std::unique_lock main_lock(event_mutex); 134 event_queue.emplace_back(
154 const auto next_time{absolute_time ? ns_into_future : GetGlobalTimeNs() + ns_into_future}; 135 Event{next_time.count(), event_fifo_id++, user_data, event_type, 0});
155 136 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
156 event_queue.emplace_back(Event{next_time.count(), event_fifo_id++, user_data, event_type, 0});
157 pending_events.fetch_add(1, std::memory_order_relaxed);
158
159 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
160
161 if (is_multicore) {
162 event_cv.notify_one();
163 } 137 }
138
139 event.Set();
164} 140}
165 141
166void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time, 142void CoreTiming::ScheduleLoopingEvent(std::chrono::nanoseconds start_time,
167 std::chrono::nanoseconds resched_time, 143 std::chrono::nanoseconds resched_time,
168 const std::shared_ptr<EventType>& event_type, 144 const std::shared_ptr<EventType>& event_type,
169 std::uintptr_t user_data, bool absolute_time) { 145 std::uintptr_t user_data, bool absolute_time) {
170 std::unique_lock main_lock(event_mutex); 146 std::scoped_lock scope{basic_lock};
171 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time}; 147 const auto next_time{absolute_time ? start_time : GetGlobalTimeNs() + start_time};
172 148
173 event_queue.emplace_back( 149 event_queue.emplace_back(
174 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()}); 150 Event{next_time.count(), event_fifo_id++, user_data, event_type, resched_time.count()});
175 pending_events.fetch_add(1, std::memory_order_relaxed);
176 151
177 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 152 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
178
179 if (is_multicore) {
180 event_cv.notify_one();
181 }
182} 153}
183 154
184void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, 155void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
185 std::uintptr_t user_data) { 156 std::uintptr_t user_data) {
186 std::unique_lock main_lock(event_mutex); 157 std::scoped_lock scope{basic_lock};
187 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 158 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
188 return e.type.lock().get() == event_type.get() && e.user_data == user_data; 159 return e.type.lock().get() == event_type.get() && e.user_data == user_data;
189 }); 160 });
@@ -192,7 +163,6 @@ void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
192 if (itr != event_queue.end()) { 163 if (itr != event_queue.end()) {
193 event_queue.erase(itr, event_queue.end()); 164 event_queue.erase(itr, event_queue.end());
194 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 165 std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
195 pending_events.fetch_sub(1, std::memory_order_relaxed);
196 } 166 }
197} 167}
198 168
@@ -232,12 +202,11 @@ u64 CoreTiming::GetClockTicks() const {
232} 202}
233 203
234void CoreTiming::ClearPendingEvents() { 204void CoreTiming::ClearPendingEvents() {
235 std::unique_lock main_lock(event_mutex);
236 event_queue.clear(); 205 event_queue.clear();
237} 206}
238 207
239void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) { 208void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
240 std::unique_lock main_lock(event_mutex); 209 std::scoped_lock lock{basic_lock};
241 210
242 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) { 211 const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
243 return e.type.lock().get() == event_type.get(); 212 return e.type.lock().get() == event_type.get();
@@ -251,28 +220,27 @@ void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
251} 220}
252 221
253void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) { 222void CoreTiming::RegisterPauseCallback(PauseCallback&& callback) {
254 std::unique_lock main_lock(event_mutex); 223 std::scoped_lock lock{basic_lock};
255 pause_callbacks.emplace_back(std::move(callback)); 224 pause_callbacks.emplace_back(std::move(callback));
256} 225}
257 226
258std::optional<s64> CoreTiming::Advance() { 227std::optional<s64> CoreTiming::Advance() {
228 std::scoped_lock lock{advance_lock, basic_lock};
259 global_timer = GetGlobalTimeNs().count(); 229 global_timer = GetGlobalTimeNs().count();
260 230
261 std::unique_lock main_lock(event_mutex);
262 while (!event_queue.empty() && event_queue.front().time <= global_timer) { 231 while (!event_queue.empty() && event_queue.front().time <= global_timer) {
263 Event evt = std::move(event_queue.front()); 232 Event evt = std::move(event_queue.front());
264 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 233 std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
265 event_queue.pop_back(); 234 event_queue.pop_back();
266 235
267 if (const auto event_type{evt.type.lock()}) { 236 if (const auto event_type{evt.type.lock()}) {
268 event_mutex.unlock(); 237 basic_lock.unlock();
269 238
270 const auto new_schedule_time{event_type->callback( 239 const auto new_schedule_time{event_type->callback(
271 evt.user_data, evt.time, 240 evt.user_data, evt.time,
272 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})}; 241 std::chrono::nanoseconds{GetGlobalTimeNs().count() - evt.time})};
273 242
274 event_mutex.lock(); 243 basic_lock.lock();
275 pending_events.fetch_sub(1, std::memory_order_relaxed);
276 244
277 if (evt.reschedule_time != 0) { 245 if (evt.reschedule_time != 0) {
278 // If this event was scheduled into a pause, its time now is going to be way behind. 246 // If this event was scheduled into a pause, its time now is going to be way behind.
@@ -285,9 +253,9 @@ std::optional<s64> CoreTiming::Advance() {
285 const auto next_schedule_time{new_schedule_time.has_value() 253 const auto next_schedule_time{new_schedule_time.has_value()
286 ? new_schedule_time.value().count() 254 ? new_schedule_time.value().count()
287 : evt.reschedule_time}; 255 : evt.reschedule_time};
256
288 event_queue.emplace_back( 257 event_queue.emplace_back(
289 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time}); 258 Event{next_time, event_fifo_id++, evt.user_data, evt.type, next_schedule_time});
290 pending_events.fetch_add(1, std::memory_order_relaxed);
291 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>()); 259 std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
292 } 260 }
293 } 261 }
@@ -304,34 +272,27 @@ std::optional<s64> CoreTiming::Advance() {
304} 272}
305 273
306void CoreTiming::ThreadLoop() { 274void CoreTiming::ThreadLoop() {
307 const auto predicate = [this] { return !event_queue.empty() || is_paused; };
308 has_started = true; 275 has_started = true;
309 while (!shutting_down) { 276 while (!shutting_down) {
310 while (!is_paused && !shutting_down) { 277 while (!paused) {
278 paused_set = false;
311 const auto next_time = Advance(); 279 const auto next_time = Advance();
312 if (next_time) { 280 if (next_time) {
313 if (*next_time > 0) { 281 if (*next_time > 0) {
314 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time); 282 std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
315 std::unique_lock main_lock(event_mutex); 283 event.WaitFor(next_time_ns);
316 event_cv.wait_for(main_lock, next_time_ns, predicate);
317 } 284 }
318 } else { 285 } else {
319 std::unique_lock main_lock(event_mutex); 286 wait_set = true;
320 event_cv.wait(main_lock, predicate); 287 event.Wait();
321 } 288 }
289 wait_set = false;
322 } 290 }
323 std::unique_lock main_lock(event_mutex); 291
324 pause_count++; 292 paused_set = true;
325 if (pause_count == worker_threads.size()) { 293 clock->Pause(true);
326 clock->Pause(true); 294 pause_event.Wait();
327 wait_signal_cv.notify_all(); 295 clock->Pause(false);
328 }
329 wait_pause_cv.wait(main_lock, [this] { return !is_paused || shutting_down; });
330 pause_count--;
331 if (pause_count == 0) {
332 clock->Pause(false);
333 wait_signal_cv.notify_all();
334 }
335 } 296 }
336} 297}
337 298
diff --git a/src/core/core_timing.h b/src/core/core_timing.h
index 09b6ed81a..6aa3ae923 100644
--- a/src/core/core_timing.h
+++ b/src/core/core_timing.h
@@ -5,7 +5,6 @@
5 5
6#include <atomic> 6#include <atomic>
7#include <chrono> 7#include <chrono>
8#include <condition_variable>
9#include <functional> 8#include <functional>
10#include <memory> 9#include <memory>
11#include <mutex> 10#include <mutex>
@@ -15,6 +14,7 @@
15#include <vector> 14#include <vector>
16 15
17#include "common/common_types.h" 16#include "common/common_types.h"
17#include "common/thread.h"
18#include "common/wall_clock.h" 18#include "common/wall_clock.h"
19 19
20namespace Core::Timing { 20namespace Core::Timing {
@@ -143,7 +143,7 @@ private:
143 /// Clear all pending events. This should ONLY be done on exit. 143 /// Clear all pending events. This should ONLY be done on exit.
144 void ClearPendingEvents(); 144 void ClearPendingEvents();
145 145
146 static void ThreadEntry(CoreTiming& instance, size_t id); 146 static void ThreadEntry(CoreTiming& instance);
147 void ThreadLoop(); 147 void ThreadLoop();
148 148
149 std::unique_ptr<Common::WallClock> clock; 149 std::unique_ptr<Common::WallClock> clock;
@@ -156,24 +156,21 @@ private:
156 // accomodated by the standard adaptor class. 156 // accomodated by the standard adaptor class.
157 std::vector<Event> event_queue; 157 std::vector<Event> event_queue;
158 u64 event_fifo_id = 0; 158 u64 event_fifo_id = 0;
159 std::atomic<size_t> pending_events{};
160 159
161 std::shared_ptr<EventType> ev_lost; 160 std::shared_ptr<EventType> ev_lost;
161 Common::Event event{};
162 Common::Event pause_event{};
163 std::mutex basic_lock;
164 std::mutex advance_lock;
165 std::unique_ptr<std::thread> timer_thread;
166 std::atomic<bool> paused{};
167 std::atomic<bool> paused_set{};
168 std::atomic<bool> wait_set{};
169 std::atomic<bool> shutting_down{};
162 std::atomic<bool> has_started{}; 170 std::atomic<bool> has_started{};
163 std::function<void()> on_thread_init{}; 171 std::function<void()> on_thread_init{};
164 172
165 std::vector<std::thread> worker_threads;
166
167 std::condition_variable event_cv;
168 std::condition_variable wait_pause_cv;
169 std::condition_variable wait_signal_cv;
170 mutable std::mutex event_mutex;
171
172 std::atomic<bool> paused_state{};
173 bool is_paused{};
174 bool shutting_down{};
175 bool is_multicore{}; 173 bool is_multicore{};
176 size_t pause_count{};
177 s64 pause_end_time{}; 174 s64 pause_end_time{};
178 175
179 /// Cycle timing 176 /// Cycle timing
diff --git a/src/tests/core/core_timing.cpp b/src/tests/core/core_timing.cpp
index 894975e6f..7c432a63c 100644
--- a/src/tests/core/core_timing.cpp
+++ b/src/tests/core/core_timing.cpp
@@ -8,7 +8,6 @@
8#include <chrono> 8#include <chrono>
9#include <cstdlib> 9#include <cstdlib>
10#include <memory> 10#include <memory>
11#include <mutex>
12#include <optional> 11#include <optional>
13#include <string> 12#include <string>
14 13
@@ -23,15 +22,14 @@ std::array<s64, 5> delays{};
23 22
24std::bitset<CB_IDS.size()> callbacks_ran_flags; 23std::bitset<CB_IDS.size()> callbacks_ran_flags;
25u64 expected_callback = 0; 24u64 expected_callback = 0;
26std::mutex control_mutex;
27 25
28template <unsigned int IDX> 26template <unsigned int IDX>
29std::optional<std::chrono::nanoseconds> HostCallbackTemplate(std::uintptr_t user_data, s64 time, 27std::optional<std::chrono::nanoseconds> HostCallbackTemplate(std::uintptr_t user_data, s64 time,
30 std::chrono::nanoseconds ns_late) { 28 std::chrono::nanoseconds ns_late) {
31 std::unique_lock<std::mutex> lk(control_mutex);
32 static_assert(IDX < CB_IDS.size(), "IDX out of range"); 29 static_assert(IDX < CB_IDS.size(), "IDX out of range");
33 callbacks_ran_flags.set(IDX); 30 callbacks_ran_flags.set(IDX);
34 REQUIRE(CB_IDS[IDX] == user_data); 31 REQUIRE(CB_IDS[IDX] == user_data);
32 REQUIRE(CB_IDS[IDX] == CB_IDS[calls_order[expected_callback]]);
35 delays[IDX] = ns_late.count(); 33 delays[IDX] = ns_late.count();
36 ++expected_callback; 34 ++expected_callback;
37 return std::nullopt; 35 return std::nullopt;