Skip to content

Commit

Permalink
Refactor: refactoring the code of get cachedb metric (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonymousGiga authored Oct 20, 2023
1 parent 7f17753 commit 79dcfaf
Show file tree
Hide file tree
Showing 16 changed files with 106 additions and 84 deletions.
36 changes: 23 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ enable_opcode_metrics = [
"reth-provider/enable_opcode_metrics",
"reth-stages/enable_opcode_metrics",
]
enable_cache_record = [
"reth-revm/enable_cache_record",
"reth-provider/enable_cache_record",
"reth-stages/enable_cache_record",
"reth-rpc/enable_cache_record",
]
finish_after_execution_stage = ["reth-stages/finish_after_execution_stage"]
enable_execution_duration_record = [
"reth-stages/enable_execution_duration_record",
Expand Down
2 changes: 0 additions & 2 deletions crates/payload/basic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,8 +707,6 @@ where
cfg: initialized_cfg.clone(),
block: initialized_block_env.clone(),
tx: tx_env_with_recovered(&tx),
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
cpu_frequency: 0f64,
};

let mut evm = revm::EVM::with_env(env);
Expand Down
11 changes: 9 additions & 2 deletions crates/revm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ reth-consensus-common = { path = "../consensus/common" }

# revm
revm.workspace = true
revm-utils = { git = "https://github.com/megaeth-labs/revm", branch = "andy/debug/performance-dashboard" }
revm-utils = { git = "https://github.com/megaeth-labs/revm", branch = "andy/debug/performance-dashboard", optional = true }

# common
tracing.workspace = true
Expand All @@ -32,4 +32,11 @@ once_cell = "1.17.0"
enable_opcode_metrics = [
"revm/enable_opcode_metrics",
"reth-provider/enable_opcode_metrics",
]
"revm-utils",
]

enable_cache_record = [
"revm/enable_cache_record",
"reth-provider/enable_cache_record",
"revm-utils",
]
18 changes: 12 additions & 6 deletions crates/revm/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ use revm::{
},
EVM,
};
#[cfg(feature = "enable_cache_record")]
use revm_utils::types::CacheDbRecord;
#[cfg(feature = "enable_opcode_metrics")]
use revm_utils::types::RevmMetricRecord;
use std::{
Expand Down Expand Up @@ -117,8 +119,6 @@ where
header,
total_difficulty,
);
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
self.evm.env.cpu_frequency = revm_utils::time::get_cpu_frequency().unwrap_or_default();
}

/// Commit change to the run-time database, and update the given [PostState] with the changes
Expand Down Expand Up @@ -390,15 +390,21 @@ where

