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

stylus_benchmark #2827

Open
wants to merge 57 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
fdb42c8
wip
diegoximenes Nov 18, 2024
6705932
Revert "wip"
diegoximenes Nov 19, 2024
d94665a
jit as a library and binary
diegoximenes Nov 21, 2024
cd1642d
Beginning of stylus_benchmark
diegoximenes Nov 21, 2024
25a770b
stylus_benchmark: create_stylus_config and create_evm_data2
diegoximenes Nov 21, 2024
472a662
Refactors jit to have *_with_wasm functions
diegoximenes Nov 25, 2024
f327531
Basic CallProgramLoop in stylus_benchmark
diegoximenes Nov 25, 2024
2aa59c9
Adds debug.toggle_measurement instruction
diegoximenes Nov 26, 2024
61620f9
toggle_measurement ink tracking
diegoximenes Nov 27, 2024
5e17f53
Run benchmark multiple times for the same wat file
diegoximenes Nov 28, 2024
89eeee8
toggle_measurement to toggle_benchmark
diegoximenes Nov 28, 2024
7d9819c
Adds whole Benchmark object to MessageFromCothread
diegoximenes Nov 28, 2024
55463ed
Stores benchmark as an Option<Benchmark> in WasmEnv
diegoximenes Nov 28, 2024
e9a74d9
reuse compiled module
diegoximenes Dec 9, 2024
1198dfa
Uses ? instead of unwrap when appropriate
diegoximenes Dec 9, 2024
d53bcc2
Small refactor on stylus_benchmark
diegoximenes Dec 9, 2024
d2668f6
stylus_benchmark: from to_result to check_result
diegoximenes Dec 9, 2024
c4fb5df
Removes send_response_with_wasm_env
diegoximenes Dec 10, 2024
f36013f
Removes cycle from Benchmark
diegoximenes Dec 10, 2024
24dac3e
Renames add_one.wat to add_i32.wat. Uses loop in add_i32.wat
diegoximenes Dec 10, 2024
3df9b24
stylus_benchmark: print ink usage
diegoximenes Dec 10, 2024
87f7386
Improves prints in stylus_benchmark
diegoximenes Dec 10, 2024
654d18a
fix cargo fmt
diegoximenes Dec 10, 2024
15ca778
fix cargo fmt
diegoximenes Dec 10, 2024
a813433
Adds rust fmt to stylus_benchmark in ci
diegoximenes Dec 10, 2024
1f1e5e3
Improve panics msgs in stylus_benchmark
diegoximenes Dec 10, 2024
175e39b
generate-wats subcommand
diegoximenes Dec 10, 2024
922fce8
Split benchmark and generate_wats in different files
diegoximenes Dec 10, 2024
cac471c
Adds benchmark to user-host Program
diegoximenes Dec 11, 2024
33e0f90
Adds benchmark to user-test Program
diegoximenes Dec 11, 2024
e8170f0
Improves panic message when toggle_measurement block doesn't exist
diegoximenes Dec 11, 2024
4cd4d13
Fix version used to compile in stylus_benchmark
diegoximenes Dec 13, 2024
bced42f
Small refactor to avoid code duplication
diegoximenes Dec 13, 2024
0fb2960
Change benchmark function arguments
diegoximenes Dec 13, 2024
26a7893
Prints avg_ink_spent_per_micro_second
diegoximenes Dec 13, 2024
0c48cab
Fix rustfmt complaints in stylus_benchmark
diegoximenes Dec 13, 2024
382da91
Fixes typo
diegoximenes Dec 13, 2024
aa0da72
stylus_benchmark: change args to --output-wat-dir-path <OUTPUT_WAT_DI…
diegoximenes Dec 18, 2024
ecb5172
Uses strum in stylus_benchmark
diegoximenes Dec 19, 2024
486cc67
Run all scenarios in stylus_benchmark
diegoximenes Dec 19, 2024
03e8315
Do not import functions in stylus_benchmark
diegoximenes Dec 19, 2024
6d59589
Adds comment to Benchmark struct
diegoximenes Dec 19, 2024
babadc9
Uses loop in benchmark programs
diegoximenes Dec 19, 2024
7fbd08a
start_benchmark/end_benchmark instead of toggle_benchmark
diegoximenes Dec 20, 2024
d3ad3e6
Stores Benchmark instead of Option<Benchmark> in WasmEnv
diegoximenes Dec 20, 2024
1fcab9f
Divide wat generation in multiple functions
diegoximenes Dec 20, 2024
367f77b
Benchmark XorI32
diegoximenes Dec 20, 2024
56b3354
Avoids unnecessary derivative
diegoximenes Dec 20, 2024
0ae7046
Fixes user-host
diegoximenes Dec 20, 2024
cc8ba64
Increases number of operations
diegoximenes Dec 20, 2024
8af1b4b
Fixes user-test
diegoximenes Dec 23, 2024
b0e79e5
Fix clippy check
diegoximenes Dec 23, 2024
c270a85
Add comment about the strategy of the programs to be benchmarked
diegoximenes Dec 23, 2024
667d6ac
Improves wat programs to be benchmarked generation
diegoximenes Dec 23, 2024
6aa0c91
Fix lint
diegoximenes Dec 23, 2024
33a80b3
Fix lint
diegoximenes Dec 23, 2024
28c045b
Fix lint
diegoximenes Dec 23, 2024
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ arbitrator/tools/wasmer/target/
arbitrator/tools/wasm-tools/
arbitrator/tools/pricers/
arbitrator/tools/module_roots/
arbitrator/tools/stylus_benchmark
arbitrator/langs/rust/target/
arbitrator/langs/bf/target/

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/arbitrator-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ jobs:
- name: Rustfmt - langs/rust
run: cargo fmt --all --manifest-path arbitrator/langs/rust/Cargo.toml -- --check

