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    /// Histogram for time account workers spent blocked waiting for storage proof results
32    /// (seconds). This is the portion of account worker idle time attributable to storage
33    /// worker latency rather than queue wait.
34    account_worker_storage_wait_seconds: Histogram,
35}
36
37impl ProofTaskTrieMetrics {
38    /// Record account nodes fetched.
39    pub fn record_account_nodes(&self, count: usize) {
40        self.blinded_account_nodes.record(count as f64);
41    }
42
43    /// Record storage nodes fetched.
44    pub fn record_storage_nodes(&self, count: usize) {
45        self.blinded_storage_nodes.record(count as f64);
46    }
47
48    /// Record storage worker idle time.
49    pub fn record_storage_worker_idle_time(&self, duration: Duration) {
50        self.storage_worker_idle_time_seconds.record(duration.as_secs_f64());
51    }
52
53    /// Record account worker idle time.
54    pub fn record_account_worker_idle_time(&self, duration: Duration) {
55        self.account_worker_idle_time_seconds.record(duration.as_secs_f64());
56    }
57
58    /// Record value encoder stats (deferred encoder variant counts and storage wait time).
59    pub(crate) fn record_value_encoder_stats(&self, stats: &ValueEncoderStats) {
60        self.deferred_encoder_dispatched.record(stats.dispatched_count as f64);
61        self.deferred_encoder_from_cache.record(stats.from_cache_count as f64);
62        self.deferred_encoder_sync.record(stats.sync_count as f64);
63        self.deferred_encoder_dispatched_missing_root
64            .record(stats.dispatched_missing_root_count as f64);
65        self.account_worker_storage_wait_seconds.record(stats.storage_wait_time.as_secs_f64());
66    }
67}
68
69/// Cursor metrics for proof task operations.
70#[derive(Clone, Debug)]
71pub struct ProofTaskCursorMetrics {
72    /// Metrics for account trie cursor operations.
73    pub account_trie_cursor: TrieCursorMetrics,
74    /// Metrics for account hashed cursor operations.
75    pub account_hashed_cursor: HashedCursorMetrics,
76    /// Metrics for storage trie cursor operations.
77    pub storage_trie_cursor: TrieCursorMetrics,
78    /// Metrics for storage hashed cursor operations.
79    pub storage_hashed_cursor: HashedCursorMetrics,
80}
81
82impl ProofTaskCursorMetrics {
83    /// Create a new instance with properly initialized cursor metrics.
84    pub fn new() -> Self {
85        Self {
86            account_trie_cursor: TrieCursorMetrics::new(TrieType::State),
87            account_hashed_cursor: HashedCursorMetrics::new(TrieType::State),
88            storage_trie_cursor: TrieCursorMetrics::new(TrieType::Storage),
89            storage_hashed_cursor: HashedCursorMetrics::new(TrieType::Storage),
90        }
91    }
92
93    /// Record the cached metrics from the provided cache and reset the cache counters.
94    ///
95    /// This method adds the current counter values from the cache to the Prometheus metrics
96    /// and then resets all cache counters to zero.
97    pub fn record(&mut self, cache: &mut ProofTaskCursorMetricsCache) {
98        self.account_trie_cursor.record(&mut cache.account_trie_cursor);
99        self.account_hashed_cursor.record(&mut cache.account_hashed_cursor);
100        self.storage_trie_cursor.record(&mut cache.storage_trie_cursor);
101        self.storage_hashed_cursor.record(&mut cache.storage_hashed_cursor);
102        cache.reset();
103    }
104}
105
106impl Default for ProofTaskCursorMetrics {
107    fn default() -> Self {
108        Self::new()
109    }
110}
111
112/// Cached cursor metrics for proof task operations.
113#[derive(Clone, Debug, Default, Copy)]
114pub struct ProofTaskCursorMetricsCache {
115    /// Cached metrics for account trie cursor operations.
116    pub account_trie_cursor: TrieCursorMetricsCache,
117    /// Cached metrics for account hashed cursor operations.
118    pub account_hashed_cursor: HashedCursorMetricsCache,
119    /// Cached metrics for storage trie cursor operations.
120    pub storage_trie_cursor: TrieCursorMetricsCache,
121    /// Cached metrics for storage hashed cursor operations.
122    pub storage_hashed_cursor: HashedCursorMetricsCache,
123}
124
125impl ProofTaskCursorMetricsCache {
126    /// Extend this cache by adding the counts from another cache.
127    ///
128    /// This accumulates the counter values from `other` into this cache.
129    pub fn extend(&mut self, other: &Self) {
130        self.account_trie_cursor.extend(&other.account_trie_cursor);
131        self.account_hashed_cursor.extend(&other.account_hashed_cursor);
132        self.storage_trie_cursor.extend(&other.storage_trie_cursor);
133        self.storage_hashed_cursor.extend(&other.storage_hashed_cursor);
134    }
135
136    /// Reset all counters to zero.
137    pub const fn reset(&mut self) {
138        self.account_trie_cursor.reset();
139        self.account_hashed_cursor.reset();
140        self.storage_trie_cursor.reset();
141        self.storage_hashed_cursor.reset();
142    }
143
144    /// Record the spans for metrics.
145    pub fn record_spans(&self) {
146        self.account_trie_cursor.record_span("account_trie_cursor");
147        self.account_hashed_cursor.record_span("account_hashed_cursor");
148        self.storage_trie_cursor.record_span("storage_trie_cursor");
149        self.storage_hashed_cursor.record_span("storage_hashed_cursor");
150    }
151}