reth_trie/
metrics.rs

1use crate::{stats::TrieStats, trie::TrieType};
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/// Metrics for [`crate::walker::TrieWalker`].
50#[derive(Clone, Metrics)]
51#[metrics(scope = "trie.walker")]
52pub struct WalkerMetrics {
53    /// The number of branch nodes seeked by the walker.
54    branch_nodes_seeked_total: Counter,
55    /// The number of subnodes out of order due to wrong tree mask.
56    out_of_order_subnode: Counter,
57}
58
59impl WalkerMetrics {
60    /// Create new metrics for the given trie type.
61    pub fn new(ty: TrieType) -> Self {
62        Self::new_with_labels(&[("type", ty.as_str())])
63    }
64
65    /// Increment `branch_nodes_seeked_total`.
66    pub fn inc_branch_nodes_seeked(&self) {
67        self.branch_nodes_seeked_total.increment(1);
68    }
69
70    /// Increment `out_of_order_subnode`.
71    pub fn inc_out_of_order_subnode(&self, amount: u64) {
72        self.out_of_order_subnode.increment(amount);
73    }
74}
75
76/// Metrics for [`crate::node_iter::TrieNodeIter`].
77#[derive(Clone, Metrics)]
78#[metrics(scope = "trie.node_iter")]
79pub struct TrieNodeIterMetrics {
80    /// The number of branch nodes returned by the iterator.
81    branch_nodes_returned_total: Counter,
82    /// The number of times the same hashed cursor key was seeked multiple times in a row by the
83    /// iterator. It does not mean the database seek was actually done, as the trie node
84    /// iterator caches the last hashed cursor seek.
85    leaf_nodes_same_seeked_total: Counter,
86    /// The number of leaf nodes seeked by the iterator.
87    leaf_nodes_seeked_total: Counter,
88    /// The number of leaf nodes advanced by the iterator.
89    leaf_nodes_advanced_total: Counter,
90    /// The number of leaf nodes returned by the iterator.
91    leaf_nodes_returned_total: Counter,
92}
93
94impl TrieNodeIterMetrics {
95    /// Create new metrics for the given trie type.
96    pub fn new(ty: TrieType) -> Self {
97        Self::new_with_labels(&[("type", ty.as_str())])
98    }
99
100    /// Increment `branch_nodes_returned_total`.
101    pub fn inc_branch_nodes_returned(&self) {
102        self.branch_nodes_returned_total.increment(1);
103    }
104
105    /// Increment `leaf_nodes_same_seeked_total`.
106    pub fn inc_leaf_nodes_same_seeked(&self) {
107        self.leaf_nodes_same_seeked_total.increment(1);
108    }
109
110    /// Increment `leaf_nodes_seeked_total`.
111    pub fn inc_leaf_nodes_seeked(&self) {
112        self.leaf_nodes_seeked_total.increment(1);
113    }
114
115    /// Increment `leaf_nodes_advanced_total`.
116    pub fn inc_leaf_nodes_advanced(&self) {
117        self.leaf_nodes_advanced_total.increment(1);
118    }
119
120    /// Increment `leaf_nodes_returned_total`.
121    pub fn inc_leaf_nodes_returned(&self) {
122        self.leaf_nodes_returned_total.increment(1);
123    }
124}