Skip to content

fix(sched): do not dequeue a lower-priority task on the PrioritizedRR tick (#729) - #809

Open
ytakano wants to merge 1 commit into
tier4:mainfrom
ytakano:fix/prioritized-rr-tick-requeue-729
Open

ytakano wants to merge 1 commit into
tier4:mainfrom
ytakano:fix/prioritized-rr-tick-requeue-729

Conversation

@ytakano

@ytakano ytakano commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

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 by do_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

  • Conditional dequeue. Add Scheduler::get_next_if() and scheduler::get_next_task_if(). They dequeue the head of the run queue only if a predicate accepts it, and the check and the dequeue are done under the run queue lock. PriorityQueue::peek() is added for this. PrioritizedFIFO and PrioritizedRR share the implementation (take_head_if()).
  • Priority check in the tick. The tick dequeues the next task only if its priority is at least that of the running task (equal: quantum rotation, higher: plain preemption) and no preemption is pending on that CPU. The pending check runs under GLOBAL_WAKE_GET_MUTEX, so a wake path cannot push a pending task between the check and the dequeue.
  • Quantum restart. If there is no task to rotate with, the tick updates last_executed_time of the running task, so the run queues are not examined again on every iteration.
  • IPI retry. If a preemption is already pending on the CPU, the tick dequeues nothing and only sends the IPI again.
  • Panicked tasks. The tick never takes a task from the Panicked scheduler. Task::priority keeps 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.
  • Sleep of the primary CPU. The tick returns the time until the quantum expires, and 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).
  • Quantum of a resumed task. do_preemption() now updates last_executed_time when it switches directly to the saved thread of a preempted task. That path does not pass through run_main, so the quantum of the resumed task looked expired at once.
  • Docs. mdbook/src/internal/scheduler.md and the comments on Task::preempts() and do_preemption() are updated.

Behavior changes to be aware of

  • While a PrioritizedRR task is running and a task is queued, the primary CPU wakes at least once per quantum (4 ms).
  • last_executed_time is also what awkernel_shell prints 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_virt and make clippy: no warnings or errors.
  • Host unit tests with the pinned toolchain (awkernel_lib, awkernel_async_lib, awkernel_drivers): all pass. A test for PriorityQueue::peek() is added.
  • QEMU x86_64, 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.
  • QEMU x86_64, test_multi_prioritized_scheduler (30 s): all 30 tasks started and finished, on this branch and on main.

Comparison with main

test_prioritized_rr always 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-bound PrioritizedRR(1) tasks and 30 CPU-bound PrioritizedRR(0) tasks, so that only PrioritizedRR(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 that do_preemption() rejected and re-queued.

Cumulative values after 20 s:

main this PR
IPIs sent by the tick 104,474 38,423
Pending tasks rejected and re-queued 55,081 16
Preemptions performed 50,611 37,324
Work done by the PrioritizedRR(1) tasks (total) 1,131,289 1,192,212
Work done by a PrioritizedRR(0) task (min / max) 67 / 125,423 38,904 / 44,301
  • On main the tick sent about 10,000 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.
  • On main at least one PrioritizedRR(0) task made no progress for the whole 20 s. With this PR the minimum and the maximum differ by about 14%.

This is one run per branch on QEMU without KVM, so the absolute values will differ on real hardware. On main the IPI and preemption rates dropped to a few hundred per second after about 11 s and the rotation of the PrioritizedRR(0) tasks almost stopped; I did not investigate why.

Related links

How was this PR tested?

  • scripts/ci.sh passes on my machine.

Notes for reviewers

… 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
ytakano marked this pull request as ready for review November 18, 2026 07:37
@ytakano
ytakano requested a review from atsushi421 November 18, 2026 07:37

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PrioritizedRR: invoke_preemption_tick() pops and re-queues a lower-priority queue head on every primary loop iteration

1 participant