core_timing.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 = 10000;
  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. s64 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() = default;
  32. CoreTiming::~CoreTiming() = default;
  33. void CoreTiming::Initialize() {
  34. downcounts.fill(MAX_SLICE_LENGTH);
  35. time_slice.fill(MAX_SLICE_LENGTH);
  36. slice_length = MAX_SLICE_LENGTH;
  37. global_timer = 0;
  38. idled_cycles = 0;
  39. current_context = 0;
  40. // The time between CoreTiming being initialized and the first call to Advance() is considered
  41. // the slice boundary between slice -1 and slice 0. Dispatcher loops must call Advance() before
  42. // executing the first cycle of each slice to prepare the slice length and downcount for
  43. // that slice.
  44. is_global_timer_sane = true;
  45. event_fifo_id = 0;
  46. const auto empty_timed_callback = [](u64, s64) {};
  47. ev_lost = CreateEvent("_lost_event", empty_timed_callback);
  48. }
  49. void CoreTiming::Shutdown() {
  50. ClearPendingEvents();
  51. }
  52. void CoreTiming::ScheduleEvent(s64 cycles_into_future, const std::shared_ptr<EventType>& event_type,
  53. u64 userdata) {
  54. std::lock_guard guard{inner_mutex};
  55. const s64 timeout = GetTicks() + cycles_into_future;
  56. // If this event needs to be scheduled before the next advance(), force one early
  57. if (!is_global_timer_sane) {
  58. ForceExceptionCheck(cycles_into_future);
  59. }
  60. event_queue.emplace_back(Event{timeout, event_fifo_id++, userdata, event_type});
  61. std::push_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  62. }
  63. void CoreTiming::UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata) {
  64. std::lock_guard guard{inner_mutex};
  65. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  66. return e.type.lock().get() == event_type.get() && e.userdata == userdata;
  67. });
  68. // Removing random items breaks the invariant so we have to re-establish it.
  69. if (itr != event_queue.end()) {
  70. event_queue.erase(itr, event_queue.end());
  71. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  72. }
  73. }
  74. u64 CoreTiming::GetTicks() const {
  75. u64 ticks = static_cast<u64>(global_timer);
  76. if (!is_global_timer_sane) {
  77. ticks += accumulated_ticks;
  78. }
  79. return ticks;
  80. }
  81. u64 CoreTiming::GetIdleTicks() const {
  82. return static_cast<u64>(idled_cycles);
  83. }
  84. void CoreTiming::AddTicks(u64 ticks) {
  85. accumulated_ticks += ticks;
  86. downcounts[current_context] -= static_cast<s64>(ticks);
  87. }
  88. void CoreTiming::ClearPendingEvents() {
  89. event_queue.clear();
  90. }
  91. void CoreTiming::RemoveEvent(const std::shared_ptr<EventType>& event_type) {
  92. std::lock_guard guard{inner_mutex};
  93. const auto itr = std::remove_if(event_queue.begin(), event_queue.end(), [&](const Event& e) {
  94. return e.type.lock().get() == event_type.get();
  95. });
  96. // Removing random items breaks the invariant so we have to re-establish it.
  97. if (itr != event_queue.end()) {
  98. event_queue.erase(itr, event_queue.end());
  99. std::make_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  100. }
  101. }
  102. void CoreTiming::ForceExceptionCheck(s64 cycles) {
  103. cycles = std::max<s64>(0, cycles);
  104. if (downcounts[current_context] <= cycles) {
  105. return;
  106. }
  107. // downcount is always (much) smaller than MAX_INT so we can safely cast cycles to an int
  108. // here. Account for cycles already executed by adjusting the g.slice_length
  109. downcounts[current_context] = static_cast<int>(cycles);
  110. }
  111. std::optional<u64> CoreTiming::NextAvailableCore(const s64 needed_ticks) const {
  112. const u64 original_context = current_context;
  113. u64 next_context = (original_context + 1) % num_cpu_cores;
  114. while (next_context != original_context) {
  115. if (time_slice[next_context] >= needed_ticks) {
  116. return {next_context};
  117. } else if (time_slice[next_context] >= 0) {
  118. return std::nullopt;
  119. }
  120. next_context = (next_context + 1) % num_cpu_cores;
  121. }
  122. return std::nullopt;
  123. }
  124. void CoreTiming::Advance() {
  125. std::unique_lock<std::mutex> guard(inner_mutex);
  126. const u64 cycles_executed = accumulated_ticks;
  127. time_slice[current_context] = std::max<s64>(0, time_slice[current_context] - accumulated_ticks);
  128. global_timer += cycles_executed;
  129. is_global_timer_sane = true;
  130. while (!event_queue.empty() && event_queue.front().time <= global_timer) {
  131. Event evt = std::move(event_queue.front());
  132. std::pop_heap(event_queue.begin(), event_queue.end(), std::greater<>());
  133. event_queue.pop_back();
  134. inner_mutex.unlock();
  135. if (auto event_type{evt.type.lock()}) {
  136. event_type->callback(evt.userdata, global_timer - evt.time);
  137. }
  138. inner_mutex.lock();
  139. }
  140. is_global_timer_sane = false;
  141. // Still events left (scheduled in the future)
  142. if (!event_queue.empty()) {
  143. const s64 needed_ticks =
  144. std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
  145. const auto next_core = NextAvailableCore(needed_ticks);
  146. if (next_core) {
  147. downcounts[*next_core] = needed_ticks;
  148. }
  149. }
  150. accumulated_ticks = 0;
  151. downcounts[current_context] = time_slice[current_context];
  152. }
  153. void CoreTiming::ResetRun() {
  154. downcounts.fill(MAX_SLICE_LENGTH);
  155. time_slice.fill(MAX_SLICE_LENGTH);
  156. current_context = 0;
  157. // Still events left (scheduled in the future)
  158. if (!event_queue.empty()) {
  159. const s64 needed_ticks =
  160. std::min<s64>(event_queue.front().time - global_timer, MAX_SLICE_LENGTH);
  161. downcounts[current_context] = needed_ticks;
  162. }
  163. is_global_timer_sane = false;
  164. accumulated_ticks = 0;
  165. }
  166. void CoreTiming::Idle() {
  167. accumulated_ticks += downcounts[current_context];
  168. idled_cycles += downcounts[current_context];
  169. downcounts[current_context] = 0;
  170. }
  171. std::chrono::microseconds CoreTiming::GetGlobalTimeUs() const {
  172. return std::chrono::microseconds{GetTicks() * 1000000 / BASE_CLOCK_RATE};
  173. }
  174. s64 CoreTiming::GetDowncount() const {
  175. return downcounts[current_context];
  176. }
  177. } // namespace Core::Timing