Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
18b0170
Try adding benchmark to runtime and exposit runtime-benchmarks
Mar 5, 2020
e2286bc
Registering state from overlay.
cheme Mar 6, 2020
660ed3d
fix
cheme Mar 6, 2020
89520b9
fix2
cheme Mar 6, 2020
1dd381f
Merge branch 'master' of https://github.com/paritytech/substrate into…
Mar 6, 2020
07da93a
Merge branch 'state-metrics' of https://github.com/cheme/substrate in…
Mar 6, 2020
ae340b5
WIP: add usage info stub functions; add InstantWithDefault
Mar 9, 2020
fc784f3
Add new method to InstantWithDefault
Mar 9, 2020
5873f57
Add sp-state-machine as optional in frame/benchmarking
Mar 9, 2020
2199777
Rename methods
Mar 9, 2020
5100dc3
add sp_stats crate
Mar 10, 2020
711a161
Update comments and typo
Mar 10, 2020
9490cb7
Add default-features = false for runtime-interface
Mar 10, 2020
fe22050
Replace instant with Duration; Make code compilable
Mar 10, 2020
71d2a7c
Add corret fn signatures
Mar 10, 2020
a6dd88d
Impl PassBy in runtime-interface
Mar 10, 2020
169da7a
Remove empty return of db_usage_info
Mar 10, 2020
850062b
Remove unimplemented code
Mar 10, 2020
003325b
call usage_info in benchmarking-cli
Mar 11, 2020
ae5c499
Add RefCell<UsageInfo> to BenchmarkingState
Mar 12, 2020
04371db
Merge branch 'scott_add_usage_info' of github.com:pscott/substrate in…
Mar 12, 2020
a1516be
Revert "call usage_info in benchmarking-cli"
Mar 12, 2020
a60a5da
Update try_borrow_mut's error logic
Mar 12, 2020
43b81b6
Add mut keyword to usage_info
Mar 12, 2020
bd96591
Add anti-pattern let _ to silence clippy's warning
Mar 12, 2020
c9db643
Clean code and update comments
Mar 12, 2020
987027b
Read bytes as u64 instead of usize
Mar 13, 2020
c412117
Add bytes as u64 instead of usize
Mar 13, 2020
0c4c653
Update unimplemented code
Mar 13, 2020
2a358ac
Revert "Merge branch 'state-metrics' of https://github.com/cheme/subs…
Mar 13, 2020
d4294c6
Merge branch 'master' of github.com:paritytech/substrate into scott_a…
Mar 18, 2020
eb76aed
Bump sp-stats version
Mar 18, 2020
c527103
Remove try_borrow()
Mar 19, 2020
c00369b
Update memory to field to be u64 instead of u32
Mar 19, 2020
ac8c8dd
Explain sp-stats' purpose
Mar 19, 2020
8f749f8
Remove unimplemented wipe
Mar 19, 2020
3407c07
Merge branch 'master' of github.com:paritytech/substrate into scott_a…
Mar 28, 2020
ebc26e1
Correctly track state read / writes; Remove cache_hits
Mar 29, 2020
774527e
Update Cargo.lock
shawntabrizi Mar 29, 2020
1727792
Merge branch 'master' into scott_add_usage_info
pscott Mar 31, 2020
3d65985
Review merge
Mar 31, 2020
b8197bf
Unexpose benchmarking pallet
Mar 31, 2020
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
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ members = [
"primitives/std",
"primitives/version",
"primitives/state-machine",
"primitives/stats",
"primitives/timestamp",
"primitives/test-primitives",
"primitives/transaction-pool",
Expand Down
2 changes: 2 additions & 0 deletions bin/node/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ pallet-authority-discovery = { version = "2.0.0-alpha.2", default-features = fal
pallet-authorship = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/authorship" }
pallet-babe = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/babe" }
pallet-balances = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/balances" }
pallet-benchmark = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/benchmark" }
pallet-collective = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/collective" }
pallet-contracts = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/contracts" }
pallet-contracts-primitives = { version = "2.0.0-alpha.2", default-features = false, path = "../../../frame/contracts/common/" }
Expand Down Expand Up @@ -96,6 +97,7 @@ std = [
"frame-executive/std",
"pallet-finality-tracker/std",
"pallet-grandpa/std",
"pallet-benchmark/std",
"pallet-im-online/std",
"pallet-indices/std",
"sp-inherents/std",
Expand Down
6 changes: 6 additions & 0 deletions bin/node/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,11 @@ impl pallet_vesting::Trait for Runtime {
type MinVestedTransfer = MinVestedTransfer;
}

impl pallet_benchmark::Trait for Runtime {
type Event = Event;
type Currency = Balances;
}

construct_runtime!(
pub enum Runtime where
Block = Block,
Expand Down Expand Up @@ -646,6 +651,7 @@ construct_runtime!(
Society: pallet_society::{Module, Call, Storage, Event<T>, Config<T>},
Recovery: pallet_recovery::{Module, Call, Storage, Event<T>},
Vesting: pallet_vesting::{Module, Call, Storage, Event<T>, Config<T>},
Benchmark: pallet_benchmark::{Module, Call, Storage, Event<T>},
}
);

Expand Down
1 change: 1 addition & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ sp-blockchain = { version = "2.0.0-alpha.2", path = "../primitives/blockchain" }
sp-state-machine = { version = "0.8.0-alpha.2", path = "../primitives/state-machine" }
sc-telemetry = { version = "2.0.0-alpha.2", path = "telemetry" }
sp-trie = { version = "2.0.0-alpha.2", path = "../primitives/trie" }
sp-stats = { version = "2.0.0-alpha.2", path = "../primitives/stats" }
prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.8.0-alpha.2", path = "../utils/prometheus" }
tracing = "0.1.10"

Expand Down
10 changes: 7 additions & 3 deletions client/api/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,12 @@ pub struct IoInfo {
pub state_reads: u64,
/// State reads (keys) from cache.
pub state_reads_cache: u64,
/// State reads (keys) from cache.
/// State reads (keys)
pub state_writes: u64,
/// State write (keys) already cached.
pub state_writes_cache: u64,
/// State write (trie nodes) to backend db.
pub state_writes_nodes: u64,
}

/// Usage statistics for running client instance.
Expand All @@ -195,7 +199,7 @@ impl fmt::Display for UsageInfo {
f,
"caches: ({} state, {} db overlay), \
state db: ({} non-canonical, {} pruning, {} pinned), \
i/o: ({} tx, {} write, {} read, {} avg tx, {}/{} key cache reads/total, {} key writes)",
i/o: ({} tx, {} write, {} read, {} avg tx, {}/{} key cache reads/total, {} trie nodes writes)",
self.memory.state_cache,
self.memory.database_cache,
self.memory.state_db.non_canonical,
Expand All @@ -207,7 +211,7 @@ impl fmt::Display for UsageInfo {
self.io.average_transaction_size,
self.io.state_reads_cache,
self.io.state_reads,
self.io.state_writes,
self.io.state_writes_nodes,
)
}
}
Expand Down
1 change: 1 addition & 0 deletions client/db/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ sp-core = { version = "2.0.0-alpha.2", path = "../../primitives/core" }
sp-runtime = { version = "2.0.0-alpha.2", path = "../../primitives/runtime" }
sc-client = { version = "0.8.0-alpha.2", path = "../" }
sp-state-machine = { version = "0.8.0-alpha.2", path = "../../primitives/state-machine" }
sp-stats= { version = "2.0.0-alpha.2", path = "../../primitives/stats" }
sc-executor = { version = "0.8.0-alpha.2", path = "../executor" }
sc-state-db = { version = "0.8.0-alpha.2", path = "../state-db" }
sp-trie = { version = "2.0.0-alpha.2", path = "../../primitives/trie" }
Expand Down
25 changes: 24 additions & 1 deletion client/db/src/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ use sp_runtime::Storage;
use sp_state_machine::{DBValue, backend::Backend as StateBackend};
use kvdb::{KeyValueDB, DBTransaction};
use kvdb_rocksdb::{Database, DatabaseConfig};
use sp_stats::UsageInfo;

type DbState<B> = sp_state_machine::TrieBackend<
Arc<dyn sp_state_machine::Storage<HashFor<B>>>, HashFor<B>
Expand All @@ -54,6 +55,7 @@ pub struct BenchmarkingState<B: BlockT> {
state: RefCell<Option<DbState<B>>>,
db: Cell<Option<Arc<dyn KeyValueDB>>>,
genesis: <DbState<B> as StateBackend<HashFor<B>>>::Transaction,
usage_info: RefCell<UsageInfo>,
}

impl<B: BlockT> BenchmarkingState<B> {
Expand All @@ -74,6 +76,7 @@ impl<B: BlockT> BenchmarkingState<B> {
path,
root: Cell::new(root),
genesis: Default::default(),
usage_info: RefCell::new(UsageInfo::empty()),
};

state.reopen()?;
Expand Down Expand Up @@ -132,7 +135,13 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
type TrieBackendStorage = <DbState<B> as StateBackend<HashFor<B>>>::TrieBackendStorage;

fn storage(&self, key: &[u8]) -> Result<Option<Vec<u8>>, Self::Error> {
self.state.borrow().as_ref().ok_or_else(state_err)?.storage(key)
let state = self.state.borrow_mut();
let db_state = state.as_ref().ok_or_else(state_err)?;
let _ = self.usage_info.try_borrow_mut().map(|mut usage_info| {
Comment thread
pscott marked this conversation as resolved.
Outdated
usage_info.reads.ops += 1;
usage_info.reads.bytes += key.len() as u64;
Comment thread
pscott marked this conversation as resolved.
Outdated
});
db_state.storage(key)
}

fn storage_hash(&self, key: &[u8]) -> Result<Option<B::Hash>, Self::Error> {
Expand Down Expand Up @@ -254,15 +263,21 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
{
if let Some(db) = self.db.take() {
let mut db_transaction = DBTransaction::new();
let mut bytes = 0;

for (key, (val, rc)) in transaction.drain() {
if rc > 0 {
db_transaction.put(0, &key, &val);
} else if rc < 0 {
db_transaction.delete(0, &key);
}
bytes += key.len() as u64;
}
db.write(db_transaction).map_err(|_| String::from("Error committing transaction"))?;
let _ = self.usage_info.try_borrow_mut().map(|mut usage_info| {
Comment thread
pscott marked this conversation as resolved.
Outdated
usage_info.writes.ops += 1;
usage_info.writes.bytes += bytes;
Comment thread
pscott marked this conversation as resolved.
Outdated
});
self.root.set(storage_root);
} else {
return Err("Trying to commit to a closed db".into())
Expand All @@ -276,6 +291,14 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
self.commit(self.root.get(), self.genesis.clone())?;
Ok(())
}

fn register_overlay_stats(&mut self, stats: &sp_stats::StateMachineStats) {
self.state.borrow_mut().as_mut().map(|s| s.register_overlay_stats(stats));
}

fn usage_info(&self) -> UsageInfo {
self.usage_info.try_borrow().map_or(UsageInfo::empty(), |info| info.clone())
}
}

impl<Block: BlockT> std::fmt::Debug for BenchmarkingState<Block> {
Expand Down
36 changes: 31 additions & 5 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ use sp_runtime::traits::{
};
use sc_executor::RuntimeInfo;
use sp_state_machine::{
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, UsageInfo as StateUsageInfo,
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction,
StorageCollection, ChildStorageCollection,
backend::Backend as StateBackend,
};
use sp_stats::{StateMachineStats, UsageInfo as StateUsageInfo};
use crate::utils::{DatabaseType, Meta, db_err, meta_keys, read_db, read_meta};
use crate::changes_tries_storage::{DbChangesTrieStorage, DbChangesTrieStorageTransaction};
use sc_client::leaves::{LeafSet, FinalizationDisplaced};
Expand Down Expand Up @@ -256,6 +257,14 @@ impl<B: BlockT> StateBackend<HashFor<B>> for RefTrackingState<B> {
{
self.state.as_trie_backend()
}

fn register_overlay_stats(&mut self, stats: &StateMachineStats) {
self.state.register_overlay_stats(stats);
}

fn usage_info(&self) -> StateUsageInfo {
self.state.usage_info()
}
}

/// Database settings.
Expand Down Expand Up @@ -1116,21 +1125,35 @@ impl<Block: BlockT> Backend<Block> {
let mut changeset: sc_state_db::ChangeSet<Vec<u8>> = sc_state_db::ChangeSet::default();
let mut ops: u64 = 0;
let mut bytes: u64 = 0;
let mut removal: u64 = 0;
let mut bytes_removal: u64 = 0;
for (key, (val, rc)) in operation.db_updates.drain() {
if rc > 0 {
ops += 1;
bytes += key.len() as u64 + val.len() as u64;

changeset.inserted.push((key, val.to_vec()));
} else if rc < 0 {
ops += 1;
bytes += key.len() as u64;
removal += 1;
bytes_removal += key.len() as u64;

changeset.deleted.push(key);
}
}
self.state_usage.tally_writes(ops, bytes);
self.state_usage.tally_writes_nodes(ops, bytes);
self.state_usage.tally_removed_nodes(removal, bytes_removal);

let mut ops: u64 = 0;
let mut bytes: u64 = 0;
for (key, value) in operation.storage_updates.iter()
.chain(operation.child_storage_updates.iter().flat_map(|(_, s)| s.iter())) {
ops += 1;
bytes += key.len() as u64;
if let Some(v) = value.as_ref() {
bytes += v.len() as u64;
}
}
self.state_usage.tally_writes(ops, bytes);
let number_u64 = number.saturated_into::<u64>();
let commit = self.storage.state_db.insert_block(
&hash,
Expand Down Expand Up @@ -1498,8 +1521,10 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
reads: io_stats.reads,
average_transaction_size: io_stats.avg_transaction_size() as u64,
state_reads: state_stats.reads.ops,
state_reads_cache: state_stats.cache_reads.ops,
state_writes: state_stats.writes.ops,
state_writes_cache: state_stats.overlay_writes.ops,
state_reads_cache: state_stats.cache_reads.ops,
state_writes_nodes: state_stats.nodes_writes.ops,
},
})
}
Expand Down Expand Up @@ -1676,6 +1701,7 @@ impl<Block: BlockT> sc_client_api::backend::Backend<Block> for Backend<Block> {
fn get_import_lock(&self) -> &RwLock<()> {
&*self.import_lock
}

}

impl<Block: BlockT> sc_client_api::backend::LocalBackend<Block> for Backend<Block> {}
Expand Down
4 changes: 3 additions & 1 deletion client/db/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,8 +592,10 @@ impl<Block> LightBlockchainStorage<Block> for LightStorage<Block>
average_transaction_size: io_stats.avg_transaction_size() as u64,
// Light client does not track those
state_reads: 0,
state_reads_cache: 0,
state_writes: 0,
state_reads_cache: 0,
state_writes_cache: 0,
state_writes_nodes: 0,
}
})
}
Expand Down
Loading