summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 17:01:35 -0300
committerGabriel Schneider <[email protected]>2026-10-29 19:12:05 -0300
commit97bb0e816513b805ca13358d8a14ba117f586403 (patch)
tree209405ca605f6b49d1a15adc486ff9dfc696a1fb
parentefcb4c35e76ef33bbd62123a5c5574f88bc8bcc7 (diff)
downloadpardes-97bb0e816513b805ca13358d8a14ba117f586403.tar.gz
pardes-97bb0e816513b805ca13358d8a14ba117f586403.zip
tty: a flooded pane's output is drawn a frame a tick at most, two of its reads queued at most, so it costs no frame a 4 KB read and holds no key or echo behind it
Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--docs/next-daemon.md93
-rw-r--r--src/tty/tty.zig57
2 files changed, 148 insertions, 2 deletions
diff --git a/docs/next-daemon.md b/docs/next-daemon.md
index 903a80ad..a39484b8 100644
--- a/docs/next-daemon.md
+++ b/docs/next-daemon.md
@@ -555,3 +555,96 @@ core time p50 0.96-0.97, keystroke-to-present p50 0.98-1.03 (A/A 1-23%),
p50 1.00-1.04 (A/A 2-18%), present-bound at the window's 30 ms. Two
costs the A/B found are gone (the geometry key, the paint-ahead's
thread start in the render).
+
+## Tty throughput notes
+
+Found and fixed 2026-10-29 (`src/tty/tty.zig`, `waitInput`).
+
+- **What the 1 GB `yes` cell measures is mostly the kernel.** A pty's
+ output processing (ONLCR, a `\n` made `\r\n`) costs the writer about
+ 0.4 us a newline here, and `yes` writes one every two bytes. Every reader
+ of a pty is capped by it: `script(1)` drains 100 MB of `yes` at 8.6 MB/s
+ with OPOST on and at 650 MB/s with it off, a Python drain at 8.0 MB/s,
+ and tmux at 5.4 MB/s. The tty pardes was at 5.7 to 6.1 MB/s, three
+ quarters of that ceiling, before any change. The 179 s were the kernel's.
+- **Where pardes itself was slow:** with 200-byte lines (`yes` of a long
+ word), the ceiling rises to 264 MB/s, tmux reaches 55 MB/s, and pardes
+ managed 18.6 MB/s. A flat profile (perf at paranoid 2 allows no call
+ graphs) put 98.7% of the samples in pardes: `Vaxis.render` 29%,
+ `body_layer.renderBody` 17%, `Shell.present` 10%, memcpy 8%,
+ ghostty's parse 1.5%. The tty drew a frame for every pty read, which
+ is 4 KB, nearly all of them overwritten before anyone could see them
+ (about 690k frames for the 1 GB cell).
+- **The fix: output is drawn once per frame tick at most** (16 ms), with
+ whatever came in until then, as the GUI drains its queue once per vsync.
+ Two things are drawn at once, as before:
+ - an input's frame;
+ - the focused pane's output within a second of an input (its echo; a
+ remote shell's arrives an RTT later).
+ Neither waits while it is undrawn, including when FrameAck held its
+ frame for the terminal's answer. Snapshot runs (the virtual clock) are
+ not paced.
+- **And a pane has two reads queued at most** (`readPty`): its reader
+ waits for the loop to take one before it reads on. ghostty parses `yes`
+ at about 9 MB/s, 0.45 ms a 4 KB read, slower than this machine's pty
+ delivers it. So a flood filled the loop's queue (512 events): a key and
+ its echo waited behind its reads, and which blocked writer a free slot
+ woke was luck, which gave half-second peaks. Paced but unbounded, the
+ wait was steady at about 20 reads, 9 ms for the key and 9 more for its
+ echo. A frame dump lined up with each key's write showed it (PARDES_FRAMES
+ and `flood.py` with FRAMES=). What a flood writes beyond two reads now
+ waits in its pty; the loop could not have kept up with it anyway.
+- **Measured** (ReleaseFast, `.scratch/daemon/term/yes.py` through a real
+ 120x40 pty, all interleaved, with measurements pinned to two physical
+ cores and so four CPUs; medians of 3 rounds, MB/s):
+
+ | | `yes`, 32 MiB | 200-byte lines, 256 MiB |
+ |---|---|---|
+ | base (before the rework) | 5.5 | 14.5 |
+ | phase 1's tip | 4.8 | 15.0 |
+ | pacing alone (the queue limit came after) | 4.6 | 117 |
+ | tmux | 5.4 | 47.6 |
+ | raw pty drain (Python) | 7.7 | 239 |
+ | GUI, test mode | 0.9 | |
+
+ Unpinned, before the pinning: 200-byte lines 18.6 to 247 MB/s (1 GB in
+ 53.7 s and in 4.0 s), and `yes` 5.7 to 6.9 MB/s. The GUI could only be
+ measured in its test mode (a hidden window), which downloads every frame
+ from the GPU, so 0.9 MB/s says nothing of a real window. There is no
+ headless compositor here, and a real window must not open on the user's
+ session. In a later interleaved run, the change as committed against
+ pacing alone gave 4.8 and 6.0 against 4.8 and 5.4 MB/s for `yes`, and
+ 102 and 117 against 124 and 119 MB/s for long lines: the same within
+ the noise. `pardes-perf --live`'s `yes` cell drew 2.3k frames for 32 MiB
+ against 11k before, in the same time: it is kernel-bound.
+- **A held key while another pane floods** (`flood.py`: a key every 30
+ ms into `cat` in one pane of `-n 3`, timed from its write to the pty to
+ its echo on screen, with pyte in the loop; `yes` in another pane):
+ in one interleaved run of two rounds (p50, p90, worst, in ms):
+
+ | | under the flood | idle |
+ |---|---|---|
+ | phase 1's tip | 3.0, 212 to 214, 445 to 470 | 2.2 to 2.3 |
+ | pacing alone | 10.3 to 10.6, 19 to 20, 26 to 33 | 2.3 |
+ | this change | 3.3 to 4.2, 4.3 to 5.6, 6.0 to 7.8 | 2.3 to 3.2 |
+
+ The driver's own cost (pyte) is in every number. With 16 KiB queued a
+ pane instead of two reads, the flood's p50 was 7.5 to 7.8 ms for no
+ throughput gained, so two reads it is.
+- **Keystroke-to-present** (`pardes-perf-live --live`, base and this
+ change interleaved, 5 and 4 runs; A/A between the base runs' halves):
+ p50 against base is 0.92 for key-down, 0.89 key-right, 0.85 edit-char,
+ 0.80 page-down and 0.93 wheel. The A/A was 0.73 to 0.92. The 32 MiB
+ `yes` took 1.00 the time with 0.18 the frames, and the big open 1.10
+ (A/A 1.05). Two earlier runs of pacing alone, 3 and 5 rounds, were
+ within their A/A too.
+- **Unrelated, found on the way:** `test/fs.py` failed
+ `unattended_messages_expire` (its first `shown()`) on phase 1's tip as
+ well; pmxpuolt on main (a message's stamp and expiry read the clock
+ again) is that fix. fs.py also needs a short TMPDIR: under
+ `.scratch/daemon/term/tmp`, `directory_panes`'s socket path is 108 bytes,
+ one past the limit, and the session starts with no 9P. Two GUI unit tests
+ fail on phase 1's tip as well (`tagline pointer mapping ...`, `a tag caret
+ past its row's end ...`). A suite started as a shell's `&` job runs with
+ SIGINT ignored, so four host_io tests whose Ctrl-C must stop a `sleep`
+ fail there; started otherwise, they pass.
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index d474e83a..760c0692 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -1407,7 +1407,10 @@ fn localSession(
sh.inotify_fd = -1;
}
while (loop.tryEvent() catch null) |ev| switch (ev) {
- .pty_read => |pr| gpa.free(pr.bytes),
+ .pty_read => |pr| {
+ tookRead(io, pr.id);
+ gpa.free(pr.bytes);
+ },
.paste => |b| gpa.free(@constCast(b)),
.lsp_done => |d| if (d.rows) |rows| allocs.lsp.free(rows),
.lsp_status => |text| allocs.lsp.free(text),
@@ -1591,6 +1594,13 @@ const Shell = struct {
pace_ns: u64 = 0,
input_seen: bool = false,
wake_to: u64 = 0,
+ /// waitInput's pacing of output: whether this wait read a pane's output,
+ /// and when the last frame was written, the last input was read and the
+ /// focused pane's output last was.
+ output_seen: bool = false,
+ drawn_ns: u64 = 0,
+ input_ns: u64 = 0,
+ focused_ns: u64 = 0,
fn of(ctx: ?*anyopaque) *Shell {
return @ptrCast(@alignCast(ctx.?));
@@ -1778,6 +1788,7 @@ const Shell = struct {
wait = @intCast(@max(1, (tick -| now) / std.time.ns_per_ms));
}
s.input_seen = false;
+ s.output_seen = false;
// Paced, what is not an input (a frame's ack, a pty's output) is
// taken in and the wait goes on to the tick: the acks alone ran the
// loop at the terminal's round trip.
@@ -1789,6 +1800,21 @@ const Shell = struct {
wait = @intCast((tick - now) / std.time.ns_per_ms);
}
if (paced and s.input_seen) s.pace_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
+ // Output alone is drawn a frame a tick at most, what came in till
+ // then in one: a flood (`yes`, 4 KB a read) was drawn a read at a
+ // time, nearly all of its frames written over before they were
+ // seen. Never while what someone waits on is undrawn: an input
+ // (its frame held for the terminal's answer, FrameAck), or the
+ // focused pane's output soon after one, its echo (a remote shell's
+ // an RTT later).
+ while (!paced and s.output_seen and !s.input_seen and !s.core.quit and s.test_clock == null) {
+ if (s.input_ns > s.drawn_ns) break;
+ if (s.focused_ns > s.drawn_ns and s.focused_ns -| s.input_ns < std.time.ns_per_s) break;
+ const due = s.drawn_ns + pardes.animation.frame_ns;
+ const now: u64 = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
+ if (now + std.time.ns_per_ms > due) break;
+ s.waitOnce(@intCast((due - now) / std.time.ns_per_ms));
+ }
}
fn waitOnce(s: *Shell, wait: u32) void {
@@ -1853,6 +1879,7 @@ const Shell = struct {
.paste, .paste_start, .winsize => s.input_seen = true,
else => {},
}
+ if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
switch (event) {
.nop => {},
.wake => if (s.test_clock) |*virtual| {
@@ -1886,6 +1913,9 @@ const Shell = struct {
core.update(.{ .resize = .{ .cols = ws.cols, .rows = ws.rows } });
},
.pty_read => |pr| {
+ tookRead(s.io, pr.id);
+ s.output_seen = true;
+ if (pr.id == core.active) s.focused_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
if (s.gens[pr.id] == pr.gen)
core.update(.{ .output = .{ .pane = @intCast(pr.id), .bytes = pr.bytes } });
s.gpa.free(pr.bytes);
@@ -2139,6 +2169,7 @@ const Shell = struct {
// vaxis's diff of the cells wrote.
s.core.frames.bytes +|= @intCast(@min(std.math.maxInt(u32), s.tty.tty_writer.pos + s.tty.writer().end -| before));
if (s.tty.tty_writer.pos + s.tty.writer().end != before) FrameAck.ask(s.tty.writer(), @intCast(std.Io.Clock.awake.now(s.io).nanoseconds));
+ s.drawn_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
tz_render.end();
}
@@ -2572,15 +2603,37 @@ fn wakeFs(ctx: ?*anyopaque) void {
_ = loop.tryPostEvent(.fs_ready) catch {};
}
+/// Each pane's reads posted and not yet taken in. Two at most: a flood the
+/// loop cannot keep up with waits in its pty, not in the queue ahead of a
+/// key and its echo (hundreds of 4 KB reads queued were half a second).
+var reads_queued: [pardes.MAX_PANES]std.atomic.Value(u32) = @splat(.init(0));
+
+/// A read of pane `id` taken in (or dropped): its reader may read on. Never
+/// below zero, for an event no reader posted (a test's).
+fn tookRead(io: std.Io, id: usize) void {
+ var queued = reads_queued[id].load(.acquire);
+ while (queued > 0) queued = reads_queued[id].cmpxchgWeak(queued, queued - 1, .acq_rel, .acquire) orelse {
+ if (queued == 2) io.futexWake(u32, &reads_queued[id].raw, 1);
+ return;
+ };
+}
+
fn readPty(io: std.Io, gpa: std.mem.Allocator, pty: std.Io.File, id: usize, gen: u32, loop: *Loop) anyerror!void {
// One buffer, read into directly: a Reader around it only added a second
// (and in a safe build, filled both as `undefined` on every start).
var buf: [0x10000]u8 = undefined;
- while (true) {
+ read: while (true) {
+ while (true) {
+ const queued = reads_queued[id].load(.acquire);
+ if (queued < 2) break;
+ io.futexWait(u32, &reads_queued[id].raw, queued) catch break :read;
+ }
const n = pty.readStreaming(io, &.{&buf}) catch break;
if (n == 0) break;
const bytes = try gpa.dupe(u8, buf[0..n]);
+ _ = reads_queued[id].fetchAdd(1, .acq_rel);
loop.postEvent(.{ .pty_read = .{ .id = id, .gen = gen, .bytes = bytes } }) catch {
+ tookRead(io, id);
gpa.free(bytes);
break;
};