From 5ceb4db33d65c2cf4d934fdd764a48b56b6e2252 Mon Sep 17 00:00:00 2001 From: Andriy Berestovskyy Date: Wed, 11 Oct 2023 22:13:06 +0200 Subject: [PATCH 1/5] feat: RUN-798: Add call context perf counter --- src/ic-cdk/CHANGELOG.md | 10 ++++++++++ src/ic-cdk/src/api/call.rs | 9 --------- src/ic-cdk/src/api/mod.rs | 24 +++++++++++++++++++++--- 3 files changed, 31 insertions(+), 12 deletions(-) diff --git a/src/ic-cdk/CHANGELOG.md b/src/ic-cdk/CHANGELOG.md index 82931e7dc..1eaf97f86 100644 --- a/src/ic-cdk/CHANGELOG.md +++ b/src/ic-cdk/CHANGELOG.md @@ -6,6 +6,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +### Added + +- Added `ic_cdk::api::performance_counter` type 1 (call context instruction counter) +- Added `call_context_instruction_counter` function as a shorthand for `performance_counter(1)` + +### Changed + +- Removed `ic_cdk::api::call::performance_counter()`. + Please use `ic_cdk::api::performance_counter` instead. + ### Refactored - Change from pleco to tanton for the chess library in the chess example. (#345) diff --git a/src/ic-cdk/src/api/call.rs b/src/ic-cdk/src/api/call.rs index 3dc23c142..5e80c71e7 100644 --- a/src/ic-cdk/src/api/call.rs +++ b/src/ic-cdk/src/api/call.rs @@ -677,15 +677,6 @@ pub fn method_name() -> String { String::from_utf8_lossy(&bytes).into_owned() } -/// Get the value of specified performance counter -/// -/// Supported counter type: -/// 0 : instruction counter. The number of WebAssembly instructions the system has determined that the canister has executed. -pub fn performance_counter(counter_type: u32) -> u64 { - // SAFETY: ic0.performance_counter is always safe to call. - unsafe { ic0::performance_counter(counter_type as i32) as u64 } -} - /// Pretends to have the Candid type `T`, but unconditionally errors /// when serialized. /// diff --git a/src/ic-cdk/src/api/mod.rs b/src/ic-cdk/src/api/mod.rs index 1395c646f..d2c7c7d29 100644 --- a/src/ic-cdk/src/api/mod.rs +++ b/src/ic-cdk/src/api/mod.rs @@ -111,17 +111,35 @@ pub fn data_certificate() -> Option> { Some(buf) } -/// Returns the number of instructions that the canister executed since the last [entry +/// Return the number of instructions that the canister executed since the last [entry /// point](https://internetcomputer.org/docs/current/references/ic-interface-spec/#entry-points). #[inline] pub fn instruction_counter() -> u64 { performance_counter(0) } +/// Return the number of WebAssembly instructions the canister has executed +/// within the call context of the current Message execution since +/// Call context creation. +/// +/// The counter monotonically increases across all message executions +/// in the call context until the corresponding call context is removed. +#[inline] +pub fn call_context_instruction_counter() -> u64 { + performance_counter(1) +} + /// Get the value of specified performance counter. /// -/// Supported counter type: -/// 0 : instruction counter. The number of WebAssembly instructions the system has determined that the canister has executed. +/// Supported counter types: +/// * `0` : current execution instruction counter. The number of WebAssembly +/// instructions the canister has executed since the beginning of the +/// current Message execution. +/// * `1` : call context instruction counter. The number of WebAssembly +/// instructions the canister has executed within the call context +/// of the current Message execution since Call context creation. +/// The counter monotonically increases across all message executions +/// in the call context until the corresponding call context is removed. #[inline] pub fn performance_counter(counter_type: u32) -> u64 { // SAFETY: ic0.performance_counter is always safe to call. From b107395b07610ea7ed8a348197d9c80dd5c5acb0 Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Thu, 12 Oct 2023 11:59:12 -0400 Subject: [PATCH 2/5] deprecated note --- src/ic-cdk/src/api/call.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/ic-cdk/src/api/call.rs b/src/ic-cdk/src/api/call.rs index ac6b9159d..39c6cb8d8 100644 --- a/src/ic-cdk/src/api/call.rs +++ b/src/ic-cdk/src/api/call.rs @@ -577,6 +577,18 @@ pub fn method_name() -> String { String::from_utf8_lossy(&bytes).into_owned() } +/// Get the value of specified performance counter +/// +/// See [`crate::api::performance_counter`]. +#[deprecated( + since = "0.11.3", + note = "This method conceptually doesn't belong to this module. Please use `ic_cdk::api::performance_counter` instead." +)] +pub fn performance_counter(counter_type: u32) -> u64 { + // SAFETY: ic0.performance_counter is always safe to call. + unsafe { ic0::performance_counter(counter_type as i32) as u64 } +} + /// Pretends to have the Candid type `T`, but unconditionally errors /// when serialized. /// From bb38ae17e258129077ff0b33758fa71dbd892445 Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Thu, 12 Oct 2023 11:59:22 -0400 Subject: [PATCH 3/5] improve doc --- src/ic-cdk/src/api/call.rs | 4 ++-- src/ic-cdk/src/api/mod.rs | 16 ++++++++-------- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/ic-cdk/src/api/call.rs b/src/ic-cdk/src/api/call.rs index 39c6cb8d8..c335e459f 100644 --- a/src/ic-cdk/src/api/call.rs +++ b/src/ic-cdk/src/api/call.rs @@ -530,7 +530,7 @@ pub fn arg_data_raw() -> Vec { bytes } -/// Get the len of the raw-argument-data-bytes. +/// Gets the len of the raw-argument-data-bytes. pub fn arg_data_raw_size() -> usize { // SAFETY: ic0.msg_arg_data_size is always safe to call. unsafe { ic0::msg_arg_data_size() as usize } @@ -577,7 +577,7 @@ pub fn method_name() -> String { String::from_utf8_lossy(&bytes).into_owned() } -/// Get the value of specified performance counter +/// Gets the value of specified performance counter /// /// See [`crate::api::performance_counter`]. #[deprecated( diff --git a/src/ic-cdk/src/api/mod.rs b/src/ic-cdk/src/api/mod.rs index a3ceb798a..c5fec0525 100644 --- a/src/ic-cdk/src/api/mod.rs +++ b/src/ic-cdk/src/api/mod.rs @@ -24,7 +24,7 @@ pub fn trap(message: &str) -> ! { unreachable!() } -/// Get current timestamp, in nanoseconds since the epoch (1970-01-01) +/// Gets current timestamp, in nanoseconds since the epoch (1970-01-01) pub fn time() -> u64 { // SAFETY: ic0.time is always safe to call. unsafe { ic0::time() as u64 } @@ -54,13 +54,13 @@ pub fn id() -> Principal { Principal::try_from(&bytes).unwrap() } -/// Get the amount of funds available in the canister. +/// Gets the amount of funds available in the canister. pub fn canister_balance() -> u64 { // SAFETY: ic0.canister_cycle_balance is always safe to call. unsafe { ic0::canister_cycle_balance() as u64 } } -/// Get the amount of funds available in the canister. +/// Gets the amount of funds available in the canister. pub fn canister_balance128() -> u128 { let mut recv = 0u128; // SAFETY: recv is writable and the size expected by ic0.canister_cycle_balance128. @@ -111,14 +111,14 @@ pub fn data_certificate() -> Option> { Some(buf) } -/// Return the number of instructions that the canister executed since the last [entry +/// Returns the number of instructions that the canister executed since the last [entry /// point](https://internetcomputer.org/docs/current/references/ic-interface-spec/#entry-points). #[inline] pub fn instruction_counter() -> u64 { performance_counter(0) } -/// Return the number of WebAssembly instructions the canister has executed +/// Returns the number of WebAssembly instructions the canister has executed /// within the call context of the current Message execution since /// Call context creation. /// @@ -129,7 +129,7 @@ pub fn call_context_instruction_counter() -> u64 { performance_counter(1) } -/// Get the value of specified performance counter. +/// Gets the value of specified performance counter. /// /// Supported counter types: /// * `0` : current execution instruction counter. The number of WebAssembly @@ -146,13 +146,13 @@ pub fn performance_counter(counter_type: u32) -> u64 { unsafe { ic0::performance_counter(counter_type as i32) as u64 } } -/// Get the value of canister version. +/// Gets the value of canister version. pub fn canister_version() -> u64 { // SAFETY: ic0.canister_version is always safe to call. unsafe { ic0::canister_version() as u64 } } -/// Determine if a Principal is a controller of the canister. +/// Determines if a Principal is a controller of the canister. pub fn is_controller(principal: &Principal) -> bool { let slice = principal.as_slice(); // SAFETY: `principal.as_bytes()`, being `&[u8]`, is a readable sequence of bytes and therefore safe to pass to `ic0.is_controller`. From 5759fc6b4a556b281385f62a5a373b40a2985e1c Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Thu, 12 Oct 2023 12:00:54 -0400 Subject: [PATCH 4/5] fmt --- src/ic-cdk/src/api/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ic-cdk/src/api/mod.rs b/src/ic-cdk/src/api/mod.rs index c5fec0525..98d13ca16 100644 --- a/src/ic-cdk/src/api/mod.rs +++ b/src/ic-cdk/src/api/mod.rs @@ -121,7 +121,7 @@ pub fn instruction_counter() -> u64 { /// Returns the number of WebAssembly instructions the canister has executed /// within the call context of the current Message execution since /// Call context creation. -/// +/// /// The counter monotonically increases across all message executions /// in the call context until the corresponding call context is removed. #[inline] From b83304af58cfadd7aecec856082d359373da2b18 Mon Sep 17 00:00:00 2001 From: Linwei Shang Date: Thu, 12 Oct 2023 12:07:48 -0400 Subject: [PATCH 5/5] v0.11.3 and changelog --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/ic-cdk/CHANGELOG.md | 11 +++++++---- src/ic-cdk/Cargo.toml | 2 +- 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2f366beb3..f588f516e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -883,7 +883,7 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "ic-cdk" -version = "0.11.2" +version = "0.11.3" dependencies = [ "candid", "ic-cdk-macros", diff --git a/Cargo.toml b/Cargo.toml index de9540aad..2b6e4ba0f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,7 @@ opt-level = 'z' [workspace.dependencies] ic0 = { path = "src/ic0", version = "0.21.1" } -ic-cdk = { path = "src/ic-cdk", version = "0.11.2" } +ic-cdk = { path = "src/ic-cdk", version = "0.11.3" } ic-cdk-timers = { path = "src/ic-cdk-timers", version = "0.5.1" } candid = "0.9.6" diff --git a/src/ic-cdk/CHANGELOG.md b/src/ic-cdk/CHANGELOG.md index c67f968c3..4c5d18573 100644 --- a/src/ic-cdk/CHANGELOG.md +++ b/src/ic-cdk/CHANGELOG.md @@ -6,15 +6,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [unreleased] +## [0.11.3] - 2023-10-12 + ### Added -- Added `ic_cdk::api::performance_counter` type 1 (call context instruction counter) -- Added `call_context_instruction_counter` function as a shorthand for `performance_counter(1)` +- Another type of performance counter: "call context instruction counter". + Can be fetched using either method below: (#435) + - `ic_cdk::api::performance_counter(1)`; + - `ic_cdk::api::call_context_instruction_counter()` as a shorthand; ### Changed -- Removed `ic_cdk::api::call::performance_counter()`. - Please use `ic_cdk::api::performance_counter()` instead. +- Deprecate `ic_cdk::api::call::performance_counter()` in favor of `ic_cdk::api::performance_counter()`. (#435) ## [0.11.2] - 2023-10-11 diff --git a/src/ic-cdk/Cargo.toml b/src/ic-cdk/Cargo.toml index 7a0d80e3d..fbfe0149d 100644 --- a/src/ic-cdk/Cargo.toml +++ b/src/ic-cdk/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ic-cdk" -version = "0.11.2" +version = "0.11.3" authors.workspace = true edition.workspace = true license.workspace = true