From ea953209ff821d135103fb275f8d19a69e0aae63 Mon Sep 17 00:00:00 2001 From: Zejun Zhao Date: Sat, 28 Dec 2024 03:01:22 +0800 Subject: [PATCH] Add a fast path while picking next thread from RT scheduling class --- kernel/src/sched/sched_class/real_time.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/kernel/src/sched/sched_class/real_time.rs b/kernel/src/sched/sched_class/real_time.rs index df65cda7e..fa8d481b1 100644 --- a/kernel/src/sched/sched_class/real_time.rs +++ b/kernel/src/sched/sched_class/real_time.rs @@ -135,6 +135,7 @@ pub(super) struct RealTimeClassRq { cpu: CpuId, index: bool, array: [PrioArray; 2], + nr_running: usize, } impl RealTimeClassRq { @@ -146,6 +147,7 @@ impl RealTimeClassRq { map: bitarr![0; 100], queue: array::from_fn(|_| VecDeque::new()), }), + nr_running: 0, } } @@ -166,21 +168,31 @@ impl SchedClassRq for RealTimeClassRq { fn enqueue(&mut self, thread: Arc, _: Option) { let prio = thread.sched_attr().real_time.prio.load(Relaxed); self.inactive_array().enqueue(thread, prio); + self.nr_running += 1; } fn len(&mut self) -> usize { - self.active_array().map.count_ones() + self.inactive_array().map.count_ones() + self.nr_running } fn is_empty(&mut self) -> bool { - self.active_array().map.is_empty() && self.inactive_array().map.is_empty() + self.nr_running == 0 } fn pick_next(&mut self) -> Option> { - self.active_array().pop().or_else(|| { + if self.nr_running == 0 { + return None; + } + + let res = self.active_array().pop().or_else(|| { self.swap_arrays(); self.active_array().pop() - }) + }); + if res.is_some() { + self.nr_running -= 1; + } + + res } fn update_current(