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