Skip to content

Commit

Permalink
FRAME Weights with Storage Metadata (paritytech#9471)
Browse files Browse the repository at this point in the history
* weights with metadata

* fix

* fix contract test

* skip metadata tag

* special handling for `frame_system`

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=frame_system --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/system/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* add skip metadata to contracts

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_contracts --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/contracts/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* fix contract test

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_democracy --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/democracy/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_bounties --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/bounties/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* expose component information

* fix test generation

* refactor list benchmarks

* move component selection out of runtime

* add benchmark verification

* missing feature

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_bounties --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/bounties/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_democracy --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/democracy/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* add internal repeats

* update weights with internal repeats

* fix warning

* return error with pov

* try without tracking

* Revert "return error with pov"

This reverts commit 44c36cb.

* Revert "try without tracking"

This reverts commit f401c44.

* Revert "Revert "try without tracking""

This reverts commit 4b4e059.

* state without tracking

* fix build

* temp test

* split db and timing benchmarks

* extend db results?

* default repeat is internal

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_democracy --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/democracy/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* fix warning

* bump linked hash map

* use linked hash map for storage tracker

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_democracy --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/democracy/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* remove conflicting short command

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_democracy --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/democracy/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* missed one linked hashmap

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_bounties --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/bounties/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_bounties --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/bounties/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* cargo run --quiet --release --features=runtime-benchmarks --manifest-path=bin/node/cli/Cargo.toml -- benchmark --chain=dev --steps=50 --repeat=20 --pallet=pallet_election_provider_multi_phase --extrinsic=* --execution=wasm --wasm-execution=compiled --heap-pages=4096 --output=./frame/election-provider-multi-phase/src/weights.rs --template=./.maintain/frame-weight-template.hbs

* new weights with latest changes

* Update frame/benchmarking/src/utils.rs

Co-authored-by: Parity Benchmarking Bot <[email protected]>
  • Loading branch information
shawntabrizi and Parity Benchmarking Bot authored Aug 7, 2021
1 parent 670ca51 commit 8a88403
Show file tree
Hide file tree
Showing 44 changed files with 3,895 additions and 2,121 deletions.
4 changes: 4 additions & 0 deletions .maintain/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ pub trait WeightInfo {

/// Weights for {{pallet}} using the Substrate node and recommended hardware.
pub struct SubstrateWeight<T>(PhantomData<T>);
{{~#if (eq pallet "frame_system")}}
impl<T: crate::Config> WeightInfo for SubstrateWeight<T> {
{{~else}}
impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
{{~/if}}
{{~#each benchmarks as |benchmark|}}
{{~#each benchmark.comments as |comment|}}
// {{comment}}
Expand Down
2 changes: 1 addition & 1 deletion client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ log = "0.4.8"
kvdb = "0.10.0"
kvdb-rocksdb = { version = "0.12.0", optional = true }
kvdb-memorydb = "0.10.0"
linked-hash-map = "0.5.2"
linked-hash-map = "0.5.4"
hash-db = "0.15.2"
parity-util-mem = { version = "0.10.0", default-features = false, features = ["std"] }
codec = { package = "parity-scale-codec", version = "2.0.0", features = ["derive"] }
Expand Down
34 changes: 25 additions & 9 deletions client/db/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
use crate::storage_cache::{new_shared_cache, CachingState, SharedCache};
use hash_db::{Hasher, Prefix};
use kvdb::{DBTransaction, KeyValueDB};
use linked_hash_map::LinkedHashMap;
use sp_core::{
hexdisplay::HexDisplay,
storage::{ChildInfo, TrackedStorageKey},
Expand Down Expand Up @@ -72,7 +73,6 @@ impl<Block: BlockT> sp_state_machine::Storage<HashFor<Block>> for StorageDb<Bloc
}
}
}

/// State that manages the backend database reference. Allows runtime to control the database.
pub struct BenchmarkingState<B: BlockT> {
root: Cell<B::Hash>,
Expand All @@ -85,15 +85,16 @@ pub struct BenchmarkingState<B: BlockT> {
/// Key tracker for keys in the main trie.
/// We track the total number of reads and writes to these keys,
/// not de-duplicated for repeats.
main_key_tracker: RefCell<HashMap<Vec<u8>, TrackedStorageKey>>,
main_key_tracker: RefCell<LinkedHashMap<Vec<u8>, TrackedStorageKey>>,
/// Key tracker for keys in a child trie.
/// Child trie are identified by their storage key (i.e. `ChildInfo::storage_key()`)
/// We track the total number of reads and writes to these keys,
/// not de-duplicated for repeats.
child_key_tracker: RefCell<HashMap<Vec<u8>, HashMap<Vec<u8>, TrackedStorageKey>>>,
child_key_tracker: RefCell<LinkedHashMap<Vec<u8>, LinkedHashMap<Vec<u8>, TrackedStorageKey>>>,
whitelist: RefCell<Vec<TrackedStorageKey>>,
proof_recorder: Option<ProofRecorder<B::Hash>>,
proof_recorder_root: Cell<B::Hash>,
enable_tracking: bool,
}

impl<B: BlockT> BenchmarkingState<B> {
Expand All @@ -102,6 +103,7 @@ impl<B: BlockT> BenchmarkingState<B> {
genesis: Storage,
_cache_size_mb: Option<usize>,
record_proof: bool,
enable_tracking: bool,
) -> Result<Self, String> {
let mut root = B::Hash::default();
let mut mdb = MemoryDB::<HashFor<B>>::default();
Expand All @@ -120,6 +122,7 @@ impl<B: BlockT> BenchmarkingState<B> {
whitelist: Default::default(),
proof_recorder: record_proof.then(Default::default),
proof_recorder_root: Cell::new(root.clone()),
enable_tracking,
};

state.add_whitelist_to_tracker();
Expand Down Expand Up @@ -180,18 +183,24 @@ impl<B: BlockT> BenchmarkingState<B> {
}

fn wipe_tracker(&self) {
*self.main_key_tracker.borrow_mut() = HashMap::new();
*self.child_key_tracker.borrow_mut() = HashMap::new();
*self.main_key_tracker.borrow_mut() = LinkedHashMap::new();
*self.child_key_tracker.borrow_mut() = LinkedHashMap::new();
self.add_whitelist_to_tracker();
}

// Childtrie is identified by its storage key (i.e. `ChildInfo::storage_key`)
fn add_read_key(&self, childtrie: Option<&[u8]>, key: &[u8]) {
if !self.enable_tracking {
return
}

let mut child_key_tracker = self.child_key_tracker.borrow_mut();
let mut main_key_tracker = self.main_key_tracker.borrow_mut();

let key_tracker = if let Some(childtrie) = childtrie {
child_key_tracker.entry(childtrie.to_vec()).or_insert_with(|| HashMap::new())
child_key_tracker
.entry(childtrie.to_vec())
.or_insert_with(|| LinkedHashMap::new())
} else {
&mut main_key_tracker
};
Expand Down Expand Up @@ -224,11 +233,17 @@ impl<B: BlockT> BenchmarkingState<B> {

// Childtrie is identified by its storage key (i.e. `ChildInfo::storage_key`)
fn add_write_key(&self, childtrie: Option<&[u8]>, key: &[u8]) {
if !self.enable_tracking {
return
}

let mut child_key_tracker = self.child_key_tracker.borrow_mut();
let mut main_key_tracker = self.main_key_tracker.borrow_mut();

let key_tracker = if let Some(childtrie) = childtrie {
child_key_tracker.entry(childtrie.to_vec()).or_insert_with(|| HashMap::new())
child_key_tracker
.entry(childtrie.to_vec())
.or_insert_with(|| LinkedHashMap::new())
} else {
&mut main_key_tracker
};
Expand Down Expand Up @@ -553,7 +568,7 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
// We only track at the level of a key-prefix and not whitelisted for now for memory size.
// TODO: Refactor to enable full storage key transparency, where we can remove the
// `prefix_key_tracker`.
let mut prefix_key_tracker = HashMap::<Vec<u8>, (u32, u32, bool)>::new();
let mut prefix_key_tracker = LinkedHashMap::<Vec<u8>, (u32, u32, bool)>::new();
self.all_trackers().iter().for_each(|tracker| {
if !tracker.whitelisted {
let prefix_length = tracker.key.len().min(32);
Expand Down Expand Up @@ -629,7 +644,8 @@ mod test {
#[test]
fn read_to_main_and_child_tries() {
let bench_state =
BenchmarkingState::<crate::tests::Block>::new(Default::default(), None, false).unwrap();
BenchmarkingState::<crate::tests::Block>::new(Default::default(), None, false, true)
.unwrap();

for _ in 0..2 {
let child1 = sp_core::storage::ChildInfo::new_default(b"child1");
Expand Down
2 changes: 1 addition & 1 deletion client/finality-grandpa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../..
sc-block-builder = { version = "0.10.0-dev", path = "../block-builder" }
finality-grandpa = { version = "0.14.1", features = ["derive-codec"] }
pin-project = "1.0.4"
linked-hash-map = "0.5.2"
linked-hash-map = "0.5.4"
async-trait = "0.1.50"
wasm-timer = "0.2"

Expand Down
2 changes: 1 addition & 1 deletion client/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ futures-timer = "3.0.2"
asynchronous-codec = "0.5"
hex = "0.4.0"
ip_network = "0.3.4"
linked-hash-map = "0.5.2"
linked-hash-map = "0.5.4"
linked_hash_set = "0.1.3"
lru = "0.6.5"
log = "0.4.8"
Expand Down
2 changes: 1 addition & 1 deletion client/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ sp-utils = { version = "4.0.0-dev", path = "../../primitives/utils" }
wasm-timer = "0.2"
derive_more = "0.99.2"
serde = { version = "1.0.126", features = ["derive"] }
linked-hash-map = "0.5.2"
linked-hash-map = "0.5.4"
retain_mut = "0.1.3"

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion client/transaction-pool/graph/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sp-core = { version = "4.0.0-dev", path = "../../../primitives/core" }
sp-runtime = { version = "4.0.0-dev", path = "../../../primitives/runtime" }
sp-transaction-pool = { version = "4.0.0-dev", path = "../../../primitives/transaction-pool" }
parity-util-mem = { version = "0.10.0", default-features = false, features = ["primitive-types"] }
linked-hash-map = "0.5.2"
linked-hash-map = "0.5.4"
retain_mut = "0.1.3"

[dev-dependencies]
Expand Down
Loading

0 comments on commit 8a88403

Please sign in to comment.