State metrics possible changes (#5168)

* Registering state from overlay.

* fix

* fix2

* Apply suggestions from code review

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
cheme
2020-04-01 19:46:40 +02:00
committed by GitHub
parent a8aedfa16f
commit 58578af074
17 changed files with 232 additions and 33 deletions
+8
View File
@@ -305,6 +305,14 @@ impl<B: BlockT> StateBackend<HashFor<B>> for BenchmarkingState<B> {
self.reopen()?;
Ok(())
}
fn register_overlay_stats(&mut self, stats: &sp_state_machine::StateMachineStats) {
self.state.borrow_mut().as_mut().map(|s| s.register_overlay_stats(stats));
}
fn usage_info(&self) -> sp_state_machine::UsageInfo {
self.state.borrow().as_ref().map_or(sp_state_machine::UsageInfo::empty(), |s| s.usage_info())
}
}
impl<Block: BlockT> std::fmt::Debug for BenchmarkingState<Block> {
+29 -5
View File
@@ -73,7 +73,7 @@ use sc_executor::RuntimeInfo;
use sp_state_machine::{
DBValue, ChangesTrieTransaction, ChangesTrieCacheAction, UsageInfo as StateUsageInfo,
StorageCollection, ChildStorageCollection,
backend::Backend as StateBackend,
backend::Backend as StateBackend, StateMachineStats,
};
use crate::utils::{DatabaseType, Meta, db_err, meta_keys, read_db, read_meta};
use crate::changes_tries_storage::{DbChangesTrieStorage, DbChangesTrieStorageTransaction};
@@ -256,6 +256,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.
@@ -1116,6 +1124,8 @@ 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;
@@ -1123,14 +1133,26 @@ impl<Block: BlockT> Backend<Block> {
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,
@@ -1498,8 +1520,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,
},
})
}
+3 -1
View File
@@ -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,
}
})
}
+29 -3
View File
@@ -25,6 +25,10 @@ pub struct StateUsageStats {
bytes_read: AtomicU64,
writes: AtomicU64,
bytes_written: AtomicU64,
writes_nodes: AtomicU64,
bytes_written_nodes: AtomicU64,
removed_nodes: AtomicU64,
bytes_removed_nodes: AtomicU64,
reads_cache: AtomicU64,
bytes_read_cache: AtomicU64,
}
@@ -38,6 +42,10 @@ impl StateUsageStats {
bytes_read: 0.into(),
writes: 0.into(),
bytes_written: 0.into(),
writes_nodes: 0.into(),
bytes_written_nodes: 0.into(),
removed_nodes: 0.into(),
bytes_removed_nodes: 0.into(),
reads_cache: 0.into(),
bytes_read_cache: 0.into(),
}
@@ -70,7 +78,19 @@ impl StateUsageStats {
val
}
/// Tally some write operations, including their byte count.
/// Tally some write trie nodes operations, including their byte count.
pub fn tally_writes_nodes(&self, ops: u64, data_bytes: u64) {
self.writes_nodes.fetch_add(ops, AtomicOrdering::Relaxed);
self.bytes_written_nodes.fetch_add(data_bytes, AtomicOrdering::Relaxed);
}
/// Tally some removed trie nodes operations, including their byte count.
pub fn tally_removed_nodes(&self, ops: u64, data_bytes: u64) {
self.removed_nodes.fetch_add(ops, AtomicOrdering::Relaxed);
self.bytes_removed_nodes.fetch_add(data_bytes, AtomicOrdering::Relaxed);
}
/// Tally some write trie nodes operations, including their byte count.
pub fn tally_writes(&self, ops: u64, data_bytes: u64) {
self.writes.fetch_add(ops, AtomicOrdering::Relaxed);
self.bytes_written.fetch_add(data_bytes, AtomicOrdering::Relaxed);
@@ -80,8 +100,10 @@ impl StateUsageStats {
pub fn merge_sm(&self, info: sp_state_machine::UsageInfo) {
self.reads.fetch_add(info.reads.ops, AtomicOrdering::Relaxed);
self.bytes_read.fetch_add(info.reads.bytes, AtomicOrdering::Relaxed);
self.writes.fetch_add(info.writes.ops, AtomicOrdering::Relaxed);
self.bytes_written.fetch_add(info.writes.bytes, AtomicOrdering::Relaxed);
self.writes_nodes.fetch_add(info.nodes_writes.ops, AtomicOrdering::Relaxed);
self.bytes_written_nodes.fetch_add(info.nodes_writes.bytes, AtomicOrdering::Relaxed);
self.removed_nodes.fetch_add(info.removed_nodes.ops, AtomicOrdering::Relaxed);
self.bytes_removed_nodes.fetch_add(info.removed_nodes.bytes, AtomicOrdering::Relaxed);
self.reads_cache.fetch_add(info.cache_reads.ops, AtomicOrdering::Relaxed);
self.bytes_read_cache.fetch_add(info.cache_reads.bytes, AtomicOrdering::Relaxed);
}
@@ -100,7 +122,11 @@ impl StateUsageStats {
sp_state_machine::UsageInfo {
reads: unit(&self.reads, &self.bytes_read),
writes: unit(&self.writes, &self.bytes_written),
nodes_writes: unit(&self.writes_nodes, &self.bytes_written_nodes),
removed_nodes: unit(&self.removed_nodes, &self.bytes_removed_nodes),
cache_reads: unit(&self.reads_cache, &self.bytes_read_cache),
modified_reads: Default::default(),
overlay_writes: Default::default(),
// TODO: Proper tracking state of memory footprint here requires
// imposing `MallocSizeOf` requirement on half of the codebase,
// so it is an open question how to do it better
+14 -1
View File
@@ -299,6 +299,8 @@ pub struct CacheChanges<B: BlockT> {
pub struct CachingState<S, B: BlockT> {
/// Usage statistics
usage: StateUsageStats,
/// State machine registered stats
overlay_stats: sp_state_machine::StateMachineStats,
/// Backing state.
state: S,
/// Cache data.
@@ -428,6 +430,7 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> CachingState<S, B> {
) -> Self {
CachingState {
usage: StateUsageStats::new(),
overlay_stats: sp_state_machine::StateMachineStats::default(),
state,
cache: CacheChanges {
shared_cache,
@@ -663,8 +666,14 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Cachin
self.state.as_trie_backend()
}
fn register_overlay_stats(&mut self, stats: &sp_state_machine::StateMachineStats) {
self.overlay_stats.add(stats);
}
fn usage_info(&self) -> sp_state_machine::UsageInfo {
self.usage.take()
let mut info = self.usage.take();
info.include_state_machine_states(&self.overlay_stats);
info
}
}
@@ -852,6 +861,10 @@ impl<S: StateBackend<HashFor<B>>, B: BlockT> StateBackend<HashFor<B>> for Syncin
.as_trie_backend()
}
fn register_overlay_stats(&mut self, stats: &sp_state_machine::StateMachineStats) {
self.caching_state().register_overlay_stats(stats);
}
fn usage_info(&self) -> sp_state_machine::UsageInfo {
self.caching_state().usage_info()
}