summaryrefslogtreecommitdiff
path: root/src/sys/os/os_sched.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2025-09-17 03:10:28 -0400
committerIan Moffett <ian@osmora.org>2025-09-17 03:12:58 -0400
commit61aaead7e2904a2756ed72a71e36eeeb21591733 (patch)
tree6fc83ac548c715c1f5782ea31e4253fc5f6e2303 /src/sys/os/os_sched.c
parentdf9eb8790b8e49c63beaef93c3194f1b0ed48f4d (diff)
kern/amd64: cpu: Add process load balancing
This commit introduces load balancing of processes between cores by using a scalable "CPU arbiter" which decides how to fetch the next core descriptor. Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'src/sys/os/os_sched.c')
-rw-r--r--src/sys/os/os_sched.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/sys/os/os_sched.c b/src/sys/os/os_sched.c
index f1bcc8c..e7cc832 100644
--- a/src/sys/os/os_sched.c
+++ b/src/sys/os/os_sched.c
@@ -33,6 +33,7 @@
*/
#include <sys/types.h>
+#include <sys/cdefs.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <sys/panic.h>
@@ -40,6 +41,41 @@
#include <sys/cpuvar.h>
#include <os/sched.h>
+__cacheline_aligned
+static struct core_arbiter arbiter = {
+ .rr_id = 0,
+ .type = CORE_ARBITER_RR
+};
+
+/*
+ * Schedule the next processor core
+ */
+struct pcore *
+cpu_sched(void)
+{
+ struct pcore *retval;
+
+ spinlock_acquire(&arbiter.lock);
+ switch (arbiter.type) {
+ case CORE_ARBITER_RR:
+ retval = cpu_get(arbiter.rr_id++);
+
+ /*
+ * If we made it at the end, wrap to the beginning.
+ * XXX: Us getting entry 0 would make the next be 1.
+ */
+ if (retval == NULL) {
+ arbiter.rr_id = 1;
+ retval = cpu_get(0);
+ }
+
+ break;
+ }
+
+ spinlock_release(&arbiter.lock);
+ return retval;
+}
+
/*
* Enqueue a process into a queue
*/