Skip to content

Commit

Permalink
pr fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ctian1 committed Mar 6, 2024
1 parent 27f54ef commit ce105e0
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 20 deletions.
4 changes: 1 addition & 3 deletions book/writing-programs/cycle-tracking.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ When writing a program, it is useful to know how many RISC-V cycles a portion of

## Tracking Cycles

To track the number of cycles spent in a portion of the program, you can either use a `println!("cycle-tracker-{start,end}:")` statement that wraps 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:

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}}
Expand Down Expand Up @@ -39,4 +38,3 @@ $ RUST_LOG=info cargo run --release
```

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

20 changes: 17 additions & 3 deletions core/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ pub struct Runtime {
/// A buffer for writing trace events to a file.
pub trace_buf: Option<BufWriter<File>>,

/// Whether the runtime should panic on halt or not.
pub panic_on_halt: bool,
/// Whether the runtime should fail on panic or not.
pub fail_on_panic: bool,

/// Whether the runtime is in constrained mode or not.
/// In unconstrained mode, any events, clock, register, or memory changes are reset after leaving
Expand Down Expand Up @@ -111,7 +111,7 @@ impl Runtime {
cycle_tracker: HashMap::new(),
io_buf: HashMap::new(),
trace_buf,
panic_on_halt: true,
fail_on_panic: true,
unconstrained: false,
unconstrained_state: ForkState::default(),
syscall_map: default_syscall_map(),
Expand Down Expand Up @@ -830,6 +830,20 @@ impl Runtime {
if let Some(ref mut buf) = self.trace_buf {
buf.flush().unwrap();
}
// Flush remaining stdout/stderr
for (fd, buf) in self.io_buf.iter() {
if !buf.is_empty() {
match fd {
1 => {
println!("[stdout] {}", buf);
}
2 => {
println!("[stderr] {}", buf);
}
_ => {}
}
}
}

// Call postprocess to set up all variables needed for global accounts, like memory
// argument or any other deferred tables.
Expand Down
2 changes: 1 addition & 1 deletion core/src/syscall/halt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl SyscallHalt {
impl Syscall for SyscallHalt {
fn execute(&self, ctx: &mut SyscallContext) -> u32 {
let exit_code = ctx.register_unsafe(Register::X10);
if ctx.rt.panic_on_halt && exit_code != 0 {
if ctx.rt.fail_on_panic && exit_code != 0 {
panic!(
"RISC-V runtime halted during program execution with non-zero exit code {}. This likely means your program panicked during execution.",
exit_code
Expand Down
35 changes: 22 additions & 13 deletions core/src/syscall/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,19 @@ impl Syscall for SyscallWrite {
);
} else {
let flush_s = update_io_buf(ctx, fd, s);
if let Some(s) = flush_s {
s.split('\n').for_each(|line| println!("stdout: {}", line));
if !flush_s.is_empty() {
flush_s
.into_iter()
.for_each(|line| println!("[stdout] {}", line));
}
}
} else if fd == 2 {
let s = core::str::from_utf8(slice).unwrap();
let flush_s = update_io_buf(ctx, fd, s);
if let Some(s) = flush_s {
s.split('\n').for_each(|line| println!("stderr: {}", line));
if !flush_s.is_empty() {
flush_s
.into_iter()
.for_each(|line| println!("[stderr] {}", line));
}
} else if fd == 3 {
rt.state.output_stream.extend_from_slice(slice);
Expand All @@ -79,16 +83,21 @@ impl Syscall for SyscallWrite {
}
}

pub fn update_io_buf(ctx: &mut SyscallContext, fd: u32, s: &str) -> Option<String> {
pub fn update_io_buf(ctx: &mut SyscallContext, fd: u32, s: &str) -> Vec<String> {
let rt = &mut ctx.rt;
if s.ends_with('\n') {
if let Some(existing) = rt.io_buf.remove(&fd) {
Some(format!("{}{}", existing, s.trim_end()))
} else {
Some(s.trim_end().to_string())
}
let entry = rt.io_buf.entry(fd).or_default();
entry.push_str(s);
if entry.contains('\n') {
// Return lines except for the last from buf.
let prev_buf = std::mem::take(entry);
let mut lines = prev_buf.split('\n').collect::<Vec<&str>>();
let last = lines.pop().unwrap_or("");
*entry = last.to_string();
lines
.into_iter()
.map(|line| line.to_string())
.collect::<Vec<String>>()
} else {
ctx.rt.io_buf.entry(fd).or_default().push_str(s);
None
vec![]
}
}
Binary file modified examples/ed25519/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/fibonacci-io/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/io/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/json/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
Binary file modified examples/rsa/program/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.

0 comments on commit ce105e0

Please sign in to comment.