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

feat: cycle tracker test program #1276

2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
// // Examples.
// "examples/chess/program/Cargo.toml",
// "examples/chess/script/Cargo.toml",
// "examples/cycle-tracking/program/Cargo.toml",
// "examples/cycle-tracking/script/Cargo.toml",
// "examples/fibonacci/program/Cargo.toml",
// "examples/fibonacci/script/Cargo.toml",
// "examples/io/program/Cargo.toml",
Expand Down
2 changes: 1 addition & 1 deletion book/writing-programs/cycle-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ When writing a program, it is useful to know how many RISC-V cycles a portion of
To track the number of cycles spent in a portion of the program, you can either put `println!("cycle-tracker-start: block name")` + `println!("cycle-tracker-end: block name")` statements (block name must be same between start and end) around the portion of your program you want to profile or use the `#[sp1_derive::cycle_tracker]` macro on a function. An example is shown below:

```rust,noplayground
{{#include ../../examples/cycle-tracking/program/src/main.rs}}
{{#include ../../examples/cycle-tracking/program/bin/normal.rs}}
```

Note that to use the macro, you must add the `sp1-derive` crate to your dependencies for your program.
Expand Down
15 changes: 11 additions & 4 deletions build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,25 @@ fn copy_elf_to_output_dir(

// The ELF is written to a target folder specified by the program's package. If built with Docker,
// includes /docker after HELPER_TARGET_SUBDIR.
let target_dir_suffix = if args.docker {
format!("{}/{}", HELPER_TARGET_SUBDIR, "docker")
let mut target_dir_suffix = HELPER_TARGET_SUBDIR.to_string();
if args.docker {
target_dir_suffix = format!("{}/{}", HELPER_TARGET_SUBDIR, "docker");
}

// The ELF's file name is the binary name if it's specified. Otherwise, it is the root package
// name.
let original_elf_file_name = if !args.binary.is_empty() {
args.binary.clone()
} else {
HELPER_TARGET_SUBDIR.to_string()
root_package_name.unwrap().clone()
};

let original_elf_path = program_metadata
.target_directory
.join(target_dir_suffix)
.join(BUILD_TARGET)
.join("release")
.join(root_package_name.unwrap());
.join(original_elf_file_name);

// The order of precedence for the ELF name is:
// 1. --elf_name flag
Expand Down
32 changes: 32 additions & 0 deletions examples/cycle-tracking/program/bin/normal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

#[sp1_derive::cycle_tracker]
pub fn expensive_function(x: usize) -> usize {
let mut y = 1;
for _ in 0..100 {
y *= x;
y %= 7919;
}
y
}

pub fn main() {
let mut nums = vec![1, 1];

// Setup a large vector with Fibonacci-esque numbers.
println!("cycle-tracker-start: setup");
for _ in 0..100 {
let mut c = nums[nums.len() - 1] + nums[nums.len() - 2];
c %= 7919;
nums.push(c);
}
println!("cycle-tracker-end: setup");

println!("cycle-tracker-start: main-body");
for i in 0..2 {
let result = expensive_function(nums[nums.len() - i - 1]);
println!("result: {}", result);
}
println!("cycle-tracker-end: main-body");
}
25 changes: 25 additions & 0 deletions examples/cycle-tracking/program/bin/report.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#![no_main]
sp1_zkvm::entrypoint!(main);

#[sp1_derive::cycle_tracker]
pub fn expensive_function(x: usize) -> usize {
let mut y = 1;
for _ in 0..100 {
y *= x;
y %= 7919;
}
y
}

pub fn main() {
let mut nums = vec![1, 1];

// Setup a large vector with Fibonacci-esque numbers.
println!("cycle-tracker-report-start: setup");
for _ in 0..100 {
let mut c = nums[nums.len() - 1] + nums[nums.len() - 2];
c %= 7919;
nums.push(c);
}
println!("cycle-tracker-report-end: setup");
}
Binary file added examples/cycle-tracking/program/elf/normal
Binary file not shown.
Binary file added examples/cycle-tracking/program/elf/report
Binary file not shown.
12 changes: 4 additions & 8 deletions helper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,10 @@ fn execute_build_cmd(
}

// Build the program with the given arguments.
let path_output = if let Some(args) = args {
sp1_build::build_program(&args, Some(program_dir.as_ref().to_path_buf()))
} else {
sp1_build::build_program(
&BuildArgs::default(),
Some(program_dir.as_ref().to_path_buf()),
)
};
let path_output = sp1_build::build_program(
&args.unwrap_or_default(),
Some(program_dir.as_ref().to_path_buf()),
);
if let Err(err) = path_output {
panic!("Failed to build SP1 program: {}.", err);
}
Expand Down
Loading