Skip to content

Commit

Permalink
Revert "refactor: refactoring of perf metric related code. (#48)"
Browse files Browse the repository at this point in the history
This reverts commit 3203e2a.
  • Loading branch information
anonymousGiga authored Mar 7, 2024
1 parent 3203e2a commit f8f8ec8
Show file tree
Hide file tree
Showing 10 changed files with 600 additions and 200 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions crates/perf-metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ tokio = { workspace = true, features = ["sync"] }
revm-utils = { workspace = true, optional = true }
revm = { workspace = true, optional = true }

ffi = { package = "reth-mdbx-sys", path = "../storage/libmdbx-rs/mdbx-sys" }
libc = "0.2"

[features]
enable_opcode_metrics = [
"revm-utils",
Expand Down
65 changes: 32 additions & 33 deletions crates/perf-metrics/src/dashboard/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,37 @@ impl Default for CacheStats {
}
}

impl From<&CacheDbRecord> for CacheStats {
fn from(record: &CacheDbRecord) -> Self {
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 = record.access_count();
let hit_stats = record.hit_stats();
let miss_stats = record.miss_stats();
let penalty_stats = record.penalty_stats();
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];
Expand All @@ -71,32 +94,8 @@ impl From<&CacheDbRecord> for CacheStats {

cache_stats
}
}

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 PrintPenalty {
fn print_penalty(&self);
}

