1 use core::{ptr::null_mut, sync::atomic::compiler_fence};
2
3 use alloc::{boxed::Box, collections::LinkedList, vec::Vec};
4
5 use crate::{
6 arch::asm::current::current_pcb,
7 include::bindings::bindings::{
8 process_control_block, MAX_CPU_NUM, PF_NEED_SCHED, SCHED_FIFO, SCHED_RR,
9 },
10 kBUG, kdebug,
11 libs::spinlock::RawSpinlock,
12 };
13
14 use super::core::{sched_enqueue, Scheduler};
15
16 /// 声明全局的rt调度器实例
17
18 pub static mut RT_SCHEDULER_PTR: *mut SchedulerRT = null_mut();
19
20 /// @brief 获取rt调度器实例的可变引用
21 #[inline]
__get_rt_scheduler() -> &'static mut SchedulerRT22 pub fn __get_rt_scheduler() -> &'static mut SchedulerRT {
23 return unsafe { RT_SCHEDULER_PTR.as_mut().unwrap() };
24 }
25
26 /// @brief 初始化rt调度器
sched_rt_init()27 pub unsafe fn sched_rt_init() {
28 kdebug!("rt scheduler init");
29 if RT_SCHEDULER_PTR.is_null() {
30 RT_SCHEDULER_PTR = Box::leak(Box::new(SchedulerRT::new()));
31 } else {
32 kBUG!("Try to init RT Scheduler twice.");
33 panic!("Try to init RT Scheduler twice.");
34 }
35 }
36
37 /// @brief RT队列(per-cpu的)
38 #[derive(Debug)]
39 struct RTQueue {
40 /// 队列的锁
41 lock: RawSpinlock,
42 /// 存储进程的双向队列
43 queue: LinkedList<&'static mut process_control_block>,
44 }
45
46 impl RTQueue {
new() -> RTQueue47 pub fn new() -> RTQueue {
48 RTQueue {
49 queue: LinkedList::new(),
50 lock: RawSpinlock::INIT,
51 }
52 }
53 /// @brief 将pcb加入队列
enqueue(&mut self, pcb: &'static mut process_control_block)54 pub fn enqueue(&mut self, pcb: &'static mut process_control_block) {
55 self.lock.lock();
56
57 // 如果进程是IDLE进程,那么就不加入队列
58 if pcb.pid == 0 {
59 self.lock.unlock();
60 return;
61 }
62 self.queue.push_back(pcb);
63 self.lock.unlock();
64 }
65
66 /// @brief 将pcb从调度队列头部取出,若队列为空,则返回None
dequeue(&mut self) -> Option<&'static mut process_control_block>67 pub fn dequeue(&mut self) -> Option<&'static mut process_control_block> {
68 let res: Option<&'static mut process_control_block>;
69 self.lock.lock();
70 if self.queue.len() > 0 {
71 // 队列不为空,返回下一个要执行的pcb
72 res = Some(self.queue.pop_front().unwrap());
73 } else {
74 // 如果队列为空,则返回None
75 res = None;
76 }
77 self.lock.unlock();
78 return res;
79 }
enqueue_front(&mut self, pcb: &'static mut process_control_block)80 pub fn enqueue_front(&mut self, pcb: &'static mut process_control_block) {
81 self.lock.lock();
82
83 // 如果进程是IDLE进程,那么就不加入队列
84 if pcb.pid == 0 {
85 self.lock.unlock();
86 return;
87 }
88 self.queue.push_front(pcb);
89 self.lock.unlock();
90 }
get_rt_queue_size(&mut self) -> usize91 pub fn get_rt_queue_size(&mut self) -> usize {
92 return self.queue.len();
93 }
94 }
95
96 /// @brief RT调度器类
97 pub struct SchedulerRT {
98 cpu_queue: Vec<Vec<&'static mut RTQueue>>,
99 load_list: Vec<&'static mut LinkedList<u64>>,
100 }
101
102 impl SchedulerRT {
103 const RR_TIMESLICE: i64 = 100;
104 const MAX_RT_PRIO: i64 = 100;
105
new() -> SchedulerRT106 pub fn new() -> SchedulerRT {
107 // 暂时手动指定核心数目
108 // todo: 从cpu模块来获取核心的数目
109 let mut result = SchedulerRT {
110 cpu_queue: Default::default(),
111 load_list: Default::default(),
112 };
113
114 // 为每个cpu核心创建队列
115 for cpu_id in 0..MAX_CPU_NUM {
116 result.cpu_queue.push(Vec::new());
117 // 每个CPU有MAX_RT_PRIO个优先级队列
118 for _ in 0..SchedulerRT::MAX_RT_PRIO {
119 result.cpu_queue[cpu_id as usize].push(Box::leak(Box::new(RTQueue::new())));
120 }
121 }
122 // 为每个cpu核心创建负载统计队列
123 for _ in 0..MAX_CPU_NUM {
124 result
125 .load_list
126 .push(Box::leak(Box::new(LinkedList::new())));
127 }
128 return result;
129 }
130
131 /// @brief 挑选下一个可执行的rt进程
pick_next_task_rt(&mut self, cpu_id: u32) -> Option<&'static mut process_control_block>132 pub fn pick_next_task_rt(&mut self, cpu_id: u32) -> Option<&'static mut process_control_block> {
133 // 循环查找,直到找到
134 // 这里应该是优先级数量,而不是CPU数量,需要修改
135 for i in 0..SchedulerRT::MAX_RT_PRIO {
136 let cpu_queue_i: &mut RTQueue = self.cpu_queue[cpu_id as usize][i as usize];
137 let proc: Option<&'static mut process_control_block> = cpu_queue_i.dequeue();
138 if proc.is_some() {
139 return proc;
140 }
141 }
142 // return 一个空值
143 None
144 }
145
get_rt_queue_len(&mut self, cpu_id: u32) -> usize146 pub fn get_rt_queue_len(&mut self, cpu_id: u32) -> usize {
147 let mut sum = 0;
148 for prio in 0..SchedulerRT::MAX_RT_PRIO {
149 sum += self.cpu_queue[cpu_id as usize][prio as usize].get_rt_queue_size();
150 }
151 return sum as usize;
152 }
153
get_load_list_len(&mut self, cpu_id: u32) -> usize154 pub fn get_load_list_len(&mut self, cpu_id: u32) -> usize {
155 return self.load_list[cpu_id as usize].len();
156 }
157
enqueue_front(&mut self, pcb: &'static mut process_control_block)158 pub fn enqueue_front(&mut self, pcb: &'static mut process_control_block) {
159 self.cpu_queue[pcb.cpu_id as usize][pcb.priority as usize].enqueue_front(pcb);
160 }
161 }
162
163 impl Scheduler for SchedulerRT {
164 /// @brief 在当前cpu上进行调度。
165 /// 请注意,进入该函数之前,需要关中断
sched(&mut self) -> Option<&'static mut process_control_block>166 fn sched(&mut self) -> Option<&'static mut process_control_block> {
167 current_pcb().flags &= !(PF_NEED_SCHED as u64);
168 // 正常流程下,这里一定是会pick到next的pcb的,如果是None的话,要抛出错误
169 let cpu_id = current_pcb().cpu_id;
170 let proc: &'static mut process_control_block =
171 self.pick_next_task_rt(cpu_id).expect("No RT process found");
172
173 // 如果是fifo策略,则可以一直占有cpu直到有优先级更高的任务就绪(即使优先级相同也不行)或者主动放弃(等待资源)
174 if proc.policy == SCHED_FIFO {
175 // 如果挑选的进程优先级小于当前进程,则不进行切换
176 if proc.priority <= current_pcb().priority {
177 sched_enqueue(proc, false);
178 } else {
179 // 将当前的进程加进队列
180 sched_enqueue(current_pcb(), false);
181 compiler_fence(core::sync::atomic::Ordering::SeqCst);
182 return Some(proc);
183 }
184 }
185 // RR调度策略需要考虑时间片
186 else if proc.policy == SCHED_RR {
187 // 同等优先级的,考虑切换
188 if proc.priority >= current_pcb().priority {
189 // 判断这个进程时间片是否耗尽,若耗尽则将其时间片赋初值然后入队
190 if proc.rt_time_slice <= 0 {
191 proc.rt_time_slice = SchedulerRT::RR_TIMESLICE;
192 proc.flags |= !(PF_NEED_SCHED as u64);
193 sched_enqueue(proc, false);
194 }
195 // 目标进程时间片未耗尽,切换到目标进程
196 else {
197 // 将当前进程加进队列
198 sched_enqueue(current_pcb(), false);
199 compiler_fence(core::sync::atomic::Ordering::SeqCst);
200 return Some(proc);
201 }
202 }
203 // curr优先级更大,说明一定是实时进程,将所选进程入队列,此时需要入队首
204 else {
205 self.cpu_queue[cpu_id as usize][proc.cpu_id as usize].enqueue_front(proc);
206 }
207 }
208 return None;
209 }
210
enqueue(&mut self, pcb: &'static mut process_control_block)211 fn enqueue(&mut self, pcb: &'static mut process_control_block) {
212 let cpu_id = pcb.cpu_id;
213 let cpu_queue = &mut self.cpu_queue[pcb.cpu_id as usize];
214 cpu_queue[cpu_id as usize].enqueue(pcb);
215 // // 获取当前时间
216 // let time = unsafe { _rdtsc() };
217 // let freq = unsafe { Cpu_tsc_freq };
218 // // kdebug!("this is timeeeeeeer {},freq is {}, {}", time, freq, cpu_id);
219 // // 将当前时间加入负载记录队列
220 // self.load_list[cpu_id as usize].push_back(time);
221 // // 如果队首元素与当前时间差超过设定值,则移除队首元素
222 // while self.load_list[cpu_id as usize].len() > 1
223 // && (time - *self.load_list[cpu_id as usize].front().unwrap() > 10000000000)
224 // {
225 // self.load_list[cpu_id as usize].pop_front();
226 // }
227 }
228 }
229