feat(trie): parallel proof metrics (#14633)

This commit is contained in:
Alexey Shekhirin
2025-02-22 11:25:49 +00:00
committed by GitHub
parent 107ab4fb79
commit d39e24d9f3
3 changed files with 35 additions and 6 deletions

View File

@ -18,7 +18,7 @@ impl Default for ParallelStateRootMetrics {
fn default() -> Self { fn default() -> Self {
Self { Self {
state_trie: TrieRootMetrics::new(TrieType::State), state_trie: TrieRootMetrics::new(TrieType::State),
parallel: ParallelTrieMetrics::default(), parallel: ParallelTrieMetrics::new_with_labels(&[("type", "root")]),
storage_trie: TrieRootMetrics::new(TrieType::Storage), storage_trie: TrieRootMetrics::new(TrieType::Storage),
} }
} }
@ -28,8 +28,7 @@ impl ParallelStateRootMetrics {
/// Record state trie metrics /// Record state trie metrics
pub fn record_state_trie(&self, stats: ParallelTrieStats) { pub fn record_state_trie(&self, stats: ParallelTrieStats) {
self.state_trie.record(stats.trie_stats()); self.state_trie.record(stats.trie_stats());
self.parallel.precomputed_storage_roots.record(stats.precomputed_storage_roots() as f64); self.parallel.record(stats);
self.parallel.missed_leaves.record(stats.missed_leaves() as f64);
} }
} }
@ -42,3 +41,11 @@ pub struct ParallelTrieMetrics {
/// The number of leaves for which we did not pre-compute the storage roots. /// The number of leaves for which we did not pre-compute the storage roots.
pub missed_leaves: Histogram, pub missed_leaves: Histogram,
} }
impl ParallelTrieMetrics {
/// Record parallel trie metrics.
pub fn record(&self, stats: ParallelTrieStats) {
self.precomputed_storage_roots.record(stats.precomputed_storage_roots() as f64);
self.missed_leaves.record(stats.missed_leaves() as f64);
}
}

View File

@ -1,4 +1,7 @@
use crate::{root::ParallelStateRootError, stats::ParallelTrieTracker, StorageRootTargets}; use crate::{
metrics::ParallelTrieMetrics, root::ParallelStateRootError, stats::ParallelTrieTracker,
StorageRootTargets,
};
use alloy_primitives::{ use alloy_primitives::{
map::{B256Map, HashMap}, map::{B256Map, HashMap},
B256, B256,
@ -48,11 +51,13 @@ pub struct ParallelProof<Factory> {
collect_branch_node_masks: bool, collect_branch_node_masks: bool,
/// Thread pool for local tasks /// Thread pool for local tasks
thread_pool: Arc<rayon::ThreadPool>, thread_pool: Arc<rayon::ThreadPool>,
#[cfg(feature = "metrics")]
metrics: ParallelTrieMetrics,
} }
impl<Factory> ParallelProof<Factory> { impl<Factory> ParallelProof<Factory> {
/// Create new state proof generator. /// Create new state proof generator.
pub const fn new( pub fn new(
view: ConsistentDbView<Factory>, view: ConsistentDbView<Factory>,
nodes_sorted: Arc<TrieUpdatesSorted>, nodes_sorted: Arc<TrieUpdatesSorted>,
state_sorted: Arc<HashedPostStateSorted>, state_sorted: Arc<HashedPostStateSorted>,
@ -66,6 +71,8 @@ impl<Factory> ParallelProof<Factory> {
prefix_sets, prefix_sets,
collect_branch_node_masks: false, collect_branch_node_masks: false,
thread_pool, thread_pool,
#[cfg(feature = "metrics")]
metrics: ParallelTrieMetrics::new_with_labels(&[("type", "proof")]),
} }
} }
@ -291,6 +298,10 @@ where
} }
let _ = hash_builder.root(); let _ = hash_builder.root();
let stats = tracker.finish();
#[cfg(feature = "metrics")]
self.metrics.record(stats);
let account_subtree = hash_builder.take_proof_nodes(); let account_subtree = hash_builder.take_proof_nodes();
let (branch_node_hash_masks, branch_node_tree_masks) = if self.collect_branch_node_masks { let (branch_node_hash_masks, branch_node_tree_masks) = if self.collect_branch_node_masks {
let updated_branch_nodes = hash_builder.updated_branch_nodes.unwrap_or_default(); let updated_branch_nodes = hash_builder.updated_branch_nodes.unwrap_or_default();
@ -308,6 +319,17 @@ where
(HashMap::default(), HashMap::default()) (HashMap::default(), HashMap::default())
}; };
debug!(
target: "trie::parallel_proof",
total_targets = storage_root_targets_len,
duration = ?stats.duration(),
branches_added = stats.branches_added(),
leaves_added = stats.leaves_added(),
missed_leaves = stats.missed_leaves(),
precomputed_storage_roots = stats.precomputed_storage_roots(),
"Calculated proof"
);
Ok(MultiProof { account_subtree, branch_node_hash_masks, branch_node_tree_masks, storages }) Ok(MultiProof { account_subtree, branch_node_hash_masks, branch_node_tree_masks, storages })
} }
} }

View File

@ -217,7 +217,7 @@ where
leaves_added = stats.leaves_added(), leaves_added = stats.leaves_added(),
missed_leaves = stats.missed_leaves(), missed_leaves = stats.missed_leaves(),
precomputed_storage_roots = stats.precomputed_storage_roots(), precomputed_storage_roots = stats.precomputed_storage_roots(),
"calculated state root" "Calculated state root"
); );
Ok((root, trie_updates)) Ok((root, trie_updates))