Skip to content

Commit

Permalink
feat: cycle tracker test program (#1276)
Browse files Browse the repository at this point in the history
  • Loading branch information
ratankaliani authored Aug 7, 2024
1 parent 66a9174 commit 5005138
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
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
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.

0 comments on commit 5005138

Please sign in to comment.