Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(stages): use MetricsListener for Execution stage gas metric #3511

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions bin/reth/src/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ impl Command {
if continuous { HeaderSyncMode::Continuous } else { HeaderSyncMode::Tip(tip_rx) };
let pipeline = builder
.with_tip_sender(tip_tx)
.with_metrics_tx(metrics_tx)
.with_metrics_tx(metrics_tx.clone())
.add_stages(
DefaultStages::new(
header_mode,
Expand All @@ -706,13 +706,16 @@ impl Command {
.set(SenderRecoveryStage {
commit_threshold: stage_config.sender_recovery.commit_threshold,
})
.set(ExecutionStage::new(
factory,
ExecutionStageThresholds {
max_blocks: stage_config.execution.max_blocks,
max_changes: stage_config.execution.max_changes,
},
))
.set(
ExecutionStage::new(
factory,
ExecutionStageThresholds {
max_blocks: stage_config.execution.max_blocks,
max_changes: stage_config.execution.max_changes,
},
)
.with_metrics_tx(metrics_tx),
)
.set(AccountHashingStage::new(
stage_config.account_hashing.clean_threshold,
stage_config.account_hashing.commit_threshold,
Expand Down
11 changes: 11 additions & 0 deletions crates/stages/src/metrics/listener.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::metrics::SyncMetrics;
use reth_primitives::{
constants::MGAS_TO_GAS,
stage::{StageCheckpoint, StageId},
BlockNumber,
};
Expand Down Expand Up @@ -31,6 +32,11 @@ pub enum MetricEvent {
/// If specified, `entities_total` metric is updated.
max_block_number: Option<BlockNumber>,
},
/// Execution stage processed some amount of gas.
ExecutionStageGas {
/// Gas processed.
gas: u64,
},
}

/// Metrics routine that listens to new metric events on the `events_rx` receiver.
Expand Down Expand Up @@ -71,6 +77,11 @@ impl MetricsListener {
stage_metrics.entities_total.set(total as f64);
}
}
MetricEvent::ExecutionStageGas { gas } => self
.sync_metrics
.execution_stage
.mgas_processed_total
.increment(gas as f64 / MGAS_TO_GAS as f64),
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/stages/src/metrics/sync_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::collections::HashMap;
#[derive(Debug, Default)]
pub(crate) struct SyncMetrics {
pub(crate) stages: HashMap<StageId, StageMetrics>,
pub(crate) execution_stage: ExecutionStageMetrics,
}

impl SyncMetrics {
Expand All @@ -29,3 +30,11 @@ pub(crate) struct StageMetrics {
/// The number of total entities of the last commit for a stage, if applicable.
pub(crate) entities_total: Gauge,
}

/// Execution stage metrics.
#[derive(Metrics)]
#[metrics(scope = "sync.execution")]
pub(crate) struct ExecutionStageMetrics {
/// The total amount of gas processed (in millions)
pub(crate) mgas_processed_total: Gauge,
}
37 changes: 17 additions & 20 deletions crates/stages/src/stages/execution.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{ExecInput, ExecOutput, Stage, StageError, UnwindInput, UnwindOutput};
use crate::{
ExecInput, ExecOutput, MetricEvent, MetricEventsSender, Stage, StageError, UnwindInput,
UnwindOutput,
};
use reth_db::{
cursor::{DbCursorRO, DbCursorRW, DbDupCursorRO},
database::Database,
Expand All @@ -7,12 +10,7 @@ use reth_db::{
transaction::{DbTx, DbTxMut},
};
use reth_interfaces::db::DatabaseError;
use reth_metrics::{
metrics::{self, Gauge},
Metrics,
};
use reth_primitives::{
constants::MGAS_TO_GAS,
stage::{
CheckpointBlockRange, EntitiesCheckpoint, ExecutionCheckpoint, StageCheckpoint, StageId,
},
Expand All @@ -25,14 +23,6 @@ use reth_provider::{
use std::{ops::RangeInclusive, time::Instant};
use tracing::*;

/// Execution stage metrics.
#[derive(Metrics)]
#[metrics(scope = "sync.execution")]
pub struct ExecutionStageMetrics {
/// The total amount of gas processed (in millions)
mgas_processed_total: Gauge,
}

/// The execution stage executes all transactions and
/// update history indexes.
///
Expand Down Expand Up @@ -64,7 +54,7 @@ pub struct ExecutionStageMetrics {
// false positive, we cannot derive it if !DB: Debug.
#[allow(missing_debug_implementations)]
pub struct ExecutionStage<EF: ExecutorFactory> {
metrics: ExecutionStageMetrics,
metrics_tx: Option<MetricEventsSender>,
/// The stage's internal executor
executor_factory: EF,
/// The commit thresholds of the execution stage.
Expand All @@ -74,7 +64,7 @@ pub struct ExecutionStage<EF: ExecutorFactory> {
impl<EF: ExecutorFactory> ExecutionStage<EF> {
/// Create new execution stage with specified config.
pub fn new(executor_factory: EF, thresholds: ExecutionStageThresholds) -> Self {
Self { metrics: ExecutionStageMetrics::default(), executor_factory, thresholds }
Self { metrics_tx: None, executor_factory, thresholds }
}

/// Create an execution stage with the provided executor factory.
Expand All @@ -84,9 +74,15 @@ impl<EF: ExecutorFactory> ExecutionStage<EF> {
Self::new(executor_factory, ExecutionStageThresholds::default())
}

/// Set the metric events sender.
pub fn with_metrics_tx(mut self, metrics_tx: MetricEventsSender) -> Self {
self.metrics_tx = Some(metrics_tx);
self
}

/// Execute the stage.
pub fn execute_inner<DB: Database>(
&self,
&mut self,
provider: &DatabaseProviderRW<'_, &DB>,
input: ExecInput,
) -> Result<ExecOutput, StageError> {
Expand Down Expand Up @@ -129,9 +125,10 @@ impl<EF: ExecutorFactory> ExecutionStage<EF> {
})?;

// Gas metrics
self.metrics
.mgas_processed_total
.increment(block.header.gas_used as f64 / MGAS_TO_GAS as f64);
if let Some(metrics_tx) = &mut self.metrics_tx {
let _ =
metrics_tx.send(MetricEvent::ExecutionStageGas { gas: block.header.gas_used });
}

// Merge state changes
state.extend(block_state);
Expand Down
Loading