From 860028f7f98b0828bcedd647be65f83e3e1a15c6 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:16:58 +0900 Subject: [PATCH 01/20] feat: get_lowest_priority_info Signed-off-by: kobayu858 --- .../tests/test_sched_preempt/Cargo.toml | 15 +++ .../tests/test_sched_preempt/src/lib.rs | 58 ++++++++++++ awkernel_async_lib/src/task.rs | 94 +++++++++++++++++++ userland/Cargo.toml | 7 +- userland/src/lib.rs | 3 + 5 files changed, 176 insertions(+), 1 deletion(-) create mode 100644 applications/tests/test_sched_preempt/Cargo.toml create mode 100644 applications/tests/test_sched_preempt/src/lib.rs diff --git a/applications/tests/test_sched_preempt/Cargo.toml b/applications/tests/test_sched_preempt/Cargo.toml new file mode 100644 index 000000000..b1aba0355 --- /dev/null +++ b/applications/tests/test_sched_preempt/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "test_sched_preempt" +version = "0.1.0" +edition = "2021" + +[dependencies] +log = "0.4" + +[dependencies.awkernel_async_lib] +path = "../../../awkernel_async_lib" +default-features = false + +[dependencies.awkernel_lib] +path = "../../../awkernel_lib" +default-features = false diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs new file mode 100644 index 000000000..95c74d3b5 --- /dev/null +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -0,0 +1,58 @@ +#![no_std] + +use awkernel_async_lib::{scheduler::SchedulerType, spawn, task::get_lowest_priority_task_info}; +use awkernel_lib::{cpu::cpu_id, delay::{wait_microsec, uptime}}; + +/// Tests related to preemption between schedulers + +#[allow(dead_code)] +enum TestType { + GetlowestTask +} + +pub async fn run() { // TASK ID:1 + wait_microsec(100000); + log::info!("[{}] Start test_sched_preempt at cpu_id: {}", uptime(), cpu_id()); + + let test_type = TestType::GetlowestTask; + match test_type { + TestType::GetlowestTask => check_lowest_task().await, + } + + log::info!("[{}] End test_sched_preempt at cpu_id: {}", uptime(), cpu_id()); +} + +async fn check_lowest_task(){ + spawn( // TASK ID:8 + "GEDF".into(), + async move { + loop { + log::info!("[{}] GEDF is start at cpu_id: {}", uptime(), cpu_id()); + wait_microsec(10000000); + log::info!("[{}] GEDF is end at cpu_id: {}", uptime(), cpu_id()); + } + }, + SchedulerType::GEDF(10000), + ) + .await; + spawn( // TASK ID:9 + "FIFO".into(), + async move { + loop { + log::info!("[{}] FIFO is start at cpu_id: {}", uptime(), cpu_id()); + wait_microsec(10000000); + log::info!("[{}] FIFO is end at cpu_id: {}", uptime(), cpu_id()); + } + }, + SchedulerType::FIFO, + ) + .await; + + wait_microsec(1000000); + + // FIFO TASK ID:1 < FIFO TASK ID:9 < GEDF TASK ID:8 + // FIFO TASK ID:1 is the lowest priority task + if let Some((task_id, priority_info)) = get_lowest_priority_task_info() { + log::debug!("Task ID: {}, sched_priority: {:?}, task_priority: {:?}, cpu_id: {}", task_id, priority_info.scheduler_priority, priority_info.task_priority, priority_info.cpu_id); + } +} \ No newline at end of file diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 9e683d5a6..1db308429 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -962,6 +962,17 @@ pub fn get_scheduler_type_by_task_id(task_id: u32) -> Option { }) } +fn get_task_priority_by_task_id(task_id: u32) -> Option { + match get_scheduler_type_by_task_id(task_id) { + Some(SchedulerType::GEDF(_)) => Some(get_absolute_deadline_by_task_id(task_id)?), + Some(SchedulerType::FIFO) => Some(get_last_executed_by_task_id(task_id)?), + Some(SchedulerType::PrioritizedFIFO(priority)) => Some(priority as u64), + Some(SchedulerType::RR) => Some(get_last_executed_by_task_id(task_id)?), + Some(SchedulerType::PriorityBasedRR(priority)) => Some(priority as u64), + _ => None, + } +} + #[inline(always)] pub fn get_absolute_deadline_by_task_id(task_id: u32) -> Option { let mut node = MCSNode::new(); @@ -1011,3 +1022,86 @@ pub fn panicking() { preempt::preemption(); } } + +pub struct PriorityInfo { + pub scheduler_priority: u8, + pub task_priority: u64, + pub cpu_id: usize, +} + +impl PriorityInfo { + fn new(scheduler_priority: u8, task_priority: u64, cpu_id: usize) -> Self { + Self { + scheduler_priority, + task_priority, + cpu_id, + } + } +} + +impl PartialOrd for PriorityInfo { + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} + +impl PartialEq for PriorityInfo { + fn eq(&self, other: &Self) -> bool { + self.scheduler_priority == other.scheduler_priority + && self.task_priority == other.task_priority + && self.cpu_id == other.cpu_id + } +} + +impl Ord for PriorityInfo { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + match self.scheduler_priority.cmp(&other.scheduler_priority) { + core::cmp::Ordering::Equal => { + if self.scheduler_priority == 0 + || self.scheduler_priority == 1 + || self.scheduler_priority == 3 + { + self.task_priority.cmp(&other.task_priority) + } else { + other.task_priority.cmp(&self.task_priority) + } + } + other => other, + } + .then(self.cpu_id.cmp(&other.cpu_id)) + } +} + +impl Eq for PriorityInfo {} + +fn create_priority_info(task_id: u32) -> Option { + let scheduler_type = get_scheduler_type_by_task_id(task_id)?; + let scheduler_priority = get_scheduler(scheduler_type).priority(); + let task_priority = get_task_priority_by_task_id(task_id)?; + + Some(PriorityInfo::new( + scheduler_priority, + task_priority, + awkernel_lib::cpu::cpu_id(), + )) +} + +pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { + let num_cpus = awkernel_lib::cpu::num_cpu(); + let mut lowest_task: Option<(u32, PriorityInfo)> = None; // (task_id, priority_info) + + for (cpu_id, task) in RUNNING.iter().enumerate() { + if cpu_id >= num_cpus { + break; + } + + let task_id = task.load(Ordering::Relaxed); + if let Some(priority_info) = create_priority_info(task_id) { + if lowest_task.as_ref().map_or(true, |(_, current)| &priority_info > current) { + lowest_task = Some((task_id, priority_info)); + } + } + } + + lowest_task +} \ No newline at end of file diff --git a/userland/Cargo.toml b/userland/Cargo.toml index cd3fe58af..f36e9fd55 100644 --- a/userland/Cargo.toml +++ b/userland/Cargo.toml @@ -62,8 +62,12 @@ optional = true path = "../applications/tests/test_load_udp" optional = true +[dependencies.test_sched_preempt] +path = "../applications/tests/test_sched_preempt" +optional = true + [features] -default = ["test_graphics"] +default = ["test_sched_preempt"] perf = ["awkernel_services/perf"] # Test applications @@ -79,3 +83,4 @@ test_gedf = ["dep:test_gedf"] test_load_udp = ["dep:test_load_udp"] test_measure_channel = ["dep:test_measure_channel"] test_measure_channel_heavy = ["dep:test_measure_channel_heavy"] +test_sched_preempt = ["dep:test_sched_preempt"] diff --git a/userland/src/lib.rs b/userland/src/lib.rs index 212807f99..8b1ea82f2 100644 --- a/userland/src/lib.rs +++ b/userland/src/lib.rs @@ -43,5 +43,8 @@ pub async fn main() -> Result<(), Cow<'static, str>> { #[cfg(feature = "test_load_udp")] load_test_udp::run().await; // load test udp + #[cfg(feature = "test_sched_preempt")] + test_sched_preempt::run().await; // tests related to preemption between schedulers + Ok(()) } From fe541f8ff09c9d3df1481d4ebef16076926ec0f8 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:30:31 +0900 Subject: [PATCH 02/20] fix: fmt Signed-off-by: kobayu858 --- .../tests/test_sched_preempt/src/lib.rs | 40 ++++++++++++++----- awkernel_async_lib/src/task.rs | 25 +++++++++--- 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs index 95c74d3b5..ce423ee03 100644 --- a/applications/tests/test_sched_preempt/src/lib.rs +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -1,29 +1,42 @@ #![no_std] use awkernel_async_lib::{scheduler::SchedulerType, spawn, task::get_lowest_priority_task_info}; -use awkernel_lib::{cpu::cpu_id, delay::{wait_microsec, uptime}}; +use awkernel_lib::{ + cpu::cpu_id, + delay::{uptime, wait_microsec}, +}; /// Tests related to preemption between schedulers #[allow(dead_code)] enum TestType { - GetlowestTask + GetlowestTask, } -pub async fn run() { // TASK ID:1 +pub async fn run() { + // TASK ID:1 wait_microsec(100000); - log::info!("[{}] Start test_sched_preempt at cpu_id: {}", uptime(), cpu_id()); + log::info!( + "[{}] Start test_sched_preempt at cpu_id: {}", + uptime(), + cpu_id() + ); let test_type = TestType::GetlowestTask; match test_type { TestType::GetlowestTask => check_lowest_task().await, } - log::info!("[{}] End test_sched_preempt at cpu_id: {}", uptime(), cpu_id()); + log::info!( + "[{}] End test_sched_preempt at cpu_id: {}", + uptime(), + cpu_id() + ); } -async fn check_lowest_task(){ - spawn( // TASK ID:8 +async fn check_lowest_task() { + spawn( + // TASK ID:8 "GEDF".into(), async move { loop { @@ -35,7 +48,8 @@ async fn check_lowest_task(){ SchedulerType::GEDF(10000), ) .await; - spawn( // TASK ID:9 + spawn( + // TASK ID:9 "FIFO".into(), async move { loop { @@ -53,6 +67,12 @@ async fn check_lowest_task(){ // FIFO TASK ID:1 < FIFO TASK ID:9 < GEDF TASK ID:8 // FIFO TASK ID:1 is the lowest priority task if let Some((task_id, priority_info)) = get_lowest_priority_task_info() { - log::debug!("Task ID: {}, sched_priority: {:?}, task_priority: {:?}, cpu_id: {}", task_id, priority_info.scheduler_priority, priority_info.task_priority, priority_info.cpu_id); + log::debug!( + "Task ID: {}, sched_priority: {:?}, task_priority: {:?}, cpu_id: {}", + task_id, + priority_info.scheduler_priority, + priority_info.task_priority, + priority_info.cpu_id + ); } -} \ No newline at end of file +} diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 1db308429..e1a228ca2 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -962,12 +962,14 @@ pub fn get_scheduler_type_by_task_id(task_id: u32) -> Option { }) } +static NONE_TASK_PRIORITY: u64 = 256; + fn get_task_priority_by_task_id(task_id: u32) -> Option { match get_scheduler_type_by_task_id(task_id) { Some(SchedulerType::GEDF(_)) => Some(get_absolute_deadline_by_task_id(task_id)?), - Some(SchedulerType::FIFO) => Some(get_last_executed_by_task_id(task_id)?), + Some(SchedulerType::FIFO) => Some(NONE_TASK_PRIORITY), Some(SchedulerType::PrioritizedFIFO(priority)) => Some(priority as u64), - Some(SchedulerType::RR) => Some(get_last_executed_by_task_id(task_id)?), + Some(SchedulerType::RR) => Some(NONE_TASK_PRIORITY), Some(SchedulerType::PriorityBasedRR(priority)) => Some(priority as u64), _ => None, } @@ -1026,14 +1028,21 @@ pub fn panicking() { pub struct PriorityInfo { pub scheduler_priority: u8, pub task_priority: u64, + pub last_executed_time: u64, pub cpu_id: usize, } impl PriorityInfo { - fn new(scheduler_priority: u8, task_priority: u64, cpu_id: usize) -> Self { + fn new( + scheduler_priority: u8, + task_priority: u64, + last_executed_time: u64, + cpu_id: usize, + ) -> Self { Self { scheduler_priority, task_priority, + last_executed_time, cpu_id, } } @@ -1068,6 +1077,7 @@ impl Ord for PriorityInfo { } other => other, } + .then(other.last_executed_time.cmp(&self.last_executed_time)) .then(self.cpu_id.cmp(&other.cpu_id)) } } @@ -1077,11 +1087,13 @@ impl Eq for PriorityInfo {} fn create_priority_info(task_id: u32) -> Option { let scheduler_type = get_scheduler_type_by_task_id(task_id)?; let scheduler_priority = get_scheduler(scheduler_type).priority(); + let last_executed_time = get_last_executed_by_task_id(task_id)?; let task_priority = get_task_priority_by_task_id(task_id)?; Some(PriorityInfo::new( scheduler_priority, task_priority, + last_executed_time, awkernel_lib::cpu::cpu_id(), )) } @@ -1097,11 +1109,14 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { let task_id = task.load(Ordering::Relaxed); if let Some(priority_info) = create_priority_info(task_id) { - if lowest_task.as_ref().map_or(true, |(_, current)| &priority_info > current) { + if lowest_task + .as_ref() + .map_or(true, |(_, current)| &priority_info > current) + { lowest_task = Some((task_id, priority_info)); } } } lowest_task -} \ No newline at end of file +} From f98cb007df0ee1eec431c7efb7871ed54c582035 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:38:15 +0900 Subject: [PATCH 03/20] fix: remove cmp if Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index e1a228ca2..49a92b988 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1065,16 +1065,7 @@ impl PartialEq for PriorityInfo { impl Ord for PriorityInfo { fn cmp(&self, other: &Self) -> core::cmp::Ordering { match self.scheduler_priority.cmp(&other.scheduler_priority) { - core::cmp::Ordering::Equal => { - if self.scheduler_priority == 0 - || self.scheduler_priority == 1 - || self.scheduler_priority == 3 - { - self.task_priority.cmp(&other.task_priority) - } else { - other.task_priority.cmp(&self.task_priority) - } - } + core::cmp::Ordering::Equal => self.task_priority.cmp(&other.task_priority), other => other, } .then(other.last_executed_time.cmp(&self.last_executed_time)) From 75c20b3d13b982b6e8ca9984ca51291b0fdf9886 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:41:35 +0900 Subject: [PATCH 04/20] refactor: change of order Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 49a92b988..cbc9f0e38 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1064,12 +1064,11 @@ impl PartialEq for PriorityInfo { impl Ord for PriorityInfo { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - match self.scheduler_priority.cmp(&other.scheduler_priority) { - core::cmp::Ordering::Equal => self.task_priority.cmp(&other.task_priority), - other => other, - } - .then(other.last_executed_time.cmp(&self.last_executed_time)) - .then(self.cpu_id.cmp(&other.cpu_id)) + other.scheduler_priority + .cmp(&self.scheduler_priority) + .then_with(|| other.task_priority.cmp(&self.task_priority)) + .then_with(|| self.last_executed_time.cmp(&other.last_executed_time)) + .then_with(|| other.cpu_id.cmp(&self.cpu_id)) } } @@ -1102,7 +1101,7 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { if let Some(priority_info) = create_priority_info(task_id) { if lowest_task .as_ref() - .map_or(true, |(_, current)| &priority_info > current) + .map_or(true, |(_, current)| &priority_info < current) { lowest_task = Some((task_id, priority_info)); } From e332bda37c52ec66f07e586d542d83cb599bf9d2 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:42:58 +0900 Subject: [PATCH 05/20] refactor: add last_exe_time at eq Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index cbc9f0e38..bc77b484b 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1058,6 +1058,7 @@ impl PartialEq for PriorityInfo { fn eq(&self, other: &Self) -> bool { self.scheduler_priority == other.scheduler_priority && self.task_priority == other.task_priority + && self.last_executed_time == other.last_executed_time && self.cpu_id == other.cpu_id } } From 5084277b08299bf077cb63190909ca400d3d5c72 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:44:28 +0900 Subject: [PATCH 06/20] refactor: eq Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index bc77b484b..28918e447 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1077,12 +1077,11 @@ impl Eq for PriorityInfo {} fn create_priority_info(task_id: u32) -> Option { let scheduler_type = get_scheduler_type_by_task_id(task_id)?; - let scheduler_priority = get_scheduler(scheduler_type).priority(); - let last_executed_time = get_last_executed_by_task_id(task_id)?; let task_priority = get_task_priority_by_task_id(task_id)?; + let last_executed_time = get_last_executed_by_task_id(task_id)?; Some(PriorityInfo::new( - scheduler_priority, + get_scheduler(scheduler_type).priority(), task_priority, last_executed_time, awkernel_lib::cpu::cpu_id(), From 05a5cf216390ca695d285deaded78fc45d0df6fa Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:46:04 +0900 Subject: [PATCH 07/20] fix: fmt Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 28918e447..ce6082ed7 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1065,7 +1065,8 @@ impl PartialEq for PriorityInfo { impl Ord for PriorityInfo { fn cmp(&self, other: &Self) -> core::cmp::Ordering { - other.scheduler_priority + other + .scheduler_priority .cmp(&self.scheduler_priority) .then_with(|| other.task_priority.cmp(&self.task_priority)) .then_with(|| self.last_executed_time.cmp(&other.last_executed_time)) From 8aaed6dd8932c6be315bc83f0b63697cc191b08f Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 13:57:49 +0900 Subject: [PATCH 08/20] fix: fmt Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index ce6082ed7..fe6560932 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1102,7 +1102,7 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { if let Some(priority_info) = create_priority_info(task_id) { if lowest_task .as_ref() - .map_or(true, |(_, current)| &priority_info < current) + .is_none_or(|(_, current)| &priority_info < current) { lowest_task = Some((task_id, priority_info)); } From 665abc85a91a34331d03b3d2be33deaf3b2c4413 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Tue, 24 Dec 2024 14:01:38 +0900 Subject: [PATCH 09/20] fix: fmt clippy Signed-off-by: kobayu858 --- applications/tests/test_sched_preempt/src/lib.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs index ce423ee03..7405a1a0f 100644 --- a/applications/tests/test_sched_preempt/src/lib.rs +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -6,13 +6,12 @@ use awkernel_lib::{ delay::{uptime, wait_microsec}, }; -/// Tests related to preemption between schedulers - #[allow(dead_code)] enum TestType { GetlowestTask, } +/// Tests related to preemption between schedulers pub async fn run() { // TASK ID:1 wait_microsec(100000); From 4ff8455ab641b4ef0197499b1c7c076c22f17a62 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 14:06:18 +0900 Subject: [PATCH 10/20] refactor: remove last exe time and cpu id Signed-off-by: kobayu858 --- applications/tests/test_sched_preempt/src/lib.rs | 3 +-- awkernel_async_lib/src/task.rs | 13 ------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs index 7405a1a0f..0c162487f 100644 --- a/applications/tests/test_sched_preempt/src/lib.rs +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -67,11 +67,10 @@ async fn check_lowest_task() { // FIFO TASK ID:1 is the lowest priority task if let Some((task_id, priority_info)) = get_lowest_priority_task_info() { log::debug!( - "Task ID: {}, sched_priority: {:?}, task_priority: {:?}, cpu_id: {}", + "Task ID: {}, sched_priority: {:?}, task_priority: {:?}", task_id, priority_info.scheduler_priority, priority_info.task_priority, - priority_info.cpu_id ); } } diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index fe6560932..bf60256e0 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1028,22 +1028,16 @@ pub fn panicking() { pub struct PriorityInfo { pub scheduler_priority: u8, pub task_priority: u64, - pub last_executed_time: u64, - pub cpu_id: usize, } impl PriorityInfo { fn new( scheduler_priority: u8, task_priority: u64, - last_executed_time: u64, - cpu_id: usize, ) -> Self { Self { scheduler_priority, task_priority, - last_executed_time, - cpu_id, } } } @@ -1058,8 +1052,6 @@ impl PartialEq for PriorityInfo { fn eq(&self, other: &Self) -> bool { self.scheduler_priority == other.scheduler_priority && self.task_priority == other.task_priority - && self.last_executed_time == other.last_executed_time - && self.cpu_id == other.cpu_id } } @@ -1069,8 +1061,6 @@ impl Ord for PriorityInfo { .scheduler_priority .cmp(&self.scheduler_priority) .then_with(|| other.task_priority.cmp(&self.task_priority)) - .then_with(|| self.last_executed_time.cmp(&other.last_executed_time)) - .then_with(|| other.cpu_id.cmp(&self.cpu_id)) } } @@ -1079,13 +1069,10 @@ impl Eq for PriorityInfo {} fn create_priority_info(task_id: u32) -> Option { let scheduler_type = get_scheduler_type_by_task_id(task_id)?; let task_priority = get_task_priority_by_task_id(task_id)?; - let last_executed_time = get_last_executed_by_task_id(task_id)?; Some(PriorityInfo::new( get_scheduler(scheduler_type).priority(), task_priority, - last_executed_time, - awkernel_lib::cpu::cpu_id(), )) } From ece33ae04ba794dc8e6df569269d1b082adb03ae Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:09:41 +0900 Subject: [PATCH 11/20] refactor: decrease in lock range Signed-off-by: kobayu858 --- .../tests/test_sched_preempt/src/lib.rs | 9 +- awkernel_async_lib/src/scheduler/gedf.rs | 1 + awkernel_async_lib/src/task.rs | 118 ++++++++++-------- 3 files changed, 71 insertions(+), 57 deletions(-) diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs index 0c162487f..7645cc6aa 100644 --- a/applications/tests/test_sched_preempt/src/lib.rs +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -64,13 +64,12 @@ async fn check_lowest_task() { wait_microsec(1000000); // FIFO TASK ID:1 < FIFO TASK ID:9 < GEDF TASK ID:8 - // FIFO TASK ID:1 is the lowest priority task - if let Some((task_id, priority_info)) = get_lowest_priority_task_info() { + // In FIFO tasks, the task with the smaller CPU ID has the lowest priority. + if let Some((task_id, cpu_id, _)) = get_lowest_priority_task_info() { log::debug!( - "Task ID: {}, sched_priority: {:?}, task_priority: {:?}", + "Task ID: {}, cpu_id: {}", task_id, - priority_info.scheduler_priority, - priority_info.task_priority, + cpu_id, ); } } diff --git a/awkernel_async_lib/src/scheduler/gedf.rs b/awkernel_async_lib/src/scheduler/gedf.rs index d419388c1..10390d331 100644 --- a/awkernel_async_lib/src/scheduler/gedf.rs +++ b/awkernel_async_lib/src/scheduler/gedf.rs @@ -73,6 +73,7 @@ impl Scheduler for GEDFScheduler { let wake_time = awkernel_lib::delay::uptime(); let absolute_deadline = wake_time + relative_deadline; + task.priority.update_priority_info(self.priority, absolute_deadline); info.update_absolute_deadline(absolute_deadline); data.queue.push(GEDFTask { diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index bf60256e0..21f847c60 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -25,7 +25,7 @@ use awkernel_lib::{ unwind::catch_unwind, }; use core::{ - sync::atomic::{AtomicU32, Ordering}, + sync::atomic::{AtomicU32,AtomicU64, Ordering}, task::{Context, Poll}, }; use futures::{ @@ -48,6 +48,7 @@ pub type TaskResult = Result<(), Cow<'static, str>>; static TASKS: Mutex = Mutex::new(Tasks::new()); // Set of tasks. static RUNNING: [AtomicU32; NUM_MAX_CPU] = array![_ => AtomicU32::new(0); NUM_MAX_CPU]; // IDs of running tasks. +static NONE_TASK_PRIORITY: u64 = 256; /// Task has ID, future, information, and a reference to a scheduler. pub struct Task { @@ -56,6 +57,7 @@ pub struct Task { future: Mutex>>, pub info: Mutex, scheduler: &'static dyn Scheduler, + pub priority: PriorityInfo, } impl Task { @@ -237,12 +239,22 @@ impl Tasks { thread: None, }); + let task_priority = match scheduler_type { + SchedulerType::GEDF(_) => NONE_TASK_PRIORITY, // Update the priority in the GEDF scheduler. + SchedulerType::FIFO => NONE_TASK_PRIORITY, + SchedulerType::PrioritizedFIFO(priority) => priority as u64, + SchedulerType::RR => NONE_TASK_PRIORITY, + SchedulerType::PriorityBasedRR(priority) => priority as u64, + SchedulerType::Panicked => NONE_TASK_PRIORITY, + }; + let task = Task { name, future: Mutex::new(future), scheduler, id, info, + priority: PriorityInfo::new(scheduler.priority(), task_priority), }; e.insert(Arc::new(task)); @@ -962,19 +974,6 @@ pub fn get_scheduler_type_by_task_id(task_id: u32) -> Option { }) } -static NONE_TASK_PRIORITY: u64 = 256; - -fn get_task_priority_by_task_id(task_id: u32) -> Option { - match get_scheduler_type_by_task_id(task_id) { - Some(SchedulerType::GEDF(_)) => Some(get_absolute_deadline_by_task_id(task_id)?), - Some(SchedulerType::FIFO) => Some(NONE_TASK_PRIORITY), - Some(SchedulerType::PrioritizedFIFO(priority)) => Some(priority as u64), - Some(SchedulerType::RR) => Some(NONE_TASK_PRIORITY), - Some(SchedulerType::PriorityBasedRR(priority)) => Some(priority as u64), - _ => None, - } -} - #[inline(always)] pub fn get_absolute_deadline_by_task_id(task_id: u32) -> Option { let mut node = MCSNode::new(); @@ -1026,59 +1025,61 @@ pub fn panicking() { } pub struct PriorityInfo { - pub scheduler_priority: u8, - pub task_priority: u64, + pub priority: AtomicU64 } impl PriorityInfo { - fn new( - scheduler_priority: u8, - task_priority: u64, - ) -> Self { - Self { - scheduler_priority, - task_priority, + fn new(scheduler_priority: u8, task_priority: u64) -> Self { + assert!(task_priority < (1 << 56), "Task priority exceeds 56 bits"); + let combined_priority = ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); + PriorityInfo { + priority: AtomicU64::new(combined_priority), } } -} -impl PartialOrd for PriorityInfo { - fn partial_cmp(&self, other: &Self) -> Option { - Some(self.cmp(other)) + pub fn update_priority_info(&self, scheduler_priority: u8, task_priority: u64) { + let combined_priority = ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); + self.priority.store(combined_priority, Ordering::Relaxed); } } -impl PartialEq for PriorityInfo { - fn eq(&self, other: &Self) -> bool { - self.scheduler_priority == other.scheduler_priority - && self.task_priority == other.task_priority +impl Clone for PriorityInfo { + fn clone(&self) -> Self { + let value = self.priority.load(Ordering::Relaxed); + PriorityInfo { + priority: AtomicU64::new(value), + } } } -impl Ord for PriorityInfo { - fn cmp(&self, other: &Self) -> core::cmp::Ordering { - other - .scheduler_priority - .cmp(&self.scheduler_priority) - .then_with(|| other.task_priority.cmp(&self.task_priority)) +impl PartialEq for PriorityInfo { + fn eq(&self, other: &Self) -> bool { + self.priority.load(Ordering::Relaxed) + == other.priority.load(Ordering::Relaxed) } } impl Eq for PriorityInfo {} -fn create_priority_info(task_id: u32) -> Option { - let scheduler_type = get_scheduler_type_by_task_id(task_id)?; - let task_priority = get_task_priority_by_task_id(task_id)?; +impl PartialOrd for PriorityInfo { + fn partial_cmp(&self, other: &Self) -> Option { + self.priority + .load(Ordering::Relaxed) + .partial_cmp(&other.priority.load(Ordering::Relaxed)) + } +} - Some(PriorityInfo::new( - get_scheduler(scheduler_type).priority(), - task_priority, - )) +impl Ord for PriorityInfo { + fn cmp(&self, other: &Self) -> core::cmp::Ordering { + self.priority + .load(Ordering::Relaxed) + .cmp(&other.priority.load(Ordering::Relaxed)) + } } -pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { +pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { let num_cpus = awkernel_lib::cpu::num_cpu(); - let mut lowest_task: Option<(u32, PriorityInfo)> = None; // (task_id, priority_info) + let mut lowest_task: Option<(u32, usize, PriorityInfo)> = None; // (task_id, cpu_id, priority_info) for (cpu_id, task) in RUNNING.iter().enumerate() { if cpu_id >= num_cpus { @@ -1086,12 +1087,25 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, PriorityInfo)> { } let task_id = task.load(Ordering::Relaxed); - if let Some(priority_info) = create_priority_info(task_id) { - if lowest_task - .as_ref() - .is_none_or(|(_, current)| &priority_info < current) - { - lowest_task = Some((task_id, priority_info)); + if task_id == 0 { + continue; + } + + let priority_info = { + let mut node = MCSNode::new(); + let tasks = TASKS.lock(&mut node); + tasks.id_to_task.get(&task_id).map(|task| task.priority.clone()) + }; + + if let Some(priority_info) = priority_info { + match &lowest_task { + Some((_, _, lowest_priority_info)) if priority_info > *lowest_priority_info => { + lowest_task = Some((task_id, cpu_id, priority_info)); + } + None => { + lowest_task = Some((task_id, cpu_id, priority_info)); + } + _ => {} } } } From 2eb1b6ececf7d4202aa421adf08751f30de7bba3 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:11:45 +0900 Subject: [PATCH 12/20] refactor: fmt Signed-off-by: kobayu858 --- .../tests/test_sched_preempt/src/lib.rs | 6 +----- awkernel_async_lib/src/scheduler/gedf.rs | 3 ++- awkernel_async_lib/src/task.rs | 18 +++++++++++------- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/applications/tests/test_sched_preempt/src/lib.rs b/applications/tests/test_sched_preempt/src/lib.rs index 7645cc6aa..5b0c89cb5 100644 --- a/applications/tests/test_sched_preempt/src/lib.rs +++ b/applications/tests/test_sched_preempt/src/lib.rs @@ -66,10 +66,6 @@ async fn check_lowest_task() { // FIFO TASK ID:1 < FIFO TASK ID:9 < GEDF TASK ID:8 // In FIFO tasks, the task with the smaller CPU ID has the lowest priority. if let Some((task_id, cpu_id, _)) = get_lowest_priority_task_info() { - log::debug!( - "Task ID: {}, cpu_id: {}", - task_id, - cpu_id, - ); + log::debug!("Task ID: {}, cpu_id: {}", task_id, cpu_id,); } } diff --git a/awkernel_async_lib/src/scheduler/gedf.rs b/awkernel_async_lib/src/scheduler/gedf.rs index 10390d331..2881f86a5 100644 --- a/awkernel_async_lib/src/scheduler/gedf.rs +++ b/awkernel_async_lib/src/scheduler/gedf.rs @@ -73,7 +73,8 @@ impl Scheduler for GEDFScheduler { let wake_time = awkernel_lib::delay::uptime(); let absolute_deadline = wake_time + relative_deadline; - task.priority.update_priority_info(self.priority, absolute_deadline); + task.priority + .update_priority_info(self.priority, absolute_deadline); info.update_absolute_deadline(absolute_deadline); data.queue.push(GEDFTask { diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 21f847c60..4fcf41cc7 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -25,7 +25,7 @@ use awkernel_lib::{ unwind::catch_unwind, }; use core::{ - sync::atomic::{AtomicU32,AtomicU64, Ordering}, + sync::atomic::{AtomicU32, AtomicU64, Ordering}, task::{Context, Poll}, }; use futures::{ @@ -1025,20 +1025,22 @@ pub fn panicking() { } pub struct PriorityInfo { - pub priority: AtomicU64 + pub priority: AtomicU64, } impl PriorityInfo { fn new(scheduler_priority: u8, task_priority: u64) -> Self { assert!(task_priority < (1 << 56), "Task priority exceeds 56 bits"); - let combined_priority = ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); + let combined_priority = + ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); PriorityInfo { priority: AtomicU64::new(combined_priority), } } pub fn update_priority_info(&self, scheduler_priority: u8, task_priority: u64) { - let combined_priority = ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); + let combined_priority = + ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); self.priority.store(combined_priority, Ordering::Relaxed); } } @@ -1054,8 +1056,7 @@ impl Clone for PriorityInfo { impl PartialEq for PriorityInfo { fn eq(&self, other: &Self) -> bool { - self.priority.load(Ordering::Relaxed) - == other.priority.load(Ordering::Relaxed) + self.priority.load(Ordering::Relaxed) == other.priority.load(Ordering::Relaxed) } } @@ -1094,7 +1095,10 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { let priority_info = { let mut node = MCSNode::new(); let tasks = TASKS.lock(&mut node); - tasks.id_to_task.get(&task_id).map(|task| task.priority.clone()) + tasks + .id_to_task + .get(&task_id) + .map(|task| task.priority.clone()) }; if let Some(priority_info) = priority_info { From bf9fafc3f15272bb10c499e2dcc9588c432798d2 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:27:16 +0900 Subject: [PATCH 13/20] refactor: more simple Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 48 ++++++++++++++++------------------ 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 4fcf41cc7..bc82c54bd 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -239,13 +239,12 @@ impl Tasks { thread: None, }); + // Set the task priority. + // If the scheduler is GEDF, the task priority will update later. let task_priority = match scheduler_type { - SchedulerType::GEDF(_) => NONE_TASK_PRIORITY, // Update the priority in the GEDF scheduler. - SchedulerType::FIFO => NONE_TASK_PRIORITY, - SchedulerType::PrioritizedFIFO(priority) => priority as u64, - SchedulerType::RR => NONE_TASK_PRIORITY, - SchedulerType::PriorityBasedRR(priority) => priority as u64, - SchedulerType::Panicked => NONE_TASK_PRIORITY, + SchedulerType::PrioritizedFIFO(priority) + | SchedulerType::PriorityBasedRR(priority) => priority as u64, + _ => NONE_TASK_PRIORITY, }; let task = Task { @@ -1030,18 +1029,21 @@ pub struct PriorityInfo { impl PriorityInfo { fn new(scheduler_priority: u8, task_priority: u64) -> Self { - assert!(task_priority < (1 << 56), "Task priority exceeds 56 bits"); - let combined_priority = - ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); PriorityInfo { - priority: AtomicU64::new(combined_priority), + priority: AtomicU64::new(Self::combine_priority(scheduler_priority, task_priority)), } } pub fn update_priority_info(&self, scheduler_priority: u8, task_priority: u64) { - let combined_priority = - ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)); - self.priority.store(combined_priority, Ordering::Relaxed); + self.priority.store( + Self::combine_priority(scheduler_priority, task_priority), + Ordering::Relaxed, + ); + } + + fn combine_priority(scheduler_priority: u8, task_priority: u64) -> u64 { + assert!(task_priority < (1 << 56), "Task priority exceeds 56 bits"); + ((scheduler_priority as u64) << 56) | (task_priority & ((1 << 56) - 1)) } } @@ -1082,11 +1084,7 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { let num_cpus = awkernel_lib::cpu::num_cpu(); let mut lowest_task: Option<(u32, usize, PriorityInfo)> = None; // (task_id, cpu_id, priority_info) - for (cpu_id, task) in RUNNING.iter().enumerate() { - if cpu_id >= num_cpus { - break; - } - + for (cpu_id, task) in RUNNING.iter().enumerate().take(num_cpus) { let task_id = task.load(Ordering::Relaxed); if task_id == 0 { continue; @@ -1102,16 +1100,14 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { }; if let Some(priority_info) = priority_info { - match &lowest_task { - Some((_, _, lowest_priority_info)) if priority_info > *lowest_priority_info => { - lowest_task = Some((task_id, cpu_id, priority_info)); - } - None => { - lowest_task = Some((task_id, cpu_id, priority_info)); - } - _ => {} + if lowest_task + .as_ref() + .map_or(true, |(_, _, lowest_priority_info)| priority_info > *lowest_priority_info) + { + lowest_task = Some((task_id, cpu_id, priority_info)); } } + } lowest_task From 2a1b28e355e26411a07199d950a89bfb86ff5e97 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:30:32 +0900 Subject: [PATCH 14/20] fix: clippy Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index bc82c54bd..7e50d61b6 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1066,9 +1066,7 @@ impl Eq for PriorityInfo {} impl PartialOrd for PriorityInfo { fn partial_cmp(&self, other: &Self) -> Option { - self.priority - .load(Ordering::Relaxed) - .partial_cmp(&other.priority.load(Ordering::Relaxed)) + Some(self.cmp(other)) } } From 97713b1a91071b1aff149168c6ef0bfad317e8eb Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:34:03 +0900 Subject: [PATCH 15/20] fix: fmt Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 7e50d61b6..9c3546be1 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1100,12 +1100,13 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { if let Some(priority_info) = priority_info { if lowest_task .as_ref() - .map_or(true, |(_, _, lowest_priority_info)| priority_info > *lowest_priority_info) + .map_or(true, |(_, _, lowest_priority_info)| { + priority_info > *lowest_priority_info + }) { lowest_task = Some((task_id, cpu_id, priority_info)); } } - } lowest_task From 51ed596f21ebc03baa7227f075aa983f2793c269 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:40:44 +0900 Subject: [PATCH 16/20] refactor: NONE_TASK_PRIORITY Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 9c3546be1..3050b91f8 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -48,7 +48,7 @@ pub type TaskResult = Result<(), Cow<'static, str>>; static TASKS: Mutex = Mutex::new(Tasks::new()); // Set of tasks. static RUNNING: [AtomicU32; NUM_MAX_CPU] = array![_ => AtomicU32::new(0); NUM_MAX_CPU]; // IDs of running tasks. -static NONE_TASK_PRIORITY: u64 = 256; +static MAX_TASK_PRIORITY: u64 = (1 << 56) - 1; // Maximum task priority. /// Task has ID, future, information, and a reference to a scheduler. pub struct Task { @@ -244,7 +244,7 @@ impl Tasks { let task_priority = match scheduler_type { SchedulerType::PrioritizedFIFO(priority) | SchedulerType::PriorityBasedRR(priority) => priority as u64, - _ => NONE_TASK_PRIORITY, + _ => MAX_TASK_PRIORITY, }; let task = Task { From 81f16cfd85e6efea06b463b6dc3d7db63e6a86c8 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 20:48:43 +0900 Subject: [PATCH 17/20] fix: clippy Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 3050b91f8..60a3be6b1 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1100,9 +1100,7 @@ pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { if let Some(priority_info) = priority_info { if lowest_task .as_ref() - .map_or(true, |(_, _, lowest_priority_info)| { - priority_info > *lowest_priority_info - }) + .is_none_or(|(_, _, lowest_priority_info)| priority_info > *lowest_priority_info) { lowest_task = Some((task_id, cpu_id, priority_info)); } From 348056f23f11f90808b057fc2e794583f35a3018 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 21:14:53 +0900 Subject: [PATCH 18/20] refactor: decrease in lock range Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 41 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 60a3be6b1..d7e5edb5c 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1079,31 +1079,30 @@ impl Ord for PriorityInfo { } pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { - let num_cpus = awkernel_lib::cpu::num_cpu(); let mut lowest_task: Option<(u32, usize, PriorityInfo)> = None; // (task_id, cpu_id, priority_info) - for (cpu_id, task) in RUNNING.iter().enumerate().take(num_cpus) { - let task_id = task.load(Ordering::Relaxed); - if task_id == 0 { - continue; - } + let running_tasks = get_tasks_running(); - let priority_info = { - let mut node = MCSNode::new(); - let tasks = TASKS.lock(&mut node); - tasks - .id_to_task - .get(&task_id) - .map(|task| task.priority.clone()) - }; + let priority_infos: Vec<(u32, usize, PriorityInfo)> = { + let mut node = MCSNode::new(); + let tasks = TASKS.lock(&mut node); + running_tasks + .into_iter() + .filter_map(|task| { + tasks + .id_to_task + .get(&task.task_id) + .map(|task_data| (task.task_id, task.cpu_id, task_data.priority.clone())) + }) + .collect() + }; - if let Some(priority_info) = priority_info { - if lowest_task - .as_ref() - .is_none_or(|(_, _, lowest_priority_info)| priority_info > *lowest_priority_info) - { - lowest_task = Some((task_id, cpu_id, priority_info)); - } + for (task_id, cpu_id, priority_info) in priority_infos { + if lowest_task + .as_ref() + .is_none_or(|(_, _, lowest_priority_info)| priority_info > *lowest_priority_info) + { + lowest_task = Some((task_id, cpu_id, priority_info)); } } From 549f063dc74a26bcce9a8d08a24266db2885c976 Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Wed, 25 Dec 2024 21:36:54 +0900 Subject: [PATCH 19/20] refactor: decrease in lock range Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index d7e5edb5c..1108be577 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -1081,7 +1081,10 @@ impl Ord for PriorityInfo { pub fn get_lowest_priority_task_info() -> Option<(u32, usize, PriorityInfo)> { let mut lowest_task: Option<(u32, usize, PriorityInfo)> = None; // (task_id, cpu_id, priority_info) - let running_tasks = get_tasks_running(); + let running_tasks: Vec = get_tasks_running() + .into_iter() + .filter(|task| task.task_id != 0) + .collect(); let priority_infos: Vec<(u32, usize, PriorityInfo)> = { let mut node = MCSNode::new(); From 4c5573bf018c499a13aee3e8f7e3b2f2a83ae4ea Mon Sep 17 00:00:00 2001 From: kobayu858 Date: Thu, 26 Dec 2024 11:41:47 +0900 Subject: [PATCH 20/20] refactor: comment Signed-off-by: kobayu858 --- awkernel_async_lib/src/task.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/awkernel_async_lib/src/task.rs b/awkernel_async_lib/src/task.rs index 1108be577..7696e24be 100644 --- a/awkernel_async_lib/src/task.rs +++ b/awkernel_async_lib/src/task.rs @@ -240,7 +240,7 @@ impl Tasks { }); // Set the task priority. - // If the scheduler is GEDF, the task priority will update later. + // If the scheduler implements dynamic priority scheduling, the task priority will be updated later. let task_priority = match scheduler_type { SchedulerType::PrioritizedFIFO(priority) | SchedulerType::PriorityBasedRR(priority) => priority as u64,