Skip to content

Commit

Permalink
optimism: computes correct time, ips, memory data
Browse files Browse the repository at this point in the history
Lacks symbol support
  • Loading branch information
rbonichon committed Oct 31, 2023
1 parent 05a6cd4 commit 7a680aa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
15 changes: 15 additions & 0 deletions optimism/src/cannon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,3 +135,18 @@ pub struct VmConfiguration {
pub pprof_cpu: bool,
pub host: Option<HostProgram>,
}

#[derive(Debug, Clone)]
pub struct Start {
pub time: std::time::Instant,
pub step: usize,
}

impl Start {
pub fn create(step: usize) -> Start {
Start {
time: std::time::Instant::now(),
step,
}
}
}
14 changes: 7 additions & 7 deletions optimism/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::{arg, value_parser, Arg, ArgAction, Command};
use kimchi_optimism::{
cannon::{State, VmConfiguration},
cannon::{self, Start, State, VmConfiguration},
mips::witness,
};
use std::{fs::File, io::BufReader, process::ExitCode};
Expand Down Expand Up @@ -113,9 +113,8 @@ fn cli() -> VmConfiguration {
pub fn main() -> ExitCode {
let configuration = cli();

println!("configuration\n{:#?}", configuration);

let file = File::open(&configuration.input_state_file).expect("Error opening input state file ");
let file =
File::open(&configuration.input_state_file).expect("Error opening input state file ");

let reader = BufReader::new(file);
// Read the JSON contents of the file as an instance of `State`.
Expand All @@ -130,12 +129,13 @@ pub fn main() -> ExitCode {
.expect("Could not spawn host process");
};

let page_size = 1 << 12;
// Initialize some data used for statistical computations
let start = Start::create(state.step as usize);

let mut env = witness::Env::<ark_bn254::Fq>::create(page_size, state);
let mut env = witness::Env::<ark_bn254::Fq>::create(cannon::PAGE_SIZE, state);

while !env.halt {
env.step(configuration.clone());
env.step(configuration.clone(), &start);
}

// TODO: Logic
Expand Down
64 changes: 51 additions & 13 deletions optimism/src/mips/witness.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{
cannon::{State, StepFrequency, VmConfiguration},
cannon::{Start, State, StepFrequency, VmConfiguration, PAGE_SIZE},
mips::registers::Registers,
};
use ark_ff::Field;
use log::info;
use std::array;
use log::{info};

pub const NUM_GLOBAL_LOOKUP_TERMS: usize = 1;
pub const NUM_DECODING_LOOKUP_TERMS: usize = 2;
Expand Down Expand Up @@ -96,35 +96,73 @@ impl<Fp: Field> Env<Fp> {
}
}

pub fn step(&mut self, config: VmConfiguration) {
self.pp_info(config.info_at);
pub fn step(&mut self, config: VmConfiguration, start: &Start) {
self.pp_info(config.info_at, start);

// Force stops at given iteration
if self.at(config.stop_at) {
self.halt = true;
return;
}

// TODO
self.halt = true;
}

fn at(& self, at: StepFrequency) -> bool {
let m:u64 = self.instruction_counter as u64;
fn at(&self, at: StepFrequency) -> bool {
let m: u64 = self.instruction_counter as u64;
match at {
StepFrequency::Never => false,
StepFrequency::Always => true,
StepFrequency::Exactly(n) => n == m ,
StepFrequency::Exactly(n) => n == m,
StepFrequency::Every(n) => m % n == 0,
}
}

fn pp_info(& self, at: StepFrequency) {
const UNIT: usize = 1024; // a "unit" of memory is 1024 bytes
const PREFIXES: &str = "KMGTPE"; // prefixes for memory quantities KiB, MiB, GiB, ...

fn memory_usage(&self) -> String {
let total = self.memory.len() * PAGE_SIZE;

if total < Self::UNIT {
format!("{total} B")
} else {
// Compute the index in the prefixes string above
let mut idx = 0;
let mut d = Self::UNIT;
let mut n = total / Self::UNIT;

while n >= Self::UNIT {
d = d * Self::UNIT;
idx += 1;
n = n / Self::UNIT;
}

let value = total as f64 / d as f64;
let prefix = Self::PREFIXES.chars().nth(idx).unwrap();

format!("{:.1} {}iB", value, prefix)
}
}

fn pp_info(&self, at: StepFrequency, start: &Start) {
if self.at(at) {
println!("Info");
let elapsed = 1.0;
let elapsed = start.time.elapsed();
let step = self.instruction_counter;
let pc = self.instruction_pointer;
let insn = 0xffffff;
let ips = 0.0 / elapsed;
let how_many_steps = step - start.step;
// Approximate instruction per seconds
let ips = how_many_steps as f64 / elapsed.as_secs() as f64;
let pages = self.memory.len();
let mem = 0;
let name = "unsupported";
info!("processing step {} pc {} insn {} ips {} page {} mem {} name {}", step, pc, insn, ips, pages, mem, name);
let mem = self.memory_usage();
let name = "unsupported"; // TODO: implement symbol lookups
info!(
"processing step {} pc {:#10x} insn {:#10x} ips {:.2} page {} mem {} name {}",
step, pc, insn, ips, pages, mem, name
);
}
}
}

0 comments on commit 7a680aa

Please sign in to comment.