From 46a7d5a65f62cc44c46e0215d578fcf221028dde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rom=C3=A1n=20C=C3=A1rdenas=20Rodr=C3=ADguez?= Date: Thu, 19 Nov 2026 12:02:27 +0900 Subject: [PATCH] feat(edf): prepare for alternative deadline computations --- awkernel_async_lib/src/scheduler/edf.rs | 130 +++++++++++++++++++----- mdbook/src/internal/scheduler.md | 4 +- 2 files changed, 109 insertions(+), 25 deletions(-) diff --git a/awkernel_async_lib/src/scheduler/edf.rs b/awkernel_async_lib/src/scheduler/edf.rs index d7811a52d..369404849 100644 --- a/awkernel_async_lib/src/scheduler/edf.rs +++ b/awkernel_async_lib/src/scheduler/edf.rs @@ -5,6 +5,15 @@ //! Runnable tasks are kept in a single affinity-aware priority queue //! ([`AffinityBTreeQueue`]); `get_next` on CPU `c` pops the earliest-deadline //! task whose `cpu_set` contains `c`. +//! +//! # Deadline Policies +//! +//! The EDF scheduler relies on a deadline policy to determine the absolute deadline of each task. +//! This allows different policies to be plugged in while keeping the queue and preemption logic consistent. +//! +//! The [`DeadlinePolicy`] trait supplies the deadline; the queue and preemption logic are shared by all policies. +//! The default deadline policy is [`DefaultDeadlinePolicy`], which uses the DAG's +//! calculated deadline if available, or falls back to the relative deadline otherwise. use super::{Scheduler, SchedulerType, Task}; use crate::{ @@ -14,7 +23,8 @@ use crate::{ GLOBAL_WAKE_GET_MUTEX, PREEMPTION_ENABLED, }, task::{ - get_task, get_task_running, set_current_task, set_need_preemption, State, MAX_TASK_PRIORITY, + get_task, get_task_running, set_current_task, set_need_preemption, DagInfo, State, + MAX_TASK_PRIORITY, }, }; use affinity_btree_queue::{AffinityBTreeQueue, DEFAULT_MIN_DEGREE}; @@ -23,6 +33,7 @@ use awkernel_lib::{ cpu::{masked_workers, num_cpu, CpuSet, CPU_SET_WORDS}, sync::mutex::{MCSNode, Mutex}, }; +use core::marker::PhantomData; /// The run queue. Priority is `(absolute_deadline, wake_time)`; smaller values /// dequeue first, and entries with fully equal keys are FIFO-ordered by the @@ -30,35 +41,60 @@ use awkernel_lib::{ type EDFQueue = AffinityBTreeQueue<(u64, u64), ClusteredTask>, DEFAULT_MIN_DEGREE, CPU_SET_WORDS>; -pub struct EDFScheduler { +pub(super) trait DeadlinePolicy { + /// Return an absolute deadline in uptime microseconds for the job. + fn absolute_deadline( + wake_time: u64, + relative_deadline: u64, + dag_info: Option<&DagInfo>, + state: State, + ) -> u64; +} + +pub(super) struct DefaultDeadlinePolicy; + +impl DeadlinePolicy for DefaultDeadlinePolicy { + fn absolute_deadline( + wake_time: u64, + relative_deadline: u64, + dag_info: Option<&DagInfo>, + state: State, + ) -> u64 { + match dag_info { + Some(dag_info) => calculate_and_update_dag_deadline(dag_info, wake_time, state), + None => wake_time + relative_deadline, + } + } +} + +pub(super) struct EDFScheduler { // `AffinityBTreeQueue::new` is not a const fn, so the queue is lazily // initialized with `num_cpu()` on first use. data: Mutex>, priority: u8, + policy: PhantomData P>, } -impl Scheduler for EDFScheduler { +impl Scheduler for EDFScheduler

