Skip to content

fix(tooling): write the frame trace from a writer thread, not the thread that draws - #2297

Merged
Hona merged 2 commits into
devfrom
hitch/trace-writer-thread
Nov 25, 2026
Merged

Hona merged 2 commits into
devfrom
hitch/trace-writer-thread

Conversation

@Hona

@Hona Hona commented Nov 25, 2026

Copy link
Copy Markdown
Member

A --trace run wrote its trace file from the thread that draws. When that write waited on the disk, the trace showed a frame gap the app did not have.

sequenceDiagram
    participant UI as frame thread
    participant W as rune-trace thread
    participant F as trace file
    Note over UI,F: before
    UI->>F: writeln! through an 8 KiB BufWriter (a write call every frame or two)
    F-->>UI: returns after the file's lock is free (60 to 220 ms seen)
    Note over UI,F: after
    UI->>W: finished line over a channel (never waits)
    W->>F: write_all, as each line arrives
    UI->>W: flush at the run's end
    W-->>UI: every line is in the file
Loading

What was measured

Recorded launches of an app with --trace on Windows (off-CPU waits of the frame thread, aligned to the trace's frames):

Frame gap in the trace Frame thread during the gap
177.2 ms 165.2 ms in NtWriteFile from Trace::frame_timed_core
233.3 ms 222.4 ms in NtWriteFile (the file's NTFS lock while it grows)
115.0 ms 103.0 ms in NtWriteFile (same)
70.4 ms 60.3 ms in NtWriteFile from Trace::frame_timed_core

The frames around each gap cost 4 to 7 ms. A frame line is about 5 KiB, so the default 8 KiB buffer made a write call every frame or two.

The change

  • Trace::out is a TraceOut: write collects a line and sends it when it ends in a newline; a thread named rune-trace owns the file and writes each line as it arrives.
  • flush waits until the file holds every line sent so far. Trace::settle_presents now ends with it: a --frames N run ends by a fast exit that drops nothing, so this is where its last frames reach the file. Before, such a run lost the buffer's tail (traces of 1,000 frames held 988 to 999).
  • Drop sends what is left, closes the channel and joins the thread.
  • wasm32 keeps the BufWriter: no threads, no disk to wait on.
impl Write for TraceOut {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        self.line.extend_from_slice(buf);
        if self.line.last() == Some(&b'\n') { self.send_line(); }
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.send_line();
        let (written, done) = std::sync::mpsc::sync_channel(1);
        if let Some(lines) = &self.lines && lines.send(TraceMsg::Flush(written)).is_ok() { done.recv().ok(); }
        Ok(())
    }
}

What a process that dies leaves behind

  • Before: everything but the buffer's tail, up to 8 KiB (the last 1 to 11 frames).
  • After: everything the thread has written. The thread writes each line with one write_all as it arrives and keeps no buffer of its own, so the loss is the lines still in the channel: none or one in a healthy run, and as many as the disk was stalled for when it stalls.
  • The channel is unbounded: a stalled disk queues lines in memory (about 5 MiB for 1,000 frames) instead of holding frames.

Cost when --trace is off

None: the thread and the channel are made in Trace::open_clock, and a Trace is only made for a trace path (open_trace_if_needed, the headless and virtual-desktop runs).

Tests

  • trace_lines_are_written_by_another_thread_and_a_stalled_disk_holds_no_frame: a sink that holds every write behind a gate; three lines are handed over while it is shut; each line is one write, on the writer's thread.
  • trace_file_holds_every_line_after_a_flush_and_after_the_drop
  • settled_trace_file_holds_every_frame_while_the_trace_is_still_open

Not in this change

  • Host::replay_window_script still writes the script's lines to stdout from the frame thread (104.6 ms seen when stdout was a file). A runner should give the app a pipe it drains.

@Hona
Hona marked this pull request as ready for review November 25, 2026 13:49
@Hona
Hona merged commit 15ce8af into dev Nov 25, 2026
3 checks passed
@Hona
Hona deleted the hitch/trace-writer-thread branch November 25, 2026 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant