reth_blockchain_tree/
metrics.rsuse metrics::Histogram;
use reth_metrics::{
metrics::{Counter, Gauge},
Metrics,
};
use std::time::{Duration, Instant};
#[derive(Metrics)]
#[metrics(scope = "blockchain_tree.block_buffer")]
pub struct BlockBufferMetrics {
pub blocks: Gauge,
}
#[derive(Debug)]
pub(crate) struct MakeCanonicalDurationsRecorder {
start: Instant,
pub(crate) actions: Vec<(MakeCanonicalAction, Duration)>,
latest: Option<Duration>,
current_metrics: MakeCanonicalMetrics,
}
impl Default for MakeCanonicalDurationsRecorder {
fn default() -> Self {
Self {
start: Instant::now(),
actions: Vec::new(),
latest: None,
current_metrics: MakeCanonicalMetrics::default(),
}
}
}
impl MakeCanonicalDurationsRecorder {
pub(crate) fn record_relative(&mut self, action: MakeCanonicalAction) {
let elapsed = self.start.elapsed();
let duration = elapsed - self.latest.unwrap_or_default();
self.actions.push((action, duration));
self.current_metrics.record(action, duration);
self.latest = Some(elapsed);
}
}
#[derive(Metrics)]
#[metrics(scope = "blockchain_tree")]
pub struct TreeMetrics {
pub sidechains: Gauge,
pub canonical_chain_height: Gauge,
pub reorgs: Counter,
pub latest_reorg_depth: Gauge,
pub longest_sidechain_height: Gauge,
pub trie_updates_insert_cached: Counter,
pub trie_updates_insert_recomputed: Counter,
}
#[derive(Debug, Copy, Clone)]
pub(crate) enum MakeCanonicalAction {
CloneOldBlocks,
FindCanonicalHeader,
SplitChain,
SplitChainForks,
MergeAllChains,
UpdateCanonicalIndex,
RetrieveStateTrieUpdates,
CommitCanonicalChainToDatabase,
RevertCanonicalChainFromDatabase,
InsertOldCanonicalChain,
ClearTrieUpdatesForOtherChildren,
}
#[derive(Metrics)]
#[metrics(scope = "blockchain_tree.make_canonical")]
struct MakeCanonicalMetrics {
clone_old_blocks: Histogram,
find_canonical_header: Histogram,
split_chain: Histogram,
split_chain_forks: Histogram,
merge_all_chains: Histogram,
update_canonical_index: Histogram,
retrieve_state_trie_updates: Histogram,
commit_canonical_chain_to_database: Histogram,
revert_canonical_chain_from_database: Histogram,
insert_old_canonical_chain: Histogram,
clear_trie_updates_for_other_children: Histogram,
}
impl MakeCanonicalMetrics {
pub(crate) fn record(&self, action: MakeCanonicalAction, duration: Duration) {
match action {
MakeCanonicalAction::CloneOldBlocks => self.clone_old_blocks.record(duration),
MakeCanonicalAction::FindCanonicalHeader => self.find_canonical_header.record(duration),
MakeCanonicalAction::SplitChain => self.split_chain.record(duration),
MakeCanonicalAction::SplitChainForks => self.split_chain_forks.record(duration),
MakeCanonicalAction::MergeAllChains => self.merge_all_chains.record(duration),
MakeCanonicalAction::UpdateCanonicalIndex => {
self.update_canonical_index.record(duration)
}
MakeCanonicalAction::RetrieveStateTrieUpdates => {
self.retrieve_state_trie_updates.record(duration)
}
MakeCanonicalAction::CommitCanonicalChainToDatabase => {
self.commit_canonical_chain_to_database.record(duration)
}
MakeCanonicalAction::RevertCanonicalChainFromDatabase => {
self.revert_canonical_chain_from_database.record(duration)
}
MakeCanonicalAction::InsertOldCanonicalChain => {
self.insert_old_canonical_chain.record(duration)
}
MakeCanonicalAction::ClearTrieUpdatesForOtherChildren => {
self.clear_trie_updates_for_other_children.record(duration)
}
}
}
}