core_timing.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #include "core/core_timing.h"
  5. #include <algorithm>
  6. #include <mutex>
  7. #include <string>
  8. #include <tuple>
  9. #include "common/assert.h"
  10. #include "common/thread.h"
  11. #include "core/core_timing_util.h"
  12. namespace Core::Timing {
  13. constexpr int MAX_SLICE_LENGTH = 20000;
  14. struct CoreTiming::Event {
  15. s64 time;
  16. u64 fifo_order;
  17. u64 userdata;
  18. const EventType* type;
  19. // Sort by time, unless the times are the same, in which case sort by
  20. // the order added to the queue
  21. friend bool operator>(const Event& left, const Event& right) {
  22. return std::tie(left.time, left.fifo_order) > std::tie(right.time, right.fifo_order);
  23. }
  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. };
  28. CoreTiming::CoreTiming() = default;
  29. CoreTiming::~CoreTiming() = default;
  30. void CoreTiming::Initialize() {
  31. downcount = MAX_SLICE_LENGTH;
  32. slice_length = MAX_SLICE_LENGTH;
  33. global_timer = 0;
  34. idled_cycles = 0;
  35. // The time between CoreTiming being initialized and the first call to Advance() is considered
  36. // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
  37. // executing the first cycle of each slice to prepare the slice length and downcount for
  38. // that slice.
  39. is_global_timer_sane = true;
  40. event_fifo_id = 0;
  41. const auto empty_timed_callback = [](u64, s64) {};
  42. ev_lost = RegisterEvent("_lost_event", empty_timed_callback);
  43. }
  44. void CoreTiming::Shutdown() {
  45. MoveEvents();
  46. ClearPendingEvents();
  47. UnregisterAllEvents();
  48. }
  49. EventType* CoreTiming::RegisterEvent(const std::string& name, TimedCallback callback) {
  50. // check for existing type with same name.
  51. // we want event type names to remain unique so that we can use them for serialization.
  52. ASSERT_MSG(event_types.find(name) == event_types.end(),
  53. "CoreTiming Event \"{}\" is already registered. Events should only be registered "
  54. "during Init to avoid breaking save states.",
  55. name.c_str());
  56. auto info = event_types.emplace(name, EventType{callback, nullptr});
  57. EventType* event_type = &info.first->second;
  58. event_type->name = &info.first->first;
  59. return event_type;
  60. }
  61. void CoreTiming::UnregisterAllEvents() {
  62. ASSERT_MSG(event_queue.empty(), "Cannot unregister events with events pending");
  63. event_types.clear();
  64. }
  65. void CoreTiming::ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata) {
  66. ASSERT(event_type != nullptr);
  67. const s64 timeout = GetTicks() + cycles_into_future;
  68. // If this event needs to be scheduled before the next advance(), force one early
  69. if (!is_global_timer_sane) {
  70. ForceExceptionCheck(cycles_into_future);
  71. }
  72. event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
  73. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  74. }
  75. void CoreTiming::ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type,
  76. u64 userdata) {
  77. ts_queue.Push(Event{global_timer + cycles_into_future, 0, userdata, event_type});
  78. }
  79. void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
  80. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  81. return e.type == event_type && e.userdata == userdata;
  82. });
  83. // Removing random items breaks the invariant so we have to re-establish it.
  84. if (itr != event_queue.end()) {
  85. event_queue.erase(itr, event_queue.end());
  86. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  87. }
  88. }
  89. void CoreTiming::UnscheduleEventThreadsafe(const EventType* event_type, u64 userdata) {
  90. unschedule_queue.Push(std::make_pair(event_type, userdata));
  91. }
  92. u64 CoreTiming::GetTicks() const {
  93. u64 ticks = static_cast<u64>(global_timer);
  94. if (!is_global_timer_sane) {
  95. ticks += slice_length - downcount;
  96. }
  97. return ticks;
  98. }
  99. u64 CoreTiming::GetIdleTicks() const {
  100. return static_cast<u64>(idled_cycles);
  101. }
  102. void CoreTiming::AddTicks(u64 ticks) {
  103. downcount -= static_cast<int>(ticks);
  104. }
  105. void CoreTiming::ClearPendingEvents() {
  106. event_queue.clear();
  107. }
  108. void CoreTiming::RemoveEvent(const EventType* event_type) {
  109. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(),
  110. [&](const Event& e) { return e.type == event_type; });
  111. // Removing random items breaks the invariant so we have to re-establish it.
  112. if (itr != event_queue.end()) {
  113. event_queue.erase(itr, event_queue.end());
  114. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  115. }
  116. }
  117. void CoreTiming::RemoveNormalAndThreadsafeEvent(const EventType* event_type) {
  118. MoveEvents();
  119. RemoveEvent(event_type);
  120. }
  121. void CoreTiming::ForceExceptionCheck(s64 cycles) {
  122. cycles = std::max<s64>(0, cycles);
  123. if (downcount <= cycles) {
  124. return;
  125. }
  126. // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
  127. // here. Account for cycles already executed by adjusting the g.slice_length
  128. slice_length -= downcount - static_cast<int>(cycles);
  129. downcount = static_cast<int>(cycles);
  130. }
  131. void CoreTiming::MoveEvents() {
  132. for (Event ev; ts_queue.Pop(ev);) {
  133. ev.fifo_order = event_fifo_id++;
  134. event_queue.emplace_back(std::move(ev));
  135. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  136. }
  137. }
  138. void CoreTiming::Advance() {
  139. MoveEvents();
  140. for (std::pair<const EventType*, u64> ev; unschedule_queue.Pop(ev);) {
  141. UnscheduleEvent(ev.first, ev.second);
  142. }
  143. const int cycles_executed = slice_length - downcount;
  144. global_timer += cycles_executed;
  145. slice_length = MAX_SLICE_LENGTH;
  146. is_global_timer_sane = true;
  147. while (!event_queue.empty() && event_queue.front().time <= global_timer) {
  148. Event evt = std::move(event_queue.front());
  149. std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  150. event_queue.pop_back();
  151. evt.type->callback(evt.userdata, static_cast<int>(global_timer - evt.time));
  152. }
  153. is_global_timer_sane = false;
  154. // Still events left (scheduled in the future)
  155. if (!event_queue.empty()) {
  156. slice_length = static_cast<int>(
  157. std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH));
  158. }
  159. downcount = slice_length;
  160. }
  161. void CoreTiming::Idle() {
  162. idled_cycles += downcount;
  163. downcount = 0;
  164. }
  165. std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
  166. return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
  167. }
  168. int CoreTiming::GetDowncount() const {
  169. return downcount;
  170. }
  171. } // namespace Core::Timing