scheduler.h 7.3 KB

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