Skip to content

feat(ax-sched): add priority-based preemptive RT scheduler with round… - #1891

Open
luchaohai wants to merge 1 commit into
rcore-os:devfrom
luchaohai:feat/ax-sched
Open

feat(ax-sched): add priority-based preemptive RT scheduler with round…#1891
luchaohai wants to merge 1 commit into
rcore-os:devfrom
luchaohai:feat/ax-sched

Conversation

@luchaohai

Copy link
Copy Markdown

…-robin

Migrate FreeRTOS scheduler design concepts into a new RTScheduler / RTTask implementation that plugs into the existing BaseScheduler trait.

Key design decisions:

  • Priority convention follows FreeRTOS: higher numbers = higher priority.
  • Ready tasks stored in BTreeMap<(-priority, task_id)> so pop_first always yields the highest-priority task; incrementing task_id gives natural round-robin within the same priority level.
  • task_tick triggers reschedule when the time slice expires (same-priority round-robin) or when a higher-priority task is ready (cross-priority preemption).
  • set_priority dynamically elevates a task by removing it from the old priority key and re-inserting under the new one.

Wiring:

  • New sched-rt feature in ax-task, axstd, and axvisor.
  • cfg_if branch in axtask::api selects RTScheduler at compile time.
  • Axvisor switches from multitask to sched-rt.

Tests:

  • Standard round-robin test via def_test_sched!.
  • Priority preemption, same-priority round-robin, and dynamic priority change tests.
  • Mixed-priority simulation covering cross-priority preemption, time-slice expiry, task blocking, and dynamic priority elevation.

…-robin

Migrate FreeRTOS scheduler design concepts into a new
`RTScheduler` / `RTTask` implementation that plugs into the existing
`BaseScheduler` trait.

Key design decisions:
- Priority convention follows FreeRTOS: higher numbers = higher priority.
- Ready tasks stored in `BTreeMap<(-priority, task_id)>` so `pop_first`
  always yields the highest-priority task; incrementing `task_id` gives
  natural round-robin within the same priority level.
- `task_tick` triggers reschedule when the time slice expires (same-priority
  round-robin) or when a higher-priority task is ready (cross-priority
  preemption).
- `set_priority` dynamically elevates a task by removing it from the old
  priority key and re-inserting under the new one.

Wiring:
- New `sched-rt` feature in `ax-task`, `axstd`, and `axvisor`.
- `cfg_if` branch in `axtask::api` selects `RTScheduler` at compile time.
- Axvisor switches from `multitask` to `sched-rt`.

Tests:
- Standard round-robin test via `def_test_sched!`.
- Priority preemption, same-priority round-robin, and dynamic priority
  change tests.
- Mixed-priority simulation covering cross-priority preemption, time-slice
  expiry, task blocking, and dynamic priority elevation.

@mai-team-app mai-team-app Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

本 PR 在 ax-sched 新增基于优先级的 RT 调度器,并在 axtask/axstd/Axvisor 增加 sched-rt 接线。实现复用了既有 BaseScheduler 边界;但当前 Axvisor 实际接入后没有生产优先级来源或更新入口,主张的跨优先级抢占不可达,行为退化为同优先级轮转。

功能开发规范适用:这是共享调度器和 OS 配置的新增能力(中风险)。已对比 dev 的 FIFO/RR/CFS;相关开放 PR #1016 是 SMP work-stealing,解决空闲 CPU 负载均衡,和本 PR 互补而非重复。未涉及 Starry 用户态 syscall/Linux ABI,也没有新增或修改应用/QEMU case,因此对应 app/runtime 流程不适用。

验证:

  • cargo test -p ax-sched:17 个单元测试及 1 个 doctest 均通过。
  • cargo fmt --all -- --check:失败;组织 CI 当前头的 Check formatting / run_host 也失败(job 92339259684)。
  • cargo clippy -p ax-sched --all-targets -- -D warnings:失败,新增测试有 15 个 clippy::useless_format 错误。
  • helper 的 head/base 预检通过;工作区和 PR 头均为 436b159d41b6afc19c8d50c2517aa427cf1fe9eb
  • cargo check -p ax-task --features 'multitask sched-rt' 在依赖下载/编译阶段未在本轮可用时间内完成,不能作为通过证据。

此前没有 review、行内评论或 issue 讨论需要处理。除下列问题外,没有发现额外的 syscall ABI、应用运行或与 #1016 的重复风险。请修复格式和 clippy,同时补全 axtask 层可实际使用的优先级接口及集成覆盖后重新验证。

Powered by gpt-5.6-terra

fn add_task(&mut self, task: Self::SchedItem) {
let task_id = self.alloc_task_id();
task.set_task_id(task_id);
self.ready_queue.insert((-task.priority(), task_id), task);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严重性:阻塞。这里用 -task.priority() 构造反序键,但优先级 API 接受任意 isize;传入 isize::MIN 时此处会溢出(同样的取负还出现在移除、重新入队和最高优先级反转路径)。在带溢出检查的构建中这会使调度路径 panic,release 则会得到错误的排序/比较结果。请改用覆盖完整 isize 域的反序排序键(或显式拒绝该值并记录、测试该前置条件)。

// Pick A first (earlier task_id).
let running = scheduler.pick_next_task().unwrap();
assert_eq!(*running.inner(), 0, "Phase 1: A should run first");
trace.push(format!(" run: A (prio=0)"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严重性:阻塞。此处及同一测试中的多个静态文本使用了无插值的 format!cargo clippy -p ax-sched --all-targets -- -D warnings 因 15 个 clippy::useless_format 错误退出。请将静态字符串改为 .to_string()/String::from(...)(或改造容器),不要用 allow 压制告警,并重新运行 clippy。

pub(crate) type AxTask = ax_sched::CFSTask<TaskInner>;
pub(crate) type Scheduler = ax_sched::CFScheduler<TaskInner>;
} else if #[cfg(feature = "sched-rt")] {
pub(crate) type AxTask = ax_sched::RTTask<TaskInner>;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

严重性:阻塞。这里虽然选中了 RTTask/RTScheduler,但当前 axtask 的生产创建路径没有给任务传入优先级,仓库中 new_with_priorityset_priority 的调用也都只在 ax-sched 单元测试里;所以 Axvisor 启用 sched-rt 后所有任务仍固定为优先级 0,跨优先级抢占这一主功能不可达,只剩 RR 行为。请在 axtask 提供并接入明确、受约束的优先级创建/更新接口(并做集成测试),或者在该能力具备前不要把 Axvisor 切换到此调度器。

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.

1 participant