Repository navigation
fix(sched): do not dequeue a lower-priority task on the PrioritizedRR tick (#729) - #809
Merged
Merged
Conversation
… tick (tier4#729) `invoke_preemption_tick()` dequeued the head of the run queues whenever the quantum of the running task had expired, without comparing priorities. A lower-priority head was sent to the target CPU, rejected by `do_preemption()`, and re-queued at the tail of its level. Nothing restarted the quantum, so this repeated on every primary loop iteration: one IPI per iteration, and the FIFO order of the lower level was lost. - Add `Scheduler::get_next_if()` and `get_next_task_if()`, which dequeue the head only if a predicate accepts it, under the run queue lock. `PriorityQueue::peek()` is added for it. - The tick dequeues a task only if its priority is at least that of the running task and no preemption is pending on the CPU. Otherwise it restarts the quantum, or sends the IPI again if one is pending. - Panicked tasks are never taken by the tick: `Task::priority` keeps the priority they had before the panic. - The tick returns the time until the quantum expires, and `scheduler::wake_task()` caps the sleep of the primary CPU with it while a task is queued, so a restarted quantum is still checked on time. - Start the quantum of a preempted task that `do_preemption()` resumes directly; it did not pass through `run_main`, so its quantum looked expired at once. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
ytakano
marked this pull request as ready for review
November 18, 2026 07:37
atsushi421
reviewed
Nov 18, 2026
- wake_task(): read NUM_TASK_IN_QUEUE after the sleep handlers have run, so that a task enqueued by a handler on the primary CPU keeps the quantum cap on the sleep of the primary CPU. - invoke_preemption_tick(): check under GLOBAL_WAKE_GET_MUTEX that the task still runs on the CPU, and push the dequeued task to the preemption-pending heap before the mutex is released. The idle check in run_main() then sees the push, and the task is not left pending on a sleeping CPU. get_next_task_if() takes an on_taken callback for this. - Start the time quantum before a task becomes visible as the running task, in do_preemption() (both branches) and in PrioritizedRRScheduler::get_next(). A stale last_executed_time let the tick rotate the task out before it ran. - Reword the comments that referred to get_next(false) and get_next_task(false), which have no callers anymore. - Remove the unused task::get_last_executed_by_task_id(). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
atsushi421
approved these changes
Nov 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #729.
PrioritizedRRScheduler::invoke_preemption_tick()dequeued the head of the run queues whenever the quantum of the running task had expired, without comparing priorities. A lower-priority head was sent to the target CPU, rejected bydo_preemption(), and re-queued at the tail of its priority level. Nothing restarted the quantum, so this repeated on every primary loop iteration.With this change the tick dequeues a task only if it can actually replace the running one, and restarts the quantum otherwise.
Changes
last_executed_timeof the running task, so the run queues are not examined again on every iteration.Task::prioritykeeps the priority a task had before it panicked, so the priority comparison cannot judge such a task, and it could displace a healthy PrioritizedRR task.scheduler::wake_task()caps its return value with it while a task is queued. Without this, a task of equal priority that arrives just after a quantum restart waits until something else wakes the primary CPU (up to 1 s when the timer is enabled).mdbook/src/internal/scheduler.mdand the comments onTask::preempts()anddo_preemption()are updated.Behavior changes to be aware of
last_executed_timeis also whatawkernel_shellprints as "Last Executed". For a long-running PrioritizedRR task it is now the time of the last quantum restart, not the time of the last dispatch.Not addressed
A task that the tick (or a wake path) moved to a preemption-pending heap is still re-queued at the tail of its level if the running task changes before the IPI is handled. This PR removes the case that happened on every iteration; the remaining window is the one described in #703.
Verification
make check BSP=aarch64_virtandmake clippy: no warnings or errors.awkernel_lib,awkernel_async_lib,awkernel_drivers): all pass. A test forPriorityQueue::peek()is added.test_prioritized_rr(45 s): the 24 priority-1 tasks ran 359 to 369 times each; priority-0 tasks ran only while the tasks were being spawned.test_multi_prioritized_scheduler(30 s): all 30 tasks started and finished, on this branch and onmain.Comparison with
maintest_prioritized_rralways has an equal-priority task in the queue, so it does not hit #729. I measured a dedicated scenario instead: QEMU x86_64 with 16 CPUs (15 workers), 7 CPU-boundPrioritizedRR(1)tasks and 30 CPU-boundPrioritizedRR(0)tasks, so that onlyPrioritizedRR(0)tasks wait in the run queue. Temporary counters (not part of this PR) counted the IPIs sent by the tick and the pending tasks thatdo_preemption()rejected and re-queued.mainis fd13859, and this PR is 00fed6c, which includes the fixes for the review comments.Cumulative values after 20 s:
mainPrioritizedRR(1)tasks (total)PrioritizedRR(0)task (min / max)mainthe tick sent about 7,900 IPIs per second for the first 10 s, and about half of them were rejected and re-queued. With this PR the rate is steady at about 1,900 per second, close to the 2,000 per second expected for 8 CPUs rotating every 4 ms.mainat least onePrioritizedRR(0)task made no progress after the first 5 s: its count stayed at 616 until the end of the run (65 s). With this PR the minimum and the maximum differ by about 11%, and the values stay steady for the whole 65 s.This is one run per branch on QEMU without KVM, so the absolute values will differ on real hardware. On
mainthe IPI and preemption rates dropped to about 400 and 180 per second after about 10 s, and the rotation of thePrioritizedRR(0)tasks almost stopped. I did not investigate why; the missing EOI that #798 fixes may be related.Related links
How was this PR tested?
scripts/ci.shpasses on my machine.Notes for reviewers