core_timing.cpp 7.3 KB

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