{ fn wake_task(&self, task: Arc) { - let (wake_time, absolute_deadline) = { + let key = { let mut node_inner = MCSNode::new(); let mut info = task.info.lock(&mut node_inner); - let dag_info = info.get_dag_info(); match info.scheduler_type { SchedulerType::EDF(relative_deadline, _) => { let wake_time = awkernel_lib::delay::uptime(); - let absolute_deadline = if let Some(ref dag_info) = dag_info { - calculate_and_update_dag_deadline(dag_info, wake_time, info.state) - } else { - // If dag_info is not present, the task is treated as a regular task, and - // the absolute_deadline is calculated using the scheduler's relative_deadline. - wake_time + relative_deadline - }; - + let key = Self::queue_key( + wake_time, + relative_deadline, + info.dag_info.as_ref(), + info.state, + ); + let (absolute_deadline, _) = key; task.priority .update_priority_info(self.priority, MAX_TASK_PRIORITY - absolute_deadline); info.update_absolute_deadline(absolute_deadline); - (wake_time, absolute_deadline) + key } _ => unreachable!(), } @@ -80,11 +116,7 @@ impl Scheduler for EDFScheduler { let mut data = self.data.lock(&mut node_inner); let queue = data.get_or_insert_with(|| EDFQueue::new(num_cpu())); queue - .push( - (absolute_deadline, wake_time), - cpu_set, - ClusteredTask::new(task), - ) + .push(key, cpu_set, ClusteredTask::new(task)) .expect("EDF: failed to push a task"); drop(data); @@ -155,12 +187,29 @@ impl Scheduler for EDFScheduler { } } -pub static SCHEDULER: EDFScheduler = EDFScheduler { - data: Mutex::new(None), - priority: get_priority(&SchedulerType::EDF(0, CpuSet::empty())), -}; +pub(super) static SCHEDULER: EDFScheduler = EDFScheduler::new(); + +impl EDFScheduler

{ + pub(super) const fn new() -> Self { + Self { + data: Mutex::new(None), + priority: get_priority(&SchedulerType::EDF(0, CpuSet::empty())), + policy: PhantomData, + } + } + + fn queue_key( + wake_time: u64, + relative_deadline: u64, + dag_info: Option<&DagInfo>, + state: State, + ) -> (u64, u64) { + ( + P::absolute_deadline(wake_time, relative_deadline, dag_info, state), + wake_time, + ) + } -impl EDFScheduler { fn invoke_preemption(&self, task: Arc) -> bool { if !PREEMPTION_ENABLED { return false; @@ -242,3 +291,36 @@ impl EDFScheduler { false } } + +#[cfg(test)] +mod tests { + use super::{DagInfo, DeadlinePolicy, DefaultDeadlinePolicy, EDFScheduler, State}; + + struct FixedDeadline; + + impl DeadlinePolicy for FixedDeadline { + fn absolute_deadline( + _wake_time: u64, + _relative_deadline: u64, + _dag_info: Option<&DagInfo>, + _state: State, + ) -> u64 { + 200 + } + } + + #[test] + fn policy_can_change_task_order_without_changing_the_tie_breaker() { + let default_first = + EDFScheduler::::queue_key(100, 40, None, State::Waiting); + let default_second = + EDFScheduler::::queue_key(110, 10, None, State::Waiting); + let fixed_first = EDFScheduler::::queue_key(100, 40, None, State::Waiting); + let fixed_second = EDFScheduler::::queue_key(110, 10, None, State::Waiting); + + assert_eq!(default_first, (140, 100)); + assert_eq!(default_second, (120, 110)); + assert_eq!(fixed_first, (200, 100)); + assert_eq!(fixed_second, (200, 110)); + } +} diff --git a/mdbook/src/internal/scheduler.md b/mdbook/src/internal/scheduler.md index 9ce66e97d..a3956024a 100644 --- a/mdbook/src/internal/scheduler.md +++ b/mdbook/src/internal/scheduler.md @@ -75,7 +75,9 @@ The Earliest Deadline First (EDF) scheduler is implemented in [edf.rs](https://g The scheduler holds a single affinity-aware priority queue, [`AffinityBTreeQueue`](https://crates.io/crates/affinity_btree_queue), backed by an augmented B-tree. Each entry carries `(priority, affinity, task)`, where the priority is the pair `(absolute_deadline, wake_time)`; smaller values dequeue first, so tasks are ordered by earliest deadline with wake time as a tie-breaker. Every B-tree node stores the OR of the affinities in its subtree, which lets `pop_for_cpu(cpu)` find the earliest-deadline task runnable on `cpu` in logarithmic time while skipping subtrees with no eligible entry. -When a task is enqueued via `wake_task()`, the scheduler reads the `SchedulerType::EDF(relative_deadline, cpu_set)` attached to the task and calculates the absolute deadline as `uptime + relative_deadline`. If the task is part of a DAG, `calculate_and_update_dag_deadline()` is used instead to propagate deadlines through the DAG. The task priority (`PriorityInfo`) becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. `invoke_preemption()` handles preemption. If the task is already running, the scheduler does not preempt. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. If the new task has a higher priority than the task running (or pending preemption) on that core, the scheduler sends an IPI to it. In that case, the scheduler pins the new task to the preemption-pending heap of that core and does not push it into the queue. If the scheduler sends no IPI, it pushes the task into the queue with its `cpu_set` as the affinity mask. One such case is a set with no busy core. Against another EDF task, a higher priority means an earlier deadline. An EDF task preempts a task of a lower-priority scheduler whatever its deadline. +`EDFScheduler

` uses an internal `DeadlinePolicy` to compute the absolute deadline once per wake. The policy receives the wake time, configured relative deadline, optional DAG metadata, and task state. `SchedulerType::EDF(relative_deadline, cpu_set)` continues to select the default policy: it uses `uptime + relative_deadline` for standalone tasks and delegates DAG tasks to `calculate_and_update_dag_deadline()`. + +The scheduler uses that same deadline for the queue key and for `PriorityInfo`, which becomes `MAX_TASK_PRIORITY - absolute_deadline`, so an earlier deadline gives a higher priority. `invoke_preemption()` handles preemption. If the task is already running, the scheduler does not preempt. Otherwise, the scheduler chooses the core running the lowest-priority task among the busy cores of the set. If the new task has a higher priority than the task running (or pending preemption) on that core, the scheduler sends an IPI to it. In that case, the scheduler pins the new task to the preemption-pending heap of that core and does not push it into the queue. If the scheduler sends no IPI, it pushes the task into the queue with its `cpu_set` as the affinity mask. One such case is a set with no busy core. Against another EDF task, a higher priority means an earlier deadline. An EDF task preempts a task of a lower-priority scheduler whatever its deadline. An idle core in the set does not stop the preemption. A task queued before or after the new task can take the idle core first. Also, a sleeping core runs a queued task only after the primary CPU wakes it. The cost is that the preemption can be unneeded, and the preempted task waits for the primary CPU to wake a sleeping core. If the set of the preempted task holds no idle core, the preempted task waits for its own core while the idle core stays idle.