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