core_timing.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 <string>
  8. #include <unordered_map>
  9. #include <vector>
  10. #include "common/common_types.h"
  11. #include "common/threadsafe_queue.h"
  12. namespace Core::Timing {
  13. /// A callback that may be scheduled for a particular core timing event.
  14. using TimedCallback = std::function<void(u64 userdata, int cycles_late)>;
  15. /// Contains the characteristics of a particular event.
  16. struct EventType {
  17. /// The event's callback function.
  18. TimedCallback callback;
  19. /// A pointer to the name of the event.
  20. const std::string* name;
  21. };
  22. /**
  23. * This is a system to schedule events into the emulated machine's future. Time is measured
  24. * in main CPU clock cycles.
  25. *
  26. * To schedule an event, you first have to register its type. This is where you pass in the
  27. * callback. You then schedule events using the type id you get back.
  28. *
  29. * The int cyclesLate that the callbacks get is how many cycles late it was.
  30. * So to schedule a new event on a regular basis:
  31. * inside callback:
  32. * ScheduleEvent(periodInCycles - cyclesLate, callback, "whatever")
  33. */
  34. class CoreTiming {
  35. public:
  36. CoreTiming();
  37. ~CoreTiming();
  38. CoreTiming(const CoreTiming&) = delete;
  39. CoreTiming(CoreTiming&&) = delete;
  40. CoreTiming& operator=(const CoreTiming&) = delete;
  41. CoreTiming& operator=(CoreTiming&&) = delete;
  42. /// CoreTiming begins at the boundary of timing slice -1. An initial call to Advance() is
  43. /// required to end slice - 1 and start slice 0 before the first cycle of code is executed.
  44. void Initialize();
  45. /// Tears down all timing related functionality.
  46. void Shutdown();
  47. /// Registers a core timing event with the given name and callback.
  48. ///
  49. /// @param name The name of the core timing event to register.
  50. /// @param callback The callback to execute for the event.
  51. ///
  52. /// @returns An EventType instance representing the registered event.
  53. ///
  54. /// @pre The name of the event being registered must be unique among all
  55. /// registered events.
  56. ///
  57. EventType* RegisterEvent(const std::string& name, TimedCallback callback);
  58. /// Unregisters all registered events thus far.
  59. void UnregisterAllEvents();
  60. /// After the first Advance, the slice lengths and the downcount will be reduced whenever an
  61. /// event is scheduled earlier than the current values.
  62. ///
  63. /// Scheduling from a callback will not update the downcount until the Advance() completes.
  64. void ScheduleEvent(s64 cycles_into_future, const EventType* event_type, u64 userdata = 0);
  65. /// This is to be called when outside of hle threads, such as the graphics thread, wants to
  66. /// schedule things to be executed on the main thread.
  67. ///
  68. /// @note This doesn't change slice_length and thus events scheduled by this might be
  69. /// called with a delay of up to MAX_SLICE_LENGTH
  70. void ScheduleEventThreadsafe(s64 cycles_into_future, const EventType* event_type,
  71. u64 userdata = 0);
  72. void UnscheduleEvent(const EventType* event_type, u64 userdata);
  73. void UnscheduleEventThreadsafe(const EventType* event_type, u64 userdata);
  74. /// We only permit one event of each type in the queue at a time.
  75. void RemoveEvent(const EventType* event_type);
  76. void RemoveNormalAndThreadsafeEvent(const EventType* event_type);
  77. void ForceExceptionCheck(s64 cycles);
  78. /// This should only be called from the emu thread, if you are calling it any other thread,
  79. /// you are doing something evil
  80. u64 GetTicks() const;
  81. u64 GetIdleTicks() const;
  82. void AddTicks(u64 ticks);
  83. /// Advance must be called at the beginning of dispatcher loops, not the end. Advance() ends
  84. /// the previous timing slice and begins the next one, you must Advance from the previous
  85. /// slice to the current one before executing any cycles. CoreTiming starts in slice -1 so an
  86. /// Advance() is required to initialize the slice length before the first cycle of emulated
  87. /// instructions is executed.
  88. void Advance();
  89. /// Pretend that the main CPU has executed enough cycles to reach the next event.
  90. void Idle();
  91. std::chrono::microseconds GetGlobalTimeUs() const;
  92. int GetDowncount() const;
  93. private:
  94. struct Event;
  95. /// Clear all pending events. This should ONLY be done on exit.
  96. void ClearPendingEvents();
  97. void MoveEvents();
  98. s64 global_timer = 0;
  99. s64 idled_cycles = 0;
  100. int slice_length = 0;
  101. int downcount = 0;
  102. // Are we in a function that has been called from Advance()
  103. // If events are scheduled from a function that gets called from Advance(),
  104. // don't change slice_length and downcount.
  105. bool is_global_timer_sane = false;
  106. // The queue is a min-heap using std::make_heap/push_heap/pop_heap.
  107. // We don't use std::priority_queue because we need to be able to serialize, unserialize and
  108. // erase arbitrary events (RemoveEvent()) regardless of the queue order. These aren't
  109. // accomodated by the standard adaptor class.
  110. std::vector<Event> event_queue;
  111. u64 event_fifo_id = 0;
  112. // Stores each element separately as a linked list node so pointers to elements
  113. // remain stable regardless of rehashes/resizing.
  114. std::unordered_map<std::string, EventType> event_types;
  115. // The queue for storing the events from other threads threadsafe until they will be added
  116. // to the event_queue by the emu thread
  117. Common::MPSCQueue<Event> ts_queue;
  118. // The queue for unscheduling the events from other threads threadsafe
  119. Common::MPSCQueue<std::pair<const EventType*, u64>> unschedule_queue;
  120. EventType* ev_lost = nullptr;
  121. };
  122. } // namespace Core::Timing