Compare commits

..

2 Commits

Author SHA1 Message Date
1f03d23dae Kernel: Fix load balancing
My code to find least loaded processor used processor index instead of
processor id to index the array. Most of the time this lead to wrong
processor returned as the least loaded, leaving some processors
basically idle.
2026-01-10 01:46:08 +02:00
2eea074473 Kernel: Remove unnecessary page table loads
loading a page table is slow as it invalidates the whole tlb
2026-01-10 01:13:48 +02:00

View File

@@ -223,7 +223,8 @@ namespace Kernel
{ {
case Thread::State::Terminated: case Thread::State::Terminated:
remove_node_from_most_loaded(m_current); remove_node_from_most_loaded(m_current);
PageTable::kernel().load(); if (&PageTable::current() != &PageTable::kernel())
PageTable::kernel().load();
delete m_current->thread; delete m_current->thread;
delete m_current; delete m_current;
m_thread_count--; m_thread_count--;
@@ -255,7 +256,8 @@ namespace Kernel
if (m_current->thread->state() != Thread::State::Terminated) if (m_current->thread->state() != Thread::State::Terminated)
break; break;
remove_node_from_most_loaded(m_current); remove_node_from_most_loaded(m_current);
PageTable::kernel().load(); if (&PageTable::current() != &PageTable::kernel())
PageTable::kernel().load();
delete m_current->thread; delete m_current->thread;
delete m_current; delete m_current;
m_thread_count--; m_thread_count--;
@@ -263,7 +265,8 @@ namespace Kernel
if (m_current == nullptr) if (m_current == nullptr)
{ {
PageTable::kernel().load(); if (&PageTable::current() != &PageTable::kernel())
PageTable::kernel().load();
*interrupt_stack = m_idle_thread->interrupt_stack(); *interrupt_stack = m_idle_thread->interrupt_stack();
*interrupt_registers = m_idle_thread->interrupt_registers(); *interrupt_registers = m_idle_thread->interrupt_registers();
m_idle_thread->m_state = Thread::State::Executing; m_idle_thread->m_state = Thread::State::Executing;
@@ -276,7 +279,8 @@ namespace Kernel
auto* thread = m_current->thread; auto* thread = m_current->thread;
auto& page_table = thread->has_process() ? thread->process().page_table() : PageTable::kernel(); auto& page_table = thread->has_process() ? thread->process().page_table() : PageTable::kernel();
page_table.load(); if (&PageTable::current() != &page_table)
page_table.load();
if (thread->state() == Thread::State::NotStarted) if (thread->state() == Thread::State::NotStarted)
{ {
@@ -407,10 +411,10 @@ namespace Kernel
uint32_t least_max_load_threads = static_cast<uint32_t>(-1); uint32_t least_max_load_threads = static_cast<uint32_t>(-1);
for (uint8_t i = 0; i < Processor::count(); i++) for (uint8_t i = 0; i < Processor::count(); i++)
{ {
auto processor_id = Processor::id_from_index(i); const auto processor_id = Processor::id_from_index(i);
if (processor_id == Processor::current_id()) if (processor_id == Processor::current_id())
continue; continue;
const auto& info = s_processor_infos[i]; const auto& info = s_processor_infos[processor_id.as_u32()];
if (info.idle_time_ns < most_idle_ns || info.max_load_threads > least_max_load_threads) if (info.idle_time_ns < most_idle_ns || info.max_load_threads > least_max_load_threads)
continue; continue;
least_loaded_id = processor_id; least_loaded_id = processor_id;