core_timing.h 4.9 KB

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