From 1f0d129707d85bd8890c6bfc8c281480cd474d28 Mon Sep 17 00:00:00 2001 From: Lion - dapplion <35266934+dapplion@users.noreply.github.com> Date: Tue, 20 Aug 2024 04:03:43 +0200 Subject: [PATCH] Add time metrics to aquire fork-choice lock (#6204) * Add time metrics to aquire fork-choice lock * Apply suggestions from code review Co-authored-by: Michael Sproul * Merge remote-tracking branch 'sigp/unstable' into fork-choice-lock-metrics * fix merge issue --- beacon_node/beacon_chain/src/canonical_head.rs | 6 ++++-- beacon_node/beacon_chain/src/metrics.rs | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/beacon_node/beacon_chain/src/canonical_head.rs b/beacon_node/beacon_chain/src/canonical_head.rs index 40e2f60b0e9..4f92f5ec8f9 100644 --- a/beacon_node/beacon_chain/src/canonical_head.rs +++ b/beacon_node/beacon_chain/src/canonical_head.rs @@ -236,12 +236,12 @@ impl CachedHead { pub struct CanonicalHead { /// Provides an in-memory representation of the non-finalized block tree and is used to run the /// fork choice algorithm and determine the canonical head. - pub fork_choice: CanonicalHeadRwLock>, + fork_choice: CanonicalHeadRwLock>, /// Provides values cached from a previous execution of `self.fork_choice.get_head`. /// /// Although `self.fork_choice` might be slightly more advanced that this value, it is safe to /// consider that these values represent the "canonical head" of the beacon chain. - pub cached_head: CanonicalHeadRwLock>, + cached_head: CanonicalHeadRwLock>, /// A lock used to prevent concurrent runs of `BeaconChain::recompute_head`. /// /// This lock **should not be made public**, it should only be used inside this module. @@ -383,11 +383,13 @@ impl CanonicalHead { /// Access a read-lock for fork choice. pub fn fork_choice_read_lock(&self) -> RwLockReadGuard> { + let _timer = metrics::start_timer(&metrics::FORK_CHOICE_READ_LOCK_AQUIRE_TIMES); self.fork_choice.read() } /// Access a write-lock for fork choice. pub fn fork_choice_write_lock(&self) -> RwLockWriteGuard> { + let _timer = metrics::start_timer(&metrics::FORK_CHOICE_WRITE_LOCK_AQUIRE_TIMES); self.fork_choice.write() } } diff --git a/beacon_node/beacon_chain/src/metrics.rs b/beacon_node/beacon_chain/src/metrics.rs index 9610001be15..82c98a2083b 100644 --- a/beacon_node/beacon_chain/src/metrics.rs +++ b/beacon_node/beacon_chain/src/metrics.rs @@ -569,6 +569,20 @@ pub static FORK_CHOICE_AFTER_FINALIZATION_TIMES: LazyLock> = exponential_buckets(1e-3, 2.0, 10), ) }); +pub static FORK_CHOICE_READ_LOCK_AQUIRE_TIMES: LazyLock> = LazyLock::new(|| { + try_create_histogram_with_buckets( + "beacon_fork_choice_read_lock_aquire_seconds", + "Time taken to aquire the fork-choice read lock", + exponential_buckets(1e-4, 4.0, 7), + ) +}); +pub static FORK_CHOICE_WRITE_LOCK_AQUIRE_TIMES: LazyLock> = LazyLock::new(|| { + try_create_histogram_with_buckets( + "beacon_fork_choice_write_lock_aquire_seconds", + "Time taken to aquire the fork-choice write lock", + exponential_buckets(1e-3, 4.0, 7), + ) +}); pub static FORK_CHOICE_SET_HEAD_LAG_TIMES: LazyLock> = LazyLock::new(|| { try_create_histogram( "beacon_fork_choice_set_head_lag_times",