-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bench)!: move benchmarking facilities to their own crate
- Loading branch information
1 parent
a17549f
commit 5ced699
Showing
15 changed files
with
130 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 = "riot-rs-bench" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
repository.workspace = true | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
cfg-if = { workspace = true } | ||
|
||
[target.'cfg(context = "cortex-m")'.dependencies] | ||
cortex-m = { workspace = true, features = ["critical-section-single-core"] } |
This file contains 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,36 @@ | ||
use cortex_m::{ | ||
peripheral::{syst::SystClkSource, SYST}, | ||
Peripherals, | ||
}; | ||
|
||
use crate::Error; | ||
|
||
#[allow(missing_docs)] | ||
pub fn benchmark<F: Fn() -> ()>(iterations: usize, f: F) -> Result<usize, Error> { | ||
let mut p = unsafe { Peripherals::steal() }; | ||
// | ||
p.SCB.clear_sleepdeep(); | ||
|
||
// | ||
p.SYST.set_clock_source(SystClkSource::Core); | ||
p.SYST.set_reload(0x00FFFFFF); | ||
p.SYST.clear_current(); | ||
p.SYST.enable_counter(); | ||
|
||
// Wait for the system timer to be ready | ||
while SYST::get_current() == 0 {} | ||
|
||
let before = SYST::get_current(); | ||
|
||
for _ in 0..iterations { | ||
f(); | ||
} | ||
|
||
let total = before - SYST::get_current(); | ||
|
||
if p.SYST.has_wrapped() { | ||
Err(Error::SystemTimerWrapped) | ||
} else { | ||
Ok(total as usize / iterations) | ||
} | ||
} |
This file contains 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,55 @@ | ||
//! Provides on-board benchmarking facilities. | ||
|
||
#![cfg_attr(not(test), no_std)] | ||
#![feature(error_in_core)] | ||
#![deny(clippy::pedantic)] | ||
#![deny(missing_docs)] | ||
|
||
cfg_if::cfg_if! { | ||
if #[cfg(context = "cortex-m")] { | ||
mod cortexm; | ||
use cortexm as bench; | ||
} | ||
else if #[cfg(context = "riot-rs")] { | ||
// When run with laze but the architecture is not supported | ||
compile_error!("benchmarking is not supported for this architecture"); | ||
} else { | ||
// Provide a default bench module, for arch-independent tooling | ||
mod bench { | ||
use crate::Error; | ||
|
||
/// Benchmarks "time" required to run the provided function. | ||
/// | ||
/// Runs the provided function `iterations` times, and returns the mean number of system timer | ||
/// increments per iteration. | ||
/// | ||
/// # Errors | ||
/// | ||
/// Returns [`Error::SystemTimerWrapped`] if the system timer counter has wrapped when | ||
/// benchmarking. | ||
#[allow(unused_variables)] | ||
pub fn benchmark<F: Fn()>(iterations: usize, f: F) -> Result<usize, Error> { | ||
unimplemented!(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
pub use bench::benchmark; | ||
|
||
/// Possible errors happening when benchmarking. | ||
#[derive(Debug)] | ||
pub enum Error { | ||
/// The system timer wrapped when benchmarking. | ||
SystemTimerWrapped, | ||
} | ||
|
||
impl core::fmt::Display for Error { | ||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { | ||
match self { | ||
Self::SystemTimerWrapped => write!(f, "system timer wrapped"), | ||
} | ||
} | ||
} | ||
|
||
impl core::error::Error for Error {} |
This file contains 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 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 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 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 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 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 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