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: refactor the performance measurement code. #46

Merged
merged 1 commit into from
Feb 29, 2024
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
18 changes: 1 addition & 17 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -202,23 +202,7 @@ finish_after_execution_stage = ["reth-stages/finish_after_execution_stage"]
enable_execution_duration_record = [
"perf-metrics/enable_execution_duration_record",
"reth-stages/enable_execution_duration_record",
"open_performance_dashboard",
]
enable_test_max_th = ["reth-stages/enable_test_max_th"]
enable_db_speed_record = [
"perf-metrics/enable_db_speed_record",
"reth-stages/enable_db_speed_record",
"open_performance_dashboard",
]
enable_execute_measure = [
"perf-metrics/enable_execute_measure",
"reth-revm/enable_execute_measure",
"reth-stages/enable_execute_measure",
"open_performance_dashboard",
]
enable_write_to_db_measure = [
"perf-metrics/enable_write_to_db_measure",
"reth-stages/enable_write_to_db_measure",
"reth-revm/enable_execution_duration_record",
"open_performance_dashboard",
]

Expand Down
7 changes: 2 additions & 5 deletions crates/perf-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ enable_opcode_metrics = [
"revm-utils",
"revm/enable_opcode_metrics"]
enable_cache_record = ["revm-utils"]
enable_execution_duration_record = ["revm-utils"]
enable_tps_gas_record = []
enable_db_speed_record = []
enable_execute_measure = ["revm-utils"]
enable_write_to_db_measure = ["revm-utils",
enable_execution_duration_record = ["revm-utils",
"revm/enable_transact_measure",
]
enable_tps_gas_record = ["revm-utils"]
117 changes: 117 additions & 0 deletions crates/perf-metrics/src/dashboard/cache.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! This module is used to support the display of cached state related metrics.
use super::commons::*;
use revm_utils::{
metrics::types::{CacheDbRecord, Function},
time_utils::convert_cycles_to_ns_f64,
};

const COL_WIDTH_BIG: usize = 20;
const COL_WIDTH_MIDDLE: usize = 14;

#[derive(Default, Debug, Copy, Clone)]
struct CacheStat {
hits: u64,
misses: u64,
miss_ratio: f64,
penalty: f64,
avg_penalty: f64,
}

const CACHE_STATS_LEN: usize = 5;
#[derive(Debug)]
struct CacheStats {
functions: [CacheStat; CACHE_STATS_LEN],
}

impl CacheStats {
fn print_item(&self, function: &str, index: usize) {
println!(
"{: <COL_WIDTH_BIG$}{:>COL_WIDTH_MIDDLE$}{:>COL_WIDTH_MIDDLE$}{:>COL_WIDTH_BIG$.3}{:>COL_WIDTH_BIG$.3}{:>COL_WIDTH_BIG$.3}",
function, self.functions[index].hits, self.functions[index].misses, self.functions[index].miss_ratio * 100.0, self.functions[index].penalty, self.functions[index].avg_penalty
);
}
}

impl Default for CacheStats {
fn default() -> Self {
CacheStats { functions: [CacheStat::default(); CACHE_STATS_LEN] }
}
}

impl Print for CacheStats {
fn print_title(&self) {
println!("================================================ Metric of State ===========================================");
println!(
"{: <COL_WIDTH_BIG$}{:>COL_WIDTH_MIDDLE$}{:>COL_WIDTH_MIDDLE$}{:>COL_WIDTH_BIG$}{:>COL_WIDTH_BIG$}{:>COL_WIDTH_BIG$}",
"State functions", "Hits", "Misses", "Miss ratio (%)","Penalty time(s)", "Avg penalty (us)"
);
}

fn print_content(&self) {
self.print_item("blockhash", Function::BlockHash as usize);
self.print_item("code_by_hash", Function::CodeByHash as usize);
self.print_item("load_account/basic", Function::LoadCacheAccount as usize);
self.print_item("storage", Function::Storage as usize);
self.print_item("total", CACHE_STATS_LEN - 1);
}
}

trait StatsAndPrint {
fn stats(&self) -> CacheStats;
fn print_penalty_distribution(&self);
}

impl StatsAndPrint for CacheDbRecord {
fn stats(&self) -> CacheStats {
let mut cache_stats = CacheStats::default();

let total_stats = self.access_count();
let hit_stats = self.hit_stats();
let miss_stats = self.miss_stats();
let penalty_stats = self.penalty_stats();

for index in 0..total_stats.function.len() {
cache_stats.functions[index].hits = hit_stats.function[index];
cache_stats.functions[index].misses = miss_stats.function[index];
cache_stats.functions[index].miss_ratio =
miss_stats.function[index] as f64 / total_stats.function[index] as f64;
cache_stats.functions[index].penalty =
cycles_as_secs(penalty_stats.time.function[index]);
cache_stats.functions[index].avg_penalty =
convert_cycles_to_ns_f64(penalty_stats.time.function[index]) /
(1000 * miss_stats.function[index]) as f64;
}
cache_stats.functions[CACHE_STATS_LEN - 1].hits = hit_stats.function.iter().sum();
cache_stats.functions[CACHE_STATS_LEN - 1].misses = miss_stats.function.iter().sum();
cache_stats.functions[CACHE_STATS_LEN - 1].miss_ratio =
cache_stats.functions[CACHE_STATS_LEN - 1].misses as f64 /
total_stats.function.iter().sum::<u64>() as f64;
cache_stats.functions[CACHE_STATS_LEN - 1].penalty =
cycles_as_secs(penalty_stats.time.function.iter().sum());
cache_stats.functions[CACHE_STATS_LEN - 1].avg_penalty =
convert_cycles_to_ns_f64(penalty_stats.time.function.iter().sum()) /
(1000 * cache_stats.functions[CACHE_STATS_LEN - 1].misses) as f64;

cache_stats
}

fn print_penalty_distribution(&self) {
println!();
println!("================Penalty percentile=============");
self.penalty_stats().percentile.print_content();
println!();
}
}

impl Print for CacheDbRecord {
fn print(&self, _block_number: u64) {
self.stats().print(_block_number);
self.print_penalty_distribution();
}
}

pub(super) fn print_state_size(block_number: u64, size: usize) {
println!();
println! {"block_number: {:?}, State size: {:?}", block_number, size};
println!();
}
77 changes: 77 additions & 0 deletions crates/perf-metrics/src/dashboard/commons.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#[cfg(any(feature = "enable_opcode_metrics", feature = "enable_cache_record",))]
use revm_utils::metrics::types::TimeDistributionStats;

/// This trait is used to support the display of metric records.
pub trait Print {
fn print_title(&self) {}
fn print_content(&self) {}
fn print(&self, _block_number: u64) {
println!();
self.print_title();
self.print_content();
println!();
}
}

#[cfg(any(feature = "enable_opcode_metrics", feature = "enable_cache_record",))]
const COL_WIDTH: usize = 15;

#[cfg(any(feature = "enable_opcode_metrics", feature = "enable_cache_record",))]
impl Print for TimeDistributionStats {
fn print_content(&self) {
let total_cnt: u64 = self.us_percentile.iter().map(|&v| v).sum();
let mut cuml = 0.0;

println!(
"{:<COL_WIDTH$} {:>COL_WIDTH$} {:>COL_WIDTH$}",
"Time (ns)", "Count (%)", "Cuml. (%)"
);
for index in 0..self.span_in_ns {
let pct = self.ns_percentile[index] as f64 / total_cnt as f64;
cuml += pct;
println!(
"{:<COL_WIDTH$} {:>COL_WIDTH$.3} {:>COL_WIDTH$.3}",
(index + 1) * 100,
pct * 100.0,
cuml * 100.0
);
}

let ns_span_in_us = ((self.span_in_ns * 100) as f64 / 1000.0) as usize;
for index in ns_span_in_us..self.span_in_us {
let pct = self.us_percentile[index] as f64 / total_cnt as f64;
cuml += pct;
println!(
"{:<COL_WIDTH$} {:>COL_WIDTH$.3} {:>COL_WIDTH$.3}",
(index + 1) * 1000,
pct * 100.0,
cuml * 100.0
);
}

println!();
println!();
println!("========>for debug:");
println!("total cnt: {:?}", total_cnt);
println!("span_in_ns: {:?}", self.span_in_ns);
println!("span_in_us: {:?}", self.span_in_us);
println!("in_ns: {:?}", self.ns_percentile);
println!("in_us: {:?}", self.us_percentile);
println!();
println!();
}
}

#[cfg(any(
feature = "enable_opcode_metrics",
feature = "enable_cache_record",
feature = "enable_execution_duration_record",
))]
pub(super) fn cycles_as_secs(cycles: u64) -> f64 {
revm_utils::time_utils::convert_cycles_to_duration(cycles).as_secs_f64()
}

#[cfg(any(feature = "enable_execution_duration_record"))]
pub(super) fn convert_bytes_to_mega(size: usize) -> f64 {
size as f64 / 1024.0 / 1024.0
}
Loading
Loading