scheduler: continue rework

This commit is contained in:
Matheus
2025-08-20 22:00:25 -03:00
parent 5098c09f26
commit 136c86354c
26 changed files with 348 additions and 154 deletions
+4 -4
View File
@@ -1,8 +1,8 @@
#include <kernel/scheduler.h>
#include <kernel/timekeeper.h>
#include <util.h>
#define MAX_INTERACTIVITY 100
#define SCALING_FACTOR (MAX_INTERACTIVITY / 2)
#define SCALING_FACTOR (SCHED_MAX_INTERACTIVITY / 2)
#define HISTORY_LIMIT_USEC 5000000
@@ -11,9 +11,9 @@ static void update_interactivity(thread_t *thread) {
time_t run_time = thread->metrics.run_time_avg_us;
if (sleep_time > run_time)
thread->metrics.interactivity_score = min((SCALING_FACTOR * run_time) / sleep_time, MAX_INTERACTIVITY);
thread->metrics.interactivity_score = min((SCALING_FACTOR * run_time) / sleep_time, SCHED_MAX_INTERACTIVITY);
else
thread->metrics.interactivity_score = min((SCALING_FACTOR * sleep_time) / run_time + SCALING_FACTOR, MAX_INTERACTIVITY);
thread->metrics.interactivity_score = min((SCALING_FACTOR * sleep_time) / run_time + SCALING_FACTOR, SCHED_MAX_INTERACTIVITY);
// TODO nice value impacting this
}
+148
View File
@@ -0,0 +1,148 @@
#include <kernel/scheduler.h>
#include <arch/cpu.h>
#include <logging.h>
#define INTERACTIVITY_LIMIT 30
// set = idle
bitmap_t sched_idle_cpu_bitmap;
spinlock_t sched_idle_cpu_bitmap_lock;
static cpu_t *pick_cpu(thread_t *thread) {
if (thread->cputarget)
return thread->cputarget;
return current_cpu();
}
static void insert_in_run_queue(sched_run_queue_t *run_queue, thread_t *thread) {
int idx = min(thread->metrics.interactivity_score * SCHED_RUN_QUEUE_SIZE / SCHED_MAX_INTERACTIVITY, SCHED_RUN_QUEUE_SIZE - 1);
thread->prev = run_queue->queues[idx].ins;
if (thread->prev)
thread->prev->next = thread;
else
run_queue->queues[idx].run = thread;
thread->next = NULL;
run_queue->queues[idx].ins = thread;
bitmap_set(&run_queue->thread_bitmap, idx, 1);
}
static thread_t *pop_from_run_queue(sched_run_queue_t *run_queue) {
long idx = bitmap_find_first_set(&run_queue->thread_bitmap, 0);
if (idx == -1)
return NULL;
thread_t *thread = run_queue->queues[idx].run;
run_queue->queues[idx].run = thread->next;
if (thread->next) {
thread->next->prev = NULL;
} else {
run_queue->queues[idx].ins = NULL;
bitmap_set(&run_queue->thread_bitmap, idx, 0);
}
return thread;
}
static void insert_in_calendar_queue(sched_calendar_queue_t *calendar_queue, thread_t *thread) {
int idx = (calendar_queue->ins + thread->metrics.interactivity_score - INTERACTIVITY_LIMIT) % SCHED_RUN_QUEUE_SIZE;
thread->next = calendar_queue->queues[idx];
calendar_queue->queues[idx] = thread;
++calendar_queue->thread_count;
}
static thread_t *pop_from_calendar_queue(sched_calendar_queue_t *calendar_queue) {
if (calendar_queue->thread_count == 0)
return NULL;
thread_t *thread;
do {
thread = calendar_queue->queues[calendar_queue->run];
if (thread)
calendar_queue->queues[calendar_queue->run] = thread->next;
calendar_queue->run = (calendar_queue->run + 1) % SCHED_RUN_QUEUE_SIZE;
if (calendar_queue->run == calendar_queue->ins)
calendar_queue->ins = (calendar_queue->ins + 1) % SCHED_RUN_QUEUE_SIZE;
} while (thread == NULL);
--calendar_queue->thread_count;
return thread;
}
// requires cpu queue to be locked
thread_t *sched_select_next_thread(void) {
thread_t *thread = pop_from_run_queue(&current_cpu()->rt_queue);
if (thread)
goto found;
thread = pop_from_calendar_queue(&current_cpu()->ts_queue);
if (thread)
goto found;
thread = pop_from_run_queue(&current_cpu()->idle_queue);
if (thread == NULL)
return NULL;
found:
thread->flags &= ~THREAD_FLAGS_QUEUED;
return thread;
}
// requires cpu queue to be locked
void sched_insert_in_cpu_queue(cpu_t *cpu, thread_t *thread) {
__assert((thread->flags & THREAD_FLAGS_RUNNING) == 0);
thread->flags |= THREAD_FLAGS_QUEUED;
switch (thread->class) {
case THREAD_CLASS_TIMESHARE:
if (thread->metrics.interactivity_score > INTERACTIVITY_LIMIT) {
// thread is not interactive
insert_in_calendar_queue(&cpu->ts_queue, thread);
break;
}
case THREAD_CLASS_REAL_TIME:
insert_in_run_queue(&cpu->rt_queue, thread);
break;
case THREAD_CLASS_IDLE:
insert_in_run_queue(&cpu->idle_queue, thread);
break;
}
spinlock_acquire(&sched_idle_cpu_bitmap_lock);
bitmap_set(&sched_idle_cpu_bitmap, cpu->internal_id, 0);
spinlock_release(&sched_idle_cpu_bitmap_lock);
}
void sched_queue(thread_t *thread) {
bool status = interrupt_set(false);
cpu_t *cpu = pick_cpu(thread);
spinlock_acquire(&cpu->sched_lock);
__assert((thread->flags & THREAD_FLAGS_QUEUED) == 0 && (thread->flags & THREAD_FLAGS_RUNNING) == 0);
sched_insert_in_cpu_queue(cpu, thread);
spinlock_release_irq_restore(&cpu->sched_lock, status);
}
// ran every 10 ms per core
void sched_calendar_tick(context_t *, dpcarg_t) {
spinlock_acquire(&current_cpu()->sched_lock);
current_cpu()->ts_queue.ins = (current_cpu()->ts_queue.ins + 1) % SCHED_RUN_QUEUE_SIZE;
spinlock_release(&current_cpu()->sched_lock);
}
+57 -138
View File
@@ -18,65 +18,6 @@
#define QUANTUM_US 100000
#define SCHEDULER_STACK_SIZE PAGE_SIZE * 16
#define RUNQUEUE_COUNT 64
typedef struct {
thread_t *list;
thread_t *last;
} rqueue_t;
static rqueue_t runqueue[RUNQUEUE_COUNT];
static uint64_t runqueuebitmap;
static spinlock_t runqueuelock;
static thread_t *getinrunqueue(rqueue_t *rq) {
thread_t *thread = rq->list;
while (thread) {
if (thread->cputarget == NULL || thread->cputarget == current_cpu())
break;
thread = thread->next;
}
if (thread) {
if (thread->prev)
thread->prev->next = thread->next;
else
rq->list = thread->next;
if (thread->next)
thread->next->prev = thread->prev;
else
rq->last = thread->prev;
}
return thread;
}
static thread_t *runqueuenext(int minprio) {
bool intstate = interrupt_set(false);
thread_t *thread = NULL;
if (runqueuebitmap == 0)
goto leave;
// TODO use the bitmap for this
for (int i = 0; i < RUNQUEUE_COUNT && i <= minprio && thread == NULL; ++i) {
thread = getinrunqueue(&runqueue[i]);
if (thread && runqueue[i].list == NULL)
runqueuebitmap &= ~((uint64_t)1 << i);
}
if (thread)
thread->flags &= ~THREAD_FLAGS_QUEUED;
leave:
interrupt_set(intstate);
return thread;
}
static __attribute__((noreturn)) void switch_thread(thread_t *thread) {
interrupt_set(false);
thread_t* current = current_thread();
@@ -90,8 +31,6 @@ static __attribute__((noreturn)) void switch_thread(thread_t *thread) {
current_cpu()->intstatus = ARCH_CONTEXT_INTSTATUS(&thread->context);
thread->cpu = current_cpu();
// XXX make the locking of the flags field better, candidate for a r/w lock
spinlock_acquire(&runqueuelock);
if (current)
current->flags &= ~THREAD_FLAGS_RUNNING;
@@ -102,7 +41,6 @@ static __attribute__((noreturn)) void switch_thread(thread_t *thread) {
thread->flags |= THREAD_FLAGS_RUNNING;
__assert((thread->flags & THREAD_FLAGS_QUEUED) == 0);
spinlock_release(&runqueuelock);
void *schedulerstack = current_cpu()->schedulerstack;
__assert(!((void *)thread->context.rsp < schedulerstack && (void *)thread->context.rsp >= (schedulerstack - SCHEDULER_STACK_SIZE)));
@@ -111,49 +49,18 @@ static __attribute__((noreturn)) void switch_thread(thread_t *thread) {
__builtin_unreachable();
}
static void runqueueinsert(thread_t *thread) {
__assert((thread->flags & THREAD_FLAGS_RUNNING) == 0);
thread->flags |= THREAD_FLAGS_QUEUED;
runqueuebitmap |= ((uint64_t)1 << thread->priority);
thread->prev = runqueue[thread->priority].last;
if (thread->prev)
thread->prev->next = thread;
else
runqueue[thread->priority].list = thread;
thread->next = NULL;
runqueue[thread->priority].last = thread;
}
void sched_queue(thread_t *thread) {
bool intstate = interrupt_set(false);
spinlock_acquire(&runqueuelock);
// maybe instead of an assert, a simple return would suffice as the thread would already be queued anyways
__assert((thread->flags & THREAD_FLAGS_QUEUED) == 0 && (thread->flags & THREAD_FLAGS_RUNNING) == 0);
runqueueinsert(thread);
spinlock_release(&runqueuelock);
interrupt_set(intstate);
// TODO yield if higher priority than current thread (or send another CPU an IPI)
}
// no metrics callback here as this will be called from things like threads stopping
__attribute__((noreturn)) void sched_stop_current_thread() {
interrupt_set(false);
spinlock_acquire(&runqueuelock);
if (current_thread())
current_thread()->flags &= ~THREAD_FLAGS_RUNNING;
thread_t *next = runqueuenext(0x0fffffff);
if (next == NULL)
next = current_cpu()->idlethread;
spinlock_acquire(&current_cpu()->sched_lock);
thread_t *next = sched_select_next_thread();
spinlock_release(&current_cpu()->sched_lock);
spinlock_release(&runqueuelock);
__assert(next);
switch_thread(next);
}
@@ -222,11 +129,8 @@ __attribute__((no_caller_saved_registers)) void sched_userspacecheck(context_t *
static void yield(context_t *context, void *) {
thread_t *thread = current_thread();
spinlock_acquire(&runqueuelock);
bool sleeping = thread->flags & THREAD_FLAGS_SLEEP;
thread_t *next = runqueuenext(sleeping ? 0x0fffffff : thread->priority);
bool gotsignal = false;
for (int i = 1; i < NSIG && thread->proc; ++i) {
void *action = thread->proc->signals.actions[i].address;
@@ -245,32 +149,31 @@ static void yield(context_t *context, void *) {
if (sleeping && (thread->shouldexit || gotsignal) && (thread->flags & THREAD_FLAGS_INTERRUPTIBLE)) {
sleeping = false;
if (next)
runqueueinsert(next);
next = NULL;
thread->flags &= ~(THREAD_FLAGS_SLEEP | THREAD_FLAGS_INTERRUPTIBLE);
thread->wakeupreason = SCHED_WAKEUP_REASON_INTERRUPTED;
spinlock_release(&thread->sleeplock);
}
if (next || sleeping) {
spinlock_acquire(&current_cpu()->sched_lock);
thread->flags &= ~THREAD_FLAGS_RUNNING;
if (sleeping == false) {
sched_insert_in_cpu_queue(current_cpu(), thread);
}
thread_t *next = sched_select_next_thread();
thread->flags |= THREAD_FLAGS_RUNNING;
spinlock_release(&current_cpu()->sched_lock);
if (next != thread || sleeping) {
ARCH_CONTEXT_THREADSAVE(thread, context);
thread->flags &= ~THREAD_FLAGS_RUNNING;
if (sleeping == false)
runqueueinsert(thread);
if (next == NULL)
next = current_cpu()->idlethread;
spinlock_release(&runqueuelock);
switch_thread(next);
}
spinlock_release(&runqueuelock);
sched_thread_running_callback(current_thread());
}
@@ -319,20 +222,16 @@ bool sched_wakeup(thread_t *thread, int reason) {
// once a scheduler dpc gets run, the return context is set to this function using the scheduler stack
static void dopreempt() {
// interrupts are disabled, the thread context is already saved
spinlock_acquire(&runqueuelock);
thread_t *current = current_thread();
thread_t *next = runqueuenext(current->priority);
current->flags &= ~THREAD_FLAGS_PREEMPTED;
if (next) {
current->flags &= ~THREAD_FLAGS_RUNNING;
runqueueinsert(current);
} else {
next = current;
}
spinlock_release(&runqueuelock);
spinlock_acquire(&current_cpu()->sched_lock);
current->flags &= ~THREAD_FLAGS_RUNNING;
sched_insert_in_cpu_queue(current_cpu(), current);
thread_t *next = sched_select_next_thread();
spinlock_release(&current_cpu()->sched_lock);
switch_thread(next);
}
@@ -385,21 +284,18 @@ static void reschedule_yield(context_t *context, void *_cpu) {
thread_t *thread = current_thread();
cpu_t *cpu = _cpu;
spinlock_acquire(&runqueuelock);
thread_t *next = runqueuenext(0x0fffffff);
ARCH_CONTEXT_THREADSAVE(thread, context);
spinlock_acquire(&cpu->sched_lock);
thread->flags &= ~THREAD_FLAGS_RUNNING;
runqueueinsert(thread);
sched_insert_in_cpu_queue(cpu, thread);
spinlock_release(&cpu->sched_lock);
if (next == NULL)
next = current_cpu()->idlethread;
spinlock_acquire(&current_cpu()->sched_lock);
thread_t *next = sched_select_next_thread();
spinlock_release(&current_cpu()->sched_lock);
arch_smp_sendipi(cpu, cpu->reschedule_isr, ARCH_SMP_IPI_TARGET, false);
spinlock_release(&runqueuelock);
switch_thread(next);
}
@@ -442,18 +338,32 @@ void sched_sleep_us(size_t us) {
sched_yield();
}
static void set_up_bitmaps(void) {
__assert(bitmap_init(&current_cpu()->rt_queue.thread_bitmap, SCHED_RUN_QUEUE_SIZE) == 0);
__assert(bitmap_init(&current_cpu()->idle_queue.thread_bitmap, SCHED_RUN_QUEUE_SIZE) == 0);
}
void sched_calendar_tick(context_t *, dpcarg_t);
void sched_ap_entry() {
SPINLOCK_INIT(current_cpu()->sched_lock);
set_up_bitmaps();
current_cpu()->schedulerstack = vmm_map(NULL, SCHEDULER_STACK_SIZE, VMM_FLAGS_ALLOCATE, ARCH_MMU_FLAGS_READ | ARCH_MMU_FLAGS_WRITE | ARCH_MMU_FLAGS_NOEXEC, NULL);
__assert(current_cpu()->schedulerstack);
current_cpu()->schedulerstack = (void *)((uintptr_t)current_cpu()->schedulerstack + SCHEDULER_STACK_SIZE);
current_cpu()->idlethread = sched_newthread(cpuidlethread, PAGE_SIZE * 4, 3, NULL, NULL);
__assert(current_cpu()->idlethread);
current_cpu()->idlethread->class = THREAD_CLASS_IDLE;
sched_queue(current_cpu()->idlethread);
current_cpu()->reschedule_isr = interrupt_allocate(reschedule_ipi, ARCH_EOI, IPL_MAX);
__assert(current_cpu()->reschedule_isr);
timer_insert(current_cpu()->timer, &current_cpu()->schedtimerentry, reschedule_timer_dpc, NULL, QUANTUM_US, true);
timer_insert(current_cpu()->timer, &current_cpu()->calendar_tick_timer_entry, sched_calendar_tick, NULL, 1000, true);
timer_resume(current_cpu()->timer);
sched_stop_current_thread();
}
@@ -461,17 +371,25 @@ void sched_ap_entry() {
void sched_init() {
proc_init();
SPINLOCK_INIT(current_cpu()->sched_lock);
SPINLOCK_INIT(sched_idle_cpu_bitmap_lock);
set_up_bitmaps();
__assert(bitmap_init(&sched_idle_cpu_bitmap, arch_smp_get_cpu_count()) == 0);
current_cpu()->schedulerstack = vmm_map(NULL, SCHEDULER_STACK_SIZE, VMM_FLAGS_ALLOCATE, ARCH_MMU_FLAGS_READ | ARCH_MMU_FLAGS_WRITE | ARCH_MMU_FLAGS_NOEXEC, NULL);
__assert(current_cpu()->schedulerstack);
current_cpu()->schedulerstack = (void *)((uintptr_t)current_cpu()->schedulerstack + SCHEDULER_STACK_SIZE);
SPINLOCK_INIT(runqueuelock);
current_cpu()->idlethread = sched_newthread(cpuidlethread, PAGE_SIZE * 4, 3, NULL, NULL);
__assert(current_cpu()->idlethread);
current_cpu()->idlethread->class = THREAD_CLASS_IDLE;
current_cpu()->thread = sched_newthread(NULL, PAGE_SIZE * 32, 0, NULL, NULL);
__assert(current_thread());
sched_queue(current_cpu()->idlethread);
current_cpu()->reschedule_isr = interrupt_allocate(reschedule_ipi, ARCH_EOI, IPL_MAX);
__assert(current_cpu()->reschedule_isr);
@@ -480,6 +398,7 @@ void sched_init() {
sched_thread_running_callback(current_thread());
timer_insert(current_cpu()->timer, &current_cpu()->schedtimerentry, reschedule_timer_dpc, NULL, QUANTUM_US, true);
timer_insert(current_cpu()->timer, &current_cpu()->calendar_tick_timer_entry, sched_calendar_tick, NULL, 10000, true);
// XXX move this resume to a more appropriate place
timer_resume(current_cpu()->timer);
}