diff --git a/Cargo.lock b/Cargo.lock index 4a95e046c2..d191766a5b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7976,6 +7976,7 @@ dependencies = [ "page_size", "parking_lot", "proptest", + "quanta", "reth-db-api", "reth-fs-util", "reth-libmdbx", @@ -9601,6 +9602,7 @@ dependencies = [ "op-alloy-consensus", "proptest", "proptest-arbitrary-interop", + "quanta", "rand 0.8.5", "rand 0.9.2", "rayon", @@ -10355,6 +10357,7 @@ dependencies = [ "futures-util", "metrics", "pin-project", + "quanta", "rayon", "reth-metrics", "thiserror 2.0.18", diff --git a/Cargo.toml b/Cargo.toml index 0b4060d563..1635818baf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -528,6 +528,7 @@ notify = { version = "8.0.0", default-features = false, features = ["macos_fseve nybbles = { version = "0.4.8", default-features = false } once_cell = { version = "1.19", default-features = false, features = ["critical-section"] } parking_lot = "0.12" +quanta = "0.12" paste = "1.0" rand = "0.9" rayon = "1.7" diff --git a/crates/engine/tree/src/persistence.rs b/crates/engine/tree/src/persistence.rs index 59e567dc0a..739fbb3d18 100644 --- a/crates/engine/tree/src/persistence.rs +++ b/crates/engine/tree/src/persistence.rs @@ -4,7 +4,7 @@ use crossbeam_channel::Sender as CrossbeamSender; use reth_chain_state::ExecutedBlock; use reth_errors::ProviderError; use reth_ethereum_primitives::EthPrimitives; -use reth_primitives_traits::NodePrimitives; +use reth_primitives_traits::{FastInstant as Instant, NodePrimitives}; use reth_provider::{ providers::ProviderNodeTypes, BlockExecutionWriter, BlockHashReader, ChainStateBlockWriter, DBProvider, DatabaseProviderFactory, ProviderFactory, SaveBlocksMode, @@ -18,7 +18,6 @@ use std::{ Arc, }, thread::JoinHandle, - time::Instant, }; use thiserror::Error; use tracing::{debug, error, instrument}; diff --git a/crates/engine/tree/src/tree/instrumented_state.rs b/crates/engine/tree/src/tree/instrumented_state.rs index 99c61e533e..09f15f5627 100644 --- a/crates/engine/tree/src/tree/instrumented_state.rs +++ b/crates/engine/tree/src/tree/instrumented_state.rs @@ -3,7 +3,7 @@ use alloy_primitives::{Address, StorageKey, StorageValue, B256}; use metrics::{Gauge, Histogram}; use reth_errors::ProviderResult; use reth_metrics::Metrics; -use reth_primitives_traits::{Account, Bytecode}; +use reth_primitives_traits::{Account, Bytecode, FastInstant as Instant}; use reth_provider::{ AccountReader, BlockHashReader, BytecodeReader, HashedPostStateProvider, StateProofProvider, StateProvider, StateRootProvider, StorageRootProvider, @@ -14,7 +14,7 @@ use reth_trie::{ }; use std::{ sync::atomic::{AtomicU64, Ordering}, - time::{Duration, Instant}, + time::Duration, }; /// Nanoseconds per second diff --git a/crates/engine/tree/src/tree/metrics.rs b/crates/engine/tree/src/tree/metrics.rs index d08b42123c..0f5c26c932 100644 --- a/crates/engine/tree/src/tree/metrics.rs +++ b/crates/engine/tree/src/tree/metrics.rs @@ -8,9 +8,9 @@ use reth_metrics::{ metrics::{Counter, Gauge, Histogram}, Metrics, }; -use reth_primitives_traits::constants::gas_units::MEGAGAS; +use reth_primitives_traits::{constants::gas_units::MEGAGAS, FastInstant as Instant}; use reth_trie::updates::TrieUpdates; -use std::time::{Duration, Instant}; +use std::time::Duration; /// Upper bounds for each gas bucket. The last bucket is a catch-all for /// everything above the final threshold: <5M, 5-10M, 10-20M, 20-30M, 30-40M, >40M. diff --git a/crates/engine/tree/src/tree/mod.rs b/crates/engine/tree/src/tree/mod.rs index bced266835..55aa1e525c 100644 --- a/crates/engine/tree/src/tree/mod.rs +++ b/crates/engine/tree/src/tree/mod.rs @@ -27,7 +27,9 @@ use reth_payload_builder::PayloadBuilderHandle; use reth_payload_primitives::{ BuiltPayload, EngineApiMessageVersion, NewPayloadError, PayloadBuilderAttributes, PayloadTypes, }; -use reth_primitives_traits::{NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader}; +use reth_primitives_traits::{ + FastInstant as Instant, NodePrimitives, RecoveredBlock, SealedBlock, SealedHeader, +}; use reth_provider::{ BlockExecutionOutput, BlockExecutionResult, BlockReader, ChangeSetReader, DatabaseProviderFactory, HashedPostStateProvider, ProviderError, StageCheckpointReader, @@ -40,7 +42,7 @@ use reth_tasks::spawn_os_thread; use reth_trie_db::ChangesetCache; use revm::interpreter::debug_unreachable; use state::TreeState; -use std::{fmt::Debug, ops, sync::Arc, time::Instant}; +use std::{fmt::Debug, ops, sync::Arc}; use crossbeam_channel::{Receiver, Sender}; use tokio::sync::{ diff --git a/crates/engine/tree/src/tree/payload_processor/mod.rs b/crates/engine/tree/src/tree/payload_processor/mod.rs index 64ce3c4cf0..a509ede133 100644 --- a/crates/engine/tree/src/tree/payload_processor/mod.rs +++ b/crates/engine/tree/src/tree/payload_processor/mod.rs @@ -27,7 +27,7 @@ use reth_evm::{ SpecFor, TxEnvFor, }; use reth_metrics::Metrics; -use reth_primitives_traits::NodePrimitives; +use reth_primitives_traits::{FastInstant as Instant, NodePrimitives}; use reth_provider::{ BlockExecutionOutput, BlockReader, DatabaseProviderROFactory, StateProvider, StateProviderFactory, StateReader, @@ -49,7 +49,6 @@ use std::{ mpsc::{self, channel}, Arc, }, - time::Instant, }; use tracing::{debug, debug_span, instrument, warn, Span}; @@ -611,7 +610,7 @@ where let _enter = debug_span!(target: "engine::tree::payload_processor", "preserve").entered(); let deferred = if let Some(state_root) = computed_state_root { - let start = std::time::Instant::now(); + let start = Instant::now(); let (trie, deferred) = task.into_trie_for_reuse( prune_depth, max_storage_tries, diff --git a/crates/engine/tree/src/tree/payload_processor/multiproof.rs b/crates/engine/tree/src/tree/payload_processor/multiproof.rs index 1e89a8c706..8dd5fc58e7 100644 --- a/crates/engine/tree/src/tree/payload_processor/multiproof.rs +++ b/crates/engine/tree/src/tree/payload_processor/multiproof.rs @@ -8,6 +8,7 @@ use crossbeam_channel::{unbounded, Receiver as CrossbeamReceiver, Sender as Cros use derive_more::derive::Deref; use metrics::{Gauge, Histogram}; use reth_metrics::Metrics; +use reth_primitives_traits::FastInstant as Instant; use reth_provider::AccountReader; use reth_revm::state::EvmState; use reth_trie::{ @@ -25,7 +26,7 @@ use reth_trie_parallel::{ targets_v2::MultiProofTargetsV2, }; use revm_primitives::map::{hash_map, B256Map}; -use std::{collections::BTreeMap, sync::Arc, time::Instant}; +use std::{collections::BTreeMap, sync::Arc}; use tracing::{debug, error, instrument, trace}; /// Source of state changes, either from EVM execution or from a Block Access List. diff --git a/crates/engine/tree/src/tree/payload_processor/prewarm.rs b/crates/engine/tree/src/tree/payload_processor/prewarm.rs index 8881ef9742..c0a944a2e6 100644 --- a/crates/engine/tree/src/tree/payload_processor/prewarm.rs +++ b/crates/engine/tree/src/tree/payload_processor/prewarm.rs @@ -31,7 +31,7 @@ use metrics::{Counter, Gauge, Histogram}; use rayon::prelude::*; use reth_evm::{execute::ExecutableTxFor, ConfigureEvm, Evm, EvmFor, RecoveredTx, SpecFor}; use reth_metrics::Metrics; -use reth_primitives_traits::NodePrimitives; +use reth_primitives_traits::{FastInstant as Instant, NodePrimitives}; use reth_provider::{ AccountReader, BlockExecutionOutput, BlockReader, StateProvider, StateProviderFactory, StateReader, @@ -39,13 +39,10 @@ use reth_provider::{ use reth_revm::{database::StateProviderDatabase, state::EvmState}; use reth_tasks::Runtime; use reth_trie::MultiProofTargets; -use std::{ - sync::{ - atomic::{AtomicBool, Ordering}, - mpsc::{self, channel, Receiver, Sender, SyncSender}, - Arc, - }, - time::Instant, +use std::sync::{ + atomic::{AtomicBool, Ordering}, + mpsc::{self, channel, Receiver, Sender, SyncSender}, + Arc, }; use tracing::{debug, debug_span, instrument, trace, warn, Span}; diff --git a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs index f42fe9a39b..4296da1b2e 100644 --- a/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs +++ b/crates/engine/tree/src/tree/payload_processor/sparse_trie.rs @@ -11,7 +11,7 @@ use alloy_primitives::B256; use alloy_rlp::{Decodable, Encodable}; use crossbeam_channel::{Receiver as CrossbeamReceiver, Sender as CrossbeamSender}; use rayon::iter::ParallelIterator; -use reth_primitives_traits::{Account, ParallelBridgeBuffered}; +use reth_primitives_traits::{Account, FastInstant as Instant, ParallelBridgeBuffered}; use reth_tasks::Runtime; use reth_trie::{ proof_v2::Target, updates::TrieUpdates, DecodedMultiProofV2, HashedPostState, Nibbles, @@ -32,10 +32,7 @@ use reth_trie_sparse::{ }; use revm_primitives::{hash_map::Entry, B256Map}; use smallvec::SmallVec; -use std::{ - sync::mpsc, - time::{Duration, Instant}, -}; +use std::{sync::mpsc, time::Duration}; use tracing::{debug, debug_span, error, instrument, trace}; #[expect(clippy::large_enum_variant)] diff --git a/crates/engine/tree/src/tree/payload_validator.rs b/crates/engine/tree/src/tree/payload_validator.rs index d82bbbf5c1..0958c25ec0 100644 --- a/crates/engine/tree/src/tree/payload_validator.rs +++ b/crates/engine/tree/src/tree/payload_validator.rs @@ -31,8 +31,8 @@ use reth_payload_primitives::{ BuiltPayload, InvalidPayloadAttributesError, NewPayloadError, PayloadTypes, }; use reth_primitives_traits::{ - AlloyBlockHeader, BlockBody, BlockTy, GotExpected, NodePrimitives, RecoveredBlock, SealedBlock, - SealedHeader, SignerRecoverable, + AlloyBlockHeader, BlockBody, BlockTy, FastInstant as Instant, GotExpected, NodePrimitives, + RecoveredBlock, SealedBlock, SealedHeader, SignerRecoverable, }; use reth_provider::{ providers::OverlayStateProviderFactory, BlockExecutionOutput, BlockNumReader, BlockReader, @@ -49,7 +49,6 @@ use std::{ collections::HashMap, panic::{self, AssertUnwindSafe}, sync::{mpsc::RecvTimeoutError, Arc}, - time::Instant, }; use tracing::{debug, debug_span, error, info, instrument, trace, warn}; diff --git a/crates/engine/tree/src/tree/persistence_state.rs b/crates/engine/tree/src/tree/persistence_state.rs index 847904c0dd..68827203e0 100644 --- a/crates/engine/tree/src/tree/persistence_state.rs +++ b/crates/engine/tree/src/tree/persistence_state.rs @@ -23,7 +23,7 @@ use alloy_eips::BlockNumHash; use alloy_primitives::B256; use crossbeam_channel::Receiver as CrossbeamReceiver; -use std::time::Instant; +use reth_primitives_traits::FastInstant as Instant; use tracing::trace; /// The state of the persistence task. diff --git a/crates/evm/evm/src/metrics.rs b/crates/evm/evm/src/metrics.rs index ae180cad29..8bb67114a5 100644 --- a/crates/evm/evm/src/metrics.rs +++ b/crates/evm/evm/src/metrics.rs @@ -2,8 +2,7 @@ use alloy_consensus::BlockHeader; use metrics::{Counter, Gauge, Histogram}; use reth_metrics::Metrics; -use reth_primitives_traits::{Block, RecoveredBlock}; -use std::time::Instant; +use reth_primitives_traits::{Block, FastInstant as Instant, RecoveredBlock}; /// Executor metrics. #[derive(Metrics, Clone)] diff --git a/crates/primitives-traits/Cargo.toml b/crates/primitives-traits/Cargo.toml index 18ee210fc5..18f73d945d 100644 --- a/crates/primitives-traits/Cargo.toml +++ b/crates/primitives-traits/Cargo.toml @@ -39,6 +39,7 @@ bytes.workspace = true dashmap = { workspace = true, features = ["inline"], optional = true } derive_more.workspace = true once_cell.workspace = true +quanta = { workspace = true, optional = true } serde_with = { workspace = true, optional = true } thiserror.workspace = true @@ -85,6 +86,7 @@ std = [ "bytes/std", "derive_more/std", "once_cell/std", + "quanta", "secp256k1?/std", "thiserror/std", "alloy-trie/std", diff --git a/crates/primitives-traits/src/lib.rs b/crates/primitives-traits/src/lib.rs index e9caa35517..67a0e913e9 100644 --- a/crates/primitives-traits/src/lib.rs +++ b/crates/primitives-traits/src/lib.rs @@ -117,6 +117,10 @@ #[macro_use] extern crate alloc; +/// Re-export of [`quanta::Instant`] for high-resolution timing with minimal overhead. +#[cfg(feature = "std")] +pub use quanta::Instant as FastInstant; + /// Common constants. pub mod constants; pub use constants::gas_units::{format_gas, format_gas_throughput}; diff --git a/crates/prune/prune/src/pruner.rs b/crates/prune/prune/src/pruner.rs index c445e52f12..5296959ab1 100644 --- a/crates/prune/prune/src/pruner.rs +++ b/crates/prune/prune/src/pruner.rs @@ -6,6 +6,7 @@ use crate::{ }; use alloy_primitives::BlockNumber; use reth_exex_types::FinishedExExHeight; +use reth_primitives_traits::FastInstant as Instant; use reth_provider::{ DBProvider, DatabaseProviderFactory, PruneCheckpointReader, PruneCheckpointWriter, StageCheckpointReader, @@ -13,7 +14,7 @@ use reth_provider::{ use reth_prune_types::{PruneProgress, PrunedSegmentInfo, PrunerOutput}; use reth_stages_types::StageId; use reth_tokio_util::{EventSender, EventStream}; -use std::time::{Duration, Instant}; +use std::time::Duration; use tokio::sync::watch; use tracing::{debug, instrument}; diff --git a/crates/stages/stages/src/stages/sender_recovery.rs b/crates/stages/stages/src/stages/sender_recovery.rs index 475917f673..1d44de7727 100644 --- a/crates/stages/stages/src/stages/sender_recovery.rs +++ b/crates/stages/stages/src/stages/sender_recovery.rs @@ -9,7 +9,9 @@ use reth_db_api::{ transaction::{DbTx, DbTxMut}, RawValue, }; -use reth_primitives_traits::{GotExpected, NodePrimitives, SignedTransaction}; +use reth_primitives_traits::{ + FastInstant as Instant, GotExpected, NodePrimitives, SignedTransaction, +}; use reth_provider::{ BlockReader, DBProvider, EitherWriter, HeaderProvider, ProviderError, PruneCheckpointReader, PruneCheckpointWriter, StaticFileProviderFactory, StatsReader, StorageSettingsCache, @@ -21,7 +23,7 @@ use reth_stages_api::{ StageId, UnwindInput, UnwindOutput, }; use reth_static_file_types::StaticFileSegment; -use std::{fmt::Debug, ops::Range, sync::mpsc, time::Instant}; +use std::{fmt::Debug, ops::Range, sync::mpsc}; use thiserror::Error; use tracing::*; diff --git a/crates/static-file/static-file/src/static_file_producer.rs b/crates/static-file/static-file/src/static_file_producer.rs index ea80189119..5edd5a995d 100644 --- a/crates/static-file/static-file/src/static_file_producer.rs +++ b/crates/static-file/static-file/src/static_file_producer.rs @@ -6,7 +6,7 @@ use parking_lot::Mutex; use rayon::prelude::*; use reth_codecs::Compact; use reth_db_api::table::Value; -use reth_primitives_traits::NodePrimitives; +use reth_primitives_traits::{FastInstant as Instant, NodePrimitives}; use reth_provider::{ providers::StaticFileWriter, BlockReader, ChainStateBlockReader, DBProvider, DatabaseProviderFactory, StageCheckpointReader, StaticFileProviderFactory, @@ -19,7 +19,6 @@ use reth_tokio_util::{EventSender, EventStream}; use std::{ ops::{Deref, RangeInclusive}, sync::Arc, - time::Instant, }; use tracing::{debug, trace}; diff --git a/crates/storage/db/Cargo.toml b/crates/storage/db/Cargo.toml index ccb3f5b0b1..4979eb7233 100644 --- a/crates/storage/db/Cargo.toml +++ b/crates/storage/db/Cargo.toml @@ -31,6 +31,7 @@ eyre = { workspace = true, optional = true } # metrics reth-metrics = { workspace = true, optional = true } metrics = { workspace = true, optional = true } +quanta = { workspace = true, optional = true } # misc page_size = { workspace = true, optional = true } @@ -70,6 +71,7 @@ mdbx = [ "dep:reth-libmdbx", "dep:eyre", "dep:page_size", + "dep:quanta", "reth-metrics", "dep:metrics", "dep:strum", diff --git a/crates/storage/db/src/metrics.rs b/crates/storage/db/src/metrics.rs index 444c9ce570..1b41ce52e7 100644 --- a/crates/storage/db/src/metrics.rs +++ b/crates/storage/db/src/metrics.rs @@ -1,8 +1,9 @@ use crate::Tables; use metrics::Histogram; +use quanta::Instant; use reth_metrics::{metrics::Counter, Metrics}; use rustc_hash::FxHashMap; -use std::time::{Duration, Instant}; +use std::time::Duration; use strum::{EnumCount, EnumIter, IntoEnumIterator}; const LARGE_VALUE_THRESHOLD_BYTES: usize = 4096; diff --git a/crates/storage/provider/src/providers/database/metrics.rs b/crates/storage/provider/src/providers/database/metrics.rs index d3ab33deb5..5552104cda 100644 --- a/crates/storage/provider/src/providers/database/metrics.rs +++ b/crates/storage/provider/src/providers/database/metrics.rs @@ -1,6 +1,7 @@ use metrics::{Gauge, Histogram}; use reth_metrics::Metrics; -use std::time::{Duration, Instant}; +use reth_primitives_traits::FastInstant as Instant; +use std::time::Duration; #[derive(Debug)] pub(crate) struct DurationsRecorder<'a> { diff --git a/crates/storage/provider/src/providers/database/provider.rs b/crates/storage/provider/src/providers/database/provider.rs index 81e47699e2..5d1750b446 100644 --- a/crates/storage/provider/src/providers/database/provider.rs +++ b/crates/storage/provider/src/providers/database/provider.rs @@ -51,8 +51,8 @@ use reth_db_api::{ use reth_execution_types::{BlockExecutionOutput, BlockExecutionResult, Chain, ExecutionOutcome}; use reth_node_types::{BlockTy, BodyTy, HeaderTy, NodeTypes, ReceiptTy, TxTy}; use reth_primitives_traits::{ - Account, Block as _, BlockBody as _, Bytecode, RecoveredBlock, SealedHeader, StorageEntry, - StorageSlotKey, + Account, Block as _, BlockBody as _, Bytecode, FastInstant as Instant, RecoveredBlock, + SealedHeader, StorageEntry, StorageSlotKey, }; use reth_prune_types::{ PruneCheckpoint, PruneMode, PruneModes, PruneSegment, MINIMUM_UNWIND_SAFE_DISTANCE, @@ -79,7 +79,6 @@ use std::{ fmt::Debug, ops::{Deref, DerefMut, Range, RangeBounds, RangeInclusive}, sync::Arc, - time::Instant, }; use tracing::{debug, instrument, trace}; diff --git a/crates/storage/provider/src/providers/rocksdb/provider.rs b/crates/storage/provider/src/providers/rocksdb/provider.rs index 4f755ee408..7fa5cf7191 100644 --- a/crates/storage/provider/src/providers/rocksdb/provider.rs +++ b/crates/storage/provider/src/providers/rocksdb/provider.rs @@ -19,7 +19,7 @@ use reth_db_api::{ table::{Compress, Decode, Decompress, Encode, Table}, tables, BlockNumberList, DatabaseError, }; -use reth_primitives_traits::BlockBody as _; +use reth_primitives_traits::{BlockBody as _, FastInstant as Instant}; use reth_prune_types::PruneMode; use reth_storage_errors::{ db::{DatabaseErrorInfo, DatabaseWriteError, DatabaseWriteOperation, LogLevel}, @@ -36,7 +36,6 @@ use std::{ fmt, path::{Path, PathBuf}, sync::Arc, - time::Instant, }; use tracing::instrument; diff --git a/crates/storage/provider/src/providers/static_file/writer.rs b/crates/storage/provider/src/providers/static_file/writer.rs index 1d0c3fbf2b..5a022bef1f 100644 --- a/crates/storage/provider/src/providers/static_file/writer.rs +++ b/crates/storage/provider/src/providers/static_file/writer.rs @@ -10,6 +10,7 @@ use reth_db::models::{AccountBeforeTx, StorageBeforeTx}; use reth_db_api::models::CompactU256; use reth_nippy_jar::{NippyJar, NippyJarError, NippyJarWriter}; use reth_node_types::NodePrimitives; +use reth_primitives_traits::FastInstant as Instant; use reth_static_file_types::{ ChangesetOffset, ChangesetOffsetReader, ChangesetOffsetWriter, SegmentHeader, SegmentRangeInclusive, StaticFileSegment, @@ -21,7 +22,6 @@ use std::{ fmt::Debug, path::{Path, PathBuf}, sync::{Arc, Weak}, - time::Instant, }; use tracing::{debug, instrument}; diff --git a/crates/tasks/Cargo.toml b/crates/tasks/Cargo.toml index 78d1e4d7ad..f2423e3697 100644 --- a/crates/tasks/Cargo.toml +++ b/crates/tasks/Cargo.toml @@ -23,6 +23,7 @@ metrics.workspace = true # misc auto_impl.workspace = true +quanta.workspace = true tracing.workspace = true thiserror.workspace = true dyn-clone.workspace = true diff --git a/crates/tasks/src/runtime.rs b/crates/tasks/src/runtime.rs index ffe02957e3..e6c1653510 100644 --- a/crates/tasks/src/runtime.rs +++ b/crates/tasks/src/runtime.rs @@ -25,7 +25,7 @@ use std::{ atomic::{AtomicUsize, Ordering}, Arc, Mutex, }, - time::Duration, + time::{Duration, Instant}, }; use tokio::{runtime::Handle, sync::mpsc::UnboundedSender, task::JoinHandle}; use tracing::{debug, error}; @@ -277,6 +277,10 @@ struct RuntimeInner { /// The task monitors critical tasks for panics and fires the shutdown signal. /// Can be taken via [`Runtime::take_task_manager_handle`] to poll for panic errors. task_manager_handle: Mutex>>>, + /// Handle to the quanta upkeep thread that periodically refreshes the cached + /// high-resolution timestamp used by [`quanta::Instant::recent()`]. + /// Dropped when the runtime is dropped, stopping the upkeep thread. + _quanta_upkeep: Option, } // ── Runtime ─────────────────────────────────────────────────────────── @@ -720,9 +724,9 @@ impl Runtime { fn do_graceful_shutdown(&self, timeout: Option) -> bool { let _ = self.0.task_events_tx.send(TaskEvent::GracefulShutdown); - let deadline = timeout.map(|t| std::time::Instant::now() + t); + let deadline = timeout.map(|t| Instant::now() + t); while self.0.graceful_tasks.load(Ordering::SeqCst) > 0 { - if deadline.is_some_and(|d| std::time::Instant::now() > d) { + if deadline.is_some_and(|d| Instant::now() > d) { debug!("graceful shutdown timed out"); return false; } @@ -919,6 +923,8 @@ impl RuntimeBuilder { result }); + let quanta_upkeep = quanta::Upkeep::new(Duration::from_millis(1)).start().ok(); + let inner = RuntimeInner { _tokio_runtime: owned_runtime, handle, @@ -941,6 +947,7 @@ impl RuntimeBuilder { #[cfg(feature = "rayon")] prewarming_pool, task_manager_handle: Mutex::new(Some(task_manager_handle)), + _quanta_upkeep: quanta_upkeep, }; Ok(Runtime(Arc::new(inner))) diff --git a/crates/trie/db/Cargo.toml b/crates/trie/db/Cargo.toml index a6c4d74303..fb2808ae85 100644 --- a/crates/trie/db/Cargo.toml +++ b/crates/trie/db/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth -reth-primitives-traits.workspace = true +reth-primitives-traits = { workspace = true, features = ["std"] } reth-execution-errors.workspace = true reth-db-api.workspace = true reth-trie.workspace = true diff --git a/crates/trie/db/src/changesets.rs b/crates/trie/db/src/changesets.rs index 15bdb58d27..dbcd3e9a4b 100644 --- a/crates/trie/db/src/changesets.rs +++ b/crates/trie/db/src/changesets.rs @@ -10,6 +10,7 @@ use crate::{DatabaseStateRoot, DatabaseTrieCursorFactory}; use alloy_primitives::{map::B256Map, BlockNumber, B256}; use parking_lot::RwLock; +use reth_primitives_traits::FastInstant as Instant; use reth_storage_api::{ BlockNumReader, ChangeSetReader, DBProvider, StageCheckpointReader, StorageChangeSetReader, StorageSettingsCache, @@ -21,7 +22,7 @@ use reth_trie::{ StateRoot, TrieInputSorted, }; use reth_trie_common::updates::{StorageTrieUpdatesSorted, TrieUpdatesSorted}; -use std::{collections::BTreeMap, ops::RangeInclusive, sync::Arc, time::Instant}; +use std::{collections::BTreeMap, ops::RangeInclusive, sync::Arc}; use tracing::debug; #[cfg(feature = "metrics")] diff --git a/crates/trie/parallel/Cargo.toml b/crates/trie/parallel/Cargo.toml index 236999ab9d..678f29eb3c 100644 --- a/crates/trie/parallel/Cargo.toml +++ b/crates/trie/parallel/Cargo.toml @@ -13,7 +13,7 @@ workspace = true [dependencies] # reth -reth-primitives-traits = { workspace = true, features = ["dashmap"] } +reth-primitives-traits = { workspace = true, features = ["dashmap", "std"] } reth-execution-errors.workspace = true reth-provider.workspace = true reth-storage-errors.workspace = true diff --git a/crates/trie/parallel/src/proof.rs b/crates/trie/parallel/src/proof.rs index 71e1b60edd..c5b972303f 100644 --- a/crates/trie/parallel/src/proof.rs +++ b/crates/trie/parallel/src/proof.rs @@ -10,13 +10,14 @@ use crate::{ use alloy_primitives::{map::B256Set, B256}; use crossbeam_channel::{unbounded as crossbeam_unbounded, Receiver as CrossbeamReceiver}; use reth_execution_errors::StorageRootError; +use reth_primitives_traits::FastInstant as Instant; use reth_storage_errors::db::DatabaseError; use reth_trie::{ prefix_set::{PrefixSet, PrefixSetMut, TriePrefixSets, TriePrefixSetsMut}, DecodedMultiProof, DecodedStorageMultiProof, HashedPostState, MultiProofTargets, Nibbles, }; use reth_trie_common::added_removed_keys::MultiAddedRemovedKeys; -use std::{sync::Arc, time::Instant}; +use std::sync::Arc; use tracing::trace; /// Parallel proof calculator. diff --git a/crates/trie/parallel/src/proof_task.rs b/crates/trie/parallel/src/proof_task.rs index 92da6605b5..879297c918 100644 --- a/crates/trie/parallel/src/proof_task.rs +++ b/crates/trie/parallel/src/proof_task.rs @@ -43,7 +43,10 @@ use alloy_primitives::{ use alloy_rlp::{BufMut, Encodable}; use crossbeam_channel::{unbounded, Receiver as CrossbeamReceiver, Sender as CrossbeamSender}; use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind, StateProofError}; -use reth_primitives_traits::dashmap::{self, DashMap}; +use reth_primitives_traits::{ + dashmap::{self, DashMap}, + FastInstant as Instant, +}; use reth_provider::{DatabaseProviderROFactory, ProviderError, ProviderResult}; use reth_storage_errors::db::DatabaseError; use reth_tasks::Runtime; @@ -73,7 +76,7 @@ use std::{ mpsc::{channel, Receiver, Sender}, Arc, }, - time::{Duration, Instant}, + time::Duration, }; use tracing::{debug, debug_span, error, instrument, trace}; diff --git a/crates/trie/sparse/src/parallel.rs b/crates/trie/sparse/src/parallel.rs index 51f04cfd72..7f231be323 100644 --- a/crates/trie/sparse/src/parallel.rs +++ b/crates/trie/sparse/src/parallel.rs @@ -13,6 +13,8 @@ use alloy_rlp::Decodable; use alloy_trie::{BranchNodeCompact, TrieMask, EMPTY_ROOT_HASH}; use core::cmp::{Ord, Ordering, PartialOrd}; use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind, SparseTrieResult}; +#[cfg(feature = "metrics")] +use reth_primitives_traits::FastInstant as Instant; use reth_trie_common::{ prefix_set::{PrefixSet, PrefixSetMut}, BranchNodeMasks, BranchNodeMasksMap, BranchNodeRef, ExtensionNodeRef, LeafNodeRef, Nibbles, @@ -966,7 +968,7 @@ impl SparseTrie for ParallelSparseTrie { changed_subtries.par_iter_mut().for_each(|changed_subtrie| { #[cfg(feature = "metrics")] - let start = std::time::Instant::now(); + let start = Instant::now(); changed_subtrie.subtrie.update_hashes( &mut changed_subtrie.prefix_set, &mut changed_subtrie.update_actions_buf, @@ -1976,7 +1978,7 @@ impl ParallelSparseTrie { }); #[cfg(feature = "metrics")] - let start = std::time::Instant::now(); + let start = Instant::now(); let mut update_actions_buf = self.updates_enabled().then(|| self.update_actions_buffers.pop().unwrap_or_default()); diff --git a/crates/trie/sparse/src/state.rs b/crates/trie/sparse/src/state.rs index 90085095ce..a43bc971f3 100644 --- a/crates/trie/sparse/src/state.rs +++ b/crates/trie/sparse/src/state.rs @@ -12,6 +12,8 @@ use alloy_rlp::{Decodable, Encodable}; use alloy_trie::proof::DecodedProofNodes; use reth_execution_errors::{SparseStateTrieErrorKind, SparseStateTrieResult, SparseTrieErrorKind}; use reth_primitives_traits::Account; +#[cfg(feature = "std")] +use reth_primitives_traits::FastInstant as Instant; use reth_trie_common::{ proof::ProofNodes, updates::{StorageTrieUpdates, TrieUpdates}, @@ -1191,7 +1193,7 @@ impl StorageTries { /// Keeps the top `max_storage_tries` by a score combining size and heat. /// Evicts lower-scored tries entirely, prunes kept tries to `max_depth`. fn prune(&mut self, max_depth: usize, max_storage_tries: usize) { - let fn_start = std::time::Instant::now(); + let fn_start = Instant::now(); let mut stats = StorageTriesPruneStats { total_tries_before: self.tries.len(), ..Default::default() }; @@ -1248,7 +1250,7 @@ impl StorageTries { // - They haven't been pruned since last access // - They're large enough to be worth pruning const MIN_SIZE_TO_PRUNE: usize = 1000; - let prune_start = std::time::Instant::now(); + let prune_start = Instant::now(); for (address, size) in &tries_to_keep { if *size < MIN_SIZE_TO_PRUNE { stats.skipped_small += 1; diff --git a/crates/trie/trie/Cargo.toml b/crates/trie/trie/Cargo.toml index b08d7d89b4..b766c143a9 100644 --- a/crates/trie/trie/Cargo.toml +++ b/crates/trie/trie/Cargo.toml @@ -14,7 +14,7 @@ workspace = true [dependencies] # reth reth-execution-errors.workspace = true -reth-primitives-traits.workspace = true +reth-primitives-traits = { workspace = true, features = ["std"] } reth-stages-types.workspace = true reth-storage-errors.workspace = true reth-trie-sparse.workspace = true diff --git a/crates/trie/trie/src/hashed_cursor/metrics.rs b/crates/trie/trie/src/hashed_cursor/metrics.rs index c8089e8943..8cca1a1c1b 100644 --- a/crates/trie/trie/src/hashed_cursor/metrics.rs +++ b/crates/trie/trie/src/hashed_cursor/metrics.rs @@ -1,7 +1,8 @@ use super::{HashedCursor, HashedStorageCursor}; use alloy_primitives::B256; +use reth_primitives_traits::FastInstant as Instant; use reth_storage_errors::db::DatabaseError; -use std::time::{Duration, Instant}; +use std::time::Duration; use tracing::trace_span; #[cfg(feature = "metrics")] diff --git a/crates/trie/trie/src/proof/trie_node.rs b/crates/trie/trie/src/proof/trie_node.rs index 2c7dad6fd2..1a7a77f792 100644 --- a/crates/trie/trie/src/proof/trie_node.rs +++ b/crates/trie/trie/src/proof/trie_node.rs @@ -2,11 +2,11 @@ use super::{Proof, StorageProof}; use crate::{hashed_cursor::HashedCursorFactory, trie_cursor::TrieCursorFactory}; use alloy_primitives::{map::HashSet, B256}; use reth_execution_errors::{SparseTrieError, SparseTrieErrorKind}; +use reth_primitives_traits::FastInstant as Instant; use reth_trie_common::{MultiProofTargets, Nibbles}; use reth_trie_sparse::provider::{ pad_path_to_key, RevealedNode, TrieNodeProvider, TrieNodeProviderFactory, }; -use std::time::Instant; use tracing::{enabled, trace, Level}; /// Factory for instantiating providers capable of retrieving blinded trie nodes via proofs. diff --git a/crates/trie/trie/src/stats.rs b/crates/trie/trie/src/stats.rs index 400c693445..e54bc3bf0b 100644 --- a/crates/trie/trie/src/stats.rs +++ b/crates/trie/trie/src/stats.rs @@ -1,4 +1,5 @@ -use std::time::{Duration, Instant}; +use reth_primitives_traits::FastInstant as Instant; +use std::time::Duration; /// Trie stats. #[derive(Clone, Copy, Debug)] diff --git a/crates/trie/trie/src/trie_cursor/metrics.rs b/crates/trie/trie/src/trie_cursor/metrics.rs index f1ec81f88f..1c256f6124 100644 --- a/crates/trie/trie/src/trie_cursor/metrics.rs +++ b/crates/trie/trie/src/trie_cursor/metrics.rs @@ -1,8 +1,9 @@ use super::{TrieCursor, TrieStorageCursor}; use crate::{BranchNodeCompact, Nibbles}; use alloy_primitives::B256; +use reth_primitives_traits::FastInstant as Instant; use reth_storage_errors::db::DatabaseError; -use std::time::{Duration, Instant}; +use std::time::Duration; use tracing::trace_span; #[cfg(feature = "metrics")]