From 6dd5ec45225f28e318c3cdaadab9324930706e19 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Sun, 18 Feb 2024 21:44:21 -0500 Subject: [PATCH] add truncate metrics --- crates/net/network/src/transactions/mod.rs | 4 ++++ crates/transaction-pool/src/metrics.rs | 12 ++++++++++++ crates/transaction-pool/src/pool/txpool.rs | 10 ++++++++++ 3 files changed, 26 insertions(+) diff --git a/crates/net/network/src/transactions/mod.rs b/crates/net/network/src/transactions/mod.rs index 1874b476d9..a351520836 100644 --- a/crates/net/network/src/transactions/mod.rs +++ b/crates/net/network/src/transactions/mod.rs @@ -972,6 +972,10 @@ where } } + // The original capacity reservation may have been too large, so we shrink the vector to + // the actual number of transactions to import. + // new_txs.shrink_to_fit(); + // import new transactions as a batch to minimize lock contention on the underlying pool if !new_txs.is_empty() { let pool = self.pool.clone(); diff --git a/crates/transaction-pool/src/metrics.rs b/crates/transaction-pool/src/metrics.rs index 9076829427..81e3e095b1 100644 --- a/crates/transaction-pool/src/metrics.rs +++ b/crates/transaction-pool/src/metrics.rs @@ -41,6 +41,18 @@ pub struct TxPoolMetrics { /// How often the pool was updated after the canonical state changed pub(crate) performed_state_updates: Counter, + + /// Total `discard_worst` calls + pub(crate) discard_worst_calls: Counter, + + /// How long it took the pool to `discard_worst` + pub(crate) discard_worst_duration: Gauge, + + /// How many times `discard_worst` looped + pub(crate) discard_worst_loops: Gauge, + + /// How many transactions were removed in the last `discard_worst` call + pub(crate) discard_worst_removed: Gauge, } /// Transaction pool blobstore metrics diff --git a/crates/transaction-pool/src/pool/txpool.rs b/crates/transaction-pool/src/pool/txpool.rs index ab877ff0c1..00fa1876bb 100644 --- a/crates/transaction-pool/src/pool/txpool.rs +++ b/crates/transaction-pool/src/pool/txpool.rs @@ -756,6 +756,10 @@ impl TxPool { /// This returns all transactions that were removed from the entire pool. pub(crate) fn discard_worst(&mut self) -> Vec>> { let mut removed = Vec::new(); + self.metrics.discard_worst_calls.increment(1); + + let now = std::time::Instant::now(); + let mut loops = 0; // Helper macro that discards the worst transactions for the pools macro_rules! discard_worst { @@ -766,6 +770,7 @@ impl TxPool { .$limit .is_exceeded($this.$pool.len(), $this.$pool.size()) { + loops += 1; trace!( "discarding transactions from {}, limit: {:?}, curr size: {}, curr len: {}", stringify!($pool), @@ -813,6 +818,11 @@ impl TxPool { ] ); + let elapsed = now.elapsed(); + self.metrics.discard_worst_duration.set(elapsed.as_secs_f64()); + self.metrics.discard_worst_removed.set(removed.len() as f64); + self.metrics.discard_worst_loops.set(loops as f64); + removed }