Repository navigation
fix(sched): do not dequeue a lower-priority task on the PrioritizedRR tick (#729) #809
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,13 +5,14 @@ use core::sync::atomic::Ordering; | |
| use core::time::Duration; | ||
|
|
||
| use crate::{ | ||
| task::{get_current_task, get_scheduler_type_by_task_id, Task}, | ||
| task::{get_current_task, get_scheduler_type_by_task_id, State, Task, NUM_TASK_IN_QUEUE}, | ||
| time::Instant, | ||
| }; | ||
| use alloc::collections::{binary_heap::BinaryHeap, btree_map::BTreeMap}; | ||
| use alloc::sync::Arc; | ||
| use awkernel_lib::{ | ||
| cpu::{num_cpu, CpuSet}, | ||
| priority_queue::PriorityQueue, | ||
| sync::mutex::{MCSNode, Mutex}, | ||
| verified::delta_list::DeltaList, | ||
| }; | ||
|
|
@@ -202,6 +203,17 @@ pub(crate) trait Scheduler { | |
| /// Get the next executable task. | ||
| fn get_next(&self, execution_ensured: bool) -> Option<Arc<Task>>; | ||
|
|
||
| /// Dequeue the task that `get_next(false)` would return, but only if | ||
| /// `pred` accepts it. A rejected task stays at the head of the run queue. | ||
| /// | ||
| /// The check and the dequeue must be done without releasing the lock of | ||
| /// the run queue, so that the dequeued task is the one `pred` accepted. | ||
| /// | ||
| /// This is used only by `get_next_task_if()`, which runs on the primary | ||
| /// CPU (CPU 0). A scheduler whose tasks must never be taken that way | ||
| /// returns [`ConditionalNext::Empty`]. | ||
| fn get_next_if(&self, pred: &dyn Fn(&Task) -> bool) -> ConditionalNext; | ||
|
|
||
| /// Get the scheduler name. | ||
| fn scheduler_name(&self) -> SchedulerType; | ||
|
|
||
|
|
@@ -258,6 +270,87 @@ pub(crate) fn get_next_task(execution_ensured: bool) -> Option<Arc<Task>> { | |
| .find_map(|scheduler_type| get_scheduler(scheduler_type).get_next(execution_ensured)) | ||
| } | ||
|
|
||
| /// The result of [`Scheduler::get_next_if`]. | ||
| pub(crate) enum ConditionalNext { | ||
| /// The run queue has no executable task. | ||
| Empty, | ||
|
|
||
| /// The head of the run queue was rejected and is left in the queue. | ||
| Rejected, | ||
|
|
||
| /// The head of the run queue was accepted and dequeued. | ||
| Taken(Arc<Task>), | ||
| } | ||
|
|
||
| /// Dequeue the next executable task only if `pred` accepts it. | ||
| /// | ||
| /// This examines the task that `get_next_task(false)` would return, except | ||
| /// that panicked tasks are never taken. If `pred` returns `false` for it, the | ||
| /// run queues are left untouched, so the task keeps its position in the | ||
| /// queue, and `None` is returned. | ||
| /// | ||
| /// `pred` is called with `GLOBAL_WAKE_GET_MUTEX`, the lock of the run queue, | ||
| /// and the lock of the task information held. | ||
| /// | ||
| /// This function must be called on the primary CPU, which can never dequeue | ||
| /// a clustered task (see `get_next_task`), so only the non-clustered | ||
| /// schedulers are examined. | ||
| pub(crate) fn get_next_task_if(pred: impl Fn(&Task) -> bool) -> Option<Arc<Task>> { | ||
| let mut node = MCSNode::new(); | ||
| let _guard = GLOBAL_WAKE_GET_MUTEX.lock(&mut node); | ||
|
|
||
| for scheduler_type in &PRIORITY_LIST[get_num_clustered_schedulers()..] { | ||
| match get_scheduler(scheduler_type).get_next_if(&pred) { | ||
| ConditionalNext::Empty => (), | ||
| ConditionalNext::Rejected => return None, | ||
| ConditionalNext::Taken(task) => return Some(task), | ||
| } | ||
| } | ||
|
|
||
| None | ||
| } | ||
|
|
||
| /// The body of [`Scheduler::get_next_if`] for the schedulers whose run queue | ||
| /// is a [`PriorityQueue`]. `task_of` returns the task of a queue entry. | ||
| /// | ||
| /// Finished tasks at the head of the queue are discarded as `get_next()` | ||
| /// does. The lock of the run queue must be held by the caller. | ||
| fn take_head_if<T>( | ||
| queue: &mut PriorityQueue<T>, | ||
| task_of: impl Fn(&T) -> &Arc<Task>, | ||
| pred: &dyn Fn(&Task) -> bool, | ||
| ) -> ConditionalNext { | ||
| loop { | ||
| let Some(task) = queue.peek().map(|entry| task_of(entry).clone()) else { | ||
| return ConditionalNext::Empty; | ||
| }; | ||
|
|
||
| let mut node = MCSNode::new(); | ||
| let mut task_info = task.info.lock(&mut node); | ||
|
|
||
| if matches!(task_info.state, State::Terminated | State::Panicked) { | ||
| drop(task_info); | ||
| queue.pop(); | ||
| NUM_TASK_IN_QUEUE.fetch_sub(1, Ordering::Relaxed); | ||
| continue; | ||
| } | ||
|
|
||
| if !pred(&task) { | ||
| return ConditionalNext::Rejected; | ||
| } | ||
|
|
||
| queue.pop(); | ||
| NUM_TASK_IN_QUEUE.fetch_sub(1, Ordering::Relaxed); | ||
|
|
||
| if task_info.state == State::Preempted { | ||
| task_info.need_preemption = false; | ||
| } | ||
|
|
||
| drop(task_info); | ||
| return ConditionalNext::Taken(task); | ||
| } | ||
| } | ||
|
|
||
| /// Get a scheduler. | ||
| /// | ||
| /// Takes `&SchedulerType` so the caller does not copy the full payload (the | ||
|
|
@@ -427,20 +520,37 @@ pub(crate) fn sleep_until_task(sleep_handler: Box<dyn FnOnce() + Send>, next: In | |
| /// If there are no sleeping tasks, this function returns `None`. | ||
| pub fn wake_task() -> Option<Duration> { | ||
| // Check whether each running task exceeds the time quantum. | ||
| let mut quantum_wait: Option<Duration> = None; | ||
|
|
||
| if PREEMPTION_ENABLED { | ||
| for cpu_id in 1..num_cpu() { | ||
| if let Some(task_id) = get_current_task(cpu_id) { | ||
| if let Some(SchedulerType::PrioritizedRR(_)) = | ||
| get_scheduler_type_by_task_id(task_id) | ||
| { | ||
| prioritized_rr::SCHEDULER.invoke_preemption_tick(cpu_id, task_id) | ||
| let wait = prioritized_rr::SCHEDULER.invoke_preemption_tick(cpu_id, task_id); | ||
| quantum_wait = match (quantum_wait, wait) { | ||
| (Some(a), Some(b)) => Some(a.min(b)), | ||
| (a, b) => a.or(b), | ||
| }; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // A queued task may be waiting for a time quantum to expire. Nothing | ||
| // else wakes the primary CPU at that time, so do not sleep beyond it. | ||
| // If no task is queued, the next enqueue wakes the primary CPU. | ||
| if NUM_TASK_IN_QUEUE.load(Ordering::Relaxed) == 0 { | ||
|
Contributor
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. This check runs before |
||
| quantum_wait = None; | ||
| } | ||
|
|
||
| let mut node = MCSNode::new(); | ||
| let mut guard = SLEEPING.lock(&mut node); | ||
| guard.wake_task(); | ||
| guard.time_to_wait() | ||
|
|
||
| match (guard.time_to_wait(), quantum_wait) { | ||
| (Some(a), Some(b)) => Some(a.min(b)), | ||
| (a, b) => a.or(b), | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,21 +2,22 @@ | |
|
|
||
| use core::cmp::max; | ||
|
|
||
| use super::{Scheduler, SchedulerType, Task}; | ||
| use super::{take_head_if, ConditionalNext, Scheduler, SchedulerType, Task}; | ||
| use crate::{ | ||
| scheduler::{ | ||
| get_next_task, get_priority, peek_preemption_pending, push_preemption_pending, | ||
| get_next_task_if, get_priority, peek_preemption_pending, push_preemption_pending, | ||
| GLOBAL_WAKE_GET_MUTEX, PREEMPTION_ENABLED, | ||
| }, | ||
| task::{ | ||
| get_last_executed_by_task_id, get_task, get_tasks_running, set_current_task, | ||
| set_need_preemption, State, NUM_TASK_IN_QUEUE, | ||
| get_task, get_tasks_running, set_current_task, set_need_preemption, State, | ||
|
Contributor
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. This removes the only use of |
||
| NUM_TASK_IN_QUEUE, | ||
| }, | ||
| }; | ||
| use alloc::{sync::Arc, vec::Vec}; | ||
| use awkernel_lib::priority_queue::PriorityQueue; | ||
| use awkernel_lib::sync::mutex::{MCSNode, Mutex}; | ||
| use core::sync::atomic::Ordering; | ||
| use core::time::Duration; | ||
|
|
||
| pub struct PrioritizedRRScheduler { | ||
| // Time quantum | ||
|
|
@@ -111,6 +112,16 @@ impl Scheduler for PrioritizedRRScheduler { | |
| None | ||
| } | ||
|
|
||
| fn get_next_if(&self, pred: &dyn Fn(&Task) -> bool) -> ConditionalNext { | ||
| let mut node = MCSNode::new(); | ||
| let mut data = self.data.lock(&mut node); | ||
|
|
||
| match (*data).as_mut() { | ||
| Some(data) => take_head_if(&mut data.queue, |entry| &entry.task, pred), | ||
| None => ConditionalNext::Empty, | ||
| } | ||
| } | ||
|
|
||
| fn scheduler_name(&self) -> SchedulerType { | ||
| SchedulerType::PrioritizedRR(0) | ||
| } | ||
|
|
@@ -128,19 +139,68 @@ pub static SCHEDULER: PrioritizedRRScheduler = PrioritizedRRScheduler { | |
| }; | ||
|
|
||
| impl PrioritizedRRScheduler { | ||
| // Invoke a preemption if the task exceeds the time quantum | ||
| pub fn invoke_preemption_tick(&self, cpu_id: usize, task_id: u32) { | ||
| if let Some(last_executed) = get_last_executed_by_task_id(task_id) { | ||
| let elapsed = last_executed.elapsed().as_micros() as u64; | ||
| if elapsed > self.interval { | ||
| if let Some(next_task) = get_next_task(false) { | ||
| push_preemption_pending(cpu_id, next_task); | ||
| let preempt_irq = awkernel_lib::interrupt::get_preempt_irq(); | ||
| set_need_preemption(task_id, cpu_id); | ||
| awkernel_lib::interrupt::send_ipi(preempt_irq, cpu_id as u32); | ||
| } | ||
| } | ||
| /// Rotate the PrioritizedRR task `task_id` running on `cpu_id` if it has | ||
| /// used up its time quantum. | ||
| /// | ||
| /// The next runnable task is dequeued and sent to `cpu_id` only if its | ||
| /// priority is at least that of the running task: an equal priority is | ||
| /// the quantum rotation, and a higher priority is a plain preemption. | ||
| /// A lower-priority task could not preempt the running task anyway, so | ||
| /// it is left in the run queue to keep its position, and the quantum of | ||
| /// the running task is restarted instead. | ||
| /// | ||
| /// Returns the time after which this function must be called again for | ||
| /// this CPU, or `None` if a preemption was requested: the preempted task | ||
| /// is enqueued again, and that wakes the primary CPU. | ||
| /// | ||
| /// This function must be called on the primary CPU. | ||
| pub fn invoke_preemption_tick(&self, cpu_id: usize, task_id: u32) -> Option<Duration> { | ||
| let current_task = get_task(task_id)?; | ||
| let interval = Duration::from_micros(self.interval); | ||
|
|
||
| let elapsed = { | ||
| let mut node = MCSNode::new(); | ||
| let info = current_task.info.lock(&mut node); | ||
| info.get_last_executed().elapsed() | ||
| }; | ||
|
|
||
| if elapsed <= interval { | ||
| return Some(interval - elapsed); | ||
| } | ||
|
|
||
| // A task is dequeued only while no preemption is pending on this | ||
| // CPU: `do_preemption` runs one pending task and re-queues the others | ||
| // at the tail of their level. The predicate runs under | ||
| // `GLOBAL_WAKE_GET_MUTEX`, so no wake path can push a pending task | ||
| // between this check and the dequeue. | ||
| let next_task = get_next_task_if(|next| { | ||
| peek_preemption_pending(cpu_id).is_none() && !current_task.preempts(next) | ||
| }); | ||
|
|
||
| if let Some(next_task) = next_task { | ||
| push_preemption_pending(cpu_id, next_task); | ||
|
Contributor
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.
|
||
| let preempt_irq = awkernel_lib::interrupt::get_preempt_irq(); | ||
| set_need_preemption(task_id, cpu_id); | ||
| awkernel_lib::interrupt::send_ipi(preempt_irq, cpu_id as u32); | ||
| return None; | ||
| } | ||
|
|
||
| if peek_preemption_pending(cpu_id).is_some() { | ||
| // A preemption has already been requested on this CPU, but it | ||
| // has not been performed yet. Request it again in case the IPI | ||
| // was not handled. | ||
| let preempt_irq = awkernel_lib::interrupt::get_preempt_irq(); | ||
| set_need_preemption(task_id, cpu_id); | ||
| awkernel_lib::interrupt::send_ipi(preempt_irq, cpu_id as u32); | ||
| } else { | ||
| // There is no task to rotate with. Restart the quantum so that | ||
| // the run queues are not examined again on every tick. | ||
| let mut node = MCSNode::new(); | ||
| let mut info = current_task.info.lock(&mut node); | ||
| info.update_last_executed(); | ||
| } | ||
|
|
||
| Some(interval) | ||
| } | ||
|
|
||
| fn invoke_preemption_wake(&self, task: Arc<Task>) -> bool { | ||
|
|
||
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.
After this change, only
run_maincallsget_next_task, and it always passestrue, so theexecution_ensured == falsebranches of the four schedulers are dead code. This doc, the doc ofget_next_task_if, and the comment inedf.rsthat the new EDFget_next_ifpoints to still describe aget_next_task(false)path on the primary CPU. Rewording them, or removing theexecution_ensuredparameter, keeps them accurate.