core_timing.cpp 7.2 KB

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