Skip to content

Commit 9a4f6b6

Browse files
authored
Preftecher metric (#609)
1 parent fe99a06 commit 9a4f6b6

File tree

5 files changed

+66
-14
lines changed

5 files changed

+66
-14
lines changed

crates/eth-sparse-mpt/src/reth_sparse_trie/mod.rs

+18-2
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,40 @@ pub fn prefetch_tries_for_accounts<'a, Provider>(
8080
consistent_db_view: ConsistentDbView<Provider>,
8181
shared_cache: SparseTrieSharedCache,
8282
changed_data: impl Iterator<Item = &'a ChangedAccountData>,
83-
) -> Result<(), SparseTrieError>
83+
) -> Result<SparseTrieMetrics, SparseTrieError>
8484
where
8585
Provider: DatabaseProviderFactory<Provider: BlockReader> + Send + Sync,
8686
Provider: StateCommitmentProvider,
8787
{
88+
let mut metrics = SparseTrieMetrics::default();
89+
90+
let start = Instant::now();
8891
let change_set = prepare_change_set_for_prefetch(changed_data);
92+
metrics.change_set_time += start.elapsed();
8993

9094
let fetcher = TrieFetcher::new(consistent_db_view);
9195

9296
for _ in 0..3 {
97+
let start = Instant::now();
9398
let gather_result = shared_cache.gather_tries_for_changes(&change_set);
99+
metrics.gather_nodes_time += start.elapsed();
94100

95101
let missing_nodes = match gather_result {
96-
Ok(_) => return Ok(()),
102+
Ok(_) => return Ok(metrics),
97103
Err(missing_nodes) => missing_nodes,
98104
};
105+
metrics.missing_nodes += missing_nodes.len();
106+
107+
let start = Instant::now();
99108
let multiproof = fetcher.fetch_missing_nodes(missing_nodes)?;
109+
110+
metrics.fetch_iterations += 1;
111+
metrics.fetch_nodes_time += start.elapsed();
112+
metrics.fetched_nodes += multiproof.len();
113+
114+
let start = Instant::now();
100115
shared_cache.update_cache_with_fetched_nodes(multiproof)?;
116+
metrics.fill_cache_time += start.elapsed();
101117
}
102118

103119
Err(SparseTrieError::FailedToFetchData)

crates/rbuilder/src/building/testing/bundle_tests/mod.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
11
pub mod setup;
22

3-
use alloy_primitives::{Address, B256, U256};
3+
use alloy_primitives::{B256, U256};
44
use itertools::Itertools;
55
use std::collections::HashSet;
6-
use uuid::Uuid;
76

87
use crate::{
98
building::{
109
testing::bundle_tests::setup::NonceValue, BuiltBlockTrace, BundleErr, OrderErr,
1110
TransactionErr,
1211
},
13-
primitives::{
14-
Bundle, BundleRefund, BundleReplacementData, BundleReplacementKey, Order, OrderId, Refund,
15-
RefundConfig, TxRevertBehavior,
16-
},
12+
primitives::{Bundle, BundleRefund, Order, OrderId, Refund, RefundConfig, TxRevertBehavior},
1713
utils::{constants::BASE_TX_GAS, int_percentage},
1814
};
1915

crates/rbuilder/src/roothash/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use tracing::trace;
2020

2121
pub use prefetcher::run_trie_prefetcher;
2222

23+
use crate::telemetry::inc_root_hash_finalize_count;
24+
2325
#[derive(Debug, Clone, Copy)]
2426
pub enum RootHashMode {
2527
/// Makes correct root hash calculation on the correct parent state.
@@ -156,6 +158,7 @@ where
156158
sparse_trie_shared_cache,
157159
&config.thread_pool,
158160
);
161+
inc_root_hash_finalize_count(metrics.fetched_nodes);
159162
trace!(?metrics, "Sparse trie metrics");
160163
root?
161164
} else {

crates/rbuilder/src/roothash/prefetcher.rs

+12-6
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ use tokio::sync::broadcast::{
1818
use tokio_util::sync::CancellationToken;
1919
use tracing::{error, trace, warn};
2020

21-
use crate::{building::evm_inspector::SlotKey, live_builder::simulation::SimulatedOrderCommand};
21+
use crate::{
22+
building::evm_inspector::SlotKey, live_builder::simulation::SimulatedOrderCommand,
23+
telemetry::inc_root_hash_prefetch_count, utils::elapsed_ms,
24+
};
2225

2326
const CONSUME_SIM_ORDERS_BATCH: usize = 128;
2427

@@ -143,12 +146,12 @@ pub fn run_trie_prefetcher<P>(
143146
}
144147

145148
let start = Instant::now();
146-
match prefetch_tries_for_accounts(
149+
let metrics = match prefetch_tries_for_accounts(
147150
consistent_db_view.clone(),
148151
shared_sparse_mpt_cache.clone(),
149152
fetch_request.values(),
150153
) {
151-
Ok(()) => {}
154+
Ok(metrics) => metrics,
152155
Err(SparseTrieError::FetchNode(FetchNodeError::Provider(
153156
ProviderError::ConsistentView(_),
154157
))) => {
@@ -158,11 +161,14 @@ pub fn run_trie_prefetcher<P>(
158161
if !err.is_db_consistency_error() {
159162
error!(?err, "Error while prefetching trie nodes");
160163
}
164+
continue;
161165
}
162-
}
166+
};
167+
inc_root_hash_prefetch_count(metrics.fetched_nodes);
168+
163169
trace!(
164-
time_ms = start.elapsed().as_millis(),
165-
accounts = fetch_request.len(),
170+
time_ms = elapsed_ms(start),
171+
?metrics,
166172
"Prefetched trie nodes"
167173
);
168174
}

crates/rbuilder/src/telemetry/metrics/mod.rs

+31
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ const RELAY_ERROR_OTHER: &str = "other";
4343
const SIM_STATUS_OK: &str = "sim_success";
4444
const SIM_STATUS_FAIL: &str = "sim_fail";
4545

46+
const ROOT_HASH_PREFETCH_STEP: &str = "prefetcher";
47+
const ROOT_HASH_FINALIZE_STEP: &str = "finalize";
48+
4649
/// We record timestamps only for blocks built within interval of the block timestamp
4750
const BLOCK_METRICS_TIMESTAMP_LOWER_DELTA: time::Duration = time::Duration::seconds(3);
4851
/// We record timestamps only for blocks built within interval of the block timestamp
@@ -100,6 +103,16 @@ register_metrics! {
100103
.unwrap();
101104

102105

106+
pub static ROOT_HASH_FETCHES: IntCounterVec = IntCounterVec::new(
107+
Opts::new(
108+
"rbuilder_sparse_mpt_root_hash_fetches",
109+
"Number of nodes fetched in a finalize or prefetch step"
110+
),
111+
&["step"],
112+
)
113+
.unwrap();
114+
115+
103116

104117
pub static CURRENT_BLOCK: IntGauge =
105118
IntGauge::new("current_block", "Current Block").unwrap();
@@ -580,6 +593,24 @@ pub fn mark_submission_start_time(block_sealed_at: OffsetDateTime) {
580593
.observe(value);
581594
}
582595

596+
pub fn inc_root_hash_prefetch_count(fetched_nodes: usize) {
597+
if fetched_nodes == 0 {
598+
return;
599+
}
600+
ROOT_HASH_FETCHES
601+
.with_label_values(&[ROOT_HASH_PREFETCH_STEP])
602+
.inc_by(fetched_nodes.try_into().unwrap_or_default());
603+
}
604+
605+
pub fn inc_root_hash_finalize_count(fetched_nodes: usize) {
606+
if fetched_nodes == 0 {
607+
return;
608+
}
609+
ROOT_HASH_FETCHES
610+
.with_label_values(&[ROOT_HASH_FINALIZE_STEP])
611+
.inc_by(fetched_nodes.try_into().unwrap_or_default());
612+
}
613+
583614
pub fn gather_prometheus_metrics(registry: &Registry) -> String {
584615
use prometheus::Encoder;
585616
let encoder = prometheus::TextEncoder::new();

0 commit comments

Comments
 (0)