use crate::benchmark::{
BenchmarkCounters, BenchmarkMarkerError, DEFAULT_PREFIX, format_marker, try_format_marker,
};
const WASM_PAGE_BYTES: u128 = 65_536;
pub fn measure(label: &str) {
ic_platform::ic0::debug_print(format_marker(DEFAULT_PREFIX, label, counters()).as_bytes());
}
pub fn try_measure(label: &str) -> Result<(), BenchmarkMarkerError> {
let marker = try_format_marker(DEFAULT_PREFIX, label, counters())?;
ic_platform::ic0::debug_print(marker.as_bytes());
Ok(())
}
#[must_use]
pub fn counters() -> BenchmarkCounters {
BenchmarkCounters {
instructions: u128::from(ic_platform::ic0::performance_counter(1)),
heap_bytes: wasm_memory_size_bytes(),
memory_bytes: u128::from(ic_platform::ic0::stable64_size()) * WASM_PAGE_BYTES,
}
}
fn wasm_memory_size_bytes() -> u128 {
u128::try_from(std::arch::wasm32::memory_size(0)).expect("usize fits into u128")
* WASM_PAGE_BYTES
}