- name: Rustfmt - tools/stylus_benchmark
run: cargo fmt --all --manifest-path arbitrator/tools/stylus_benchmark/Cargo.toml -- --check

- name: Make proofs from test cases
run: make -j test-gen-proofs

Expand Down
1 change: 1 addition & 0 deletions arbitrator/Cargo.lock

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

1 change: 1 addition & 0 deletions arbitrator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
exclude = [
"stylus/tests/",
"tools/wasmer/",
"tools/stylus_benchmark",
]
resolver = "2"

Expand Down
1 change: 1 addition & 0 deletions arbitrator/arbutil/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ serde = { version = "1.0.130", features = ["derive", "rc"] }
num_enum = "0.7.1"
sha2 = "0.10.7"
sha3 = "0.10.8"
derivative = "2.2.0"
15 changes: 15 additions & 0 deletions arbitrator/arbutil/src/benchmark.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

use crate::evm::api::Ink;
use derivative::Derivative;
use std::time::{Duration, Instant};

#[derive(Derivative, Clone, Copy)]
#[derivative(Debug)]
diegoximenes marked this conversation as resolved.
Show resolved Hide resolved
pub struct Benchmark {
diegoximenes marked this conversation as resolved.
Show resolved Hide resolved
pub timer: Instant,
pub elapsed: Option<Duration>,
pub ink_start: Ink,
pub ink_total: Option<Ink>,
}
1 change: 1 addition & 0 deletions arbitrator/arbutil/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2022-2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

pub mod benchmark;
/// cbindgen:ignore
pub mod color;
pub mod crypto;
Expand Down
51 changes: 51 additions & 0 deletions arbitrator/jit/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright 2021-2024, Offchain Labs, Inc.
// For license information, see https://github.com/OffchainLabs/nitro/blob/master/LICENSE

use std::path::PathBuf;
use structopt::StructOpt;

mod arbcompress;
mod caller_env;
pub mod machine;
mod prepare;
pub mod program;
mod socket;
pub mod stylus_backend;
mod test;
mod wasip1_stub;
mod wavmio;

#[derive(StructOpt)]
#[structopt(name = "jit-prover")]
pub struct Opts {
#[structopt(short, long)]
binary: PathBuf,
#[structopt(long, default_value = "0")]
inbox_position: u64,
#[structopt(long, default_value = "0")]
delayed_inbox_position: u64,
#[structopt(long, default_value = "0")]
position_within_message: u64,
#[structopt(long)]
last_block_hash: Option<String>,
#[structopt(long)]
last_send_root: Option<String>,
#[structopt(long)]
inbox: Vec<PathBuf>,
#[structopt(long)]
delayed_inbox: Vec<PathBuf>,
#[structopt(long)]
preimages: Option<PathBuf>,
#[structopt(long)]
cranelift: bool,
#[structopt(long)]
forks: bool,
#[structopt(long)]
pub debug: bool,
#[structopt(long)]
pub require_success: bool,
// JSON inputs supercede any of the command-line inputs which could
// be specified in the JSON file.
#[structopt(long)]
json_inputs: Option<PathBuf>,
}
51 changes: 3 additions & 48 deletions arbitrator/jit/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,58 +1,13 @@
// Copyright 2022-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

use crate::machine::{Escape, WasmEnv};
use arbutil::{color, Color};
use eyre::Result;
use std::path::PathBuf;
use jit::machine;
use jit::machine::{Escape, WasmEnv};
use jit::Opts;
use structopt::StructOpt;

mod arbcompress;
mod caller_env;
mod machine;
mod prepare;
mod program;
mod socket;
mod stylus_backend;
mod test;
mod wasip1_stub;
mod wavmio;

