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
Open
Kanata9020
wants to merge
1
commit into
tier4:main
Choose a base branch
from
azu-lab:task-dvfs-freq
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| #![no_std] | ||
|
|
||
| extern crate alloc; | ||
|
|
||
| use alloc::vec::Vec; | ||
| 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}, | ||
| 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; | ||
| } | ||
|
|
||
| // Check that each worker CPU (CPU#0 runs no task) requested the minimum | ||
| // frequency when idle. Read the scheduler's records instead of running a | ||
| // task on each CPU, which would change its frequency. Read them all before | ||
| // logging, because the logger task resets the record of the CPU it runs on. | ||
| let self_cpu = awkernel_lib::cpu::cpu_id(); | ||
| let requested: Vec<bool> = (1..num_cpu()) | ||
| .map(task::is_idle_min_freq_requested) | ||
| .collect(); | ||
|
|
||
| for (core, requested) in (1..num_cpu()).zip(requested) { | ||
| if core == self_cpu { | ||
| log::info!("after all tasks: CPU#{core} is running this test, skipped"); | ||
| } else if requested { | ||
| log::info!("after all tasks: CPU#{core} requested the minimum frequency when idle"); | ||
| } else { | ||
| log::error!( | ||
| "after all tasks: CPU#{core} did not request the minimum frequency when idle" | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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(()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = [] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Activate the
dvfsfeature?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's already enabled in userland: the
test_dvfs_taskfeature inuserland/Cargo.tomlenablesawkernel/dvfs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it is enabled in userland, this example needs the
awkernel_async_lib/dvfsfeature to compile. Thus, I think it is a good practice to also activate it here.Now, you are using Awkernel's
userland, which does all the feature heavy-lifting. However, if you also activate the feature here, third-party users could also use you example on their custom userlands.