Skip to content

Commit a4cd2cd

Browse files
author
Keefe Liu
committed
add some logs for debug
1 parent 3e06b78 commit a4cd2cd

2 files changed

Lines changed: 90 additions & 80 deletions

File tree

crates/trie/parallel/src/parallel_root.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,27 @@
1-
#[cfg(feature = "metrics")]
2-
use crate::metrics::ParallelStateRootMetrics;
3-
use crate::{stats::ParallelTrieTracker, storage_root_targets::StorageRootTargets};
1+
use std::collections::HashMap;
2+
43
use alloy_rlp::{BufMut, Encodable};
54
use rayon::prelude::*;
5+
use thiserror::Error;
6+
use tracing::*;
7+
68
use reth_db_api::database::Database;
79
use reth_execution_errors::StorageRootError;
810
use reth_primitives::B256;
9-
use reth_provider::{providers::ConsistentDbView, DatabaseProviderFactory, ProviderError};
11+
use reth_provider::{DatabaseProviderFactory, ProviderError, providers::ConsistentDbView};
1012
use reth_trie::{
13+
HashBuilder,
1114
hashed_cursor::{HashedCursorFactory, HashedPostStateCursorFactory},
15+
HashedPostState,
16+
Nibbles,
1217
node_iter::{TrieElement, TrieNodeIter},
13-
trie_cursor::TrieCursorFactory,
14-
updates::TrieUpdates,
15-
walker::TrieWalker,
16-
HashBuilder, HashedPostState, Nibbles, StorageRoot, TrieAccount,
18+
StorageRoot, trie_cursor::TrieCursorFactory, TrieAccount, updates::TrieUpdates, walker::TrieWalker,
1719
};
1820
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
19-
use std::collections::HashMap;
20-
use thiserror::Error;
21-
use tracing::*;
21+
22+
use crate::{stats::ParallelTrieTracker, storage_root_targets::StorageRootTargets};
23+
#[cfg(feature = "metrics")]
24+
use crate::metrics::ParallelStateRootMetrics;
2225

2326
/// Parallel incremental state root calculator.
2427
///
@@ -181,14 +184,14 @@ where
181184
}
182185
}
183186
}
184-
debug!(target: "trie::parallel_state_root", "test info: total elapsed in account node {:?}", start.elapsed());
185-
debug!(target: "trie::parallel_state_root", "test info: hash elapsed in branch node {:?}us", hash_elapsed_branch);
186-
debug!(target: "trie::parallel_state_root", "test info: hash elapsed in leaf node {:?}us", hash_elapsed_leaf);
187-
debug!(target: "trie::parallel_state_root", "test info: hash elapsed in missing node {:?}us", hash_elapsed_miss);
187+
debug!(target: "trie::parallel_state_root", "test info: total time elapsed {:?}", start.elapsed());
188+
debug!(target: "trie::parallel_state_root", "test info: time elapsed in missing leaves {:?}us", hash_elapsed_miss);
189+
// debug!(target: "trie::parallel_state_root", "test info: time elapsed in hash branch node {:?}us", hash_elapsed_branch);
190+
// debug!(target: "trie::parallel_state_root", "test info: time elapsed in hash leaf node {:?}us", hash_elapsed_leaf);
188191

189192
let start = std::time::Instant::now();
190193
let root = hash_builder.root();
191-
debug!(target: "trie::parallel_state_root", "test info: elapsed in hash builder root {:?}", start.elapsed());
194+
debug!(target: "trie::parallel_state_root", "test info: total time elapsed in hash builder {:?}us", start.elapsed().as_micros()+hash_elapsed_branch+hash_elapsed_leaf);
192195

