From f8f7ca1cc9fd8b22a4ab9b3b212d6bf48553411d Mon Sep 17 00:00:00 2001 From: yohkaz Date: Thu, 23 Jan 2025 12:38:01 +0200 Subject: [PATCH] Add latency metrics to eviction pools routines --- crates/transaction-pool/src/metrics.rs | 42 ++++++++++++++++++++- crates/transaction-pool/src/pool/blob.rs | 11 +++++- crates/transaction-pool/src/pool/parked.rs | 8 ++++ crates/transaction-pool/src/pool/pending.rs | 8 ++++ crates/transaction-pool/src/pool/txpool.rs | 9 ++++- 5 files changed, 74 insertions(+), 4 deletions(-) diff --git a/crates/transaction-pool/src/metrics.rs b/crates/transaction-pool/src/metrics.rs index f5d269b361fc..1cd8186f90d2 100644 --- a/crates/transaction-pool/src/metrics.rs +++ b/crates/transaction-pool/src/metrics.rs @@ -1,7 +1,7 @@ //! Transaction pool metrics. use reth_metrics::{ - metrics::{Counter, Gauge}, + metrics::{Counter, Gauge, Histogram}, Metrics, }; @@ -123,3 +123,43 @@ pub struct AllTransactionsMetrics { /// The current base fee pub(crate) base_fee: Gauge, } + +/// Blob pool eviction metrics +#[derive(Metrics, Clone)] +#[metrics(scope = "transaction_pool")] +pub struct BlobEvictionMetrics { + /// Counter for the number of blob transactions evicted + pub(crate) blob_transactions_evicted: Counter, + /// Blob eviction routine time + pub(crate) blob_eviction_duration_seconds: Histogram, +} + +/// Pending pool eviction metrics +#[derive(Metrics, Clone)] +#[metrics(scope = "transaction_pool")] +pub struct PendingEvictionMetrics { + /// Counter for the number of pending transactions evicted + pub(crate) pending_transactions_evicted: Counter, + /// Pending eviction routine time + pub(crate) pending_eviction_duration_seconds: Histogram, +} + +/// Parked pool eviction metrics +#[derive(Metrics, Clone)] +#[metrics(scope = "transaction_pool")] +pub struct ParkedEvictionMetrics { + /// Counter for the number of parked transactions evicted + pub(crate) parked_transactions_evicted: Counter, + /// Parked eviction routine time + pub(crate) parked_eviction_duration_seconds: Histogram, +} + +/// Tx pool eviction metrics +#[derive(Metrics, Clone)] +#[metrics(scope = "transaction_pool")] +pub struct TxEvictionMetrics { + /// Counter for the number of transactions evicted + pub(crate) tx_transactions_evicted: Counter, + /// Transaction eviction routine time + pub(crate) tx_eviction_duration_seconds: Histogram, +} diff --git a/crates/transaction-pool/src/pool/blob.rs b/crates/transaction-pool/src/pool/blob.rs index e6c0cb245c3f..ef927dc79a4b 100644 --- a/crates/transaction-pool/src/pool/blob.rs +++ b/crates/transaction-pool/src/pool/blob.rs @@ -1,7 +1,7 @@ use super::txpool::PendingFees; use crate::{ - identifier::TransactionId, pool::size::SizeTracker, traits::BestTransactionsAttributes, - PoolTransaction, SubPoolLimit, ValidPoolTransaction, + identifier::TransactionId, metrics::BlobEvictionMetrics, pool::size::SizeTracker, + traits::BestTransactionsAttributes, PoolTransaction, SubPoolLimit, ValidPoolTransaction, }; use std::{ cmp::Ordering, @@ -31,6 +31,8 @@ pub(crate) struct BlobTransactions { /// /// See also [`PoolTransaction::size`]. size_of: SizeTracker, + /// Metrics for evictions + eviction_metrics: BlobEvictionMetrics, } // === impl BlobTransactions === @@ -221,6 +223,7 @@ impl BlobTransactions { &mut self, limit: SubPoolLimit, ) -> Vec>> { + let start = std::time::Instant::now(); let mut removed = Vec::new(); while self.exceeds(&limit) { @@ -229,6 +232,9 @@ impl BlobTransactions { removed.push(self.remove_transaction(&id).expect("transaction exists")); } + self.eviction_metrics.blob_eviction_duration_seconds.record(start.elapsed()); + self.eviction_metrics.blob_transactions_evicted.increment(removed.len() as u64); + removed } @@ -257,6 +263,7 @@ impl Default for BlobTransactions { all: Default::default(), size_of: Default::default(), pending_fees: Default::default(), + eviction_metrics: Default::default(), } } } diff --git a/crates/transaction-pool/src/pool/parked.rs b/crates/transaction-pool/src/pool/parked.rs index 29216af47d02..dcec61bc0aca 100644 --- a/crates/transaction-pool/src/pool/parked.rs +++ b/crates/transaction-pool/src/pool/parked.rs @@ -1,5 +1,6 @@ use crate::{ identifier::{SenderId, TransactionId}, + metrics::ParkedEvictionMetrics, pool::size::SizeTracker, PoolTransaction, SubPoolLimit, ValidPoolTransaction, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER, }; @@ -45,6 +46,8 @@ pub struct ParkedPool { /// /// See also [`PoolTransaction::size`]. size_of: SizeTracker, + /// Eviction metrics + eviction_metrics: ParkedEvictionMetrics, } // === impl ParkedPool === @@ -188,6 +191,7 @@ impl ParkedPool { &mut self, limit: SubPoolLimit, ) -> Vec>> { + let start = std::time::Instant::now(); if !self.exceeds(&limit) { // if we are below the limits, we don't need to drop anything return Vec::new() @@ -213,6 +217,9 @@ impl ParkedPool { } } + self.eviction_metrics.parked_eviction_duration_seconds.record(start.elapsed()); + self.eviction_metrics.parked_transactions_evicted.increment(removed.len() as u64); + removed } @@ -333,6 +340,7 @@ impl Default for ParkedPool { last_sender_submission: Default::default(), sender_transaction_count: Default::default(), size_of: Default::default(), + eviction_metrics: Default::default(), } } } diff --git a/crates/transaction-pool/src/pool/pending.rs b/crates/transaction-pool/src/pool/pending.rs index d169bfa37af0..37ceb0564a77 100644 --- a/crates/transaction-pool/src/pool/pending.rs +++ b/crates/transaction-pool/src/pool/pending.rs @@ -1,5 +1,6 @@ use crate::{ identifier::{SenderId, TransactionId}, + metrics::PendingEvictionMetrics, pool::{ best::{BestTransactions, BestTransactionsWithFees}, size::SizeTracker, @@ -48,6 +49,8 @@ pub struct PendingPool { /// Used to broadcast new transactions that have been added to the `PendingPool` to existing /// `static_files` of this pool. new_transaction_notifier: broadcast::Sender>, + /// Metrics for evictions + eviction_metrics: PendingEvictionMetrics, } // === impl PendingPool === @@ -64,6 +67,7 @@ impl PendingPool { highest_nonces: Default::default(), size_of: Default::default(), new_transaction_notifier, + eviction_metrics: Default::default(), } } @@ -468,6 +472,7 @@ impl PendingPool { &mut self, limit: SubPoolLimit, ) -> Vec>> { + let start = std::time::Instant::now(); let mut removed = Vec::new(); // return early if the pool is already under the limits if !self.exceeds(&limit) { @@ -484,6 +489,9 @@ impl PendingPool { // pool to be under the limit self.remove_to_limit(&limit, true, &mut removed); + self.eviction_metrics.pending_eviction_duration_seconds.record(start.elapsed()); + self.eviction_metrics.pending_transactions_evicted.increment(removed.len() as u64); + removed } diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index a0a1f6a369fe..a7a724fdc5c7 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -4,7 +4,7 @@ use crate::{ config::{LocalTransactionConfig, TXPOOL_MAX_ACCOUNT_SLOTS_PER_SENDER}, error::{Eip4844PoolTransactionError, InvalidPoolTransactionError, PoolError, PoolErrorKind}, identifier::{SenderId, TransactionId}, - metrics::{AllTransactionsMetrics, TxPoolMetrics}, + metrics::{AllTransactionsMetrics, TxEvictionMetrics, TxPoolMetrics}, pool::{ best::BestTransactions, blob::BlobTransactions, @@ -114,6 +114,8 @@ pub struct TxPool { metrics: TxPoolMetrics, /// The last update kind that was applied to the pool. latest_update_kind: Option, + /// Metrics for evictions + eviction_metrics: TxEvictionMetrics, } // === impl TxPool === @@ -131,6 +133,7 @@ impl TxPool { config, metrics: Default::default(), latest_update_kind: None, + eviction_metrics: Default::default(), } } @@ -956,6 +959,7 @@ impl TxPool { /// /// This returns all transactions that were removed from the entire pool. pub(crate) fn discard_worst(&mut self) -> Vec>> { + let start = std::time::Instant::now(); let mut removed = Vec::new(); // Helper macro that discards the worst transactions for the pools @@ -1013,6 +1017,9 @@ impl TxPool { ] ); + self.eviction_metrics.tx_eviction_duration_seconds.record(start.elapsed()); + self.eviction_metrics.tx_transactions_evicted.increment(removed.len() as u64); + removed }