1use crate::stats::TrieStats;
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, Copy, Debug)]
51pub enum TrieType {
52 State,
54 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#[derive(Clone, Metrics)]
69#[metrics(scope = "trie.walker")]
70pub struct WalkerMetrics {
71 out_of_order_subnode: Counter,
73}
74
75impl WalkerMetrics {
76 pub fn new(ty: TrieType) -> Self {
78 Self::new_with_labels(&[("type", ty.as_str())])
79 }
80
81 pub fn inc_out_of_order_subnode(&self, amount: u64) {
83 self.out_of_order_subnode.increment(amount);
84 }
85}