core_timing.cpp 7.1 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/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 u64 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. u64 userdata;
  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 = [](u64, 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, u64 userdata) {
  93. {
  94. std::scoped_lock scope{basic_lock};
  95. const u64 timeout = static_cast<u64>((GetGlobalTimeNs() + ns_into_future).count());
  96. event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
  97. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  98. }
  99. event.Set();
  100. }
  101. void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
  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.userdata == userdata;
  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 -= 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.userdata, 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