diff --git a/crates/sovereign-sdk/adapters/risc0-bonsai/src/lib.rs b/crates/sovereign-sdk/adapters/risc0-bonsai/src/lib.rs index 1a6b8b2df..15cb3a716 100644 --- a/crates/sovereign-sdk/adapters/risc0-bonsai/src/lib.rs +++ b/crates/sovereign-sdk/adapters/risc0-bonsai/src/lib.rs @@ -10,9 +10,6 @@ use sov_rollup_interface::zk::Matches; #[cfg(feature = "native")] pub mod host; -#[cfg(feature = "bench")] -pub mod metrics; - /// Uniquely identifies a Risc0 binary. Roughly equivalent to /// the hash of the ELF file. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] diff --git a/crates/sovereign-sdk/adapters/risc0-bonsai/src/metrics.rs b/crates/sovereign-sdk/adapters/risc0-bonsai/src/metrics.rs deleted file mode 100644 index d217f2b35..000000000 --- a/crates/sovereign-sdk/adapters/risc0-bonsai/src/metrics.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Defines utilities for collecting runtime metrics from inside a Risc0 VM -use std::collections::HashMap; - -use anyhow::Context; -use once_cell::sync::Lazy; -use parking_lot::Mutex; -use risc0_zkvm::Bytes; - -/// A global hashmap mapping metric names to their values. -pub static GLOBAL_HASHMAP: Lazy>> = - Lazy::new(|| Mutex::new(HashMap::new())); - -/// Increments the requested metric by the given value, creating a -/// new entry in the global map if necessary. -fn add_value(metric: String, value: u64) { - let mut hashmap = GLOBAL_HASHMAP.lock(); - hashmap - .entry(metric) - .and_modify(|(sum, count)| { - *sum += value; - *count += 1; - }) - .or_insert((value, 1)); -} - -/// Deserialize a `Bytes` into a null-separated `(String, u64)` tuple. This function -/// expects its arguments to match the format of arguments to Risc0's io callbacks. -fn deserialize_custom(serialized: Bytes) -> Result<(String, u64), anyhow::Error> { - let null_pos = serialized - .iter() - .position(|&b| b == 0) - .context("Could not find separator in provided bytes")?; - let (string_bytes, size_bytes_with_null) = serialized.split_at(null_pos); - let size_bytes = &size_bytes_with_null[1..]; // Skip the null terminator - let string = String::from_utf8(string_bytes.to_vec())?; - let size = u64::from_ne_bytes(size_bytes.try_into()?); // Convert bytes back into usize - Ok((string, size)) -} - -/// A custom callback for extracting metrics from the Risc0 zkvm. -/// -/// When the "bench" feature is enabled, this callback is registered as a syscall -/// in the Risc0 VM and invoked whenever a function annotated with the [`sov-zk-cycle-utils::cycle_tracker`] -/// macro is invoked. -pub fn metrics_callback(input: Bytes) -> Result { - let met_tuple = deserialize_custom(input)?; - add_value(met_tuple.0, met_tuple.1); - Ok(Bytes::new()) -}