193196
trie_updates.finalize(
194197
account_node_iter.walker,
@@ -240,11 +243,13 @@ impl From<ParallelStateRootError> for ProviderError {
240243

241244
#[cfg(test)]
242245
mod tests {
243-
use super::*;
244246
use rand::Rng;
245-
use reth_primitives::{keccak256, Account, Address, StorageEntry, U256};
246-
use reth_provider::{test_utils::create_test_provider_factory, HashingWriter};
247-
use reth_trie::{test_utils, HashedStorage};
247+
248+
use reth_primitives::{Account, Address, keccak256, StorageEntry, U256};
249+
use reth_provider::{HashingWriter, test_utils::create_test_provider_factory};
250+
use reth_trie::{HashedStorage, test_utils};
251+
252+
use super::*;
248253

249254
#[tokio::test]
250255
async fn random_parallel_root() {

crates/trie/prefetch/src/prefetch.rs

Lines changed: 65 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,28 @@
1+
use std::{collections::HashMap, sync::Arc};
2+
13
use rayon::prelude::*;
4+
use thiserror::Error;
5+
use tokio::{
6+
sync::{mpsc::UnboundedReceiver, Mutex, oneshot::Receiver},
7+
task::JoinSet,
8+
};
9+
use tracing::{debug, trace};
10+
211
use reth_db::database::Database;
312
use reth_execution_errors::StorageRootError;
4-
use reth_primitives::{revm_primitives::EvmState, B256};
5-
use reth_provider::{providers::ConsistentDbView, ProviderError, ProviderFactory};
13+
use reth_primitives::{B256, revm_primitives::EvmState};
14+
use reth_provider::{ProviderError, ProviderFactory, providers::ConsistentDbView};
615
use reth_trie::{
716
hashed_cursor::{HashedCursorFactory, HashedPostStateCursorFactory},
17+
HashedPostState,
18+
HashedStorage,
819
metrics::TrieRootMetrics,
920
node_iter::{TrieElement, TrieNodeIter},
1021
stats::TrieTracker,
11-
trie_cursor::TrieCursorFactory,
12-
walker::TrieWalker,
13-
HashedPostState, HashedStorage, StorageRoot,
22+
StorageRoot, trie_cursor::TrieCursorFactory, walker::TrieWalker,
1423
};
1524
use reth_trie_db::{DatabaseHashedCursorFactory, DatabaseTrieCursorFactory};
1625
use reth_trie_parallel::{parallel_root::ParallelStateRootError, StorageRootTargets};
17-
use std::{collections::HashMap, sync::Arc};
18-
use thiserror::Error;
19-
use tokio::{
20-
sync::{mpsc::UnboundedReceiver, oneshot::Receiver, Mutex},
21-
task::JoinSet,
22-
};
23-
use tracing::{debug, trace};
2426

2527
/// Prefetch trie storage when executing transactions.
2628
#[derive(Debug, Clone)]
@@ -37,9 +39,9 @@ pub struct TriePrefetch {
3739

3840
#[derive(Default, Debug)]
3941
pub struct GlobalStats {
40-
pub branch_count: usize,
41-
pub leaf_count: usize,
42-
pub missing_count: usize,
42+
pub branch_prefetched: u64,
43+
pub leaves_prefetched: u64,
44+
pub missed_leaves_prefetched: u64,
4345
}
4446

4547
impl Default for TriePrefetch {
@@ -79,18 +81,18 @@ impl TriePrefetch {
7981
let hashed_state = self.deduplicate_and_update_cached(state);
8082

8183
let self_clone = Arc::new(self.clone());
82-
// let global_stats = Arc::clone(&self.global_stats);
84+
let global_stats = Arc::clone(&self.global_stats);
8385
join_set.spawn(async move {
84-
if let Err(e) = self_clone.prefetch_once::<DB>(consistent_view, hashed_state).await {
86+
if let Err(e) = self_clone.prefetch_once::<DB>(consistent_view, hashed_state, global_stats).await {
8587
debug!(target: "trie::trie_prefetch", ?e, "Error while prefetching trie storage");
8688
};
8789
});
8890
}
8991
}
9092
_ = &mut interrupt_rx => {
91-
debug!(target: "trie::trie_prefetch", "Interrupted trie prefetch task. Unprocessed tx {:?}", prefetch_rx.len());
93+
debug!(target: "trie::trie_prefetch", "Interrupted trie prefetch task. Unprocessed tx {:?}, Processed accounts: {:?}", prefetch_rx.len(), self.cached_accounts.len());
9294
join_set.abort_all();
93-
// debug!(target: "trie::trie_prefetch", "test info: prefetch trie node count: {:?}", self.global_stats.lock().await);
95+
debug!(target: "trie::trie_prefetch", "test info: prefetch account trie node count: {:?}", self.global_stats.lock().await);
9496
return
9597
}
9698
}
@@ -104,45 +106,46 @@ impl TriePrefetch {
104106

105107
// deduplicate accounts if their keys are not present in storages
106108
for (address, account) in &hashed_state.accounts {
107-
if !hashed_state.storages.contains_key(address) &&
108-
!self.cached_accounts.contains_key(address)
109+
// if !hashed_state.storages.contains_key(address) &&
110+
// !self.cached_accounts.contains_key(address)
111+
if !self.cached_accounts.contains_key(address)
109112
{
110113
self.cached_accounts.insert(*address, true);
111114
new_hashed_state.accounts.insert(*address, *account);
112115
}
113116
}
114117

115118
// deduplicate storages
116-
for (address, storage) in &hashed_state.storages {
117-
let cached_entry = self.cached_storages.entry(*address).or_default();
118-
119-
// Collect the keys to be added to `new_storage` after filtering
120-
let keys_to_add: Vec<_> = storage
121-
.storage
122-
.iter()
123-
.filter(|(slot, _)| !cached_entry.contains_key(*slot))
124-
.map(|(slot, _)| *slot)
125-
.collect();
126-
127-
// Iterate over `keys_to_add` to update `cached_entry` and `new_storage`
128-
let new_storage: HashMap<_, _> = keys_to_add
129-
.into_iter()
130-
.map(|slot| {
131-
cached_entry.insert(slot, true);
132-
(slot, *storage.storage.get(&slot).unwrap())
133-
})
134-
.collect();
135-
136-
if !new_storage.is_empty() {
137-
new_hashed_state
138-
.storages
139-
.insert(*address, HashedStorage::from_iter(false, new_storage.into_iter()));
140-
141-
if let Some(account) = hashed_state.accounts.get(address) {
142-
new_hashed_state.accounts.insert(*address, *account);
143-
}
144-
}
145-
}
119+
// for (address, storage) in &hashed_state.storages {
120+
// let cached_entry = self.cached_storages.entry(*address).or_default();
121+
//
122+
// // Collect the keys to be added to `new_storage` after filtering
123+
// let keys_to_add: Vec<_> = storage
124+
// .storage
125+
// .iter()
126+
// .filter(|(slot, _)| !cached_entry.contains_key(*slot))
127+
// .map(|(slot, _)| *slot)
128+
// .collect();
129+
//
130+
// // Iterate over `keys_to_add` to update `cached_entry` and `new_storage`
131+
// let new_storage: HashMap<_, _> = keys_to_add
132+
// .into_iter()
133+
// .map(|slot| {
134+
// cached_entry.insert(slot, true);
135+
// (slot, *storage.storage.get(&slot).unwrap())
136+
// })
137+
// .collect();
138+
//
139+
// if !new_storage.is_empty() {
140+
// new_hashed_state
141+
// .storages
142+
// .insert(*address, HashedStorage::from_iter(false, new_storage.into_iter()));
143+
//
144+
// if let Some(account) = hashed_state.accounts.get(address) {
145+
// new_hashed_state.accounts.insert(*address, *account);
146+
// }
147+
// }
148+
// }
146149

147150
new_hashed_state
148151
}
@@ -152,12 +155,13 @@ impl TriePrefetch {
152155
self: Arc<Self>,
153156
consistent_view: Arc<ConsistentDbView<DB, ProviderFactory<DB>>>,
154157
hashed_state: HashedPostState,
155-
// global_stats: Arc<Mutex<GlobalStats>>,
158+
global_stats: Arc<Mutex<GlobalStats>>,
156159
) -> Result<(), TriePrefetchError>
157160
where
158161
DB: Database,
159162
{
160163
let mut tracker = TrieTracker::default();
164+
let mut leaves_missed = 0u64;
161165

162166
let prefix_sets = hashed_state.construct_prefix_sets().freeze();
163167
let storage_root_targets = StorageRootTargets::new(
@@ -213,21 +217,16 @@ impl TriePrefetch {
213217
match node {
214218
TrieElement::Branch(_) => {
215219
tracker.inc_branch();
216-
// let mut stats = global_stats.lock().await;
217-
// stats.branch_count += 1;
218220
}
219221
TrieElement::Leaf(hashed_address, _) => {
220222
match storage_roots.remove(&hashed_address) {
221223
Some(result) => {
222-
// let mut stats = global_stats.lock().await;
223-
// stats.leaf_count += 1;
224224
result
225225
}
226226
// Since we do not store all intermediate nodes in the database, there might
227227
// be a possibility of re-adding a non-modified leaf to the hash builder.
228228
None => {
229-
// let mut stats = global_stats.lock().await;
230-
// stats.missing_count += 1;
229+
leaves_missed += 1;
231230

232231
StorageRoot::new_hashed(
233232
trie_cursor_factory.clone(),
@@ -251,11 +250,17 @@ impl TriePrefetch {
251250
#[cfg(feature = "metrics")]
252251
self.metrics.record(stats);
253252

254-
trace!(
253+
let mut gstats = global_stats.lock().await;
254+
gstats.branch_prefetched += stats.branches_added();
255+
gstats.leaves_prefetched += stats.leaves_added() - leaves_missed;
256+
gstats.missed_leaves_prefetched += leaves_missed;
257+
258+
debug!(
255259
target: "trie::trie_prefetch",
256260
duration = ?stats.duration(),
257261
branches_added = stats.branches_added(),
258-
leaves_added = stats.leaves_added(),
262+
leaves_added = stats.leaves_added()-leaves_missed,
263+
leaves_missed = leaves_missed,
259264
"prefetched account trie"
260265
);
261266

0 commit comments

Comments
 (0)