Comment and reorganize the scheduler
This commit is contained in:
parent
b5d1e44782
commit
3a94e7ea33
|
@ -19,6 +19,11 @@
|
||||||
|
|
||||||
namespace Kernel {
|
namespace Kernel {
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SelectThreads, Yield functions originally by TuxSH.
|
||||||
|
* licensed under GPLv2 or later under exception provided by the author.
|
||||||
|
*/
|
||||||
|
|
||||||
void GlobalScheduler::AddThread(SharedPtr<Thread> thread) {
|
void GlobalScheduler::AddThread(SharedPtr<Thread> thread) {
|
||||||
thread_list.push_back(std::move(thread));
|
thread_list.push_back(std::move(thread));
|
||||||
}
|
}
|
||||||
|
@ -29,15 +34,23 @@ void GlobalScheduler::RemoveThread(Thread* thread) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* SelectThreads, Yield functions originally by TuxSH.
|
* UnloadThread selects a core and forces it to unload its current thread's context
|
||||||
* licensed under GPLv2 or later under exception provided by the author.
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void GlobalScheduler::UnloadThread(s32 core) {
|
void GlobalScheduler::UnloadThread(s32 core) {
|
||||||
Scheduler& sched = Core::System::GetInstance().Scheduler(core);
|
Scheduler& sched = Core::System::GetInstance().Scheduler(core);
|
||||||
sched.UnloadThread();
|
sched.UnloadThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* SelectThread takes care of selecting the new scheduled thread.
|
||||||
|
* It does it in 3 steps:
|
||||||
|
* - First a thread is selected from the top of the priority queue. If no thread
|
||||||
|
* is obtained then we move to step two, else we are done.
|
||||||
|
* - Second we try to get a suggested thread that's not assigned to any core or
|
||||||
|
* that is not the top thread in that core.
|
||||||
|
* - Third is no suggested thread is found, we do a second pass and pick a running
|
||||||
|
* thread in another core and swap it with its current thread.
|
||||||
|
*/
|
||||||
void GlobalScheduler::SelectThread(u32 core) {
|
void GlobalScheduler::SelectThread(u32 core) {
|
||||||
auto update_thread = [](Thread* thread, Scheduler& sched) {
|
auto update_thread = [](Thread* thread, Scheduler& sched) {
|
||||||
if (thread != sched.selected_thread) {
|
if (thread != sched.selected_thread) {
|
||||||
|
@ -51,105 +64,58 @@ void GlobalScheduler::SelectThread(u32 core) {
|
||||||
};
|
};
|
||||||
Scheduler& sched = Core::System::GetInstance().Scheduler(core);
|
Scheduler& sched = Core::System::GetInstance().Scheduler(core);
|
||||||
Thread* current_thread = nullptr;
|
Thread* current_thread = nullptr;
|
||||||
|
// Step 1: Get top thread in schedule queue.
|
||||||
current_thread = scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front();
|
current_thread = scheduled_queue[core].empty() ? nullptr : scheduled_queue[core].front();
|
||||||
if (!current_thread) {
|
if (current_thread) {
|
||||||
Thread* winner = nullptr;
|
update_thread(current_thread, sched);
|
||||||
std::set<s32> sug_cores;
|
return;
|
||||||
for (auto thread : suggested_queue[core]) {
|
}
|
||||||
s32 this_core = thread->GetProcessorID();
|
// Step 2: Try selecting a suggested thread.
|
||||||
Thread* thread_on_core = nullptr;
|
Thread* winner = nullptr;
|
||||||
if (this_core >= 0) {
|
std::set<s32> sug_cores;
|
||||||
thread_on_core = scheduled_queue[this_core].front();
|
for (auto thread : suggested_queue[core]) {
|
||||||
}
|
s32 this_core = thread->GetProcessorID();
|
||||||
if (this_core < 0 || thread != thread_on_core) {
|
Thread* thread_on_core = nullptr;
|
||||||
winner = thread;
|
if (this_core >= 0) {
|
||||||
break;
|
thread_on_core = scheduled_queue[this_core].front();
|
||||||
}
|
|
||||||
sug_cores.insert(this_core);
|
|
||||||
}
|
}
|
||||||
if (winner && winner->GetPriority() > 2) {
|
if (this_core < 0 || thread != thread_on_core) {
|
||||||
if (winner->IsRunning()) {
|
winner = thread;
|
||||||
UnloadThread(winner->GetProcessorID());
|
break;
|
||||||
}
|
}
|
||||||
TransferToCore(winner->GetPriority(), core, winner);
|
sug_cores.insert(this_core);
|
||||||
current_thread = winner;
|
}
|
||||||
} else {
|
// if we got a suggested thread, select it, else do a second pass.
|
||||||
for (auto& src_core : sug_cores) {
|
if (winner && winner->GetPriority() > 2) {
|
||||||
auto it = scheduled_queue[src_core].begin();
|
if (winner->IsRunning()) {
|
||||||
it++;
|
UnloadThread(winner->GetProcessorID());
|
||||||
if (it != scheduled_queue[src_core].end()) {
|
}
|
||||||
Thread* thread_on_core = scheduled_queue[src_core].front();
|
TransferToCore(winner->GetPriority(), core, winner);
|
||||||
Thread* to_change = *it;
|
update_thread(winner, sched);
|
||||||
if (thread_on_core->IsRunning() || to_change->IsRunning()) {
|
return;
|
||||||
UnloadThread(src_core);
|
}
|
||||||
}
|
// Step 3: Select a suggested thread from another core
|
||||||
TransferToCore(thread_on_core->GetPriority(), core, thread_on_core);
|
for (auto& src_core : sug_cores) {
|
||||||
current_thread = thread_on_core;
|
auto it = scheduled_queue[src_core].begin();
|
||||||
}
|
it++;
|
||||||
|
if (it != scheduled_queue[src_core].end()) {
|
||||||
|
Thread* thread_on_core = scheduled_queue[src_core].front();
|
||||||
|
Thread* to_change = *it;
|
||||||
|
if (thread_on_core->IsRunning() || to_change->IsRunning()) {
|
||||||
|
UnloadThread(src_core);
|
||||||
}
|
}
|
||||||
|
TransferToCore(thread_on_core->GetPriority(), core, thread_on_core);
|
||||||
|
current_thread = thread_on_core;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
update_thread(current_thread, sched);
|
update_thread(current_thread, sched);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GlobalScheduler::SelectThreads() {
|
/*
|
||||||
auto update_thread = [](Thread* thread, Scheduler& sched) {
|
* YieldThread takes a thread and moves it to the back of the it's priority list
|
||||||
if (thread != sched.selected_thread) {
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
if (thread == nullptr) {
|
*/
|
||||||
++sched.idle_selection_count;
|
|
||||||
}
|
|
||||||
sched.selected_thread = thread;
|
|
||||||
}
|
|
||||||
sched.context_switch_pending = sched.selected_thread != sched.current_thread;
|
|
||||||
std::atomic_thread_fence(std::memory_order_seq_cst);
|
|
||||||
};
|
|
||||||
|
|
||||||
auto& system = Core::System::GetInstance();
|
|
||||||
|
|
||||||
std::unordered_set<Thread*> picked_threads;
|
|
||||||
// This maintain the "current thread is on front of queue" invariant
|
|
||||||
std::array<Thread*, NUM_CPU_CORES> current_threads;
|
|
||||||
for (u32 i = 0; i < NUM_CPU_CORES; i++) {
|
|
||||||
Scheduler& sched = system.Scheduler(i);
|
|
||||||
current_threads[i] = scheduled_queue[i].empty() ? nullptr : scheduled_queue[i].front();
|
|
||||||
if (current_threads[i])
|
|
||||||
picked_threads.insert(current_threads[i]);
|
|
||||||
update_thread(current_threads[i], sched);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Do some load-balancing. Allow second pass.
|
|
||||||
std::array<Thread*, NUM_CPU_CORES> current_threads_2 = current_threads;
|
|
||||||
for (u32 i = 0; i < NUM_CPU_CORES; i++) {
|
|
||||||
if (!scheduled_queue[i].empty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
Thread* winner = nullptr;
|
|
||||||
for (auto thread : suggested_queue[i]) {
|
|
||||||
if (thread->GetProcessorID() < 0 || thread != current_threads[i]) {
|
|
||||||
if (picked_threads.count(thread) == 0 && !thread->IsRunning()) {
|
|
||||||
winner = thread;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (winner) {
|
|
||||||
TransferToCore(winner->GetPriority(), i, winner);
|
|
||||||
current_threads_2[i] = winner;
|
|
||||||
picked_threads.insert(winner);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// See which to-be-current threads have changed & update accordingly
|
|
||||||
for (u32 i = 0; i < NUM_CPU_CORES; i++) {
|
|
||||||
Scheduler& sched = system.Scheduler(i);
|
|
||||||
if (current_threads_2[i] != current_threads[i]) {
|
|
||||||
update_thread(current_threads_2[i], sched);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
reselection_pending.store(false, std::memory_order_release);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GlobalScheduler::YieldThread(Thread* yielding_thread) {
|
void GlobalScheduler::YieldThread(Thread* yielding_thread) {
|
||||||
// Note: caller should use critical section, etc.
|
// Note: caller should use critical section, etc.
|
||||||
u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
|
u32 core_id = static_cast<u32>(yielding_thread->GetProcessorID());
|
||||||
|
@ -164,6 +130,12 @@ void GlobalScheduler::YieldThread(Thread* yielding_thread) {
|
||||||
AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* YieldThreadAndBalanceLoad takes a thread and moves it to the back of the it's priority list.
|
||||||
|
* Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
|
||||||
|
* a better priority than the next thread in the core.
|
||||||
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
|
*/
|
||||||
void GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
|
void GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
|
||||||
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
||||||
// etc.
|
// etc.
|
||||||
|
@ -213,6 +185,12 @@ void GlobalScheduler::YieldThreadAndBalanceLoad(Thread* yielding_thread) {
|
||||||
AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
AskForReselectionOrMarkRedundant(yielding_thread, winner);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* YieldThreadAndWaitForLoadBalancing takes a thread and moves it out of the scheduling queue
|
||||||
|
* and into the suggested queue. If no thread can be squeduled afterwards in that core,
|
||||||
|
* a suggested thread is obtained instead.
|
||||||
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
|
*/
|
||||||
void GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread) {
|
void GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread) {
|
||||||
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
// Note: caller should check if !thread.IsSchedulerOperationRedundant and use critical section,
|
||||||
// etc.
|
// etc.
|
||||||
|
@ -256,8 +234,8 @@ void GlobalScheduler::YieldThreadAndWaitForLoadBalancing(Thread* yielding_thread
|
||||||
|
|
||||||
void GlobalScheduler::AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner) {
|
void GlobalScheduler::AskForReselectionOrMarkRedundant(Thread* current_thread, Thread* winner) {
|
||||||
if (current_thread == winner) {
|
if (current_thread == winner) {
|
||||||
// Nintendo (not us) has a nullderef bug on current_thread->owner, but which is never
|
// TODO(blinkhawk): manage redundant operations, this is not implemented.
|
||||||
// triggered.
|
// as its mostly an optimization.
|
||||||
// current_thread->SetRedundantSchedulerOperation();
|
// current_thread->SetRedundantSchedulerOperation();
|
||||||
} else {
|
} else {
|
||||||
reselection_pending.store(true, std::memory_order_release);
|
reselection_pending.store(true, std::memory_order_release);
|
||||||
|
|
|
@ -48,14 +48,12 @@ public:
|
||||||
}
|
}
|
||||||
|
|
||||||
void Schedule(u32 priority, u32 core, Thread* thread) {
|
void Schedule(u32 priority, u32 core, Thread* thread) {
|
||||||
ASSERT_MSG(thread->GetProcessorID() == core,
|
ASSERT_MSG(thread->GetProcessorID() == core, "Thread must be assigned to this core.");
|
||||||
"Thread must be assigned to this core.");
|
|
||||||
scheduled_queue[core].add(thread, priority);
|
scheduled_queue[core].add(thread, priority);
|
||||||
}
|
}
|
||||||
|
|
||||||
void SchedulePrepend(u32 priority, u32 core, Thread* thread) {
|
void SchedulePrepend(u32 priority, u32 core, Thread* thread) {
|
||||||
ASSERT_MSG(thread->GetProcessorID() == core,
|
ASSERT_MSG(thread->GetProcessorID() == core, "Thread must be assigned to this core.");
|
||||||
"Thread must be assigned to this core.");
|
|
||||||
scheduled_queue[core].add(thread, priority, false);
|
scheduled_queue[core].add(thread, priority, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,17 +82,47 @@ public:
|
||||||
Suggest(priority, source_core, thread);
|
Suggest(priority, source_core, thread);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* UnloadThread selects a core and forces it to unload its current thread's context
|
||||||
|
*/
|
||||||
void UnloadThread(s32 core);
|
void UnloadThread(s32 core);
|
||||||
|
|
||||||
void SelectThreads();
|
/*
|
||||||
|
* SelectThread takes care of selecting the new scheduled thread.
|
||||||
|
* It does it in 3 steps:
|
||||||
|
* - First a thread is selected from the top of the priority queue. If no thread
|
||||||
|
* is obtained then we move to step two, else we are done.
|
||||||
|
* - Second we try to get a suggested thread that's not assigned to any core or
|
||||||
|
* that is not the top thread in that core.
|
||||||
|
* - Third is no suggested thread is found, we do a second pass and pick a running
|
||||||
|
* thread in another core and swap it with its current thread.
|
||||||
|
*/
|
||||||
void SelectThread(u32 core);
|
void SelectThread(u32 core);
|
||||||
|
|
||||||
bool HaveReadyThreads(u32 core_id) {
|
bool HaveReadyThreads(u32 core_id) {
|
||||||
return !scheduled_queue[core_id].empty();
|
return !scheduled_queue[core_id].empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* YieldThread takes a thread and moves it to the back of the it's priority list
|
||||||
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
|
*/
|
||||||
void YieldThread(Thread* thread);
|
void YieldThread(Thread* thread);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* YieldThreadAndBalanceLoad takes a thread and moves it to the back of the it's priority list.
|
||||||
|
* Afterwards, tries to pick a suggested thread from the suggested queue that has worse time or
|
||||||
|
* a better priority than the next thread in the core.
|
||||||
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
|
*/
|
||||||
void YieldThreadAndBalanceLoad(Thread* thread);
|
void YieldThreadAndBalanceLoad(Thread* thread);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* YieldThreadAndWaitForLoadBalancing takes a thread and moves it out of the scheduling queue
|
||||||
|
* and into the suggested queue. If no thread can be squeduled afterwards in that core,
|
||||||
|
* a suggested thread is obtained instead.
|
||||||
|
* This operation can be redundant and no scheduling is changed if marked as so.
|
||||||
|
*/
|
||||||
void YieldThreadAndWaitForLoadBalancing(Thread* thread);
|
void YieldThreadAndWaitForLoadBalancing(Thread* thread);
|
||||||
|
|
||||||
u32 CpuCoresCount() const {
|
u32 CpuCoresCount() const {
|
||||||
|
|
Reference in New Issue