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

Commit

Permalink
WeightInfo for Scheduler (#7138)
Browse files Browse the repository at this point in the history
* initial scheduler stuff

* integrate weightinfo

* Update pallet_scheduler.rs
  • Loading branch information
shawntabrizi authored and bkchr committed Sep 18, 2020
1 parent 7cc397f commit 98a6a8c
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 28 deletions.
4 changes: 3 additions & 1 deletion bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ impl pallet_proxy::Trait for Runtime {

parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub const MaxScheduledPerBlock: u32 = 50;
}

impl pallet_scheduler::Trait for Runtime {
Expand All @@ -282,7 +283,8 @@ impl pallet_scheduler::Trait for Runtime {
type Call = Call;
type MaximumWeight = MaximumSchedulerWeight;
type ScheduleOrigin = EnsureRoot<AccountId>;
type WeightInfo = ();
type MaxScheduledPerBlock = MaxScheduledPerBlock;
type WeightInfo = weights::pallet_scheduler::WeightInfo;
}

parameter_types! {
Expand Down
1 change: 1 addition & 0 deletions bin/node/runtime/src/weights/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub mod pallet_identity;
pub mod pallet_indices;
pub mod pallet_im_online;
pub mod pallet_proxy;
pub mod pallet_scheduler;
pub mod pallet_staking;
pub mod pallet_timestamp;
pub mod pallet_utility;
Expand Down
51 changes: 51 additions & 0 deletions bin/node/runtime/src/weights/pallet_scheduler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// This file is part of Substrate.

// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6

#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};

pub struct WeightInfo;
impl pallet_scheduler::WeightInfo for WeightInfo {
fn schedule(s: u32, ) -> Weight {
(37_835_000 as Weight)
.saturating_add((81_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(1 as Weight))
}
fn cancel(s: u32, ) -> Weight {
(34_707_000 as Weight)
.saturating_add((3_125_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn schedule_named(s: u32, ) -> Weight {
(48_065_000 as Weight)
.saturating_add((110_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn cancel_named(s: u32, ) -> Weight {
(38_776_000 as Weight)
.saturating_add((3_138_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
}
1 change: 1 addition & 0 deletions frame/democracy/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ impl pallet_scheduler::Trait for Test {
type Call = Call;
type MaximumWeight = MaximumSchedulerWeight;
type ScheduleOrigin = EnsureRoot<u64>;
type MaxScheduledPerBlock = ();
type WeightInfo = ();
}
parameter_types! {
Expand Down
13 changes: 7 additions & 6 deletions frame/scheduler/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use frame_benchmarking::benchmarks;
use crate::Module as Scheduler;
use frame_system::Module as System;

const MAX_SCHEDULED: u32 = 50;
const BLOCK_NUMBER: u32 = 2;

// Add `n` named items to the schedule
Expand Down Expand Up @@ -56,7 +55,7 @@ benchmarks! {
_ { }

schedule {
let s in 0 .. MAX_SCHEDULED;
let s in 0 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
let periodic = Some((T::BlockNumber::one(), 100));
let priority = 0;
Expand All @@ -73,7 +72,7 @@ benchmarks! {
}

cancel {
let s in 1 .. MAX_SCHEDULED;
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();

fill_schedule::<T>(when, s)?;
Expand All @@ -92,7 +91,7 @@ benchmarks! {
}

schedule_named {
let s in 0 .. MAX_SCHEDULED;
let s in 0 .. T::MaxScheduledPerBlock::get();
let id = s.encode();
let when = BLOCK_NUMBER.into();
let periodic = Some((T::BlockNumber::one(), 100));
Expand All @@ -110,7 +109,7 @@ benchmarks! {
}

cancel_named {
let s in 1 .. MAX_SCHEDULED;
let s in 1 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();

fill_schedule::<T>(when, s)?;
Expand All @@ -127,8 +126,10 @@ benchmarks! {
);
}

// TODO: Make this more complex and flexible so it can be used in automation.
#[extra]
on_initialize {
let s in 0 .. MAX_SCHEDULED;
let s in 0 .. T::MaxScheduledPerBlock::get();
let when = BLOCK_NUMBER.into();
fill_schedule::<T>(when, s)?;
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
Expand Down
50 changes: 50 additions & 0 deletions frame/scheduler/src/default_weights.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// This file is part of Substrate.

// Copyright (C) 2020 Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6

#![allow(unused_parens)]
#![allow(unused_imports)]

use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};

impl crate::WeightInfo for () {
fn schedule(s: u32, ) -> Weight {
(37_835_000 as Weight)
.saturating_add((81_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(1 as Weight))
}
fn cancel(s: u32, ) -> Weight {
(34_707_000 as Weight)
.saturating_add((3_125_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(1 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn schedule_named(s: u32, ) -> Weight {
(48_065_000 as Weight)
.saturating_add((110_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
fn cancel_named(s: u32, ) -> Weight {
(38_776_000 as Weight)
.saturating_add((3_138_000 as Weight).saturating_mul(s as Weight))
.saturating_add(DbWeight::get().reads(2 as Weight))
.saturating_add(DbWeight::get().writes(2 as Weight))
}
}
56 changes: 35 additions & 21 deletions frame/scheduler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

mod benchmarking;
mod default_weights;

use sp_std::{prelude::*, marker::PhantomData, borrow::Borrow};
use codec::{Encode, Decode, Codec};
Expand All @@ -69,15 +70,6 @@ pub trait WeightInfo {
fn cancel(s: u32, ) -> Weight;
fn schedule_named(s: u32, ) -> Weight;
fn cancel_named(s: u32, ) -> Weight;
fn on_initialize(s: u32, ) -> Weight;
}

impl WeightInfo for () {
fn schedule(_s: u32, ) -> Weight { 1_000_000_000 }
fn cancel(_s: u32, ) -> Weight { 1_000_000_000 }
fn schedule_named(_s: u32, ) -> Weight { 1_000_000_000 }
fn cancel_named(_s: u32, ) -> Weight { 1_000_000_000 }
fn on_initialize(_s: u32, ) -> Weight { 1_000_000_000 }
}

/// Our pallet's configuration trait. All our types and constants go in here. If the
Expand Down Expand Up @@ -106,6 +98,10 @@ pub trait Trait: system::Trait {
/// Required origin to schedule or cancel calls.
type ScheduleOrigin: EnsureOrigin<<Self as system::Trait>::Origin>;

/// The maximum number of scheduled calls in the queue for a single block.
/// Not strictly enforced, but used for weight estimation.
type MaxScheduledPerBlock: Get<u32>;

/// Weight information for extrinsics in this pallet.
type WeightInfo: WeightInfo;
}
Expand Down Expand Up @@ -213,7 +209,7 @@ decl_module! {
/// - Write: Agenda
/// - Will use base weight of 25 which should be good for up to 30 scheduled calls
/// # </weight>
#[weight = 25_000_000 + T::DbWeight::get().reads_writes(1, 1)]
#[weight = T::WeightInfo::schedule(T::MaxScheduledPerBlock::get())]
fn schedule(origin,
when: T::BlockNumber,
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
Expand All @@ -235,7 +231,7 @@ decl_module! {
/// - Write: Agenda, Lookup
/// - Will use base weight of 100 which should be good for up to 30 scheduled calls
/// # </weight>
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(1, 2)]
#[weight = T::WeightInfo::cancel(T::MaxScheduledPerBlock::get())]
fn cancel(origin, when: T::BlockNumber, index: u32) {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Trait>::Origin::from(origin);
Expand All @@ -252,7 +248,7 @@ decl_module! {
/// - Write: Agenda, Lookup
/// - Will use base weight of 35 which should be good for more than 30 scheduled calls
/// # </weight>
#[weight = 35_000_000 + T::DbWeight::get().reads_writes(2, 2)]
#[weight = T::WeightInfo::schedule_named(T::MaxScheduledPerBlock::get())]
fn schedule_named(origin,
id: Vec<u8>,
when: T::BlockNumber,
Expand All @@ -277,7 +273,7 @@ decl_module! {
/// - Write: Agenda, Lookup
/// - Will use base weight of 100 which should be good for up to 30 scheduled calls
/// # </weight>
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(2, 2)]
#[weight = T::WeightInfo::cancel_named(T::MaxScheduledPerBlock::get())]
fn cancel_named(origin, id: Vec<u8>) {
T::ScheduleOrigin::ensure_origin(origin.clone())?;
let origin = <T as Trait>::Origin::from(origin);
Expand All @@ -289,7 +285,7 @@ decl_module! {
/// # <weight>
/// Same as [`schedule`].
/// # </weight>
#[weight = 25_000_000 + T::DbWeight::get().reads_writes(1, 1)]
#[weight = T::WeightInfo::schedule(T::MaxScheduledPerBlock::get())]
fn schedule_after(origin,
after: T::BlockNumber,
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
Expand All @@ -308,7 +304,7 @@ decl_module! {
/// # <weight>
/// Same as [`schedule_named`].
/// # </weight>
#[weight = 35_000_000 + T::DbWeight::get().reads_writes(2, 2)]
#[weight = T::WeightInfo::schedule_named(T::MaxScheduledPerBlock::get())]
fn schedule_named_after(origin,
id: Vec<u8>,
after: T::BlockNumber,
Expand Down Expand Up @@ -340,16 +336,20 @@ decl_module! {
.enumerate()
.filter_map(|(index, s)| s.map(|inner| (index as u32, inner)))
.collect::<Vec<_>>();
if queued.len() as u32 > T::MaxScheduledPerBlock::get() {
frame_support::debug::warn!(
"Warning: This block has more items queued in Scheduler than \
expected from the runtime configuration. An update might be needed."
);
}
queued.sort_by_key(|(_, s)| s.priority);
let base_weight: Weight = T::DbWeight::get().reads_writes(1, 2) // Agenda + Agenda(next)
.saturating_add(10_000_000); // Base Weight
let base_weight: Weight = T::DbWeight::get().reads_writes(1, 2); // Agenda + Agenda(next)
let mut total_weight: Weight = 0;
queued.into_iter()
.enumerate()
.scan(base_weight, |cumulative_weight, (order, (index, s))| {
*cumulative_weight = cumulative_weight
.saturating_add(s.call.get_dispatch_info().weight)
.saturating_add(25_000_000); // Base multiplier
.saturating_add(s.call.get_dispatch_info().weight);

if s.maybe_id.is_some() {
// Remove/Modify Lookup
Expand Down Expand Up @@ -466,6 +466,12 @@ impl<T: Trait> Module<T> {
});
Agenda::<T>::append(when, s);
let index = Agenda::<T>::decode_len(when).unwrap_or(1) as u32 - 1;
if index > T::MaxScheduledPerBlock::get() {
frame_support::debug::warn!(
"Warning: There are more items queued in the Scheduler than \
expected from the runtime configuration. An update might be needed."
);
}
Self::deposit_event(RawEvent::Scheduled(when, index));

Ok((when, index))
Expand Down Expand Up @@ -535,6 +541,12 @@ impl<T: Trait> Module<T> {
};
Agenda::<T>::append(when, Some(s));
let index = Agenda::<T>::decode_len(when).unwrap_or(1) as u32 - 1;
if index > T::MaxScheduledPerBlock::get() {
frame_support::debug::warn!(
"Warning: There are more items queued in the Scheduler than \
expected from the runtime configuration. An update might be needed."
);
}
let address = (when, index);
Lookup::<T>::insert(&id, &address);
Self::deposit_event(RawEvent::Scheduled(when, index));
Expand Down Expand Up @@ -734,6 +746,7 @@ mod tests {
}
parameter_types! {
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
pub const MaxScheduledPerBlock: u32 = 10;
}
ord_parameter_types! {
pub const One: u64 = 1;
Expand All @@ -746,6 +759,7 @@ mod tests {
type Call = Call;
type MaximumWeight = MaximumSchedulerWeight;
type ScheduleOrigin = EnsureOneOf<u64, EnsureRoot<u64>, EnsureSignedBy<One, u64>>;
type MaxScheduledPerBlock = MaxScheduledPerBlock;
type WeightInfo = ();
}
type System = system::Module<Test>;
Expand Down Expand Up @@ -982,8 +996,8 @@ mod tests {
#[test]
fn on_initialize_weight_is_correct() {
new_test_ext().execute_with(|| {
let base_weight: Weight = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 2) + 10_000_000;
let base_multiplier = 25_000_000;
let base_weight: Weight = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 2);
let base_multiplier = 0;
let named_multiplier = <Test as frame_system::Trait>::DbWeight::get().writes(1);
let periodic_multiplier = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 1);

Expand Down

0 comments on commit 98a6a8c

Please sign in to comment.