scheduler.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright 2018 yuzu emulator team
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <memory>
  7. #include <vector>
  8. #include "common/common_types.h"
  9. #include "common/multi_level_queue.h"
  10. #include "core/hle/kernel/thread.h"
  11. namespace Core {
  12. class ARM_Interface;
  13. class System;
  14. } // namespace Core
  15. namespace Kernel {
  16. class Process;
  17. class GlobalScheduler final {
  18. public:
  19. static constexpr u32 NUM_CPU_CORES = 4;
  20. explicit GlobalScheduler(Core::System& system);
  21. ~GlobalScheduler();
  22. /// Adds a new thread to the scheduler
  23. void AddThread(std::shared_ptr<Thread> thread);
  24. /// Removes a thread from the scheduler
  25. void RemoveThread(std::shared_ptr<Thread> thread);
  26. /// Returns a list of all threads managed by the scheduler
  27. const std::vector<std::shared_ptr<Thread>>& GetThreadList() const {
  28. return thread_list;
  29. }
  30. /**
  31. * Add a thread to the suggested queue of a cpu core. Suggested threads may be
  32. * picked if no thread is scheduled to run on the core.
  33. */
  34. void Suggest(u32 priority, std::size_t core, Thread* thread);
  35. /**
  36. * Remove a thread to the suggested queue of a cpu core. Suggested threads may be
  37. * picked if no thread is scheduled to run on the core.
  38. */
  39. void Unsuggest(u32 priority, std::size_t core, Thread* thread);
  40. /**
  41. * Add a thread to the scheduling queue of a cpu core. The thread is added at the
  42. * back the queue in its priority level.
  43. */
  44. void Schedule(u32 priority, std::size_t core, Thread* thread);
  45. /**
  46. * Add a thread to the scheduling queue of a cpu core. The thread is added at the
  47. * front the queue in its priority level.
  48. */
  49. void SchedulePrepend(u32 priority, std::size_t core, Thread* thread);
  50. /// Reschedule an already scheduled thread based on a new priority
  51. void Reschedule(u32 priority, std::size_t core, Thread* thread);
  52. /// Unschedules a thread.
  53. void Unschedule(u32 priority, std::size_t core, Thread* thread);
  54. /// Selects a core and forces it to unload its current thread's context
  55. void UnloadThread(std::size_t core);
  56. /**
  57. * Takes care of selecting the new scheduled thread in three steps:
  58. *
  59. * 1. First a thread is selected from the top of the priority queue. If no thread
  60. * is obtained then we move to step two, else we are done.
  61. *
  62. * 2. Second we try to get a suggested thread that's not assigned to any core or
  63. * that is not the top thread in that core.
  64. *
  65. * 3. Third is no suggested thread is found, we do a second pass and pick a running
  66. * thread in another core and swap it with its current thread.
  67. */
  68. void SelectThread(std::size_t core);
  69. bool HaveReadyThreads(std::size_t core_id) const {
  70. return !scheduled_queue[core_id].empty();
  71. }
  72. /**
  73. * Takes a thread and moves it to the back of the it's priority list.
  74. *
  75. * @note This operation can be redundant and no scheduling is changed if marked as so.
  76. */
  77. bool YieldThread(Thread* thread);
  78. /**
  79. * Takes a thread and moves it to the back of the it's priority list.
  80. * Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
  81. * a better priority than the next thread in the core.
  82. *
  83. * @note This operation can be redundant and no scheduling is changed if marked as so.
  84. */
  85. bool YieldThreadAndBalanceLoad(Thread* thread);
  86. /**
  87. * Takes a thread and moves it out of the scheduling queue.
  88. * and into the suggested queue. If no thread can be scheduled afterwards in that core,
  89. * a suggested thread is obtained instead.
  90. *
  91. * @note This operation can be redundant and no scheduling is changed if marked as so.
  92. */
  93. bool YieldThreadAndWaitForLoadBalancing(Thread* thread);
  94. /**
  95. * Rotates the scheduling queues of threads at a preemption priority and then does
  96. * some core rebalancing. Preemption priorities can be found in the array
  97. * 'preemption_priorities'.
  98. *
  99. * @note This operation happens every 10ms.
  100. */
  101. void PreemptThreads();
  102. u32 CpuCoresCount() const {
  103. return NUM_CPU_CORES;
  104. }
  105. void SetReselectionPending() {
  106. is_reselection_pending.store(true, std::memory_order_release);
  107. }
  108. bool IsReselectionPending() const {
  109. return is_reselection_pending.load(std::memory_order_acquire);
  110. }
  111. void Shutdown();
  112. private:
  113. /**
  114. * Transfers a thread into an specific core. If the destination_core is -1
  115. * it will be unscheduled from its source code and added into its suggested
  116. * queue.
  117. */
  118. void TransferToCore(u32 priority, s32 destination_core, Thread* thread);
  119. bool AskForReselectionOrMarkRedundant(Thread* current_thread, const Thread* winner);
  120. static constexpr u32 min_regular_priority = 2;
  121. std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> scheduled_queue;
  122. std::array<Common::MultiLevelQueue<Thread*, THREADPRIO_COUNT>, NUM_CPU_CORES> suggested_queue;
  123. std::atomic<bool> is_reselection_pending{false};
  124. // The priority levels at which the global scheduler preempts threads every 10 ms. They are
  125. // ordered from Core 0 to Core 3.
  126. std::array<u32, NUM_CPU_CORES> preemption_priorities = {59, 59, 59, 62};
  127. /// Lists all thread ids that aren't deleted/etc.
  128. std::vector<std::shared_ptr<Thread>> thread_list;
  129. Core::System& system;
  130. };
  131. class Scheduler final {
  132. public:
  133. explicit Scheduler(Core::System& system, Core::ARM_Interface& cpu_core, std::size_t core_id);
  134. ~Scheduler();
  135. /// Returns whether there are any threads that are ready to run.
  136. bool HaveReadyThreads() const;
  137. /// Reschedules to the next available thread (call after current thread is suspended)
  138. void TryDoContextSwitch();
  139. /// Unloads currently running thread
  140. void UnloadThread();
  141. /// Select the threads in top of the scheduling multilist.
  142. void SelectThreads();
  143. /// Gets the current running thread
  144. Thread* GetCurrentThread() const;
  145. /// Gets the currently selected thread from the top of the multilevel queue
  146. Thread* GetSelectedThread() const;
  147. /// Gets the timestamp for the last context switch in ticks.
  148. u64 GetLastContextSwitchTicks() const;
  149. bool ContextSwitchPending() const {
  150. return is_context_switch_pending;
  151. }
  152. /// Shutdowns the scheduler.
  153. void Shutdown();
  154. private:
  155. friend class GlobalScheduler;
  156. /// Switches the CPU's active thread context to that of the specified thread
  157. void SwitchContext();
  158. /**
  159. * Called on every context switch to update the internal timestamp
  160. * This also updates the running time ticks for the given thread and
  161. * process using the following difference:
  162. *
  163. * ticks += most_recent_ticks - last_context_switch_ticks
  164. *
  165. * The internal tick timestamp for the scheduler is simply the
  166. * most recent tick count retrieved. No special arithmetic is
  167. * applied to it.
  168. */
  169. void UpdateLastContextSwitchTime(Thread* thread, Process* process);
  170. std::shared_ptr<Thread> current_thread = nullptr;
  171. std::shared_ptr<Thread> selected_thread = nullptr;
  172. Core::System& system;
  173. Core::ARM_Interface& cpu_core;
  174. u64 last_context_switch_time = 0;
  175. u64 idle_selection_count = 0;
  176. const std::size_t core_id;
  177. bool is_context_switch_pending = false;
  178. };
  179. } // namespace Kernel