diff --git a/book/writing-programs/cycle-tracking.md b/book/writing-programs/cycle-tracking.md index 171a36d3ff..489594a426 100644 --- a/book/writing-programs/cycle-tracking.md +++ b/book/writing-programs/cycle-tracking.md @@ -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. diff --git a/core/src/syscall/write.rs b/core/src/syscall/write.rs index a99a736012..7195aeb26a 100644 --- a/core/src/syscall/write.rs +++ b/core/src/syscall/write.rs @@ -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 { - 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. @@ -153,6 +149,7 @@ fn end_cycle_tracker(rt: &mut Runtime, name: &str) -> Option { 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 { let rt = &mut ctx.rt; let entry = rt.io_buf.entry(fd).or_default();