Move WeightCounter to sp-weights (#12603)

* Move WeightCounter to sp_weights

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Rename to WeightMeter and test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Fix pallet-scheduler for new usage

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Update primitives/weights/src/weight_meter.rs

Co-authored-by: David <dvdplm@gmail.com>

* More tests for can_accrue

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Clippy

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Remove defensive_accrue and fixup consumed_ratio

I dont think there is a good use-case for defensive_accrue
without saturation. Only in tests maybe, will remove for now
until we have a use-case.

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

* Test

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>

Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
Co-authored-by: David <dvdplm@gmail.com>
Co-authored-by: Gavin Wood <gavin@parity.io>
This commit is contained in:
Oliver Tale-Yazdi
2022-11-11 12:26:47 +01:00
committed by GitHub
parent e04b0c4929
commit bd2166de79
4 changed files with 195 additions and 39 deletions
+8 -12
View File
@@ -122,17 +122,13 @@ fn make_origin<T: Config>(signed: bool) -> <T as Config>::PalletsOrigin {
}
}
fn dummy_counter() -> WeightCounter {
WeightCounter { used: Weight::zero(), limit: Weight::MAX }
}
benchmarks! {
// `service_agendas` when no work is done.
service_agendas_base {
let now = T::BlockNumber::from(BLOCK_NUMBER);
IncompleteSince::<T>::put(now - One::one());
}: {
Scheduler::<T>::service_agendas(&mut dummy_counter(), now, 0);
Scheduler::<T>::service_agendas(&mut WeightMeter::max_limit(), now, 0);
} verify {
assert_eq!(IncompleteSince::<T>::get(), Some(now - One::one()));
}
@@ -144,7 +140,7 @@ benchmarks! {
fill_schedule::<T>(now, s)?;
let mut executed = 0;
}: {
Scheduler::<T>::service_agenda(&mut dummy_counter(), &mut executed, now, now, 0);
Scheduler::<T>::service_agenda(&mut WeightMeter::max_limit(), &mut executed, now, now, 0);
} verify {
assert_eq!(executed, 0);
}
@@ -155,7 +151,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -169,7 +165,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, false, false, Some(s), 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -181,7 +177,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(false, true, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -193,7 +189,7 @@ benchmarks! {
let now = BLOCK_NUMBER.into();
let task = make_task::<T>(true, false, false, None, 0);
// prevent any tasks from actually being executed as we only want the surrounding weight.
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::zero() };
let mut counter = WeightMeter::from_limit(Weight::zero());
}: {
let result = Scheduler::<T>::service_task(&mut counter, now, now, 0, true, task);
} verify {
@@ -201,7 +197,7 @@ benchmarks! {
// `execute_dispatch` when the origin is `Signed`, not counting the dispatable's weight.
execute_dispatch_signed {
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::MAX };
let mut counter = WeightMeter::max_limit();
let origin = make_origin::<T>(true);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {
@@ -212,7 +208,7 @@ benchmarks! {
// `execute_dispatch` when the origin is not `Signed`, not counting the dispatable's weight.
execute_dispatch_unsigned {
let mut counter = WeightCounter { used: Weight::zero(), limit: Weight::MAX };
let mut counter = WeightMeter::max_limit();
let origin = make_origin::<T>(false);
let call = T::Preimages::realize(&make_call::<T>(None)).unwrap().0;
}: {
+7 -27
View File
@@ -70,7 +70,7 @@ use frame_support::{
Bounded, CallerTrait, EnsureOrigin, Get, Hash as PreimageHash, IsType, OriginTrait,
PalletInfoAccess, PrivilegeCmp, QueryPreimage, StorageVersion, StorePreimage,
},
weights::Weight,
weights::{Weight, WeightMeter},
};
use frame_system::{self as system};
pub use pallet::*;
@@ -143,25 +143,6 @@ pub type ScheduledOf<T> = Scheduled<
<T as frame_system::Config>::AccountId,
>;
struct WeightCounter {
used: Weight,
limit: Weight,
}
impl WeightCounter {
fn check_accrue(&mut self, w: Weight) -> bool {
let test = self.used.saturating_add(w);
if test.any_gt(self.limit) {
false
} else {
self.used = test;
true
}
}
fn can_accrue(&mut self, w: Weight) -> bool {
self.used.saturating_add(w).all_lte(self.limit)
}
}
pub(crate) trait MarginalWeightInfo: WeightInfo {
fn service_task(maybe_lookup_len: Option<usize>, named: bool, periodic: bool) -> Weight {
let base = Self::service_task_base();
@@ -306,10 +287,9 @@ pub mod pallet {
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
/// Execute the scheduled calls
fn on_initialize(now: T::BlockNumber) -> Weight {
let mut weight_counter =
WeightCounter { used: Weight::zero(), limit: T::MaximumWeight::get() };
let mut weight_counter = WeightMeter::from_limit(T::MaximumWeight::get());
Self::service_agendas(&mut weight_counter, now, u32::max_value());
weight_counter.used
weight_counter.consumed
}
}
@@ -933,7 +913,7 @@ use ServiceTaskError::*;
impl<T: Config> Pallet<T> {
/// Service up to `max` agendas queue starting from earliest incompletely executed agenda.
fn service_agendas(weight: &mut WeightCounter, now: T::BlockNumber, max: u32) {
fn service_agendas(weight: &mut WeightMeter, now: T::BlockNumber, max: u32) {
if !weight.check_accrue(T::WeightInfo::service_agendas_base()) {
return
}
@@ -961,7 +941,7 @@ impl<T: Config> Pallet<T> {
/// Returns `true` if the agenda was fully completed, `false` if it should be revisited at a
/// later block.
fn service_agenda(
weight: &mut WeightCounter,
weight: &mut WeightMeter,
executed: &mut u32,
now: T::BlockNumber,
when: T::BlockNumber,
@@ -1030,7 +1010,7 @@ impl<T: Config> Pallet<T> {
/// - realizing the task's call which can include a preimage lookup.
/// - Rescheduling the task for execution in a later agenda if periodic.
fn service_task(
weight: &mut WeightCounter,
weight: &mut WeightMeter,
now: T::BlockNumber,
when: T::BlockNumber,
agenda_index: u32,
@@ -1110,7 +1090,7 @@ impl<T: Config> Pallet<T> {
/// NOTE: Only the weight for this function will be counted (origin lookup, dispatch and the
/// call itself).
fn execute_dispatch(
weight: &mut WeightCounter,
weight: &mut WeightMeter,
origin: T::PalletsOrigin,
call: <T as Config>::RuntimeCall,
) -> Result<DispatchResult, ServiceTaskError> {