-
Notifications
You must be signed in to change notification settings - Fork 23
Single-Hart O(1) Enhancement #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
vicLin8712
wants to merge
3
commits into
sysprog21:main
Choose a base branch
from
vicLin8712:o1-sched
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,18 @@ | |
static int32_t noop_rtsched(void); | ||
void _timer_tick_handler(void); | ||
|
||
/* hart 0 scheduler */ | ||
static sched_t hart0_sched = { | ||
.ready_bitmap = 0, | ||
.ready_queues = {NULL}, | ||
.rr_cursors = {NULL}, | ||
.quantum_cycles = {0}, | ||
.last_selected_prio = 0, | ||
.local_switches = 0, | ||
.current_task = NULL, | ||
.hart_id = 0, | ||
}; | ||
|
||
/* Kernel-wide control block (KCB) */ | ||
static kcb_t kernel_state = { | ||
.tasks = NULL, | ||
|
@@ -25,6 +37,7 @@ static kcb_t kernel_state = { | |
.task_count = 0, | ||
.ticks = 0, | ||
.preemptive = true, /* Default to preemptive mode */ | ||
.harts = &hart0_sched, | ||
}; | ||
kcb_t *kcb = &kernel_state; | ||
|
||
|
@@ -345,7 +358,14 @@ static void sched_enqueue_task(tcb_t *task) | |
task->time_slice = get_priority_timeslice(task->prio_level); | ||
task->state = TASK_READY; | ||
|
||
/* Task selection is handled directly through the master task list */ | ||
/* Push task into corresponding ready queue and setup bitmap */ | ||
CRITICAL_ENTER(); | ||
if (!kcb->harts->ready_queues[task->prio_level]) | ||
kcb->harts->ready_queues[task->prio_level] = list_create(); | ||
|
||
list_pushback(kcb->harts->ready_queues[task->prio_level], task); | ||
bitmap_set(task->prio_level); | ||
CRITICAL_LEAVE(); | ||
} | ||
|
||
/* Remove task from ready queues - state-based approach for compatibility */ | ||
|
@@ -355,9 +375,20 @@ void sched_dequeue_task(tcb_t *task) | |
return; | ||
|
||
/* For tasks that need to be removed from ready state (suspended/cancelled), | ||
* we rely on the state change. The scheduler will skip non-ready tasks | ||
* when it encounters them during the round-robin traversal. | ||
* we rely on the state change. The scheduler will remove it from | ||
* corresponding priority ready queue and setup bitmap by checking remaining | ||
* task count in this queue. | ||
* | ||
* The state of task will be modified by `mo_task_suspended` or | ||
* `mo_task_cancel`. | ||
*/ | ||
CRITICAL_ENTER(); | ||
list_node_t *node = find_task_node_by_id(task->id); | ||
list_remove(kcb->harts->ready_queues[task->prio_level], node); | ||
|
||
if (!kcb->harts->ready_queues[task->prio_level]->length) | ||
bitmap_clean(task->prio_level); | ||
CRITICAL_LEAVE(); | ||
} | ||
|
||
/* Handle time slice expiration for current task */ | ||
|
@@ -418,33 +449,29 @@ uint16_t sched_select_next_task(void) | |
|
||
/* Mark current task as ready if it was running */ | ||
if (current_task->state == TASK_RUNNING) | ||
current_task->state = TASK_READY; | ||
sched_enqueue_task(current_task); | ||
|
||
/* Round-robin search: find next ready task in the master task list */ | ||
list_node_t *start_node = kcb->task_current; | ||
list_node_t *node = start_node; | ||
int iterations = 0; /* Safety counter to prevent infinite loops */ | ||
|
||
do { | ||
/* Move to next task (circular) */ | ||
node = list_cnext(kcb->tasks, node); | ||
if (!node || !node->data) | ||
continue; | ||
|
||
tcb_t *task = node->data; | ||
/* Find highest priority task queue */ | ||
uint32_t bitmap = kcb->harts->ready_bitmap; | ||
int highest_prio_level = 0; | ||
for (; !(bitmap & 1U); highest_prio_level++, bitmap >>= 1) | ||
; | ||
|
||
/* Skip non-ready tasks */ | ||
if (task->state != TASK_READY) | ||
continue; | ||
|
||
/* Found a ready task */ | ||
kcb->task_current = node; | ||
task->state = TASK_RUNNING; | ||
task->time_slice = get_priority_timeslice(task->prio_level); | ||
/* Pop out from corresponding queue and mark it as TASK_RUNNING */ | ||
list_node_t *node = | ||
kcb->harts->ready_queues[highest_prio_level]->head->next; | ||
list_pop(kcb->harts->ready_queues[highest_prio_level]); | ||
((tcb_t *) node->data)->state = TASK_RUNNING; | ||
kcb->task_current = node; | ||
|
||
return task->id; | ||
/* Check popped queue is empty or not */ | ||
if (kcb->harts->ready_queues[highest_prio_level]->length == 0) | ||
bitmap_clean(highest_prio_level); | ||
|
||
} while (node != start_node && ++iterations < SCHED_IMAX); | ||
if (node) | ||
return ((tcb_t *) node->data)->id; | ||
|
||
/* No ready tasks found - this should not happen in normal operation */ | ||
panic(ERR_NO_TASKS); | ||
|
@@ -603,6 +630,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req) | |
} | ||
} | ||
|
||
|
||
list_node_t *node = list_pushback(kcb->tasks, tcb); | ||
if (!node) { | ||
CRITICAL_LEAVE(); | ||
|
@@ -615,8 +643,12 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req) | |
tcb->id = kcb->next_tid++; | ||
kcb->task_count++; /* Cached count of active tasks for quick access */ | ||
|
||
if (!kcb->task_current) | ||
/* If tcb is the first task, turn it into TASK_RUNNING state and does not | ||
* put into ready queue */ | ||
if (!kcb->task_current) { | ||
kcb->task_current = node; | ||
tcb->state = TASK_RUNNING; | ||
} | ||
|
||
CRITICAL_LEAVE(); | ||
|
||
|
@@ -630,8 +662,10 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req) | |
|
||
/* Add to cache and mark ready */ | ||
cache_task(tcb->id, tcb); | ||
sched_enqueue_task(tcb); | ||
|
||
/* Active task from TASK_STOPPED state */ | ||
if (tcb->state == TASK_STOPPED) | ||
sched_enqueue_task(tcb); | ||
return tcb->id; | ||
} | ||
|
||
|
@@ -721,7 +755,6 @@ int32_t mo_task_suspend(uint16_t id) | |
CRITICAL_LEAVE(); | ||
return ERR_TASK_CANT_SUSPEND; | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid adding or removing blank lines unnecessarily. |
||
task->state = TASK_SUSPENDED; | ||
bool is_current = (kcb->task_current == node); | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, the comment says this is an 8-bit bitmap, but it's declared as uint32_t?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there be more priority level extensions in the future? If not, I'll modify this declaration.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if it's necessary in the future, we can easily come back and modify this.