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

オープン
#726 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
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時間
マージ済み PR(30日)
28

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

tier4/awkernel のほかの issue

tier4/awkernel の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。