Skip to content

Commit dda82cc

Browse files
author
Maciej Kot
committed
Chore: RUN-725: Cleanup old metering
1 parent 3fb48f4 commit dda82cc

File tree

16 files changed

+239
-534
lines changed

16 files changed

+239
-534
lines changed

rs/config/src/embedders.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ impl Default for FeatureFlags {
105105

106106
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
107107
pub enum MeteringType {
108-
Old,
109108
New,
110109
/// for testing and benchmarking
111110
None,

rs/drun/src/main.rs

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use clap::{Arg, ArgAction, ArgMatches, Command};
1+
use clap::{Arg, ArgMatches, Command};
22
use ic_canister_sandbox_backend_lib::{
33
canister_sandbox_main, compiler_sandbox::compiler_sandbox_main,
44
launcher::sandbox_launcher_main, RUN_AS_CANISTER_SANDBOX_FLAG, RUN_AS_COMPILER_SANDBOX_FLAG,
55
RUN_AS_SANDBOX_LAUNCHER_FLAG,
66
};
7-
use ic_config::{embedders::MeteringType, flag_status::FlagStatus, Config, ConfigSource};
7+
use ic_config::{flag_status::FlagStatus, Config, ConfigSource};
88
use ic_drun::{run_drun, DrunOptions};
99
use ic_registry_subnet_type::SubnetType;
1010
use std::path::PathBuf;
@@ -17,7 +17,6 @@ const ARG_MESSAGES: &str = "messages";
1717
const ARG_EXTRA_BATCHES: &str = "extra-batches";
1818
const ARG_INSTRUCTION_LIMIT: &str = "instruction-limit";
1919
const ARG_SUBNET_TYPE: &str = "subnet-type";
20-
const USE_OLD_METERING: &str = "use-old-metering";
2120

2221
fn main() -> Result<(), String> {
2322
// Check if `drun` is running in the canister sandbox mode where it waits
@@ -56,7 +55,7 @@ async fn drun_main() -> Result<(), String> {
5655
.rate_limiting_of_debug_prints = FlagStatus::Disabled;
5756
default_config.hypervisor.rate_limiting_of_heap_delta = FlagStatus::Disabled;
5857
default_config.hypervisor.rate_limiting_of_instructions = FlagStatus::Disabled;
59-
let mut cfg = Config::load_with_default(&source, default_config).unwrap_or_else(|err| {
58+
let cfg = Config::load_with_default(&source, default_config).unwrap_or_else(|err| {
6059
eprintln!("Failed to load config:\n {}", err);
6160
std::process::exit(1);
6261
});
@@ -90,13 +89,6 @@ async fn drun_main() -> Result<(), String> {
9089
})
9190
.unwrap_or(SubnetType::System);
9291

93-
let use_old_metering = matches.get_flag(USE_OLD_METERING);
94-
cfg.hypervisor.embedders_config.metering_type = if use_old_metering {
95-
MeteringType::Old
96-
} else {
97-
MeteringType::New
98-
};
99-
10092
let uo = DrunOptions {
10193
msg_filename: matches.value_of(ARG_MESSAGES).unwrap().to_string(),
10294
cfg,
@@ -167,11 +159,5 @@ fn get_arg_matches() -> ArgMatches {
167159
.value_name("Subnet Type")
168160
.takes_value(true),
169161
)
170-
.arg(
171-
Arg::new(USE_OLD_METERING)
172-
.long(USE_OLD_METERING)
173-
.help("Enable the old metering in the local canister execution environment.")
174-
.action(ArgAction::SetTrue),
175-
)
176162
.get_matches()
177163
}

rs/embedders/src/wasm_utils/instrumentation.rs

Lines changed: 6 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,6 @@ impl InjectedImports {
163163

164164
// Gets the cost of an instruction.
165165
pub fn instruction_to_cost(i: &Operator) -> u64 {
166-
match i {
167-
// The following instructions are mostly signaling the start/end of code blocks,
168-
// so we assign 0 cost to them.
169-
Operator::Block { .. } => 0,
170-
Operator::Else => 0,
171-
Operator::End => 0,
172-
Operator::Loop { .. } => 0,
173-
174-
// Default cost of an instruction is 1.
175-
_ => 1,
176-
}
177-
}
178-
179-
// Gets the cost of an instruction.
180-
pub fn instruction_to_cost_new(i: &Operator) -> u64 {
181166
// This aims to be a complete list of all instructions that can be executed, with certain exceptions.
182167
// The exceptions are: SIMD instructions, atomic instructions, and the dynamic cost of
183168
// of operations such as table/memory fill, copy, init. This
@@ -788,7 +773,6 @@ pub(super) fn instrument(
788773
special_indices,
789774
subnet_type,
790775
dirty_page_overhead,
791-
metering_type,
792776
)
793777
}
794778

@@ -872,7 +856,6 @@ fn replace_system_api_functions(
872856
special_indices: SpecialIndices,
873857
subnet_type: SubnetType,
874858
dirty_page_overhead: NumInstructions,
875-
metering_type: MeteringType,
876859
) {
877860
let api_indexes = calculate_api_indexes(module);
878861
let number_of_func_imports = module
@@ -884,12 +867,9 @@ fn replace_system_api_functions(
884867
// Collect a single map of all the function indexes that need to be
885868
// replaced.
886869
let mut func_index_replacements = BTreeMap::new();
887-
for (api, (ty, body)) in replacement_functions(
888-
special_indices,
889-
subnet_type,
890-
dirty_page_overhead,
891-
metering_type,
892-
) {
870+
for (api, (ty, body)) in
871+
replacement_functions(special_indices, subnet_type, dirty_page_overhead)
872+
{
893873
if let Some(old_index) = api_indexes.get(&api) {
894874
let type_idx = add_func_type(module, ty);
895875
let new_index = (number_of_func_imports + module.functions.len()) as u32;
@@ -1198,9 +1178,8 @@ fn inject_metering(
11981178
metering_type: MeteringType,
11991179
) {
12001180
let points = match metering_type {
1201-
MeteringType::Old => injections_old(code),
12021181
MeteringType::None => Vec::new(),
1203-
MeteringType::New => injections_new(code),
1182+
MeteringType::New => injections(code),
12041183
};
12051184
let points = points.iter().filter(|point| match point.cost_detail {
12061185
InjectionPointCostDetail::StaticCost {
@@ -1531,79 +1510,20 @@ fn inject_try_grow_wasm_memory(
15311510
}
15321511
}
15331512

1534-
// This function scans through the Wasm code and creates an injection point
1535-
// at the beginning of every basic block (straight-line sequence of instructions
1536-
// with no branches) and before each bulk memory instruction. An injection point
1537-
// contains a "hint" about the context of every basic block, specifically if
1538-
// it's re-entrant or not. This version over-estimates the cost of code with
1539-
// returns and jumps.
1540-
fn injections_old(code: &[Operator]) -> Vec<InjectionPoint> {
1541-
let mut res = Vec::new();
1542-
let mut stack = Vec::new();
1543-
use Operator::*;
1544-
// The function itself is a re-entrant code block.
1545-
let mut curr = InjectionPoint::new_static_cost(0, Scope::ReentrantBlockStart, 0);
1546-
for (position, i) in code.iter().enumerate() {
1547-
curr.cost_detail.increment_cost(instruction_to_cost(i));
1548-
match i {
1549-
// Start of a re-entrant code block.
1550-
Loop { .. } => {
1551-
stack.push(curr);
1552-
curr = InjectionPoint::new_static_cost(position + 1, Scope::ReentrantBlockStart, 0);
1553-
}
1554-
// Start of a non re-entrant code block.
1555-
If { .. } | Block { .. } => {
1556-
stack.push(curr);
1557-
curr =
1558-
InjectionPoint::new_static_cost(position + 1, Scope::NonReentrantBlockStart, 0);
1559-
}
1560-
// End of a code block but still more code left.
1561-
Else | Br { .. } | BrIf { .. } | BrTable { .. } => {
1562-
res.push(curr);
1563-
curr = InjectionPoint::new_static_cost(position + 1, Scope::BlockEnd, 0);
1564-
}
1565-
// `End` signals the end of a code block. If there's nothing more on the stack, we've
1566-
// gone through all the code.
1567-
End => {
1568-
res.push(curr);
1569-
curr = match stack.pop() {
1570-
Some(val) => val,
1571-
None => break,
1572-
};
1573-
}
1574-
// Bulk memory instructions require injected metering __before__ the instruction
1575-
// executes so that size arguments can be read from the stack at runtime.
1576-
MemoryFill { .. }
1577-
| MemoryCopy { .. }
1578-
| MemoryInit { .. }
1579-
| TableCopy { .. }
1580-
| TableInit { .. }
1581-
| TableFill { .. } => {
1582-
res.push(InjectionPoint::new_dynamic_cost(position));
1583-
}
1584-
// Nothing special to be done for other instructions.
1585-
_ => (),
1586-
}
1587-
}
1588-
1589-
res.sort_by_key(|k| k.position);
1590-
res
1591-
}
1592-
15931513
// This function scans through the Wasm code and creates an injection point
15941514
// at the beginning of every basic block (straight-line sequence of instructions
15951515
// with no branches) and before each bulk memory instruction. An injection point
15961516
// contains a "hint" about the context of every basic block, specifically if
15971517
// it's re-entrant or not.
1598-
fn injections_new(code: &[Operator]) -> Vec<InjectionPoint> {
1518+
fn injections(code: &[Operator]) -> Vec<InjectionPoint> {
15991519
let mut res = Vec::new();
16001520
use Operator::*;
16011521
// The function itself is a re-entrant code block.
16021522
// Start with at least one fuel being consumed because even empty
16031523
// functions should consume at least some fuel.
16041524
let mut curr = InjectionPoint::new_static_cost(0, Scope::ReentrantBlockStart, 1);
16051525
for (position, i) in code.iter().enumerate() {
1606-
curr.cost_detail.increment_cost(instruction_to_cost_new(i));
1526+
curr.cost_detail.increment_cost(instruction_to_cost(i));
16071527
match i {
16081528
// Start of a re-entrant code block.
16091529
Loop { .. } => {

rs/embedders/src/wasm_utils/system_api_replacements.rs

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
//!
1313
1414
use crate::{
15-
wasm_utils::instrumentation::InjectedImports, wasmtime_embedder::system_api_complexity,
16-
InternalErrorCode,
15+
wasm_utils::instrumentation::InjectedImports,
16+
wasmtime_embedder::system_api_complexity::overhead_native, InternalErrorCode,
1717
};
18-
use ic_config::embedders::MeteringType;
1918
use ic_interfaces::execution_environment::StableMemoryApi;
2019
use ic_registry_subnet_type::SubnetType;
2120
use ic_sys::PAGE_SIZE;
@@ -26,15 +25,12 @@ use wasmtime_environ::WASM_PAGE_SIZE;
2625

2726
use super::{instrumentation::SpecialIndices, SystemApiFunc};
2827

29-
use crate::wasmtime_embedder::system_api_complexity::system_api;
30-
3128
const MAX_32_BIT_STABLE_MEMORY_IN_PAGES: i64 = 64 * 1024; // 4GiB
3229

3330
pub(super) fn replacement_functions(
3431
special_indices: SpecialIndices,
3532
subnet_type: SubnetType,
3633
dirty_page_overhead: NumInstructions,
37-
metering_type: MeteringType,
3834
) -> Vec<(SystemApiFunc, (FuncType, Body<'static>))> {
3935
let count_clean_pages_fn_index = special_indices.count_clean_pages_fn.unwrap();
4036
let dirty_pages_counter_index = special_indices.dirty_pages_counter_ix.unwrap();
@@ -243,11 +239,7 @@ pub(super) fn replacement_functions(
243239
},
244240
I64ExtendI32U,
245241
I64Const {
246-
value: system_api::complexity_overhead_native!(
247-
STABLE_READ,
248-
metering_type
249-
)
250-
.get() as i64,
242+
value: overhead_native::STABLE_READ.get() as i64,
251243
},
252244
I64Add,
253245
Call {
@@ -505,11 +497,7 @@ pub(super) fn replacement_functions(
505497
}
506498
},
507499
I64Const {
508-
value: system_api::complexity_overhead_native!(
509-
STABLE64_READ,
510-
metering_type
511-
)
512-
.get() as i64,
500+
value: overhead_native::STABLE64_READ.get() as i64,
513501
},
514502
I64Add,
515503
Call {
@@ -795,11 +783,7 @@ pub(super) fn replacement_functions(
795783
},
796784
I64ExtendI32U,
797785
I64Const {
798-
value: system_api::complexity_overhead_native!(
799-
STABLE_WRITE,
800-
metering_type
801-
)
802-
.get() as i64,
786+
value: overhead_native::STABLE_WRITE.get() as i64,
803787
},
804788
I64Add,
805789
Call {
@@ -1026,11 +1010,7 @@ pub(super) fn replacement_functions(
10261010
}
10271011
},
10281012
I64Const {
1029-
value: system_api::complexity_overhead_native!(
1030-
STABLE64_WRITE,
1031-
metering_type
1032-
)
1033-
.get() as i64,
1013+
value: overhead_native::STABLE64_WRITE.get() as i64,
10341014
},
10351015
I64Add,
10361016
Call {

rs/embedders/src/wasmtime_embedder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ impl WasmtimeEmbedder {
244244
self.config.feature_flags,
245245
self.config.stable_memory_dirty_page_limit,
246246
self.config.stable_memory_accessed_page_limit,
247-
self.config.metering_type,
248247
main_memory_type,
249248
);
250249

0 commit comments

Comments
 (0)