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

Commit 98a6a8c

Browse files
shawntabrizibkchr
authored andcommitted
WeightInfo for Scheduler (#7138)
* initial scheduler stuff * integrate weightinfo * Update pallet_scheduler.rs
1 parent 7cc397f commit 98a6a8c

File tree

7 files changed

+148
-28
lines changed

7 files changed

+148
-28
lines changed

bin/node/runtime/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ impl pallet_proxy::Trait for Runtime {
273273

274274
parameter_types! {
275275
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
276+
pub const MaxScheduledPerBlock: u32 = 50;
276277
}
277278

278279
impl pallet_scheduler::Trait for Runtime {
@@ -282,7 +283,8 @@ impl pallet_scheduler::Trait for Runtime {
282283
type Call = Call;
283284
type MaximumWeight = MaximumSchedulerWeight;
284285
type ScheduleOrigin = EnsureRoot<AccountId>;
285-
type WeightInfo = ();
286+
type MaxScheduledPerBlock = MaxScheduledPerBlock;
287+
type WeightInfo = weights::pallet_scheduler::WeightInfo;
286288
}
287289

288290
parameter_types! {

bin/node/runtime/src/weights/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub mod pallet_identity;
2424
pub mod pallet_indices;
2525
pub mod pallet_im_online;
2626
pub mod pallet_proxy;
27+
pub mod pallet_scheduler;
2728
pub mod pallet_staking;
2829
pub mod pallet_timestamp;
2930
pub mod pallet_utility;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6
19+
20+
#![allow(unused_parens)]
21+
#![allow(unused_imports)]
22+
23+
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
24+
25+
pub struct WeightInfo;
26+
impl pallet_scheduler::WeightInfo for WeightInfo {
27+
fn schedule(s: u32, ) -> Weight {
28+
(37_835_000 as Weight)
29+
.saturating_add((81_000 as Weight).saturating_mul(s as Weight))
30+
.saturating_add(DbWeight::get().reads(1 as Weight))
31+
.saturating_add(DbWeight::get().writes(1 as Weight))
32+
}
33+
fn cancel(s: u32, ) -> Weight {
34+
(34_707_000 as Weight)
35+
.saturating_add((3_125_000 as Weight).saturating_mul(s as Weight))
36+
.saturating_add(DbWeight::get().reads(1 as Weight))
37+
.saturating_add(DbWeight::get().writes(2 as Weight))
38+
}
39+
fn schedule_named(s: u32, ) -> Weight {
40+
(48_065_000 as Weight)
41+
.saturating_add((110_000 as Weight).saturating_mul(s as Weight))
42+
.saturating_add(DbWeight::get().reads(2 as Weight))
43+
.saturating_add(DbWeight::get().writes(2 as Weight))
44+
}
45+
fn cancel_named(s: u32, ) -> Weight {
46+
(38_776_000 as Weight)
47+
.saturating_add((3_138_000 as Weight).saturating_mul(s as Weight))
48+
.saturating_add(DbWeight::get().reads(2 as Weight))
49+
.saturating_add(DbWeight::get().writes(2 as Weight))
50+
}
51+
}

frame/democracy/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl pallet_scheduler::Trait for Test {
128128
type Call = Call;
129129
type MaximumWeight = MaximumSchedulerWeight;
130130
type ScheduleOrigin = EnsureRoot<u64>;
131+
type MaxScheduledPerBlock = ();
131132
type WeightInfo = ();
132133
}
133134
parameter_types! {

frame/scheduler/src/benchmarking.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use frame_benchmarking::benchmarks;
2828
use crate::Module as Scheduler;
2929
use frame_system::Module as System;
3030

31-
const MAX_SCHEDULED: u32 = 50;
3231
const BLOCK_NUMBER: u32 = 2;
3332

3433
// Add `n` named items to the schedule
@@ -56,7 +55,7 @@ benchmarks! {
5655
_ { }
5756

5857
schedule {
59-
let s in 0 .. MAX_SCHEDULED;
58+
let s in 0 .. T::MaxScheduledPerBlock::get();
6059
let when = BLOCK_NUMBER.into();
6160
let periodic = Some((T::BlockNumber::one(), 100));
6261
let priority = 0;
@@ -73,7 +72,7 @@ benchmarks! {
7372
}
7473

7574
cancel {
76-
let s in 1 .. MAX_SCHEDULED;
75+
let s in 1 .. T::MaxScheduledPerBlock::get();
7776
let when = BLOCK_NUMBER.into();
7877

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

9493
schedule_named {
95-
let s in 0 .. MAX_SCHEDULED;
94+
let s in 0 .. T::MaxScheduledPerBlock::get();
9695
let id = s.encode();
9796
let when = BLOCK_NUMBER.into();
9897
let periodic = Some((T::BlockNumber::one(), 100));
@@ -110,7 +109,7 @@ benchmarks! {
110109
}
111110

112111
cancel_named {
113-
let s in 1 .. MAX_SCHEDULED;
112+
let s in 1 .. T::MaxScheduledPerBlock::get();
114113
let when = BLOCK_NUMBER.into();
115114

116115
fill_schedule::<T>(when, s)?;
@@ -127,8 +126,10 @@ benchmarks! {
127126
);
128127
}
129128

129+
// TODO: Make this more complex and flexible so it can be used in automation.
130+
#[extra]
130131
on_initialize {
131-
let s in 0 .. MAX_SCHEDULED;
132+
let s in 0 .. T::MaxScheduledPerBlock::get();
132133
let when = BLOCK_NUMBER.into();
133134
fill_schedule::<T>(when, s)?;
134135
}: { Scheduler::<T>::on_initialize(BLOCK_NUMBER.into()); }
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// This file is part of Substrate.
2+
3+
// Copyright (C) 2020 Parity Technologies (UK) Ltd.
4+
// SPDX-License-Identifier: Apache-2.0
5+
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
//! THIS FILE WAS AUTO-GENERATED USING THE SUBSTRATE BENCHMARK CLI VERSION 2.0.0-rc6
19+
20+
#![allow(unused_parens)]
21+
#![allow(unused_imports)]
22+
23+
use frame_support::weights::{Weight, constants::RocksDbWeight as DbWeight};
24+
25+
impl crate::WeightInfo for () {
26+
fn schedule(s: u32, ) -> Weight {
27+
(37_835_000 as Weight)
28+
.saturating_add((81_000 as Weight).saturating_mul(s as Weight))
29+
.saturating_add(DbWeight::get().reads(1 as Weight))
30+
.saturating_add(DbWeight::get().writes(1 as Weight))
31+
}
32+
fn cancel(s: u32, ) -> Weight {
33+
(34_707_000 as Weight)
34+
.saturating_add((3_125_000 as Weight).saturating_mul(s as Weight))
35+
.saturating_add(DbWeight::get().reads(1 as Weight))
36+
.saturating_add(DbWeight::get().writes(2 as Weight))
37+
}
38+
fn schedule_named(s: u32, ) -> Weight {
39+
(48_065_000 as Weight)
40+
.saturating_add((110_000 as Weight).saturating_mul(s as Weight))
41+
.saturating_add(DbWeight::get().reads(2 as Weight))
42+
.saturating_add(DbWeight::get().writes(2 as Weight))
43+
}
44+
fn cancel_named(s: u32, ) -> Weight {
45+
(38_776_000 as Weight)
46+
.saturating_add((3_138_000 as Weight).saturating_mul(s as Weight))
47+
.saturating_add(DbWeight::get().reads(2 as Weight))
48+
.saturating_add(DbWeight::get().writes(2 as Weight))
49+
}
50+
}

frame/scheduler/src/lib.rs

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#![cfg_attr(not(feature = "std"), no_std)]
5353

5454
mod benchmarking;
55+
mod default_weights;
5556

5657
use sp_std::{prelude::*, marker::PhantomData, borrow::Borrow};
5758
use codec::{Encode, Decode, Codec};
@@ -69,15 +70,6 @@ pub trait WeightInfo {
6970
fn cancel(s: u32, ) -> Weight;
7071
fn schedule_named(s: u32, ) -> Weight;
7172
fn cancel_named(s: u32, ) -> Weight;
72-
fn on_initialize(s: u32, ) -> Weight;
73-
}
74-
75-
impl WeightInfo for () {
76-
fn schedule(_s: u32, ) -> Weight { 1_000_000_000 }
77-
fn cancel(_s: u32, ) -> Weight { 1_000_000_000 }
78-
fn schedule_named(_s: u32, ) -> Weight { 1_000_000_000 }
79-
fn cancel_named(_s: u32, ) -> Weight { 1_000_000_000 }
80-
fn on_initialize(_s: u32, ) -> Weight { 1_000_000_000 }
8173
}
8274

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

101+
/// The maximum number of scheduled calls in the queue for a single block.
102+
/// Not strictly enforced, but used for weight estimation.
103+
type MaxScheduledPerBlock: Get<u32>;
104+
109105
/// Weight information for extrinsics in this pallet.
110106
type WeightInfo: WeightInfo;
111107
}
@@ -213,7 +209,7 @@ decl_module! {
213209
/// - Write: Agenda
214210
/// - Will use base weight of 25 which should be good for up to 30 scheduled calls
215211
/// # </weight>
216-
#[weight = 25_000_000 + T::DbWeight::get().reads_writes(1, 1)]
212+
#[weight = T::WeightInfo::schedule(T::MaxScheduledPerBlock::get())]
217213
fn schedule(origin,
218214
when: T::BlockNumber,
219215
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
@@ -235,7 +231,7 @@ decl_module! {
235231
/// - Write: Agenda, Lookup
236232
/// - Will use base weight of 100 which should be good for up to 30 scheduled calls
237233
/// # </weight>
238-
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(1, 2)]
234+
#[weight = T::WeightInfo::cancel(T::MaxScheduledPerBlock::get())]
239235
fn cancel(origin, when: T::BlockNumber, index: u32) {
240236
T::ScheduleOrigin::ensure_origin(origin.clone())?;
241237
let origin = <T as Trait>::Origin::from(origin);
@@ -252,7 +248,7 @@ decl_module! {
252248
/// - Write: Agenda, Lookup
253249
/// - Will use base weight of 35 which should be good for more than 30 scheduled calls
254250
/// # </weight>
255-
#[weight = 35_000_000 + T::DbWeight::get().reads_writes(2, 2)]
251+
#[weight = T::WeightInfo::schedule_named(T::MaxScheduledPerBlock::get())]
256252
fn schedule_named(origin,
257253
id: Vec<u8>,
258254
when: T::BlockNumber,
@@ -277,7 +273,7 @@ decl_module! {
277273
/// - Write: Agenda, Lookup
278274
/// - Will use base weight of 100 which should be good for up to 30 scheduled calls
279275
/// # </weight>
280-
#[weight = 100_000_000 + T::DbWeight::get().reads_writes(2, 2)]
276+
#[weight = T::WeightInfo::cancel_named(T::MaxScheduledPerBlock::get())]
281277
fn cancel_named(origin, id: Vec<u8>) {
282278
T::ScheduleOrigin::ensure_origin(origin.clone())?;
283279
let origin = <T as Trait>::Origin::from(origin);
@@ -289,7 +285,7 @@ decl_module! {
289285
/// # <weight>
290286
/// Same as [`schedule`].
291287
/// # </weight>
292-
#[weight = 25_000_000 + T::DbWeight::get().reads_writes(1, 1)]
288+
#[weight = T::WeightInfo::schedule(T::MaxScheduledPerBlock::get())]
293289
fn schedule_after(origin,
294290
after: T::BlockNumber,
295291
maybe_periodic: Option<schedule::Period<T::BlockNumber>>,
@@ -308,7 +304,7 @@ decl_module! {
308304
/// # <weight>
309305
/// Same as [`schedule_named`].
310306
/// # </weight>
311-
#[weight = 35_000_000 + T::DbWeight::get().reads_writes(2, 2)]
307+
#[weight = T::WeightInfo::schedule_named(T::MaxScheduledPerBlock::get())]
312308
fn schedule_named_after(origin,
313309
id: Vec<u8>,
314310
after: T::BlockNumber,
@@ -340,16 +336,20 @@ decl_module! {
340336
.enumerate()
341337
.filter_map(|(index, s)| s.map(|inner| (index as u32, inner)))
342338
.collect::<Vec<_>>();
339+
if queued.len() as u32 > T::MaxScheduledPerBlock::get() {
340+
frame_support::debug::warn!(
341+
"Warning: This block has more items queued in Scheduler than \
342+
expected from the runtime configuration. An update might be needed."
343+
);
344+
}
343345
queued.sort_by_key(|(_, s)| s.priority);
344-
let base_weight: Weight = T::DbWeight::get().reads_writes(1, 2) // Agenda + Agenda(next)
345-
.saturating_add(10_000_000); // Base Weight
346+
let base_weight: Weight = T::DbWeight::get().reads_writes(1, 2); // Agenda + Agenda(next)
346347
let mut total_weight: Weight = 0;
347348
queued.into_iter()
348349
.enumerate()
349350
.scan(base_weight, |cumulative_weight, (order, (index, s))| {
350351
*cumulative_weight = cumulative_weight
351-
.saturating_add(s.call.get_dispatch_info().weight)
352-
.saturating_add(25_000_000); // Base multiplier
352+
.saturating_add(s.call.get_dispatch_info().weight);
353353

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

471477
Ok((when, index))
@@ -535,6 +541,12 @@ impl<T: Trait> Module<T> {
535541
};
536542
Agenda::<T>::append(when, Some(s));
537543
let index = Agenda::<T>::decode_len(when).unwrap_or(1) as u32 - 1;
544+
if index > T::MaxScheduledPerBlock::get() {
545+
frame_support::debug::warn!(
546+
"Warning: There are more items queued in the Scheduler than \
547+
expected from the runtime configuration. An update might be needed."
548+
);
549+
}
538550
let address = (when, index);
539551
Lookup::<T>::insert(&id, &address);
540552
Self::deposit_event(RawEvent::Scheduled(when, index));
@@ -734,6 +746,7 @@ mod tests {
734746
}
735747
parameter_types! {
736748
pub MaximumSchedulerWeight: Weight = Perbill::from_percent(80) * MaximumBlockWeight::get();
749+
pub const MaxScheduledPerBlock: u32 = 10;
737750
}
738751
ord_parameter_types! {
739752
pub const One: u64 = 1;
@@ -746,6 +759,7 @@ mod tests {
746759
type Call = Call;
747760
type MaximumWeight = MaximumSchedulerWeight;
748761
type ScheduleOrigin = EnsureOneOf<u64, EnsureRoot<u64>, EnsureSignedBy<One, u64>>;
762+
type MaxScheduledPerBlock = MaxScheduledPerBlock;
749763
type WeightInfo = ();
750764
}
751765
type System = system::Module<Test>;
@@ -982,8 +996,8 @@ mod tests {
982996
#[test]
983997
fn on_initialize_weight_is_correct() {
984998
new_test_ext().execute_with(|| {
985-
let base_weight: Weight = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 2) + 10_000_000;
986-
let base_multiplier = 25_000_000;
999+
let base_weight: Weight = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 2);
1000+
let base_multiplier = 0;
9871001
let named_multiplier = <Test as frame_system::Trait>::DbWeight::get().writes(1);
9881002
let periodic_multiplier = <Test as frame_system::Trait>::DbWeight::get().reads_writes(1, 1);
9891003

0 commit comments

Comments
 (0)