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