-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
232 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use crate::stats::TrieStats; | ||
use metrics::Histogram; | ||
use reth_metrics::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 struct TrieRootMetrics { | ||
/// The number of seconds trie root calculation lasted. | ||
duration_seconds: 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 { | ||
/// Create new metrics for the given trie type. | ||
pub fn new(ty: TrieType) -> Self { | ||
Self::new_with_labels(&[("type", ty.as_str())]) | ||
} | ||
|
||
/// Record trie stats as metrics. | ||
pub fn record(&self, stats: TrieStats) { | ||
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); | ||
} | ||
} | ||
|
||
/// Trie type for differentiating between various trie calculations. | ||
#[derive(Clone, Copy, Debug)] | ||
pub enum TrieType { | ||
/// State trie type. | ||
State, | ||
/// Storage trie type. | ||
Storage, | ||
} | ||
|
||
impl TrieType { | ||
pub(crate) const fn as_str(&self) -> &'static str { | ||
match self { | ||
Self::State => "state", | ||
Self::Storage => "storage", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
} | ||
} | ||
} |
Oops, something went wrong.