Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: on low wasm memory hook #286

Closed
wants to merge 12 commits into from
2 changes: 2 additions & 0 deletions spec/_attachments/ic.did
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type canister_settings = record {
memory_allocation : opt nat;
freezing_threshold : opt nat;
reserved_cycles_limit : opt nat;
low_wasm_memory_threshold : opt nat;
};

type definite_canister_settings = record {
Expand All @@ -15,6 +16,7 @@ type definite_canister_settings = record {
memory_allocation : nat;
freezing_threshold : nat;
reserved_cycles_limit : nat;
low_wasm_memory_threshold: nat;
};

type change_origin = variant {
Expand Down
20 changes: 19 additions & 1 deletion spec/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,8 @@ In order for a WebAssembly module to be usable as the code for the canister, it

- If it exports a function called `canister_heartbeat`, the function must have type `() -> ()`.

- If it exports a function called `canister_on_low_wasm_memory`, the function must have type `() -> ()`.

- If it exports a function called `canister_global_timer`, the function must have type `() -> ()`.

- If it exports any functions called `canister_update <name>`, `canister_query <name>`, or `canister_composite_query <name>` for some `name`, the functions must have type `() -> ()`.
Expand Down Expand Up @@ -1215,6 +1217,8 @@ The canister provides entry points which are invoked by the IC under various cir

- The canister may export functions with name `canister_composite_query <name>` and type `() -> ()`.

- The canister may export functions with name `canister_on_low_wasm_memory` and type `() -> ()`.
mraszyk marked this conversation as resolved.
Show resolved Hide resolved

- The canister table may contain functions of type `(env : i32) -> ()` which may be used as callbacks for inter-canister calls and composite query methods.

If the execution of any of these entry points traps for any reason, then all changes to the WebAssembly state, as well as the effect of any externally visible system call (like `ic0.msg_reply`, `ic0.msg_reject`, `ic0.call_perform`), are discarded. For upgrades, this transactional behavior applies to the `canister_pre_upgrade`/`canister_post_upgrade` sequence as a whole.
Expand Down Expand Up @@ -1289,6 +1293,18 @@ While an implementation will likely try to keep the interval between the value o

:::

#### On Low Wasm Memory {#on-low-wasm-memory}

A canister can export a function with the name `canister_on_low_wasm_memory`, which is scheduled whenever the canister's wasm memory size in bytes grows from below a threshold `t` to >= `t`.
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved
The threshold `t` can be defined in the [canister's settings](#ic-canister_status) and by default it is set to 2<sup>64</sup> − 1.
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved
dragoljub-duric marked this conversation as resolved.
Show resolved Hide resolved

:::note

While the above function is scheduled immediately once the condition above is triggered, it may not necessarily be executed immediately if it does not have enough cycles.
If the canister gets frozen immediately after the function is scheduled for execution, the function will run once the canister's unfrozen _if_ the canister's wasm memory remains above the threshold `t`.
mraszyk marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the canister gets frozen immediately after the function is scheduled for execution, the function will run once the canister's unfrozen if the canister's wasm memory remains above the threshold t.

This property is not established by the implementation.


:::

#### Callbacks

Callbacks are addressed by their table index (as a proxy for a Wasm `funcref`).
Expand Down Expand Up @@ -1399,7 +1415,7 @@ The comment after each function lists from where these functions may be invoked:

- `F`: from `canister_inspect_message`

- `T`: from *system task* (`canister_heartbeat` or `canister_global_timer`)
- `T`: from *system task* (`canister_heartbeat` or `canister_global_timer` `canister_on_low_wasm_memory`)
mraszyk marked this conversation as resolved.
Show resolved Hide resolved

- `*` = `I G U Q CQ Ry Rt CRy CRt C CC F T` (NB: Not `(start)`)

Expand Down Expand Up @@ -2077,6 +2093,8 @@ Indicates various information about the canister. It contains:

- The reserved cycles limit of the canister, i.e., the maximum number of cycles that can be in the canister's reserved balance after increasing the canister's memory allocation and/or actual memory usage.

- The "low wasm memory" threshold, which is used to determine when the [canister_on_low_wasm_memory](#on-low-wasm-memory) function is executed.

- A SHA256 hash of the module installed on the canister. This is `null` if the canister is empty.

- The actual memory usage of the canister.
Expand Down
Loading