Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add benchmarking for parachain runtime ump pallet #3889

Merged
merged 79 commits into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
79 commits
Select commit Hold shift + click to select a range
b5b483d
Claim grounds, not working yet.
hirschenberger Sep 18, 2021
80c429b
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Sep 27, 2021
ab8669c
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Sep 27, 2021
f76f68f
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Sep 28, 2021
ce8a9d7
still wip, nothing to see
hirschenberger Sep 29, 2021
ec01972
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Oct 1, 2021
d0c192e
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Oct 2, 2021
e5be79e
Module visibility problem
hirschenberger Oct 3, 2021
a67cf3f
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Oct 5, 2021
021b161
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Oct 8, 2021
b33272c
Add benchmarks for all parachain-enabled chains
hirschenberger Oct 11, 2021
a3330da
Add missing mock weights
hirschenberger Oct 11, 2021
06cfaba
Fix mock weight info
hirschenberger Oct 11, 2021
4fea1a2
Add max-weight to extrinsic weight
hirschenberger Oct 11, 2021
664d159
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Oct 12, 2021
6ed9b4b
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Oct 12, 2021
29f22a6
sorting declarations
hirschenberger Oct 12, 2021
f0af485
Merge remote-tracking branch 'hirschenberger/issue_3850_ump' into iss…
hirschenberger Oct 12, 2021
1839089
Use a maximum size message to benchmark worst-case
hirschenberger Oct 12, 2021
f301300
remove warning
hirschenberger Oct 12, 2021
7fc2677
Merge remote-tracking branch 'hirschenberger/issue_3850_ump' into iss…
hirschenberger Oct 12, 2021
4c266c0
Fix msg size overflow in tests
hirschenberger Oct 12, 2021
cb2e886
Use benchmarked weight in PostInfo
hirschenberger Oct 18, 2021
db93791
Update runtime/parachains/src/ump.rs
hirschenberger Oct 21, 2021
7c07f51
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Oct 28, 2021
0daa3c2
Delete obsolete file
hirschenberger Oct 29, 2021
a0cef07
Compile fix
hirschenberger Oct 29, 2021
9fd5cc3
Fix spelling
hirschenberger Oct 29, 2021
6d67445
Add benchmark for `UmpSink::process_upward_message`
hirschenberger Oct 29, 2021
e654409
Fix warning
hirschenberger Oct 29, 2021
c189f7a
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Oct 30, 2021
c316d56
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Oct 30, 2021
b78d21a
cargo run --quiet --release --features=runtime-benchmarks -- benchmar…
Oct 30, 2021
f88e7ee
Update runtime/parachains/src/ump.rs
hirschenberger Nov 1, 2021
0e7cd37
Give mock weights for tests a name instead of ()
hirschenberger Nov 5, 2021
47647c2
Merge branch 'issue_3850_ump' of github.com:hirschenberger/polkadot i…
hirschenberger Nov 5, 2021
cc33e9e
Add benchmark for `initializer_on_new_session` aka.
hirschenberger Nov 5, 2021
75e3dd0
Remove unnescessary pub specifier and change folding to a for loop
hirschenberger Nov 10, 2021
b235bb6
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Nov 10, 2021
1d9873f
Update runtime/parachains/src/ump.rs
hirschenberger Nov 18, 2021
4b84e06
Update runtime/parachains/src/ump.rs
hirschenberger Nov 18, 2021
17982a5
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Nov 18, 2021
7482848
Add comment on weight calculation and expected slight overestimation
hirschenberger Nov 18, 2021
6610270
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Nov 22, 2021
787608e
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Dec 3, 2021
aa2a4ee
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Dec 7, 2021
283f98a
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Dec 14, 2021
220c0b1
Introduce a size bound constant for upward messages
hirschenberger Dec 15, 2021
4f3ff46
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Dec 15, 2021
ea9ff4f
Use KiB not kB for message size bound
hirschenberger Dec 15, 2021
dfde952
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Dec 15, 2021
4f8ecb1
Enforce max-message size bound
hirschenberger Dec 16, 2021
d11f098
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Jan 7, 2022
699717e
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Jan 17, 2022
5eafb12
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Jan 18, 2022
28007f2
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Jan 21, 2022
70f5f18
Use a const for upward message size bound.
hirschenberger Jan 21, 2022
5251072
Decrease max_upward_message_sizes to 50KiB as suggested by @pepyakin
hirschenberger Jan 21, 2022
1cf033f
Fix creating overweight messages
hirschenberger Jan 26, 2022
bb0e3d9
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Jan 26, 2022
d1e7ab2
Merge branch 'master' of https://github.com/paritytech/polkadot into …
Jan 29, 2022
2bada43
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 29, 2022
a36d37c
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 29, 2022
29f7451
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 29, 2022
eeb51e6
Remove unused variable
hirschenberger Jan 29, 2022
04df439
Merge branch 'master' of https://github.com/paritytech/polkadot into …
hirschenberger Jan 30, 2022
0856dd2
Merge branch 'issue_3850_ump' of github.com:hirschenberger/polkadot i…
hirschenberger Jan 30, 2022
3ca5fa5
Rename benchmark function to the function actually benchmarked and some
hirschenberger Jan 30, 2022
73d0e81
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 30, 2022
55d773e
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 30, 2022
019a989
cargo run --quiet --profile=production --features=runtime-benchmarks…
Jan 30, 2022
9be975e
Merge remote-tracking branch 'origin/master' into issue_3850_ump
hirschenberger Feb 4, 2022
c75aa20
Better variable naming
hirschenberger Feb 4, 2022
7938978
Merge branch 'issue_3850_ump' of github.com:hirschenberger/polkadot i…
hirschenberger Feb 4, 2022
3e98695
Fix build
hirschenberger Feb 4, 2022
1c62492
Fix build
hirschenberger Feb 4, 2022
799f9a1
Merge branch 'issue_3850_ump' of github.com:hirschenberger/polkadot i…
hirschenberger Feb 4, 2022
441b6c4
Fix some remarks from @pepyakin
hirschenberger Feb 9, 2022
780d40f
Merge remote-tracking branch 'upstream/master' into issue_3850_ump
emostov Feb 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bridges/bin/rialto/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ fn testnet_genesis(
// an import since that's a made up number and should be replaced with a constant
// obtained by benchmarking anyway.
ump_service_total_weight: 4 * 1_000_000_000,
max_upward_message_size: 1024 * 1024,
max_upward_message_size: 50 * 1024,
emostov marked this conversation as resolved.
Show resolved Hide resolved
max_upward_message_num_per_candidate: 5,
hrmp_sender_deposit: 0,
hrmp_recipient_deposit: 0,
Expand Down
2 changes: 1 addition & 1 deletion node/service/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ fn default_parachains_host_configuration(
max_upward_queue_size: 1024 * 1024,
max_downward_message_size: 1024 * 1024,
ump_service_total_weight: 100_000_000_000,
max_upward_message_size: 1024 * 1024,
max_upward_message_size: 50 * 1024,
max_upward_message_num_per_candidate: 5,
hrmp_sender_deposit: 0,
hrmp_recipient_deposit: 0,
Expand Down
2 changes: 2 additions & 0 deletions runtime/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1228,6 +1228,7 @@ impl parachains_ump::Config for Runtime {
crate::parachains_ump::XcmSink<xcm_executor::XcmExecutor<xcm_config::XcmConfig>, Runtime>;
type FirstMessageFactorPercent = FirstMessageFactorPercent;
type ExecuteOverweightOrigin = EnsureRoot<AccountId>;
type WeightInfo = weights::runtime_parachains_ump::WeightInfo<Runtime>;
}

impl parachains_dmp::Config for Runtime {}
Expand Down Expand Up @@ -1543,6 +1544,7 @@ mod benches {
[runtime_parachains::initializer, Initializer]
[runtime_parachains::paras_inherent, ParaInherent]
[runtime_parachains::paras, Paras]
[runtime_parachains::ump, Ump]
// Substrate
[pallet_balances, Balances]
[pallet_bags_list, BagsList]
Expand Down
1 change: 1 addition & 0 deletions runtime/kusama/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ pub mod runtime_parachains_hrmp;
pub mod runtime_parachains_initializer;
pub mod runtime_parachains_paras;
pub mod runtime_parachains_paras_inherent;
pub mod runtime_parachains_ump;
66 changes: 66 additions & 0 deletions runtime/kusama/src/weights/runtime_parachains_ump.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Copyright 2017-2021 Parity Technologies (UK) Ltd.
// This file is part of Polkadot.

// Polkadot 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.

// Polkadot 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 Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Autogenerated weights for `runtime_parachains::ump`
//!
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 4.0.0-dev
//! DATE: 2022-01-30, STEPS: `50`, REPEAT: 20, LOW RANGE: `[]`, HIGH RANGE: `[]`
//! EXECUTION: Some(Wasm), WASM-EXECUTION: Compiled, CHAIN: Some("kusama-dev"), DB CACHE: 1024

// Executed Command:
// target/production/polkadot
// benchmark
// --chain=kusama-dev
// --steps=50
// --repeat=20
// --pallet=runtime_parachains::ump
// --extrinsic=*
// --execution=wasm
// --wasm-execution=compiled
// --heap-pages=4096
// --header=./file_header.txt
// --output=./runtime/kusama/src/weights/runtime_parachains_ump.rs

#![cfg_attr(rustfmt, rustfmt_skip)]
#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::{traits::Get, weights::Weight};
use sp_std::marker::PhantomData;

/// Weight functions for `runtime_parachains::ump`.
pub struct WeightInfo<T>(PhantomData<T>);
impl<T: frame_system::Config> runtime_parachains::ump::WeightInfo for WeightInfo<T> {
fn sink_process_upward_message(s: u32, ) -> Weight {
(3_715_000 as Weight)
// Standard Error: 0
.saturating_add((2_000 as Weight).saturating_mul(s as Weight))
}
// Storage: Ump NeedsDispatch (r:1 w:1)
// Storage: Ump NextDispatchRoundStartWith (r:1 w:1)
// Storage: Ump RelayDispatchQueues (r:0 w:1)
// Storage: Ump RelayDispatchQueueSize (r:0 w:1)
fn clean_ump_after_outgoing() -> Weight {
(6_711_000 as Weight)
.saturating_add(T::DbWeight::get().reads(2 as Weight))
.saturating_add(T::DbWeight::get().writes(4 as Weight))
}
// Storage: Ump Overweight (r:1 w:1)
fn service_overweight() -> Weight {
(16_427_000 as Weight)
.saturating_add(T::DbWeight::get().reads(1 as Weight))
.saturating_add(T::DbWeight::get().writes(1 as Weight))
}
}
8 changes: 8 additions & 0 deletions runtime/parachains/src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,8 @@ pub enum InconsistentError<BlockNumber> {
},
/// `validation_upgrade_delay` is less than or equal 1.
ValidationUpgradeDelayIsTooLow { validation_upgrade_delay: BlockNumber },
/// Maximum UMP message size (`MAX_UPWARD_MESSAGE_SIZE_BOUND`) exceeded.
MaxUpwardMessageSizeExceeded { max_message_size: u32 },
/// Maximum number of HRMP outbound channels exceeded.
MaxHrmpOutboundChannelsExceeded,
/// Maximum number of HRMP inbound channels exceeded.
Expand Down Expand Up @@ -388,6 +390,12 @@ where
})
}

