From af794cb178f335fb05f54f8d58d095e0db2292b6 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 16 Jun 2023 12:04:58 +0400 Subject: [PATCH 01/32] update TaskHandler return gas amount from all traits methods and from process_with method --- common/src/scheduler/task.rs | 26 ++++--- pallets/gear/src/lib.rs | 14 ++-- pallets/gear/src/manager/mod.rs | 2 + pallets/gear/src/manager/task.rs | 42 +++++++---- .../gear/src/manager/task_gas_allowance.rs | 73 +++++++++++++++++++ 5 files changed, 125 insertions(+), 32 deletions(-) create mode 100644 pallets/gear/src/manager/task_gas_allowance.rs diff --git a/common/src/scheduler/task.rs b/common/src/scheduler/task.rs index ebeb0ff7e78..df07a1a088f 100644 --- a/common/src/scheduler/task.rs +++ b/common/src/scheduler/task.rs @@ -23,6 +23,8 @@ use frame_support::{ }; use gear_core::ids::{CodeId, MessageId, ProgramId, ReservationId}; +pub type Gas = u64; + /// Scheduled task sense and required data for processing action. /// /// CAUTION: NEVER ALLOW `ScheduledTask` BE A BIG DATA. @@ -85,7 +87,7 @@ pub enum ScheduledTask { } impl ScheduledTask { - pub fn process_with(self, handler: &mut impl TaskHandler) { + pub fn process_with(self, handler: &mut impl TaskHandler) -> Gas { use ScheduledTask::*; match self { @@ -117,32 +119,32 @@ pub trait TaskHandler { // Rent charging section. // ----- /// Pause program action. - fn pause_program(&mut self, program_id: ProgramId); - /// Remove code action. - fn remove_code(&mut self, code_id: CodeId); + fn pause_program(&mut self, program_id: ProgramId) -> Gas; + /// Remove code action + fn remove_code(&mut self, code_id: CodeId) -> Gas; /// Remove from mailbox action. - fn remove_from_mailbox(&mut self, user_id: AccountId, message_id: MessageId); + fn remove_from_mailbox(&mut self, user_id: AccountId, message_id: MessageId) -> Gas; /// Remove from waitlist action. - fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId); + fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas; /// Remove paused program action. - fn remove_paused_program(&mut self, program_id: ProgramId); + fn remove_paused_program(&mut self, program_id: ProgramId) -> Gas; // Time chained section. // ----- /// Wake message action. - fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId); + fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas; // Send delayed message to program action. - fn send_dispatch(&mut self, stashed_message_id: MessageId); + fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas; // Send delayed message to user action. - fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool); + fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas; /// Remove gas reservation action. - fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId); + fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas; /// Remove data created by resume program session. - fn remove_resume_session(&mut self, session_id: SessionId); + fn remove_resume_session(&mut self, session_id: SessionId) -> Gas; } #[test] diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 4dacfb22b37..44bd0320f20 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -949,22 +949,22 @@ pub mod pallet { // Decreasing gas allowance due to DB deletion. GasAllowanceOf::::decrease(DbWeightOf::::get().writes(1).ref_time()); - // Processing task. - // - // NOTE: Gas allowance decrease should be implemented - // inside `TaskHandler` trait and/or inside other - // generic types, which interact with storage. - task.process_with(ext_manager); + let mut task_gas_allowance = manager::TaskGasAllowance::::new(); + let task_gas = task.clone().process_with(&mut task_gas_allowance); // Checking gas allowance. // // Making sure we have gas to remove next task // or update the first block of incomplete tasks. - if GasAllowanceOf::::get() <= DbWeightOf::::get().writes(2).ref_time() { + if GasAllowanceOf::::get().saturating_sub(task_gas) <= DbWeightOf::::get().writes(2).ref_time() { stopped_at = Some(bn); log::debug!("Stopping processing tasks at: {stopped_at:?}"); break; } + + // Processing task and update allowance of gas. + let task_gas = task.process_with(ext_manager); + GasAllowanceOf::::decrease(task_gas); } // Stopping iteration over blocks if no resources left. diff --git a/pallets/gear/src/manager/mod.rs b/pallets/gear/src/manager/mod.rs index c22356b08dc..9dfb4bdc999 100644 --- a/pallets/gear/src/manager/mod.rs +++ b/pallets/gear/src/manager/mod.rs @@ -47,9 +47,11 @@ mod journal; mod task; +mod task_gas_allowance; pub use journal::*; pub use task::*; +pub use task_gas_allowance::TaskGasAllowance; use crate::{Config, CurrencyOf, GasHandlerOf, Pallet, ProgramStorageOf, QueueOf, TaskPoolOf}; use common::{ diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index f070c8c1e9b..5cd612b3d8b 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -44,7 +44,7 @@ impl TaskHandler for ExtManager where T::AccountId: Origin, { - fn pause_program(&mut self, program_id: ProgramId) { + fn pause_program(&mut self, program_id: ProgramId) -> Gas { log::debug!("pause_program; id = {:?}", program_id); let program = ProgramStorageOf::::get_program(program_id) @@ -70,7 +70,7 @@ where change: ProgramChangeKind::Paused, }); - return; + return 0; }; // terminate uninitialized program @@ -142,13 +142,15 @@ where } Pallet::::deposit_event(event); + + 0 } - fn remove_code(&mut self, _code_id: CodeId) { - todo!("#646"); + fn remove_code(&mut self, _code_id: CodeId) -> Gas { + todo!("#646") } - fn remove_from_mailbox(&mut self, user_id: T::AccountId, message_id: MessageId) { + fn remove_from_mailbox(&mut self, user_id: T::AccountId, message_id: MessageId) -> Gas { // Read reason. let reason = UserMessageReadSystemReason::OutOfRent.into_reason(); @@ -188,9 +190,11 @@ where // Queueing dispatch. QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); + + 0 } - fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) { + fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas { // Wake reason. let reason = MessageWokenSystemReason::OutOfRent.into_reason(); @@ -259,13 +263,15 @@ where // Consuming gas handler for waitlisted message. Pallet::::consume_and_retrieve(waitlisted.id()); + + 0 } - fn remove_paused_program(&mut self, _program_id: ProgramId) { - todo!("#646"); + fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas { + todo!("#646") } - fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) { + fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas { if let Some(dispatch) = Pallet::::wake_dispatch( program_id, message_id, @@ -274,9 +280,11 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); } + + 0 } - fn send_dispatch(&mut self, stashed_message_id: MessageId) { + fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas { // No validation required. If program doesn't exist, then NotExecuted appears. let (dispatch, hold_interval) = DispatchStashOf::::take(stashed_message_id) @@ -287,9 +295,11 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); + + 0 } - fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) { + fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas { // TODO: validate here destination and send error reply, if required. // Atm despite the fact that program may exist, message goes into mailbox / event. let (message, hold_interval) = DispatchStashOf::::take(stashed_message_id) @@ -300,15 +310,21 @@ where Pallet::::charge_for_hold(message.id(), hold_interval, StorageType::DispatchStash); Pallet::::send_user_message_after_delay(message, to_mailbox); + + 0 } - fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) { + fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas { let _slot = Self::remove_gas_reservation_impl(program_id, reservation_id); + + 0 } - fn remove_resume_session(&mut self, session_id: SessionId) { + fn remove_resume_session(&mut self, session_id: SessionId) -> Gas { log::debug!("Execute task to remove resume session with session_id = {session_id}"); ProgramStorageOf::::remove_resume_session(session_id) .unwrap_or_else(|e| unreachable!("ProgramStorage corrupted! {:?}", e)); + + 0 } } diff --git a/pallets/gear/src/manager/task_gas_allowance.rs b/pallets/gear/src/manager/task_gas_allowance.rs new file mode 100644 index 00000000000..aa911ab1492 --- /dev/null +++ b/pallets/gear/src/manager/task_gas_allowance.rs @@ -0,0 +1,73 @@ +// This file is part of Gear. + +// Copyright (C) Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +use super::*; +use common::{scheduler::Gas, paused_program_storage::SessionId}; + +pub struct TaskGasAllowance(PhantomData); + +impl TaskGasAllowance { + pub fn new() -> Self { + Self(PhantomData) + } +} + +impl TaskHandler for TaskGasAllowance +where + T::AccountId: Origin, +{ + fn pause_program(&mut self, _program_id: ProgramId) -> Gas { + 0 + } + + fn remove_code(&mut self, _code_id: CodeId) -> Gas { + 0 + } + + fn remove_from_mailbox(&mut self, _user_id: T::AccountId, _message_id: MessageId) -> Gas { + 0 + } + + fn remove_from_waitlist(&mut self, _program_id: ProgramId, _message_id: MessageId) -> Gas { + 0 + } + + fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas { + 0 + } + + fn wake_message(&mut self, _program_id: ProgramId, _message_id: MessageId) -> Gas { + 0 + } + + fn send_dispatch(&mut self, _stashed_message_id: MessageId) -> Gas { + 0 + } + + fn send_user_message(&mut self, _stashed_message_id: MessageId, _to_mailbox: bool) -> Gas { + 0 + } + + fn remove_gas_reservation(&mut self, _program_id: ProgramId, _reservation_id: ReservationId) -> Gas { + 0 + } + + fn remove_resume_session(&mut self, _session_id: SessionId) -> Gas { + 0 + } +} From fc635889e46a5cb2b50583423a7b6eec7b416f16 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 16 Jun 2023 15:32:44 +0400 Subject: [PATCH 02/32] func get_maximum_task_gas implement benchmark for RemoveResumeSession use bechmarked weight for the task --- pallets/gear/src/benchmarking/mod.rs | 51 ++++++++++--- pallets/gear/src/lib.rs | 3 +- pallets/gear/src/manager/mod.rs | 2 - pallets/gear/src/manager/task.rs | 21 +++++- .../gear/src/manager/task_gas_allowance.rs | 73 ------------------- pallets/gear/src/weights.rs | 19 +++++ runtime/gear/src/weights/pallet_gear.rs | 19 +++++ runtime/vara/src/weights/pallet_gear.rs | 19 +++++ 8 files changed, 118 insertions(+), 89 deletions(-) delete mode 100644 pallets/gear/src/manager/task_gas_allowance.rs diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index b88c701f629..ee5d94eb71c 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -68,7 +68,7 @@ use common::{ paused_program_storage::SessionId, storage::{Counter, *}, ActiveProgram, CodeMetadata, CodeStorage, GasPrice, GasTree, Origin, PausedProgramStorage, - ProgramStorage, ReservableTree, + ProgramStorage, ReservableTree, scheduler::TaskHandler, }; use core_processor::{ common::{DispatchOutcome, JournalNote}, @@ -240,6 +240,17 @@ where .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)) } +#[track_caller] +fn get_last_session_id_if_any() -> Option { + let event_record = SystemPallet::::events().pop().unwrap(); + let event = <::RuntimeEvent as From<_>>::from(event_record.event); + let event: Result, _> = event.try_into(); + match event { + Ok(Event::ProgramResumeSessionStarted { session_id, .. }) => Some(session_id), + _ => None, + } +} + fn resume_session_prepare( c: u32, program_id: ProgramId, @@ -260,14 +271,6 @@ where ) .expect("failed to start resume session"); - let event_record = SystemPallet::::events().pop().unwrap(); - let event = <::RuntimeEvent as From<_>>::from(event_record.event); - let event: Result, _> = event.try_into(); - let session_id = match event { - Ok(Event::ProgramResumeSessionStarted { session_id, .. }) => session_id, - _ => unreachable!(), - }; - let memory_pages = { let mut pages = Vec::with_capacity(c as usize); for i in 0..c { @@ -277,7 +280,7 @@ where pages }; - (session_id, memory_pages) + (get_last_session_id_if_any::().unwrap(), memory_pages) } /// An instantiated and deployed program. @@ -2721,6 +2724,34 @@ benchmarks! { sbox.invoke(); } + tasks_remove_resume_session { + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + let code = benchmarking::generate_wasm2(16.into()).unwrap(); + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(&code), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), code, salt, b"init_payload".to_vec(), 10_000_000_000, 0u32.into()).expect("submit program failed"); + + init_block::(None); + + ProgramStorageOf::::pause_program(program_id, 100u32.into()).unwrap(); + + Gear::::resume_session_init( + RawOrigin::Signed(caller).into(), + program_id, + Default::default(), + CodeId::default(), + ) + .expect("failed to start resume session"); + + let session_id = get_last_session_id_if_any::().unwrap(); + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.remove_resume_session(session_id); + } + verify { + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 44bd0320f20..314ecce5dc0 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -949,8 +949,7 @@ pub mod pallet { // Decreasing gas allowance due to DB deletion. GasAllowanceOf::::decrease(DbWeightOf::::get().writes(1).ref_time()); - let mut task_gas_allowance = manager::TaskGasAllowance::::new(); - let task_gas = task.clone().process_with(&mut task_gas_allowance); + let task_gas = manager::get_maximum_task_gas::(&task); // Checking gas allowance. // diff --git a/pallets/gear/src/manager/mod.rs b/pallets/gear/src/manager/mod.rs index 9dfb4bdc999..c22356b08dc 100644 --- a/pallets/gear/src/manager/mod.rs +++ b/pallets/gear/src/manager/mod.rs @@ -47,11 +47,9 @@ mod journal; mod task; -mod task_gas_allowance; pub use journal::*; pub use task::*; -pub use task_gas_allowance::TaskGasAllowance; use crate::{Config, CurrencyOf, GasHandlerOf, Pallet, ProgramStorageOf, QueueOf, TaskPoolOf}; use common::{ diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 5cd612b3d8b..110eae065ea 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -18,7 +18,7 @@ use crate::{ manager::ExtManager, Config, CurrencyOf, DispatchStashOf, Event, Pallet, ProgramStorageOf, - QueueOf, WaitlistOf, + QueueOf, WaitlistOf, weights::WeightInfo, }; use alloc::string::ToString; use common::{ @@ -40,6 +40,23 @@ use gear_core::{ use gear_core_errors::{SimpleReplyError, SimpleSignalError}; use sp_runtime::traits::Zero; +pub fn get_maximum_task_gas(task: &ScheduledTask) -> Gas { + use ScheduledTask::*; + + match task { + PauseProgram(_) => 0, + RemoveCode(_) => todo!("#646"), + RemoveFromMailbox(_, _) => 0, + RemoveFromWaitlist(_, _) => 0, + RemovePausedProgram(_) => todo!("#646"), + WakeMessage(_, _) => 0, + SendDispatch(_) => 0, + SendUserMessage { .. } => 0, + RemoveGasReservation(_, _) => 0, + RemoveResumeSession(_) => ::WeightInfo::tasks_remove_resume_session().ref_time(), + } +} + impl TaskHandler for ExtManager where T::AccountId: Origin, @@ -325,6 +342,6 @@ where ProgramStorageOf::::remove_resume_session(session_id) .unwrap_or_else(|e| unreachable!("ProgramStorage corrupted! {:?}", e)); - 0 + ::WeightInfo::tasks_remove_resume_session().ref_time() } } diff --git a/pallets/gear/src/manager/task_gas_allowance.rs b/pallets/gear/src/manager/task_gas_allowance.rs deleted file mode 100644 index aa911ab1492..00000000000 --- a/pallets/gear/src/manager/task_gas_allowance.rs +++ /dev/null @@ -1,73 +0,0 @@ -// This file is part of Gear. - -// Copyright (C) Gear Technologies Inc. -// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 - -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. - -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -use super::*; -use common::{scheduler::Gas, paused_program_storage::SessionId}; - -pub struct TaskGasAllowance(PhantomData); - -impl TaskGasAllowance { - pub fn new() -> Self { - Self(PhantomData) - } -} - -impl TaskHandler for TaskGasAllowance -where - T::AccountId: Origin, -{ - fn pause_program(&mut self, _program_id: ProgramId) -> Gas { - 0 - } - - fn remove_code(&mut self, _code_id: CodeId) -> Gas { - 0 - } - - fn remove_from_mailbox(&mut self, _user_id: T::AccountId, _message_id: MessageId) -> Gas { - 0 - } - - fn remove_from_waitlist(&mut self, _program_id: ProgramId, _message_id: MessageId) -> Gas { - 0 - } - - fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas { - 0 - } - - fn wake_message(&mut self, _program_id: ProgramId, _message_id: MessageId) -> Gas { - 0 - } - - fn send_dispatch(&mut self, _stashed_message_id: MessageId) -> Gas { - 0 - } - - fn send_user_message(&mut self, _stashed_message_id: MessageId, _to_mailbox: bool) -> Gas { - 0 - } - - fn remove_gas_reservation(&mut self, _program_id: ProgramId, _reservation_id: ReservationId) -> Gas { - 0 - } - - fn remove_resume_session(&mut self, _session_id: SessionId) -> Gas { - 0 - } -} diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 4cec0775979..357ec010e33 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -215,6 +215,7 @@ pub trait WeightInfo { fn instr_i32rotl(r: u32, ) -> Weight; fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; + fn tasks_remove_resume_session() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2066,6 +2067,15 @@ impl WeightInfo for SubstrateWeight { // Standard Error: 6_162 .saturating_add(Weight::from_parts(509_221, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } } // For backwards compatibility and tests @@ -3911,4 +3921,13 @@ impl WeightInfo for () { // Standard Error: 6_162 .saturating_add(Weight::from_parts(509_221, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 2166a54dd0d..168c44b8252 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -215,6 +215,7 @@ pub trait WeightInfo { fn instr_i32rotl(r: u32, ) -> Weight; fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; + fn tasks_remove_resume_session() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2066,6 +2067,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Standard Error: 6_162 .saturating_add(Weight::from_parts(509_221, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } } // For backwards compatibility and tests @@ -3911,4 +3921,13 @@ impl WeightInfo for () { // Standard Error: 6_162 .saturating_add(Weight::from_parts(509_221, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index 14f78ef6c32..ec5670bb876 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -215,6 +215,7 @@ pub trait WeightInfo { fn instr_i32rotl(r: u32, ) -> Weight; fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; + fn tasks_remove_resume_session() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2066,6 +2067,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Standard Error: 9_758 .saturating_add(Weight::from_parts(516_687, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(2_u64)) + } } // For backwards compatibility and tests @@ -3911,4 +3921,13 @@ impl WeightInfo for () { // Standard Error: 9_758 .saturating_add(Weight::from_parts(516_687, 0).saturating_mul(r.into())) } + fn tasks_remove_resume_session() -> Weight { + // Proof Size summary in bytes: + // Measured: `352` + // Estimated: `4169` + // Minimum execution time: 3_000_000 picoseconds. + Weight::from_parts(3_000_000, 4169) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + } } From 718b17493b64b32767a0b89551c57f71d280c2ff Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 16 Jun 2023 17:16:31 +0400 Subject: [PATCH 03/32] tasks_remove_gas_reservation benchmark --- Cargo.toml | 2 +- examples/binaries/reserve-gas/Cargo.toml | 3 ++- examples/binaries/reserve-gas/build.rs | 6 ++++- examples/binaries/reserve-gas/src/lib.rs | 6 ++--- pallets/gear/Cargo.toml | 3 +++ pallets/gear/src/benchmarking/mod.rs | 33 ++++++++++++++++++++++++ pallets/gear/src/manager/task.rs | 4 +-- pallets/gear/src/weights.rs | 19 ++++++++++++++ runtime/gear/src/weights/pallet_gear.rs | 19 ++++++++++++++ runtime/vara/src/weights/pallet_gear.rs | 19 ++++++++++++++ 10 files changed, 106 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b4ecd05a32e..da714eddae2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -365,7 +365,7 @@ demo-proxy = { path = "examples/binaries/proxy", default-features = false } demo-proxy-relay = { path = "examples/binaries/proxy-relay" } demo-proxy-reservation-with-gas = { path = "examples/binaries/proxy-reservation-with-gas" } demo-reservation-manager = { path = "examples/binaries/reservation-manager" } -demo-reserve-gas = { path = "examples/binaries/reserve-gas" } +demo-reserve-gas = { path = "examples/binaries/reserve-gas", default-features = false } demo-rwlock = { path = "examples/binaries/rwlock" } demo-send-from-reservation = { path = "examples/binaries/send-from-reservation" } demo-signal-entry = { path = "examples/binaries/signal-entry" } diff --git a/examples/binaries/reserve-gas/Cargo.toml b/examples/binaries/reserve-gas/Cargo.toml index c1f32256422..df3afe8c7a1 100644 --- a/examples/binaries/reserve-gas/Cargo.toml +++ b/examples/binaries/reserve-gas/Cargo.toml @@ -18,5 +18,6 @@ gtest.workspace = true [features] debug = ["gstd/debug"] -std = [] +wasm-wrapper = [] +std = ["wasm-wrapper", "parity-scale-codec/std"] default = ["std"] diff --git a/examples/binaries/reserve-gas/build.rs b/examples/binaries/reserve-gas/build.rs index 4c502a3ddee..b911ce127bb 100644 --- a/examples/binaries/reserve-gas/build.rs +++ b/examples/binaries/reserve-gas/build.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use gear_wasm_builder::WasmBuilder; + fn main() { - gear_wasm_builder::build(); + WasmBuilder::new() + .exclude_features(vec!["std", "wasm-wrapper"]) + .build(); } diff --git a/examples/binaries/reserve-gas/src/lib.rs b/examples/binaries/reserve-gas/src/lib.rs index d103441ca4d..46cec0085aa 100644 --- a/examples/binaries/reserve-gas/src/lib.rs +++ b/examples/binaries/reserve-gas/src/lib.rs @@ -23,12 +23,12 @@ extern crate alloc; use alloc::vec::Vec; use parity_scale_codec::{Decode, Encode}; -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] mod code { include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); } -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] pub use code::WASM_BINARY_OPT as WASM_BINARY; pub const RESERVATION_AMOUNT: u64 = 50_000_000; @@ -62,7 +62,7 @@ pub enum ReplyAction { pub type GasAmount = u64; pub type BlockCount = u32; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { use super::*; use gstd::{ diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 869563722ae..26c7fe80990 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -57,6 +57,7 @@ rand_pcg = { workspace = true, optional = true } sp-consensus-slots = { workspace = true, optional = true } test-syscalls = { workspace = true, optional = true } demo-proxy = { workspace = true, optional = true } +demo-reserve-gas = { workspace = true, optional = true } [dev-dependencies] wabt.workspace = true @@ -147,6 +148,7 @@ std = [ "sp-consensus-babe/std", "test-syscalls?/std", "demo-proxy?/std", + "demo-reserve-gas?/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -163,6 +165,7 @@ runtime-benchmarks = [ "test-syscalls/wasm-wrapper", "demo-proxy/wasm-wrapper", "gsys", + "demo-reserve-gas/wasm-wrapper", ] runtime-benchmarks-checkers = [] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index ee5d94eb71c..b21706b464d 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -2752,6 +2752,39 @@ benchmarks! { verify { } + tasks_remove_gas_reservation { + use demo_reserve_gas::{InitAction, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + InitAction::Normal(vec![(50_000, 100),]) + .encode(), + 10_000_000_000, 0u32.into()).expect("submit program failed"); + + Gear::::process_queue(Default::default()); + + let program: ActiveProgram<_> = ProgramStorageOf::::get_program(program_id) + .expect("program should exist") + .try_into() + .expect("program should be active"); + + let reservation_id = program.gas_reservation_map.first_key_value().map(|(k, _v)| *k).unwrap(); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.remove_gas_reservation(program_id, reservation_id); + } + verify { + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 110eae065ea..922e89200a3 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -52,7 +52,7 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga WakeMessage(_, _) => 0, SendDispatch(_) => 0, SendUserMessage { .. } => 0, - RemoveGasReservation(_, _) => 0, + RemoveGasReservation(_, _) => ::WeightInfo::tasks_remove_gas_reservation().ref_time(), RemoveResumeSession(_) => ::WeightInfo::tasks_remove_resume_session().ref_time(), } } @@ -334,7 +334,7 @@ where fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas { let _slot = Self::remove_gas_reservation_impl(program_id, reservation_id); - 0 + ::WeightInfo::tasks_remove_gas_reservation().ref_time() } fn remove_resume_session(&mut self, session_id: SessionId) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 357ec010e33..547105d1c88 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -216,6 +216,7 @@ pub trait WeightInfo { fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; + fn tasks_remove_gas_reservation() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2076,6 +2077,15 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) + } } // For backwards compatibility and tests @@ -3930,4 +3940,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 168c44b8252..b0b21498332 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -216,6 +216,7 @@ pub trait WeightInfo { fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; + fn tasks_remove_gas_reservation() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2076,6 +2077,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) + } } // For backwards compatibility and tests @@ -3930,4 +3940,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index ec5670bb876..c2eb22c1f85 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -216,6 +216,7 @@ pub trait WeightInfo { fn instr_i64rotr(r: u32, ) -> Weight; fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; + fn tasks_remove_gas_reservation() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2076,6 +2077,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(T::DbWeight::get().reads(5_u64)) + .saturating_add(T::DbWeight::get().writes(4_u64)) + } } // For backwards compatibility and tests @@ -3930,4 +3940,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } + fn tasks_remove_gas_reservation() -> Weight { + // Proof Size summary in bytes: + // Measured: `714` + // Estimated: `16349` + // Minimum execution time: 19_000_000 picoseconds. + Weight::from_parts(21_000_000, 16349) + .saturating_add(RocksDbWeight::get().reads(5_u64)) + .saturating_add(RocksDbWeight::get().writes(4_u64)) + } } From 05479ec64f762a00ec7277405bf6502435cbe1eb Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Sat, 17 Jun 2023 17:16:34 +0400 Subject: [PATCH 04/32] tasks_send_user_message* benchmarks --- Cargo.toml | 2 +- examples/binaries/delayed-sender/Cargo.toml | 3 +- examples/binaries/delayed-sender/build.rs | 6 ++- examples/binaries/delayed-sender/src/code.rs | 18 +++++++ examples/binaries/delayed-sender/src/lib.rs | 7 +-- pallets/gear/Cargo.toml | 3 ++ pallets/gear/src/benchmarking/mod.rs | 57 +++++++++++++++++--- pallets/gear/src/manager/task.rs | 10 +++- pallets/gear/src/weights.rs | 38 +++++++++++++ runtime/gear/src/weights/pallet_gear.rs | 38 +++++++++++++ runtime/vara/src/weights/pallet_gear.rs | 38 +++++++++++++ 11 files changed, 205 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da714eddae2..71715701321 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -343,7 +343,7 @@ demo-calc-hash-in-one-block = { path = "examples/binaries/calc-hash/in-one-block demo-calc-hash-over-blocks = { path = "examples/binaries/calc-hash/over-blocks" } demo-compose = { path = "examples/binaries/compose" } demo-constructor = { path = "examples/binaries/constructor" } -demo-delayed-sender = { path = "examples/binaries/delayed-sender" } +demo-delayed-sender = { path = "examples/binaries/delayed-sender", default-features = false } demo-distributor = { path = "examples/binaries/distributor" } demo-futures-unordered = { path = "examples/binaries/futures-unordered", features = ["debug"] } demo-gas-burned = { path = "examples/binaries/gas-burned" } diff --git a/examples/binaries/delayed-sender/Cargo.toml b/examples/binaries/delayed-sender/Cargo.toml index 0847154c407..7f02e1ea44c 100644 --- a/examples/binaries/delayed-sender/Cargo.toml +++ b/examples/binaries/delayed-sender/Cargo.toml @@ -14,5 +14,6 @@ gear-wasm-builder.workspace = true [features] debug = ["gstd/debug"] -std = [ ] +wasm-wrapper = [] +std = ["wasm-wrapper"] default = ["std"] diff --git a/examples/binaries/delayed-sender/build.rs b/examples/binaries/delayed-sender/build.rs index 4c502a3ddee..b911ce127bb 100644 --- a/examples/binaries/delayed-sender/build.rs +++ b/examples/binaries/delayed-sender/build.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use gear_wasm_builder::WasmBuilder; + fn main() { - gear_wasm_builder::build(); + WasmBuilder::new() + .exclude_features(vec!["std", "wasm-wrapper"]) + .build(); } diff --git a/examples/binaries/delayed-sender/src/code.rs b/examples/binaries/delayed-sender/src/code.rs index 1688c50ad7c..8ac684cdbfb 100644 --- a/examples/binaries/delayed-sender/src/code.rs +++ b/examples/binaries/delayed-sender/src/code.rs @@ -1,3 +1,21 @@ +// This file is part of Gear. + +// Copyright (C) Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use gstd::{msg, MessageId, exec}; static mut MID: Option = None; diff --git a/examples/binaries/delayed-sender/src/lib.rs b/examples/binaries/delayed-sender/src/lib.rs index 9348dfb7a52..1175ebd0d91 100644 --- a/examples/binaries/delayed-sender/src/lib.rs +++ b/examples/binaries/delayed-sender/src/lib.rs @@ -15,17 +15,18 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . + #![no_std] -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] mod code { include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); } -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] pub use code::WASM_BINARY_OPT as WASM_BINARY; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { include! {"./code.rs"} } diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 26c7fe80990..2eb391f898d 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -58,6 +58,7 @@ sp-consensus-slots = { workspace = true, optional = true } test-syscalls = { workspace = true, optional = true } demo-proxy = { workspace = true, optional = true } demo-reserve-gas = { workspace = true, optional = true } +demo-delayed-sender = { workspace = true, optional = true } [dev-dependencies] wabt.workspace = true @@ -149,6 +150,7 @@ std = [ "test-syscalls?/std", "demo-proxy?/std", "demo-reserve-gas?/std", + "demo-delayed-sender?/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -166,6 +168,7 @@ runtime-benchmarks = [ "demo-proxy/wasm-wrapper", "gsys", "demo-reserve-gas/wasm-wrapper", + "demo-delayed-sender/wasm-wrapper", ] runtime-benchmarks-checkers = [] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index b21706b464d..cb86c80633a 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -57,7 +57,7 @@ use crate::{ manager::ExtManager, pallet, schedule::INSTR_BENCHMARK_BATCH_SIZE, BalanceOf, BenchmarkStorage, Call, Config, Event, ExecutionEnvironment, Ext as Externalities, GasHandlerOf, MailboxOf, Pallet as Gear, Pallet, ProgramStorageOf, QueueOf, RentFreePeriodOf, ResumeMinimalPeriodOf, - Schedule, + Schedule, TaskPoolOf, }; use ::alloc::{ collections::{BTreeMap, BTreeSet}, @@ -68,7 +68,7 @@ use common::{ paused_program_storage::SessionId, storage::{Counter, *}, ActiveProgram, CodeMetadata, CodeStorage, GasPrice, GasTree, Origin, PausedProgramStorage, - ProgramStorage, ReservableTree, scheduler::TaskHandler, + ProgramStorage, ReservableTree, scheduler::{TaskHandler, ScheduledTask}, }; use core_processor::{ common::{DispatchOutcome, JournalNote}, @@ -240,9 +240,8 @@ where .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)) } -#[track_caller] fn get_last_session_id_if_any() -> Option { - let event_record = SystemPallet::::events().pop().unwrap(); + let event_record = SystemPallet::::events().pop()?; let event = <::RuntimeEvent as From<_>>::from(event_record.event); let event: Result, _> = event.try_into(); match event { @@ -283,6 +282,37 @@ where (get_last_session_id_if_any::().unwrap(), memory_pages) } +fn tasks_send_user_message_prepare() -> (MessageId, bool) +where + T::AccountId: Origin, + { + use demo_delayed_sender::WASM_BINARY; + + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let delay = 1u32; + let salt = vec![]; + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + delay.encode(), + 100_000_000_000, 0u32.into()).expect("submit program failed"); + + Gear::::process_queue(Default::default()); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()).next().unwrap(); + match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!(), + } +} + /// An instantiated and deployed program. #[derive(Clone)] struct Program { @@ -2749,8 +2779,6 @@ benchmarks! { }: { ext_manager.remove_resume_session(session_id); } - verify { - } tasks_remove_gas_reservation { use demo_reserve_gas::{InitAction, WASM_BINARY}; @@ -2782,7 +2810,22 @@ benchmarks! { }: { ext_manager.remove_gas_reservation(program_id, reservation_id); } - verify { + + tasks_send_user_message_to_mailbox { + let (message_id, to_mailbox) = tasks_send_user_message_prepare::(); + assert!(to_mailbox); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.send_user_message(message_id, true); + } + + tasks_send_user_message { + let (message_id, to_mailbox) = tasks_send_user_message_prepare::(); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.send_user_message(message_id, false); } // This is no benchmark. It merely exist to have an easy way to pretty print the currently diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 922e89200a3..08d3037583c 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -51,7 +51,10 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga RemovePausedProgram(_) => todo!("#646"), WakeMessage(_, _) => 0, SendDispatch(_) => 0, - SendUserMessage { .. } => 0, + SendUserMessage { .. } => { + core::cmp::max(::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), + ::WeightInfo::tasks_send_user_message().ref_time(),) + } RemoveGasReservation(_, _) => ::WeightInfo::tasks_remove_gas_reservation().ref_time(), RemoveResumeSession(_) => ::WeightInfo::tasks_remove_resume_session().ref_time(), } @@ -328,7 +331,10 @@ where Pallet::::send_user_message_after_delay(message, to_mailbox); - 0 + match to_mailbox { + true => ::WeightInfo::tasks_send_user_message_to_mailbox(), + false => ::WeightInfo::tasks_send_user_message(), + }.ref_time() } fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 547105d1c88..a760b5ea1d3 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -217,6 +217,8 @@ pub trait WeightInfo { fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; fn tasks_remove_gas_reservation() -> Weight; + fn tasks_send_user_message_to_mailbox() -> Weight; + fn tasks_send_user_message() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2086,6 +2088,24 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(T::DbWeight::get().reads(6_u64)) + .saturating_add(T::DbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(T::DbWeight::get().reads(9_u64)) + .saturating_add(T::DbWeight::get().writes(8_u64)) + } } // For backwards compatibility and tests @@ -3949,4 +3969,22 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(RocksDbWeight::get().reads(6_u64)) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(RocksDbWeight::get().reads(9_u64)) + .saturating_add(RocksDbWeight::get().writes(8_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index b0b21498332..7b81b8dcd4c 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -217,6 +217,8 @@ pub trait WeightInfo { fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; fn tasks_remove_gas_reservation() -> Weight; + fn tasks_send_user_message_to_mailbox() -> Weight; + fn tasks_send_user_message() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2086,6 +2088,24 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(T::DbWeight::get().reads(6_u64)) + .saturating_add(T::DbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(T::DbWeight::get().reads(9_u64)) + .saturating_add(T::DbWeight::get().writes(8_u64)) + } } // For backwards compatibility and tests @@ -3949,4 +3969,22 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(RocksDbWeight::get().reads(6_u64)) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(RocksDbWeight::get().reads(9_u64)) + .saturating_add(RocksDbWeight::get().writes(8_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index c2eb22c1f85..dfb4c3359e2 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -217,6 +217,8 @@ pub trait WeightInfo { fn instr_i32rotr(r: u32, ) -> Weight; fn tasks_remove_resume_session() -> Weight; fn tasks_remove_gas_reservation() -> Weight; + fn tasks_send_user_message_to_mailbox() -> Weight; + fn tasks_send_user_message() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2086,6 +2088,24 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(5_u64)) .saturating_add(T::DbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(T::DbWeight::get().reads(6_u64)) + .saturating_add(T::DbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(T::DbWeight::get().reads(9_u64)) + .saturating_add(T::DbWeight::get().writes(8_u64)) + } } // For backwards compatibility and tests @@ -3949,4 +3969,22 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(5_u64)) .saturating_add(RocksDbWeight::get().writes(4_u64)) } + fn tasks_send_user_message_to_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `734` + // Estimated: `21234` + // Minimum execution time: 18_000_000 picoseconds. + Weight::from_parts(19_000_000, 21234) + .saturating_add(RocksDbWeight::get().reads(6_u64)) + .saturating_add(RocksDbWeight::get().writes(5_u64)) + } + fn tasks_send_user_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `617` + // Estimated: `25736` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25736) + .saturating_add(RocksDbWeight::get().reads(9_u64)) + .saturating_add(RocksDbWeight::get().writes(8_u64)) + } } From d71c136799e5adb921cb85228057b435ed9dc67f Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Sat, 17 Jun 2023 17:17:28 +0400 Subject: [PATCH 05/32] make fmt --- common/src/scheduler/task.rs | 6 +++++- pallets/gear/src/benchmarking/mod.rs | 22 ++++++++++++++++------ pallets/gear/src/lib.rs | 4 +++- pallets/gear/src/manager/task.rs | 27 ++++++++++++++++++--------- 4 files changed, 42 insertions(+), 17 deletions(-) diff --git a/common/src/scheduler/task.rs b/common/src/scheduler/task.rs index df07a1a088f..4070c89c573 100644 --- a/common/src/scheduler/task.rs +++ b/common/src/scheduler/task.rs @@ -141,7 +141,11 @@ pub trait TaskHandler { fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas; /// Remove gas reservation action. - fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas; + fn remove_gas_reservation( + &mut self, + program_id: ProgramId, + reservation_id: ReservationId, + ) -> Gas; /// Remove data created by resume program session. fn remove_resume_session(&mut self, session_id: SessionId) -> Gas; diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index cb86c80633a..e418afafbe8 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -66,9 +66,10 @@ use ::alloc::{ use common::{ self, benchmarking, paused_program_storage::SessionId, + scheduler::{ScheduledTask, TaskHandler}, storage::{Counter, *}, ActiveProgram, CodeMetadata, CodeStorage, GasPrice, GasTree, Origin, PausedProgramStorage, - ProgramStorage, ReservableTree, scheduler::{TaskHandler, ScheduledTask}, + ProgramStorage, ReservableTree, }; use core_processor::{ common::{DispatchOutcome, JournalNote}, @@ -285,25 +286,34 @@ where fn tasks_send_user_message_prepare() -> (MessageId, bool) where T::AccountId: Origin, - { +{ use demo_delayed_sender::WASM_BINARY; let caller = benchmarking::account("caller", 0, 0); - ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + ::Currency::deposit_creating( + &caller, + 200_000_000_000_000u128.unique_saturated_into(), + ); init_block::(None); let delay = 1u32; let salt = vec![]; - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + Gear::::upload_program( + RawOrigin::Signed(caller.clone()).into(), WASM_BINARY.to_vec(), salt, delay.encode(), - 100_000_000_000, 0u32.into()).expect("submit program failed"); + 100_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); Gear::::process_queue(Default::default()); - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()).next().unwrap(); + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .unwrap(); match task { ScheduledTask::SendUserMessage { message_id, diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 314ecce5dc0..cbb1134aca5 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -955,7 +955,9 @@ pub mod pallet { // // Making sure we have gas to remove next task // or update the first block of incomplete tasks. - if GasAllowanceOf::::get().saturating_sub(task_gas) <= DbWeightOf::::get().writes(2).ref_time() { + if GasAllowanceOf::::get().saturating_sub(task_gas) + <= DbWeightOf::::get().writes(2).ref_time() + { stopped_at = Some(bn); log::debug!("Stopping processing tasks at: {stopped_at:?}"); break; diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 08d3037583c..86ad86d553f 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -17,8 +17,8 @@ // along with this program. If not, see . use crate::{ - manager::ExtManager, Config, CurrencyOf, DispatchStashOf, Event, Pallet, ProgramStorageOf, - QueueOf, WaitlistOf, weights::WeightInfo, + manager::ExtManager, weights::WeightInfo, Config, CurrencyOf, DispatchStashOf, Event, Pallet, + ProgramStorageOf, QueueOf, WaitlistOf, }; use alloc::string::ToString; use common::{ @@ -51,12 +51,16 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga RemovePausedProgram(_) => todo!("#646"), WakeMessage(_, _) => 0, SendDispatch(_) => 0, - SendUserMessage { .. } => { - core::cmp::max(::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), - ::WeightInfo::tasks_send_user_message().ref_time(),) + SendUserMessage { .. } => core::cmp::max( + ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), + ::WeightInfo::tasks_send_user_message().ref_time(), + ), + RemoveGasReservation(_, _) => { + ::WeightInfo::tasks_remove_gas_reservation().ref_time() + } + RemoveResumeSession(_) => { + ::WeightInfo::tasks_remove_resume_session().ref_time() } - RemoveGasReservation(_, _) => ::WeightInfo::tasks_remove_gas_reservation().ref_time(), - RemoveResumeSession(_) => ::WeightInfo::tasks_remove_resume_session().ref_time(), } } @@ -334,10 +338,15 @@ where match to_mailbox { true => ::WeightInfo::tasks_send_user_message_to_mailbox(), false => ::WeightInfo::tasks_send_user_message(), - }.ref_time() + } + .ref_time() } - fn remove_gas_reservation(&mut self, program_id: ProgramId, reservation_id: ReservationId) -> Gas { + fn remove_gas_reservation( + &mut self, + program_id: ProgramId, + reservation_id: ReservationId, + ) -> Gas { let _slot = Self::remove_gas_reservation_impl(program_id, reservation_id); ::WeightInfo::tasks_remove_gas_reservation().ref_time() From 4ed5a9f8c31d6cd19deef1e1f386323668c87a82 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Sun, 18 Jun 2023 13:57:05 +0400 Subject: [PATCH 06/32] adjust scheduler tests to the new gas charging --- pallets/gear-scheduler/src/tests.rs | 15 +++++----- pallets/gear/src/lib.rs | 46 ++++++++++++++++++++--------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/pallets/gear-scheduler/src/tests.rs b/pallets/gear-scheduler/src/tests.rs index aeed57e2013..3f57e5da585 100644 --- a/pallets/gear-scheduler/src/tests.rs +++ b/pallets/gear-scheduler/src/tests.rs @@ -85,6 +85,7 @@ fn populate_wl_from( (mid, pid) } +#[track_caller] fn task_and_wl_message_exist( mid: impl Into, pid: impl Into, @@ -96,9 +97,7 @@ fn task_and_wl_message_exist( let ts = TaskPoolOf::::contains(&bn, &ScheduledTask::RemoveFromWaitlist(pid, mid)); let wl = WaitlistOf::::contains(&pid, &mid); - if ts != wl { - panic!("Logic invalidated"); - } + assert_eq!(ts, wl, "Logic invalidated"); ts } @@ -200,10 +199,10 @@ fn gear_handles_tasks() { // Check if task and message got processed in block `bn`. run_to_block(bn, Some(u64::MAX)); - // Read of the first block of incomplete tasks and write for removal of task. + // Read of the first block of incomplete tasks, read of the first task and write for removal of task. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(1, 1).ref_time() + u64::MAX - db_r_w(2, 1).ref_time() ); // Storages checking. @@ -312,7 +311,7 @@ fn gear_handles_outdated_tasks() { // Check if task and message got processed before start of block `bn`. // But due to the low gas allowance, we may process the only first task. - run_to_block(bn, Some(db_r_w(1, 2).ref_time() + 1)); + run_to_block(bn, Some(db_r_w(2, 2).ref_time() + 1)); // Read of the first block of incomplete tasks, write to it afterwards + single task processing. assert_eq!(GasAllowanceOf::::get(), 1); @@ -340,10 +339,10 @@ fn gear_handles_outdated_tasks() { // Check if missed task and message got processed in block `bn`. run_to_block(bn + 1, Some(u64::MAX)); - // Delete of the first block of incomplete tasks + single task processing. + // Delete of the first block of incomplete tasks + single DB read (task) + single task processing. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(0, 2).ref_time() + u64::MAX - db_r_w(1, 2).ref_time() ); let cost2 = wl_cost_for(bn + 1 - initial_block); diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index cbb1134aca5..58a10a504e7 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -134,6 +134,9 @@ pub type RentCostPerBlockOf = ::ProgramRentCostPerBlock; pub type ResumeMinimalPeriodOf = ::ProgramResumeMinimalRentPeriod; pub type ResumeSessionDurationOf = ::ProgramResumeSessionDuration; +// Estimated count of tasks in the single block to process. +const MAX_TASK_COUNT: usize = 1_000; + /// The current storage version. const GEAR_STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -929,43 +932,58 @@ pub mod pallet { ..=current_bn.saturated_into()) .map(|block| block.saturated_into::>()); for bn in missing_blocks { - // Tasks drain iterator. - let tasks = TaskPoolOf::::drain_prefix_keys(bn); - // Checking gas allowance. // - // Making sure we have gas to remove next task + // Making sure we have gas to read next task // or update the first block of incomplete tasks. - if GasAllowanceOf::::get() <= DbWeightOf::::get().writes(2).ref_time() { + if GasAllowanceOf::::get() <= DbWeightOf::::get().reads_writes(1, 1).ref_time() { stopped_at = Some(bn); log::debug!("Stopping processing tasks at: {stopped_at:?}"); break; } + let tasks = TaskPoolOf::::iter_prefix_keys(bn); + let mut tasks_to_delete = Vec::with_capacity(MAX_TASK_COUNT); + // Iterating over tasks, scheduled on `bn`. for task in tasks { - log::debug!("Processing task: {:?}", task); - - // Decreasing gas allowance due to DB deletion. - GasAllowanceOf::::decrease(DbWeightOf::::get().writes(1).ref_time()); + // Decreasing gas allowance due to read from DB. + GasAllowanceOf::::decrease(DbWeightOf::::get().reads(1).ref_time()); - let task_gas = manager::get_maximum_task_gas::(&task); + // gas required to process task and to remove it from DB. + let task_gas = manager::get_maximum_task_gas::(&task) + .saturating_add(DbWeightOf::::get().writes(1).ref_time()); + log::debug!("Processing task: {:?}, gas = {task_gas}", task); // Checking gas allowance. // - // Making sure we have gas to remove next task - // or update the first block of incomplete tasks. + // Making sure we have gas to update the first block of incomplete tasks. if GasAllowanceOf::::get().saturating_sub(task_gas) - <= DbWeightOf::::get().writes(2).ref_time() + <= DbWeightOf::::get().writes(1).ref_time() { stopped_at = Some(bn); - log::debug!("Stopping processing tasks at: {stopped_at:?}"); + log::debug!("Not enough gas to process task at: {stopped_at:?}"); break; } // Processing task and update allowance of gas. + tasks_to_delete.push((bn, task.clone())); let task_gas = task.process_with(ext_manager); GasAllowanceOf::::decrease(task_gas); + + // Check that there is enough gas allowance to read next task, update the first block of incomplete tasks and remove already processed tasks. + if GasAllowanceOf::::get() + <= DbWeightOf::::get().reads_writes(1, 1 + tasks_to_delete.len() as u64).ref_time() + { + stopped_at = Some(bn); + log::debug!("Stopping processing tasks at (read next): {stopped_at:?}"); + break; + } + } + + for (bn, task) in tasks_to_delete { + log::debug!("Remove task: {:?}", task); + let _ = TaskPoolOf::::delete(bn, task); } // Stopping iteration over blocks if no resources left. From d8ce91d7635341b0f51cbc06bdd3bada3c094e64 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Sun, 18 Jun 2023 14:19:13 +0400 Subject: [PATCH 07/32] make clippy --- pallets/gear/src/benchmarking/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index e418afafbe8..117eb8e03e5 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -300,7 +300,7 @@ where let delay = 1u32; let salt = vec![]; Gear::::upload_program( - RawOrigin::Signed(caller.clone()).into(), + RawOrigin::Signed(caller).into(), WASM_BINARY.to_vec(), salt, delay.encode(), @@ -2800,7 +2800,7 @@ benchmarks! { let salt = vec![]; let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + Gear::::upload_program(RawOrigin::Signed(caller).into(), WASM_BINARY.to_vec(), salt, InitAction::Normal(vec![(50_000, 100),]) From 00f7bcc47bf9de71e01c7bdabbab2ddd6899aba3 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 19 Jun 2023 16:49:29 +0400 Subject: [PATCH 08/32] tasks_send_dispatch --- Cargo.toml | 2 +- examples/binaries/constructor/Cargo.toml | 3 +- examples/binaries/constructor/build.rs | 6 ++- examples/binaries/constructor/src/arg.rs | 2 +- examples/binaries/constructor/src/call.rs | 2 +- examples/binaries/constructor/src/lib.rs | 10 ++--- pallets/gear/Cargo.toml | 3 ++ pallets/gear/src/benchmarking/mod.rs | 49 +++++++++++++++++++++++ pallets/gear/src/manager/task.rs | 4 +- pallets/gear/src/weights.rs | 19 +++++++++ runtime/gear/src/weights/pallet_gear.rs | 19 +++++++++ runtime/vara/src/weights/pallet_gear.rs | 19 +++++++++ 12 files changed, 126 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31175862cd8..5515846edbc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -344,7 +344,7 @@ demo-calc-hash = { path = "examples/binaries/calc-hash" } demo-calc-hash-in-one-block = { path = "examples/binaries/calc-hash/in-one-block" } demo-calc-hash-over-blocks = { path = "examples/binaries/calc-hash/over-blocks" } demo-compose = { path = "examples/binaries/compose" } -demo-constructor = { path = "examples/binaries/constructor" } +demo-constructor = { path = "examples/binaries/constructor", default-features = false } demo-delayed-sender = { path = "examples/binaries/delayed-sender", default-features = false } demo-distributor = { path = "examples/binaries/distributor" } demo-futures-unordered = { path = "examples/binaries/futures-unordered", features = ["debug"] } diff --git a/examples/binaries/constructor/Cargo.toml b/examples/binaries/constructor/Cargo.toml index bc622516468..f37d28fa650 100644 --- a/examples/binaries/constructor/Cargo.toml +++ b/examples/binaries/constructor/Cargo.toml @@ -16,5 +16,6 @@ gear-wasm-builder.workspace = true [features] debug = ["gstd/debug"] -std = [] +wasm-wrapper = [] +std = ["wasm-wrapper"] default = ["std"] diff --git a/examples/binaries/constructor/build.rs b/examples/binaries/constructor/build.rs index 4c502a3ddee..b911ce127bb 100644 --- a/examples/binaries/constructor/build.rs +++ b/examples/binaries/constructor/build.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use gear_wasm_builder::WasmBuilder; + fn main() { - gear_wasm_builder::build(); + WasmBuilder::new() + .exclude_features(vec!["std", "wasm-wrapper"]) + .build(); } diff --git a/examples/binaries/constructor/src/arg.rs b/examples/binaries/constructor/src/arg.rs index 2d78edf7576..b749bbc03b7 100644 --- a/examples/binaries/constructor/src/arg.rs +++ b/examples/binaries/constructor/src/arg.rs @@ -90,7 +90,7 @@ impl From<&'static str> for Arg { } } -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { use super::*; diff --git a/examples/binaries/constructor/src/call.rs b/examples/binaries/constructor/src/call.rs index 3c5f3852457..9a7f0dc62e1 100644 --- a/examples/binaries/constructor/src/call.rs +++ b/examples/binaries/constructor/src/call.rs @@ -41,7 +41,7 @@ pub enum Call { MessageId, } -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { use super::*; use crate::DATA; diff --git a/examples/binaries/constructor/src/lib.rs b/examples/binaries/constructor/src/lib.rs index d112fb3d26b..0ebc224368d 100644 --- a/examples/binaries/constructor/src/lib.rs +++ b/examples/binaries/constructor/src/lib.rs @@ -16,22 +16,22 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#![no_std] +#![cfg_attr(not(feature = "std"), no_std)] extern crate alloc; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] pub(crate) use wasm::DATA; -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] mod code { include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); } -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] pub use code::WASM_BINARY_OPT as WASM_BINARY; mod arg; diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 2eb391f898d..c0b97c0bc46 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -59,6 +59,7 @@ test-syscalls = { workspace = true, optional = true } demo-proxy = { workspace = true, optional = true } demo-reserve-gas = { workspace = true, optional = true } demo-delayed-sender = { workspace = true, optional = true } +demo-constructor = { workspace = true, optional = true } [dev-dependencies] wabt.workspace = true @@ -151,6 +152,7 @@ std = [ "demo-proxy?/std", "demo-reserve-gas?/std", "demo-delayed-sender?/std", + "demo-constructor?/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -169,6 +171,7 @@ runtime-benchmarks = [ "gsys", "demo-reserve-gas/wasm-wrapper", "demo-delayed-sender/wasm-wrapper", + "demo-constructor/wasm-wrapper", ] runtime-benchmarks-checkers = [] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index da7347f819b..474bd03224d 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -2826,6 +2826,55 @@ benchmarks! { ext_manager.send_user_message(message_id, false); } + tasks_send_dispatch { + use demo_constructor::{Call, Calls, Scheme, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + Scheme::empty().encode(), + 10_000_000_000, 0u32.into()).expect("submit program failed"); + + let delay = 1u32; + let calls = Calls::builder().add_call(Call::Send( + <[u8; 32]>::from(program_id.into_origin()).into(), + [].into(), + Some(0u64.into()), + 0u128.into(), + delay.into(), + )); + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + calls.encode(), + 10_000_000_000, + 0u32.into(), + ).expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .unwrap(); + let message_id = match task { + ScheduledTask::SendDispatch ( + message_id, + ) => message_id, + _ => unreachable!(), + }; + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.send_dispatch(message_id); + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 86ad86d553f..a10d9e99238 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -50,7 +50,7 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga RemoveFromWaitlist(_, _) => 0, RemovePausedProgram(_) => todo!("#646"), WakeMessage(_, _) => 0, - SendDispatch(_) => 0, + SendDispatch(_) => ::WeightInfo::tasks_send_dispatch().ref_time(), SendUserMessage { .. } => core::cmp::max( ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), ::WeightInfo::tasks_send_user_message().ref_time(), @@ -320,7 +320,7 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - 0 + ::WeightInfo::tasks_send_dispatch().ref_time() } fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 2809d24e5c9..4505ad72091 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -218,6 +218,7 @@ pub trait WeightInfo { fn tasks_remove_gas_reservation() -> Weight; fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; + fn tasks_send_dispatch() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2095,6 +2096,15 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(9_u64)) .saturating_add(T::DbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } } // For backwards compatibility and tests @@ -3966,4 +3976,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(9_u64)) .saturating_add(RocksDbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index ce26bf37b19..34c5ce08000 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -219,6 +219,7 @@ pub trait WeightInfo { fn tasks_remove_gas_reservation() -> Weight; fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; + fn tasks_send_dispatch() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2096,6 +2097,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(9_u64)) .saturating_add(T::DbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } } // For backwards compatibility and tests @@ -3977,4 +3987,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(9_u64)) .saturating_add(RocksDbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index 5df6d7937ea..cb6379b1497 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -219,6 +219,7 @@ pub trait WeightInfo { fn tasks_remove_gas_reservation() -> Weight; fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; + fn tasks_send_dispatch() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2096,6 +2097,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(9_u64)) .saturating_add(T::DbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } } // For backwards compatibility and tests @@ -3977,4 +3987,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(9_u64)) .saturating_add(RocksDbWeight::get().writes(8_u64)) } + fn tasks_send_dispatch() -> Weight { + // Proof Size summary in bytes: + // Measured: `561` + // Estimated: `19705` + // Minimum execution time: 15_000_000 picoseconds. + Weight::from_parts(16_000_000, 19705) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } } From 41df2419eb3cea107d38fe1d811dcbd2f0d1a336 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 19 Jun 2023 16:49:54 +0400 Subject: [PATCH 09/32] make fmt --- pallets/gear/src/benchmarking/mod.rs | 2 +- pallets/gear/src/lib.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 474bd03224d..cc3a86b6cf5 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -2859,7 +2859,7 @@ benchmarks! { ).expect("failed to send message"); Gear::::process_queue(Default::default()); - + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) .next() .unwrap(); diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index 58a10a504e7..797156d9efe 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -936,7 +936,9 @@ pub mod pallet { // // Making sure we have gas to read next task // or update the first block of incomplete tasks. - if GasAllowanceOf::::get() <= DbWeightOf::::get().reads_writes(1, 1).ref_time() { + if GasAllowanceOf::::get() + <= DbWeightOf::::get().reads_writes(1, 1).ref_time() + { stopped_at = Some(bn); log::debug!("Stopping processing tasks at: {stopped_at:?}"); break; @@ -973,7 +975,9 @@ pub mod pallet { // Check that there is enough gas allowance to read next task, update the first block of incomplete tasks and remove already processed tasks. if GasAllowanceOf::::get() - <= DbWeightOf::::get().reads_writes(1, 1 + tasks_to_delete.len() as u64).ref_time() + <= DbWeightOf::::get() + .reads_writes(1, 1 + tasks_to_delete.len() as u64) + .ref_time() { stopped_at = Some(bn); log::debug!("Stopping processing tasks at (read next): {stopped_at:?}"); From 23890ddfe186c64b16f5ec0c82c3bb88807b3d3c Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 19 Jun 2023 17:04:17 +0400 Subject: [PATCH 10/32] fix deps for gclient --- gclient/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gclient/Cargo.toml b/gclient/Cargo.toml index d123f4c54d4..bc79cf9ce12 100644 --- a/gclient/Cargo.toml +++ b/gclient/Cargo.toml @@ -43,6 +43,6 @@ demo-node.workspace = true demo-program-factory.workspace = true demo-proxy = { workspace = true, features = ["std"] } demo-proxy-relay.workspace = true -demo-reserve-gas.workspace = true +demo-reserve-gas = { workspace = true, features = ["std"] } gmeta = { workspace = true } gstd = { workspace = true, features = ["debug"] } From 61be2d7bea3b3fe613f94dcef16f1c51ceaf1afd Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 20 Jun 2023 16:04:32 +0400 Subject: [PATCH 11/32] tasks_wake_message --- Cargo.toml | 2 +- examples/binaries/waiter/Cargo.toml | 11 ++---- examples/binaries/waiter/build.rs | 6 ++- examples/binaries/waiter/src/code.rs | 28 +++++++++++--- examples/binaries/waiter/src/lib.rs | 14 ++++--- gsdk/tests/rpc.rs | 2 +- pallets/gear/Cargo.toml | 3 ++ pallets/gear/src/benchmarking/mod.rs | 49 +++++++++++++++++++++++++ pallets/gear/src/manager/task.rs | 20 ++++++---- pallets/gear/src/tests.rs | 8 ++-- pallets/gear/src/weights.rs | 36 ++++++++++++++++++ runtime/gear/src/weights/pallet_gear.rs | 36 ++++++++++++++++++ runtime/vara/src/weights/pallet_gear.rs | 36 ++++++++++++++++++ 13 files changed, 220 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5515846edbc..1cf11c973e4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -374,7 +374,7 @@ demo-signal-entry = { path = "examples/binaries/signal-entry" } demo-state-rollback = { path = "examples/binaries/state-rollback" } demo-sync-duplicate = { path = "examples/binaries/sync-duplicate" } demo-vec = { path = "examples/binaries/vec" } -demo-waiter = { path = "examples/binaries/waiter" } +demo-waiter = { path = "examples/binaries/waiter", default-features = false } demo-wait-timeout = { path = "examples/binaries/wait-timeout" } demo-wait-wake = { path = "examples/binaries/wait_wake" } demo-waiting-proxy = { path = "examples/binaries/waiting-proxy" } diff --git a/examples/binaries/waiter/Cargo.toml b/examples/binaries/waiter/Cargo.toml index e212003ea4f..3328995827d 100644 --- a/examples/binaries/waiter/Cargo.toml +++ b/examples/binaries/waiter/Cargo.toml @@ -2,8 +2,8 @@ name = "demo-waiter" version = "0.1.0" authors.workspace = true -edition = "2021" -license = "GPL-3.0" +edition.workspace = true +license.workspace = true workspace = "../../../" [dependencies] @@ -13,11 +13,8 @@ gstd.workspace = true [build-dependencies] gear-wasm-builder.workspace = true -[dev-dependencies] - -[lib] - [features] debug = ["gstd/debug"] -std = ["codec/std"] +wasm-wrapper = [] +std = ["codec/std", "wasm-wrapper"] default = ["std"] diff --git a/examples/binaries/waiter/build.rs b/examples/binaries/waiter/build.rs index 4c502a3ddee..b911ce127bb 100644 --- a/examples/binaries/waiter/build.rs +++ b/examples/binaries/waiter/build.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use gear_wasm_builder::WasmBuilder; + fn main() { - gear_wasm_builder::build(); + WasmBuilder::new() + .exclude_features(vec!["std", "wasm-wrapper"]) + .build(); } diff --git a/examples/binaries/waiter/src/code.rs b/examples/binaries/waiter/src/code.rs index dcdab84035d..622df6a59ee 100644 --- a/examples/binaries/waiter/src/code.rs +++ b/examples/binaries/waiter/src/code.rs @@ -1,3 +1,21 @@ +// This file is part of Gear. + +// Copyright (C) 2021-2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use crate::{Command, WaitSubcommand}; use gstd::{errors::Error, exec, msg, MessageId}; @@ -17,33 +35,33 @@ async fn main() { match cmd { Command::Wait(subcommand) => process_wait_subcommand(subcommand), Command::SendFor(to, duration) => { - msg::send_bytes_for_reply(to, [], 0, 0) + msg::send_bytes_for_reply(to.into(), [], 0, 0) .expect("send message failed") .exactly(Some(duration)) .expect("Invalid wait duration.") .await; } Command::SendUpTo(to, duration) => { - msg::send_bytes_for_reply(to, [], 0, 0) + msg::send_bytes_for_reply(to.into(), [], 0, 0) .expect("send message failed") .up_to(Some(duration)) .expect("Invalid wait duration.") .await; } Command::SendUpToWait(to, duration) => { - msg::send_bytes_for_reply(to, [], 0, 0) + msg::send_bytes_for_reply(to.into(), [], 0, 0) .expect("send message failed") .up_to(Some(duration)) .expect("Invalid wait duration.") .await; // after waking, wait again. - msg::send_bytes_for_reply(to, [], 0, 0) + msg::send_bytes_for_reply(to.into(), [], 0, 0) .expect("send message failed") .await; } Command::SendAndWaitFor(duration, to) => { - msg::send(to, b"ping", 0); + msg::send(to.into(), b"ping", 0); exec::wait_for(duration); } Command::ReplyAndWait(subcommand) => { diff --git a/examples/binaries/waiter/src/lib.rs b/examples/binaries/waiter/src/lib.rs index 539f366e0d7..540f5cb6c84 100644 --- a/examples/binaries/waiter/src/lib.rs +++ b/examples/binaries/waiter/src/lib.rs @@ -15,29 +15,33 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -#![no_std] + +#![cfg_attr(not(feature = "std"), no_std)] use codec::{Decode, Encode}; -use gstd::ActorId; -#[cfg(feature = "std")] +type ActorId = [u8; 32]; + +#[cfg(feature = "wasm-wrapper")] mod code { include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); } -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] pub use code::WASM_BINARY_OPT as WASM_BINARY; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { include! {"./code.rs"} } +#[cfg(feature = "std")] pub fn system_reserve() -> u64 { gstd::Config::system_reserve() } // Re-exports for testing +#[cfg(feature = "std")] pub fn default_wait_up_to_duration() -> u32 { gstd::Config::wait_up_to() } diff --git a/gsdk/tests/rpc.rs b/gsdk/tests/rpc.rs index 3fe10b94575..237d22088bb 100644 --- a/gsdk/tests/rpc.rs +++ b/gsdk/tests/rpc.rs @@ -142,7 +142,7 @@ async fn test_calculate_reply_gas() -> Result<()> { let salt = vec![]; let pid = ProgramId::generate(CodeId::generate(demo_waiter::WASM_BINARY), &salt); - let payload = demo_waiter::Command::SendUpTo(alice.into(), 10); + let payload = demo_waiter::Command::SendUpTo(alice, 10); // 1. upload program. let signer = Api::new(Some(&node_uri(&node))) diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index c0b97c0bc46..808e7eac885 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -60,6 +60,7 @@ demo-proxy = { workspace = true, optional = true } demo-reserve-gas = { workspace = true, optional = true } demo-delayed-sender = { workspace = true, optional = true } demo-constructor = { workspace = true, optional = true } +demo-waiter = { workspace = true, optional = true } [dev-dependencies] wabt.workspace = true @@ -153,6 +154,7 @@ std = [ "demo-reserve-gas?/std", "demo-delayed-sender?/std", "demo-constructor?/std", + "demo-waiter?/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -172,6 +174,7 @@ runtime-benchmarks = [ "demo-reserve-gas/wasm-wrapper", "demo-delayed-sender/wasm-wrapper", "demo-constructor/wasm-wrapper", + "demo-waiter/wasm-wrapper", ] runtime-benchmarks-checkers = [] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index cc3a86b6cf5..dcaaca86f33 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -2875,6 +2875,55 @@ benchmarks! { ext_manager.send_dispatch(message_id); } + tasks_wake_message { + use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + vec![], + 10_000_000_000, 0u32.into()).expect("submit program failed"); + + let delay = 10u32; + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + Command::Wait(WaitSubcommand::WaitFor(delay)).encode(), + 10_000_000_000, + 0u32.into(), + ).expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .unwrap(); + let (_program_id, message_id) = match task { + ScheduledTask::WakeMessage ( + program_id, + message_id, + ) => (program_id, message_id), + _ => unreachable!(), + }; + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.wake_message(program_id, message_id); + } + + tasks_wake_message_no_wake { + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.wake_message(Default::default(), Default::default()); + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index a10d9e99238..4c79b183c19 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -31,7 +31,7 @@ use common::{ storage::*, Origin, PausedProgramStorage, Program, ProgramStorage, }; -use core::convert::TryInto; +use core::{cmp, convert::TryInto}; use frame_support::traits::{Currency, ExistenceRequirement}; use gear_core::{ ids::{CodeId, MessageId, ProgramId, ReservationId}, @@ -49,7 +49,10 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga RemoveFromMailbox(_, _) => 0, RemoveFromWaitlist(_, _) => 0, RemovePausedProgram(_) => todo!("#646"), - WakeMessage(_, _) => 0, + WakeMessage(_, _) => cmp::max( + ::WeightInfo::tasks_wake_message().ref_time(), + ::WeightInfo::tasks_wake_message_no_wake().ref_time(), + ), SendDispatch(_) => ::WeightInfo::tasks_send_dispatch().ref_time(), SendUserMessage { .. } => core::cmp::max( ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), @@ -296,16 +299,19 @@ where } fn wake_message(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas { - if let Some(dispatch) = Pallet::::wake_dispatch( + match Pallet::::wake_dispatch( program_id, message_id, MessageWokenRuntimeReason::WakeCalled.into_reason(), ) { - QueueOf::::queue(dispatch) - .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - } + Some(dispatch) => { + QueueOf::::queue(dispatch) + .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - 0 + ::WeightInfo::tasks_wake_message().ref_time() + } + None => ::WeightInfo::tasks_wake_message_no_wake().ref_time(), + } } fn send_dispatch(&mut self, stashed_message_id: MessageId) -> Gas { diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index 6753bde5a0d..1189d3292f0 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -4847,7 +4847,7 @@ fn test_requeue_after_wait_for_timeout() { run_to_next_block(None); let duration = 10; - let payload = Command::SendAndWaitFor(duration, USER_1.into()).encode(); + let payload = Command::SendAndWaitFor(duration, USER_1.into_origin().into()).encode(); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), program_id, @@ -4917,7 +4917,7 @@ fn test_sending_waits() { // // Send message and then wait_for. let duration = 5; - let payload = Command::SendFor(USER_1.into(), duration).encode(); + let payload = Command::SendFor(USER_1.into_origin().into(), duration).encode(); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), @@ -4936,7 +4936,7 @@ fn test_sending_waits() { // // Send message and then wait_up_to. let duration = 10; - let payload = Command::SendUpTo(USER_1.into(), duration).encode(); + let payload = Command::SendUpTo(USER_1.into_origin().into(), duration).encode(); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), program_id, @@ -4954,7 +4954,7 @@ fn test_sending_waits() { // // Send message and then wait no_more, wake, wait no_more again. let duration = 10; - let payload = Command::SendUpToWait(USER_2.into(), duration).encode(); + let payload = Command::SendUpToWait(USER_2.into_origin().into(), duration).encode(); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), program_id, diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 4505ad72091..7d15d978bef 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -219,6 +219,8 @@ pub trait WeightInfo { fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; fn tasks_send_dispatch() -> Weight; + fn tasks_wake_message() -> Weight; + fn tasks_wake_message_no_wake() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2105,6 +2107,23 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } } // For backwards compatibility and tests @@ -3985,4 +4004,21 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 34c5ce08000..21078031738 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -220,6 +220,8 @@ pub trait WeightInfo { fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; fn tasks_send_dispatch() -> Weight; + fn tasks_wake_message() -> Weight; + fn tasks_wake_message_no_wake() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2106,6 +2108,23 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } } // For backwards compatibility and tests @@ -3996,4 +4015,21 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index cb6379b1497..556609b9729 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -220,6 +220,8 @@ pub trait WeightInfo { fn tasks_send_user_message_to_mailbox() -> Weight; fn tasks_send_user_message() -> Weight; fn tasks_send_dispatch() -> Weight; + fn tasks_wake_message() -> Weight; + fn tasks_wake_message_no_wake() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2106,6 +2108,23 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(T::DbWeight::get().reads(8_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(T::DbWeight::get().reads(1_u64)) + } } // For backwards compatibility and tests @@ -3996,4 +4015,21 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } + fn tasks_wake_message() -> Weight { + // Proof Size summary in bytes: + // Measured: `823` + // Estimated: `25565` + // Minimum execution time: 20_000_000 picoseconds. + Weight::from_parts(21_000_000, 25565) + .saturating_add(RocksDbWeight::get().reads(8_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) + } + fn tasks_wake_message_no_wake() -> Weight { + // Proof Size summary in bytes: + // Measured: `80` + // Estimated: `3545` + // Minimum execution time: 2_000_000 picoseconds. + Weight::from_parts(2_000_000, 3545) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + } } From 95322847be9c61a9979c38d890457e3cbe38fabf Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 20 Jun 2023 17:40:26 +0400 Subject: [PATCH 12/32] fix gsdk deps --- gsdk/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gsdk/Cargo.toml b/gsdk/Cargo.toml index 28412f3ecf9..1a13c59c0b8 100644 --- a/gsdk/Cargo.toml +++ b/gsdk/Cargo.toml @@ -34,7 +34,7 @@ gsdk = { path = ".", features = ["testing"] } tokio = { workspace = true, features = [ "full" ] } demo-messager.workspace = true demo-new-meta.workspace = true -demo-waiter.workspace = true +demo-waiter = { workspace = true, features = ["std"] } [features] testing = [ "rand" ] From 82abcc8f85766966049190aab1e9cdfd84fc9db2 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 20 Jun 2023 18:29:53 +0400 Subject: [PATCH 13/32] tasks_remove_from_waitlist --- pallets/gear-scheduler/src/tests.rs | 10 +++- pallets/gear/src/benchmarking/mod.rs | 77 +++++++++++++++++++++++-- pallets/gear/src/manager/task.rs | 6 +- pallets/gear/src/weights.rs | 19 ++++++ runtime/gear/src/weights/pallet_gear.rs | 19 ++++++ runtime/vara/src/weights/pallet_gear.rs | 19 ++++++ 6 files changed, 139 insertions(+), 11 deletions(-) diff --git a/pallets/gear-scheduler/src/tests.rs b/pallets/gear-scheduler/src/tests.rs index 3f57e5da585..fa628dd5a16 100644 --- a/pallets/gear-scheduler/src/tests.rs +++ b/pallets/gear-scheduler/src/tests.rs @@ -197,12 +197,14 @@ fn gear_handles_tasks() { GasPrice::gas_price(DEFAULT_GAS) ); + let task = ScheduledTask::RemoveFromWaitlist(Default::default(), Default::default()); + let task_gas = pallet_gear::manager::get_maximum_task_gas::(&task); // Check if task and message got processed in block `bn`. run_to_block(bn, Some(u64::MAX)); // Read of the first block of incomplete tasks, read of the first task and write for removal of task. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(2, 1).ref_time() + u64::MAX - db_r_w(2, 1).ref_time() - task_gas ); // Storages checking. @@ -311,7 +313,9 @@ fn gear_handles_outdated_tasks() { // Check if task and message got processed before start of block `bn`. // But due to the low gas allowance, we may process the only first task. - run_to_block(bn, Some(db_r_w(2, 2).ref_time() + 1)); + let task = ScheduledTask::RemoveFromWaitlist(Default::default(), Default::default()); + let task_gas = pallet_gear::manager::get_maximum_task_gas::(&task); + run_to_block(bn, Some(db_r_w(2, 2).ref_time() + task_gas + 1)); // Read of the first block of incomplete tasks, write to it afterwards + single task processing. assert_eq!(GasAllowanceOf::::get(), 1); @@ -342,7 +346,7 @@ fn gear_handles_outdated_tasks() { // Delete of the first block of incomplete tasks + single DB read (task) + single task processing. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(1, 2).ref_time() + u64::MAX - db_r_w(1, 2).ref_time() - task_gas ); let cost2 = wl_cost_for(bn + 1 - initial_block); diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index dcaaca86f33..c51054ca124 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -241,13 +241,34 @@ where } fn get_last_session_id_if_any() -> Option { - let event_record = SystemPallet::::events().pop()?; - let event = <::RuntimeEvent as From<_>>::from(event_record.event); - let event: Result, _> = event.try_into(); - match event { - Ok(Event::ProgramResumeSessionStarted { session_id, .. }) => Some(session_id), + filter_event_reverse::(|event| match event { + Event::ProgramResumeSessionStarted { session_id, .. } => Some(session_id), _ => None, - } + }) +} + +fn get_last_waited_expiration_if_any() -> Option { + filter_event_reverse::(|event| match event { + Event::MessageWaited { expiration, .. } => Some(expiration), + _ => None, + }) +} + +pub fn filter_event_reverse(mapping_filter: F) -> Option +where + T: Config, + F: Fn(Event) -> Option, +{ + SystemPallet::::events() + .into_iter() + .rev() + .filter_map(|event_record| { + let event = <::RuntimeEvent as From<_>>::from(event_record.event); + let event: Result, _> = event.try_into(); + + event.ok() + }) + .find_map(mapping_filter) } fn resume_session_prepare( @@ -2924,6 +2945,50 @@ benchmarks! { ext_manager.wake_message(Default::default(), Default::default()); } + tasks_remove_from_waitlist { + use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + vec![], + 10_000_000_000, 0u32.into()).expect("submit program failed"); + + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + Command::Wait(WaitSubcommand::Wait).encode(), + 10_000_000_000, + 0u32.into(), + ).expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let expiration = get_last_waited_expiration_if_any::().expect("message should be waited"); + + let task = TaskPoolOf::::iter_prefix_keys(expiration) + .next() + .unwrap(); + let (_program_id, message_id) = match task { + ScheduledTask::RemoveFromWaitlist ( + program_id, + message_id, + ) => (program_id, message_id), + _ => unreachable!(), + }; + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.remove_from_waitlist(program_id, message_id); + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 4c79b183c19..a5acff7c6ef 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -47,7 +47,9 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga PauseProgram(_) => 0, RemoveCode(_) => todo!("#646"), RemoveFromMailbox(_, _) => 0, - RemoveFromWaitlist(_, _) => 0, + RemoveFromWaitlist(_, _) => { + ::WeightInfo::tasks_remove_from_waitlist().ref_time() + } RemovePausedProgram(_) => todo!("#646"), WakeMessage(_, _) => cmp::max( ::WeightInfo::tasks_wake_message().ref_time(), @@ -291,7 +293,7 @@ where // Consuming gas handler for waitlisted message. Pallet::::consume_and_retrieve(waitlisted.id()); - 0 + ::WeightInfo::tasks_remove_from_waitlist().ref_time() } fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 7d15d978bef..88880de402c 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -221,6 +221,7 @@ pub trait WeightInfo { fn tasks_send_dispatch() -> Weight; fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; + fn tasks_remove_from_waitlist() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2124,6 +2125,15 @@ impl WeightInfo for SubstrateWeight { Weight::from_parts(2_000_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4021,4 +4031,13 @@ impl WeightInfo for () { Weight::from_parts(2_000_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 21078031738..6877247bc75 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -222,6 +222,7 @@ pub trait WeightInfo { fn tasks_send_dispatch() -> Weight; fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; + fn tasks_remove_from_waitlist() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2125,6 +2126,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { Weight::from_parts(2_000_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4032,4 +4042,13 @@ impl WeightInfo for () { Weight::from_parts(2_000_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index 556609b9729..cdd1418c6cb 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -222,6 +222,7 @@ pub trait WeightInfo { fn tasks_send_dispatch() -> Weight; fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; + fn tasks_remove_from_waitlist() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2125,6 +2126,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { Weight::from_parts(2_000_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4032,4 +4042,13 @@ impl WeightInfo for () { Weight::from_parts(2_000_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } + fn tasks_remove_from_waitlist() -> Weight { + // Proof Size summary in bytes: + // Measured: `1200` + // Estimated: `47840` + // Minimum execution time: 41_000_000 picoseconds. + Weight::from_parts(44_000_000, 47840) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } From 4f514088d3b349e746ee92a1c61c77fbcecad17c Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 20 Jun 2023 19:10:19 +0400 Subject: [PATCH 14/32] tasks_remove_from_mailbox --- pallets/gear/src/benchmarking/mod.rs | 71 +++++++++++++++++-------- pallets/gear/src/manager/task.rs | 8 +-- pallets/gear/src/weights.rs | 19 +++++++ runtime/gear/src/weights/pallet_gear.rs | 19 +++++++ runtime/vara/src/weights/pallet_gear.rs | 19 +++++++ 5 files changed, 110 insertions(+), 26 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index c51054ca124..8f8e2ead7b5 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -247,13 +247,6 @@ fn get_last_session_id_if_any() -> Option { }) } -fn get_last_waited_expiration_if_any() -> Option { - filter_event_reverse::(|event| match event { - Event::MessageWaited { expiration, .. } => Some(expiration), - _ => None, - }) -} - pub fn filter_event_reverse(mapping_filter: F) -> Option where T: Config, @@ -303,7 +296,7 @@ where (get_last_session_id_if_any::().unwrap(), memory_pages) } -fn tasks_send_user_message_prepare() -> (MessageId, bool) +fn tasks_send_user_message_prepare(delay: u32) where T::AccountId: Origin, { @@ -317,7 +310,6 @@ where init_block::(None); - let delay = 1u32; let salt = vec![]; Gear::::upload_program( RawOrigin::Signed(caller).into(), @@ -330,17 +322,6 @@ where .expect("submit program failed"); Gear::::process_queue(Default::default()); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .unwrap(); - match task { - ScheduledTask::SendUserMessage { - message_id, - to_mailbox, - } => (message_id, to_mailbox), - _ => unreachable!(), - } } /// An instantiated and deployed program. @@ -2831,7 +2812,19 @@ benchmarks! { } tasks_send_user_message_to_mailbox { - let (message_id, to_mailbox) = tasks_send_user_message_prepare::(); + let delay = 1u32; + tasks_send_user_message_prepare::(delay); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .expect("task should be scheduled"); + let (message_id, to_mailbox) = match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!("task should be SendUserMessage"), + }; assert!(to_mailbox); let mut ext_manager = ExtManager::::default(); @@ -2840,7 +2833,20 @@ benchmarks! { } tasks_send_user_message { - let (message_id, to_mailbox) = tasks_send_user_message_prepare::(); + let delay = 1u32; + tasks_send_user_message_prepare::(delay); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .expect("task should be scheduled"); + let (message_id, to_mailbox) = match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!("task should be SendUserMessage"), + }; + assert!(to_mailbox); let mut ext_manager = ExtManager::::default(); }: { @@ -2971,7 +2977,10 @@ benchmarks! { Gear::::process_queue(Default::default()); - let expiration = get_last_waited_expiration_if_any::().expect("message should be waited"); + let expiration = filter_event_reverse::(|event| match event { + Event::MessageWaited { expiration, .. } => Some(expiration), + _ => None, + }).expect("message should be waited"); let task = TaskPoolOf::::iter_prefix_keys(expiration) .next() @@ -2989,6 +2998,22 @@ benchmarks! { ext_manager.remove_from_waitlist(program_id, message_id); } + tasks_remove_from_mailbox { + tasks_send_user_message_prepare::(0u32); + + let (user, message_id) = filter_event_reverse::(|event| match event { + Event::UserMessageSent { + message, + .. + } => Some((message.destination(), message.id())), + _ => None, + }).expect("message should be sent"); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.remove_from_mailbox(T::AccountId::from_origin(user.into_origin()), message_id); + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index a5acff7c6ef..93df9815c58 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -46,7 +46,9 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga match task { PauseProgram(_) => 0, RemoveCode(_) => todo!("#646"), - RemoveFromMailbox(_, _) => 0, + RemoveFromMailbox(_, _) => { + ::WeightInfo::tasks_remove_from_mailbox().ref_time() + } RemoveFromWaitlist(_, _) => { ::WeightInfo::tasks_remove_from_waitlist().ref_time() } @@ -56,7 +58,7 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga ::WeightInfo::tasks_wake_message_no_wake().ref_time(), ), SendDispatch(_) => ::WeightInfo::tasks_send_dispatch().ref_time(), - SendUserMessage { .. } => core::cmp::max( + SendUserMessage { .. } => cmp::max( ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(), ::WeightInfo::tasks_send_user_message().ref_time(), ), @@ -220,7 +222,7 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - 0 + ::WeightInfo::tasks_remove_from_mailbox().ref_time() } fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 88880de402c..120b0c3ddfd 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -222,6 +222,7 @@ pub trait WeightInfo { fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; + fn tasks_remove_from_mailbox() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2134,6 +2135,15 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(14_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(T::DbWeight::get().reads(12_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4040,4 +4050,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(14_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(RocksDbWeight::get().reads(12_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 6877247bc75..d05adf14607 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -223,6 +223,7 @@ pub trait WeightInfo { fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; + fn tasks_remove_from_mailbox() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2135,6 +2136,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(14_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(T::DbWeight::get().reads(12_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4051,4 +4061,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(14_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(RocksDbWeight::get().reads(12_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index cdd1418c6cb..40d3b6866c1 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -223,6 +223,7 @@ pub trait WeightInfo { fn tasks_wake_message() -> Weight; fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; + fn tasks_remove_from_mailbox() -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2135,6 +2136,15 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(14_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(T::DbWeight::get().reads(12_u64)) + .saturating_add(T::DbWeight::get().writes(11_u64)) + } } // For backwards compatibility and tests @@ -4051,4 +4061,13 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(14_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + fn tasks_remove_from_mailbox() -> Weight { + // Proof Size summary in bytes: + // Measured: `907` + // Estimated: `37005` + // Minimum execution time: 30_000_000 picoseconds. + Weight::from_parts(33_000_000, 37005) + .saturating_add(RocksDbWeight::get().reads(12_u64)) + .saturating_add(RocksDbWeight::get().writes(11_u64)) + } } From c4821bc36b3b577a090abc723ca3648ccb907ec9 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Thu, 22 Jun 2023 10:31:59 +0300 Subject: [PATCH 15/32] tasks_pause_program --- Cargo.toml | 2 +- examples/binaries/init-wait/Cargo.toml | 9 ++-- examples/binaries/init-wait/build.rs | 6 ++- examples/binaries/init-wait/src/code.rs | 18 +++++++ examples/binaries/init-wait/src/lib.rs | 24 ++++++++-- pallets/gear/Cargo.toml | 3 ++ pallets/gear/src/benchmarking/mod.rs | 60 ++++++++++++++++++++++++ pallets/gear/src/manager/task.rs | 19 ++++++-- pallets/gear/src/weights.rs | 62 +++++++++++++++++++++++++ runtime/gear/src/weights/pallet_gear.rs | 62 +++++++++++++++++++++++++ runtime/vara/src/weights/pallet_gear.rs | 62 +++++++++++++++++++++++++ 11 files changed, 314 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1cf11c973e4..78038a39968 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -352,7 +352,7 @@ demo-gas-burned = { path = "examples/binaries/gas-burned" } demo-fungible-token = { path = "examples/binaries/fungible-token" } demo-incomplete-async-payloads = { path = "examples/binaries/incomplete-async-payloads" } demo-init-fail-sender = { path = "examples/binaries/init-fail-sender" } -demo-init-wait = { path = "examples/binaries/init-wait" } +demo-init-wait = { path = "examples/binaries/init-wait", default-features = false } demo-init-wait-reply-exit = { path = "examples/binaries/init-wait-reply-exit" } demo-messager = { path = "examples/binaries/messager" } demo-meta-io = { path = "examples/binaries/new-meta/io" } diff --git a/examples/binaries/init-wait/Cargo.toml b/examples/binaries/init-wait/Cargo.toml index ad1e3cd5d4d..f797445a305 100644 --- a/examples/binaries/init-wait/Cargo.toml +++ b/examples/binaries/init-wait/Cargo.toml @@ -2,8 +2,8 @@ name = "demo-init-wait" version = "0.1.0" authors.workspace = true -edition = "2021" -license = "GPL-3.0" +edition.workspace = true +license.workspace = true workspace = "../../../" [dependencies] @@ -12,9 +12,8 @@ gstd.workspace = true [build-dependencies] gear-wasm-builder.workspace = true -[lib] - [features] debug = ["gstd/debug"] -std = [] +wasm-wrapper = [] +std = ["wasm-wrapper"] default = ["std"] diff --git a/examples/binaries/init-wait/build.rs b/examples/binaries/init-wait/build.rs index 4c502a3ddee..b911ce127bb 100644 --- a/examples/binaries/init-wait/build.rs +++ b/examples/binaries/init-wait/build.rs @@ -16,6 +16,10 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . +use gear_wasm_builder::WasmBuilder; + fn main() { - gear_wasm_builder::build(); + WasmBuilder::new() + .exclude_features(vec!["std", "wasm-wrapper"]) + .build(); } diff --git a/examples/binaries/init-wait/src/code.rs b/examples/binaries/init-wait/src/code.rs index 9d51084eab1..bde8c7cee2e 100644 --- a/examples/binaries/init-wait/src/code.rs +++ b/examples/binaries/init-wait/src/code.rs @@ -1,3 +1,21 @@ +// This file is part of Gear. + +// Copyright (C) 2021-2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + use gstd::{exec, msg, BTreeMap, MessageId}; #[derive(PartialEq, Debug)] diff --git a/examples/binaries/init-wait/src/lib.rs b/examples/binaries/init-wait/src/lib.rs index e5e4ef17484..a4fc7ea8b42 100644 --- a/examples/binaries/init-wait/src/lib.rs +++ b/examples/binaries/init-wait/src/lib.rs @@ -1,14 +1,32 @@ +// This file is part of Gear. + +// Copyright (C) 2021-2023 Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + #![cfg_attr(not(feature = "std"), no_std)] -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] mod code { include!(concat!(env!("OUT_DIR"), "/wasm_binary.rs")); } -#[cfg(feature = "std")] +#[cfg(feature = "wasm-wrapper")] pub use code::WASM_BINARY_OPT as WASM_BINARY; -#[cfg(not(feature = "std"))] +#[cfg(not(feature = "wasm-wrapper"))] mod wasm { include! {"./code.rs"} } diff --git a/pallets/gear/Cargo.toml b/pallets/gear/Cargo.toml index 808e7eac885..a131f9cd4e1 100644 --- a/pallets/gear/Cargo.toml +++ b/pallets/gear/Cargo.toml @@ -61,6 +61,7 @@ demo-reserve-gas = { workspace = true, optional = true } demo-delayed-sender = { workspace = true, optional = true } demo-constructor = { workspace = true, optional = true } demo-waiter = { workspace = true, optional = true } +demo-init-wait = { workspace = true, optional = true } [dev-dependencies] wabt.workspace = true @@ -155,6 +156,7 @@ std = [ "demo-delayed-sender?/std", "demo-constructor?/std", "demo-waiter?/std", + "demo-init-wait?/std", ] runtime-benchmarks = [ "frame-benchmarking/runtime-benchmarks", @@ -175,6 +177,7 @@ runtime-benchmarks = [ "demo-delayed-sender/wasm-wrapper", "demo-constructor/wasm-wrapper", "demo-waiter/wasm-wrapper", + "demo-init-wait/wasm-wrapper", ] runtime-benchmarks-checkers = [] try-runtime = ["frame-support/try-runtime"] diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 8f8e2ead7b5..87b886ea9fb 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -264,6 +264,7 @@ where .find_map(mapping_filter) } +#[track_caller] fn resume_session_prepare( c: u32, program_id: ProgramId, @@ -296,6 +297,7 @@ where (get_last_session_id_if_any::().unwrap(), memory_pages) } +#[track_caller] fn tasks_send_user_message_prepare(delay: u32) where T::AccountId: Origin, @@ -324,6 +326,43 @@ where Gear::::process_queue(Default::default()); } +#[track_caller] +fn tasks_pause_program_prepare(c: u32, code: Vec) -> ProgramId +where + T::AccountId: Origin, +{ + let caller = benchmarking::account("caller", 0, 0); + ::Currency::deposit_creating(&caller, 400_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(&code), &salt); + Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), code, salt, b"init_payload".to_vec(), 10_000_000_000, 0u32.into()).expect("submit program failed"); + + Gear::::process_queue(Default::default()); + + let memory_page = { + let mut page = PageBuf::new_zeroed(); + page[0] = 1; + + page + }; + + for i in 0 .. c { + ProgramStorageOf::::set_program_page_data(program_id, GearPage::from(i as u16), memory_page.clone()); + } + + ProgramStorageOf::::update_active_program(program_id, |program| { + program.pages_with_data = BTreeSet::from_iter((0..c).map(|i| GearPage::from(i as u16))); + + let wasm_pages = (c as usize * GEAR_PAGE_SIZE) / WASM_PAGE_SIZE; + program.allocations = BTreeSet::from_iter((0..wasm_pages).map(|i| WasmPage::from(i as u16))); + }).expect("program should exist"); + + program_id +} + /// An instantiated and deployed program. #[derive(Clone)] struct Program { @@ -3014,6 +3053,27 @@ benchmarks! { ext_manager.remove_from_mailbox(T::AccountId::from_origin(user.into_origin()), message_id); } + tasks_pause_program { + let c in 0 .. (MAX_PAGES - 1) * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u32; + + let code = benchmarking::generate_wasm2(0.into()).unwrap(); + let program_id = tasks_pause_program_prepare::(c, code); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.pause_program(program_id); + } + + tasks_pause_program_uninited { + let c in 0 .. (MAX_PAGES - 1) * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u32; + + let program_id = tasks_pause_program_prepare::(c, demo_init_wait::WASM_BINARY.to_vec()); + + let mut ext_manager = ExtManager::::default(); + }: { + ext_manager.pause_program(program_id); + } + // This is no benchmark. It merely exist to have an easy way to pretty print the currently // configured `Schedule` during benchmark development. // It can be outputted using the following command: diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 93df9815c58..7aed84610d7 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -36,6 +36,8 @@ use frame_support::traits::{Currency, ExistenceRequirement}; use gear_core::{ ids::{CodeId, MessageId, ProgramId, ReservationId}, message::{DispatchKind, ReplyMessage}, + code::MAX_WASM_PAGE_COUNT, + memory::{GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, }; use gear_core_errors::{SimpleReplyError, SimpleSignalError}; use sp_runtime::traits::Zero; @@ -44,7 +46,13 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga use ScheduledTask::*; match task { - PauseProgram(_) => 0, + PauseProgram(_) => { + let count = u32::from(MAX_WASM_PAGE_COUNT * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 / 2); + cmp::max( + ::WeightInfo::tasks_pause_program(count).ref_time(), + ::WeightInfo::tasks_pause_program_uninited(count).ref_time() + ) + } RemoveCode(_) => todo!("#646"), RemoveFromMailbox(_, _) => { ::WeightInfo::tasks_remove_from_mailbox().ref_time() @@ -81,6 +89,11 @@ where let program = ProgramStorageOf::::get_program(program_id) .unwrap_or_else(|| unreachable!("Program to pause not found.")); + let pages_with_data = match program { + Program::Active(ref p) => p.pages_with_data.len() as u32, + _ => unreachable!("Pause program task logic corrupted!"), + }; + let Some(init_message_id) = program.is_uninitialized() else { // pause initialized program let gas_reservation_map = ProgramStorageOf::::pause_program(program_id, Pallet::::block_number()) @@ -101,7 +114,7 @@ where change: ProgramChangeKind::Paused, }); - return 0; + return ::WeightInfo::tasks_pause_program(pages_with_data).ref_time(); }; // terminate uninitialized program @@ -174,7 +187,7 @@ where Pallet::::deposit_event(event); - 0 + ::WeightInfo::tasks_pause_program_uninited(pages_with_data).ref_time() } fn remove_code(&mut self, _code_id: CodeId) -> Gas { diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 120b0c3ddfd..66a15fce1ea 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -223,6 +223,8 @@ pub trait WeightInfo { fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; fn tasks_remove_from_mailbox() -> Weight; + fn tasks_pause_program(c: u32, ) -> Weight; + fn tasks_pause_program_uninited(c: u32, ) -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2144,6 +2146,36 @@ impl WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(12_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(9_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } // For backwards compatibility and tests @@ -4059,4 +4091,34 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(12_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(9_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index d05adf14607..6775be7d123 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -224,6 +224,8 @@ pub trait WeightInfo { fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; fn tasks_remove_from_mailbox() -> Weight; + fn tasks_pause_program(c: u32, ) -> Weight; + fn tasks_pause_program_uninited(c: u32, ) -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2145,6 +2147,36 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(12_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(9_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } // For backwards compatibility and tests @@ -4070,4 +4102,34 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(12_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(9_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index 40d3b6866c1..9193d4548e9 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -224,6 +224,8 @@ pub trait WeightInfo { fn tasks_wake_message_no_wake() -> Weight; fn tasks_remove_from_waitlist() -> Weight; fn tasks_remove_from_mailbox() -> Weight; + fn tasks_pause_program(c: u32, ) -> Weight; + fn tasks_pause_program_uninited(c: u32, ) -> Weight; fn allocation_cost() -> Weight; fn grow_cost() -> Weight; fn initial_cost() -> Weight; @@ -2145,6 +2147,36 @@ impl pallet_gear::WeightInfo for SubstrateWeight { .saturating_add(T::DbWeight::get().reads(12_u64)) .saturating_add(T::DbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(4_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(2_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(T::DbWeight::get().writes(9_u64)) + .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } // For backwards compatibility and tests @@ -4070,4 +4102,34 @@ impl WeightInfo for () { .saturating_add(RocksDbWeight::get().reads(12_u64)) .saturating_add(RocksDbWeight::get().writes(11_u64)) } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2200 + c * (16400 ±0)` + // Estimated: `19363 + c * (84480 ±0)` + // Minimum execution time: 39_946_000 picoseconds. + Weight::from_parts(40_607_000, 19363) + // Standard Error: 46_882 + .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(4_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(2_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 84480).saturating_mul(c.into())) + } + /// The range of component `c` is `[0, 2044]`. + fn tasks_pause_program_uninited(c: u32, ) -> Weight { + // Proof Size summary in bytes: + // Measured: `2975 + c * (42 ±0)` + // Estimated: `61356 + c * (2947 ±0)` + // Minimum execution time: 104_317_000 picoseconds. + Weight::from_parts(160_861_611, 61356) + // Standard Error: 3_255 + .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) + .saturating_add(RocksDbWeight::get().writes(9_u64)) + .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) + .saturating_add(Weight::from_parts(0, 2947).saturating_mul(c.into())) + } } From f6b1c7ec43c0ff6c291029140954fa96c86a3f60 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 26 Jun 2023 16:12:36 +0400 Subject: [PATCH 16/32] make pre-commit --- pallets/gear/src/benchmarking/mod.rs | 29 ++++++++++++++++++++++------ pallets/gear/src/manager/task.rs | 9 +++++---- pallets/gear/src/mock.rs | 4 ++-- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 87b886ea9fb..1892ecdaae4 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -332,13 +332,24 @@ where T::AccountId: Origin, { let caller = benchmarking::account("caller", 0, 0); - ::Currency::deposit_creating(&caller, 400_000_000_000_000u128.unique_saturated_into()); + ::Currency::deposit_creating( + &caller, + 400_000_000_000_000u128.unique_saturated_into(), + ); init_block::(None); let salt = vec![]; let program_id = ProgramId::generate(CodeId::generate(&code), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), code, salt, b"init_payload".to_vec(), 10_000_000_000, 0u32.into()).expect("submit program failed"); + Gear::::upload_program( + RawOrigin::Signed(caller).into(), + code, + salt, + b"init_payload".to_vec(), + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); Gear::::process_queue(Default::default()); @@ -349,16 +360,22 @@ where page }; - for i in 0 .. c { - ProgramStorageOf::::set_program_page_data(program_id, GearPage::from(i as u16), memory_page.clone()); + for i in 0..c { + ProgramStorageOf::::set_program_page_data( + program_id, + GearPage::from(i as u16), + memory_page.clone(), + ); } ProgramStorageOf::::update_active_program(program_id, |program| { program.pages_with_data = BTreeSet::from_iter((0..c).map(|i| GearPage::from(i as u16))); let wasm_pages = (c as usize * GEAR_PAGE_SIZE) / WASM_PAGE_SIZE; - program.allocations = BTreeSet::from_iter((0..wasm_pages).map(|i| WasmPage::from(i as u16))); - }).expect("program should exist"); + program.allocations = + BTreeSet::from_iter((0..wasm_pages).map(|i| WasmPage::from(i as u16))); + }) + .expect("program should exist"); program_id } diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 7aed84610d7..c752ef2ebe2 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -34,10 +34,10 @@ use common::{ use core::{cmp, convert::TryInto}; use frame_support::traits::{Currency, ExistenceRequirement}; use gear_core::{ - ids::{CodeId, MessageId, ProgramId, ReservationId}, - message::{DispatchKind, ReplyMessage}, code::MAX_WASM_PAGE_COUNT, + ids::{CodeId, MessageId, ProgramId, ReservationId}, memory::{GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, + message::{DispatchKind, ReplyMessage}, }; use gear_core_errors::{SimpleReplyError, SimpleSignalError}; use sp_runtime::traits::Zero; @@ -47,10 +47,11 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga match task { PauseProgram(_) => { - let count = u32::from(MAX_WASM_PAGE_COUNT * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 / 2); + let count = + u32::from(MAX_WASM_PAGE_COUNT * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 / 2); cmp::max( ::WeightInfo::tasks_pause_program(count).ref_time(), - ::WeightInfo::tasks_pause_program_uninited(count).ref_time() + ::WeightInfo::tasks_pause_program_uninited(count).ref_time(), ) } RemoveCode(_) => todo!("#646"), diff --git a/pallets/gear/src/mock.rs b/pallets/gear/src/mock.rs index 263084303e6..fd12f2fc8b1 100644 --- a/pallets/gear/src/mock.rs +++ b/pallets/gear/src/mock.rs @@ -49,7 +49,7 @@ pub(crate) const USER_3: AccountId = 3; pub(crate) const LOW_BALANCE_USER: AccountId = 4; pub(crate) const BLOCK_AUTHOR: AccountId = 255; const NORMAL_DISPATCH_RATIO: Perbill = Perbill::from_percent(75); -const MAX_BLOCK: u64 = 100_000_000_000; +const MAX_BLOCK: u64 = 300_000_000_000; macro_rules! dry_run { ( @@ -237,7 +237,7 @@ pub fn new_test_ext() -> sp_io::TestExternalities { pallet_balances::GenesisConfig:: { balances: vec![ (USER_1, 5_000_000_000_000_000_u128), - (USER_2, 200_000_000_000_000_u128), + (USER_2, 350_000_000_000_000_u128), (USER_3, 500_000_000_000_000_u128), (LOW_BALANCE_USER, 1_000_000_u128), (BLOCK_AUTHOR, 500_000_u128), From cb57cd2006d519a3d398918290eef4e7d14ff143 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 26 Jun 2023 18:07:40 +0400 Subject: [PATCH 17/32] add upper bound on block weight in execution_over_blocks test case --- pallets/gear/src/tests.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index c2393f363c9..4c9c5df53fd 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -8350,6 +8350,8 @@ fn free_storage_hold_on_scheduler_overwhelm() { #[test] fn execution_over_blocks() { + const MAX_BLOCK: u64 = 10_000_000_000; + init_logger(); let assert_last_message = |src: [u8; 32], count: u128| { @@ -8390,7 +8392,7 @@ fn execution_over_blocks() { )); let in_one_block = get_last_program_id(); - run_to_next_block(None); + run_to_next_block(Some(MAX_BLOCK)); // estimate start cost let pkg = Package::new(times, src); @@ -8411,7 +8413,7 @@ fn execution_over_blocks() { use demo_calc_hash_in_one_block::{Package, WASM_BINARY}; // We suppose that gas limit is less than gas allowance - let block_gas_limit = BlockGasLimitOf::::get() - 10000; + let block_gas_limit = MAX_BLOCK - 10_000; // Deploy demo-calc-hash-in-one-block. assert_ok!(Gear::upload_program( @@ -8436,20 +8438,20 @@ fn execution_over_blocks() { 0, )); - run_to_next_block(None); + run_to_next_block(Some(MAX_BLOCK)); assert_last_message([0; 32], 128); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), in_one_block, - Package::new(17_384, src).encode(), + Package::new(1_024, src).encode(), block_gas_limit, 0, )); let message_id = get_last_message_id(); - run_to_next_block(None); + run_to_next_block(Some(MAX_BLOCK)); assert_failed( message_id, @@ -8460,7 +8462,7 @@ fn execution_over_blocks() { new_test_ext().execute_with(|| { use demo_calc_hash::sha2_512_256; use demo_calc_hash_over_blocks::{Method, WASM_BINARY}; - let block_gas_limit = BlockGasLimitOf::::get(); + let block_gas_limit = MAX_BLOCK; let (_, calc_threshold) = estimate_gas_per_calc(); @@ -8470,25 +8472,25 @@ fn execution_over_blocks() { WASM_BINARY.to_vec(), DEFAULT_SALT.to_vec(), calc_threshold.encode(), - 10_000_000_000, + 9_000_000_000, 0, )); let over_blocks = get_last_program_id(); assert!(ProgramStorageOf::::program_exists(over_blocks)); - let (src, id, expected) = ([0; 32], sha2_512_256(b"42"), 8_192); + let (src, id, expected) = ([0; 32], sha2_512_256(b"42"), 448); // trigger calculation assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), over_blocks, Method::Start { src, id, expected }.encode(), - 10_000_000_000, + 9_000_000_000, 0, )); - run_to_next_block(None); + run_to_next_block(Some(MAX_BLOCK)); let mut count = 0; loop { @@ -8507,7 +8509,7 @@ fn execution_over_blocks() { )); count += 1; - run_to_next_block(None); + run_to_next_block(Some(MAX_BLOCK)); } assert!(count > 1); From 34f675fbe4d945bb032206d28599165053a2ff97 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 27 Jun 2023 10:49:48 +0400 Subject: [PATCH 18/32] feature gate gstd part of demo-constructor --- examples/binaries/constructor/src/scheme/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/examples/binaries/constructor/src/scheme/mod.rs b/examples/binaries/constructor/src/scheme/mod.rs index e2365ab4684..f86ad8bd106 100644 --- a/examples/binaries/constructor/src/scheme/mod.rs +++ b/examples/binaries/constructor/src/scheme/mod.rs @@ -5,6 +5,7 @@ use parity_scale_codec::{Decode, Encode}; pub mod demo_exit_handle; pub mod demo_exit_init; pub mod demo_ping; +#[cfg(not(feature = "wasm-wrapper"))] pub mod demo_proxy_with_gas; pub mod demo_reply_deposit; pub mod demo_value_sender; From 23314d4779b73fd505f159dd9418e28fce96c448 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 27 Jun 2023 11:32:23 +0400 Subject: [PATCH 19/32] use gcore in demo-constructor, adjust test case --- Cargo.lock | 1 + examples/binaries/constructor/Cargo.toml | 1 + .../binaries/constructor/src/scheme/demo_proxy_with_gas.rs | 2 +- examples/binaries/constructor/src/scheme/mod.rs | 1 - pallets/gear/src/tests.rs | 5 +++-- 5 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c45ba6245c..b685d344205 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1900,6 +1900,7 @@ dependencies = [ name = "demo-constructor" version = "0.1.0" dependencies = [ + "gcore", "gear-wasm-builder", "gstd", "hex", diff --git a/examples/binaries/constructor/Cargo.toml b/examples/binaries/constructor/Cargo.toml index f37d28fa650..3d1720ec6db 100644 --- a/examples/binaries/constructor/Cargo.toml +++ b/examples/binaries/constructor/Cargo.toml @@ -7,6 +7,7 @@ license.workspace = true workspace = "../../../" [dependencies] +gcore.workspace = true gstd.workspace = true parity-scale-codec = { workspace = true, features = ["derive"] } hex.workspace = true diff --git a/examples/binaries/constructor/src/scheme/demo_proxy_with_gas.rs b/examples/binaries/constructor/src/scheme/demo_proxy_with_gas.rs index 35c33e3b6c1..70b7e2a1ba5 100644 --- a/examples/binaries/constructor/src/scheme/demo_proxy_with_gas.rs +++ b/examples/binaries/constructor/src/scheme/demo_proxy_with_gas.rs @@ -1,5 +1,5 @@ use crate::{Arg, Call, Calls, Scheme}; -use gstd::errors::{ReplyCode, SuccessReplyReason}; +use gcore::errors::{ReplyCode, SuccessReplyReason}; use parity_scale_codec::Encode; pub const PROXIED_MESSAGE: &[u8] = b"proxied message"; diff --git a/examples/binaries/constructor/src/scheme/mod.rs b/examples/binaries/constructor/src/scheme/mod.rs index f86ad8bd106..e2365ab4684 100644 --- a/examples/binaries/constructor/src/scheme/mod.rs +++ b/examples/binaries/constructor/src/scheme/mod.rs @@ -5,7 +5,6 @@ use parity_scale_codec::{Decode, Encode}; pub mod demo_exit_handle; pub mod demo_exit_init; pub mod demo_ping; -#[cfg(not(feature = "wasm-wrapper"))] pub mod demo_proxy_with_gas; pub mod demo_reply_deposit; pub mod demo_value_sender; diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index 4c9c5df53fd..36fed6a6247 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -8430,17 +8430,18 @@ fn execution_over_blocks() { let src = [0; 32]; + let expected = 64; assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), in_one_block, - Package::new(128, src).encode(), + Package::new(expected, src).encode(), block_gas_limit, 0, )); run_to_next_block(Some(MAX_BLOCK)); - assert_last_message([0; 32], 128); + assert_last_message([0; 32], expected); assert_ok!(Gear::send_message( RuntimeOrigin::signed(USER_1), From 0542d321f445e8c52468b6383e1aa319d26e2675 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 27 Jun 2023 12:34:28 +0400 Subject: [PATCH 20/32] adjust test overblocks --- pallets/gear/src/tests.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pallets/gear/src/tests.rs b/pallets/gear/src/tests.rs index 36fed6a6247..8a50adfb536 100644 --- a/pallets/gear/src/tests.rs +++ b/pallets/gear/src/tests.rs @@ -8480,7 +8480,7 @@ fn execution_over_blocks() { assert!(ProgramStorageOf::::program_exists(over_blocks)); - let (src, id, expected) = ([0; 32], sha2_512_256(b"42"), 448); + let (src, id, expected) = ([0; 32], sha2_512_256(b"42"), 512); // trigger calculation assert_ok!(Gear::send_message( From 14196ec15dde7529a0512fda70bd181147ec813d Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 11 Jul 2023 11:14:37 +0400 Subject: [PATCH 21/32] make fmt --- examples/binaries/waiter/src/lib.rs | 2 +- pallets/gear/src/manager/task.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/binaries/waiter/src/lib.rs b/examples/binaries/waiter/src/lib.rs index 29b55ad9ed4..a17ab9feff0 100644 --- a/examples/binaries/waiter/src/lib.rs +++ b/examples/binaries/waiter/src/lib.rs @@ -20,8 +20,8 @@ extern crate alloc; -use codec::{Decode, Encode}; use alloc::vec::Vec; +use codec::{Decode, Encode}; type ActorId = [u8; 32]; diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 2e2afefca8b..5944786bfca 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -36,8 +36,8 @@ use frame_support::traits::{Currency, ExistenceRequirement}; use gear_core::{ code::MAX_WASM_PAGE_COUNT, ids::{CodeId, MessageId, ProgramId, ReservationId}, - pages::{GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, message::{DispatchKind, ReplyMessage}, + pages::{GEAR_PAGE_SIZE, WASM_PAGE_SIZE}, }; use gear_core_errors::{ErrorReplyReason, SignalCode}; use sp_runtime::traits::Zero; From 2cd29d30bcc51e12bc3142521a954cd00e0f9d6c Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Tue, 11 Jul 2023 12:03:00 +0400 Subject: [PATCH 22/32] set features for constructor dependency --- gclient/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gclient/Cargo.toml b/gclient/Cargo.toml index 5fcf184bf19..68d07689d7a 100644 --- a/gclient/Cargo.toml +++ b/gclient/Cargo.toml @@ -38,7 +38,7 @@ demo-btree.workspace = true demo-calc-hash.workspace = true demo-calc-hash-in-one-block.workspace = true demo-capacitor.workspace = true -demo-constructor.workspace = true +demo-constructor = { workspace = true, features = ["std"] } demo-distributor.workspace = true demo-meta-io.workspace = true demo-mul-by-const.workspace = true From 8867de27efa7d4257d6e14f528cdbb262aa9cf4b Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 8 Sep 2023 10:52:46 +0400 Subject: [PATCH 23/32] use drain iterator --- pallets/gear-scheduler/src/tests.rs | 8 ++-- pallets/gear/src/lib.rs | 62 ++++++++++++++++------------- 2 files changed, 39 insertions(+), 31 deletions(-) diff --git a/pallets/gear-scheduler/src/tests.rs b/pallets/gear-scheduler/src/tests.rs index 76bda8da141..535bd073192 100644 --- a/pallets/gear-scheduler/src/tests.rs +++ b/pallets/gear-scheduler/src/tests.rs @@ -200,10 +200,10 @@ fn gear_handles_tasks() { let task_gas = pallet_gear::manager::get_maximum_task_gas::(&task); // Check if task and message got processed in block `bn`. run_to_block(bn, Some(u64::MAX)); - // Read of the first block of incomplete tasks, read of the first task and write for removal of task. + // Read of the first block of incomplete tasks and write for removal of task. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(2, 1).ref_time() - task_gas + u64::MAX - db_r_w(1, 1).ref_time() - task_gas ); // Storages checking. @@ -342,10 +342,10 @@ fn gear_handles_outdated_tasks() { // Check if missed task and message got processed in block `bn`. run_to_block(bn + 1, Some(u64::MAX)); - // Delete of the first block of incomplete tasks + single DB read (task) + single task processing. + // Delete of the first block of incomplete tasks + single task processing. assert_eq!( GasAllowanceOf::::get(), - u64::MAX - db_r_w(1, 2).ref_time() - task_gas + u64::MAX - db_r_w(0, 2).ref_time() - task_gas ); let cost2 = wl_cost_for(bn + 1 - initial_block); diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index a69c5230d1b..14991e17dbc 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -128,9 +128,6 @@ pub type ResumeSessionDurationOf = ::ProgramResumeSessionDuratio pub(crate) type VoucherOf = ::Voucher; pub(crate) type GearBank = pallet_gear_bank::Pallet; -// Estimated count of tasks in the single block to process. -const MAX_TASK_COUNT: usize = 1_000; - /// The current storage version. const GEAR_STORAGE_VERSION: StorageVersion = StorageVersion::new(1); @@ -946,52 +943,55 @@ pub mod pallet { ..=current_bn.saturated_into()) .map(|block| block.saturated_into::>()); for bn in missing_blocks { + let tasks = TaskPoolOf::::drain_prefix_keys(bn); + // Checking gas allowance. // - // Making sure we have gas to read next task + // Making sure we have gas to remove next task // or update the first block of incomplete tasks. - if GasAllowanceOf::::get() - <= DbWeightOf::::get().reads_writes(1, 1).ref_time() - { + if GasAllowanceOf::::get() <= DbWeightOf::::get().writes(2).ref_time() { stopped_at = Some(bn); log::debug!("Stopping processing tasks at: {stopped_at:?}"); break; } - let tasks = TaskPoolOf::::iter_prefix_keys(bn); - let mut tasks_to_delete = Vec::with_capacity(MAX_TASK_COUNT); + let gas_delta = DbWeightOf::::get() + .writes(1) + .ref_time() + .saturating_sub(DbWeightOf::::get().reads(1).ref_time()); // Iterating over tasks, scheduled on `bn`. + let mut last_task = None; for task in tasks { - // Decreasing gas allowance due to read from DB. + // Decreasing gas allowance due to DB read. GasAllowanceOf::::decrease(DbWeightOf::::get().reads(1).ref_time()); - // gas required to process task and to remove it from DB. - let task_gas = manager::get_maximum_task_gas::(&task) - .saturating_add(DbWeightOf::::get().writes(1).ref_time()); - log::debug!("Processing task: {:?}, gas = {task_gas}", task); + // gas required to process task. + let max_task_gas = manager::get_maximum_task_gas::(&task); + log::debug!("Processing task: {task:?}, max gas = {max_task_gas}"); // Checking gas allowance. // - // Making sure we have gas to update the first block of incomplete tasks. - if GasAllowanceOf::::get().saturating_sub(task_gas) - <= DbWeightOf::::get().writes(1).ref_time() + // Making sure we have gas to process the current task + // and update the first block of incomplete tasks. + if GasAllowanceOf::::get().saturating_sub(max_task_gas) + <= DbWeightOf::::get() + .writes(1) + .ref_time() + .saturating_add(gas_delta) { - stopped_at = Some(bn); - log::debug!("Not enough gas to process task at: {stopped_at:?}"); + last_task = Some(task); + log::debug!("Not enough gas to process task at: {bn:?}"); break; } // Processing task and update allowance of gas. - tasks_to_delete.push((bn, task.clone())); let task_gas = task.process_with(ext_manager); - GasAllowanceOf::::decrease(task_gas); + GasAllowanceOf::::decrease(task_gas + gas_delta); // Check that there is enough gas allowance to read next task, update the first block of incomplete tasks and remove already processed tasks. if GasAllowanceOf::::get() - <= DbWeightOf::::get() - .reads_writes(1, 1 + tasks_to_delete.len() as u64) - .ref_time() + <= DbWeightOf::::get().reads_writes(1, 1).ref_time() { stopped_at = Some(bn); log::debug!("Stopping processing tasks at (read next): {stopped_at:?}"); @@ -999,9 +999,17 @@ pub mod pallet { } } - for (bn, task) in tasks_to_delete { - log::debug!("Remove task: {:?}", task); - let _ = TaskPoolOf::::delete(bn, task); + if let Some(task) = last_task { + stopped_at = Some(bn); + + // since there is the overlay mechanism we don't need to subtract write cost + // from gas allowance on task insertion. + TaskPoolOf::::add(bn, task) + .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e)); + GasAllowanceOf::::put( + GasAllowanceOf::::get() + .saturating_add(DbWeightOf::::get().writes(1).ref_time()), + ); } // Stopping iteration over blocks if no resources left. From ba40b8b4391e9f7b577f03e8ae8e59c870583fdd Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 8 Sep 2023 15:35:52 +0400 Subject: [PATCH 24/32] fix review remarks --- common/src/lib.rs | 15 +++--------- common/src/scheduler/task.rs | 6 ++--- pallets/gear/src/manager/task.rs | 40 +++++++++++++++++++------------- 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/common/src/lib.rs b/common/src/lib.rs index 2ca0c1104d8..a232e5dc88f 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -73,6 +73,9 @@ pub use gas_provider::{ LockId, LockableTree, Provider as GasProvider, ReservableTree, Tree as GasTree, }; +/// Type alias for gas entity. +pub type Gas = u64; + pub trait Origin: Sized { fn into_origin(self) -> H256; fn from_origin(val: H256) -> Self; @@ -206,18 +209,6 @@ impl Program { }) ) } - - pub fn is_uninitialized(&self) -> Option { - if let Program::Active(ActiveProgram { - state: ProgramState::Uninitialized { message_id }, - .. - }) = self - { - Some(*message_id) - } else { - None - } - } } #[derive(Clone, Debug, derive_more::Display)] diff --git a/common/src/scheduler/task.rs b/common/src/scheduler/task.rs index 4070c89c573..cbfd0575943 100644 --- a/common/src/scheduler/task.rs +++ b/common/src/scheduler/task.rs @@ -16,15 +16,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::paused_program_storage::SessionId; +use crate::{paused_program_storage::SessionId, Gas}; use frame_support::{ codec::{self, Decode, Encode, MaxEncodedLen}, scale_info::{self, TypeInfo}, }; use gear_core::ids::{CodeId, MessageId, ProgramId, ReservationId}; -pub type Gas = u64; - /// Scheduled task sense and required data for processing action. /// /// CAUTION: NEVER ALLOW `ScheduledTask` BE A BIG DATA. @@ -120,7 +118,7 @@ pub trait TaskHandler { // ----- /// Pause program action. fn pause_program(&mut self, program_id: ProgramId) -> Gas; - /// Remove code action + /// Remove code action. fn remove_code(&mut self, code_id: CodeId) -> Gas; /// Remove from mailbox action. fn remove_from_mailbox(&mut self, user_id: AccountId, message_id: MessageId) -> Gas; diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 3eacf285a53..b5e1445088f 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -17,7 +17,7 @@ // along with this program. If not, see . use crate::{ - manager::ExtManager, weights::WeightInfo, Config, DispatchStashOf, Event, Pallet, + manager::ExtManager, weights::WeightInfo, Config, DbWeightOf, DispatchStashOf, Event, Pallet, ProgramStorageOf, QueueOf, TaskPoolOf, WaitlistOf, }; use alloc::string::ToString; @@ -29,7 +29,7 @@ use common::{ paused_program_storage::SessionId, scheduler::*, storage::*, - Origin, PausedProgramStorage, Program, ProgramStorage, + ActiveProgram, Gas, Origin, PausedProgramStorage, Program, ProgramState, ProgramStorage, }; use core::cmp; use gear_core::{ @@ -47,12 +47,17 @@ pub fn get_maximum_task_gas(task: &ScheduledTask) -> Ga match task { PauseProgram(_) => { - let count = - u32::from(MAX_WASM_PAGE_COUNT * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 / 2); - cmp::max( - ::WeightInfo::tasks_pause_program(count).ref_time(), - ::WeightInfo::tasks_pause_program_uninited(count).ref_time(), - ) + // TODO: #3079 + if ::ProgramRentEnabled::get() { + let count = + u32::from(MAX_WASM_PAGE_COUNT * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u16 / 2); + cmp::max( + ::WeightInfo::tasks_pause_program(count).ref_time(), + ::WeightInfo::tasks_pause_program_uninited(count).ref_time(), + ) + } else { + DbWeightOf::::get().writes(2).ref_time() + } } RemoveCode(_) => todo!("#646"), RemoveFromMailbox(_, _) => { @@ -85,6 +90,10 @@ where T::AccountId: Origin, { fn pause_program(&mut self, program_id: ProgramId) -> Gas { + // + // TODO: #3079 + // + if !::ProgramRentEnabled::get() { log::debug!("Program rent logic is disabled."); @@ -104,18 +113,17 @@ where TaskPoolOf::::add(expiration_block, task) .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e)); - return 0; + return DbWeightOf::::get().writes(1).ref_time(); } - let program = ProgramStorageOf::::get_program(program_id) - .unwrap_or_else(|| unreachable!("Program to pause not found.")); + let program: ActiveProgram<_> = ProgramStorageOf::::get_program(program_id) + .unwrap_or_else(|| unreachable!("Program to pause not found.")) + .try_into() + .unwrap_or_else(|e| unreachable!("Pause program task logic corrupted: {e:?}")); - let pages_with_data = match program { - Program::Active(ref p) => p.pages_with_data.len() as u32, - _ => unreachable!("Pause program task logic corrupted!"), - }; + let pages_with_data = program.pages_with_data.len() as u32; - let Some(init_message_id) = program.is_uninitialized() else { + let ProgramState::Uninitialized{ message_id: init_message_id } = program.state else { // pause initialized program let gas_reservation_map = ProgramStorageOf::::pause_program(program_id, Pallet::::block_number()) From 45336f7fdbe41bebe8c1feb3c23b29ed612680ee Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Fri, 8 Sep 2023 15:43:09 +0400 Subject: [PATCH 25/32] rename funcs --- pallets/gear/src/benchmarking/mod.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 76ff7b4304c..a8d7569deed 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -242,14 +242,14 @@ where .unwrap_or_else(|e| unreachable!("core-processor logic invalidated: {}", e)) } -fn get_last_session_id_if_any() -> Option { - filter_event_reverse::(|event| match event { +fn get_last_session_id() -> Option { + find_latest_event::(|event| match event { Event::ProgramResumeSessionStarted { session_id, .. } => Some(session_id), _ => None, }) } -pub fn filter_event_reverse(mapping_filter: F) -> Option +pub fn find_latest_event(mapping_filter: F) -> Option where T: Config, F: Fn(Event) -> Option, @@ -296,7 +296,7 @@ where pages }; - (get_last_session_id_if_any::().unwrap(), memory_pages) + (get_last_session_id::().unwrap(), memory_pages) } #[track_caller] @@ -2843,7 +2843,7 @@ benchmarks! { ) .expect("failed to start resume session"); - let session_id = get_last_session_id_if_any::().unwrap(); + let session_id = get_last_session_id::().unwrap(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.remove_resume_session(session_id); @@ -3049,7 +3049,7 @@ benchmarks! { Gear::::process_queue(Default::default()); - let expiration = filter_event_reverse::(|event| match event { + let expiration = find_latest_event::(|event| match event { Event::MessageWaited { expiration, .. } => Some(expiration), _ => None, }).expect("message should be waited"); @@ -3073,7 +3073,7 @@ benchmarks! { tasks_remove_from_mailbox { tasks_send_user_message_prepare::(0u32); - let (user, message_id) = filter_event_reverse::(|event| match event { + let (user, message_id) = find_latest_event::(|event| match event { Event::UserMessageSent { message, .. From a8f4abd0410f2d451de379a1ad053a7795fa5dea Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 11 Sep 2023 11:25:46 +0400 Subject: [PATCH 26/32] fix build --- Cargo.lock | 1 + Cargo.toml | 2 +- examples/waiter/Cargo.toml | 1 + examples/waiter/src/lib.rs | 13 +++++++------ 4 files changed, 10 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c724a14a978..c9a52cbdc89 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2315,6 +2315,7 @@ version = "0.1.0" dependencies = [ "demo-waiter", "futures", + "gcore", "gear-core", "gear-wasm-builder", "gstd", diff --git a/Cargo.toml b/Cargo.toml index c7ab545ea31..2775993fe1a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -372,7 +372,7 @@ demo-calc-hash-in-one-block = { path = "examples/calc-hash/in-one-block" } demo-calc-hash-over-blocks = { path = "examples/calc-hash/over-blocks" } demo-custom = { path = "examples/custom" } demo-compose = { path = "examples/compose" } -demo-constructor = { path = "examples/constructor" } +demo-constructor = { path = "examples/constructor", default-features = false } demo-delayed-sender = { path = "examples/delayed-sender" } demo-distributor = { path = "examples/distributor" } demo-futures-unordered = { path = "examples/futures-unordered", features = ["debug"] } diff --git a/examples/waiter/Cargo.toml b/examples/waiter/Cargo.toml index c6cea1d7564..3090fc3f7a0 100644 --- a/examples/waiter/Cargo.toml +++ b/examples/waiter/Cargo.toml @@ -10,6 +10,7 @@ workspace = "../../" parity-scale-codec = { workspace = true, features = ["derive"] } futures.workspace = true gstd.workspace = true +gcore.workspace = true [build-dependencies] gear-wasm-builder.workspace = true diff --git a/examples/waiter/src/lib.rs b/examples/waiter/src/lib.rs index 167524743a6..1ee1121fb77 100644 --- a/examples/waiter/src/lib.rs +++ b/examples/waiter/src/lib.rs @@ -22,6 +22,7 @@ extern crate alloc; use alloc::vec::Vec; use parity_scale_codec::{Decode, Encode}; +use gcore::BlockCount; type ActorId = [u8; 32]; @@ -101,14 +102,14 @@ pub enum RwLockContinuation { #[derive(Debug, Encode, Decode)] pub enum Command { Wait(WaitSubcommand), - SendFor(ActorId, gstd::BlockCount), - SendUpTo(ActorId, gstd::BlockCount), - SendUpToWait(ActorId, gstd::BlockCount), - SendAndWaitFor(gstd::BlockCount, ActorId), + SendFor(ActorId, BlockCount), + SendUpTo(ActorId, BlockCount), + SendUpToWait(ActorId, BlockCount), + SendAndWaitFor(BlockCount, ActorId), ReplyAndWait(WaitSubcommand), - SleepFor(Vec, SleepForWaitType), + SleepFor(Vec, SleepForWaitType), WakeUp([u8; 32]), - MxLock(gstd::BlockCount, MxLockContinuation), + MxLock(BlockCount, MxLockContinuation), MxLockStaticAccess(LockStaticAccessSubcommand), RwLock(RwLockType, RwLockContinuation), RwLockStaticAccess(RwLockType, LockStaticAccessSubcommand), From 397c6e8ee18639fef0709ce76ea39344dd3ca66b Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 11 Sep 2023 12:01:22 +0400 Subject: [PATCH 27/32] remove copy-paste --- pallets/gear/src/benchmarking/mod.rs | 59 +++++++++++++--------------- 1 file changed, 28 insertions(+), 31 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 9aab72c17c9..57f3df7ab22 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -300,8 +300,9 @@ where } #[track_caller] -fn tasks_send_user_message_prepare(delay: u32) +fn tasks_send_user_message_prepare(delay: u32) where + T: Config, T::AccountId: Origin, { use demo_delayed_sender::WASM_BINARY; @@ -325,6 +326,30 @@ where Gear::::process_queue(Default::default()); } +#[track_caller] +fn tasks_send_user_message_common() -> MessageId +where + T: Config, + T::AccountId: Origin, +{ + let delay = 1u32; + tasks_send_user_message_prepare::(delay); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .expect("task should be scheduled"); + let (message_id, to_mailbox) = match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!("task should be SendUserMessage"), + }; + assert!(to_mailbox); + + message_id +} + #[track_caller] fn tasks_pause_program_prepare(c: u32, code: Vec) -> ProgramId where @@ -2892,42 +2917,14 @@ benchmarks! { } tasks_send_user_message_to_mailbox { - let delay = 1u32; - tasks_send_user_message_prepare::(delay); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .expect("task should be scheduled"); - let (message_id, to_mailbox) = match task { - ScheduledTask::SendUserMessage { - message_id, - to_mailbox, - } => (message_id, to_mailbox), - _ => unreachable!("task should be SendUserMessage"), - }; - assert!(to_mailbox); - + let message_id = tasks_send_user_message_common::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, true); } tasks_send_user_message { - let delay = 1u32; - tasks_send_user_message_prepare::(delay); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .expect("task should be scheduled"); - let (message_id, to_mailbox) = match task { - ScheduledTask::SendUserMessage { - message_id, - to_mailbox, - } => (message_id, to_mailbox), - _ => unreachable!("task should be SendUserMessage"), - }; - assert!(to_mailbox); - + let message_id = tasks_send_user_message_common::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, false); From 2d8f0d99324453354801b59fb7a8564565021b57 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 11 Sep 2023 17:42:34 +0400 Subject: [PATCH 28/32] fix review remarks --- examples/waiter/src/lib.rs | 2 +- pallets/gear/src/lib.rs | 31 ++++++++++++++++--------------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/waiter/src/lib.rs b/examples/waiter/src/lib.rs index 1ee1121fb77..b7d4619f789 100644 --- a/examples/waiter/src/lib.rs +++ b/examples/waiter/src/lib.rs @@ -21,8 +21,8 @@ extern crate alloc; use alloc::vec::Vec; -use parity_scale_codec::{Decode, Encode}; use gcore::BlockCount; +use parity_scale_codec::{Decode, Encode}; type ActorId = [u8; 32]; diff --git a/pallets/gear/src/lib.rs b/pallets/gear/src/lib.rs index a0bf4321406..e6f3b9c910e 100644 --- a/pallets/gear/src/lib.rs +++ b/pallets/gear/src/lib.rs @@ -948,16 +948,11 @@ pub mod pallet { break; } - let gas_delta = DbWeightOf::::get() - .writes(1) - .ref_time() - .saturating_sub(DbWeightOf::::get().reads(1).ref_time()); - // Iterating over tasks, scheduled on `bn`. let mut last_task = None; for task in tasks { - // Decreasing gas allowance due to DB read. - GasAllowanceOf::::decrease(DbWeightOf::::get().reads(1).ref_time()); + // Decreasing gas allowance due to DB deletion. + GasAllowanceOf::::decrease(DbWeightOf::::get().writes(1).ref_time()); // gas required to process task. let max_task_gas = manager::get_maximum_task_gas::(&task); @@ -968,21 +963,27 @@ pub mod pallet { // Making sure we have gas to process the current task // and update the first block of incomplete tasks. if GasAllowanceOf::::get().saturating_sub(max_task_gas) - <= DbWeightOf::::get() - .writes(1) - .ref_time() - .saturating_add(gas_delta) + <= DbWeightOf::::get().writes(1).ref_time() { + // Since the task is not processed write DB cost should be refunded. + // In the same time gas allowance should be charged for read DB cost. + GasAllowanceOf::::put( + GasAllowanceOf::::get() + .saturating_add(DbWeightOf::::get().writes(1).ref_time()) + .saturating_sub(DbWeightOf::::get().reads(1).ref_time()), + ); + last_task = Some(task); log::debug!("Not enough gas to process task at: {bn:?}"); + break; } // Processing task and update allowance of gas. let task_gas = task.process_with(ext_manager); - GasAllowanceOf::::decrease(task_gas + gas_delta); + GasAllowanceOf::::decrease(task_gas); - // Check that there is enough gas allowance to read next task, update the first block of incomplete tasks and remove already processed tasks. + // Check that there is enough gas allowance to query next task and update the first block of incomplete tasks. if GasAllowanceOf::::get() <= DbWeightOf::::get().reads_writes(1, 1).ref_time() { @@ -997,12 +998,12 @@ pub mod pallet { // since there is the overlay mechanism we don't need to subtract write cost // from gas allowance on task insertion. - TaskPoolOf::::add(bn, task) - .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e)); GasAllowanceOf::::put( GasAllowanceOf::::get() .saturating_add(DbWeightOf::::get().writes(1).ref_time()), ); + TaskPoolOf::::add(bn, task) + .unwrap_or_else(|e| unreachable!("Scheduling logic invalidated! {:?}", e)); } // Stopping iteration over blocks if no resources left. From 6ce3d101dbc5051d737ab559574939338ca56daf Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 11 Sep 2023 17:54:54 +0400 Subject: [PATCH 29/32] fix review remarks --- pallets/gear/src/benchmarking/mod.rs | 113 ++--------------------- pallets/gear/src/benchmarking/tasks.rs | 118 +++++++++++++++++++++++++ pallets/gear/src/manager/task.rs | 8 +- 3 files changed, 128 insertions(+), 111 deletions(-) create mode 100644 pallets/gear/src/benchmarking/tasks.rs diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index 57f3df7ab22..b0e6fe7863b 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -40,6 +40,7 @@ mod sandbox; mod syscalls; mod utils; +mod tasks; use syscalls::Benches; mod tests; @@ -299,108 +300,6 @@ where (get_last_session_id::().unwrap(), memory_pages) } -#[track_caller] -fn tasks_send_user_message_prepare(delay: u32) -where - T: Config, - T::AccountId: Origin, -{ - use demo_delayed_sender::WASM_BINARY; - - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - Gear::::upload_program( - RawOrigin::Signed(caller).into(), - WASM_BINARY.to_vec(), - salt, - delay.encode(), - 100_000_000_000, - 0u32.into(), - ) - .expect("submit program failed"); - - Gear::::process_queue(Default::default()); -} - -#[track_caller] -fn tasks_send_user_message_common() -> MessageId -where - T: Config, - T::AccountId: Origin, -{ - let delay = 1u32; - tasks_send_user_message_prepare::(delay); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .expect("task should be scheduled"); - let (message_id, to_mailbox) = match task { - ScheduledTask::SendUserMessage { - message_id, - to_mailbox, - } => (message_id, to_mailbox), - _ => unreachable!("task should be SendUserMessage"), - }; - assert!(to_mailbox); - - message_id -} - -#[track_caller] -fn tasks_pause_program_prepare(c: u32, code: Vec) -> ProgramId -where - T::AccountId: Origin, -{ - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 400_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(&code), &salt); - Gear::::upload_program( - RawOrigin::Signed(caller).into(), - code, - salt, - b"init_payload".to_vec(), - 10_000_000_000, - 0u32.into(), - ) - .expect("submit program failed"); - - Gear::::process_queue(Default::default()); - - let memory_page = { - let mut page = PageBuf::new_zeroed(); - page[0] = 1; - - page - }; - - for i in 0..c { - ProgramStorageOf::::set_program_page_data( - program_id, - GearPage::from(i as u16), - memory_page.clone(), - ); - } - - ProgramStorageOf::::update_active_program(program_id, |program| { - program.pages_with_data = BTreeSet::from_iter((0..c).map(|i| GearPage::from(i as u16))); - - let wasm_pages = (c as usize * GEAR_PAGE_SIZE) / WASM_PAGE_SIZE; - program.allocations = - BTreeSet::from_iter((0..wasm_pages).map(|i| WasmPage::from(i as u16))); - }) - .expect("program should exist"); - - program_id -} - /// An instantiated and deployed program. #[derive(Clone)] struct Program { @@ -2917,14 +2816,14 @@ benchmarks! { } tasks_send_user_message_to_mailbox { - let message_id = tasks_send_user_message_common::(); + let message_id = tasks::send_user_message_common::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, true); } tasks_send_user_message { - let message_id = tasks_send_user_message_common::(); + let message_id = tasks::send_user_message_common::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, false); @@ -3079,7 +2978,7 @@ benchmarks! { } tasks_remove_from_mailbox { - tasks_send_user_message_prepare::(0u32); + tasks::send_user_message_prepare::(0u32); let (user, message_id) = find_latest_event::(|event| match event { Event::UserMessageSent { @@ -3098,7 +2997,7 @@ benchmarks! { let c in 0 .. (MAX_PAGES - 1) * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u32; let code = benchmarking::generate_wasm2(0.into()).unwrap(); - let program_id = tasks_pause_program_prepare::(c, code); + let program_id = tasks::pause_program_prepare::(c, code); let mut ext_manager = ExtManager::::default(); }: { @@ -3108,7 +3007,7 @@ benchmarks! { tasks_pause_program_uninited { let c in 0 .. (MAX_PAGES - 1) * (WASM_PAGE_SIZE / GEAR_PAGE_SIZE) as u32; - let program_id = tasks_pause_program_prepare::(c, demo_init_wait::WASM_BINARY.to_vec()); + let program_id = tasks::pause_program_prepare::(c, demo_init_wait::WASM_BINARY.to_vec()); let mut ext_manager = ExtManager::::default(); }: { diff --git a/pallets/gear/src/benchmarking/tasks.rs b/pallets/gear/src/benchmarking/tasks.rs new file mode 100644 index 00000000000..68c4f4f3c64 --- /dev/null +++ b/pallets/gear/src/benchmarking/tasks.rs @@ -0,0 +1,118 @@ +// This file is part of Gear. + +// Copyright (C) Gear Technologies Inc. +// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +use super::*; + +#[track_caller] +pub(super) fn send_user_message_prepare(delay: u32) +where + T: Config, + T::AccountId: Origin, +{ + use demo_delayed_sender::WASM_BINARY; + + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + Gear::::upload_program( + RawOrigin::Signed(caller).into(), + WASM_BINARY.to_vec(), + salt, + delay.encode(), + 100_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + Gear::::process_queue(Default::default()); +} + +#[track_caller] +pub(super) fn send_user_message_common() -> MessageId +where + T: Config, + T::AccountId: Origin, +{ + let delay = 1u32; + send_user_message_prepare::(delay); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .expect("task should be scheduled"); + let (message_id, to_mailbox) = match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!("task should be SendUserMessage"), + }; + assert!(to_mailbox); + + message_id +} + +#[track_caller] +pub(super) fn pause_program_prepare(c: u32, code: Vec) -> ProgramId +where + T::AccountId: Origin, +{ + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 400_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(&code), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller).into(), + code, + salt, + b"init_payload".to_vec(), + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + Gear::::process_queue(Default::default()); + + let memory_page = { + let mut page = PageBuf::new_zeroed(); + page[0] = 1; + + page + }; + + for i in 0..c { + ProgramStorageOf::::set_program_page_data( + program_id, + GearPage::from(i as u16), + memory_page.clone(), + ); + } + + ProgramStorageOf::::update_active_program(program_id, |program| { + program.pages_with_data = BTreeSet::from_iter((0..c).map(|i| GearPage::from(i as u16))); + + let wasm_pages = (c as usize * GEAR_PAGE_SIZE) / WASM_PAGE_SIZE; + program.allocations = + BTreeSet::from_iter((0..wasm_pages).map(|i| WasmPage::from(i as u16))); + }) + .expect("program should exist"); + + program_id +} diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index b5e1445088f..5e2c8b9e1f8 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -353,11 +353,11 @@ where .unwrap_or_else(|_| unreachable!("Signal message sent to user")); Pallet::::send_user_message_after_delay(message, to_mailbox); - match to_mailbox { - true => ::WeightInfo::tasks_send_user_message_to_mailbox(), - false => ::WeightInfo::tasks_send_user_message(), + if to_mailbox { + ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time() + } else { + ::WeightInfo::tasks_send_user_message().ref_time() } - .ref_time() } fn remove_gas_reservation( From 2ec54382ce5763dff17b9b8383bc90424b4f4a1f Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Mon, 11 Sep 2023 18:25:51 +0400 Subject: [PATCH 30/32] move all tasks benchmarks in the module --- pallets/gear/src/benchmarking/mod.rs | 191 +--------------- pallets/gear/src/benchmarking/tasks.rs | 303 +++++++++++++++++++++++-- 2 files changed, 287 insertions(+), 207 deletions(-) diff --git a/pallets/gear/src/benchmarking/mod.rs b/pallets/gear/src/benchmarking/mod.rs index b0e6fe7863b..dd72110fe3f 100644 --- a/pallets/gear/src/benchmarking/mod.rs +++ b/pallets/gear/src/benchmarking/mod.rs @@ -39,8 +39,8 @@ mod code; mod sandbox; mod syscalls; -mod utils; mod tasks; +mod utils; use syscalls::Benches; mod tests; @@ -2759,165 +2759,42 @@ benchmarks! { } tasks_remove_resume_session { - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - let code = benchmarking::generate_wasm2(16.into()).unwrap(); - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(&code), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), code, salt, b"init_payload".to_vec(), 10_000_000_000, 0u32.into()).expect("submit program failed"); - - init_block::(None); - - ProgramStorageOf::::pause_program(program_id, 100u32.into()).unwrap(); - - Gear::::resume_session_init( - RawOrigin::Signed(caller).into(), - program_id, - Default::default(), - CodeId::default(), - ) - .expect("failed to start resume session"); - - let session_id = get_last_session_id::().unwrap(); + let session_id = tasks::remove_resume_session::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.remove_resume_session(session_id); } tasks_remove_gas_reservation { - use demo_reserve_gas::{InitAction, WASM_BINARY}; - - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); - Gear::::upload_program(RawOrigin::Signed(caller).into(), - WASM_BINARY.to_vec(), - salt, - InitAction::Normal(vec![(50_000, 100),]) - .encode(), - 10_000_000_000, 0u32.into()).expect("submit program failed"); - - Gear::::process_queue(Default::default()); - - let program: ActiveProgram<_> = ProgramStorageOf::::get_program(program_id) - .expect("program should exist") - .try_into() - .expect("program should be active"); - - let reservation_id = program.gas_reservation_map.first_key_value().map(|(k, _v)| *k).unwrap(); - + let (program_id, reservation_id) = tasks::remove_gas_reservation::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.remove_gas_reservation(program_id, reservation_id); } tasks_send_user_message_to_mailbox { - let message_id = tasks::send_user_message_common::(); + let message_id = tasks::send_user_message::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, true); } tasks_send_user_message { - let message_id = tasks::send_user_message_common::(); + let message_id = tasks::send_user_message::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_user_message(message_id, false); } tasks_send_dispatch { - use demo_constructor::{Call, Calls, Scheme, WASM_BINARY}; - - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), - WASM_BINARY.to_vec(), - salt, - Scheme::empty().encode(), - 10_000_000_000, 0u32.into()).expect("submit program failed"); - - let delay = 1u32; - let calls = Calls::builder().add_call(Call::Send( - <[u8; 32]>::from(program_id.into_origin()).into(), - [].into(), - Some(0u64.into()), - 0u128.into(), - delay.into(), - )); - Gear::::send_message( - RawOrigin::Signed(caller).into(), - program_id, - calls.encode(), - 10_000_000_000, - 0u32.into(), - false, - ).expect("failed to send message"); - - Gear::::process_queue(Default::default()); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .unwrap(); - let message_id = match task { - ScheduledTask::SendDispatch ( - message_id, - ) => message_id, - _ => unreachable!(), - }; - + let message_id = tasks::send_dispatch::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.send_dispatch(message_id); } tasks_wake_message { - use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; - - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), - WASM_BINARY.to_vec(), - salt, - vec![], - 10_000_000_000, 0u32.into()).expect("submit program failed"); - - let delay = 10u32; - Gear::::send_message( - RawOrigin::Signed(caller).into(), - program_id, - Command::Wait(WaitSubcommand::WaitFor(delay)).encode(), - 10_000_000_000, - 0u32.into(), - false, - ).expect("failed to send message"); - - Gear::::process_queue(Default::default()); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .unwrap(); - let (_program_id, message_id) = match task { - ScheduledTask::WakeMessage ( - program_id, - message_id, - ) => (program_id, message_id), - _ => unreachable!(), - }; - + let (program_id, message_id) = tasks::wake_message::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.wake_message(program_id, message_id); @@ -2930,64 +2807,14 @@ benchmarks! { } tasks_remove_from_waitlist { - use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; - - let caller = benchmarking::account("caller", 0, 0); - CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); - - init_block::(None); - - let salt = vec![]; - let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); - Gear::::upload_program(RawOrigin::Signed(caller.clone()).into(), - WASM_BINARY.to_vec(), - salt, - vec![], - 10_000_000_000, 0u32.into()).expect("submit program failed"); - - Gear::::send_message( - RawOrigin::Signed(caller).into(), - program_id, - Command::Wait(WaitSubcommand::Wait).encode(), - 10_000_000_000, - 0u32.into(), - false, - ).expect("failed to send message"); - - Gear::::process_queue(Default::default()); - - let expiration = find_latest_event::(|event| match event { - Event::MessageWaited { expiration, .. } => Some(expiration), - _ => None, - }).expect("message should be waited"); - - let task = TaskPoolOf::::iter_prefix_keys(expiration) - .next() - .unwrap(); - let (_program_id, message_id) = match task { - ScheduledTask::RemoveFromWaitlist ( - program_id, - message_id, - ) => (program_id, message_id), - _ => unreachable!(), - }; - + let (program_id, message_id) = tasks::remove_from_waitlist::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.remove_from_waitlist(program_id, message_id); } tasks_remove_from_mailbox { - tasks::send_user_message_prepare::(0u32); - - let (user, message_id) = find_latest_event::(|event| match event { - Event::UserMessageSent { - message, - .. - } => Some((message.destination(), message.id())), - _ => None, - }).expect("message should be sent"); - + let (user, message_id) = tasks::remove_from_mailbox::(); let mut ext_manager = ExtManager::::default(); }: { ext_manager.remove_from_mailbox(T::AccountId::from_origin(user.into_origin()), message_id); diff --git a/pallets/gear/src/benchmarking/tasks.rs b/pallets/gear/src/benchmarking/tasks.rs index 68c4f4f3c64..6447b509f01 100644 --- a/pallets/gear/src/benchmarking/tasks.rs +++ b/pallets/gear/src/benchmarking/tasks.rs @@ -14,9 +14,10 @@ // GNU General Public License for more details. use super::*; +use gear_core::ids::ReservationId; #[track_caller] -pub(super) fn send_user_message_prepare(delay: u32) +fn send_user_message_prepare(delay: u32) where T: Config, T::AccountId: Origin, @@ -43,33 +44,10 @@ where } #[track_caller] -pub(super) fn send_user_message_common() -> MessageId +pub(super) fn pause_program_prepare(c: u32, code: Vec) -> ProgramId where T: Config, T::AccountId: Origin, -{ - let delay = 1u32; - send_user_message_prepare::(delay); - - let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) - .next() - .expect("task should be scheduled"); - let (message_id, to_mailbox) = match task { - ScheduledTask::SendUserMessage { - message_id, - to_mailbox, - } => (message_id, to_mailbox), - _ => unreachable!("task should be SendUserMessage"), - }; - assert!(to_mailbox); - - message_id -} - -#[track_caller] -pub(super) fn pause_program_prepare(c: u32, code: Vec) -> ProgramId -where - T::AccountId: Origin, { let caller = benchmarking::account("caller", 0, 0); CurrencyOf::::deposit_creating(&caller, 400_000_000_000_000u128.unique_saturated_into()); @@ -116,3 +94,278 @@ where program_id } + +#[track_caller] +pub(super) fn remove_resume_session() -> SessionId +where + T: Config, + T::AccountId: Origin, +{ + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + let code = benchmarking::generate_wasm2(16.into()).unwrap(); + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(&code), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller.clone()).into(), + code, + salt, + b"init_payload".to_vec(), + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + init_block::(None); + + ProgramStorageOf::::pause_program(program_id, 100u32.into()).unwrap(); + + Gear::::resume_session_init( + RawOrigin::Signed(caller).into(), + program_id, + Default::default(), + CodeId::default(), + ) + .expect("failed to start resume session"); + + get_last_session_id::().unwrap() +} + +#[track_caller] +pub(super) fn remove_gas_reservation() -> (ProgramId, ReservationId) +where + T: Config, + T::AccountId: Origin, +{ + use demo_reserve_gas::{InitAction, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller).into(), + WASM_BINARY.to_vec(), + salt, + InitAction::Normal(vec![(50_000, 100)]).encode(), + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + Gear::::process_queue(Default::default()); + + let program: ActiveProgram<_> = ProgramStorageOf::::get_program(program_id) + .expect("program should exist") + .try_into() + .expect("program should be active"); + + ( + program_id, + program + .gas_reservation_map + .first_key_value() + .map(|(k, _v)| *k) + .unwrap(), + ) +} + +#[track_caller] +pub(super) fn send_user_message() -> MessageId +where + T: Config, + T::AccountId: Origin, +{ + let delay = 1u32; + send_user_message_prepare::(delay); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .expect("task should be scheduled"); + let (message_id, to_mailbox) = match task { + ScheduledTask::SendUserMessage { + message_id, + to_mailbox, + } => (message_id, to_mailbox), + _ => unreachable!("task should be SendUserMessage"), + }; + assert!(to_mailbox); + + message_id +} + +#[track_caller] +pub(super) fn send_dispatch() -> MessageId +where + T: Config, + T::AccountId: Origin, +{ + use demo_constructor::{Call, Calls, Scheme, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + Scheme::empty().encode(), + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + let delay = 1u32; + let calls = Calls::builder().add_call(Call::Send( + <[u8; 32]>::from(program_id.into_origin()).into(), + [].into(), + Some(0u64.into()), + 0u128.into(), + delay.into(), + )); + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + calls.encode(), + 10_000_000_000, + 0u32.into(), + false, + ) + .expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .unwrap(); + + match task { + ScheduledTask::SendDispatch(message_id) => message_id, + _ => unreachable!(), + } +} + +#[track_caller] +pub(super) fn wake_message() -> (ProgramId, MessageId) +where + T: Config, + T::AccountId: Origin, +{ + use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + vec![], + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + let delay = 10u32; + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + Command::Wait(WaitSubcommand::WaitFor(delay)).encode(), + 10_000_000_000, + 0u32.into(), + false, + ) + .expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let task = TaskPoolOf::::iter_prefix_keys(Gear::::block_number() + delay.into()) + .next() + .unwrap(); + let (_program_id, message_id) = match task { + ScheduledTask::WakeMessage(program_id, message_id) => (program_id, message_id), + _ => unreachable!(), + }; + + (program_id, message_id) +} + +#[track_caller] +pub(super) fn remove_from_waitlist() -> (ProgramId, MessageId) +where + T: Config, + T::AccountId: Origin, +{ + use demo_waiter::{Command, WaitSubcommand, WASM_BINARY}; + + let caller = benchmarking::account("caller", 0, 0); + CurrencyOf::::deposit_creating(&caller, 200_000_000_000_000u128.unique_saturated_into()); + + init_block::(None); + + let salt = vec![]; + let program_id = ProgramId::generate(CodeId::generate(WASM_BINARY), &salt); + Gear::::upload_program( + RawOrigin::Signed(caller.clone()).into(), + WASM_BINARY.to_vec(), + salt, + vec![], + 10_000_000_000, + 0u32.into(), + ) + .expect("submit program failed"); + + Gear::::send_message( + RawOrigin::Signed(caller).into(), + program_id, + Command::Wait(WaitSubcommand::Wait).encode(), + 10_000_000_000, + 0u32.into(), + false, + ) + .expect("failed to send message"); + + Gear::::process_queue(Default::default()); + + let expiration = find_latest_event::(|event| match event { + Event::MessageWaited { expiration, .. } => Some(expiration), + _ => None, + }) + .expect("message should be waited"); + + let task = TaskPoolOf::::iter_prefix_keys(expiration) + .next() + .unwrap(); + let (_program_id, message_id) = match task { + ScheduledTask::RemoveFromWaitlist(program_id, message_id) => (program_id, message_id), + _ => unreachable!(), + }; + + (program_id, message_id) +} + +#[track_caller] +pub(super) fn remove_from_mailbox() -> (ProgramId, MessageId) +where + T: Config, + T::AccountId: Origin, +{ + send_user_message_prepare::(0u32); + + find_latest_event::(|event| match event { + Event::UserMessageSent { message, .. } => Some((message.destination(), message.id())), + _ => None, + }) + .expect("message should be sent") +} From e8da3a73b6cb1f94280fafb7a1b80fed1c6d49d6 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Wed, 13 Sep 2023 15:45:40 +0400 Subject: [PATCH 31/32] add logging for returned gas value --- pallets/gear/src/manager/task.rs | 59 +++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 12 deletions(-) diff --git a/pallets/gear/src/manager/task.rs b/pallets/gear/src/manager/task.rs index 5e2c8b9e1f8..54f6eb85f2b 100644 --- a/pallets/gear/src/manager/task.rs +++ b/pallets/gear/src/manager/task.rs @@ -144,7 +144,10 @@ where change: ProgramChangeKind::Paused, }); - return ::WeightInfo::tasks_pause_program(pages_with_data).ref_time(); + let gas = ::WeightInfo::tasks_pause_program(pages_with_data).ref_time(); + log::trace!("Task gas: tasks_pause_program = {gas}"); + + return gas; }; // terminate uninitialized program @@ -196,7 +199,11 @@ where change: ProgramChangeKind::Terminated, }); - ::WeightInfo::tasks_pause_program_uninited(pages_with_data).ref_time() + let gas = + ::WeightInfo::tasks_pause_program_uninited(pages_with_data).ref_time(); + log::trace!("Task gas: tasks_pause_program_uninited = {gas}"); + + gas } fn remove_code(&mut self, _code_id: CodeId) -> Gas { @@ -226,7 +233,10 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - ::WeightInfo::tasks_remove_from_mailbox().ref_time() + let gas = ::WeightInfo::tasks_remove_from_mailbox().ref_time(); + log::trace!("Task gas: tasks_remove_from_mailbox = {gas}"); + + gas } fn remove_from_waitlist(&mut self, program_id: ProgramId, message_id: MessageId) -> Gas { @@ -299,7 +309,10 @@ where Self::process_failed_init(program_id, origin, true); } - ::WeightInfo::tasks_remove_from_waitlist().ref_time() + let gas = ::WeightInfo::tasks_remove_from_waitlist().ref_time(); + log::trace!("Task gas: tasks_remove_from_waitlist = {gas}"); + + gas } fn remove_paused_program(&mut self, _program_id: ProgramId) -> Gas { @@ -316,9 +329,17 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - ::WeightInfo::tasks_wake_message().ref_time() + let gas = ::WeightInfo::tasks_wake_message().ref_time(); + log::trace!("Task gas: tasks_wake_message = {gas}"); + + gas + } + None => { + let gas = ::WeightInfo::tasks_wake_message_no_wake().ref_time(); + log::trace!("Task gas: tasks_wake_message_no_wake = {gas}"); + + gas } - None => ::WeightInfo::tasks_wake_message_no_wake().ref_time(), } } @@ -334,7 +355,10 @@ where QueueOf::::queue(dispatch) .unwrap_or_else(|e| unreachable!("Message queue corrupted! {:?}", e)); - ::WeightInfo::tasks_send_dispatch().ref_time() + let gas = ::WeightInfo::tasks_send_dispatch().ref_time(); + log::trace!("Task gas: tasks_send_dispatch = {gas}"); + + gas } fn send_user_message(&mut self, stashed_message_id: MessageId, to_mailbox: bool) -> Gas { @@ -354,9 +378,15 @@ where Pallet::::send_user_message_after_delay(message, to_mailbox); if to_mailbox { - ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time() + let gas = ::WeightInfo::tasks_send_user_message_to_mailbox().ref_time(); + log::trace!("Task gas: tasks_send_user_message_to_mailbox = {gas}"); + + gas } else { - ::WeightInfo::tasks_send_user_message().ref_time() + let gas = ::WeightInfo::tasks_send_user_message().ref_time(); + log::trace!("Task gas: tasks_send_user_message = {gas}"); + + gas } } @@ -367,14 +397,19 @@ where ) -> Gas { let _slot = Self::remove_gas_reservation_impl(program_id, reservation_id); - ::WeightInfo::tasks_remove_gas_reservation().ref_time() + let gas = ::WeightInfo::tasks_remove_gas_reservation().ref_time(); + log::trace!("Task gas: tasks_remove_gas_reservation = {gas}"); + + gas } fn remove_resume_session(&mut self, session_id: SessionId) -> Gas { - log::debug!("Execute task to remove resume session with session_id = {session_id}"); ProgramStorageOf::::remove_resume_session(session_id) .unwrap_or_else(|e| unreachable!("ProgramStorage corrupted! {:?}", e)); - ::WeightInfo::tasks_remove_resume_session().ref_time() + let gas = ::WeightInfo::tasks_remove_resume_session().ref_time(); + log::trace!("Task gas: tasks_remove_resume_session = {gas}"); + + gas } } From db1ac81b7af7cf637e648e947296be3b49b2d884 Mon Sep 17 00:00:00 2001 From: Georgy Shepelev Date: Thu, 14 Sep 2023 16:35:25 +0400 Subject: [PATCH 32/32] update weights --- pallets/gear/src/weights.rs | 204 +++++++++++------------ runtime/gear/src/weights/pallet_gear.rs | 204 +++++++++++------------ runtime/vara/src/weights/pallet_gear.rs | 212 ++++++++++++------------ 3 files changed, 310 insertions(+), 310 deletions(-) diff --git a/pallets/gear/src/weights.rs b/pallets/gear/src/weights.rs index 6439d0d75c7..3ad8efedec0 100644 --- a/pallets/gear/src/weights.rs +++ b/pallets/gear/src/weights.rs @@ -2077,53 +2077,53 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 5_698_000 picoseconds. + Weight::from_parts(5_953_000, 4169) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(T::DbWeight::get().reads(5_u64)) - .saturating_add(T::DbWeight::get().writes(4_u64)) + // Measured: `1003` + // Estimated: `23637` + // Minimum execution time: 61_615_000 picoseconds. + Weight::from_parts(64_309_000, 23637) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `784` + // Estimated: `21534` + // Minimum execution time: 43_449_000 picoseconds. + Weight::from_parts(44_990_000, 21534) .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(T::DbWeight::get().reads(9_u64)) - .saturating_add(T::DbWeight::get().writes(8_u64)) + // Measured: `906` + // Estimated: `33891` + // Minimum execution time: 75_764_000 picoseconds. + Weight::from_parts(77_875_000, 33891) + .saturating_add(T::DbWeight::get().reads(11_u64)) + .saturating_add(T::DbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `591` + // Estimated: `19885` + // Minimum execution time: 31_362_000 picoseconds. + Weight::from_parts(32_425_000, 19885) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `872` + // Estimated: `25908` + // Minimum execution time: 45_023_000 picoseconds. + Weight::from_parts(46_479_000, 25908) .saturating_add(T::DbWeight::get().reads(8_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } @@ -2131,37 +2131,37 @@ impl WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_365_000 picoseconds. + Weight::from_parts(3_639_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(T::DbWeight::get().reads(14_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1522` + // Estimated: `57192` + // Minimum execution time: 114_023_000 picoseconds. + Weight::from_parts(117_157_000, 57192) + .saturating_add(T::DbWeight::get().reads(16_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(T::DbWeight::get().reads(12_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1228` + // Estimated: `46026` + // Minimum execution time: 90_320_000 picoseconds. + Weight::from_parts(93_096_000, 46026) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `2200 + c * (16400 ±0)` // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Minimum execution time: 28_326_000 picoseconds. + Weight::from_parts(28_612_000, 19363) + // Standard Error: 73_191 + .saturating_add(Weight::from_parts(39_403_570, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -2171,13 +2171,13 @@ impl WeightInfo for SubstrateWeight { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(T::DbWeight::get().reads(14_u64)) + // Measured: `3025 + c * (42 ±0)` + // Estimated: `59431 + c * (2947 ±0)` + // Minimum execution time: 86_517_000 picoseconds. + Weight::from_parts(78_047_954, 59431) + // Standard Error: 2_660 + .saturating_add(Weight::from_parts(1_060_607, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(13_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(9_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) @@ -4028,53 +4028,53 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 5_698_000 picoseconds. + Weight::from_parts(5_953_000, 4169) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(RocksDbWeight::get().reads(5_u64)) - .saturating_add(RocksDbWeight::get().writes(4_u64)) + // Measured: `1003` + // Estimated: `23637` + // Minimum execution time: 61_615_000 picoseconds. + Weight::from_parts(64_309_000, 23637) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `784` + // Estimated: `21534` + // Minimum execution time: 43_449_000 picoseconds. + Weight::from_parts(44_990_000, 21534) .saturating_add(RocksDbWeight::get().reads(6_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(RocksDbWeight::get().reads(9_u64)) - .saturating_add(RocksDbWeight::get().writes(8_u64)) + // Measured: `906` + // Estimated: `33891` + // Minimum execution time: 75_764_000 picoseconds. + Weight::from_parts(77_875_000, 33891) + .saturating_add(RocksDbWeight::get().reads(11_u64)) + .saturating_add(RocksDbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `591` + // Estimated: `19885` + // Minimum execution time: 31_362_000 picoseconds. + Weight::from_parts(32_425_000, 19885) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `872` + // Estimated: `25908` + // Minimum execution time: 45_023_000 picoseconds. + Weight::from_parts(46_479_000, 25908) .saturating_add(RocksDbWeight::get().reads(8_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } @@ -4082,37 +4082,37 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_365_000 picoseconds. + Weight::from_parts(3_639_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(RocksDbWeight::get().reads(14_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1522` + // Estimated: `57192` + // Minimum execution time: 114_023_000 picoseconds. + Weight::from_parts(117_157_000, 57192) + .saturating_add(RocksDbWeight::get().reads(16_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(RocksDbWeight::get().reads(12_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1228` + // Estimated: `46026` + // Minimum execution time: 90_320_000 picoseconds. + Weight::from_parts(93_096_000, 46026) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `2200 + c * (16400 ±0)` // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Minimum execution time: 28_326_000 picoseconds. + Weight::from_parts(28_612_000, 19363) + // Standard Error: 73_191 + .saturating_add(Weight::from_parts(39_403_570, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -4122,13 +4122,13 @@ impl WeightInfo for () { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(RocksDbWeight::get().reads(14_u64)) + // Measured: `3025 + c * (42 ±0)` + // Estimated: `59431 + c * (2947 ±0)` + // Minimum execution time: 86_517_000 picoseconds. + Weight::from_parts(78_047_954, 59431) + // Standard Error: 2_660 + .saturating_add(Weight::from_parts(1_060_607, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(13_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(9_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) diff --git a/runtime/gear/src/weights/pallet_gear.rs b/runtime/gear/src/weights/pallet_gear.rs index 600811fa09e..8b14d71ab58 100644 --- a/runtime/gear/src/weights/pallet_gear.rs +++ b/runtime/gear/src/weights/pallet_gear.rs @@ -2077,53 +2077,53 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 5_698_000 picoseconds. + Weight::from_parts(5_953_000, 4169) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(T::DbWeight::get().reads(5_u64)) - .saturating_add(T::DbWeight::get().writes(4_u64)) + // Measured: `1003` + // Estimated: `23637` + // Minimum execution time: 61_615_000 picoseconds. + Weight::from_parts(64_309_000, 23637) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `784` + // Estimated: `21534` + // Minimum execution time: 43_449_000 picoseconds. + Weight::from_parts(44_990_000, 21534) .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(T::DbWeight::get().reads(9_u64)) - .saturating_add(T::DbWeight::get().writes(8_u64)) + // Measured: `906` + // Estimated: `33891` + // Minimum execution time: 75_764_000 picoseconds. + Weight::from_parts(77_875_000, 33891) + .saturating_add(T::DbWeight::get().reads(11_u64)) + .saturating_add(T::DbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `591` + // Estimated: `19885` + // Minimum execution time: 31_362_000 picoseconds. + Weight::from_parts(32_425_000, 19885) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `872` + // Estimated: `25908` + // Minimum execution time: 45_023_000 picoseconds. + Weight::from_parts(46_479_000, 25908) .saturating_add(T::DbWeight::get().reads(8_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } @@ -2131,37 +2131,37 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_365_000 picoseconds. + Weight::from_parts(3_639_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(T::DbWeight::get().reads(14_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1522` + // Estimated: `57192` + // Minimum execution time: 114_023_000 picoseconds. + Weight::from_parts(117_157_000, 57192) + .saturating_add(T::DbWeight::get().reads(16_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(T::DbWeight::get().reads(12_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1228` + // Estimated: `46026` + // Minimum execution time: 90_320_000 picoseconds. + Weight::from_parts(93_096_000, 46026) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `2200 + c * (16400 ±0)` // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Minimum execution time: 28_326_000 picoseconds. + Weight::from_parts(28_612_000, 19363) + // Standard Error: 73_191 + .saturating_add(Weight::from_parts(39_403_570, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -2171,13 +2171,13 @@ impl pallet_gear::WeightInfo for SubstrateWeight { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(T::DbWeight::get().reads(14_u64)) + // Measured: `3025 + c * (42 ±0)` + // Estimated: `59431 + c * (2947 ±0)` + // Minimum execution time: 86_517_000 picoseconds. + Weight::from_parts(78_047_954, 59431) + // Standard Error: 2_660 + .saturating_add(Weight::from_parts(1_060_607, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(13_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(9_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) @@ -4028,53 +4028,53 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 5_698_000 picoseconds. + Weight::from_parts(5_953_000, 4169) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(RocksDbWeight::get().reads(5_u64)) - .saturating_add(RocksDbWeight::get().writes(4_u64)) + // Measured: `1003` + // Estimated: `23637` + // Minimum execution time: 61_615_000 picoseconds. + Weight::from_parts(64_309_000, 23637) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `784` + // Estimated: `21534` + // Minimum execution time: 43_449_000 picoseconds. + Weight::from_parts(44_990_000, 21534) .saturating_add(RocksDbWeight::get().reads(6_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(RocksDbWeight::get().reads(9_u64)) - .saturating_add(RocksDbWeight::get().writes(8_u64)) + // Measured: `906` + // Estimated: `33891` + // Minimum execution time: 75_764_000 picoseconds. + Weight::from_parts(77_875_000, 33891) + .saturating_add(RocksDbWeight::get().reads(11_u64)) + .saturating_add(RocksDbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `591` + // Estimated: `19885` + // Minimum execution time: 31_362_000 picoseconds. + Weight::from_parts(32_425_000, 19885) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `872` + // Estimated: `25908` + // Minimum execution time: 45_023_000 picoseconds. + Weight::from_parts(46_479_000, 25908) .saturating_add(RocksDbWeight::get().reads(8_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } @@ -4082,37 +4082,37 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_365_000 picoseconds. + Weight::from_parts(3_639_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(RocksDbWeight::get().reads(14_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1522` + // Estimated: `57192` + // Minimum execution time: 114_023_000 picoseconds. + Weight::from_parts(117_157_000, 57192) + .saturating_add(RocksDbWeight::get().reads(16_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(RocksDbWeight::get().reads(12_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1228` + // Estimated: `46026` + // Minimum execution time: 90_320_000 picoseconds. + Weight::from_parts(93_096_000, 46026) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: // Measured: `2200 + c * (16400 ±0)` // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Minimum execution time: 28_326_000 picoseconds. + Weight::from_parts(28_612_000, 19363) + // Standard Error: 73_191 + .saturating_add(Weight::from_parts(39_403_570, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -4122,13 +4122,13 @@ impl WeightInfo for () { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(RocksDbWeight::get().reads(14_u64)) + // Measured: `3025 + c * (42 ±0)` + // Estimated: `59431 + c * (2947 ±0)` + // Minimum execution time: 86_517_000 picoseconds. + Weight::from_parts(78_047_954, 59431) + // Standard Error: 2_660 + .saturating_add(Weight::from_parts(1_060_607, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(13_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(9_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into()))) diff --git a/runtime/vara/src/weights/pallet_gear.rs b/runtime/vara/src/weights/pallet_gear.rs index 600811fa09e..c5702325ce6 100644 --- a/runtime/vara/src/weights/pallet_gear.rs +++ b/runtime/vara/src/weights/pallet_gear.rs @@ -2077,53 +2077,53 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 6_081_000 picoseconds. + Weight::from_parts(6_314_000, 4169) .saturating_add(T::DbWeight::get().reads(1_u64)) .saturating_add(T::DbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(T::DbWeight::get().reads(5_u64)) - .saturating_add(T::DbWeight::get().writes(4_u64)) + // Measured: `1107` + // Estimated: `24053` + // Minimum execution time: 61_245_000 picoseconds. + Weight::from_parts(64_310_000, 24053) + .saturating_add(T::DbWeight::get().reads(7_u64)) + .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `888` + // Estimated: `22158` + // Minimum execution time: 47_184_000 picoseconds. + Weight::from_parts(48_335_000, 22158) .saturating_add(T::DbWeight::get().reads(6_u64)) .saturating_add(T::DbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(T::DbWeight::get().reads(9_u64)) - .saturating_add(T::DbWeight::get().writes(8_u64)) + // Measured: `1010` + // Estimated: `34619` + // Minimum execution time: 75_767_000 picoseconds. + Weight::from_parts(77_229_000, 34619) + .saturating_add(T::DbWeight::get().reads(11_u64)) + .saturating_add(T::DbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `695` + // Estimated: `20509` + // Minimum execution time: 31_513_000 picoseconds. + Weight::from_parts(33_232_000, 20509) .saturating_add(T::DbWeight::get().reads(7_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `976` + // Estimated: `26636` + // Minimum execution time: 48_739_000 picoseconds. + Weight::from_parts(49_963_000, 26636) .saturating_add(T::DbWeight::get().reads(8_u64)) .saturating_add(T::DbWeight::get().writes(6_u64)) } @@ -2131,37 +2131,37 @@ impl pallet_gear::WeightInfo for SubstrateWeight { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_513_000 picoseconds. + Weight::from_parts(3_670_000, 3545) .saturating_add(T::DbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(T::DbWeight::get().reads(14_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1626` + // Estimated: `58232` + // Minimum execution time: 109_582_000 picoseconds. + Weight::from_parts(112_343_000, 58232) + .saturating_add(T::DbWeight::get().reads(16_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(T::DbWeight::get().reads(12_u64)) - .saturating_add(T::DbWeight::get().writes(11_u64)) + // Measured: `1332` + // Estimated: `46962` + // Minimum execution time: 90_789_000 picoseconds. + Weight::from_parts(93_329_000, 46962) + .saturating_add(T::DbWeight::get().reads(14_u64)) + .saturating_add(T::DbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2200 + c * (16400 ±0)` - // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Measured: `2303 + c * (16400 ±0)` + // Estimated: `19878 + c * (84480 ±0)` + // Minimum execution time: 31_262_000 picoseconds. + Weight::from_parts(31_610_000, 19878) + // Standard Error: 69_131 + .saturating_add(Weight::from_parts(39_928_419, 0).saturating_mul(c.into())) .saturating_add(T::DbWeight::get().reads(4_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(2_u64)) @@ -2171,13 +2171,13 @@ impl pallet_gear::WeightInfo for SubstrateWeight { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(T::DbWeight::get().reads(14_u64)) + // Measured: `3129 + c * (42 ±0)` + // Estimated: `60575 + c * (2947 ±0)` + // Minimum execution time: 91_223_000 picoseconds. + Weight::from_parts(98_002_861, 60575) + // Standard Error: 2_086 + .saturating_add(Weight::from_parts(1_092_801, 0).saturating_mul(c.into())) + .saturating_add(T::DbWeight::get().reads(13_u64)) .saturating_add(T::DbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(T::DbWeight::get().writes(9_u64)) .saturating_add(T::DbWeight::get().writes((1_u64).saturating_mul(c.into()))) @@ -4028,53 +4028,53 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `352` // Estimated: `4169` - // Minimum execution time: 3_000_000 picoseconds. - Weight::from_parts(3_000_000, 4169) + // Minimum execution time: 6_081_000 picoseconds. + Weight::from_parts(6_314_000, 4169) .saturating_add(RocksDbWeight::get().reads(1_u64)) .saturating_add(RocksDbWeight::get().writes(2_u64)) } fn tasks_remove_gas_reservation() -> Weight { // Proof Size summary in bytes: - // Measured: `714` - // Estimated: `16349` - // Minimum execution time: 19_000_000 picoseconds. - Weight::from_parts(21_000_000, 16349) - .saturating_add(RocksDbWeight::get().reads(5_u64)) - .saturating_add(RocksDbWeight::get().writes(4_u64)) + // Measured: `1107` + // Estimated: `24053` + // Minimum execution time: 61_245_000 picoseconds. + Weight::from_parts(64_310_000, 24053) + .saturating_add(RocksDbWeight::get().reads(7_u64)) + .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_send_user_message_to_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `734` - // Estimated: `21234` - // Minimum execution time: 18_000_000 picoseconds. - Weight::from_parts(19_000_000, 21234) + // Measured: `888` + // Estimated: `22158` + // Minimum execution time: 47_184_000 picoseconds. + Weight::from_parts(48_335_000, 22158) .saturating_add(RocksDbWeight::get().reads(6_u64)) .saturating_add(RocksDbWeight::get().writes(5_u64)) } fn tasks_send_user_message() -> Weight { // Proof Size summary in bytes: - // Measured: `617` - // Estimated: `25736` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25736) - .saturating_add(RocksDbWeight::get().reads(9_u64)) - .saturating_add(RocksDbWeight::get().writes(8_u64)) + // Measured: `1010` + // Estimated: `34619` + // Minimum execution time: 75_767_000 picoseconds. + Weight::from_parts(77_229_000, 34619) + .saturating_add(RocksDbWeight::get().reads(11_u64)) + .saturating_add(RocksDbWeight::get().writes(10_u64)) } fn tasks_send_dispatch() -> Weight { // Proof Size summary in bytes: - // Measured: `561` - // Estimated: `19705` - // Minimum execution time: 15_000_000 picoseconds. - Weight::from_parts(16_000_000, 19705) + // Measured: `695` + // Estimated: `20509` + // Minimum execution time: 31_513_000 picoseconds. + Weight::from_parts(33_232_000, 20509) .saturating_add(RocksDbWeight::get().reads(7_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } fn tasks_wake_message() -> Weight { // Proof Size summary in bytes: - // Measured: `823` - // Estimated: `25565` - // Minimum execution time: 20_000_000 picoseconds. - Weight::from_parts(21_000_000, 25565) + // Measured: `976` + // Estimated: `26636` + // Minimum execution time: 48_739_000 picoseconds. + Weight::from_parts(49_963_000, 26636) .saturating_add(RocksDbWeight::get().reads(8_u64)) .saturating_add(RocksDbWeight::get().writes(6_u64)) } @@ -4082,37 +4082,37 @@ impl WeightInfo for () { // Proof Size summary in bytes: // Measured: `80` // Estimated: `3545` - // Minimum execution time: 2_000_000 picoseconds. - Weight::from_parts(2_000_000, 3545) + // Minimum execution time: 3_513_000 picoseconds. + Weight::from_parts(3_670_000, 3545) .saturating_add(RocksDbWeight::get().reads(1_u64)) } fn tasks_remove_from_waitlist() -> Weight { // Proof Size summary in bytes: - // Measured: `1200` - // Estimated: `47840` - // Minimum execution time: 41_000_000 picoseconds. - Weight::from_parts(44_000_000, 47840) - .saturating_add(RocksDbWeight::get().reads(14_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1626` + // Estimated: `58232` + // Minimum execution time: 109_582_000 picoseconds. + Weight::from_parts(112_343_000, 58232) + .saturating_add(RocksDbWeight::get().reads(16_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } fn tasks_remove_from_mailbox() -> Weight { // Proof Size summary in bytes: - // Measured: `907` - // Estimated: `37005` - // Minimum execution time: 30_000_000 picoseconds. - Weight::from_parts(33_000_000, 37005) - .saturating_add(RocksDbWeight::get().reads(12_u64)) - .saturating_add(RocksDbWeight::get().writes(11_u64)) + // Measured: `1332` + // Estimated: `46962` + // Minimum execution time: 90_789_000 picoseconds. + Weight::from_parts(93_329_000, 46962) + .saturating_add(RocksDbWeight::get().reads(14_u64)) + .saturating_add(RocksDbWeight::get().writes(13_u64)) } /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2200 + c * (16400 ±0)` - // Estimated: `19363 + c * (84480 ±0)` - // Minimum execution time: 39_946_000 picoseconds. - Weight::from_parts(40_607_000, 19363) - // Standard Error: 46_882 - .saturating_add(Weight::from_parts(30_965_406, 0).saturating_mul(c.into())) + // Measured: `2303 + c * (16400 ±0)` + // Estimated: `19878 + c * (84480 ±0)` + // Minimum execution time: 31_262_000 picoseconds. + Weight::from_parts(31_610_000, 19878) + // Standard Error: 69_131 + .saturating_add(Weight::from_parts(39_928_419, 0).saturating_mul(c.into())) .saturating_add(RocksDbWeight::get().reads(4_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(2_u64)) @@ -4122,13 +4122,13 @@ impl WeightInfo for () { /// The range of component `c` is `[0, 2044]`. fn tasks_pause_program_uninited(c: u32, ) -> Weight { // Proof Size summary in bytes: - // Measured: `2975 + c * (42 ±0)` - // Estimated: `61356 + c * (2947 ±0)` - // Minimum execution time: 104_317_000 picoseconds. - Weight::from_parts(160_861_611, 61356) - // Standard Error: 3_255 - .saturating_add(Weight::from_parts(1_076_170, 0).saturating_mul(c.into())) - .saturating_add(RocksDbWeight::get().reads(14_u64)) + // Measured: `3129 + c * (42 ±0)` + // Estimated: `60575 + c * (2947 ±0)` + // Minimum execution time: 91_223_000 picoseconds. + Weight::from_parts(98_002_861, 60575) + // Standard Error: 2_086 + .saturating_add(Weight::from_parts(1_092_801, 0).saturating_mul(c.into())) + .saturating_add(RocksDbWeight::get().reads(13_u64)) .saturating_add(RocksDbWeight::get().reads((1_u64).saturating_mul(c.into()))) .saturating_add(RocksDbWeight::get().writes(9_u64)) .saturating_add(RocksDbWeight::get().writes((1_u64).saturating_mul(c.into())))