Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions awkernel_async_lib/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,8 @@ const fn get_num_clustered_schedulers() -> usize {
count
}

// `get_next_task` and `NUM_TASK_IN_QUEUE` accounting rely on the clustered
// schedulers forming a strict prefix of `PRIORITY_LIST`. Reject any reorder
// at compile time.
// `get_next_task` relies on the clustered schedulers forming a strict prefix
// of `PRIORITY_LIST`. Reject any reorder at compile time.
const _: () = {
let mut i = get_num_clustered_schedulers();
while i < PRIORITY_LIST.len() {
Expand Down Expand Up @@ -191,8 +190,8 @@ static GLOBAL_WAKE_GET_MUTEX: Mutex<()> = Mutex::new(());
///
/// 1. Wrap each entry of its run queue in [`ClusteredTask`] so that
/// `NUM_CLUSTERED_TASKS_IN_QUEUE` is kept consistent.
/// 2. Implement [`Scheduler::queued_cpu_mask`] to report the exact set of
/// CPUs its queued tasks are runnable on (`wake_workers` relies on it).
/// 2. Implement [`Scheduler::queued_workers`] to report which workers its
/// queued tasks need (`wake_workers` relies on it).
/// 3. Be placed in the clustered prefix of `PRIORITY_LIST` and be matched by
/// `SchedulerType::is_clustered` (checked at compile time).
pub(crate) trait Scheduler {
Expand All @@ -209,22 +208,28 @@ pub(crate) trait Scheduler {
#[allow(dead_code)] // TODO: to be removed
fn priority(&self) -> u8;

/// The set of CPUs that have at least one queued task eligible to run on
/// them. Non-clustered schedulers use the default (empty) implementation;
/// The workers that queued tasks need, as `(mask, num_global)`.
///
/// `mask` holds every CPU that a queued task restricted to some of the
/// workers can run on. `num_global` is the number of queued tasks that
/// every worker can run, so any `num_global` workers can run them.
/// Non-clustered schedulers use the default (empty) implementation;
/// clustered schedulers must override it.
fn queued_cpu_mask(&self) -> CpuSet {
CpuSet::empty()
fn queued_workers(&self) -> (CpuSet, usize) {
(CpuSet::empty(), 0)
}
}

/// Return the union of [`Scheduler::queued_cpu_mask`] over all clustered
/// schedulers: the exact set of CPUs for which a clustered task is queued.
pub(crate) fn clustered_queued_cpu_mask() -> CpuSet {
/// Return [`Scheduler::queued_workers`] summed over all clustered schedulers.
pub(crate) fn clustered_queued_workers() -> (CpuSet, usize) {
let mut mask = CpuSet::empty();
let mut num_global = 0;
for scheduler_type in &PRIORITY_LIST[..get_num_clustered_schedulers()] {
mask = mask.union(get_scheduler(scheduler_type).queued_cpu_mask());
let (m, n) = get_scheduler(scheduler_type).queued_workers();
mask = mask.union(m);
num_global += n;
}
mask
(mask, num_global)
}

/// Get the next executable task.
Expand Down Expand Up @@ -253,18 +258,10 @@ pub(crate) fn get_next_task(execution_ensured: bool) -> Option<Arc<Task>> {
// Skip the clustered prefix: it was either just tried above and returned
// None (the queue state cannot change in between because both `wake_task`
// and `get_next_task` hold `GLOBAL_WAKE_GET_MUTEX`), or the counter was 0
// and its queues are empty. This also structurally guarantees that the
// `NUM_TASK_IN_QUEUE` decrement below never applies to a clustered task,
// which is counted by `NUM_CLUSTERED_TASKS_IN_QUEUE` instead.
let task = PRIORITY_LIST[get_num_clustered_schedulers()..]
// and its queues are empty.
PRIORITY_LIST[get_num_clustered_schedulers()..]
.iter()
.find_map(|scheduler_type| get_scheduler(scheduler_type).get_next(execution_ensured));

if task.is_some() {
crate::task::NUM_TASK_IN_QUEUE.fetch_sub(1, Ordering::Relaxed);
}

task
.find_map(|scheduler_type| get_scheduler(scheduler_type).get_next(execution_ensured))
}

/// Get a scheduler.
Expand Down
Loading
Loading