|
| 1 | +:::{note} |
| 2 | +**AI Translation Notice** |
| 3 | + |
| 4 | +This document was automatically translated by `hunyuan-turbos-latest` model, for reference only. |
| 5 | + |
| 6 | +- Source document: kernel/sched/fifo.md |
| 7 | + |
| 8 | +- Translation time: 2026-01-05 12:01:34 |
| 9 | + |
| 10 | +- Translation model: `hunyuan-turbos-latest` |
| 11 | + |
| 12 | +Please report issues via [Community Channel](https://github.com/DragonOS-Community/DragonOS/issues) |
| 13 | + |
| 14 | +::: |
| 15 | + |
| 16 | +# FIFO Scheduler |
| 17 | + |
| 18 | +   The FIFO (First-In-First-Out) scheduler is a real-time scheduling policy implemented in DragonOS. The FIFO scheduler adopts an advanced-first-out scheduling algorithm to provide deterministic scheduling behavior for real-time tasks. |
| 19 | + |
| 20 | +## 1. Design Overview |
| 21 | + |
| 22 | +   The FIFO scheduler is designed for real-time tasks, with the following core characteristics: |
| 23 | + |
| 24 | +1. **No time slice mechanism**: Once a FIFO task obtains the CPU, it will run continuously until it voluntarily releases the CPU or is preempted by a higher-priority task. |
| 25 | +2. **Priority scheduling**: Supports 100 priorities from 0 to 99, where a smaller number indicates a higher priority. |
| 26 | +3. **Same-priority FIFO**: Tasks with the same priority are executed strictly in the order they enter the queue. |
| 27 | + |
| 28 | +## 2. Data Structures |
| 29 | + |
| 30 | +### 2.1 FifoRunQueue |
| 31 | + |
| 32 | +   `FifoRunQueue` is the run queue of the FIFO scheduler, with one instance maintained per CPU. |
| 33 | + |
| 34 | +```rust |
| 35 | +pub struct FifoRunQueue { |
| 36 | + queues: Vec<VecDeque<Arc<ProcessControlBlock>>>, // 100个优先级队列 |
| 37 | + active: u128, // 优先级位图,快速查找最高优先级 |
| 38 | + nr_running: usize, // 运行队列中的进程数 |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +**Design Highlights:** |
| 43 | + |
| 44 | +- **Multi-level queues**: Uses 100 `VecDeque` to store processes of different priorities separately. |
| 45 | +- **Bitmap optimization**: The `active` field uses a 128-bit bitmap to record which priority queues are non-empty, quickly locating the highest priority through the `trailing_zeros()` instruction. |
| 46 | +- **O(1) selection**: Utilizes the bitmap and deque to achieve O(1) time complexity for the `pick_next()` operation. |
| 47 | + |
| 48 | +### 2.2 FifoScheduler |
| 49 | + |
| 50 | +   `FifoScheduler` implements the `Scheduler` trait, providing the core logic of the FIFO scheduling policy. |
| 51 | + |
| 52 | +## 3. Implemented Features |
| 53 | + |
| 54 | +### 3.1 Basic Scheduling Operations |
| 55 | + |
| 56 | +| Function | Functionality | Implementation Status | |
| 57 | +|----------|---------------|-----------------------| |
| 58 | +| `enqueue()` | Add a process to the scheduling queue | ✅ Implemented | |
| 59 | +| `dequeue()` | Remove a process from the scheduling queue | ✅ Implemented | |
| 60 | +| `pick_next_task()` | Select the next process to execute | ✅ Implemented | |
| 61 | +| `yield_task()` | Current process voluntarily yields the CPU | ✅ Implemented | |
| 62 | + |
| 63 | +### 3.2 Preemption Mechanism |
| 64 | + |
| 65 | +**check_preempt_currnet()**: When a new process is awakened, check whether the current process needs to be preempted. |
| 66 | +- If the new process has a higher priority, trigger preemption. |
| 67 | +- Supports preemption between real-time tasks and normal tasks. |
| 68 | + |
| 69 | +**tick()**: Clock interrupt handling. |
| 70 | +- Check whether a higher-priority task has entered the queue. |
| 71 | +- If so, trigger rescheduling. |
| 72 | + |
| 73 | +### 3.3 Scheduling Priority |
| 74 | + |
| 75 | +The FIFO scheduler uses a priority range compatible with Linux: |
| 76 | + |
| 77 | +```rust |
| 78 | +pub const MAX_RT_PRIO: i32 = 100; // 实时优先级范围 0-99 |
| 79 | +``` |
| 80 | + |
| 81 | +- Priority 0: Highest priority. |
| 82 | +- Priority 99: Lowest real-time priority. |
| 83 | +- Priority >=100: Normal processes (CFS scheduling). |
| 84 | + |
| 85 | +### 3.4 Policy Switching |
| 86 | + |
| 87 | +Through the `ProcessManager::set_fifo_policy()` interface, it supports switching kernel threads to the FIFO scheduling policy at runtime: |
| 88 | + |
| 89 | +```rust |
| 90 | +pub fn set_fifo_policy(pcb: &Arc<ProcessControlBlock>, prio: i32) -> Result<(), SystemError> |
| 91 | +``` |
| 92 | + |
| 93 | +This function will: |
| 94 | +1. Verify that the process must be a kernel thread (KTHREAD flag). |
| 95 | +2. Verify that the priority is within the valid range (0-99). |
| 96 | +3. Handle the state change of the process in the run queue. |
| 97 | +4. Trigger preemption checks. |
| 98 | + |
| 99 | +## 4. Scheduling Process |
| 100 | + |
| 101 | +### 4.1 Process Enqueue |
| 102 | + |
| 103 | +``` |
| 104 | +enqueue() |
| 105 | + ↓ |
| 106 | +计算进程优先级索引 |
| 107 | + ↓ |
| 108 | +加入对应优先级队列尾部 |
| 109 | + ↓ |
| 110 | +更新位图active |
| 111 | + ↓ |
| 112 | +nr_running++ |
| 113 | +``` |
| 114 | + |
| 115 | +### 4.2 Selecting the Next Process |
| 116 | + |
| 117 | +``` |
| 118 | +pick_next_task() |
| 119 | + ↓ |
| 120 | +从位图获取最高优先级(trailing_zeros) |
| 121 | + ↓ |
| 122 | +返回该优先级队列队首进程 |
| 123 | +``` |
| 124 | + |
| 125 | +### 4.3 Preemption Judgment |
| 126 | + |
| 127 | +``` |
| 128 | +新进程唤醒 / 时钟中断 |
| 129 | + ↓ |
| 130 | +获取当前进程和新进程优先级 |
| 131 | + ↓ |
| 132 | +if (新进程优先级 < 当前进程优先级): // 数字越小优先级越高 |
| 133 | + ↓ |
| 134 | +设置重调度标志 |
| 135 | +``` |
| 136 | + |
| 137 | +## 5. Demo Functionality |
| 138 | + |
| 139 | +   The demo functionality can be enabled through the `fifo_demo` feature (`kernel/src/sched/fifo_demo.rs`), which creates a FIFO-scheduled kernel thread: |
| 140 | +- Sets CPU affinity to Core 0. |
| 141 | +- Sets the FIFO scheduling policy with priority 50. |
| 142 | +- Outputs a log every 5 seconds. |
| 143 | + |
| 144 | +Enabling method: Add the feature in `Cargo.toml` and call `fifo_demo_init()`. |
| 145 | + |
| 146 | +## 6. TODO |
| 147 | + |
| 148 | +### 6.1 Multi-core Support |
| 149 | + |
| 150 | +- [ ] Implement FIFO task load balancing between multiple CPUs. |
| 151 | +- [ ] Support setting and migrating CPU affinity for tasks. |
| 152 | + |
| 153 | +### 6.2 Scheduling Enhancements |
| 154 | + |
| 155 | +- [ ] Implement the SCHED_RR (round-robin) scheduling policy. |
| 156 | +- [ ] Support dynamic priority adjustment. |
| 157 | +- [ ] Add scheduling latency statistics and monitoring. |
| 158 | + |
| 159 | +### 6.3 Real-time Guarantees |
| 160 | + |
| 161 | +- [ ] Implement real-time task bandwidth limits. |
| 162 | +- [ ] Add priority inheritance mechanism (to prevent priority inversion). |
| 163 | +- [ ] Support EDF (Earliest Deadline First) scheduling policy. |
| 164 | + |
| 165 | +### 6.4 User-space Interface |
| 166 | + |
| 167 | +- [ ] Implement the `sched_setscheduler` system call. |
| 168 | + |
| 169 | +### 6.5 Optimization and Debugging |
| 170 | + |
| 171 | +- [ ] Add debugging information output for the FIFO scheduler. |
| 172 | +- [ ] Implement a scheduling latency monitoring interface. |
| 173 | +- [ ] Optimize bitmap operations to support more priority levels. |
0 commit comments