if self.max_upward_message_size > crate::ump::MAX_UPWARD_MESSAGE_SIZE_BOUND {
return Err(MaxUpwardMessageSizeExceeded {
max_message_size: self.max_upward_message_size,
})
}

if self.hrmp_max_parachain_outbound_channels > crate::hrmp::HRMP_MAX_OUTBOUND_CHANNELS_BOUND
{
return Err(MaxHrmpOutboundChannelsExceeded)
Expand Down
1 change: 1 addition & 0 deletions runtime/parachains/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ impl crate::ump::Config for Test {
type UmpSink = TestUmpSink;
type FirstMessageFactorPercent = FirstMessageFactorPercent;
type ExecuteOverweightOrigin = frame_system::EnsureRoot<AccountId>;
type WeightInfo = crate::ump::TestWeightInfo;
}

impl crate::hrmp::Config for Test {
Expand Down
78 changes: 63 additions & 15 deletions runtime/parachains/src/ump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ use xcm::latest::Outcome;

pub use pallet::*;

/// Maximum value that `config.max_upward_message_size` can be set to
///
/// This is used for benchmarking sanely bounding relevant storate items. It is expected from the `configurations`
/// pallet to check these values before setting.
pub const MAX_UPWARD_MESSAGE_SIZE_BOUND: u32 = 50 * 1024;

#[cfg(feature = "runtime-benchmarks")]
mod benchmarking;

#[cfg(test)]
pub(crate) mod tests;

Expand Down Expand Up @@ -99,29 +108,38 @@ impl<XcmExecutor: xcm::latest::ExecuteXcm<C::Call>, C: Config> UmpSink for XcmSi
};

let id = upward_message_id(&data[..]);
let maybe_msg = VersionedXcm::<C::Call>::decode_all_with_depth_limit(
let maybe_msg_and_weight = VersionedXcm::<C::Call>::decode_all_with_depth_limit(
xcm::MAX_XCM_DECODE_DEPTH,
&mut data,
)
.map(Xcm::<C::Call>::try_from);
match maybe_msg {
.map(|xcm| {
(
Xcm::<C::Call>::try_from(xcm),
// NOTE: We are overestimating slightly here.
// The benchmark is timing this whole function with different message sizes and a NOOP extrinsic to
// measure the size-dependent weight. But as we use the weight funtion **in** the benchmarked funtion we
// are taking call and control-flow overhead into account twice.
<C as Config>::WeightInfo::sink_process_upward_message(data.len() as u32),
)
});
match maybe_msg_and_weight {
Err(_) => {
Pallet::<C>::deposit_event(Event::InvalidFormat(id));
Ok(0)
},
Ok(Err(())) => {
Ok((Err(()), weight_used)) => {
Pallet::<C>::deposit_event(Event::UnsupportedVersion(id));
Ok(0)
Ok(weight_used)
},
Ok(Ok(xcm_message)) => {
Ok((Ok(xcm_message), weight_used)) => {
let xcm_junction = Junction::Parachain(origin.into());
let outcome = XcmExecutor::execute_xcm(xcm_junction, xcm_message, max_weight);
match outcome {
Outcome::Error(XcmError::WeightLimitReached(required)) => Err((id, required)),
outcome => {
let weight_used = outcome.weight_used();
let outcome_weight = outcome.weight_used();
Pallet::<C>::deposit_event(Event::ExecutedUpward(id, outcome));
Ok(weight_used)
Ok(weight_used.saturating_add(outcome_weight))
},
}
},
Expand Down Expand Up @@ -165,6 +183,29 @@ impl fmt::Debug for AcceptanceCheckErr {
}
}

/// Weight information of this pallet.
pub trait WeightInfo {
hirschenberger marked this conversation as resolved.
Show resolved Hide resolved
fn service_overweight() -> Weight;
fn sink_process_upward_message(s: u32) -> Weight;
fn clean_ump_after_outgoing() -> Weight;
}

/// fallback implementation
pub struct TestWeightInfo;
impl WeightInfo for TestWeightInfo {
fn service_overweight() -> Weight {
Weight::MAX
}

fn sink_process_upward_message(_msg_size: u32) -> Weight {
Weight::MAX
}

fn clean_ump_after_outgoing() -> Weight {
Weight::MAX
}
}

#[frame_support::pallet]
pub mod pallet {
use super::*;
Expand Down Expand Up @@ -192,6 +233,9 @@ pub mod pallet {

/// Origin which is allowed to execute overweight messages.
type ExecuteOverweightOrigin: EnsureOrigin<Self::Origin>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}

#[pallet::event]
Expand Down Expand Up @@ -303,7 +347,7 @@ pub mod pallet {
///
/// Events:
/// - `OverweightServiced`: On success.
#[pallet::weight(weight_limit.saturating_add(1_000_000))]
#[pallet::weight(weight_limit.saturating_add(<T as Config>::WeightInfo::service_overweight()))]
pub fn service_overweight(
origin: OriginFor<T>,
index: OverweightIndex,
Expand All @@ -317,7 +361,7 @@ pub mod pallet {
.map_err(|_| Error::<T>::WeightOverLimit)?;
Overweight::<T>::remove(index);
Self::deposit_event(Event::OverweightServiced(index, used));
Ok(Some(used.saturating_add(1_000_000)).into())
Ok(Some(used.saturating_add(<T as Config>::WeightInfo::service_overweight())).into())
}
}
}
Expand All @@ -336,20 +380,22 @@ impl<T: Config> Pallet<T> {
pub(crate) fn initializer_on_new_session(
_notification: &initializer::SessionChangeNotification<T::BlockNumber>,
outgoing_paras: &[ParaId],
) {
Self::perform_outgoing_para_cleanup(outgoing_paras);
) -> Weight {
Self::perform_outgoing_para_cleanup(outgoing_paras)
}

/// Iterate over all paras that were noted for offboarding and remove all the data
/// associated with them.
fn perform_outgoing_para_cleanup(outgoing: &[ParaId]) {
fn perform_outgoing_para_cleanup(outgoing: &[ParaId]) -> Weight {
let mut weight: Weight = 0;
for outgoing_para in outgoing {
Self::clean_ump_after_outgoing(outgoing_para);
weight = weight.saturating_add(Self::clean_ump_after_outgoing(outgoing_para));
}
weight
}

/// Remove all relevant storage items for an outgoing parachain.
fn clean_ump_after_outgoing(outgoing_para: &ParaId) {
pub(crate) fn clean_ump_after_outgoing(outgoing_para: &ParaId) -> Weight {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Why is this pub(crate) though? Can we constrain this to pub(in benchmarking) or in a similar fashion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I do pub(in crate::ump::benchmarking) the compiler complains: error[E0742]: visibilities can only be restricted to ancestor modules

<Self as Store>::RelayDispatchQueueSize::remove(outgoing_para);
<Self as Store>::RelayDispatchQueues::remove(outgoing_para);

Expand All @@ -366,6 +412,8 @@ impl<T: Config> Pallet<T> {
<Self as Store>::NextDispatchRoundStartWith::mutate(|v| {
*v = v.filter(|p| p == outgoing_para)
});

<T as Config>::WeightInfo::clean_ump_after_outgoing()
}

/// Check that all the upward messages sent by a candidate pass the acceptance criteria. Returns
Expand Down
Loading