Skip to content

Commit

Permalink
customize use_new_metering
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Oct 7, 2023
1 parent ca83f4e commit 8017d08
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ We also provide some built-in functions:
* `gzip(blob)`: gzip a blob value.
* `stringify(exp1, exp2, exp3, ...)`: convert all expressions to string and concat. Only supports primitive types.
* `output(path, content)`: append text content to file path.
* `wasm_profiling(path)/wasm_profiling(path, record { trace_only_funcs = <vec text>; start_page = <nat>; page_limit = <nat> })`: load Wasm module, instrument the code and store as a blob value. Calling profiled canister binds the cost to variable `__cost_{id}` or `__cost__`. The second argument is optional, and all fields in the record are also optional. If provided, `trace_only_funcs` will only count and trace the provided set of functions; `start_page` writes the logs to a preallocated pages in stable memory; `page_limit` specifies the number of the preallocated pages, default to 30 if omitted. See [ic-wasm's doc](https://github.com/dfinity/ic-wasm#working-with-upgrades-and-stable-memory) for more details.
* `wasm_profiling(path)/wasm_profiling(path, record { trace_only_funcs = <vec text>; start_page = <nat>; page_limit = <nat>; use_new_metering = <bool> })`: load Wasm module, instrument the code and store as a blob value. Calling profiled canister binds the cost to variable `__cost_{id}` or `__cost__`. The second argument is optional, and all fields in the record are also optional. If provided, `trace_only_funcs` will only count and trace the provided set of functions; `use_new_metering` decides the cost model, defaults to the global `--use-new-metering` flag; `start_page` writes the logs to a preallocated pages in stable memory; `page_limit` specifies the number of the preallocated pages, default to 30 if omitted. See [ic-wasm's doc](https://github.com/dfinity/ic-wasm#working-with-upgrades-and-stable-memory) for more details.
* `flamegraph(canister_id, title, filename)`: generate flamegraph for the last update call to canister_id, with title and write to `{filename}.svg`. The cost of the update call is returned.
* `concat(e1, e2)`: concatenate two vec/record/text together.
* `add/sub/mul/div(e1, e2)`: addition/subtraction/multiplication/division of two integer/float numbers. If one of the arguments is float32/float64, the result is float64; otherwise, the result is integer. You can use type annotation to get the integer part of the float number. For example `div((mul(div(1, 3.0), 1000) : nat), 100.0)` returns `3.33`.
Expand Down
14 changes: 12 additions & 2 deletions src/exp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,6 @@ impl Exp {
"wasm_profiling" => match args.as_slice() {
[IDLValue::Text(file)] | [IDLValue::Text(file), IDLValue::Record(_)] => {
use ic_wasm::instrumentation::{instrument, Config};
let use_new_metering = helper.use_new_metering;
let path = resolve_path(&helper.base_path, file);
let blob = std::fs::read(&path)
.with_context(|| format!("Cannot read {path:?}"))?;
Expand All @@ -202,6 +201,17 @@ impl Exp {
} else {
None
};
let use_new_metering = if let Some(v) =
get_field(fs, "use_new_metering")
{
if let IDLValue::Bool(b) = v {
*b
} else {
return Err(anyhow!("use_new_metering expects a bool"));
}
} else {
helper.use_new_metering
};
let trace_only_funcs = if let Some(v) =
get_field(fs, "trace_only_funcs")
{
Expand Down Expand Up @@ -233,7 +243,7 @@ impl Exp {
trace_only_funcs: vec![],
start_address: None,
page_limit: None,
use_new_metering,
use_new_metering: helper.use_new_metering,
},
};
instrument(&mut m, config).map_err(|e| anyhow::anyhow!("{e}"))?;
Expand Down

0 comments on commit 8017d08

Please sign in to comment.