core_timing.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. ClearPendingEvents();
  46. UnregisterAllEvents();
  47. }
  48. EventType* CoreTiming::RegisterEvent(const std::string& name, TimedCallback callback) {
  49. std::lock_guard guard{inner_mutex};
  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. std::lock_guard guard{inner_mutex};
  68. const s64 timeout = GetTicks() + cycles_into_future;
  69. // If this event needs to be scheduled before the next advance(), force one early
  70. if (!is_global_timer_sane) {
  71. ForceExceptionCheck(cycles_into_future);
  72. }
  73. event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
  74. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  75. }
  76. void CoreTiming::UnscheduleEvent(const EventType* event_type, u64 userdata) {
  77. std::lock_guard guard{inner_mutex};
  78. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  79. return e.type == event_type && e.userdata == userdata;
  80. });
  81. // Removing random items breaks the invariant so we have to re-establish it.
  82. if (itr != event_queue.end()) {
  83. event_queue.erase(itr, event_queue.end());
  84. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  85. }
  86. }
  87. u64 CoreTiming::GetTicks() const {
  88. u64 ticks = static_cast<u64>(global_timer);
  89. if (!is_global_timer_sane) {
  90. ticks += slice_length - downcount;
  91. }
  92. return ticks;
  93. }
  94. u64 CoreTiming::GetIdleTicks() const {
  95. return static_cast<u64>(idled_cycles);
  96. }
  97. void CoreTiming::AddTicks(u64 ticks) {
  98. downcount -= static_cast<int>(ticks);
  99. }
  100. void CoreTiming::ClearPendingEvents() {
  101. event_queue.clear();
  102. }
  103. void CoreTiming::RemoveEvent(const EventType* event_type) {
  104. std::lock_guard guard{inner_mutex};
  105. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(),
  106. [&](const Event& e) { return e.type == event_type; });
  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::ForceExceptionCheck(s64 cycles) {
  114. cycles = std::max<s64>(0, cycles);
  115. if (downcount <= cycles) {
  116. return;
  117. }
  118. // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
  119. // here. Account for cycles already executed by adjusting the g.slice_length
  120. slice_length -= downcount - static_cast<int>(cycles);
  121. downcount = static_cast<int>(cycles);
  122. }
  123. void CoreTiming::Advance() {
  124. std::unique_lock<std::mutex> guard(inner_mutex);
  125. const int cycles_executed = slice_length - downcount;
  126. global_timer += cycles_executed;
  127. slice_length = MAX_SLICE_LENGTH;
  128. is_global_timer_sane = true;
  129. while (!event_queue.empty() && event_queue.front().time <= global_timer) {
  130. Event evt = std::move(event_queue.front());
  131. std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  132. event_queue.pop_back();
  133. inner_mutex.unlock();
  134. evt.type->callback(evt.userdata, global_timer - evt.time);
  135. inner_mutex.lock();
  136. }
  137. is_global_timer_sane = false;
  138. // Still events left (scheduled in the future)
  139. if (!event_queue.empty()) {
  140. slice_length = static_cast<int>(
  141. std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH));
  142. }
  143. downcount = slice_length;
  144. }
  145. void CoreTiming::Idle() {
  146. idled_cycles += downcount;
  147. downcount = 0;
  148. }
  149. std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
  150. return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
  151. }
  152. int CoreTiming::GetDowncount() const {
  153. return downcount;
  154. }
  155. } // namespace Core::Timing