reth_trie/
metrics.rs

1use crate::stats::TrieStats;
2use metrics::{Counter, Histogram};
3use reth_metrics::Metrics;
4
5/// Wrapper for state root metrics.
6#[derive(Debug)]
7pub struct StateRootMetrics {
8    /// State trie metrics.
9    pub state_trie: TrieRootMetrics,
10    /// Storage trie metrics.
11    pub storage_trie: TrieRootMetrics,
12}
13
14impl Default for StateRootMetrics {
15    fn default() -> Self {
16        Self {
17            state_trie: TrieRootMetrics::new(TrieType::State),
18            storage_trie: TrieRootMetrics::new(TrieType::Storage),
19        }
20    }
21}
22
23/// Metrics for trie root calculation.
24#[derive(Clone, Metrics)]
25#[metrics(scope = "trie")]
26pub struct TrieRootMetrics {
27    /// The number of seconds trie root calculation lasted.
28    duration_seconds: Histogram,
29    /// The number of branches added during trie root calculation.
30    branches_added: Histogram,
31    /// The number of leaves added during trie root calculation.
32    leaves_added: Histogram,
33}
34
35impl TrieRootMetrics {
36    /// Create new metrics for the given trie type.
37    pub fn new(ty: TrieType) -> Self {
38        Self::new_with_labels(&[("type", ty.as_str())])
39    }
40
41    /// Record trie stats as metrics.
42    pub fn record(&self, stats: TrieStats) {
43        self.duration_seconds.record(stats.duration().as_secs_f64());
44        self.branches_added.record(stats.branches_added() as f64);
45        self.leaves_added.record(stats.leaves_added() as f64);
46    }
47}
48
49/// Trie type for differentiating between various trie calculations.
50#[derive(Clone, Copy, Debug)]
51pub enum TrieType {
52    /// State trie type.
53    State,
54    /// Storage trie type.
55    Storage,
56}
57
58impl TrieType {
59    pub(crate) const fn as_str(&self) -> &'static str {
60        match self {
61            Self::State => "state",
62            Self::Storage => "storage",
63        }
64    }
65}
66
67/// Metrics for trie walker
68#[derive(Clone, Metrics)]
69#[metrics(scope = "trie.walker")]
70pub struct WalkerMetrics {
71    /// The number of subnodes out of order due to wrong tree mask.
72    out_of_order_subnode: Counter,
73}
74
75impl WalkerMetrics {
76    /// Create new metrics for the given trie type.
77    pub fn new(ty: TrieType) -> Self {
78        Self::new_with_labels(&[("type", ty.as_str())])
79    }
80
81    /// Increment `out_of_order_subnode`.
82    pub fn inc_out_of_order_subnode(&self, amount: u64) {
83        self.out_of_order_subnode.increment(amount);
84    }
85}