forked from Bananymous/banan-os
Kernel: Rewrite the whole scheduler and re-architecture SMP handling
Change Semaphore -> ThreadBlocker
This was not a semaphore, I just named it one because I didn't know
what semaphore was. I have meant to change this sooner, but it was in
no way urgent :D
Implement SMP events. Processors can now be sent SMP events through
IPIs. SMP events can be sent either to a single processor or broadcasted
to every processor.
PageTable::{map_page,map_range,unmap_page,unmap_range}() now send SMP
event to invalidate TLB caches for the changed pages.
Scheduler no longer uses a global run queue. Each processor has its own
scheduler that keeps track of the load on the processor. Once every
second schedulers do load balancing. Schedulers have no access to other
processors' schedulers, they just see approximate loads. If scheduler
decides that it has too much load, it will send a thread to another
processor through a SMP event.
Schedulers are currently run using the timer interrupt on BSB. This
should be not the case, and each processor should use its LAPIC timer
for interrupts. There is no reason to broadcast SMP event to all
processors when BSB gets timer interrupt.
Old scheduler only achieved 20% idle load on qemu. That was probably a
very inefficient implementation. This new scheduler seems to average
around 1% idle load. This is much closer to what I would expect. On my
own laptop idle load seems to be only around 0.5% on each processor.
This commit is contained in:
@@ -8,7 +8,7 @@
|
||||
namespace Kernel
|
||||
{
|
||||
|
||||
static constexpr uint64_t s_ata_timeout = 1000;
|
||||
static constexpr uint64_t s_ata_timeout_ms = 1000;
|
||||
|
||||
static void start_cmd(volatile HBAPortMemorySpace* port)
|
||||
{
|
||||
@@ -118,9 +118,9 @@ namespace Kernel
|
||||
command.c = 1;
|
||||
command.command = ATA_COMMAND_IDENTIFY;
|
||||
|
||||
uint64_t timeout = SystemTimer::get().ms_since_boot() + s_ata_timeout;
|
||||
const uint64_t timeout_ms = SystemTimer::get().ms_since_boot() + s_ata_timeout_ms;
|
||||
while (m_port->tfd & (ATA_STATUS_BSY | ATA_STATUS_DRQ))
|
||||
if (SystemTimer::get().ms_since_boot() >= timeout)
|
||||
if (SystemTimer::get().ms_since_boot() >= timeout_ms)
|
||||
return BAN::Error::from_errno(ETIMEDOUT);
|
||||
|
||||
m_port->ci = 1 << slot.value();
|
||||
@@ -158,17 +158,17 @@ namespace Kernel
|
||||
{
|
||||
static constexpr uint64_t poll_timeout_ms = 10;
|
||||
|
||||
auto start_time = SystemTimer::get().ms_since_boot();
|
||||
const auto start_time_ms = SystemTimer::get().ms_since_boot();
|
||||
|
||||
while (SystemTimer::get().ms_since_boot() < start_time + poll_timeout_ms)
|
||||
while (SystemTimer::get().ms_since_boot() < start_time_ms + poll_timeout_ms)
|
||||
if (!(m_port->ci & (1 << command_slot)))
|
||||
return {};
|
||||
|
||||
// FIXME: This should actually block once semaphores support blocking with timeout.
|
||||
// FIXME: This should actually block once ThreadBlocker support blocking with timeout.
|
||||
// This doesn't allow scheduler to go properly idle.
|
||||
while (SystemTimer::get().ms_since_boot() < start_time + s_ata_timeout)
|
||||
while (SystemTimer::get().ms_since_boot() < start_time_ms + s_ata_timeout_ms)
|
||||
{
|
||||
Scheduler::get().yield();
|
||||
Processor::yield();
|
||||
if (!(m_port->ci & (1 << command_slot)))
|
||||
return {};
|
||||
}
|
||||
|
||||
@@ -67,9 +67,7 @@ namespace Kernel
|
||||
|
||||
static void select_delay()
|
||||
{
|
||||
auto time = SystemTimer::get().ns_since_boot();
|
||||
while (SystemTimer::get().ns_since_boot() < time + 400)
|
||||
continue;
|
||||
SystemTimer::get().sleep_ns(400);
|
||||
}
|
||||
|
||||
void ATABus::select_device(bool secondary)
|
||||
@@ -106,7 +104,7 @@ namespace Kernel
|
||||
io_write(ATA_PORT_CONTROL, ATA_CONTROL_nIEN);
|
||||
|
||||
io_write(ATA_PORT_COMMAND, ATA_COMMAND_IDENTIFY);
|
||||
SystemTimer::get().sleep(1);
|
||||
SystemTimer::get().sleep_ms(1);
|
||||
|
||||
// No device on port
|
||||
if (io_read(ATA_PORT_STATUS) == 0)
|
||||
@@ -130,7 +128,7 @@ namespace Kernel
|
||||
}
|
||||
|
||||
io_write(ATA_PORT_COMMAND, ATA_COMMAND_IDENTIFY_PACKET);
|
||||
SystemTimer::get().sleep(1);
|
||||
SystemTimer::get().sleep_ms(1);
|
||||
|
||||
if (auto res = wait(true); res.is_error())
|
||||
{
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <kernel/Lock/LockGuard.h>
|
||||
#include <kernel/Scheduler.h>
|
||||
#include <kernel/Storage/NVMe/Queue.h>
|
||||
#include <kernel/Thread.h>
|
||||
#include <kernel/Timer/Timer.h>
|
||||
|
||||
namespace Kernel
|
||||
@@ -44,7 +44,7 @@ namespace Kernel
|
||||
|
||||
m_doorbell.cq_head = m_cq_head;
|
||||
|
||||
m_semaphore.unblock();
|
||||
m_thread_blocker.unblock();
|
||||
}
|
||||
|
||||
uint16_t NVMeQueue::submit_command(NVMe::SubmissionQueueEntry& sqe)
|
||||
@@ -66,15 +66,15 @@ namespace Kernel
|
||||
m_doorbell.sq_tail = m_sq_tail;
|
||||
}
|
||||
|
||||
const uint64_t start_time = SystemTimer::get().ms_since_boot();
|
||||
while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time + s_nvme_command_poll_timeout_ms)
|
||||
const uint64_t start_time_ms = SystemTimer::get().ms_since_boot();
|
||||
while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time_ms + s_nvme_command_poll_timeout_ms)
|
||||
continue;
|
||||
|
||||
// FIXME: Here is a possible race condition if done mask is set before
|
||||
// scheduler has put the current thread blocking.
|
||||
// EINTR should also be handled here.
|
||||
while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time + s_nvme_command_timeout_ms)
|
||||
Scheduler::get().block_current_thread(&m_semaphore, start_time + s_nvme_command_timeout_ms);
|
||||
while (!(m_done_mask & cid_mask) && SystemTimer::get().ms_since_boot() < start_time_ms + s_nvme_command_timeout_ms)
|
||||
m_thread_blocker.block_with_wake_time_ms(start_time_ms + s_nvme_command_timeout_ms);
|
||||
|
||||
if (m_done_mask & cid_mask)
|
||||
{
|
||||
@@ -93,7 +93,7 @@ namespace Kernel
|
||||
while (~m_used_mask == 0)
|
||||
{
|
||||
m_lock.unlock(state);
|
||||
m_semaphore.block_with_timeout(s_nvme_command_timeout_ms);
|
||||
m_thread_blocker.block_with_timeout_ms(s_nvme_command_timeout_ms);
|
||||
state = m_lock.lock();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user