Skip to content

Commit

Permalink
Introduce current the read counters SBI call
Browse files Browse the repository at this point in the history
The READ_COUNTERS SBI call returns the performance counters as recorded
by the benchmark module. The specific ABI and counters returns depend
on the benchmark module compiled with Miralis.


Co-authored-by: Charly Castes <charly.castes@epfl.ch>
  • Loading branch information
francois141 and CharlyCst committed Jan 16, 2025
1 parent 5fba489 commit a978dc0
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 7 deletions.
2 changes: 2 additions & 0 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ pub mod abi {
pub const MIRALIS_LOG_FID: usize = 2;
/// Benchmark prints and exit.
pub const MIRALIS_BENCHMARK_FID: usize = 3;
/// Returns the performance counters managed by Miralis.
pub const MIRALIS_READ_COUNTERS_FID: usize = 4;

/// Log level constants, with the same semantic as the `log` crate.
pub mod log {
Expand Down
17 changes: 16 additions & 1 deletion src/benchmark/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
//! the number of instruction for example.
use spin::Mutex;

use crate::arch::{Arch, Architecture, Csr};
use crate::arch::{Arch, Architecture, Csr, Register};
use crate::benchmark::{BenchmarkModule, Counter, Scope};
use crate::config;
use crate::platform::{Plat, Platform};
use crate::virt::traits::*;
use crate::virt::VirtContext;

#[macro_export]
macro_rules! _benchmark_print {
Expand Down Expand Up @@ -180,6 +182,19 @@ impl BenchmarkModule for DefaultBenchmark {
BENCH.lock().counters[index] += 1;
}

fn read_counters(ctx: &mut VirtContext) {
let benchmark = BENCH.lock();

ctx.set(
Register::X10,
benchmark.counters[Counter::FirmwareExits as usize],
);
ctx.set(
Register::X11,
benchmark.counters[Counter::WorldSwitches as usize],
);
}

/// Print formated string with value of the counters
fn display_counters() {
let bench = BENCH.lock();
Expand Down
3 changes: 3 additions & 0 deletions src/benchmark/empty.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::benchmark::default::IntervalCounter;
use crate::benchmark::{BenchmarkModule, Counter, Scope};
use crate::virt::VirtContext;

pub struct EmptyBenchmark {}

Expand All @@ -26,6 +27,8 @@ impl BenchmarkModule for EmptyBenchmark {
) {
}

fn read_counters(_ctx: &mut VirtContext) {}

fn display_counters() {}

fn get_counter_value(_counter: Counter) -> usize {
Expand Down
7 changes: 7 additions & 0 deletions src/benchmark/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod empty;
use config_select::select_env;

use crate::benchmark::default::IntervalCounter;
use crate::virt::VirtContext;

pub type Benchmark = select_env!["MIRALIS_BENCHMARK_TYPE":
"default" => default::DefaultBenchmark
Expand All @@ -28,9 +29,15 @@ pub trait BenchmarkModule {
scope: &Scope,
value: usize,
);

/// Print formated string with value of the counters
fn display_counters();

/// Read the performance counters into the virtual registers.
///
/// Note: the specific ABI is depends on the benchmark back-end.
fn read_counters(ctx: &mut VirtContext);

fn get_counter_value(counter: Counter) -> usize;
}

Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub const TARGET_PAYLOAD_ADDRESS: usize =
pub const TARGET_STACK_SIZE: usize =
parse_usize_or(option_env!("MIRALIS_TARGET_STACK_SIZE"), 0x8000);

/// The choosen policy name
/// The chosen policy name
///
/// For now this variable is unused, but we keep it still to force re-compilation when the policy
/// name changes. We can get rid of it once it becomes possible to track dependencies on
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub unsafe fn main_loop(ctx: &mut VirtContext, mctx: &mut MiralisContext, policy
Benchmark::stop_interval_counters(Scope::RunVCPU);
Benchmark::start_interval_counters(Scope::HandleTrap);

if handle_trap(ctx, mctx, policy) == ExitResult::Donne {
if handle_trap(ctx, mctx, policy) == ExitResult::Done {
return;
}

Expand Down
11 changes: 7 additions & 4 deletions src/virt/emulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub enum ExitResult {
/// Continue execution of the virtual firmware or payload.
Continue,
/// Terminate execution successfully.
Donne,
Done,
}

impl VirtContext {
Expand Down Expand Up @@ -510,7 +510,7 @@ impl VirtContext {
log::info!("Success!");
log::info!("Number of exits: {}", self.nb_exits);
// Terminate execution
return ExitResult::Donne;
return ExitResult::Done;
}
abi::MIRALIS_LOG_FID => {
let log_level = self.get(Register::X10);
Expand All @@ -533,18 +533,21 @@ impl VirtContext {
}
}

// For now we don't return error code or the lenght written
// For now we don't return error code or the length written
self.set(Register::X10, 0);
self.set(Register::X11, 0);
self.pc += 4;
}
abi::MIRALIS_BENCHMARK_FID => {
Benchmark::display_counters();
Plat::exit_success();
}
abi::MIRALIS_READ_COUNTERS_FID => {
Benchmark::read_counters(self);
}
_ => panic!("Invalid Miralis FID: 0x{:x}", fid),
}

self.pc += 4;
ExitResult::Continue
}
}
Expand Down

0 comments on commit a978dc0

Please sign in to comment.