2019-12-13 10:21:43 +01:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-12-13 10:21:43 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <mesosphere.hpp>
|
|
|
|
|
2021-11-16 20:25:57 +01:00
|
|
|
#pragma GCC push_options
|
|
|
|
#pragma GCC optimize ("-O3")
|
2019-12-13 10:21:43 +01:00
|
|
|
|
2021-11-16 20:25:57 +01:00
|
|
|
namespace ams::kern {
|
2021-01-08 11:13:36 +01:00
|
|
|
|
2020-02-14 07:05:20 +01:00
|
|
|
bool KScheduler::s_scheduler_update_needed;
|
|
|
|
KScheduler::LockType KScheduler::s_scheduler_lock;
|
|
|
|
KSchedulerPriorityQueue KScheduler::s_priority_queue;
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
namespace {
|
|
|
|
|
2021-09-18 01:18:11 +02:00
|
|
|
class KSchedulerInterruptHandler : public KInterruptHandler {
|
2020-02-05 23:07:51 +01:00
|
|
|
public:
|
2021-09-18 01:18:11 +02:00
|
|
|
constexpr KSchedulerInterruptHandler() : KInterruptHandler() { /* ... */ }
|
2020-02-05 23:07:51 +01:00
|
|
|
|
|
|
|
virtual KInterruptTask *OnInterrupt(s32 interrupt_id) override {
|
2020-08-17 23:20:24 +02:00
|
|
|
MESOSPHERE_UNUSED(interrupt_id);
|
2020-02-05 23:07:51 +01:00
|
|
|
return GetDummyInterruptTask();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
ALWAYS_INLINE void IncrementScheduledCount(KThread *thread) {
|
|
|
|
if (KProcess *parent = thread->GetOwnerProcess(); parent != nullptr) {
|
2020-02-20 05:42:21 +01:00
|
|
|
parent->IncrementScheduledCount();
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-18 01:18:11 +02:00
|
|
|
KSchedulerInterruptHandler g_scheduler_interrupt_handler;
|
2020-02-05 23:07:51 +01:00
|
|
|
|
2021-09-18 01:18:11 +02:00
|
|
|
ALWAYS_INLINE auto *GetSchedulerInterruptHandler() {
|
|
|
|
return std::addressof(g_scheduler_interrupt_handler);
|
2020-02-05 23:07:51 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::Initialize(KThread *idle_thread) {
|
2021-09-18 01:12:01 +02:00
|
|
|
/* Set core ID/idle thread/interrupt task manager. */
|
|
|
|
m_core_id = GetCurrentCoreId();
|
|
|
|
m_idle_thread = idle_thread;
|
|
|
|
m_state.idle_thread_stack = m_idle_thread->GetStackTop();
|
|
|
|
m_state.interrupt_task_manager = std::addressof(Kernel::GetInterruptTaskManager());
|
2020-02-05 22:02:35 +01:00
|
|
|
|
|
|
|
/* Insert the main thread into the priority queue. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock lk;
|
|
|
|
GetPriorityQueue().PushBack(GetCurrentThreadPointer());
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
|
2020-02-05 23:07:51 +01:00
|
|
|
/* Bind interrupt handler. */
|
2021-09-18 01:18:11 +02:00
|
|
|
Kernel::GetInterruptManager().BindHandler(GetSchedulerInterruptHandler(), KInterruptName_Scheduler, m_core_id, KInterruptController::PriorityLevel_Scheduler, false, false);
|
2020-12-01 22:41:37 +01:00
|
|
|
|
|
|
|
/* Set the current thread. */
|
2020-12-18 02:18:47 +01:00
|
|
|
m_current_thread = GetCurrentThreadPointer();
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::Activate() {
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentThread().GetDisableDispatchCount() == 1);
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_state.should_count_idle = KTargetSystem::IsDebugMode();
|
|
|
|
m_is_active = true;
|
2020-02-05 22:02:35 +01:00
|
|
|
RescheduleCurrentCore();
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 KScheduler::UpdateHighestPriorityThread(KThread *highest_thread) {
|
2020-12-18 02:18:47 +01:00
|
|
|
if (KThread *prev_highest_thread = m_state.highest_priority_thread; AMS_LIKELY(prev_highest_thread != highest_thread)) {
|
2020-02-05 22:02:35 +01:00
|
|
|
if (AMS_LIKELY(prev_highest_thread != nullptr)) {
|
|
|
|
IncrementScheduledCount(prev_highest_thread);
|
|
|
|
prev_highest_thread->SetLastScheduledTick(KHardwareTimer::GetTick());
|
|
|
|
}
|
2020-12-18 02:18:47 +01:00
|
|
|
if (m_state.should_count_idle) {
|
2020-02-05 22:02:35 +01:00
|
|
|
if (AMS_LIKELY(highest_thread != nullptr)) {
|
2020-07-31 10:27:09 +02:00
|
|
|
if (KProcess *process = highest_thread->GetOwnerProcess(); process != nullptr) {
|
2023-02-21 11:12:17 +01:00
|
|
|
/* Set running thread (and increment switch count). */
|
|
|
|
process->SetRunningThread(m_core_id, highest_thread, m_state.idle_count, ++m_state.switch_count);
|
2020-07-31 10:27:09 +02:00
|
|
|
}
|
2020-02-05 22:02:35 +01:00
|
|
|
} else {
|
2023-02-21 11:12:17 +01:00
|
|
|
/* Set idle count and switch count to switch count + 1. */
|
|
|
|
m_state.idle_count = ++m_state.switch_count;
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
MESOSPHERE_KTRACE_SCHEDULE_UPDATE(m_core_id, (prev_highest_thread != nullptr ? prev_highest_thread : m_idle_thread), (highest_thread != nullptr ? highest_thread : m_idle_thread));
|
2020-08-04 03:11:13 +02:00
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
m_state.highest_priority_thread = highest_thread;
|
|
|
|
m_state.needs_scheduling = true;
|
|
|
|
return (1ul << m_core_id);
|
2020-02-05 22:02:35 +01:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 KScheduler::UpdateHighestPriorityThreadsImpl() {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* Clear that we need to update. */
|
|
|
|
ClearSchedulerUpdateNeeded();
|
|
|
|
|
|
|
|
u64 cores_needing_scheduling = 0, idle_cores = 0;
|
|
|
|
KThread *top_threads[cpu::NumCores];
|
|
|
|
auto &priority_queue = GetPriorityQueue();
|
|
|
|
|
|
|
|
/* We want to go over all cores, finding the highest priority thread and determining if scheduling is needed for that core. */
|
|
|
|
for (size_t core_id = 0; core_id < cpu::NumCores; core_id++) {
|
|
|
|
KThread *top_thread = priority_queue.GetScheduledFront(core_id);
|
|
|
|
if (top_thread != nullptr) {
|
2021-10-19 10:20:28 +02:00
|
|
|
/* We need to check if the thread's process has a pinned thread. */
|
|
|
|
if (KProcess *parent = top_thread->GetOwnerProcess(); parent != nullptr) {
|
|
|
|
/* Check that there's a pinned thread other than the current top thread. */
|
|
|
|
if (KThread *pinned = parent->GetPinnedThread(core_id); pinned != nullptr && pinned != top_thread) {
|
|
|
|
/* We need to prefer threads with kernel waiters to the pinned thread. */
|
|
|
|
if (top_thread->GetNumKernelWaiters() == 0 && top_thread != parent->GetExceptionThread()) {
|
|
|
|
/* If the pinned thread is runnable, use it. */
|
2020-07-28 02:32:04 +02:00
|
|
|
if (pinned->GetRawState() == KThread::ThreadState_Runnable) {
|
|
|
|
top_thread = pinned;
|
2020-02-05 22:02:35 +01:00
|
|
|
} else {
|
|
|
|
top_thread = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idle_cores |= (1ul << core_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
top_threads[core_id] = top_thread;
|
|
|
|
cores_needing_scheduling |= Kernel::GetScheduler(core_id).UpdateHighestPriorityThread(top_threads[core_id]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Idle cores are bad. We're going to try to migrate threads to each idle core in turn. */
|
|
|
|
while (idle_cores != 0) {
|
|
|
|
s32 core_id = __builtin_ctzll(idle_cores);
|
|
|
|
|
|
|
|
if (KThread *suggested = priority_queue.GetSuggestedFront(core_id); suggested != nullptr) {
|
|
|
|
s32 migration_candidates[cpu::NumCores];
|
|
|
|
size_t num_candidates = 0;
|
|
|
|
|
|
|
|
/* While we have a suggested thread, try to migrate it! */
|
|
|
|
while (suggested != nullptr) {
|
|
|
|
/* Check if the suggested thread is the top thread on its core. */
|
|
|
|
const s32 suggested_core = suggested->GetActiveCore();
|
|
|
|
if (KThread *top_thread = (suggested_core >= 0) ? top_threads[suggested_core] : nullptr; top_thread != suggested) {
|
|
|
|
/* Make sure we're not dealing with threads too high priority for migration. */
|
|
|
|
if (top_thread != nullptr && top_thread->GetPriority() < HighestCoreMigrationAllowedPriority) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The suggested thread isn't bound to its core, so we can migrate it! */
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(suggested_core, suggested);
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_CORE_MIGRATION(suggested->GetId(), suggested_core, core_id, 1);
|
2020-02-05 22:02:35 +01:00
|
|
|
top_threads[core_id] = suggested;
|
|
|
|
cores_needing_scheduling |= Kernel::GetScheduler(core_id).UpdateHighestPriorityThread(top_threads[core_id]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Note this core as a candidate for migration. */
|
|
|
|
MESOSPHERE_ASSERT(num_candidates < cpu::NumCores);
|
|
|
|
migration_candidates[num_candidates++] = suggested_core;
|
|
|
|
suggested = priority_queue.GetSuggestedNext(core_id, suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If suggested is nullptr, we failed to migrate a specific thread. So let's try all our candidate cores' top threads. */
|
|
|
|
if (suggested == nullptr) {
|
|
|
|
for (size_t i = 0; i < num_candidates; i++) {
|
|
|
|
/* Check if there's some other thread that can run on the candidate core. */
|
|
|
|
const s32 candidate_core = migration_candidates[i];
|
|
|
|
suggested = top_threads[candidate_core];
|
|
|
|
if (KThread *next_on_candidate_core = priority_queue.GetScheduledNext(candidate_core, suggested); next_on_candidate_core != nullptr) {
|
|
|
|
/* The candidate core can run some other thread! We'll migrate its current top thread to us. */
|
|
|
|
top_threads[candidate_core] = next_on_candidate_core;
|
|
|
|
cores_needing_scheduling |= Kernel::GetScheduler(candidate_core).UpdateHighestPriorityThread(top_threads[candidate_core]);
|
|
|
|
|
|
|
|
/* Perform the migration. */
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(candidate_core, suggested);
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_CORE_MIGRATION(suggested->GetId(), candidate_core, core_id, 2);
|
2020-02-05 22:02:35 +01:00
|
|
|
top_threads[core_id] = suggested;
|
|
|
|
cores_needing_scheduling |= Kernel::GetScheduler(core_id).UpdateHighestPriorityThread(top_threads[core_id]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idle_cores &= ~(1ul << core_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cores_needing_scheduling;
|
2019-12-13 10:21:43 +01:00
|
|
|
}
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
void KScheduler::SwitchThread(KThread *next_thread) {
|
2021-09-18 01:12:01 +02:00
|
|
|
KProcess * const cur_process = GetCurrentProcessPointer();
|
|
|
|
KThread * const cur_thread = GetCurrentThreadPointer();
|
2020-02-05 22:02:35 +01:00
|
|
|
|
|
|
|
/* We never want to schedule a null thread, so use the idle thread if we don't have a next. */
|
|
|
|
if (next_thread == nullptr) {
|
2020-12-18 02:18:47 +01:00
|
|
|
next_thread = m_idle_thread;
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
|
2021-05-31 13:20:59 +02:00
|
|
|
if (next_thread->GetCurrentCore() != m_core_id) {
|
|
|
|
next_thread->SetCurrentCore(m_core_id);
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
/* If we're not actually switching thread, there's nothing to do. */
|
|
|
|
if (next_thread == cur_thread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Next thread is now known not to be nullptr, and must not be dispatchable. */
|
|
|
|
MESOSPHERE_ASSERT(next_thread->GetDisableDispatchCount() == 1);
|
|
|
|
|
|
|
|
/* Update the CPU time tracking variables. */
|
2020-12-18 02:18:47 +01:00
|
|
|
const s64 prev_tick = m_last_context_switch_time;
|
2020-02-05 22:02:35 +01:00
|
|
|
const s64 cur_tick = KHardwareTimer::GetTick();
|
|
|
|
const s64 tick_diff = cur_tick - prev_tick;
|
2020-12-18 02:18:47 +01:00
|
|
|
cur_thread->AddCpuTime(m_core_id, tick_diff);
|
2020-02-05 22:02:35 +01:00
|
|
|
if (cur_process != nullptr) {
|
2020-02-20 05:42:21 +01:00
|
|
|
cur_process->AddCpuTime(tick_diff);
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
2020-12-18 02:18:47 +01:00
|
|
|
m_last_context_switch_time = cur_tick;
|
2020-02-05 22:02:35 +01:00
|
|
|
|
|
|
|
/* Update our previous thread. */
|
|
|
|
if (cur_process != nullptr) {
|
|
|
|
/* NOTE: Combining this into AMS_LIKELY(!... && ...) triggers an internal compiler error: Segmentation fault in GCC 9.2.0. */
|
2020-12-18 02:18:47 +01:00
|
|
|
if (AMS_LIKELY(!cur_thread->IsTerminationRequested()) && AMS_LIKELY(cur_thread->GetActiveCore() == m_core_id)) {
|
2021-10-20 22:29:38 +02:00
|
|
|
m_state.prev_thread = cur_thread;
|
2020-02-05 22:02:35 +01:00
|
|
|
} else {
|
2023-02-21 21:15:01 +01:00
|
|
|
m_state.prev_thread = nullptr;
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_THREAD_SWITCH(next_thread);
|
|
|
|
|
2021-10-27 21:31:53 +02:00
|
|
|
#if defined(MESOSPHERE_ENABLE_HARDWARE_SINGLE_STEP)
|
|
|
|
/* Ensure the single-step bit in mdscr reflects the correct single-step state for the new thread. */
|
2021-11-06 07:38:43 +01:00
|
|
|
/* NOTE: Per ARM docs, changing the single-step bit requires a "context synchronization event" to */
|
|
|
|
/* be sure that our new configuration takes. However, there are three types of synchronization event: */
|
|
|
|
/* Taking an exception, returning from an exception, and ISB. The single-step bit change only matters */
|
|
|
|
/* in EL0...which implies a return-from-exception has occurred since we set the bit. Thus, forcing */
|
|
|
|
/* an ISB is unnecessary, and we can modify the register safely and be confident it will affect the next */
|
|
|
|
/* userland instruction executed. */
|
2022-03-23 03:59:39 +01:00
|
|
|
cpu::MonitorDebugSystemControlRegisterAccessor().SetSoftwareStep(next_thread->IsHardwareSingleStep()).Store();
|
2021-10-27 21:31:53 +02:00
|
|
|
#endif
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
/* Switch the current process, if we're switching processes. */
|
|
|
|
if (KProcess *next_process = next_thread->GetOwnerProcess(); next_process != cur_process) {
|
2020-02-20 05:42:21 +01:00
|
|
|
KProcess::Switch(cur_process, next_process);
|
2020-02-05 22:02:35 +01:00
|
|
|
}
|
|
|
|
|
2020-02-15 00:27:34 +01:00
|
|
|
/* Set the new thread. */
|
|
|
|
SetCurrentThread(next_thread);
|
2020-12-18 02:18:47 +01:00
|
|
|
m_current_thread = next_thread;
|
2020-02-15 00:27:34 +01:00
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
/* Set the new Thread Local region. */
|
|
|
|
cpu::SwitchThreadLocalRegion(GetInteger(next_thread->GetThreadLocalRegionAddress()));
|
|
|
|
}
|
|
|
|
|
2020-07-11 03:39:53 +02:00
|
|
|
void KScheduler::ClearPreviousThread(KThread *thread) {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
for (size_t i = 0; i < cpu::NumCores; ++i) {
|
2021-10-20 22:29:38 +02:00
|
|
|
/* Get an atomic reference to the core scheduler's previous thread. */
|
|
|
|
const util::AtomicRef<KThread *> prev_thread(Kernel::GetScheduler(static_cast<s32>(i)).m_state.prev_thread);
|
|
|
|
|
2020-08-13 05:47:14 +02:00
|
|
|
/* Atomically clear the previous thread if it's our target. */
|
2020-08-13 22:30:47 +02:00
|
|
|
KThread *compare = thread;
|
2021-10-20 22:29:38 +02:00
|
|
|
prev_thread.CompareExchangeStrong(compare, nullptr);
|
2020-07-11 03:39:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 22:02:35 +01:00
|
|
|
void KScheduler::OnThreadStateChanged(KThread *thread, KThread::ThreadState old_state) {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* Check if the state has changed, because if it hasn't there's nothing to do. */
|
2020-02-06 14:34:38 +01:00
|
|
|
const KThread::ThreadState cur_state = thread->GetRawState();
|
2020-02-05 22:02:35 +01:00
|
|
|
if (cur_state == old_state) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update the priority queues. */
|
|
|
|
if (old_state == KThread::ThreadState_Runnable) {
|
|
|
|
/* If we were previously runnable, then we're not runnable now, and we should remove. */
|
|
|
|
GetPriorityQueue().Remove(thread);
|
|
|
|
IncrementScheduledCount(thread);
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
} else if (cur_state == KThread::ThreadState_Runnable) {
|
|
|
|
/* If we're now runnable, then we weren't previously, and we should add. */
|
|
|
|
GetPriorityQueue().PushBack(thread);
|
|
|
|
IncrementScheduledCount(thread);
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::OnThreadPriorityChanged(KThread *thread, s32 old_priority) {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* If the thread is runnable, we want to change its priority in the queue. */
|
2020-02-06 14:34:38 +01:00
|
|
|
if (thread->GetRawState() == KThread::ThreadState_Runnable) {
|
2020-02-05 22:02:35 +01:00
|
|
|
GetPriorityQueue().ChangePriority(old_priority, thread == GetCurrentThreadPointer(), thread);
|
|
|
|
IncrementScheduledCount(thread);
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::OnThreadAffinityMaskChanged(KThread *thread, const KAffinityMask &old_affinity, s32 old_core) {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* If the thread is runnable, we want to change its affinity in the queue. */
|
2020-02-06 14:34:38 +01:00
|
|
|
if (thread->GetRawState() == KThread::ThreadState_Runnable) {
|
2020-02-05 22:02:35 +01:00
|
|
|
GetPriorityQueue().ChangeAffinityMask(old_core, old_affinity, thread);
|
|
|
|
IncrementScheduledCount(thread);
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 04:16:09 +01:00
|
|
|
void KScheduler::RotateScheduledQueue(s32 core_id, s32 priority) {
|
|
|
|
MESOSPHERE_ASSERT(IsSchedulerLockedByCurrentThread());
|
|
|
|
|
|
|
|
/* Get a reference to the priority queue. */
|
|
|
|
auto &priority_queue = GetPriorityQueue();
|
|
|
|
|
|
|
|
/* Rotate the front of the queue to the end. */
|
|
|
|
KThread *top_thread = priority_queue.GetScheduledFront(core_id, priority);
|
|
|
|
KThread *next_thread = nullptr;
|
|
|
|
if (top_thread != nullptr) {
|
|
|
|
next_thread = priority_queue.MoveToScheduledBack(top_thread);
|
|
|
|
if (next_thread != top_thread) {
|
|
|
|
IncrementScheduledCount(top_thread);
|
|
|
|
IncrementScheduledCount(next_thread);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* While we have a suggested thread, try to migrate it! */
|
|
|
|
{
|
|
|
|
KThread *suggested = priority_queue.GetSuggestedFront(core_id, priority);
|
|
|
|
while (suggested != nullptr) {
|
|
|
|
/* Check if the suggested thread is the top thread on its core. */
|
|
|
|
const s32 suggested_core = suggested->GetActiveCore();
|
|
|
|
if (KThread *top_on_suggested_core = (suggested_core >= 0) ? priority_queue.GetScheduledFront(suggested_core) : nullptr; top_on_suggested_core != suggested) {
|
|
|
|
/* If the next thread is a new thread that has been waiting longer than our suggestion, we prefer it to our suggestion. */
|
|
|
|
if (top_thread != next_thread && next_thread != nullptr && next_thread->GetLastScheduledTick() < suggested->GetLastScheduledTick()) {
|
|
|
|
suggested = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're allowed to do a migration, do one. */
|
|
|
|
/* NOTE: Unlike migrations in UpdateHighestPriorityThread, this moves the suggestion to the front of the queue. */
|
|
|
|
if (top_on_suggested_core == nullptr || top_on_suggested_core->GetPriority() >= HighestCoreMigrationAllowedPriority) {
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(suggested_core, suggested, true);
|
|
|
|
IncrementScheduledCount(suggested);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next suggestion. */
|
|
|
|
suggested = priority_queue.GetSamePriorityNext(core_id, suggested);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now that we might have migrated a thread with the same priority, check if we can do better. */
|
|
|
|
{
|
|
|
|
KThread *best_thread = priority_queue.GetScheduledFront(core_id);
|
|
|
|
if (best_thread == GetCurrentThreadPointer()) {
|
|
|
|
best_thread = priority_queue.GetScheduledNext(core_id, best_thread);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the best thread we can choose has a priority the same or worse than ours, try to migrate a higher priority thread. */
|
|
|
|
if (best_thread != nullptr && best_thread->GetPriority() >= priority) {
|
|
|
|
KThread *suggested = priority_queue.GetSuggestedFront(core_id);
|
|
|
|
while (suggested != nullptr) {
|
|
|
|
/* If the suggestion's priority is the same as ours, don't bother. */
|
|
|
|
if (suggested->GetPriority() >= best_thread->GetPriority()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if the suggested thread is the top thread on its core. */
|
|
|
|
const s32 suggested_core = suggested->GetActiveCore();
|
|
|
|
if (KThread *top_on_suggested_core = (suggested_core >= 0) ? priority_queue.GetScheduledFront(suggested_core) : nullptr; top_on_suggested_core != suggested) {
|
|
|
|
/* If we're allowed to do a migration, do one. */
|
|
|
|
/* NOTE: Unlike migrations in UpdateHighestPriorityThread, this moves the suggestion to the front of the queue. */
|
|
|
|
if (top_on_suggested_core == nullptr || top_on_suggested_core->GetPriority() >= HighestCoreMigrationAllowedPriority) {
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(suggested_core, suggested, true);
|
|
|
|
IncrementScheduledCount(suggested);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next suggestion. */
|
2020-02-08 04:48:07 +01:00
|
|
|
suggested = priority_queue.GetSuggestedNext(core_id, suggested);
|
2020-02-08 04:16:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* After a rotation, we need a scheduler update. */
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
|
2020-07-11 08:30:15 +02:00
|
|
|
void KScheduler::YieldWithoutCoreMigration() {
|
2020-07-11 09:48:26 +02:00
|
|
|
/* Validate preconditions. */
|
|
|
|
MESOSPHERE_ASSERT(CanSchedule());
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentProcessPointer() != nullptr);
|
|
|
|
|
|
|
|
/* Get the current thread and process. */
|
|
|
|
KThread &cur_thread = GetCurrentThread();
|
|
|
|
KProcess &cur_process = GetCurrentProcess();
|
|
|
|
|
|
|
|
/* If the thread's yield count matches, there's nothing for us to do. */
|
|
|
|
if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the priority queue. */
|
|
|
|
auto &priority_queue = GetPriorityQueue();
|
|
|
|
|
|
|
|
/* Perform the yield. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
const auto cur_state = cur_thread.GetRawState();
|
|
|
|
if (cur_state == KThread::ThreadState_Runnable) {
|
|
|
|
/* Put the current thread at the back of the queue. */
|
|
|
|
KThread *next_thread = priority_queue.MoveToScheduledBack(std::addressof(cur_thread));
|
|
|
|
IncrementScheduledCount(std::addressof(cur_thread));
|
|
|
|
|
|
|
|
/* If the next thread is different, we have an update to perform. */
|
|
|
|
if (next_thread != std::addressof(cur_thread)) {
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
} else {
|
|
|
|
/* Otherwise, set the thread's yield count so that we won't waste work until the process is scheduled again. */
|
|
|
|
cur_thread.SetYieldScheduleCount(cur_process.GetScheduledCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 08:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::YieldWithCoreMigration() {
|
2020-07-11 09:48:26 +02:00
|
|
|
/* Validate preconditions. */
|
|
|
|
MESOSPHERE_ASSERT(CanSchedule());
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentProcessPointer() != nullptr);
|
|
|
|
|
|
|
|
/* Get the current thread and process. */
|
|
|
|
KThread &cur_thread = GetCurrentThread();
|
|
|
|
KProcess &cur_process = GetCurrentProcess();
|
|
|
|
|
|
|
|
/* If the thread's yield count matches, there's nothing for us to do. */
|
|
|
|
if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the priority queue. */
|
|
|
|
auto &priority_queue = GetPriorityQueue();
|
|
|
|
|
|
|
|
/* Perform the yield. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
const auto cur_state = cur_thread.GetRawState();
|
|
|
|
if (cur_state == KThread::ThreadState_Runnable) {
|
|
|
|
/* Get the current active core. */
|
|
|
|
const s32 core_id = cur_thread.GetActiveCore();
|
|
|
|
|
|
|
|
/* Put the current thread at the back of the queue. */
|
|
|
|
KThread *next_thread = priority_queue.MoveToScheduledBack(std::addressof(cur_thread));
|
|
|
|
IncrementScheduledCount(std::addressof(cur_thread));
|
|
|
|
|
|
|
|
/* While we have a suggested thread, try to migrate it! */
|
|
|
|
bool recheck = false;
|
|
|
|
KThread *suggested = priority_queue.GetSuggestedFront(core_id);
|
|
|
|
while (suggested != nullptr) {
|
|
|
|
/* Check if the suggested thread is the thread running on its core. */
|
|
|
|
const s32 suggested_core = suggested->GetActiveCore();
|
|
|
|
|
2020-12-18 02:18:47 +01:00
|
|
|
if (KThread *running_on_suggested_core = (suggested_core >= 0) ? Kernel::GetScheduler(suggested_core).m_state.highest_priority_thread : nullptr; running_on_suggested_core != suggested) {
|
2020-07-11 09:48:26 +02:00
|
|
|
/* If the current thread's priority is higher than our suggestion's we prefer the next thread to the suggestion. */
|
|
|
|
/* We also prefer the next thread when the current thread's priority is equal to the suggestions, but the next thread has been waiting longer. */
|
|
|
|
if ((suggested->GetPriority() > cur_thread.GetPriority()) ||
|
|
|
|
(suggested->GetPriority() == cur_thread.GetPriority() && next_thread != std::addressof(cur_thread) && next_thread->GetLastScheduledTick() < suggested->GetLastScheduledTick()))
|
|
|
|
{
|
|
|
|
suggested = nullptr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we're allowed to do a migration, do one. */
|
|
|
|
/* NOTE: Unlike migrations in UpdateHighestPriorityThread, this moves the suggestion to the front of the queue. */
|
|
|
|
if (running_on_suggested_core == nullptr || running_on_suggested_core->GetPriority() >= HighestCoreMigrationAllowedPriority) {
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(suggested_core, suggested, true);
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_CORE_MIGRATION(suggested->GetId(), suggested_core, core_id, 3);
|
2020-07-11 09:48:26 +02:00
|
|
|
IncrementScheduledCount(suggested);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
/* We couldn't perform a migration, but we should check again on a future yield. */
|
|
|
|
recheck = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next suggestion. */
|
|
|
|
suggested = priority_queue.GetSuggestedNext(core_id, suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* If we still have a suggestion or the next thread is different, we have an update to perform. */
|
|
|
|
if (suggested != nullptr || next_thread != std::addressof(cur_thread)) {
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
} else if (!recheck) {
|
|
|
|
/* Otherwise if we don't need to re-check, set the thread's yield count so that we won't waste work until the process is scheduled again. */
|
|
|
|
cur_thread.SetYieldScheduleCount(cur_process.GetScheduledCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 08:30:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void KScheduler::YieldToAnyThread() {
|
2020-07-11 09:48:26 +02:00
|
|
|
/* Validate preconditions. */
|
|
|
|
MESOSPHERE_ASSERT(CanSchedule());
|
|
|
|
MESOSPHERE_ASSERT(GetCurrentProcessPointer() != nullptr);
|
|
|
|
|
|
|
|
/* Get the current thread and process. */
|
|
|
|
KThread &cur_thread = GetCurrentThread();
|
|
|
|
KProcess &cur_process = GetCurrentProcess();
|
|
|
|
|
|
|
|
/* If the thread's yield count matches, there's nothing for us to do. */
|
|
|
|
if (cur_thread.GetYieldScheduleCount() == cur_process.GetScheduledCount()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a reference to the priority queue. */
|
|
|
|
auto &priority_queue = GetPriorityQueue();
|
|
|
|
|
|
|
|
/* Perform the yield. */
|
|
|
|
{
|
|
|
|
KScopedSchedulerLock sl;
|
|
|
|
|
|
|
|
const auto cur_state = cur_thread.GetRawState();
|
|
|
|
if (cur_state == KThread::ThreadState_Runnable) {
|
|
|
|
/* Get the current active core. */
|
|
|
|
const s32 core_id = cur_thread.GetActiveCore();
|
|
|
|
|
|
|
|
/* Migrate the current thread to core -1. */
|
|
|
|
cur_thread.SetActiveCore(-1);
|
|
|
|
priority_queue.ChangeCore(core_id, std::addressof(cur_thread));
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_CORE_MIGRATION(cur_thread.GetId(), core_id, -1, 4);
|
2020-07-11 09:48:26 +02:00
|
|
|
IncrementScheduledCount(std::addressof(cur_thread));
|
|
|
|
|
|
|
|
/* If there's nothing scheduled, we can try to perform a migration. */
|
|
|
|
if (priority_queue.GetScheduledFront(core_id) == nullptr) {
|
|
|
|
/* While we have a suggested thread, try to migrate it! */
|
|
|
|
KThread *suggested = priority_queue.GetSuggestedFront(core_id);
|
|
|
|
while (suggested != nullptr) {
|
|
|
|
/* Check if the suggested thread is the top thread on its core. */
|
|
|
|
const s32 suggested_core = suggested->GetActiveCore();
|
|
|
|
if (KThread *top_on_suggested_core = (suggested_core >= 0) ? priority_queue.GetScheduledFront(suggested_core) : nullptr; top_on_suggested_core != suggested) {
|
|
|
|
/* If we're allowed to do a migration, do one. */
|
|
|
|
if (top_on_suggested_core == nullptr || top_on_suggested_core->GetPriority() >= HighestCoreMigrationAllowedPriority) {
|
|
|
|
suggested->SetActiveCore(core_id);
|
|
|
|
priority_queue.ChangeCore(suggested_core, suggested);
|
2020-08-04 03:11:13 +02:00
|
|
|
MESOSPHERE_KTRACE_CORE_MIGRATION(suggested->GetId(), suggested_core, core_id, 5);
|
2020-07-11 09:48:26 +02:00
|
|
|
IncrementScheduledCount(suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Regardless of whether we migrated, we had a candidate, so we're done. */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the next suggestion. */
|
|
|
|
suggested = priority_queue.GetSuggestedNext(core_id, suggested);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If the suggestion is different from the current thread, we need to perform an update. */
|
|
|
|
if (suggested != std::addressof(cur_thread)) {
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
} else {
|
|
|
|
/* Otherwise, set the thread's yield count so that we won't waste work until the process is scheduled again. */
|
|
|
|
cur_thread.SetYieldScheduleCount(cur_process.GetScheduledCount());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Otherwise, we have an update to perform. */
|
|
|
|
SetSchedulerUpdateNeeded();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-11 08:30:15 +02:00
|
|
|
}
|
2020-02-05 22:02:35 +01:00
|
|
|
|
2019-12-13 10:21:43 +01:00
|
|
|
}
|
2021-11-16 20:25:57 +01:00
|
|
|
|
|
|
|
#pragma GCC pop_options
|