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

未关闭
#726 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 2 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
42/100
Issue 类型
重构
描述清晰度
基本清楚
活跃度
活跃
技术栈
rust

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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)
主要语言
Rust
星标
282
派生
12
平均合并
2 天 9 小时
30 天内合并 PR
28

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

tier4/awkernel 的其他 Issue

查看 tier4/awkernel 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。