Skip to content

Commit

Permalink
fix(benchmarks,runtime-interface): clear store by counter when runnin…
Browse files Browse the repository at this point in the history
…ing benchmarks (#4192)
  • Loading branch information
ByteNacked authored Aug 27, 2024
1 parent 93e684b commit e3fb2ac
Show file tree
Hide file tree
Showing 9 changed files with 278 additions and 233 deletions.
1 change: 1 addition & 0 deletions pallets/gear/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"common/runtime-benchmarks",
"gear-runtime-interface/runtime-benchmarks",
"gear-core-backend/mock",
"gear-core-errors/codec",
"gear-sandbox",
Expand Down
210 changes: 106 additions & 104 deletions pallets/gear/src/weights.rs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions runtime-interface/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,6 @@ std = [
"ark-scale/std",
"sha2/std"
]
runtime-benchmarks = [
"gear-sandbox-interface/runtime-benchmarks"
]
1 change: 1 addition & 0 deletions runtime-interface/sandbox/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ std = [
"sp-runtime-interface/std",
"sp-wasm-interface/std",
]
runtime-benchmarks = []
28 changes: 28 additions & 0 deletions runtime-interface/sandbox/src/detail.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ impl Sandboxes {

&mut self.store
}

// Clears the underlying store if the counter exceeds the limit.
#[cfg(feature = "runtime-benchmarks")]
pub fn clear(&mut self) {
BENCH_SANDBOX_RESET_COUNTER.with_borrow_mut(|c| {
if *c >= BENCH_SANDBOX_RESET_COUNTER_LIMIT {
*c = 0;
self.store.clear();
}
*c += 1;
});
}
}

