Repository navigation
feat(task): add per-task CPU frequency #808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| [package] | ||
| name = "test_dvfs_task" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| log = "0.4" | ||
|
|
||
| [dependencies.awkernel_async_lib] | ||
| path = "../../../awkernel_async_lib" | ||
| default-features = false | ||
|
|
||
| [dependencies.awkernel_lib] | ||
| path = "../../../awkernel_lib" | ||
| default-features = false | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| use core::{ | ||
| sync::atomic::{AtomicUsize, Ordering}, | ||
| time::Duration, | ||
| }; | ||
|
|
||
| use awkernel_async_lib::{ | ||
| scheduler::SchedulerType, | ||
| task::{self, TaskResult}, | ||
| }; | ||
| use awkernel_lib::{ | ||
| cpu::{CpuSet, num_cpu}, | ||
| dvfs::{get_curr_cpu_freq, get_max_cpu_freq, get_min_cpu_freq}, | ||
| time::Instant, | ||
| }; | ||
|
|
||
| const NUM_LOOP: usize = 100_000_000; | ||
| const NUM_ROUND: usize = 10; | ||
| const NUM_TASKS: usize = 3; | ||
|
|
||
| /// Number of finished `busy_loop` tasks. | ||
| static FINISHED: AtomicUsize = AtomicUsize::new(0); | ||
|
|
||
| pub async fn run() { | ||
| if num_cpu() < 7 { | ||
| log::error!("need at least 7 CPUs"); | ||
| return; | ||
| } | ||
|
|
||
| let max = match get_max_cpu_freq() { | ||
| Ok(f) => f, | ||
| Err(e) => { | ||
| log::error!("get_max_cpu_freq failed: {e:?}"); | ||
| return; | ||
| } | ||
| }; | ||
|
|
||
| task::spawn( | ||
| "dvfs_task_none".into(), | ||
| busy_loop(1, None), | ||
| SchedulerType::EDF(1_000_000, CpuSet::empty().with(2)), | ||
| ); | ||
|
|
||
| task::spawn_with_freq( | ||
| "dvfs_task_half".into(), | ||
| busy_loop(2, Some(max / 2)), | ||
| SchedulerType::EDF(1_000_000, CpuSet::empty().with(4)), | ||
| max / 2, | ||
| ); | ||
|
|
||
| task::spawn_with_freq( | ||
| "dvfs_task_max".into(), | ||
| busy_loop(3, Some(max)), | ||
| SchedulerType::EDF(1_000_000, CpuSet::empty().with(6)), | ||
| max, | ||
| ); | ||
|
|
||
| // Wait for all tasks to finish. | ||
| while FINISHED.load(Ordering::Relaxed) < NUM_TASKS { | ||
| awkernel_async_lib::sleep(Duration::from_millis(100)).await; | ||
| } | ||
|
|
||
| // Measure the frequency of each worker CPU, one by one. | ||
| // CPU#0 is the primary CPU and does not run tasks. | ||
| let min = get_min_cpu_freq(); | ||
| for core in 1..num_cpu() { | ||
| let handle = awkernel_async_lib::spawn( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probe is spawned without a frequency request, so run_main() requests the maximum frequency before the probe calls get_curr_cpu_freq(). The reading is taken while the probe is running, so it can’t tell us whether the scheduler requested the minimum frequency when the CPU went idle. Could we record the result of set_min_cpu_freq() in the idle path and check it afterward instead? |
||
| "dvfs_task_measure".into(), | ||
| async move { | ||
| match get_curr_cpu_freq() { | ||
| Ok(f) => log::info!("after all tasks: CPU#{core} = {f} Hz (min = {min:?})"), | ||
| Err(e) => log::warn!("after all tasks: CPU#{core}: {e:?}"), | ||
| } | ||
| }, | ||
| SchedulerType::EDF(1_000_000, CpuSet::empty().with(core)), | ||
| ) | ||
| .await; | ||
| let _ = handle.join().await; | ||
| } | ||
| } | ||
|
|
||
| async fn busy_loop(label: u32, expected: Option<u64>) -> TaskResult { | ||
| for _ in 0..NUM_ROUND { | ||
| // Measure while keeping interrupts (and thus preemption) disabled. | ||
| let int_guard = awkernel_lib::interrupt::InterruptGuard::new(); | ||
| let cpuid = awkernel_lib::cpu::cpu_id(); | ||
| let start = Instant::now(); | ||
| for _ in 0..NUM_LOOP { | ||
| core::hint::black_box(()); | ||
| } | ||
| let t = start.elapsed(); | ||
| let current = get_curr_cpu_freq(); | ||
| drop(int_guard); | ||
|
|
||
| // Log outside the guard. | ||
| match current { | ||
| Ok(current) => log::info!( | ||
| "dvfs_task {label}: CPU#{cpuid}, current = {current}, expected = {expected:?}, time = {t:?}" | ||
| ), | ||
| Err(e) => log::warn!("dvfs_task {label}: get_curr_cpu_freq failed: {e:?}"), | ||
| } | ||
|
|
||
| awkernel_async_lib::r#yield().await; | ||
| } | ||
| log::info!("dvfs_task {label} finished"); | ||
| FINISHED.fetch_add(1, Ordering::Relaxed); | ||
| Ok(()) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,3 +44,4 @@ spinlock = ["awkernel_lib/spinlock"] | |
| clippy = [] | ||
| perf = [] | ||
| period-index-propagation = ["perf"] | ||
| dvfs = [] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,6 +186,9 @@ pub struct TaskInfo { | |
| panicked: bool, | ||
| pub(crate) dag_info: Option<DagInfo>, | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| freq: Option<u64>, | ||
|
|
||
| #[cfg(not(feature = "no_preempt"))] | ||
| thread: Option<PtrWorkerThreadContext>, | ||
| } | ||
|
|
@@ -252,6 +255,18 @@ impl TaskInfo { | |
| pub fn get_dag_info(&self) -> Option<DagInfo> { | ||
| self.dag_info.clone() | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| #[cfg(feature = "dvfs")] | ||
| pub fn update_freq(&mut self, freq: u64) { | ||
| self.freq = Some(freq); | ||
| } | ||
|
|
||
| #[inline(always)] | ||
| #[cfg(feature = "dvfs")] | ||
| pub fn get_freq(&self) -> Option<u64> { | ||
| self.freq | ||
| } | ||
| } | ||
|
|
||
| /// State of task. | ||
|
|
@@ -294,6 +309,7 @@ impl Tasks { | |
| scheduler: &'static dyn Scheduler, | ||
| mut scheduler_type: SchedulerType, | ||
| dag_info: Option<DagInfo>, | ||
| #[cfg(feature = "dvfs")] freq: Option<u64>, | ||
| ) -> u32 { | ||
| let mut id = self.candidate_id; | ||
| loop { | ||
|
|
@@ -337,6 +353,9 @@ impl Tasks { | |
| panicked: false, | ||
| dag_info, | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| freq, | ||
|
|
||
| #[cfg(not(feature = "no_preempt"))] | ||
| thread: None, | ||
| }); | ||
|
|
@@ -403,7 +422,14 @@ pub fn spawn( | |
| future: impl Future<Output = TaskResult> + 'static + Send, | ||
| sched_type: SchedulerType, | ||
| ) -> u32 { | ||
| inner_spawn(name, future, sched_type, None) | ||
| inner_spawn( | ||
| name, | ||
| future, | ||
| sched_type, | ||
| None, | ||
| #[cfg(feature = "dvfs")] | ||
| None, | ||
| ) | ||
| } | ||
|
|
||
| /// Spawn a detached task with DAG information. | ||
|
|
@@ -433,14 +459,32 @@ pub fn spawn_with_dag_info( | |
| sched_type: SchedulerType, | ||
| dag_info: DagInfo, | ||
| ) -> u32 { | ||
| inner_spawn(name, future, sched_type, Some(dag_info)) | ||
| inner_spawn( | ||
| name, | ||
| future, | ||
| sched_type, | ||
| Some(dag_info), | ||
| #[cfg(feature = "dvfs")] | ||
| None, | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| pub fn spawn_with_freq( | ||
| name: Cow<'static, str>, | ||
| future: impl Future<Output = TaskResult> + 'static + Send, | ||
| sched_type: SchedulerType, | ||
| freq: u64, | ||
| ) -> u32 { | ||
| inner_spawn(name, future, sched_type, None, Some(freq)) | ||
| } | ||
|
Kanata9020 marked this conversation as resolved.
Resolved
|
||
|
|
||
| pub fn inner_spawn( | ||
| name: Cow<'static, str>, | ||
| future: impl Future<Output = TaskResult> + 'static + Send, | ||
| sched_type: SchedulerType, | ||
| dag_info: Option<DagInfo>, | ||
| #[cfg(feature = "dvfs")] freq: Option<u64>, | ||
| ) -> u32 { | ||
| if let SchedulerType::PrioritizedFIFO(p) | SchedulerType::PrioritizedRR(p) = sched_type { | ||
| if p > HIGHEST_PRIORITY { | ||
|
|
@@ -456,7 +500,15 @@ pub fn inner_spawn( | |
|
|
||
| let mut node = MCSNode::new(); | ||
| let mut tasks = TASKS.lock(&mut node); | ||
| let id = tasks.spawn(name, future.fuse(), scheduler, sched_type, dag_info); | ||
| let id = tasks.spawn( | ||
| name, | ||
| future.fuse(), | ||
| scheduler, | ||
| sched_type, | ||
| dag_info, | ||
| #[cfg(feature = "dvfs")] | ||
| freq, | ||
| ); | ||
| let task = tasks.id_to_task.get(&id).cloned(); | ||
| drop(tasks); | ||
|
|
||
|
|
@@ -1297,6 +1349,9 @@ pub fn run_main() { | |
| continue; | ||
| } | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| let freq; | ||
|
|
||
| { | ||
| let mut node = MCSNode::new(); | ||
| let mut info = task.info.lock(&mut node); | ||
|
|
@@ -1307,6 +1362,25 @@ pub fn run_main() { | |
| } | ||
|
|
||
| info.update_last_executed(); | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| { | ||
| freq = info.freq; | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "dvfs")] | ||
| match freq { | ||
| Some(f) => { | ||
| if let Err(e) = awkernel_lib::dvfs::set_cpu_freq_clamped(f) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we also apply the incoming task’s frequency policy when resuming a preempted task? This code only runs before Future::poll. Both yield_and_pool(ctx) in run_main() and the direct switch to a saved context in preempt.rs bypass it. For example, if a max-frequency task preempts a task running at half speed, the latter can resume without its half-speed request being reapplied. Both switch paths should apply the incoming task’s requested frequency, or max if it has no explicit request. |
||
| log::error!("CPU#{cpu_id}: failed to set the frequency to {f} Hz: {e}"); | ||
| } | ||
| } | ||
| None => { | ||
| if let Err(e) = awkernel_lib::dvfs::set_max_cpu_freq() { | ||
| log::error!("CPU#{cpu_id}: failed to set the frequency to max: {e}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Use the primary memory allocator. | ||
|
|
@@ -1415,6 +1489,12 @@ pub fn run_main() { | |
| #[cfg(feature = "perf")] | ||
| perf::start_idle(); | ||
|
|
||
| // No runnable task. Lower the frequency to the minimum while idle. | ||
| #[cfg(feature = "dvfs")] | ||
| if let Err(e) = awkernel_lib::dvfs::set_min_cpu_freq() { | ||
| log::error!("CPU#{cpu_id}: failed to set the frequency to min: {e}"); | ||
| } | ||
|
|
||
| awkernel_lib::cpu::sleep_cpu(None); | ||
| } | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.