impl PrintPenalty for CacheDbRecord {
fn print_penalty(&self) {
fn print_penalty_distribution(&self) {
println!();
println!("================Penalty percentile=============");
self.penalty_stats().percentile.print_content();
Expand All @@ -106,8 +105,8 @@ impl PrintPenalty for CacheDbRecord {

impl Print for CacheDbRecord {
fn print(&self, _block_number: u64) {
Into::<CacheStats>::into(self).print(_block_number);
self.print_penalty();
self.stats().print(_block_number);
self.print_penalty_distribution();
}
}

Expand Down
207 changes: 104 additions & 103 deletions crates/perf-metrics/src/dashboard/opcode.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
//! This module is used to support the display of opcode statistics metrics.
use super::commons::*;
use revm::revm_opcode::*;
use revm_utils::{metrics::types::OpcodeRecord, time_utils::convert_cycles_to_ns_f64};
use std::collections::BTreeMap;
use revm_utils::metrics::types::OpcodeRecord;

const MGAS_TO_GAS: u64 = 1_000_000u64;
const COL_WIDTH: usize = 15;
const OPCODE_NUMBER: usize = 256;
pub(crate) const OPCODE_NUMBER: usize = 256;

#[derive(Debug, Clone, Copy, Default)]
struct OpcodeInfo {
pub(crate) struct OpcodeInfo {
/// opcode category
category: &'static str,
pub(crate) category: &'static str,
/// gas fee
gas: u64,
pub(crate) gas: u64,
/// opcode cost a fixed gas fee?
static_gas: bool,
pub(crate) static_gas: bool,
}

const MERGE_MAP: [Option<(u8, OpcodeInfo)>; OPCODE_NUMBER] = [
pub(crate) const MERGE_MAP: [Option<(u8, OpcodeInfo)>; OPCODE_NUMBER] = [
Some((STOP, OpcodeInfo { category: "stop", gas: 0, static_gas: true })), //0x00
Some((ADD, OpcodeInfo { category: "arithmetic", gas: 3, static_gas: true })), //0x01
Some((MUL, OpcodeInfo { category: "arithmetic", gas: 5, static_gas: true })), //0x02
Expand Down Expand Up @@ -277,6 +273,12 @@ const MERGE_MAP: [Option<(u8, OpcodeInfo)>; OPCODE_NUMBER] = [
Some((SELFDESTRUCT, OpcodeInfo { category: "host", gas: 5000, static_gas: false })), //0xff
];

use super::commons::*;
use revm_utils::time_utils::convert_cycles_to_ns_f64;
use std::collections::BTreeMap;
const MGAS_TO_GAS: u64 = 1_000_000u64;

const COL_WIDTH: usize = 15;
#[derive(Default, Debug)]
struct OpcodeMergeRecord {
count: u64,
Expand Down Expand Up @@ -336,6 +338,92 @@ impl OpcodeStat {
}
}

#[derive(Debug)]
struct OpcodeStats {
overall: OpcodeStat,
opcode: [Option<OpcodeStat>; OPCODE_NUMBER],
merge_records: BTreeMap<&'static str, OpcodeMergeRecord>,
}

const ARRAY_REPEAT_VALUE: std::option::Option<OpcodeStat> = None;
impl Default for OpcodeStats {
fn default() -> Self {
Self {
overall: OpcodeStat::default(),
opcode: [ARRAY_REPEAT_VALUE; OPCODE_NUMBER],
merge_records: BTreeMap::new(),
}
}
}

impl OpcodeStats {
fn print_opcode_title(&self) {
println!("===========================================================================Metric of instruction======================================================================");
println!(
"{: <COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$} \
{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}",
"Opcode",
"Count",
"Count (%)",
"Time (s)",
"Time (%)",
"Cost (ns)",
"Total Mgas",
"Gas (%)",
"Static gas",
"Dyn. gas",
"Category"
);
}

fn print_opcodes(&self) {
println!();
self.print_opcode_title();
self.overall.print("overall");
for i in 0..OPCODE_NUMBER {
let name = OpCode::new(i as u8);
if name.is_none() {
continue
}
self.opcode[i]
.as_ref()
.expect("opcode record should not empty")
.print(name.unwrap().as_str());
}
println!();
}

fn print_category(&self) {
println!("\n");
println!("==========================================================================================");
println!(
"{:<COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}",
"Opcode Cat.", "Count", "Count (%)", "Time (s)", "Time (%)", "Cost (ns)",
);

for (k, v) in self.merge_records.iter() {
if *k == "" {
continue
}
println!(
"{:<COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$.2}{:>COL_WIDTH$.1}{:>COL_WIDTH$.3}{:>COL_WIDTH$.3}",
*k,
v.count,
v.count_pct * 100.0,
cycles_as_secs(v.time),
v.time_pct * 100.0,
v.avg_cost,
);
}
}
}

trait SupportPrint {
fn stats(&self) -> OpcodeStats;
fn print_addition_count(&self);
fn print_sload_percentile(&self);
}

// Return (total_gas, static_gas, dyn_gas).
fn caculate_gas(opcode: u8, count: u64, total_gas: i128) -> (f64, u64, f64) {
let (base_gas, is_static) = match MERGE_MAP[opcode as usize] {
Expand All @@ -361,29 +449,11 @@ fn category_name(opcode: u8) -> Option<&'static str> {
Some(MERGE_MAP[opcode as usize]?.1.category)
}

#[derive(Debug)]
struct OpcodeStats {
overall: OpcodeStat,
opcode: [Option<OpcodeStat>; OPCODE_NUMBER],
merge_records: BTreeMap<&'static str, OpcodeMergeRecord>,
}

const ARRAY_REPEAT_VALUE: std::option::Option<OpcodeStat> = None;
impl Default for OpcodeStats {
fn default() -> Self {
Self {
overall: OpcodeStat::default(),
opcode: [ARRAY_REPEAT_VALUE; OPCODE_NUMBER],
merge_records: BTreeMap::new(),
}
}
}

impl From<&OpcodeRecord> for OpcodeStats {
fn from(record: &OpcodeRecord) -> Self {
impl SupportPrint for OpcodeRecord {
fn stats(&self) -> OpcodeStats {
let mut opcode_stats = OpcodeStats::default();
// induction
for (i, v) in record.opcode_record.iter().enumerate() {
for (i, v) in self.opcode_record.iter().enumerate() {
// opcode
let op = i as u8;
let mut opcode_stat = OpcodeStat::default();
Expand Down Expand Up @@ -427,7 +497,7 @@ impl From<&OpcodeRecord> for OpcodeStats {
}

// calculate opcode pct
for (i, _v) in record.opcode_record.iter().enumerate() {
for (i, _v) in self.opcode_record.iter().enumerate() {
opcode_stats.opcode[i].as_mut().expect("empty").count_pct =
opcode_stats.opcode[i].as_mut().expect("empty").count as f64 /
opcode_stats.overall.count as f64;
Expand All @@ -453,76 +523,7 @@ impl From<&OpcodeRecord> for OpcodeStats {

opcode_stats
}
}

impl OpcodeStats {
fn print_opcode_title(&self) {
println!("===========================================================================Metric of instruction======================================================================");
println!(
"{: <COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$} \
{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}",
"Opcode",
"Count",
"Count (%)",
"Time (s)",
"Time (%)",
"Cost (ns)",
"Total Mgas",
"Gas (%)",
"Static gas",
"Dyn. gas",
"Category"
);
}

fn print_opcodes(&self) {
println!();
self.print_opcode_title();
self.overall.print("overall");
for i in 0..OPCODE_NUMBER {
let name = OpCode::new(i as u8);
if name.is_none() {
continue
}
self.opcode[i]
.as_ref()
.expect("opcode record should not empty")
.print(name.unwrap().as_str());
}
println!();
}

fn print_category(&self) {
println!("\n");
println!("==========================================================================================");
println!(
"{:<COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$}",
"Opcode Cat.", "Count", "Count (%)", "Time (s)", "Time (%)", "Cost (ns)",
);

for (k, v) in self.merge_records.iter() {
if *k == "" {
continue
}
println!(
"{:<COL_WIDTH$}{:>COL_WIDTH$}{:>COL_WIDTH$.2}{:>COL_WIDTH$.1}{:>COL_WIDTH$.3}{:>COL_WIDTH$.3}",
*k,
v.count,
v.count_pct * 100.0,
cycles_as_secs(v.time),
v.time_pct * 100.0,
v.avg_cost,
);
}
}
}

trait ExtraPrint {
fn print_addition_count(&self);
fn print_sload_percentile(&self);
}

impl ExtraPrint for OpcodeRecord {
fn print_addition_count(&self) {
println!();
println!("call additional rdtsc count: {}", self.additional_count[0]);
Expand All @@ -540,7 +541,7 @@ impl ExtraPrint for OpcodeRecord {

impl Print for OpcodeRecord {
fn print(&self, _block_number: u64) {
let opcode_stats: OpcodeStats = self.into();
let opcode_stats = self.stats();
opcode_stats.print_opcodes();
opcode_stats.print_category();
self.print_addition_count();
Expand Down
Loading

0 comments on commit f8f8ec8

Please sign in to comment.