reth_trie/
metrics.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::stats::TrieStats;
use metrics::{Counter, Histogram};
use reth_metrics::Metrics;

/// Wrapper for state root metrics.
#[derive(Debug)]
pub struct StateRootMetrics {
    /// State trie metrics.
    pub state_trie: TrieRootMetrics,
    /// Storage trie metrics.
    pub storage_trie: TrieRootMetrics,
}

impl Default for StateRootMetrics {
    fn default() -> Self {
        Self {
            state_trie: TrieRootMetrics::new(TrieType::State),
            storage_trie: TrieRootMetrics::new(TrieType::Storage),
        }
    }
}

/// Metrics for trie root calculation.
#[derive(Clone, Metrics)]
#[metrics(scope = "trie")]
pub struct TrieRootMetrics {
    /// The number of seconds trie root calculation lasted.
    duration_seconds: Histogram,
    /// The number of branches added during trie root calculation.
    branches_added: Histogram,
    /// The number of leaves added during trie root calculation.
    leaves_added: Histogram,
}

impl TrieRootMetrics {
    /// Create new metrics for the given trie type.
    pub fn new(ty: TrieType) -> Self {
        Self::new_with_labels(&[("type", ty.as_str())])
    }

    /// Record trie stats as metrics.
    pub fn record(&self, stats: TrieStats) {
        self.duration_seconds.record(stats.duration().as_secs_f64());
        self.branches_added.record(stats.branches_added() as f64);
        self.leaves_added.record(stats.leaves_added() as f64);
    }
}

/// Trie type for differentiating between various trie calculations.
#[derive(Clone, Copy, Debug)]
pub enum TrieType {
    /// State trie type.
    State,
    /// Storage trie type.
    Storage,
}

impl TrieType {
    pub(crate) const fn as_str(&self) -> &'static str {
        match self {
            Self::State => "state",
            Self::Storage => "storage",
        }
    }
}

/// Metrics for trie walker
#[derive(Clone, Metrics)]
#[metrics(scope = "trie.walker")]
pub struct WalkerMetrics {
    /// The number of subnodes out of order due to wrong tree mask.
    out_of_order_subnode: Counter,
}

impl WalkerMetrics {
    /// Create new metrics for the given trie type.
    pub fn new(ty: TrieType) -> Self {
        Self::new_with_labels(&[("type", ty.as_str())])
    }

    /// Increment `out_of_order_subnode`.
    pub fn inc_out_of_order_subnode(&self, amount: u64) {
        self.out_of_order_subnode.increment(amount);
    }
}