Remove the task-ID tie-break from `Task`'s ordering

Abierto
#726 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 2 días

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
42/100
Tipo de issue
Refactorización
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
rust

Línea de trabajo

Start in awkernel_async_lib/src/task.rs at impl Ord for Task, then read PREEMPTION_PENDING_TASKS and peek_preemption_pending()/do_preemption() in scheduler.rs, and the victim selection in prioritized_fifo.rs, prioritized_rr.rs, gedf.rs and clustered_edf.rs. Done means the pending heap orders by priority then FIFO wake sequence via an explicit wrapper, victim selection compares task.priority directly, and Ord/PartialOrd on Task can be removed while preemption tests still pass.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

Remove the task-ID tie-break from Task's ordering

Summary

impl Ord for Task (awkernel_async_lib/src/task.rs) compares by priority and
breaks ties by task ID (a larger ID is "greater"). The tie-break exists only to
make the ordering total and consistent with PartialEq (which compares IDs); it
has no scheduling meaning. It was introduced in #572 with the opposite direction
and flipped together with the priority direction in #634.

The preemption decision no longer depends on it: Task::preempts() requires a
strictly higher priority (branch fix_equal_priority_preemption, commit 82f9b6a8),
so a task woken with the same priority as a running task no longer preempts it.

Two users of Task: Ord remain. Neither is incorrect, but both make the task ID
part of scheduling behavior where it should not be, and they are the only reason
Task needs an Ord at all.

1. Preemption-pending heaps

scheduler.rs: PREEMPTION_PENDING_TASKS: BTreeMap<usize, BinaryHeap<Arc<Task>>>.
peek_preemption_pending() and do_preemption()'s moved_preemption_pending.pop()
take the heap maximum as the pending task to run next.

Among pending tasks of equal priority, the heap pops the one with the largest ID
first, i.e. LIFO by spawn order, whereas every run queue is FIFO within a priority
level. Task IDs are also reused after u32 wrap-around (Tasks::spawn searches for
a free ID from candidate_id), so "larger ID" does not even mean "newer" in the
long run.

Proposed change: store a wrapper in the heap with an explicit ordering, like the
run queues already do (GEDFTask, AffinityBTreeQueue's (priority, seq) key):

struct PendingTask {
    priority: u64,   // snapshot of task.priority at push time
    seq: u64,        // global wake sequence number; smaller = earlier
    task: Arc<Task>,
}
// Ord: priority descending, then seq ascending (FIFO among equals).

Snapshotting the priority also fixes a latent issue: PriorityInfo is an atomic
that EDF updates on every wake, and mutating a key while it sits in a BinaryHeap
breaks the heap invariant. remove_preemption_pending() keeps matching by task.id.

2. Victim selection in invoke_preemption*()

prioritized_fifo.rs, prioritized_rr.rs, gedf.rs (max(t, highest_pending),
.min() over (Arc<Task>, cpu_id)) and clustered_edf.rs (running.max(pending),
target_task >= *lowest) use Task: Ord to find the lowest-priority target CPU.

The tie-break cannot change whether preemption happens (all equal-priority
candidates have the same priority, so preempts() gives the same answer for any
of them); it only decides which equal-priority task is preempted. Today that is
the one with the smallest ID. This should compare task.priority directly, e.g.
min_by(|a, b| a.priority.cmp(&b.priority)), picking the first CPU in iteration
order on ties, so the choice is explicit rather than a side effect of Task::cmp.

Result

With 1 and 2 done, Ord/PartialOrd for Task can be removed (keeping
PartialEq/Eq by ID), and the only place where a priority comparison between
tasks happens is Task::preempts() plus the explicit orderings of the run queues
and the pending heap.

Related

  • #572 (introduced the tie-break), #634 (flipped its direction)
  • branch fix_equal_priority_preemption / 82f9b6a8 (strict comparison in the
    preemption decision; prerequisite for this issue)
Lenguaje dominante
Rust
Estrellas
282
Forks
12
Merge medio
2 d 9 h
PR fusionados (30 d)
28

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de tier4/awkernel

Todos los issues de tier4/awkernel

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.