Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Move stats to bin #1407

Merged
merged 14 commits into from
May 16, 2023
100 changes: 2 additions & 98 deletions Cargo.lock

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

8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ exp_bench: ## Run Exp Circuit benchmarks
circuit_benches: evm_bench state_bench ## Run All Circuit benchmarks

stats_state_circuit: # Print a table with State Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_state_states_stats -- --nocapture --ignored
@cargo run --bin stats --features stats -- state

stats_evm_circuit: # Print a table with EVM Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_evm_states_stats -- --nocapture --ignored
@cargo run --bin stats --features stats -- evm

stats_copy_circuit: # Print a table with Copy Circuit stats by ExecState/opcode
@cargo test -p zkevm-circuits --features=test,warn-unimplemented get_copy_states_stats -- --nocapture --ignored
@cargo run --bin stats --features stats -- copy

evm_exec_steps_occupancy: # Print a table for each EVM-CellManager CellType with the top 10 occupancy ExecutionSteps associated
@cargo test -p zkevm-circuits --release get_exec_steps_occupancy --features=test,warn-unimplemented -- --nocapture --ignored
@cargo run --bin stats --features stats -- exec

.PHONY: clippy doc fmt test test_benches test-all evm_bench state_bench circuit_benches evm_exec_steps_occupancy stats_state_circuit stats_evm_circuit stats_copy_circuit help
9 changes: 6 additions & 3 deletions zkevm-circuits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ maingate = { git = "https://github.com/privacy-scaling-explorations/halo2wrong"
integer = { git = "https://github.com/privacy-scaling-explorations/halo2wrong", tag = "v2023_04_20" }
libsecp256k1 = "0.7"
num-bigint = { version = "0.4" }
subtle = "2.4"
rand_chacha = "0.3"
snark-verifier = { git = "https://github.com/privacy-scaling-explorations/snark-verifier", tag = "v2023_04_20", default-features = false, features = ["loader_halo2", "system_halo2"] }
cli-table = { version = "0.4", optional = true }

[dev-dependencies]
bus-mapping = { path = "../bus-mapping", features = ["test"] }
criterion = "0.3"
ctor = "0.1.22"
ethers-signers = "0.17.0"
hex = "0.4.3"
itertools = "0.10.1"
mock = { path = "../mock" }
pretty_assertions = "1.0.0"
cli-table = "0.4"
serde = { version = "1.0.130", features = ["derive"] }
serde_json = "1.0.78"

Expand All @@ -55,3 +53,8 @@ default = []
test = ["ethers-signers", "mock", "bus-mapping/test"]
test-circuits = []
warn-unimplemented = ["eth-types/warn-unimplemented"]
stats = ["warn-unimplemented", "dep:cli-table"]

[[bin]]
name = "stats"
required-features = ["stats"]
Original file line number Diff line number Diff line change
@@ -1,61 +1,17 @@
use std::cmp::Ordering;

use crate::evm_circuit::step::ExecutionState;
use bus_mapping::{
circuit_input_builder::{self, CircuitsParams, ExecState},
mock::BlockData,
};
use cli_table::{
format::{Justify, Separator},
print_stdout, Table, WithTitle,
};
use eth_types::{bytecode, evm_types::OpcodeId, geth_types::GethData, Address, Bytecode, ToWord};
use mock::{eth, test_ctx::TestContext, MOCK_ACCOUNTS};
use strum::IntoEnumIterator;

/// Helper type to print formatted tables in MarkDown
pub(crate) struct DisplayTable<const N: usize> {
header: [String; N],
rows: Vec<[String; N]>,
}

impl<const N: usize> DisplayTable<N> {
pub(crate) fn new(header: [String; N]) -> Self {
Self {
header,
rows: Vec::new(),
}
}
fn push_row(&mut self, row: [String; N]) {
self.rows.push(row)
}
fn print_row(row: &[String; N], rows_width: &[usize; N]) {
for (i, h) in row.iter().enumerate() {
if i == 0 {
print!("|");
}
print!(" {:width$} |", h, width = rows_width[i]);
}
println!();
}
pub(crate) fn print(&self) {
let mut rows_width = [0; N];
for row in std::iter::once(&self.header).chain(self.rows.iter()) {
for (i, s) in row.iter().enumerate() {
if s.len() > rows_width[i] {
rows_width[i] = s.len();
}
}
}
Self::print_row(&self.header, &rows_width);
for (i, width) in rows_width.iter().enumerate() {
if i == 0 {
print!("|");
}
print!(" {:-<width$} |", "", width = width);
}
println!();
for row in &self.rows {
Self::print_row(row, &rows_width);
}
}
}
use zkevm_circuits::evm_circuit::step::ExecutionState;

/// Generate the prefix bytecode to trigger a big amount of rw operations
pub(crate) fn bytecode_prefix_op_big_rws(opcode: OpcodeId) -> Bytecode {
Expand Down Expand Up @@ -106,12 +62,34 @@ pub(crate) fn bytecode_prefix_op_big_rws(opcode: OpcodeId) -> Bytecode {
}
}

/// Wrap f64 for both sorting and pretty formatting
#[derive(PartialEq, PartialOrd)]
struct PrettyF64(f64);

impl std::fmt::Display for PrettyF64 {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:1.3}", self.0)
}
}

impl From<f64> for PrettyF64 {
fn from(value: f64) -> Self {
Self(value)
}
}

#[derive(Table)]
struct Row {
#[table(title = "Execution State")]
state: ExecutionState,
#[table(title = "Opcode")]
opcode: OpcodeId,
#[table(title = "Height", justify = "Justify::Right")]
height: usize,
#[table(title = "Gas Cost", justify = "Justify::Right")]
gas_cost: u64,
height_per_gas: f64,
#[table(title = "Height per Gas", justify = "Justify::Right")]
height_per_gas: PrettyF64,
}

/// This function prints to stdout a table with all the implemented states
Expand Down Expand Up @@ -161,7 +139,6 @@ pub(crate) fn print_circuit_stats_by_states(
STOP
};

let mut table = DisplayTable::new(["state", "opcode", "h", "g", "h/g"].map(|s| s.into()));
let mut rows = vec![];
for state in implemented_states {
if !fn_filter(state) {
Expand Down Expand Up @@ -251,7 +228,7 @@ pub(crate) fn print_circuit_stats_by_states(
opcode,
height,
gas_cost,
height_per_gas: height as f64 / gas_cost as f64,
height_per_gas: (height as f64 / gas_cost as f64).into(),
});
}
}
Expand All @@ -261,16 +238,6 @@ pub(crate) fn print_circuit_stats_by_states(
.unwrap_or(Ordering::Greater)
});

for row in rows.iter() {
let row = [
format!("{:?}", row.state),
format!("{:?}", row.opcode),
format!("{}", row.height),
format!("{}", row.gas_cost),
format!("{:1.3}", row.height_per_gas),
];
table.push_row(row);
}

table.print();
print_stdout(rows.with_title().separator(Separator::builder().build()))
.expect("the table renders");
}
Loading