core_timing.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback) {
  14. return std::make_shared<EventType>(std::move(callback), std::move(name));
  15. }
  16. struct CoreTiming::Event {
  17. u64 time;
  18. u64 fifo_order;
  19. u64 userdata;
  20. std::weak_ptr<EventType> type;
  21. // Sort by time, unless the times are the same, in which case sort by
  22. // the order added to the queue
  23. friend bool operator>(const Event& left, const Event& right) {
  24. return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
  25. }
  26. friend bool operator<(const Event& left, const Event& right) {
  27. return std::tie(left.time, left.fifo_order) < std::tie(right.time, right.fifo_order);
  28. }
  29. };
  30. CoreTiming::CoreTiming() {
  31. clock =
  32. Common::CreateBestMatchingClock(Core::Hardware::BASE_CLOCK_RATE, Core::Hardware::CNTFREQ);
  33. }
  34. CoreTiming::~CoreTiming() = default;
  35. void CoreTiming::ThreadEntry(CoreTiming& instance) {
  36. std::string name = "yuzu:HostTiming";
  37. MicroProfileOnThreadCreate(name.c_str());
  38. Common::SetCurrentThreadName(name.c_str());
  39. instance.on_thread_init();
  40. instance.ThreadLoop();
  41. }
  42. void CoreTiming::Initialize(std::function<void(void)>&& on_thread_init_) {
  43. on_thread_init = std::move(on_thread_init_);
  44. event_fifo_id = 0;
  45. const auto empty_timed_callback = [](u64, s64) {};
  46. ev_lost = CreateEvent("_lost_event", empty_timed_callback);
  47. timer_thread = std::make_unique<std::thread>(ThreadEntry, std::ref(*this));
  48. }
  49. void CoreTiming::Shutdown() {
  50. paused = true;
  51. shutting_down = true;
  52. event.Set();
  53. timer_thread->join();
  54. ClearPendingEvents();
  55. timer_thread.reset();
  56. has_started = false;
  57. }
  58. void CoreTiming::Pause(bool is_paused) {
  59. paused = is_paused;
  60. }
  61. void CoreTiming::SyncPause(bool is_paused) {
  62. if (is_paused == paused && paused_set == paused) {
  63. return;
  64. }
  65. Pause(is_paused);
  66. if (!is_paused) {
  67. pause_event.Set();
  68. }
  69. event.Set();
  70. while (paused_set != is_paused)
  71. ;
  72. }
  73. bool CoreTiming::IsRunning() const {
  74. return !paused_set;
  75. }
  76. bool CoreTiming::HasPendingEvents() const {
  77. return !(wait_set && event_queue.empty());
  78. }
  79. void CoreTiming::ScheduleEvent(s64 ns_into_future, const std::shared_ptr<EventType>& event_type,
  80. u64 userdata) {
  81. basic_lock.lock();
  82. const u64 timeout = static_cast<u64>(GetGlobalTimeNs().count() + ns_into_future);
  83. event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
  84. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  85. basic_lock.unlock();
  86. event.Set();
  87. }
  88. void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
  89. basic_lock.lock();
  90. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  91. return e.type.lock().get() == event_type.get() && e.userdata == userdata;
  92. });
  93. // Removing random items breaks the invariant so we have to re-establish it.
  94. if (itr != event_queue.end()) {
  95. event_queue.erase(itr, event_queue.end());
  96. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  97. }
  98. basic_lock.unlock();
  99. }
  100. void CoreTiming::AddTicks(std::size_t core_index, u64 ticks) {
  101. ticks_count[core_index] += ticks;
  102. }
  103. void CoreTiming::ResetTicks(std::size_t core_index) {
  104. ticks_count[core_index] = 0;
  105. }
  106. u64 CoreTiming::GetCPUTicks() const {
  107. return clock->GetCPUCycles();
  108. }
  109. u64 CoreTiming::GetClockTicks() const {
  110. return clock->GetClockCycles();
  111. }
  112. void CoreTiming::ClearPendingEvents() {
  113. event_queue.clear();
  114. }
  115. void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
  116. basic_lock.lock();
  117. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  118. return e.type.lock().get() == event_type.get();
  119. });
  120. // Removing random items breaks the invariant so we have to re-establish it.
  121. if (itr != event_queue.end()) {
  122. event_queue.erase(itr, event_queue.end());
  123. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  124. }
  125. basic_lock.unlock();
  126. }
  127. std::optional<s64> CoreTiming::Advance() {
  128. advance_lock.lock();
  129. basic_lock.lock();
  130. global_timer = GetGlobalTimeNs().count();
  131. while (!event_queue.empty() && event_queue.front().time <= global_timer) {
  132. Event evt = std::move(event_queue.front());
  133. std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  134. event_queue.pop_back();
  135. basic_lock.unlock();
  136. if (auto event_type{evt.type.lock()}) {
  137. event_type->callback(evt.userdata, global_timer - evt.time);
  138. }
  139. basic_lock.lock();
  140. global_timer = GetGlobalTimeNs().count();
  141. }
  142. if (!event_queue.empty()) {
  143. const s64 next_time = event_queue.front().time - global_timer;
  144. basic_lock.unlock();
  145. advance_lock.unlock();
  146. return next_time;
  147. } else {
  148. basic_lock.unlock();
  149. advance_lock.unlock();
  150. return std::nullopt;
  151. }
  152. }
  153. void CoreTiming::ThreadLoop() {
  154. has_started = true;
  155. while (!shutting_down) {
  156. while (!paused) {
  157. paused_set = false;
  158. const auto next_time = Advance();
  159. if (next_time) {
  160. if (*next_time > 0) {
  161. std::chrono::nanoseconds next_time_ns = std::chrono::nanoseconds(*next_time);
  162. event.WaitFor(next_time_ns);
  163. }
  164. } else {
  165. wait_set = true;
  166. event.Wait();
  167. }
  168. wait_set = false;
  169. }
  170. paused_set = true;
  171. clock->Pause(true);
  172. pause_event.Wait();
  173. clock->Pause(false);
  174. }
  175. }
  176. std::chrono::nanoseconds CoreTiming::GetGlobalTimeNs() const {
  177. return clock->GetTimeNS();
  178. }
  179. std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
  180. return clock->GetTimeUS();
  181. }
  182. } // namespace Core::Timing