core_timing.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project
  2. // Licensed under GPLv2+
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <chrono>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. #include <optional>
  10. #include <string>
  11. #include <vector>
  12. #include "common/common_types.h"
  13. #include "common/threadsafe_queue.h"
  14. namespace Core::Timing {
  15. /// A callback that may be scheduled for a particular core timing event.
  16. using TimedCallback = std::function<void(u64 userdata, s64 cycles_late)>;
  17. /// Contains the characteristics of a particular event.
  18. struct EventType {
  19. EventType(TimedCallback&& callback, std::string&& name)
  20. : callback{std::move(callback)}, name{std::move(name)} {}
  21. /// The event's callback function.
  22. TimedCallback callback;
  23. /// A pointer to the name of the event.
  24. const std::string name;
  25. };
  26. /**
  27. * This is a system to schedule events into the emulated machine's future. Time is measured
  28. * in main CPU clock cycles.
  29. *
  30. * To schedule an event, you first have to register its type. This is where you pass in the
  31. * callback. You then schedule events using the type id you get back.
  32. *
  33. * The int cyclesLate that the callbacks get is how many cycles late it was.
  34. * So to schedule a new event on a regular basis:
  35. * inside callback:
  36. * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
  37. */
  38. class CoreTiming {
  39. public:
  40. CoreTiming();
  41. ~CoreTiming();
  42. CoreTiming(const CoreTiming&) = delete;
  43. CoreTiming(CoreTiming&&) = delete;
  44. CoreTiming& operator=(const CoreTiming&) = delete;
  45. CoreTiming& operator=(CoreTiming&&) = delete;
  46. /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  47. /// required to end slice - 1 and start slice 0 before the first cycle of code is executed.
  48. void Initialize();
  49. /// Tears down all timing related functionality.
  50. void Shutdown();
  51. /// After the first Advance, the slice lengths and the downcount will be reduced whenever an
  52. /// event is scheduled earlier than the current values.
  53. ///
  54. /// Scheduling from a callback will not update the downcount until the Advance() completes.
  55. void ScheduleEvent(s64 cycles_into_future, const std::shared_ptr<EventType>& event_type,
  56. u64 userdata = 0);
  57. void UnscheduleEvent(const std::shared_ptr<EventType>& event_type, u64 userdata);
  58. /// We only permit one event of each type in the queue at a time.
  59. void RemoveEvent(const std::shared_ptr<EventType>& event_type);
  60. void ForceExceptionCheck(s64 cycles);
  61. /// This should only be called from the emu thread, if you are calling it any other thread,
  62. /// you are doing something evil
  63. u64 GetTicks() const;
  64. u64 GetIdleTicks() const;
  65. void AddTicks(u64 ticks);
  66. /// Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  67. /// the previous timing slice and begins the next one, you must Advance from the previous
  68. /// slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  69. /// Advance() is required to initialize the slice length before the first cycle of emulated
  70. /// instructions is executed.
  71. void Advance();
  72. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  73. void Idle();
  74. std::chrono::microseconds GetGlobalTimeUs() const;
  75. void ResetRun();
  76. s64 GetDowncount() const;
  77. void SwitchContext(u64 new_context) {
  78. current_context = new_context;
  79. }
  80. bool CanCurrentContextRun() const {
  81. return time_slice[current_context] > 0;
  82. }
  83. std::optional<u64> NextAvailableCore(const s64 needed_ticks) const;
  84. private:
  85. struct Event;
  86. /// Clear all pending events. This should ONLY be done on exit.
  87. void ClearPendingEvents();
  88. static constexpr u64 num_cpu_cores = 4;
  89. s64 global_timer = 0;
  90. s64 idled_cycles = 0;
  91. s64 slice_length = 0;
  92. u64 accumulated_ticks = 0;
  93. std::array<s64, num_cpu_cores> downcounts{};
  94. // Slice of time assigned to each core per run.
  95. std::array<s64, num_cpu_cores> time_slice{};
  96. u64 current_context = 0;
  97. // Are we in a function that has been called from Advance()
  98. // If events are scheduled from a function that gets called from Advance(),
  99. // don't change slice_length and downcount.
  100. bool is_global_timer_sane = false;
  101. // The queue is a min-heap using std::make_heap/push_heap/pop_heap.
  102. // We don't use std::priority_queue because we need to be able to serialize, unserialize and
  103. // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
  104. // accomodated by the standard adaptor class.
  105. std::vector<Event> event_queue;
  106. u64 event_fifo_id = 0;
  107. std::shared_ptr<EventType> ev_lost;
  108. std::mutex inner_mutex;
  109. };
  110. /// Creates a core timing event with the given name and callback.
  111. ///
  112. /// @param name The name of the core timing event to create.
  113. /// @param callback The callback to execute for the event.
  114. ///
  115. /// @returns An EventType instance representing the created event.
  116. ///
  117. std::shared_ptr<EventType> CreateEvent(std::string name, TimedCallback&& callback);
  118. } // namespace Core::Timing