1use crate::{stats::TrieStats, trie::TrieType};
2use metrics::{Counter, Histogram};
3use reth_metrics::Metrics;
45/// Wrapper for state root metrics.
6#[derive(Debug)]
7pub struct StateRootMetrics {
8/// State trie metrics.
9pub state_trie: TrieRootMetrics,
10/// Storage trie metrics.
11pub storage_trie: TrieRootMetrics,
12}
1314impl Defaultfor StateRootMetrics {
15fn default() -> Self {
16Self {
17 state_trie: TrieRootMetrics::new(TrieType::State),
18 storage_trie: TrieRootMetrics::new(TrieType::Storage),
19 }
20 }
21}
2223/// 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.
28duration_seconds: Histogram,
29/// The number of branches added during trie root calculation.
30branches_added: Histogram,
31/// The number of leaves added during trie root calculation.
32leaves_added: Histogram,
33}
3435impl TrieRootMetrics {
36/// Create new metrics for the given trie type.
37pub fn new(ty: TrieType) -> Self {
38Self::new_with_labels(&[("type", ty.as_str())])
39 }
4041/// Record trie stats as metrics.
42pub fn record(&self, stats: TrieStats) {
43self.duration_seconds.record(stats.duration().as_secs_f64());
44self.branches_added.record(stats.branches_added() as f64);
45self.leaves_added.record(stats.leaves_added() as f64);
46 }
47}
4849/// 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.
54out_of_order_subnode: Counter,
55}
5657impl WalkerMetrics {
58/// Create new metrics for the given trie type.
59pub fn new(ty: TrieType) -> Self {
60Self::new_with_labels(&[("type", ty.as_str())])
61 }
6263/// Increment `out_of_order_subnode`.
64pub fn inc_out_of_order_subnode(&self, amount: u64) {
65self.out_of_order_subnode.increment(amount);
66 }
67}
6869/// 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.
74branch_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.
78leaf_nodes_same_seeked_total: Counter,
79/// The number of times the same leaf node as we just advanced to was seeked by the iterator.
80leaf_nodes_same_seeked_as_advanced_total: Counter,
81/// The number of leaf nodes seeked by the iterator.
82leaf_nodes_seeked_total: Counter,
83/// The number of leaf nodes advanced by the iterator.
84leaf_nodes_advanced_total: Counter,
85/// The number of leaf nodes returned by the iterator.
86leaf_nodes_returned_total: Counter,
87}
8889impl TrieNodeIterMetrics {
90/// Create new metrics for the given trie type.
91pub fn new(ty: TrieType) -> Self {
92Self::new_with_labels(&[("type", ty.as_str())])
93 }
9495/// Increment `branch_nodes_returned_total`.
96pub fn inc_branch_nodes_returned(&self) {
97self.branch_nodes_returned_total.increment(1);
98 }
99100/// Increment `leaf_nodes_same_seeked_total`.
101pub fn inc_leaf_nodes_same_seeked(&self) {
102self.leaf_nodes_same_seeked_total.increment(1);
103 }
104105/// Increment `leaf_nodes_same_seeked_as_advanced_total`.
106pub fn inc_leaf_nodes_same_seeked_as_advanced(&self) {
107self.leaf_nodes_same_seeked_as_advanced_total.increment(1);
108 }
109110/// Increment `leaf_nodes_seeked_total`.
111pub fn inc_leaf_nodes_seeked(&self) {
112self.leaf_nodes_seeked_total.increment(1);
113 }
114115/// Increment `leaf_nodes_advanced_total`.
116pub fn inc_leaf_nodes_advanced(&self) {
117self.leaf_nodes_advanced_total.increment(1);
118 }
119120/// Increment `leaf_nodes_returned_total`.
121pub fn inc_leaf_nodes_returned(&self) {
122self.leaf_nodes_returned_total.increment(1);
123 }
124}