Skip to content

Commit

Permalink
main: yield CPU time after threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
jewelcodes committed Jan 17, 2025
1 parent 3465192 commit 9cfa35f
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,17 @@
#include <kernel/servers.h>
#include <platform/platform.h>

static int idleThreshold = 0;

void *idleThread(void *args) {
int count = 0;
for(;;) {
if(!syscallProcess()) platformIdle();
count++;
if(count >= idleThreshold) {
count = 0;
platformIdle();
}
}
}

Expand Down Expand Up @@ -67,9 +75,15 @@ void *kernelThread(void *args) {
setLocalSched(true);
setScheduling(true);

int count = 0;
for(;;) {
serverIdle();
if(!syscallProcess()) platformIdle();
count++;
if(count >= idleThreshold) {
count = 0;
platformIdle();
}
}
}

Expand All @@ -84,10 +98,17 @@ int main(int argc, char **argv) {
socketInit(); // sockets
schedInit(); // scheduler

if(platformCountCPU() > 16)
idleThreshold = 2;
else if(platformCountCPU() > 8)
idleThreshold = 4;
else
idleThreshold = 8;

// number of kernel threads = number of CPU cores + 1
kthreadCreate(&kernelThread, NULL);

for(int i = 1; i < platformCountCPU(); i++)
for(int i = 0; i < platformCountCPU(); i++)
kthreadCreate(&idleThread, NULL);

// now enable the scheduler
Expand Down

0 comments on commit 9cfa35f

Please sign in to comment.