Skip to content

Commit

Permalink
Low memory threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
adamspofford-dfinity committed Dec 30, 2024
1 parent 8f8f45c commit 1260bf0
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 32 deletions.
96 changes: 73 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ license = "Apache-2.0"
candid = "0.10.11"
candid_parser = "0.1.4"
dfx-core = { path = "src/dfx-core", version = "0.1.0" }
ic-agent = "0.39"
ic-agent = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "0a51f2a65dde7d9e1790c378bd60e1768e3be257" }
ic-asset = { path = "src/canisters/frontend/ic-asset", version = "0.21.0" }
ic-cdk = "0.13.1"
ic-identity-hsm = "0.39"
ic-utils = "0.39"
ic-identity-hsm = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "0a51f2a65dde7d9e1790c378bd60e1768e3be257" }
ic-utils = { version = "0.39", git = "https://github.com/dfinity/agent-rs", rev = "0a51f2a65dde7d9e1790c378bd60e1768e3be257" }

aes-gcm = "0.10.3"
anyhow = "1.0.56"
Expand Down
5 changes: 5 additions & 0 deletions e2e/assets/allocate_memory/src/e2e_project_backend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ fn greet(s: String) -> String {
fn greet_update(s: String) -> String {
format!("Hello, {s}!")
}

#[ic_cdk::on_low_wasm_memory]
fn on_low_wasm_memory() {
ic_cdk::println!("Low memory!");
}
17 changes: 17 additions & 0 deletions e2e/tests-dfx/update_settings.bash
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ teardown() {
assert_contains "Canister exceeded its current Wasm memory limit of 8 bytes"
}

@test "set wasm memory threshold" {
dfx_new_rust
install_asset allocate_memory
dfx_start
assert_command dfx canister create e2e_project_backend --no-wallet --wasm-memory-threshold 2MiB --wasm-memory-limit 2MiB
assert_command dfx deploy e2e_project_backend
assert_command dfx canister status e2e_project_backend
assert_contains "Wasm memory threshold: 2_097_152 Bytes"
assert_command dfx canister update-settings e2e_project_backend --wasm-memory-threshold 1MiB
assert_command dfx canister status e2e_project_backend
assert_contains "Wasm memory threshold: 1_048_576 Bytes"
assert_command dfx canister call e2e_project_backend greet_update '("alice")'
sleep 10ms
assert_command dfx canister logs e2e_project_backend
assert_contains "Low memory!"
}

@test "set log visibility" {
dfx_new
dfx_start
Expand Down
28 changes: 28 additions & 0 deletions src/dfx-core/src/config/model/dfinity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ use crate::error::dfx_config::GetRemoteCanisterIdError::GetRemoteCanisterIdFaile
use crate::error::dfx_config::GetReservedCyclesLimitError::GetReservedCyclesLimitFailed;
use crate::error::dfx_config::GetSpecifiedIdError::GetSpecifiedIdFailed;
use crate::error::dfx_config::GetWasmMemoryLimitError::GetWasmMemoryLimitFailed;
use crate::error::dfx_config::GetWasmMemoryThresholdError::GetWasmMemoryThresholdFailed;
use crate::error::dfx_config::{
AddDependenciesError, GetCanisterConfigError, GetCanisterNamesWithDependenciesError,
GetComputeAllocationError, GetFreezingThresholdError, GetLogVisibilityError,
GetMemoryAllocationError, GetPullCanistersError, GetRemoteCanisterIdError,
GetReservedCyclesLimitError, GetSpecifiedIdError, GetWasmMemoryLimitError,
GetWasmMemoryThresholdError,
};
use crate::error::fs::CanonicalizePathError;
use crate::error::load_dfx_config::LoadDfxConfigError;
Expand Down Expand Up @@ -477,6 +479,21 @@ pub struct InitializationValues {
#[schemars(with = "Option<ByteSchema>")]
pub wasm_memory_limit: Option<Byte>,

/// # Wasm Memory Threshold
///
/// Specifies a threshold (in bytes) on the Wasm memory usage of the canister,
/// as a distance from `wasm_memory_limit`.
///
/// When the remaining memory before the limit drops below this threshold, its
/// `on_low_wasm_memory` hook will be invoked. This enables it to self-optimize,
/// or raise an alert, or otherwise attempt to prevent itself from reaching
/// `wasm_memory_limit`.
///
/// Must be a number of bytes between 0 and 2^48 (i.e. 256 TiB), inclusive.
/// Can be specified as an integer, or as an SI unit string (e.g. "4KB", "2 MiB")
#[schemars(with = "Option<ByteSchema>")]
pub wasm_memory_threshold: Option<Byte>,

/// # Log Visibility
/// Specifies who is allowed to read the canister's logs.
///
Expand Down Expand Up @@ -1004,6 +1021,17 @@ impl ConfigInterface {
.wasm_memory_limit)
}

pub fn get_wasm_memory_threshold(
&self,
canister_name: &str,
) -> Result<Option<Byte>, GetWasmMemoryThresholdError> {
Ok(self
.get_canister_config(canister_name)
.map_err(|e| GetWasmMemoryThresholdFailed(canister_name.to_string(), e))?
.initialization_values
.wasm_memory_threshold)
}

pub fn get_log_visibility(
&self,
canister_name: &str,
Expand Down
6 changes: 6 additions & 0 deletions src/dfx-core/src/error/dfx_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ pub enum GetWasmMemoryLimitError {
GetWasmMemoryLimitFailed(String, #[source] GetCanisterConfigError),
}

#[derive(Error, Debug)]
pub enum GetWasmMemoryThresholdError {
#[error("Failed to get Wasm memory threshold for canister '{0}'")]
GetWasmMemoryThresholdFailed(String, #[source] GetCanisterConfigError),
}

#[derive(Error, Debug)]
pub enum GetLogVisibilityError {
#[error("Failed to get log visibility for canister '{0}'")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ crate-type = ["cdylib"]

[dependencies]
candid = "0.10"
ic-cdk = "0.16"
ic-cdk-timers = "0.10" # Feel free to remove this dependency if you don't need timers
ic-cdk = "0.17"
ic-cdk-timers = "0.11" # Feel free to remove this dependency if you don't need timers
1 change: 1 addition & 0 deletions src/dfx/src/commands/canister/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ pub fn get_effective_canister_id(
| MgmtMethod::BitcoinGetUtxos
| MgmtMethod::BitcoinSendTransaction
| MgmtMethod::BitcoinGetCurrentFeePercentiles
| MgmtMethod::BitcoinGetBlockHeaders
| MgmtMethod::EcdsaPublicKey
| MgmtMethod::SignWithEcdsa
| MgmtMethod::NodeMetricsHistory => Ok(CanisterId::management_canister()),
Expand Down
Loading

0 comments on commit 1260bf0

Please sign in to comment.