/// Handle revm metric records.
#[cfg(feature = "enable_opcode_metrics")]
fn get_revm_metric_record(&self) -> RevmMetricRecord {
fn get_opcode_record(&self) -> RevmMetricRecord {
self.revm_metric_record.clone()
}

/// Handle revm metric records.
#[cfg(feature = "enable_opcode_metrics")]
fn get_revm_metric_cachedb_size(&self) -> usize {
/// Get cachedb size.
#[cfg(feature = "enable_cache_record")]
fn get_cachedb_size(&self) -> usize {
self.evm.db.as_ref().expect("db is empty").size()
}

/// Get cacheDb metric record.
#[cfg(feature = "enable_cache_record")]
fn get_cachedb_record(&self) -> CacheDbRecord {
self.evm.db.as_ref().expect("db is empty").get_metric()
}
}

/// Increment the balance for the given account in the [PostState].
Expand Down
4 changes: 4 additions & 0 deletions crates/rpc/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,7 @@ enable_opcode_metrics = [
"revm-primitives/enable_opcode_metrics",
"reth-revm/enable_opcode_metrics",
]

enable_cache_record = [
"reth-revm/enable_cache_record",
]
32 changes: 7 additions & 25 deletions crates/rpc/rpc/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ use reth_rpc_types::{
BlockError, Bundle, CallRequest, RichBlock, StateContext,
};
use reth_tasks::TaskSpawner;
#[cfg(feature = "enable_cache_record")]
use revm::db::CacheDbRecord;
use revm::{
db::{CacheDB, EmptyDB},
primitives::Env,
Expand Down Expand Up @@ -102,9 +104,7 @@ where
while let Some(tx) = transactions.next() {
let tx = tx.into_ecrecovered().ok_or(BlockError::InvalidSignature)?;
let tx = tx_env_with_recovered(&tx);
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env =
Env { cfg: cfg.clone(), block: block_env.clone(), tx, cpu_frequency: 0f64 };
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
let (result, state_changes) =
this.trace_transaction(opts.clone(), env, at, &mut db)?;
results.push(TraceResult::Success { result });
Expand Down Expand Up @@ -202,13 +202,7 @@ where
tx.hash,
)?;

// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env {
cfg,
block: block_env,
tx: tx_env_with_recovered(&tx),
cpu_frequency: 0f64,
};
let env = Env { cfg, block: block_env, tx: tx_env_with_recovered(&tx) };
this.trace_transaction(opts, env, state_at, &mut db).map(|(trace, _)| trace)
})
.await
Expand Down Expand Up @@ -398,13 +392,7 @@ where
for tx in transactions {
let tx = tx.into_ecrecovered().ok_or(BlockError::InvalidSignature)?;
let tx = tx_env_with_recovered(&tx);
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env {
cfg: cfg.clone(),
block: block_env.clone(),
tx,
cpu_frequency: 0f64,
};
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
let (res, _) = transact(&mut db, env)?;
db.commit(res.state);
}
Expand Down Expand Up @@ -605,14 +593,8 @@ where
logs,
block_hashes,
db: State::new(state),
#[cfg(feature = "enable_opcode_metrics")]
cache_hits: (0u64, 0u64, 0u64, 0u64),
#[cfg(feature = "enable_opcode_metrics")]
cache_misses: (0u64, 0u64, 0u64, 0u64),
#[cfg(feature = "enable_opcode_metrics")]
cache_misses_penalty: (0u128, 0u128, 0u128, 0u128),
#[cfg(feature = "enable_opcode_metrics")]
cpu_frequency: 0f64,
#[cfg(feature = "enable_cache_record")]
cache_record: CacheDbRecord::default(),
}
} else {
CacheDB::new(State::new(state))
Expand Down
4 changes: 1 addition & 3 deletions crates/rpc/rpc/src/eth/api/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,7 @@ where
for tx in transactions {
let tx = tx.into_ecrecovered().ok_or(BlockError::InvalidSignature)?;
let tx = tx_env_with_recovered(&tx);
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env =
Env { cfg: cfg.clone(), block: block_env.clone(), tx, cpu_frequency: 0f64 };
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };
let (res, _) = transact(&mut db, env)?;
db.commit(res.state);
}
Expand Down
9 changes: 2 additions & 7 deletions crates/rpc/rpc/src/eth/api/pending_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,8 @@ impl PendingBlockEnv {
let tx = pool_tx.to_recovered_transaction();

// Configure the environment for the block.
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env {
cfg: cfg.clone(),
block: block_env.clone(),
tx: tx_env_with_recovered(&tx),
cpu_frequency: 0f64,
};
let env =
Env { cfg: cfg.clone(), block: block_env.clone(), tx: tx_env_with_recovered(&tx) };

let mut evm = revm::EVM::with_env(env);
evm.database(&mut db);
Expand Down
4 changes: 1 addition & 3 deletions crates/rpc/rpc/src/eth/api/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -667,9 +667,7 @@ where
// replay all transactions prior to the targeted transaction
replay_transactions_until(&mut db, cfg.clone(), block_env.clone(), block_txs, tx.hash)?;

// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env =
Env { cfg, block: block_env, tx: tx_env_with_recovered(&tx), cpu_frequency: 0f64 };
let env = Env { cfg, block: block_env, tx: tx_env_with_recovered(&tx) };

let mut inspector = TracingInspector::new(config);
let (res, _, db) = inspect_and_return_db(db, env, &mut inspector)?;
Expand Down
18 changes: 6 additions & 12 deletions crates/rpc/rpc/src/eth/revm_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use reth_rpc_types::{
state::{AccountOverride, StateOverride},
BlockOverrides, CallRequest,
};
#[cfg(feature = "enable_cache_record")]
use revm::db::CacheDbRecord;
use revm::{
db::{CacheDB, EmptyDB},
precompile::{Precompiles, SpecId as PrecompilesSpecId},
Expand Down Expand Up @@ -179,8 +181,7 @@ where
I: IntoIterator<Item = Tx>,
Tx: FillableTransaction,
{
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env { cfg, block: block_env, tx: TxEnv::default(), cpu_frequency: 0f64 };
let env = Env { cfg, block: block_env, tx: TxEnv::default() };
let mut evm = revm::EVM::with_env(env);
evm.database(db);
for tx in transactions.into_iter() {
Expand Down Expand Up @@ -267,8 +268,7 @@ pub(crate) fn build_call_evm_env(
request: CallRequest,
) -> EthResult<Env> {
let tx = create_txn_env(&block, request)?;
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env { cfg, block, tx, cpu_frequency: 0f64 };
let env = Env { cfg, block, tx };
Ok(env)
}

Expand Down Expand Up @@ -518,14 +518,8 @@ where
logs: db.logs.clone(),
block_hashes: db.block_hashes.clone(),
db: Default::default(),
#[cfg(feature = "enable_opcode_metrics")]
cache_hits: (0u64, 0u64, 0u64, 0u64),
#[cfg(feature = "enable_opcode_metrics")]
cache_misses: (0u64, 0u64, 0u64, 0u64),
#[cfg(feature = "enable_opcode_metrics")]
cache_misses_penalty: (0u128, 0u128, 0u128, 0u128),
#[cfg(feature = "enable_opcode_metrics")]
cpu_frequency: 0f64,
#[cfg(feature = "enable_cache_record")]
cache_record: CacheDbRecord::default(),
}
}

Expand Down
7 changes: 2 additions & 5 deletions crates/rpc/rpc/src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,7 @@ where
.evm_env_at(block_id.unwrap_or(BlockId::Number(BlockNumberOrTag::Latest)))
.await?;
let tx = tx_env_with_recovered(&tx.into_ecrecovered_transaction());
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env = Env { cfg, block, tx, cpu_frequency: 0f64 };
let env = Env { cfg, block, tx };

let config = tracing_config(&trace_types);

Expand Down Expand Up @@ -339,9 +338,7 @@ where
};

let tx = tx_env_with_recovered(&tx);
// #[cfg(feature = "enable_opcode_metrics")]// Error: why this?
let env =
Env { cfg: cfg.clone(), block: block_env.clone(), tx, cpu_frequency: 0f64 };
let env = Env { cfg: cfg.clone(), block: block_env.clone(), tx };

let mut inspector = TracingInspector::new(config);
let (res, _) = inspect(&mut db, env, &mut inspector)?;
Expand Down
Loading

0 comments on commit 79dcfaf

Please sign in to comment.