From 0a11eb3a3d0525a6d7179224a04daa9fec00fb4f Mon Sep 17 00:00:00 2001 From: Andriy Berestovskyy Date: Mon, 11 Dec 2023 12:42:59 +0000 Subject: [PATCH] feat: RUN-847: Track time, balance and call System APIs --- .../src/wasmtime_embedder/system_api.rs | 4 +- rs/interfaces/src/execution_environment.rs | 22 ++- rs/system_api/BUILD.bazel | 9 ++ rs/system_api/src/lib.rs | 41 ++++- rs/system_api/tests/system_api.rs | 4 +- rs/system_api/tests/system_api_tracking.rs | 149 ++++++++++++++++++ 6 files changed, 218 insertions(+), 11 deletions(-) create mode 100644 rs/system_api/tests/system_api_tracking.rs diff --git a/rs/embedders/src/wasmtime_embedder/system_api.rs b/rs/embedders/src/wasmtime_embedder/system_api.rs index 44fe546dc62..a33e1aeeb35 100644 --- a/rs/embedders/src/wasmtime_embedder/system_api.rs +++ b/rs/embedders/src/wasmtime_embedder/system_api.rs @@ -891,8 +891,8 @@ pub(crate) fn syscalls( &mut caller, overhead!(CANISTER_CYCLE_BALANCE128, metering_type), )?; - with_memory_and_system_api(&mut caller, |system_api, memory| { - system_api.ic0_canister_cycle_balance128(dst, memory) + with_memory_and_system_api(&mut caller, |s, memory| { + s.ic0_canister_cycle_balance128(dst, memory) })?; if feature_flags.write_barrier == FlagStatus::Enabled { mark_writes_on_bytemap(&mut caller, dst as usize, 16) diff --git a/rs/interfaces/src/execution_environment.rs b/rs/interfaces/src/execution_environment.rs index 93e7f3bd092..de0113d7ed1 100644 --- a/rs/interfaces/src/execution_environment.rs +++ b/rs/interfaces/src/execution_environment.rs @@ -85,6 +85,7 @@ pub enum PerformanceCounterType { } /// System API call ids to track their execution (in alphabetical order). +#[derive(Debug)] pub enum SystemApiCallId { /// Tracker for `ic0.accept_message())` AcceptMessage, @@ -194,6 +195,21 @@ pub enum SystemApiCallId { UpdateAvailableMemory, } +/// System API call counters, i.e. how many times each tracked System API call +/// was invoked. +// #[derive(Serialize, Deserialize, Clone, Debug, Default)] +#[derive(Clone, Default)] +pub struct SystemApiCallCounters { + /// Counter for `ic0.call_perform()` + pub call_perform: usize, + /// Counter for `ic0.canister_cycle_balance()` + pub canister_cycle_balance: usize, + /// Counter for `ic0.canister_cycle_balance128()` + pub canister_cycle_balance128: usize, + /// Counter for `ic0.time()` + pub time: usize, +} + /// Tracks the available memory on a subnet. The main idea is to separately track /// the execution available memory, the message available memory and the wasm custom /// sections available memory. The different flavors of memory are independent of each @@ -832,7 +848,7 @@ pub trait SystemApi { ) -> HypervisorResult<(NumPages, NumInstructions)>; /// The canister can query the IC for the current time. - fn ic0_time(&self) -> HypervisorResult