scheduler: implement nice

This commit is contained in:
Matheus
2025-08-21 20:46:06 -03:00
parent 3cf396393a
commit dbebef0e03
12 changed files with 89 additions and 32 deletions
+7
View File
@@ -264,6 +264,13 @@ static int process(cred_t *cred, int actions, void *arg0, void *arg1, void *arg2
DONE_CHECK(actions);
}
if (actions & AUTH_ACTIONS_PROCESS_NICE) {
if (CRED_IS_ESU(cred))
weight += 1;
DONE_CHECK(actions);
}
done:
return weight ? AUTH_DECISION_ALLOW : AUTH_DECISION_DEFER;
}
+4 -3
View File
@@ -11,11 +11,12 @@ 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, SCHED_MAX_INTERACTIVITY);
thread->metrics.interactivity_score = (SCALING_FACTOR * run_time) / sleep_time;
else
thread->metrics.interactivity_score = min((SCALING_FACTOR * sleep_time) / run_time + SCALING_FACTOR, SCHED_MAX_INTERACTIVITY);
thread->metrics.interactivity_score = (SCALING_FACTOR * sleep_time) / run_time + SCALING_FACTOR;
// TODO nice value impacting this
thread->metrics.interactivity_score += thread->nice + (thread->proc ? thread->proc->nice : 0);
thread->metrics.interactivity_score = min(thread->metrics.interactivity_score, SCHED_MAX_INTERACTIVITY);
}
static void offset_times(thread_t *thread, time_t run_us, time_t sleep_us) {
+2 -7
View File
@@ -310,15 +310,10 @@ void sched_reschedule_on_cpu(cpu_t *cpu, bool target) {
if (cpu == current_cpu())
goto leave;
long old_priority = current_thread()->priority;
current_thread()->priority = 0;
sched_thread_stopping_callback(current_thread(), false);
arch_context_saveandcall(reschedule_yield, current_cpu()->schedulerstack, cpu);
current_thread()->priority = old_priority;
leave:
interrupt_set(status);
if (target == false)
@@ -354,7 +349,7 @@ void sched_ap_entry() {
__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);
current_cpu()->idlethread = sched_newthread(cpuidlethread, PAGE_SIZE * 4, 100, NULL, NULL);
__assert(current_cpu()->idlethread);
current_cpu()->idlethread->class = THREAD_CLASS_IDLE;
sched_queue(current_cpu()->idlethread);
@@ -382,7 +377,7 @@ void sched_init() {
__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);
current_cpu()->idlethread = sched_newthread(cpuidlethread, PAGE_SIZE * 4, 100, 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);
+17
View File
@@ -0,0 +1,17 @@
#include <kernel/syscalls.h>
#include <kernel/thread.h>
#include <arch/cpu.h>
syscallret_t syscall_nice(context_t *, int nice) {
syscallret_t ret;
if (nice < 0) {
}
current_thread()->proc->nice = max(min(current_thread()->proc->nice + nice, SCHED_NICE_MAX), SCHED_NICE_MIN);
ret.ret = current_thread()->proc->nice;
return ret;
}
+2 -2
View File
@@ -5,7 +5,7 @@
static scache_t *thread_cache;
thread_t *sched_newthread(void *ip, size_t kstacksize, int priority, proc_t *proc, void *ustack) {
thread_t *sched_newthread(void *ip, size_t kstacksize, int nice, proc_t *proc, void *ustack) {
if (thread_cache == NULL) {
thread_cache = slab_newcache(sizeof(thread_t), 0, NULL, NULL);
__assert(thread_cache);
@@ -28,8 +28,8 @@ thread_t *sched_newthread(void *ip, size_t kstacksize, int priority, proc_t *pro
// non kernel thread vmm contexts are handled by the caller
thread->vmmctx = proc ? NULL : &vmm_kernelctx;
thread->proc = proc;
thread->priority = priority;
thread->kernelstacksize = kstacksize;
thread->nice = nice;
if (proc) {
// each thread holds one reference to proc
PROC_HOLD(proc);