core_timing.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2020 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #include <algorithm>
  5. #include <mutex>
  6. #include <string>
  7. #include <tuple>
  8. #include "common/assert.h"
  9. #include "common/microprofile.h"
  10. #include "core/core_timing.h"
  11. #include "core/core_timing_util.h"
  12. namespace Core::Timing {
  13. constexpr s64 MAX_SLICE_LENGTH = 4000;
  14. std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback) {
  15. return std::make_shared<EventType>(std::move(callback), std::move(name));
  16. }
  17. struct CoreTiming::Event {
  18. u64 time;
  19. u64 fifo_order;
  20. std::uintptr_t user_data;
  21. std::weak_ptr<EventType> type;
  22. // Sort by time, unless the times are the same, in which case sort by
  23. // the order added to the queue
  24. friend bool operator>(const Event& left, const Event& right) {
  25. return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
  26. }
  27. friend bool operator<(const Event& left, const Event& right) {
  28. return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
  29. }
  30. };
  31. CoreTiming::CoreTiming() {
  32. clock =
  33. Common::CreateBestMatchingClock(Core::Hardware::BASE_CLOCK_RATE, Core::Hardware::CNTFREQ);
  34. }
  35. CoreTiming::~CoreTiming() = default;
  36. void CoreTiming::ThreadEntry(CoreTiming& instance) {
  37. constexpr char name[] = "yuzu:HostTiming";
  38. MicroProfileOnThreadCreate(name);
  39. Common::SetCurrentThreadName(name);
  40. Common::SetCurrentThreadPriority(Common::ThreadPriority::VeryHigh);
  41. instance.on_thread_init();
  42. instance.ThreadLoop();
  43. }
  44. void CoreTiming::Initialize(std::function<void()>&& on_thread_init_) {
  45. on_thread_init = std::move(on_thread_init_);
  46. event_fifo_id = 0;
  47. shutting_down = false;
  48. ticks = 0;
  49. const auto empty_timed_callback = [](std::uintptr_t, std::chrono::nanoseconds) {};
  50. ev_lost = CreateEvent("_lost_event", empty_timed_callback);
  51. if (is_multicore) {
  52. timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
  53. }
  54. }
  55. void CoreTiming::Shutdown() {
  56. paused = true;
  57. shutting_down = true;
  58. pause_event.Set();
  59. event.Set();
  60. if (timer_thread) {
  61. timer_thread->join();
  62. }
  63. ClearPendingEvents();
  64. timer_thread.reset();
  65. has_started = false;
  66. }
  67. void CoreTiming::Pause(bool is_paused) {
  68. paused = is_paused;
  69. pause_event.Set();
  70. }
  71. void CoreTiming::SyncPause(bool is_paused) {
  72. if (is_paused == paused && paused_set == paused) {
  73. return;
  74. }
  75. Pause(is_paused);
  76. if (timer_thread) {
  77. if (!is_paused) {
  78. pause_event.Set();
  79. }
  80. event.Set();
  81. while (paused_set != is_paused)
  82. ;
  83. }
  84. }
  85. bool CoreTiming::IsRunning() const {
  86. return !paused_set;
  87. }
  88. bool CoreTiming::HasPendingEvents() const {
  89. return !(wait_set && event_queue.empty());
  90. }
  91. void CoreTiming::ScheduleEvent(std::chrono::nanoseconds ns_into_future,
  92. const std::shared_ptr<EventType>& event_type,
  93. std::uintptr_t user_data) {
  94. {
  95. std::scoped_lock scope{basic_lock};
  96. const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
  97. event_queue.emplace_back(Event{timeout, event_fifo_id++, user_data, event_type});
  98. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  99. }
  100. event.Set();
  101. }
  102. void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type,
  103. std::uintptr_t user_data) {
  104. std::scoped_lock scope{basic_lock};
  105. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  106. return e.type.lock().get() == event_type.get() && e.user_data == user_data;
  107. });
  108. // Removing random items breaks the invariant so we have to re-establish it.
  109. if (itr != event_queue.end()) {
  110. event_queue.erase(itr, event_queue.end());
  111. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  112. }
  113. }
  114. void CoreTiming::AddTicks(u64 ticks) {
  115. this->ticks += ticks;
  116. downcount -= static_cast<s64>(ticks);
  117. }
  118. void CoreTiming::Idle() {
  119. if (!event_queue.empty()) {
  120. const u64 next_event_time = event_queue.front().time;
  121. const u64 next_ticks = nsToCycles(std::chrono::nanoseconds(next_event_time)) + 10U;
  122. if (next_ticks > ticks) {
  123. ticks = next_ticks;
  124. }
  125. return;
  126. }
  127. ticks += 1000U;
  128. }
  129. void CoreTiming::ResetTicks() {
  130. downcount = MAX_SLICE_LENGTH;
  131. }
  132. u64 CoreTiming::GetCPUTicks() const {
  133. if (is_multicore) {
  134. return clock->GetCPUCycles();
  135. }
  136. return ticks;
  137. }
  138. u64 CoreTiming::GetClockTicks() const {
  139. if (is_multicore) {
  140. return clock->GetClockCycles();
  141. }
  142. return CpuCyclesToClockCycles(ticks);
  143. }
  144. void CoreTiming::ClearPendingEvents() {
  145. event_queue.clear();
  146. }
  147. void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
  148. std::scoped_lock lock{basic_lock};
  149. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  150. return e.type.lock().get() == event_type.get();
  151. });
  152. // Removing random items breaks the invariant so we have to re-establish it.
  153. if (itr != event_queue.end()) {
  154. event_queue.erase(itr, event_queue.end());
  155. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  156. }
  157. }
  158. std::optional<s64> CoreTiming::Advance() {
  159. std::scoped_lock lock{advance_lock, basic_lock};
  160. global_timer = GetGlobalTimeNs().count();
  161. while (!event_queue.empty() && event_queue.front().time <= global_timer) {
  162. Event evt = std::move(event_queue.front());
  163. std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  164. event_queue.pop_back();
  165. basic_lock.unlock();
  166. if (const auto event_type{evt.type.lock()}) {
  167. event_type->callback(
  168. evt.user_data, std::chrono::nanoseconds{static_cast<s64>(global_timer - evt.time)});
  169. }
  170. basic_lock.lock();
  171. global_timer = GetGlobalTimeNs().count();
  172. }
  173. if (!event_queue.empty()) {
  174. const s64 next_time = event_queue.front().time - global_timer;
  175. return next_time;
  176. } else {
  177. return std::nullopt;
  178. }
  179. }
  180. void CoreTiming::ThreadLoop() {
  181. has_started = true;
  182. while (!shutting_down) {
  183. while (!paused) {
  184. paused_set = false;
  185. const auto next_time = Advance();
  186. if (next_time) {
  187. if (*next_time > 0) {
  188. std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
  189. event.WaitFor(next_time_ns);
  190. }
  191. } else {
  192. wait_set = true;
  193. event.Wait();
  194. }
  195. wait_set = false;
  196. }
  197. paused_set = true;
  198. clock->Pause(true);
  199. pause_event.Wait();
  200. clock->Pause(false);
  201. }
  202. }
  203. std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
  204. if (is_multicore) {
  205. return clock->GetTimeNS();
  206. }
  207. return CyclesToNs(ticks);
  208. }
  209. std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
  210. if (is_multicore) {
  211. return clock->GetTimeUS();
  212. }
  213. return CyclesToUs(ticks);
  214. }
  215. } // namespace Core::Timing