#[derive(StructOpt)]
#[structopt(name = "jit-prover")]
pub struct Opts {
#[structopt(short, long)]
binary: PathBuf,
#[structopt(long, default_value = "0")]
inbox_position: u64,
#[structopt(long, default_value = "0")]
delayed_inbox_position: u64,
#[structopt(long, default_value = "0")]
position_within_message: u64,
#[structopt(long)]
last_block_hash: Option<String>,
#[structopt(long)]
last_send_root: Option<String>,
#[structopt(long)]
inbox: Vec<PathBuf>,
#[structopt(long)]
delayed_inbox: Vec<PathBuf>,
#[structopt(long)]
preimages: Option<PathBuf>,
#[structopt(long)]
cranelift: bool,
#[structopt(long)]
forks: bool,
#[structopt(long)]
debug: bool,
#[structopt(long)]
require_success: bool,
// JSON inputs supercede any of the command-line inputs which could
// be specified in the JSON file.
#[structopt(long)]
json_inputs: Option<PathBuf>,
}

fn main() -> Result<()> {
let opts = Opts::from_args();
let env = match WasmEnv::cli(&opts) {
Expand Down
2 changes: 1 addition & 1 deletion arbitrator/jit/src/prepare.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2022-2024, Offchain Labs, Inc.
// For license information, see https://github.com/nitro/blob/master/LICENSE

use crate::WasmEnv;
use crate::machine::WasmEnv;
use arbutil::{Bytes32, PreimageType};
use eyre::Ok;
use prover::parse_input::FileData;
Expand Down
63 changes: 46 additions & 17 deletions arbitrator/jit/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
#![allow(clippy::too_many_arguments)]

use crate::caller_env::JitEnv;
use crate::machine::{Escape, MaybeEscape, WasmEnvMut};
use crate::stylus_backend::exec_wasm;
use crate::machine::{Escape, MaybeEscape, WasmEnv, WasmEnvMut};
use crate::stylus_backend::{exec_wasm, MessageFromCothread};
use arbutil::evm::api::Gas;
use arbutil::Bytes32;
use arbutil::{evm::EvmData, format::DebugBytes, heapify};
Expand All @@ -16,6 +16,7 @@ use prover::{
machine::Module,
programs::{config::PricingParams, prelude::*},
};
use std::sync::Arc;

const DEFAULT_STYLUS_ARBOS_VERSION: u64 = 31;

Expand Down Expand Up @@ -130,10 +131,6 @@ pub fn new_program(
let evm_data: EvmData = unsafe { *Box::from_raw(evm_data_handler as *mut EvmData) };
let config: JitConfig = unsafe { *Box::from_raw(stylus_config_handler as *mut JitConfig) };

// buy ink
let pricing = config.stylus.pricing;
let ink = pricing.gas_to_ink(Gas(gas));

let Some(module) = exec.module_asms.get(&compiled_hash).cloned() else {
return Err(Escape::Failure(format!(
"module hash {:?} not found in {:?}",
Expand All @@ -142,6 +139,21 @@ pub fn new_program(
)));
};

exec_program(exec, module, calldata, config, evm_data, gas)
}

pub fn exec_program(
exec: &mut WasmEnv,
module: Arc<[u8]>,
calldata: Vec<u8>,
config: JitConfig,
evm_data: EvmData,
gas: u64,
) -> Result<u32, Escape> {
// buy ink
let pricing = config.stylus.pricing;
let ink = pricing.gas_to_ink(Gas(gas));

let cothread = exec_wasm(
module,
calldata,
Expand All @@ -162,7 +174,10 @@ pub fn new_program(
/// returns request_id for the first request from the program
pub fn start_program(mut env: WasmEnvMut, module: u32) -> Result<u32, Escape> {
let (_, exec) = env.jit_env();
start_program_with_wasm_env(exec, module)
}

pub fn start_program_with_wasm_env(exec: &mut WasmEnv, module: u32) -> Result<u32, Escape> {
if exec.threads.len() as u32 != module || module == 0 {
return Escape::hostio(format!(
"got request for thread {module} but len is {}",
Expand All @@ -179,26 +194,27 @@ pub fn start_program(mut env: WasmEnvMut, module: u32) -> Result<u32, Escape> {
/// request_id MUST be last request id returned from start_program or send_response
pub fn get_request(mut env: WasmEnvMut, id: u32, len_ptr: GuestPtr) -> Result<u32, Escape> {
let (mut mem, exec) = env.jit_env();
let msg = get_last_msg(exec, id)?;
mem.write_u32(len_ptr, msg.req_data.len() as u32);
Ok(msg.req_type)
}

pub fn get_last_msg(exec: &mut WasmEnv, id: u32) -> Result<MessageFromCothread, Escape> {
let thread = exec.threads.last_mut().unwrap();
let msg = thread.last_message()?;
if msg.1 != id {
return Escape::hostio("get_request id doesn't match");
};
mem.write_u32(len_ptr, msg.0.req_data.len() as u32);
Ok(msg.0.req_type)
Ok(msg.0)
}

// gets data associated with last request.
// request_id MUST be last request receieved
// data_ptr MUST point to a buffer of at least the length returned by get_request
pub fn get_request_data(mut env: WasmEnvMut, id: u32, data_ptr: GuestPtr) -> MaybeEscape {
let (mut mem, exec) = env.jit_env();
let thread = exec.threads.last_mut().unwrap();
let msg = thread.last_message()?;
if msg.1 != id {
return Escape::hostio("get_request id doesn't match");
};
mem.write_slice(data_ptr, &msg.0.req_data);
let msg = get_last_msg(exec, id)?;
mem.write_slice(data_ptr, &msg.req_data);
Ok(())
}

Expand All @@ -217,11 +233,21 @@ pub fn set_response(
let result = mem.read_slice(result_ptr, result_len as usize);
let raw_data = mem.read_slice(raw_data_ptr, raw_data_len as usize);

set_response_with_wasm_env(exec, id, gas, result, raw_data)
}

pub fn set_response_with_wasm_env(
exec: &mut WasmEnv,
id: u32,
gas: u64,
result: Vec<u8>,
raw_data: Vec<u8>,
) -> MaybeEscape {
let thread = exec.threads.last_mut().unwrap();
thread.set_response(id, result, raw_data, Gas(gas))
}

/// sends previos response
/// sends previous response
/// MUST be called right after set_response to the same id
/// returns request_id for the next request
pub fn send_response(mut env: WasmEnvMut, req_id: u32) -> Result<u32, Escape> {
Expand All @@ -239,16 +265,19 @@ pub fn send_response(mut env: WasmEnvMut, req_id: u32) -> Result<u32, Escape> {
/// removes the last created program
pub fn pop(mut env: WasmEnvMut) -> MaybeEscape {
let (_, exec) = env.jit_env();
pop_with_wasm_env(exec)
}

pub fn pop_with_wasm_env(exec: &mut WasmEnv) -> MaybeEscape {
match exec.threads.pop() {
None => Err(Escape::Child(eyre!("no child"))),
Some(mut thread) => thread.wait_done(),
}
}

pub struct JitConfig {
stylus: StylusConfig,
compile: CompileConfig,
pub stylus: StylusConfig,
pub compile: CompileConfig,
}

/// Creates a `StylusConfig` from its component parts.
Expand Down
4 changes: 4 additions & 0 deletions arbitrator/jit/src/stylus_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#![allow(clippy::too_many_arguments)]

use crate::machine::{Escape, MaybeEscape};
use arbutil::benchmark::Benchmark;
use arbutil::evm::api::{Gas, Ink, VecReader};
use arbutil::evm::{
api::{EvmApiMethod, EVM_API_METHOD_REQ_OFFSET},
Expand Down Expand Up @@ -35,6 +36,7 @@ struct MessageToCothread {
pub struct MessageFromCothread {
pub req_type: u32,
pub req_data: Vec<u8>,
pub benchmark: Option<Benchmark>,
}

struct CothreadRequestor {
Expand All @@ -51,6 +53,7 @@ impl RequestHandler<VecReader> for CothreadRequestor {
let msg = MessageFromCothread {
req_type: req_type as u32 + EVM_API_METHOD_REQ_OFFSET,
req_data: req_data.as_ref().to_vec(),
benchmark: None,
};

if let Err(error) = self.tx.send(msg) {
Expand Down Expand Up @@ -169,6 +172,7 @@ pub fn exec_wasm(
let msg = MessageFromCothread {
req_data: output,
req_type: out_kind as u32,
benchmark: instance.env().benchmark,
};
instance
.env_mut()
Expand Down
4 changes: 4 additions & 0 deletions arbitrator/stylus/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// For license information, see https://github.com/nitro/blob/master/LICENSE

use arbutil::{
benchmark::Benchmark,
evm::{
api::{DataReader, EvmApi, Ink},
EvmData,
Expand Down Expand Up @@ -48,6 +49,8 @@ pub struct WasmEnv<D: DataReader, E: EvmApi<D>> {
pub compile: CompileConfig,
/// The runtime config
pub config: Option<StylusConfig>,
// Used to benchmark execution blocks of code
pub benchmark: Option<Benchmark>,
// Using the unused generic parameter D in a PhantomData field
_data_reader_marker: PhantomData<D>,
}
Expand All @@ -68,6 +71,7 @@ impl<D: DataReader, E: EvmApi<D>> WasmEnv<D, E> {
outs: vec![],
memory: None,
meter: None,
benchmark: None,
_data_reader_marker: PhantomData,
}
}
Expand Down
Loading
Loading