Skip to content

Commit

Permalink
Add user cycles and segment count
Browse files Browse the repository at this point in the history
  • Loading branch information
eureka-cpu committed Dec 5, 2024
1 parent b8bf7bb commit 2d0c69f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
22 changes: 15 additions & 7 deletions cli/src/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,25 @@
use anyhow::Result;
use risc0_binfmt::{MemoryImage, Program};
use risc0_zkvm::{ExecutorEnv, ExecutorImpl, GUEST_MAX_MEM};
use risc0_zkvm::{ExecutorEnv, ExecutorImpl, Session, GUEST_MAX_MEM};
use risc0_zkvm_platform::PAGE_SIZE;

pub fn estimate<E: MkImage>(elf: E, env: ExecutorEnv) -> Result<()> {
let cycles = get_cycle_count(elf, env)?;
println!("total: {cycles}");
let session = get_session(elf, env)?;
println!(
"User cycles: {}\nTotal cycles: {}\nSegments: {}",
session.user_cycles,
session.total_cycles,
session.segments.len()
);

Ok(())
}

/// Get the total number of cycles by stepping through the ELF using emulation
/// tools from the risc0_circuit_rv32im module.
pub fn get_cycle_count<E: MkImage>(elf: E, env: ExecutorEnv) -> Result<u64> {
Ok(ExecutorImpl::new(env, elf.mk_image()?)?.run()?.total_cycles)
pub fn get_session<E: MkImage>(elf: E, env: ExecutorEnv) -> Result<Session> {
Ok(ExecutorImpl::new(env, elf.mk_image()?)?.run()?)
}

/// Helper trait for loading an image from an elf.
Expand Down Expand Up @@ -59,8 +64,11 @@ mod estimate_tests {
.session_limit(DEFAULT_SESSION_LIMIT);
let image = MemoryImage::new(&program, PAGE_SIZE as u32)
.expect("failed to create image from basic program");
let res = estimate::get_cycle_count(image, env.build().unwrap());
let res = estimate::get_session(image, env.build().unwrap());

assert_eq!(res.ok(), Some(16384));
assert_eq!(
res.ok().and_then(|session| Some(session.total_cycles)),
Some(16384)
);
}
}
8 changes: 4 additions & 4 deletions cli/src/tests/estimate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ fn estimate_simple() {
"--input-file",
input_file.to_str().unwrap(),
]);
bonsol_estimate
.assert()
.success()
.stdout(predicates::str::is_match(r##"total cycles: 65536"##).unwrap());
bonsol_estimate.assert().success().stdout(
predicates::str::is_match(r##"User cycles: 3380\nTotal cycles: 65536\nSegments: 1"##)
.unwrap(),
);
}

0 comments on commit 2d0c69f

Please sign in to comment.