trs: block-buffer stdout when not a tty (out.rs sink) - #146
Open
nanavati wants to merge 1 commit into
Open
Conversation
The reference C engines inherit stdio's contract: block-buffered when piped, line-buffered on a terminal. Rust's std stdout is always a LineWriter — one write(2) per $display line on piped batch runs (TrafficBRAM ~3.6k, SparseRF ~5.6k). All runtime stdout writers now route through a process-level sink (trs-interp/src/out.rs): std stdout on a tty or under TRS_STDOUT_LINE, a 64KiB BufWriter over raw fd 1 otherwise. Flush points: $fflush; BDPI phase 0 (user C writes through libc's own buffers); run teardown (the batch path exits via _exit, which skips atexit, so its flush is explicit; RunCore teardown mirrors it); and an atexit net for every exit(3) path. Panics/signals lose buffered output, exactly like C block buffering. Two tiers share fd 1 with a foreign writer and pin line mode instead: bk_init (libtrs_capi.so is dlopened into bluetcl, whose stdio interleaves with sim output) and run_script (command responses print through std stdout between advances). Witnesses: FloatTest artifact 345 stdout writes -> 1 in block mode, output byte-identical across line/block/RunCore; battery 22/22 classic and TRS_RUNCORE=1; dual corpus seals 1003 PASS / 0 DIFF (witnessed + driver-armed), 998 aot + 5 interp. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CerPH99xuDTaGhaBQ3wwqS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Batch stdout is now block-buffered when fd 1 is not a terminal, matching the reference C engines' stdio contract (Bluesim/Verilator inherit block buffering from libc when piped; Rust's std stdout is unconditionally a LineWriter, costing one
write(2)per$displayline on piped runs — TrafficBRAM pays ~3,600 of them, SparseRF ~5,600).New
trs-interp/src/out.rs: a process-level stdout sink — std stdout (line-buffered) on a tty, a 64KiBBufWriterover raw fd 1 otherwise. Every stdout writer in the runtime routes through it:write_display, the$fwritestdout slot,qprintln!(mem-file loader messages, prim warnings), the BRAM write-collision warning, andOutput error:reporting.Flush points (the ordering contract)
$fflush— explicit user flush (foreign.rs)bdpi.rs,jit_stdio_cb)libc::_exit(which skips atexit), so the pre-_exitflush is explicit; RunCore's teardown mirrors itatexit— registered on block-mode init, covering everyexit(3)path (process::exit(86)selfcheck bails included)Panics and signals lose buffered output, exactly like C block buffering.
The two-buffers-one-fd hazard
Two tiers share fd 1 with a foreign writer and must NOT block-buffer independently:
libtrs_capi.sois dlopened into bluetcl, whose own stdio interleaves with sim output —bk_initpins line mode before any write.trs run -c/-f) prints command responses through std stdout between sim advances —run_scriptpins line mode.TRS_STDOUT_LINE(any value) forces line mode as a user escape hatch.Witnesses
TRS_RUNCORE=1.TRS_RUNCORE_CHECK=1 TRS_SELFCHECK=1, and driver-armedTRS_RUNCORE=1), engines 998 aot + 5 interp — byte parity holds with buffering in play, loader diagnostics and warning text included.Stamp (min-of-N interleaved, same host, trs legs byte-verified vs the Bluesim oracle)
Verilator scoreboard 5W/3L/1T (was 5W/4L); Bluesim 11/11. The output-volume prediction registered before the stamp hit on all seven ranges; TrafficBRAM's remaining 0.06ms is the guarded-FIFO bounce rung's target.