From 4998dbb365d2a7e3bfedf23792854f340c438693 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Sun, 3 Mar 2024 17:31:34 +0100 Subject: [PATCH 1/8] feat(trie): metrics --- Cargo.lock | 2 ++ crates/trie/Cargo.toml | 4 +++ crates/trie/src/lib.rs | 2 ++ crates/trie/src/metrics.rs | 69 ++++++++++++++++++++++++++++++++++++++ crates/trie/src/trie.rs | 24 +++++++++++-- 5 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 crates/trie/src/metrics.rs diff --git a/Cargo.lock b/Cargo.lock index 16c338475fbe..723dc3a39fab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7004,10 +7004,12 @@ dependencies = [ "auto_impl", "criterion", "derive_more", + "metrics", "once_cell", "proptest", "reth-db", "reth-interfaces", + "reth-metrics", "reth-primitives", "reth-provider", "revm", diff --git a/crates/trie/Cargo.toml b/crates/trie/Cargo.toml index a6bb0bbc3224..1a5ad9cf8955 100644 --- a/crates/trie/Cargo.toml +++ b/crates/trie/Cargo.toml @@ -18,6 +18,10 @@ reth-interfaces.workspace = true reth-db.workspace = true revm.workspace = true +# metrics +reth-metrics.workspace = true +metrics.workspace = true + # alloy alloy-rlp.workspace = true alloy-chains.workspace = true diff --git a/crates/trie/src/lib.rs b/crates/trie/src/lib.rs index 5950c3a38f31..edf26952da13 100644 --- a/crates/trie/src/lib.rs +++ b/crates/trie/src/lib.rs @@ -50,6 +50,8 @@ pub mod updates; mod progress; pub use progress::{IntermediateStateRootState, StateRootProgress}; +mod metrics; + /// Collection of trie-related test utilities. #[cfg(any(test, feature = "test-utils"))] pub mod test_utils; diff --git a/crates/trie/src/metrics.rs b/crates/trie/src/metrics.rs new file mode 100644 index 000000000000..20d71bb896fa --- /dev/null +++ b/crates/trie/src/metrics.rs @@ -0,0 +1,69 @@ +use metrics::Histogram; +use reth_metrics::Metrics; +use std::time::Instant; + +#[derive(Metrics)] +#[metrics(scope = "trie")] +pub(crate) struct TrieRootMetrics { + /// The number of microseconds trie root calculation lasted. + duration_micros: Histogram, + /// The number of branches added during trie root calculation. + branches_added: Histogram, + /// The number of leaves added during trie root calculation. + leaves_added: Histogram, +} + +impl TrieRootMetrics { + pub(crate) fn new(ty: TrieType) -> Self { + Self::new_with_labels(&[("type", ty.as_str())]) + } + + pub(crate) fn record(&self, tracker: TrieTracker) { + self.duration_micros.record(tracker.started_at.elapsed().as_millis() as f64); + self.branches_added.record(tracker.branches_added as f64); + self.leaves_added.record(tracker.leaves_added as f64); + } +} + +/// Trie type for differentiating between various trie calculations. +#[derive(Clone, Copy, Debug)] +pub(crate) enum TrieType { + State, + Storage, +} + +impl TrieType { + pub(crate) const fn as_str(&self) -> &'static str { + match self { + Self::State => "state", + Self::Storage => "storage", + } + } +} + +#[derive(Clone, Copy, Debug)] +pub(crate) struct TrieTracker { + started_at: Instant, + branches_added: u64, + leaves_added: u64, +} + +impl Default for TrieTracker { + fn default() -> Self { + Self { started_at: Instant::now(), branches_added: 0, leaves_added: 0 } + } +} + +impl TrieTracker { + pub(crate) fn leaves_added(&self) -> u64 { + self.leaves_added + } + + pub(crate) fn inc_branch(&mut self) { + self.branches_added += 1; + } + + pub(crate) fn inc_leaf(&mut self) { + self.leaves_added += 1; + } +} diff --git a/crates/trie/src/trie.rs b/crates/trie/src/trie.rs index 9d51077ba117..34409579a27d 100644 --- a/crates/trie/src/trie.rs +++ b/crates/trie/src/trie.rs @@ -1,5 +1,6 @@ use crate::{ hashed_cursor::{HashedCursorFactory, HashedStorageCursor}, + metrics::{TrieRootMetrics, TrieTracker, TrieType}, node_iter::{AccountNode, AccountNodeIter, StorageNode, StorageNodeIter}, prefix_set::{PrefixSet, PrefixSetLoader, PrefixSetMut, TriePrefixSets}, progress::{IntermediateStateRootState, StateRootProgress}, @@ -32,6 +33,8 @@ pub struct StateRoot { previous_state: Option, /// The number of updates after which the intermediate progress should be returned. threshold: u64, + /// State root metrics. + metrics: TrieRootMetrics, } impl StateRoot { @@ -67,6 +70,7 @@ impl StateRoot { prefix_sets: self.prefix_sets, threshold: self.threshold, previous_state: self.previous_state, + metrics: self.metrics, } } @@ -78,6 +82,7 @@ impl StateRoot { prefix_sets: self.prefix_sets, threshold: self.threshold, previous_state: self.previous_state, + metrics: self.metrics, } } } @@ -91,6 +96,7 @@ impl<'a, TX: DbTx> StateRoot<&'a TX, &'a TX> { prefix_sets: TriePrefixSets::default(), previous_state: None, threshold: 100_000, + metrics: TrieRootMetrics::new(TrieType::State), } } @@ -198,6 +204,7 @@ where fn calculate(self, retain_updates: bool) -> Result { trace!(target: "trie::loader", "calculating state root"); + let mut tracker = TrieTracker::default(); let mut trie_updates = TrieUpdates::default(); let hashed_account_cursor = self.hashed_cursor_factory.hashed_account_cursor()?; @@ -230,9 +237,11 @@ where while let Some(node) = account_node_iter.try_next()? { match node { AccountNode::Branch(node) => { + tracker.inc_branch(); hash_builder.add_branch(node.key, node.value, node.children_are_in_trie); } AccountNode::Leaf(hashed_address, account) => { + tracker.inc_leaf(); hashed_entries_walked += 1; // We assume we can always calculate a storage root without @@ -310,6 +319,7 @@ where self.prefix_sets.destroyed_accounts.into_iter().map(TrieKey::StorageTrie), ); + self.metrics.record(tracker); Ok(StateRootProgress::Complete(root, hashed_entries_walked, trie_updates)) } } @@ -325,6 +335,8 @@ pub struct StorageRoot { pub hashed_address: B256, /// The set of storage slot prefixes that have changed. pub prefix_set: PrefixSet, + /// Storage root metrics. + metrics: TrieRootMetrics, } impl StorageRoot { @@ -344,6 +356,7 @@ impl StorageRoot { hashed_cursor_factory, hashed_address, prefix_set: PrefixSetMut::default().freeze(), + metrics: TrieRootMetrics::new(TrieType::Storage), } } @@ -360,6 +373,7 @@ impl StorageRoot { hashed_cursor_factory, hashed_address: self.hashed_address, prefix_set: self.prefix_set, + metrics: self.metrics, } } @@ -370,6 +384,7 @@ impl StorageRoot { hashed_cursor_factory: self.hashed_cursor_factory, hashed_address: self.hashed_address, prefix_set: self.prefix_set, + metrics: self.metrics, } } } @@ -415,6 +430,7 @@ where retain_updates: bool, ) -> Result<(B256, usize, TrieUpdates), StorageRootError> { trace!(target: "trie::storage_root", hashed_address = ?self.hashed_address, "calculating storage root"); + let mut hashed_storage_cursor = self.hashed_cursor_factory.hashed_storage_cursor()?; // short circuit on empty storage @@ -426,21 +442,22 @@ where )) } + let mut tracker = TrieTracker::default(); let trie_cursor = self.trie_cursor_factory.storage_tries_cursor(self.hashed_address)?; let walker = TrieWalker::new(trie_cursor, self.prefix_set).with_updates(retain_updates); let mut hash_builder = HashBuilder::default().with_updates(retain_updates); - let mut storage_slots_walked = 0; let mut storage_node_iter = StorageNodeIter::new(walker, hashed_storage_cursor, self.hashed_address); while let Some(node) = storage_node_iter.try_next()? { match node { StorageNode::Branch(node) => { + tracker.inc_branch(); hash_builder.add_branch(node.key, node.value, node.children_are_in_trie); } StorageNode::Leaf(hashed_slot, value) => { - storage_slots_walked += 1; + tracker.inc_leaf(); hash_builder.add_leaf( Nibbles::unpack(hashed_slot), alloy_rlp::encode_fixed_size(&value).as_ref(), @@ -458,7 +475,10 @@ where trie_updates.extend(walker_updates); trie_updates.extend_with_storage_updates(self.hashed_address, hash_builder_updates); + self.metrics.record(tracker); trace!(target: "trie::storage_root", ?root, hashed_address = ?self.hashed_address, "calculated storage root"); + + let storage_slots_walked = tracker.leaves_added() as usize; Ok((root, storage_slots_walked, trie_updates)) } } From 677c000a2240f9cc8caaf1c43f1f4d01eb376bfd Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 08:52:49 +0100 Subject: [PATCH 2/8] secs f64 --- crates/trie/src/metrics.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/trie/src/metrics.rs b/crates/trie/src/metrics.rs index 20d71bb896fa..35c01d0ec8da 100644 --- a/crates/trie/src/metrics.rs +++ b/crates/trie/src/metrics.rs @@ -5,8 +5,8 @@ use std::time::Instant; #[derive(Metrics)] #[metrics(scope = "trie")] pub(crate) struct TrieRootMetrics { - /// The number of microseconds trie root calculation lasted. - duration_micros: Histogram, + /// The number of seconds trie root calculation lasted. + duration: Histogram, /// The number of branches added during trie root calculation. branches_added: Histogram, /// The number of leaves added during trie root calculation. @@ -19,7 +19,7 @@ impl TrieRootMetrics { } pub(crate) fn record(&self, tracker: TrieTracker) { - self.duration_micros.record(tracker.started_at.elapsed().as_millis() as f64); + self.duration.record(tracker.started_at.elapsed().as_secs_f64()); self.branches_added.record(tracker.branches_added as f64); self.leaves_added.record(tracker.leaves_added as f64); } From 5a9429793548f4f94c1e2633ba9c0c0765d56054 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 10:19:17 +0100 Subject: [PATCH 3/8] cache storage root metrics init, hide metrics behind the feature flag --- crates/trie/Cargo.toml | 10 +++-- crates/trie/src/lib.rs | 7 ++- crates/trie/src/metrics.rs | 68 ++++++++++++++--------------- crates/trie/src/stats.rs | 61 ++++++++++++++++++++++++++ crates/trie/src/trie.rs | 88 ++++++++++++++++++++++++++++++++------ 5 files changed, 181 insertions(+), 53 deletions(-) create mode 100644 crates/trie/src/stats.rs diff --git a/crates/trie/Cargo.toml b/crates/trie/Cargo.toml index 1a5ad9cf8955..d20b991c2907 100644 --- a/crates/trie/Cargo.toml +++ b/crates/trie/Cargo.toml @@ -19,8 +19,7 @@ reth-db.workspace = true revm.workspace = true # metrics -reth-metrics.workspace = true -metrics.workspace = true + # alloy alloy-rlp.workspace = true @@ -34,7 +33,11 @@ thiserror.workspace = true derive_more.workspace = true auto_impl = "1" -# test-utils +# `metrics` feature +reth-metrics = { workspace = true, optional = true } +metrics = { workspace = true, optional = true } + +# `test-utils` feature triehash = { version = "0.8", optional = true } [dev-dependencies] @@ -56,6 +59,7 @@ similar-asserts.workspace = true criterion.workspace = true [features] +metrics = ["reth-metrics", "dep:metrics"] test-utils = ["triehash"] [[bench]] diff --git a/crates/trie/src/lib.rs b/crates/trie/src/lib.rs index edf26952da13..cf2b34c5f180 100644 --- a/crates/trie/src/lib.rs +++ b/crates/trie/src/lib.rs @@ -50,7 +50,12 @@ pub mod updates; mod progress; pub use progress::{IntermediateStateRootState, StateRootProgress}; -mod metrics; +/// Trie metrics tracker. +pub mod stats; + +/// Trie root metrics. +#[cfg(feature = "metrics")] +pub mod metrics; /// Collection of trie-related test utilities. #[cfg(any(test, feature = "test-utils"))] diff --git a/crates/trie/src/metrics.rs b/crates/trie/src/metrics.rs index 35c01d0ec8da..b7dd38db5488 100644 --- a/crates/trie/src/metrics.rs +++ b/crates/trie/src/metrics.rs @@ -1,10 +1,29 @@ +use crate::stats::TrieStats; use metrics::Histogram; use reth_metrics::Metrics; -use std::time::Instant; -#[derive(Metrics)] +/// Wrapper for state root metrics. +#[derive(Debug)] +pub struct StateRootMetrics { + /// State trie metrics. + pub state_trie: TrieRootMetrics, + /// Storage trie metrics. + pub storage_trie: TrieRootMetrics, +} + +impl Default for StateRootMetrics { + fn default() -> Self { + Self { + state_trie: TrieRootMetrics::new(TrieType::State), + storage_trie: TrieRootMetrics::new(TrieType::Storage), + } + } +} + +/// Metrics for trie root calculation. +#[derive(Clone, Metrics)] #[metrics(scope = "trie")] -pub(crate) struct TrieRootMetrics { +pub struct TrieRootMetrics { /// The number of seconds trie root calculation lasted. duration: Histogram, /// The number of branches added during trie root calculation. @@ -14,21 +33,25 @@ pub(crate) struct TrieRootMetrics { } impl TrieRootMetrics { - pub(crate) fn new(ty: TrieType) -> Self { + /// Create new metrics for the given trie type. + pub fn new(ty: TrieType) -> Self { Self::new_with_labels(&[("type", ty.as_str())]) } - pub(crate) fn record(&self, tracker: TrieTracker) { - self.duration.record(tracker.started_at.elapsed().as_secs_f64()); - self.branches_added.record(tracker.branches_added as f64); - self.leaves_added.record(tracker.leaves_added as f64); + /// Record trie stats as metrics. + pub fn record(&self, stats: TrieStats) { + self.duration.record(stats.duration().as_secs_f64()); + self.branches_added.record(stats.branches_added() as f64); + self.leaves_added.record(stats.leaves_added() as f64); } } /// Trie type for differentiating between various trie calculations. #[derive(Clone, Copy, Debug)] -pub(crate) enum TrieType { +pub enum TrieType { + /// State trie type. State, + /// Storage trie type. Storage, } @@ -40,30 +63,3 @@ impl TrieType { } } } - -#[derive(Clone, Copy, Debug)] -pub(crate) struct TrieTracker { - started_at: Instant, - branches_added: u64, - leaves_added: u64, -} - -impl Default for TrieTracker { - fn default() -> Self { - Self { started_at: Instant::now(), branches_added: 0, leaves_added: 0 } - } -} - -impl TrieTracker { - pub(crate) fn leaves_added(&self) -> u64 { - self.leaves_added - } - - pub(crate) fn inc_branch(&mut self) { - self.branches_added += 1; - } - - pub(crate) fn inc_leaf(&mut self) { - self.leaves_added += 1; - } -} diff --git a/crates/trie/src/stats.rs b/crates/trie/src/stats.rs new file mode 100644 index 000000000000..95541a6d4710 --- /dev/null +++ b/crates/trie/src/stats.rs @@ -0,0 +1,61 @@ +use std::time::{Duration, Instant}; + +/// Trie stats. +#[derive(Clone, Copy, Debug)] +pub struct TrieStats { + duration: Duration, + branches_added: u64, + leaves_added: u64, +} + +impl TrieStats { + /// Duration for root calculation. + pub fn duration(&self) -> Duration { + self.duration + } + + /// Number of leaves added to the hash builder during the calculation. + pub fn leaves_added(&self) -> u64 { + self.leaves_added + } + + /// Number of branches added to the hash builder during the calculation. + pub fn branches_added(&self) -> u64 { + self.branches_added + } +} + +/// Trie metrics tracker. +#[derive(Debug)] +pub struct TrieTracker { + started_at: Instant, + branches_added: u64, + leaves_added: u64, +} + +impl Default for TrieTracker { + fn default() -> Self { + Self { started_at: Instant::now(), branches_added: 0, leaves_added: 0 } + } +} + +impl TrieTracker { + /// Increment the number of branches added to the hash builder during the calculation. + pub fn inc_branch(&mut self) { + self.branches_added += 1; + } + + /// Increment the number of leaves added to the hash builder during the calculation. + pub fn inc_leaf(&mut self) { + self.leaves_added += 1; + } + + /// Called when root calculation is finished to return trie statistics. + pub fn finish(self) -> TrieStats { + TrieStats { + duration: self.started_at.elapsed(), + branches_added: self.branches_added, + leaves_added: self.leaves_added, + } + } +} diff --git a/crates/trie/src/trie.rs b/crates/trie/src/trie.rs index 34409579a27d..eb7fecad3a81 100644 --- a/crates/trie/src/trie.rs +++ b/crates/trie/src/trie.rs @@ -1,9 +1,9 @@ use crate::{ hashed_cursor::{HashedCursorFactory, HashedStorageCursor}, - metrics::{TrieRootMetrics, TrieTracker, TrieType}, node_iter::{AccountNode, AccountNodeIter, StorageNode, StorageNodeIter}, prefix_set::{PrefixSet, PrefixSetLoader, PrefixSetMut, TriePrefixSets}, progress::{IntermediateStateRootState, StateRootProgress}, + stats::TrieTracker, trie_cursor::TrieCursorFactory, updates::{TrieKey, TrieOp, TrieUpdates}, walker::TrieWalker, @@ -20,6 +20,9 @@ use reth_primitives::{ use std::ops::RangeInclusive; use tracing::{debug, trace}; +#[cfg(feature = "metrics")] +use crate::metrics::{StateRootMetrics, TrieRootMetrics, TrieType}; + /// StateRoot is used to compute the root node of a state trie. #[derive(Debug)] pub struct StateRoot { @@ -33,8 +36,9 @@ pub struct StateRoot { previous_state: Option, /// The number of updates after which the intermediate progress should be returned. threshold: u64, + #[cfg(feature = "metrics")] /// State root metrics. - metrics: TrieRootMetrics, + metrics: StateRootMetrics, } impl StateRoot { @@ -70,6 +74,7 @@ impl StateRoot { prefix_sets: self.prefix_sets, threshold: self.threshold, previous_state: self.previous_state, + #[cfg(feature = "metrics")] metrics: self.metrics, } } @@ -82,6 +87,7 @@ impl StateRoot { prefix_sets: self.prefix_sets, threshold: self.threshold, previous_state: self.previous_state, + #[cfg(feature = "metrics")] metrics: self.metrics, } } @@ -96,7 +102,8 @@ impl<'a, TX: DbTx> StateRoot<&'a TX, &'a TX> { prefix_sets: TriePrefixSets::default(), previous_state: None, threshold: 100_000, - metrics: TrieRootMetrics::new(TrieType::State), + #[cfg(feature = "metrics")] + metrics: StateRootMetrics::default(), } } @@ -203,7 +210,7 @@ where } fn calculate(self, retain_updates: bool) -> Result { - trace!(target: "trie::loader", "calculating state root"); + trace!(target: "trie::state_root", "calculating state root"); let mut tracker = TrieTracker::default(); let mut trie_updates = TrieUpdates::default(); @@ -255,6 +262,8 @@ where self.trie_cursor_factory.clone(), self.hashed_cursor_factory.clone(), hashed_address, + #[cfg(feature = "metrics")] + self.metrics.storage_trie.clone(), ) .with_prefix_set( self.prefix_sets @@ -319,7 +328,20 @@ where self.prefix_sets.destroyed_accounts.into_iter().map(TrieKey::StorageTrie), ); - self.metrics.record(tracker); + let stats = tracker.finish(); + + #[cfg(feature = "metrics")] + self.metrics.state_trie.record(stats); + + trace!( + target: "trie::state_root", + ?root, + duration = ?stats.duration(), + branches_added = stats.branches_added(), + leaves_added = stats.leaves_added(), + "calculated state root" + ); + Ok(StateRootProgress::Complete(root, hashed_entries_walked, trie_updates)) } } @@ -335,14 +357,26 @@ pub struct StorageRoot { pub hashed_address: B256, /// The set of storage slot prefixes that have changed. pub prefix_set: PrefixSet, + #[cfg(feature = "metrics")] /// Storage root metrics. metrics: TrieRootMetrics, } impl StorageRoot { /// Creates a new storage root calculator given a raw address. - pub fn new(trie_cursor_factory: T, hashed_cursor_factory: H, address: Address) -> Self { - Self::new_hashed(trie_cursor_factory, hashed_cursor_factory, keccak256(address)) + pub fn new( + trie_cursor_factory: T, + hashed_cursor_factory: H, + address: Address, + #[cfg(feature = "metrics")] metrics: TrieRootMetrics, + ) -> Self { + Self::new_hashed( + trie_cursor_factory, + hashed_cursor_factory, + keccak256(address), + #[cfg(feature = "metrics")] + metrics, + ) } /// Creates a new storage root calculator given a hashed address. @@ -350,13 +384,15 @@ impl StorageRoot { trie_cursor_factory: T, hashed_cursor_factory: H, hashed_address: B256, + #[cfg(feature = "metrics")] metrics: TrieRootMetrics, ) -> Self { Self { trie_cursor_factory, hashed_cursor_factory, hashed_address, prefix_set: PrefixSetMut::default().freeze(), - metrics: TrieRootMetrics::new(TrieType::Storage), + #[cfg(feature = "metrics")] + metrics, } } @@ -373,6 +409,7 @@ impl StorageRoot { hashed_cursor_factory, hashed_address: self.hashed_address, prefix_set: self.prefix_set, + #[cfg(feature = "metrics")] metrics: self.metrics, } } @@ -384,6 +421,7 @@ impl StorageRoot { hashed_cursor_factory: self.hashed_cursor_factory, hashed_address: self.hashed_address, prefix_set: self.prefix_set, + #[cfg(feature = "metrics")] metrics: self.metrics, } } @@ -392,12 +430,24 @@ impl StorageRoot { impl<'a, TX: DbTx> StorageRoot<&'a TX, &'a TX> { /// Create a new storage root calculator from database transaction and raw address. pub fn from_tx(tx: &'a TX, address: Address) -> Self { - Self::new(tx, tx, address) + Self::new( + tx, + tx, + address, + #[cfg(feature = "metrics")] + TrieRootMetrics::new(TrieType::Storage), + ) } /// Create a new storage root calculator from database transaction and hashed address. pub fn from_tx_hashed(tx: &'a TX, hashed_address: B256) -> Self { - Self::new_hashed(tx, tx, hashed_address) + Self::new_hashed( + tx, + tx, + hashed_address, + #[cfg(feature = "metrics")] + TrieRootMetrics::new(TrieType::Storage), + ) } } @@ -475,10 +525,22 @@ where trie_updates.extend(walker_updates); trie_updates.extend_with_storage_updates(self.hashed_address, hash_builder_updates); - self.metrics.record(tracker); - trace!(target: "trie::storage_root", ?root, hashed_address = ?self.hashed_address, "calculated storage root"); + let stats = tracker.finish(); + + #[cfg(feature = "metrics")] + self.metrics.record(stats); + + trace!( + target: "trie::storage_root", + ?root, + hashed_address = %self.hashed_address, + duration = ?stats.duration(), + branches_added = stats.branches_added(), + leaves_added = stats.leaves_added(), + "calculated storage root" + ); - let storage_slots_walked = tracker.leaves_added() as usize; + let storage_slots_walked = stats.leaves_added() as usize; Ok((root, storage_slots_walked, trie_updates)) } } From de036d77f6f0910b6c95a191a99c30e0a7565bde Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 10:21:21 +0100 Subject: [PATCH 4/8] fix doc --- crates/trie/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/trie/src/lib.rs b/crates/trie/src/lib.rs index cf2b34c5f180..ffd08375a83b 100644 --- a/crates/trie/src/lib.rs +++ b/crates/trie/src/lib.rs @@ -50,10 +50,10 @@ pub mod updates; mod progress; pub use progress::{IntermediateStateRootState, StateRootProgress}; -/// Trie metrics tracker. +/// Trie calculation stats. pub mod stats; -/// Trie root metrics. +/// Trie calculation metrics. #[cfg(feature = "metrics")] pub mod metrics; From 0c330036cf66ea95867e75b8a82a1585ef46c05b Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 10:24:04 +0100 Subject: [PATCH 5/8] enable metrics by default --- crates/trie/Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/trie/Cargo.toml b/crates/trie/Cargo.toml index d20b991c2907..8bd8829ed8bc 100644 --- a/crates/trie/Cargo.toml +++ b/crates/trie/Cargo.toml @@ -59,6 +59,7 @@ similar-asserts.workspace = true criterion.workspace = true [features] +default = ["metrics"] metrics = ["reth-metrics", "dep:metrics"] test-utils = ["triehash"] From 6635ce3501a7017168bc5f2c58f69377a58c5cd8 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 14:17:16 +0100 Subject: [PATCH 6/8] rename duration --- crates/trie/src/metrics.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/trie/src/metrics.rs b/crates/trie/src/metrics.rs index b7dd38db5488..7582f37418d9 100644 --- a/crates/trie/src/metrics.rs +++ b/crates/trie/src/metrics.rs @@ -25,7 +25,7 @@ impl Default for StateRootMetrics { #[metrics(scope = "trie")] pub struct TrieRootMetrics { /// The number of seconds trie root calculation lasted. - duration: Histogram, + duration_seconds: Histogram, /// The number of branches added during trie root calculation. branches_added: Histogram, /// The number of leaves added during trie root calculation. @@ -40,7 +40,7 @@ impl TrieRootMetrics { /// Record trie stats as metrics. pub fn record(&self, stats: TrieStats) { - self.duration.record(stats.duration().as_secs_f64()); + self.duration_seconds.record(stats.duration().as_secs_f64()); self.branches_added.record(stats.branches_added() as f64); self.leaves_added.record(stats.leaves_added() as f64); } From 6eaa1c76ec968467d04ef277187c05085d6ae38e Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 15:18:28 +0100 Subject: [PATCH 7/8] rm obsolete section from Cargo.toml --- crates/trie/Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/crates/trie/Cargo.toml b/crates/trie/Cargo.toml index 8bd8829ed8bc..45939c14bfef 100644 --- a/crates/trie/Cargo.toml +++ b/crates/trie/Cargo.toml @@ -18,9 +18,6 @@ reth-interfaces.workspace = true reth-db.workspace = true revm.workspace = true -# metrics - - # alloy alloy-rlp.workspace = true alloy-chains.workspace = true From d94dca6bd4a27833ea6735f4d72bcd41df1f92a5 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Mon, 4 Mar 2024 15:25:28 +0100 Subject: [PATCH 8/8] use display impl for root --- crates/trie/src/trie.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/trie/src/trie.rs b/crates/trie/src/trie.rs index eb7fecad3a81..8a4a69fed905 100644 --- a/crates/trie/src/trie.rs +++ b/crates/trie/src/trie.rs @@ -335,7 +335,7 @@ where trace!( target: "trie::state_root", - ?root, + %root, duration = ?stats.duration(), branches_added = stats.branches_added(), leaves_added = stats.leaves_added(), @@ -532,7 +532,7 @@ where trace!( target: "trie::storage_root", - ?root, + %root, hashed_address = %self.hashed_address, duration = ?stats.duration(), branches_added = stats.branches_added(),