Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyan-dfinity committed Oct 7, 2023
1 parent 11941ea commit bfd54a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ enum SubCommand {
/// The number of pages of the preallocated stable memory
#[clap(short, long, requires("start_page"))]
page_limit: Option<i32>,
/// Use the new metering cost, default to false
/// Use the new metering cost, default to false. This flag will eventually be removed and set to true after the mainnet has adapted the new metering.
#[clap(short, long)]
use_new_metering: bool,
},
Expand Down
31 changes: 28 additions & 3 deletions src/instrumentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,10 @@ fn inject_metering(
}
Instr::Call(Call { func }) => {
curr.cost += instr_cost(instr, use_new_metering);
match func_cost.get_cost(*func) {
match func_cost
.get_cost(*func)
.unwrap_or((instr_cost(instr, use_new_metering), InjectionKind::Static))
{
(cost, InjectionKind::Static) => curr.cost += cost,
(cost, kind @ InjectionKind::Dynamic)
| (cost, kind @ InjectionKind::Dynamic64) => {
Expand Down Expand Up @@ -616,10 +619,22 @@ fn inject_init(m: &mut Module, is_init: GlobalId) {
fn inject_pre_upgrade(m: &mut Module, vars: &Variables, config: &Config) {
let writer = get_ic_func_id(m, "stable_write");
let memory = get_memory_id(m);
/*let a = m.locals.add(ValType::I64);
let b = m.locals.add(ValType::I64);
let c = m.locals.add(ValType::I64);*/
let mut builder = get_or_create_export_func(m, "canister_pre_upgrade");
#[rustfmt::skip]
builder
// no need to backup memory, since this is the end of the pre-upgrade.
// backup memory. This is not strictly needed, since it's at the end of pre-upgrade.
/*.i32_const(0)
.load(memory, LoadKind::I64 { atomic: false }, MemArg { offset: 0, align: 8})
.local_set(a)
.i32_const(8)
.load(memory, LoadKind::I64 { atomic: false }, MemArg { offset: 0, align: 8})
.local_set(b)
.i32_const(16)
.load(memory, LoadKind::I64 { atomic: false }, MemArg { offset: 0, align: 8})
.local_set(c)*/
// persist globals
.i32_const(0)
.global_get(vars.total_counter)
Expand All @@ -639,7 +654,17 @@ fn inject_pre_upgrade(m: &mut Module, vars: &Variables, config: &Config) {
.i32_const(config.metadata_start_address())
.i32_const(0)
.i32_const(METADATA_SIZE)
.call(writer);
.call(writer)
// restore memory
/*.i32_const(0)
.local_get(a)
.store(memory, StoreKind::I64 { atomic: false }, MemArg { offset: 0, align: 8 })
.i32_const(8)
.local_get(b)
.store(memory, StoreKind::I64 { atomic: false }, MemArg { offset: 0, align: 8 })
.i32_const(16)
.local_get(c)
.store(memory, StoreKind::I64 { atomic: false }, MemArg { offset: 0, align: 8 })*/;
}
fn inject_post_upgrade(m: &mut Module, vars: &Variables, config: &Config) {
let reader = get_ic_func_id(m, "stable_read");
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@ impl FunctionCost {
}
Self(res)
}
pub fn get_cost(&self, id: FunctionId) -> (i64, InjectionKind) {
*self.0.get(&id).unwrap_or(&(1, InjectionKind::Static))
pub fn get_cost(&self, id: FunctionId) -> Option<(i64, InjectionKind)> {
self.0.get(&id).copied()
}
}
pub(crate) fn instr_cost(i: &ir::Instr, use_new_metering: bool) -> i64 {
Expand Down

0 comments on commit bfd54a1

Please sign in to comment.