Fix CPU load balancer bugs, scheduling points support for remote CPUs
All checks were successful
Build documentation / build-and-deploy (push) Successful in 28s

This commit is contained in:
2026-02-05 23:44:32 +01:00
parent 5283787a80
commit 5fe9d0a158
19 changed files with 129 additions and 79 deletions

View File

@@ -15,22 +15,19 @@
#include <sys/smp.h>
#include <sys/syscall.h>
/// Cpu ID counter
static atomic_uint cpu_counter = 0;
/// The CPUs
static struct cpu cpus[CPUS_MAX];
static atomic_int cpu_init_count;
static atomic_int last_cpu_index = 0;
static atomic_int cpu_counter;
/// Allocate a CPU structure
struct cpu* cpu_make (uint64_t lapic_id) {
int id = atomic_fetch_add (&cpu_counter, 1);
struct cpu* cpu = &cpus[id];
struct cpu* cpu_make (uint64_t lapic_id, uint64_t cpu_id) {
struct cpu* cpu = &cpus[cpu_id];
memset (cpu, 0, sizeof (*cpu));
cpu->lock = SPIN_LOCK_INIT;
cpu->id = id;
cpu->id = cpu_id;
cpu->lapic_id = lapic_id;
amd64_wrmsr (MSR_GS_BASE, (uint64_t)cpu);
@@ -53,27 +50,30 @@ void cpu_request_sched (struct cpu* cpu) {
}
struct cpu* cpu_find_lightest (void) {
struct cpu* cpu = &cpus[0];
struct limine_mp_response* mp = limine_mp_request.response;
int load = atomic_load (&cpu->proc_run_q_count);
int start = atomic_fetch_add (&last_cpu_index, 1) % mp->cpu_count;
struct cpu* best_cpu = &cpus[start];
int best_load = atomic_load (&best_cpu->proc_run_q_count);
for (unsigned int i = 1; i < cpu_counter; i++) {
struct cpu* new_cpu = &cpus[i];
int new_load = atomic_load (&new_cpu->proc_run_q_count);
if (new_load < load) {
load = new_load;
cpu = new_cpu;
for (int i = 1; i < (int)mp->cpu_count; i++) {
int idx = (start + i) % mp->cpu_count;
struct cpu* cpu = &cpus[idx];
int l = atomic_load (&cpu->proc_run_q_count);
if (l < best_load) {
best_load = l;
best_cpu = cpu;
}
}
return cpu;
return best_cpu;
}
/// Bootstrap code for non-BSP CPUs
static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
amd64_load_kernel_cr3 ();
struct cpu* cpu = cpu_make (mp_info->lapic_id);
struct cpu* cpu = cpu_make (mp_info->lapic_id, mp_info->processor_id);
amd64_init (cpu, true); /* gdt + idt */
syscall_init ();
@@ -82,10 +82,11 @@ static void amd64_smp_bootstrap (struct limine_mp_info* mp_info) {
DEBUG ("CPU %u is online!\n", thiscpu->id);
atomic_fetch_sub (&cpu_init_count, 1);
atomic_fetch_sub (&cpu_counter, 1);
struct proc* spin_proc = proc_spawn_rd ("spin.exe");
proc_register (spin_proc, thiscpu);
struct cpu* spin_cpu = thiscpu;
proc_register (spin_proc, &spin_cpu);
spin_lock_ctx_t ctxcpu;
spin_lock (&spin_proc->cpu->lock, &ctxcpu);
@@ -98,7 +99,7 @@ void smp_init (void) {
struct limine_mp_response* mp = limine_mp_request.response;
cpu_init_count = mp->cpu_count - 1; /* Don't include BSP */
cpu_counter = mp->cpu_count - 1;
for (size_t i = 0; i < mp->cpu_count; i++) {
if (mp->cpus[i]->lapic_id != thiscpu->lapic_id) {
@@ -106,8 +107,6 @@ void smp_init (void) {
}
}
while (atomic_load (&cpu_init_count) > 0)
while (atomic_load (&cpu_counter) > 0)
;
DEBUG ("All CPUs are online\n");
}