1use crate::{stats::TrieStats, trie::TrieType};
2use metrics::{Counter, Histogram};
3use reth_metrics::Metrics;
4
5#[derive(Debug)]
7pub struct StateRootMetrics {
8 pub state_trie: TrieRootMetrics,
10 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#[derive(Clone, Metrics)]
25#[metrics(scope = "trie")]
26pub struct TrieRootMetrics {
27 duration_seconds: Histogram,
29 branches_added: Histogram,
31 leaves_added: Histogram,
33}
34
35impl TrieRootMetrics {
36 pub fn new(ty: TrieType) -> Self {
38 Self::new_with_labels(&[("type", ty.as_str())])
39 }
40
41 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#[derive(Clone, Metrics)]
51#[metrics(scope = "trie.walker")]
52pub struct WalkerMetrics {
53 branch_nodes_seeked_total: Counter,
55 out_of_order_subnode: Counter,
57}
58
59impl WalkerMetrics {
60 pub fn new(ty: TrieType) -> Self {
62 Self::new_with_labels(&[("type", ty.as_str())])
63 }
64
65 pub fn inc_branch_nodes_seeked(&self) {
67 self.branch_nodes_seeked_total.increment(1);
68 }
69
70 pub fn inc_out_of_order_subnode(&self, amount: u64) {
72 self.out_of_order_subnode.increment(amount);
73 }
74}
75
76#[derive(Clone, Metrics)]
78#[metrics(scope = "trie.node_iter")]
79pub struct TrieNodeIterMetrics {
80 branch_nodes_returned_total: Counter,
82 leaf_nodes_same_seeked_total: Counter,
86 leaf_nodes_seeked_total: Counter,
88 leaf_nodes_advanced_total: Counter,
90 leaf_nodes_returned_total: Counter,
92}
93
94impl TrieNodeIterMetrics {
95 pub fn new(ty: TrieType) -> Self {
97 Self::new_with_labels(&[("type", ty.as_str())])
98 }
99
100 pub fn inc_branch_nodes_returned(&self) {
102 self.branch_nodes_returned_total.increment(1);
103 }
104
105 pub fn inc_leaf_nodes_same_seeked(&self) {
107 self.leaf_nodes_same_seeked_total.increment(1);
108 }
109
110 pub fn inc_leaf_nodes_seeked(&self) {
112 self.leaf_nodes_seeked_total.increment(1);
113 }
114
115 pub fn inc_leaf_nodes_advanced(&self) {
117 self.leaf_nodes_advanced_total.increment(1);
118 }
119
120 pub fn inc_leaf_nodes_returned(&self) {
122 self.leaf_nodes_returned_total.increment(1);
123 }
124}