Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/ratan/add-cycle-tracker-report' …
Browse files Browse the repository at this point in the history
…into ratan/cycle-tracking-program-execution
  • Loading branch information
ratankaliani committed Aug 7, 2024
2 parents 08c605c + a4b1580 commit c937e4c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
13 changes: 13 additions & 0 deletions book/writing-programs/cycle-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@ stdout: result: 2940

Note that we elegantly handle nested cycle tracking, as you can see above.

### Get Tracked Cycle Counts
To include tracked cycle counts in the `ExecutionReport` when using `ProverClient::execute`, use the following annotations:

```rust,noplayground
fn main() {
println!("cycle-tracker-report-start: block name");
// ...
println!("cycle-tracker-report-end: block name");
}
```

This will log the cycle count for `main_function` and include it in the `ExecutionReport` in the `cycle_tracker` map.

## Tracking Cycles with Tracing

The `cycle-tracker` annotation is a convenient way to track cycles for specific sections of code. However, sometimes it can also be useful to track what functions are taking the most cycles across the entire program, without having to annotate every function individually.
Expand Down
25 changes: 11 additions & 14 deletions core/src/syscall/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,17 @@ enum CycleTrackerCommand {

/// Parse a cycle tracker command from a string. If the string does not match any known command, returns None.
fn parse_cycle_tracker_command(s: &str) -> Option<CycleTrackerCommand> {
match s.split_once(':') {
Some(("cycle-tracker-start", name)) => {
Some(CycleTrackerCommand::Start(name.trim().to_string()))
}
Some(("cycle-tracker-end", name)) => {
Some(CycleTrackerCommand::End(name.trim().to_string()))
}
Some(("cycle-tracker-report-start", name)) => {
Some(CycleTrackerCommand::ReportStart(name.trim().to_string()))
}
Some(("cycle-tracker-report-end", name)) => {
Some(CycleTrackerCommand::ReportEnd(name.trim().to_string()))
}
_ => None,
if let Some((command, fn_name)) = s.split_once(':') {
let trimmed_name = fn_name.trim().to_string();
match command {
"cycle-tracker-start" => Some(CycleTrackerCommand::Start(trimmed_name)),
"cycle-tracker-end" => Some(CycleTrackerCommand::End(trimmed_name)),
"cycle-tracker-report-start" => Some(CycleTrackerCommand::ReportStart(trimmed_name)),
"cycle-tracker-report-end" => Some(CycleTrackerCommand::ReportEnd(trimmed_name)),
_ => None,
};
}
None
}

/// Handle a cycle tracker command.
Expand Down Expand Up @@ -153,6 +149,7 @@ fn end_cycle_tracker(rt: &mut Runtime, name: &str) -> Option<u64> {
None
}

/// Update the io buffer for the given file descriptor with the given string.
fn update_io_buf(ctx: &mut SyscallContext, fd: u32, s: &str) -> Vec<String> {
let rt = &mut ctx.rt;
let entry = rt.io_buf.entry(fd).or_default();
Expand Down

0 comments on commit c937e4c

Please sign in to comment.