thread_local! {
Expand Down Expand Up @@ -453,6 +465,14 @@ pub fn memory_new(context: &mut dyn FunctionContext, initial: u32, maximum: u32)

let data_ptr: *const _ = caller.data();
method_result = SANDBOXES.with(|sandboxes| {
// The usual method to clear the store doesn't work for benchmarks (see `Sanboxes::get`).
// This issue is more prominent in so-called "onetime syscall" benchmarks because
// they run with minimal steps and a large number of repeats, leading to significant slowdowns.
// Therefore, we have to clear it manually if the `BENCH_SANDBOX_RESET_COUNTER` exceeds the limit.
// Otherwise, the store becomes too big and will slow down the benchmarks.
#[cfg(feature = "runtime-benchmarks")]
sandboxes.borrow_mut().clear();

sandboxes
.borrow_mut()
.get(data_ptr as usize)
Expand Down Expand Up @@ -577,3 +597,11 @@ pub fn set_global_val(

method_result
}

#[cfg(feature = "runtime-benchmarks")]
const BENCH_SANDBOX_RESET_COUNTER_LIMIT: u32 = 100;

#[cfg(feature = "runtime-benchmarks")]
thread_local! {
static BENCH_SANDBOX_RESET_COUNTER: RefCell<u32> = const { RefCell::new(0) };
}
24 changes: 12 additions & 12 deletions runtime/vara/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,21 @@ fn syscall_weights_test() {
gr_reservation_send_per_byte: 500.into(),
gr_reservation_send_commit: 2_900_000.into(),
gr_reply_commit: 12_000_000.into(),
gr_reply_commit_wgas: 12_100_000.into(),
gr_reservation_reply: 8_300_000.into(),
gr_reply_commit_wgas: 12_000_000.into(),
gr_reservation_reply: 8_500_000.into(),
gr_reservation_reply_per_byte: 675_000.into(),
gr_reservation_reply_commit: 7_800_000.into(),
gr_reservation_reply_commit: 8_000_000.into(),
gr_reply_push: 1_700_000.into(),
gr_reply: 13_600_000.into(),
gr_reply: 12_500_000.into(),
gr_reply_per_byte: 650.into(),
gr_reply_wgas: 11_900_000.into(),
gr_reply_wgas: 12_500_000.into(),
gr_reply_wgas_per_byte: 650.into(),
gr_reply_push_per_byte: 640.into(),
gr_reply_to: 950_200.into(),
gr_signal_code: 962_500.into(),
gr_signal_from: 941_500.into(),
gr_reply_input: 13_300_000.into(),
gr_reply_input_wgas: 10_600_000.into(),
gr_reply_input: 13_500_000.into(),
gr_reply_input_wgas: 8_000_000.into(),
gr_reply_push_input: 1_200_000.into(),
gr_reply_push_input_per_byte: 146.into(),
gr_send_input: 3_100_000.into(),
Expand All @@ -255,11 +255,11 @@ fn syscall_weights_test() {
gr_debug: 1_200_000.into(),
gr_debug_per_byte: 450.into(),
gr_reply_code: 919_800.into(),
gr_exit: 96_500_000.into(),
gr_leave: 130_300_000.into(),
gr_wait: 112_500_000.into(),
gr_wait_for: 92_000_000.into(),
gr_wait_up_to: 127_000_000.into(),
gr_exit: 18_000_000.into(),
gr_leave: 14_000_000.into(),
gr_wait: 14_000_000.into(),
gr_wait_for: 14_000_000.into(),
gr_wait_up_to: 14_500_000.into(),
gr_wake: 3_000_000.into(),
gr_create_program: 4_100_000.into(),
gr_create_program_payload_per_byte: 120.into(),
Expand Down
210 changes: 106 additions & 104 deletions runtime/vara/src/weights/pallet_gear.rs

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions scripts/benchmarking/run_all_benchmarks.sh
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ for PALLET in "${PALLETS[@]}"; do
# Run multithreaded benchmarks (pallet_gear_builtin) on fixed 4 cores.
if [ -n "$INSTANCE_TYPE" ] && [ "$PALLET" == "pallet_gear_builtin" ]
then
PREV_TASKSET_CMD=$TASKSET_CMD
TASKSET_CMD="taskset -c 2,3,4,5"
echo "[+] Running pallet_gear_builtin benches on fixed 4 cores: 2,3,4,5"
fi
Expand Down Expand Up @@ -220,6 +221,13 @@ for PALLET in "${PALLETS[@]}"; do
echo "[-] Failed to benchmark $PALLET. Error written to $ERR_FILE; continuing..."
fi
fi

# Reset the taskset command if it was changed.
if [ -n "$PREV_TASKSET_CMD" ]
then
TASKSET_CMD=$PREV_TASKSET_CMD
unset PREV_TASKSET_CMD
fi
done

if [ "$skip_machine_benchmark" != true ]
Expand Down
26 changes: 13 additions & 13 deletions utils/wasm-instrument/src/gas_metering/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,39 +497,39 @@ impl Default for SyscallWeights {
proof_size: 0,
},
gr_reply_commit: Weight {
ref_time: 12018944,
ref_time: 12049464,
proof_size: 0,
},
gr_reply_commit_wgas: Weight {
ref_time: 12137604,
ref_time: 11720008,
proof_size: 0,
},
gr_reservation_reply: Weight {
ref_time: 8379472,
ref_time: 8504802,
proof_size: 0,
},
gr_reservation_reply_per_byte: Weight {
ref_time: 720353,
proof_size: 0,
},
gr_reservation_reply_commit: Weight {
ref_time: 7809250,
ref_time: 8289934,
proof_size: 0,
},
gr_reply_push: Weight {
ref_time: 1701621,
proof_size: 0,
},
gr_reply: Weight {
ref_time: 13603312,
ref_time: 12488174,
proof_size: 0,
},
gr_reply_per_byte: Weight {
ref_time: 711,
proof_size: 0,
},
gr_reply_wgas: Weight {
ref_time: 11943522,
ref_time: 12363104,
proof_size: 0,
},
gr_reply_wgas_per_byte: Weight {
Expand All @@ -553,11 +553,11 @@ impl Default for SyscallWeights {
proof_size: 0,
},
gr_reply_input: Weight {
ref_time: 13351726,
ref_time: 13518262,
proof_size: 0,
},
gr_reply_input_wgas: Weight {
ref_time: 10595976,
ref_time: 8132850,
proof_size: 0,
},
gr_reply_push_input: Weight {
Expand Down Expand Up @@ -597,23 +597,23 @@ impl Default for SyscallWeights {
proof_size: 0,
},
gr_exit: Weight {
ref_time: 96563242,
ref_time: 18105950,
proof_size: 0,
},
gr_leave: Weight {
ref_time: 130303114,
ref_time: 13825758,
proof_size: 0,
},
gr_wait: Weight {
ref_time: 112591140,
ref_time: 13811842,
proof_size: 0,
},
gr_wait_for: Weight {
ref_time: 92188166,
ref_time: 13619656,
proof_size: 0,
},
gr_wait_up_to: Weight {
ref_time: 127918232,
ref_time: 14400276,
proof_size: 0,
},
gr_wake: Weight {
Expand Down

0 comments on commit e3fb2ac

Please sign in to comment.