Skip to main content

reth_trie_parallel/
proof_task_metrics.rs

1use crate::value_encoder::ValueEncoderStats;
2use reth_metrics::{metrics::Histogram, Metrics};
3use reth_trie::{
4    hashed_cursor::{HashedCursorMetrics, HashedCursorMetricsCache},
5    trie_cursor::{TrieCursorMetrics, TrieCursorMetricsCache},
6    TrieType,
7};
8use std::time::Duration;
9
10/// Metrics for the proof task.
11#[derive(Clone, Metrics)]
12#[metrics(scope = "trie.proof_task")]
13pub struct ProofTaskTrieMetrics {
14    /// A histogram for the number of blinded account nodes fetched.
15    blinded_account_nodes: Histogram,
16    /// A histogram for the number of blinded storage nodes fetched.
17    blinded_storage_nodes: Histogram,
18    /// Histogram for storage worker idle time in seconds (waiting for proof jobs).
19    storage_worker_idle_time_seconds: Histogram,
20    /// Histogram for account worker idle time in seconds (waiting for proof jobs + storage
21    /// results).
22    account_worker_idle_time_seconds: Histogram,
23    /// Histogram for `Dispatched` deferred encoder variant count.
24    deferred_encoder_dispatched: Histogram,
25    /// Histogram for `FromCache` deferred encoder variant count.
26    deferred_encoder_from_cache: Histogram,
27    /// Histogram for `Sync` deferred encoder variant count.
28    deferred_encoder_sync: Histogram,
29    /// Histogram for dispatched storage proofs that fell back to sync due to missing root.
30    deferred_encoder_dispatched_missing_root: Histogram,
31}
32
33impl ProofTaskTrieMetrics {
34    /// Record account nodes fetched.
35    pub fn record_account_nodes(&self, count: usize) {
36        self.blinded_account_nodes.record(count as f64);
37    }
38
39    /// Record storage nodes fetched.
40    pub fn record_storage_nodes(&self, count: usize) {
41        self.blinded_storage_nodes.record(count as f64);
42    }
43
44    /// Record storage worker idle time.
45    pub fn record_storage_worker_idle_time(&self, duration: Duration) {
46        self.storage_worker_idle_time_seconds.record(duration.as_secs_f64());
47    }
48
49    /// Record account worker idle time.
50    pub fn record_account_worker_idle_time(&self, duration: Duration) {
51        self.account_worker_idle_time_seconds.record(duration.as_secs_f64());
52    }
53
54    /// Record value encoder stats (deferred encoder variant counts).
55    pub(crate) fn record_value_encoder_stats(&self, stats: &ValueEncoderStats) {
56        self.deferred_encoder_dispatched.record(stats.dispatched_count as f64);
57        self.deferred_encoder_from_cache.record(stats.from_cache_count as f64);
58        self.deferred_encoder_sync.record(stats.sync_count as f64);
59        self.deferred_encoder_dispatched_missing_root
60            .record(stats.dispatched_missing_root_count as f64);
61    }
62}
63
64/// Cursor metrics for proof task operations.
65#[derive(Clone, Debug)]
66pub struct ProofTaskCursorMetrics {
67    /// Metrics for account trie cursor operations.
68    pub account_trie_cursor: TrieCursorMetrics,
69    /// Metrics for account hashed cursor operations.
70    pub account_hashed_cursor: HashedCursorMetrics,
71    /// Metrics for storage trie cursor operations.
72    pub storage_trie_cursor: TrieCursorMetrics,
73    /// Metrics for storage hashed cursor operations.
74    pub storage_hashed_cursor: HashedCursorMetrics,
75}
76
77impl ProofTaskCursorMetrics {
78    /// Create a new instance with properly initialized cursor metrics.
79    pub fn new() -> Self {
80        Self {
81            account_trie_cursor: TrieCursorMetrics::new(TrieType::State),
82            account_hashed_cursor: HashedCursorMetrics::new(TrieType::State),
83            storage_trie_cursor: TrieCursorMetrics::new(TrieType::Storage),
84            storage_hashed_cursor: HashedCursorMetrics::new(TrieType::Storage),
85        }
86    }
87
88    /// Record the cached metrics from the provided cache and reset the cache counters.
89    ///
90    /// This method adds the current counter values from the cache to the Prometheus metrics
91    /// and then resets all cache counters to zero.
92    pub fn record(&mut self, cache: &mut ProofTaskCursorMetricsCache) {
93        self.account_trie_cursor.record(&mut cache.account_trie_cursor);
94        self.account_hashed_cursor.record(&mut cache.account_hashed_cursor);
95        self.storage_trie_cursor.record(&mut cache.storage_trie_cursor);
96        self.storage_hashed_cursor.record(&mut cache.storage_hashed_cursor);
97        cache.reset();
98    }
99}
100
101impl Default for ProofTaskCursorMetrics {
102    fn default() -> Self {
103        Self::new()
104    }
105}
106
107/// Cached cursor metrics for proof task operations.
108#[derive(Clone, Debug, Default, Copy)]
109pub struct ProofTaskCursorMetricsCache {
110    /// Cached metrics for account trie cursor operations.
111    pub account_trie_cursor: TrieCursorMetricsCache,
112    /// Cached metrics for account hashed cursor operations.
113    pub account_hashed_cursor: HashedCursorMetricsCache,
114    /// Cached metrics for storage trie cursor operations.
115    pub storage_trie_cursor: TrieCursorMetricsCache,
116    /// Cached metrics for storage hashed cursor operations.
117    pub storage_hashed_cursor: HashedCursorMetricsCache,
118}
119
120impl ProofTaskCursorMetricsCache {
121    /// Extend this cache by adding the counts from another cache.
122    ///
123    /// This accumulates the counter values from `other` into this cache.
124    pub fn extend(&mut self, other: &Self) {
125        self.account_trie_cursor.extend(&other.account_trie_cursor);
126        self.account_hashed_cursor.extend(&other.account_hashed_cursor);
127        self.storage_trie_cursor.extend(&other.storage_trie_cursor);
128        self.storage_hashed_cursor.extend(&other.storage_hashed_cursor);
129    }
130
131    /// Reset all counters to zero.
132    pub const fn reset(&mut self) {
133        self.account_trie_cursor.reset();
134        self.account_hashed_cursor.reset();
135        self.storage_trie_cursor.reset();
136        self.storage_hashed_cursor.reset();
137    }
138
139    /// Record the spans for metrics.
140    pub fn record_spans(&self) {
141        self.account_trie_cursor.record_span("account_trie_cursor");
142        self.account_hashed_cursor.record_span("account_hashed_cursor");
143        self.storage_trie_cursor.record_span("storage_trie_cursor");
144        self.storage_hashed_cursor.record_span("storage_hashed_cursor");
145    }
146}