Skip to content

Commit 9a61fef

Browse files
Introduce current the read counters SBI call
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>
1 parent 5fba489 commit 9a61fef

File tree

7 files changed

+37
-7
lines changed

7 files changed

+37
-7
lines changed

crates/core/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ pub mod abi {
2424
pub const MIRALIS_LOG_FID: usize = 2;
2525
/// Benchmark prints and exit.
2626
pub const MIRALIS_BENCHMARK_FID: usize = 3;
27+
/// Returns the performance counters managed by Miralis.
28+
pub const MIRALIS_READ_COUNTERS_FID: usize = 4;
2729

2830
/// Log level constants, with the same semantic as the `log` crate.
2931
pub mod log {

src/benchmark/default.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
//! the number of instruction for example.
55
use spin::Mutex;
66

7-
use crate::arch::{Arch, Architecture, Csr};
7+
use crate::arch::{Arch, Architecture, Csr, Register};
88
use crate::benchmark::{BenchmarkModule, Counter, Scope};
99
use crate::config;
1010
use crate::platform::{Plat, Platform};
11+
use crate::virt::traits::*;
12+
use crate::virt::VirtContext;
1113

1214
#[macro_export]
1315
macro_rules! _benchmark_print {
@@ -180,6 +182,19 @@ impl BenchmarkModule for DefaultBenchmark {
180182
BENCH.lock().counters[index] += 1;
181183
}
182184

185+
fn read_counters(ctx: &mut VirtContext) {
186+
let benchmark = BENCH.lock();
187+
188+
ctx.set(
189+
Register::X10,
190+
benchmark.counters[Counter::FirmwareExits as usize],
191+
);
192+
ctx.set(
193+
Register::X11,
194+
benchmark.counters[Counter::WorldSwitches as usize],
195+
);
196+
}
197+
183198
/// Print formated string with value of the counters
184199
fn display_counters() {
185200
let bench = BENCH.lock();

src/benchmark/empty.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::benchmark::default::IntervalCounter;
22
use crate::benchmark::{BenchmarkModule, Counter, Scope};
3+
use crate::virt::VirtContext;
34

45
pub struct EmptyBenchmark {}
56

@@ -26,6 +27,8 @@ impl BenchmarkModule for EmptyBenchmark {
2627
) {
2728
}
2829

30+
fn read_counters(_ctx: &mut VirtContext) {}
31+
2932
fn display_counters() {}
3033

3134
fn get_counter_value(_counter: Counter) -> usize {

src/benchmark/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ mod empty;
88
use config_select::select_env;
99

1010
use crate::benchmark::default::IntervalCounter;
11+
use crate::virt::VirtContext;
1112

1213
pub type Benchmark = select_env!["MIRALIS_BENCHMARK_TYPE":
1314
"default" => default::DefaultBenchmark
@@ -28,9 +29,15 @@ pub trait BenchmarkModule {
2829
scope: &Scope,
2930
value: usize,
3031
);
32+
3133
/// Print formated string with value of the counters
3234
fn display_counters();
3335

36+
/// Read the performance counters into the virtual registers.
37+
///
38+
/// Note: the specific ABI is depends on the benchmark back-end.
39+
fn read_counters(ctx: &mut VirtContext);
40+
3441
fn get_counter_value(counter: Counter) -> usize;
3542
}
3643

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ pub const TARGET_PAYLOAD_ADDRESS: usize =
103103
pub const TARGET_STACK_SIZE: usize =
104104
parse_usize_or(option_env!("MIRALIS_TARGET_STACK_SIZE"), 0x8000);
105105

106-
/// The choosen policy name
106+
/// The chosen policy name
107107
///
108108
/// For now this variable is unused, but we keep it still to force re-compilation when the policy
109109
/// name changes. We can get rid of it once it becomes possible to track dependencies on

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ pub unsafe fn main_loop(ctx: &mut VirtContext, mctx: &mut MiralisContext, policy
5151
Benchmark::stop_interval_counters(Scope::RunVCPU);
5252
Benchmark::start_interval_counters(Scope::HandleTrap);
5353

54-
if handle_trap(ctx, mctx, policy) == ExitResult::Donne {
54+
if handle_trap(ctx, mctx, policy) == ExitResult::Done {
5555
return;
5656
}
5757

src/virt/emulator.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub enum ExitResult {
2626
/// Continue execution of the virtual firmware or payload.
2727
Continue,
2828
/// Terminate execution successfully.
29-
Donne,
29+
Done,
3030
}
3131

3232
impl VirtContext {
@@ -510,7 +510,7 @@ impl VirtContext {
510510
log::info!("Success!");
511511
log::info!("Number of exits: {}", self.nb_exits);
512512
// Terminate execution
513-
return ExitResult::Donne;
513+
return ExitResult::Done;
514514
}
515515
abi::MIRALIS_LOG_FID => {
516516
let log_level = self.get(Register::X10);
@@ -533,18 +533,21 @@ impl VirtContext {
533533
}
534534
}
535535

536-
// For now we don't return error code or the lenght written
536+
// For now we don't return error code or the length written
537537
self.set(Register::X10, 0);
538538
self.set(Register::X11, 0);
539-
self.pc += 4;
540539
}
541540
abi::MIRALIS_BENCHMARK_FID => {
542541
Benchmark::display_counters();
543542
Plat::exit_success();
544543
}
544+
abi::MIRALIS_READ_COUNTERS_FID => {
545+
Benchmark::read_counters(self);
546+
}
545547
_ => panic!("Invalid Miralis FID: 0x{:x}", fid),
546548
}
547549

550+
self.pc += 4;
548551
ExitResult::Continue
549552
}
550553
}

0 commit comments

Comments
 (0)