From 1017496cc9c7e95a39b93928653e7ff338069152 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 21:06:02 -0300 Subject: 9P: a request that changes the core waits for the output already read out of the ptys before it, as a terminal shows a program's output before what it led to: `cmd | pardes -` typed in a pane writes its pager over 9P after the shell echoed the line and started the command, and the session, which feeds its queue a read a step (dev/tui/tui.zig `takeQueue`), could still hold that echo when the write was served at its next rest (runtime/9p_io.zig `onServe`); the pager's layout then gave the shell's pane its rows as the stale emulator had its cursor (one short), and the emulator's resize cleared the command line as a prompt the shell would draw again (ghostty's Screen.clearPromptForRedraw: the cursor still after `133;B`, the `133;C` not yet fed), which bash, running the command, never did; process-test's `pagercolor` failed so as a pair 7 of 20 pinned to one CPU (a client's keys reach the shell a step each, so its echo comes a key a read: 55 reads queued in a failing run, the write served with seven still to feed) and on the own path 1 of 20 (its keys a burst a step: a read or two); now core/Turn.zig counts the reads (`reads`, by each pty reader as it reads, dev/Inbox.zig `readPty`, beside `unapplied`) and those applied or dropped (`applied`, `outputApplied` from `Inbox.applied`), and `awaitOutput` waits, the turn given up, until every read made before the request is applied, only while the editor waits in its loop (`listening`, set by the new `listen` in the tui's `waitOnce` and the window's `waitInput`, cleared by `wake`): resting in a call of its own (a save through its own mount, core/fs.zig `writeOut`) it applies nothing and the request goes first as before; `onServe` waits so for every request that `needsQuiet` and asks `quiet` after; a user could hit it typing or pasting `cmd | pardes -` in a pane of a session with a client attached, or under load on the own path (the pager open, the command line wiped from the shell's screen); pagercolor pinned to CPU 5 after: as a pair 20 of 20 first time, own path 20 of 20 (before: 13 of 20, 19 of 20); a core test of the rule (`Turn` now in core's test imports); core-test 918/1075 (157 skipped), shell-test 1116/1119 (3 skipped) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- core/Turn.zig | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ core/root.zig | 1 + dev/Inbox.zig | 8 ++++-- dev/gui/gui.zig | 2 +- dev/tui/tui.zig | 2 +- runtime/9p_io.zig | 7 ++++- 6 files changed, 93 insertions(+), 5 deletions(-) diff --git a/core/Turn.zig b/core/Turn.zig index 249c6795..d0e49d44 100644 --- a/core/Turn.zig +++ b/core/Turn.zig @@ -43,6 +43,15 @@ restores: u64 = 0, /// Bumped each time a language server's answer (or its lack) is taken /// in (`lspResponse`): a write that asked one waits for this. lsp_answers: u64 = 0, +/// Reads out of the ptys (a pane's reader, as it reads) and those applied +/// or dropped since (`outputApplied`): a write waits for the output read +/// before it (`awaitOutput`). +reads: std.atomic.Value(u64) = .init(0), +applied: std.atomic.Value(u64) = .init(0), +/// The editor gave the turn up to wait for input (`listen`), not for a +/// call of its own (`rest`, `yield`: a save through its own mount, whose +/// write it would never answer applying output meanwhile). +listening: bool = false, /// All of the above, and `out` reaching zero. settled: std.Io.Condition = .init, /// Every `yield`, ever: a replica's step makes none (each syscall of its @@ -69,12 +78,22 @@ pub fn rest(t: *Turn) void { t.release(); } +/// The editor is about to wait for input, the output its readers read +/// among it (its loop's wait): `rest`, and a write may wait for that +/// output meanwhile (`awaitOutput`). +pub fn listen(t: *Turn) void { + if (t.io == null) return; + t.listening = true; + t.release(); +} + /// The editor takes the turn back -- once every step that is out in a /// syscall has returned, since a step of the editor's may change what /// those still point into. pub fn wake(t: *Turn) void { const io = t.io orelse return; t.mutex.lockUncancelable(io); + t.listening = false; while (t.out != 0) t.settled.waitUncancelable(io, &t.mutex); } @@ -123,6 +142,32 @@ pub fn bump(t: *Turn, counter: *u64) void { t.settled.broadcast(io); } +/// A pty's read applied, or dropped: a write waiting on it told. +pub fn outputApplied(t: *Turn) void { + _ = t.applied.fetchAdd(1, .acq_rel); + if (t.io) |io| t.settled.broadcast(io); +} + +/// With the turn: waits, turn given up meanwhile, until every read out of +/// a pty before now is applied, as a terminal shows a program's output +/// before what it led to: `cmd | pardes -` typed in a pane writes its +/// pager after the shell echoed the line and started the command, and +/// the editor, a read a step, could still hold that echo (a client's keys +/// come a step each, so a shell's echo comes a key a read): the pager's +/// layout then saw the shell's line not yet ended, and its emulator's +/// resize cleared that line as a prompt the shell would draw again +/// (ghostty's Screen.clearPromptForRedraw). Only while the editor waits +/// in its loop (`listening`): resting in a call of its own it applies +/// nothing, and the request goes first, as before. A read its reader +/// drops off the turn wakes nobody: seen at the next recheck. +pub fn awaitOutput(t: *Turn) void { + const mark = t.reads.load(.acquire); + while (t.listening and t.applied.load(.acquire) < mark) t.settled.waitTimeout(t.io.?, &t.mutex, .{ .duration = .{ .raw = .fromMilliseconds(25), .clock = .awake } }) catch |err| switch (err) { + error.Timeout => {}, + error.Canceled => return, + }; +} + /// With the turn: waits, turn given up meanwhile, until `counter` has /// moved past `seen`; stopping ends the wait early. pub fn awaitPast(t: *Turn, counter: *const u64, seen: u64) void { @@ -139,3 +184,36 @@ fn release(t: *Turn) void { t.mutex.unlock(t.io.?); if (woken) if (t.wake_parked) |f| f(t.wake_ctx); } + +test "a write waits for the output read before it while the editor listens, not while it rests in a call of its own" { + const io = std.testing.io; + var t: Turn = .{}; + t.start(io); + _ = t.reads.fetchAdd(2, .acq_rel); + // A save through its own mount: the editor rests in it, applies + // nothing, and the write it waits on is served at once. + t.rest(); + _ = t.take(); + t.awaitOutput(); + try std.testing.expectEqual(@as(u64, 0), t.applied.load(.acquire)); + t.give(); + // Its loop's wait: the write waits for both reads, applied meanwhile. + t.wake(); + t.listen(); + const editor = try std.Thread.spawn(.{}, struct { + fn apply(turn: *Turn) void { + for (0..2) |_| { + turn.wake(); + turn.outputApplied(); + turn.listen(); + } + } + }.apply, .{&t}); + _ = t.take(); + t.awaitOutput(); + try std.testing.expectEqual(@as(u64, 2), t.applied.load(.acquire)); + t.give(); + editor.join(); + t.wake(); + t.stop(); +} diff --git a/core/root.zig b/core/root.zig index 3bca5e03..a476f90f 100644 --- a/core/root.zig +++ b/core/root.zig @@ -443,6 +443,7 @@ test { _ = @import("Handover.zig"); _ = @import("walk.zig"); _ = @import("views.zig"); + _ = Turn; // A window's build links the window's C (font.c over FreeType and // HarfBuzz) into each of its programs, and their allocator (ui_malloc // and the rest) is gui.zig's. diff --git a/dev/Inbox.zig b/dev/Inbox.zig index d733b971..1df516cb 100644 --- a/dev/Inbox.zig +++ b/dev/Inbox.zig @@ -118,7 +118,9 @@ pub fn acceptReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { /// One of `pane`'s reads applied, or dropped unapplied. pub fn applied(q: *Inbox, pane: pardes.Pane.Id) void { const n = &q.unapplied[pane.index()]; - if (n.load(.acquire) > 0) _ = n.fetchSub(1, .acq_rel); + if (n.load(.acquire) == 0) return; + _ = n.fetchSub(1, .acq_rel); + pardes.turn.outputApplied(); } pub fn cancelReader(q: *Inbox, pane: pardes.Pane.Id, gen: u32) void { @@ -316,8 +318,10 @@ fn readPty(q: *Inbox, pty: std.Io.File, pane: pardes.Pane.Id, gen: u32) void { }, }; if (n == 0) continue; - // Counted at once: out of the pty, the poll no longer sees it. + // Counted at once: out of the pty, the poll no longer sees it, + // nor a write after it (Turn.awaitOutput). _ = q.unapplied[pane.index()].fetchAdd(1, .acq_rel); + _ = pardes.turn.reads.fetchAdd(1, .acq_rel); const bytes = q.gpa.dupe(u8, buf[0..n]) catch |err| { q.applied(pane); failure = err; diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 3193d966..b00073fa 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -2696,7 +2696,7 @@ fn waitInput(ctx: ?*anyopaque, timeout_ms: u32) void { s.held = null; if (held) |h| sev = h; // The wait is the 9P connections' turn with the core. - pardes.turn.rest(); + pardes.turn.listen(); const got = held != null or c.SDL_WaitEventTimeout(&sev, ms); pardes.turn.wake(); // A virtual clock: while a render-time animation moves, each loop diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index 5d75722f..ec5a3309 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -1494,7 +1494,7 @@ const Shell = struct { s.wake_to = s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0; requestWake(s.io, wait); } - pardes.turn.rest(); + pardes.turn.listen(); const got = s.loop.nextEvent() catch { pardes.turn.wake(); if (s.client) |c| c.link.quit = true; diff --git a/runtime/9p_io.zig b/runtime/9p_io.zig index 1079b102..50011690 100644 --- a/runtime/9p_io.zig +++ b/runtime/9p_io.zig @@ -183,8 +183,13 @@ pub const Listener = struct { const refused = pardes.ctlfs.Reply.fail(req.tag, pardes.ctlfs.E.IO); return conn.reply(&refused, ""); } - const quiet = pardes.turn.take(); + _ = pardes.turn.take(); defer pardes.turn.give(); + // What changes the core comes after the output already read out of + // the ptys (a pane's `cmd | pardes -`: its pager after the line's + // echo); quiet asked after that wait, which gave the turn up. + if (pardes.ctlfs.needsQuiet(l.core, req)) pardes.turn.awaitOutput(); + const quiet = pardes.turn.out == 0; // A connection a Restore is cutting (`reset`) is the old editor's // client: its fids name the old editor's panes and opens. if (l.cutting(conn)) { -- cgit v1.3 From 0593b639b584e272518562e587f2a02cdfad8f5a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 21:20:39 -0300 Subject: tty: a pair's client glides on the case's clock, as the session's own terminal does: its Shell was built in `runClient` with no `test_clock`, so under PARDES_TEST_CLOCK its glide (dev/glide.zig: a scroll's picture, critically damped, at whole rows: a one-row glide's first frames keep the old rows) ran on the real clock, frames 16 ms apart with the process asleep between them, longer than a case's stable probe waits (test/tty.zig `waitStable`'s fast path, 50 ms unchanged and 10 ms asleep): process-test's `themesel` as a pair snapped the list's selection moved and the list not yet scrolled, 0 of 10 first time pinned to one CPU (the runner's retry, its probes wider, waited the glide out: why the retry always passed, open since pair-8); and the clock alone was not enough (0 of 10, now at the first scroll every time): the virtual clock jumps at each wake to `wake_to`, which `Shell.waitOnce` set from the core's clock, and a client's core clock is the session's, still between units, so its glide never moved: a client's `wake_to` while it glides is its own virtual clock plus a frame (the session's own unchanged); a user cannot hit it (the clock is the tests'); themesel as a pair pinned after: 10 of 10 first time; process-test as pairs 0 of 146 failed but for `root-ctl: Exit is answered` once (a detached start's socket not there in 20 s at load 8.6; 10 of 10 alone pinned), `tty boot: ninep`'s dial refused once in the parallel batch and passed alone (the base's, pardes-raw-tty's words), own path 0 of 146 with none failing first time Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- dev/tui/tui.zig | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/dev/tui/tui.zig b/dev/tui/tui.zig index ec5a3309..d1bbe140 100644 --- a/dev/tui/tui.zig +++ b/dev/tui/tui.zig @@ -791,6 +791,9 @@ pub fn runClient(init: std.process.Init, opts: pardes.Options, path: []const u8, .client = &c, .peers = &no_peers, .ptys = &no_ptys, + // Its own motions (the glide) on the case's clock, as a session's + // own terminal's (`Own.open`). + .test_clock = host_io.testClock(), }; tty = .{ .sh = &sh, .screen = &screen, .loop = &loop, .joins = &start }; no_inbox.wake_fn = Shell.wakeForQueue; @@ -1490,8 +1493,13 @@ const Shell = struct { } else next: { if (wait != 0) { // A virtual clock moves a frame a wake while the tty's own - // motions move (the glide), as the real one would. - s.wake_to = s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0; + // motions move (the glide), as the real one would: a + // client's from where it is, its core's clock and wakes + // the session's, still between units. + s.wake_to = if (s.client != null) + (if (s.moving()) (s.test_clock orelse 0) + pardes.frame_ns else 0) + else + s.core.nextWake() orelse if (s.moving()) s.core.clock.now_ns + pardes.frame_ns else 0; requestWake(s.io, wait); } pardes.turn.listen(); -- cgit v1.3 From 86414697b09897bdd5091037a4709748cecbc3bf Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 21:47:57 -0300 Subject: daemon.py: pair_rows' last row says which side went when "a Detach with no daemon is refused" fails (the client, the session, or both; the client's exit code; whether the session lives; the client terminal's last 600 bytes, its last screen and words: a pair's session writes no log), as merge58's ReleaseSafe plain run 2 of 3 failed there saying nothing; not reproduced as it is (this tree's ReleaseSafe, pair_rows alone: 20 of 20 pinned to two CPUs, 20 of 20 to one); widened by an experiment (the lone pair's client put off the session after its third unit, PARDES_TEST_DIVERGE=3) it fails with those words and now says "the client and the session went (client exit 1, session gone)", the client's last words "'lone' let this client go", its screen showing the Detach refused just before: the Detach is refused as it should be, and what ends the editor is the client letting its link go (a divergence, or 2 s behind) and its rejoin through the daemon's replica socket failing (no daemon keeps a PARDES_DAEMON=off pair), then the session ending with its last link (runtime/session.zig): the product's, open (a ruling: a link with no way back kept through a lag; a divergence there has no remedy without a re-dump over the same link) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/daemon.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/daemon.py b/test/daemon.py index 9e0047e5..1c86e5ad 100644 --- a/test/daemon.py +++ b/test/daemon.py @@ -925,7 +925,11 @@ def pair_rows(binary): t, c, nine, session, pane = started('lone', dict(env, PARDES_DAEMON='off')) typed_detach(t, nine, pane) time.sleep(1) - assert c.poll() is None and alive(session), 'a Detach with no daemon is refused' + # Which went, if one did: the client's exit and its screen's last + # words, the session's life (its stderr is /dev/null: no log). + went = [w for w, gone in (('the client', c.poll() is not None), ('the session', not alive(session))) if gone] + assert not went, (f'a Detach with no daemon is refused: {" and ".join(went)} went (client exit {c.poll()}, ' + f'session {"alive" if alive(session) else "gone"}); the terminal\'s last bytes: {t.text()[-600:]!r}') nine.socket.close() t.close() terms.remove(t) -- cgit v1.3 From 9e5b54cfdf73728745d2aeaafac00d823114543e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sun, 22 Nov 2026 22:27:25 -0300 Subject: replicas: the soak with a pair among its sides (`--pair`): client 0 is the session's pair client, as a terminal's `pardes` is under the variable (runtime/main.zig `pair`): its socket pair made before the session, the session's end its first client (`Soak.options`: `o.pair`, joined with `.own` before the first effects by runtime/session.zig), its size the session's, no dial and no own typist (nobody else draws the session's view: `watchedHost` false), and its replica's core its own boot (`Soak.booted`: `Replica.initBoot` with the options the session starts with, as `replica.ownBoot`), kept when the welcome's digest is its own (`confirmBoot`'s rule) or else the session's dump loaded and the FIELDS of the two cores that differ printed and counted unequal (the one place a client's core comes from no dump: where the coordinator's merge57 gate saw a pair's first quiet check differ once on ReleaseSafe); a line per rtt says the boots and how many were not the session's core; three runs of `--clients 5 --pair` (Debug, loads 1.7-8.5): diverged 0 and unequal 0 at 30, 80 and 207 ms each, the pair's client booted its own core 36 times and every one was the session's at its join (no field named: none differed), quiet checks 988/0 933/0 959/0, 1327/0 1112/0 1063/0, 1076/0 1064/0 987/0, unlisted stops 0, the corpus through a client 22 of 22 scenes the same, no leak; so the merge57 gate's pair `differ` is not a boot's in Debug (not run on ReleaseSafe) Co-Authored-By: Claude Opus Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- test/replicas.zig | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/test/replicas.zig b/test/replicas.zig index 90f7c10d..57bd7617 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -51,6 +51,12 @@ var trace = false; /// `--witness`: the witness judged at every run's end, not only after a /// divergence (`Witness`). var witness_always = false; +/// `--pair`: client 0 is the session's pair client (Options.pair: a +/// terminal's `pardes` as a client and its session): joined before the +/// session's first effects, its core its own boot (replica.ownBoot), the +/// welcome's digest its check (confirmBoot), the session's own frontend +/// gone (no typist, its view the pair's). +var pair_mode = false; /// The PDF the soak opens (`--pdf`), absolute. var pdf_path: ?[]const u8 = null; const frame_us = 16_667; @@ -246,6 +252,10 @@ const Totals = struct { /// (PARDES_TEST_DIVERGE's), and clients that left; quiet checks. joins: u64 = 0, refused: u64 = 0, + /// A pair's client's own boot the session's core at its join (`--pair`), + /// or not (its dump loaded instead, the fields that differ said). + boots: u64 = 0, + boots_unequal: u64 = 0, rejoined: u64 = 0, detached: u64 = 0, agree: u64 = 0, @@ -1174,6 +1184,9 @@ const Soak = struct { /// `Peers.detach` shut its link; the session's own view's is its /// terminal's, which the soak has none of). detach_view: ?u8 = null, + /// `--pair`: the session's end of client 0's link, its first client's + /// (`options`). + pair_fd: c_int = -1, // ---- the soak ---- /// The soak's time (us): the last message's. @@ -1220,7 +1233,7 @@ const Soak = struct { /// its own (Core.render: drawn whenever a frame is owed and someone /// sees it, and a terminal is always seen). fn watchedHost(_: ?*anyopaque) bool { - return true; + return !pair_mode; } fn ptyWrite(ctx: ?*anyopaque, id: pardes.Pane.Id, bytes: []const u8) void { @@ -1290,7 +1303,10 @@ const Soak = struct { // ---- the session loop's frontend ---- - pub fn options(_: *Soak, _: *pardes.Options) void {} + /// A pair's: its first client on the link `run` made. + pub fn options(s: *Soak, o: *pardes.Options) void { + if (s.pair_fd >= 0) o.pair = s.pair_fd; + } pub fn open(s: *Soak, l: *SessionLoop) !pardes.Host { s.l = l; @@ -1355,6 +1371,8 @@ const Soak = struct { // let go); the first leaves when it is done; the second is put off // the session once (PARDES_TEST_DIVERGE) and joins again for its view. for (s.clients, 0..) |*cl, c| { + // A pair's client joined at the loop's start. + if (pair_mode and c == 0) continue; cl.dialling = true; const at: u64 = switch (c) { 0, 1 => s.random.uintLessThan(u64, 100_000), @@ -1366,7 +1384,7 @@ const Soak = struct { }; try net.send(.{ .at = at, .n = 0, .kind = .dial, .who = @intCast(c + 1), .bytes = &.{} }); } - try net.send(.{ .at = s.random.uintLessThan(u64, 200_000), .n = 0, .kind = .own, .who = 0, .bytes = &.{} }); + if (!pair_mode) try net.send(.{ .at = s.random.uintLessThan(u64, 200_000), .n = 0, .kind = .own, .who = 0, .bytes = &.{} }); try net.send(.{ .at = tui_at, .n = 0, .kind = .output, .who = 0, .bytes = &.{} }); try net.send(.{ .at = session_at, .n = 0, .kind = .output, .who = 2, .bytes = &.{} }); try net.send(.{ .at = flood_at, .n = 0, .kind = .output, .who = 1, .bytes = &.{} }); @@ -1934,7 +1952,7 @@ const Soak = struct { try s.totals.add(&old.stats); old.deinit(); } - const rp = try replica.Replica.initDump(gpa, cl.id, cl.view, m.bytes, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); + const rp = if (pair_mode and c == 0 and !s.has_link[0]) try s.booted(cl, m.bytes) else try replica.Replica.initDump(gpa, cl.id, cl.view, m.bytes, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); cl.r = rp; // In process: its clock reads are the session's machine's, and the // units' heads carry the soak's clock (`stamp`). @@ -1979,6 +1997,29 @@ const Soak = struct { if (cl.left > 0 and cl.left == s.r.steps) try net.send(.{ .at = m.at + typistGap(s.random), .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }); } + /// A pair's client's replica: its core its own boot from the options + /// the session was started with (`run`'s), as the pair's client makes + /// it (replica.ownBoot), kept when the welcome's digest is its own + /// (confirmBoot); else the session's dump, loaded, and the fields of + /// the two cores that differ said. + fn booted(s: *Soak, cl: *Client, dump: []const u8) !*replica.Replica { + s.totals.boots += 1; + const boot = try replica.Replica.initBoot(gpa, .{ .cols = 100, .rows = 30, .boot = .tty, .distrusted = &cl.distrusted }); + if (boot.digest() == cl.digest) { + boot.id = cl.id; + boot.view = cl.view; + return boot; + } + defer boot.deinit(); + s.totals.boots_unequal += 1; + const rp = try replica.Replica.initDump(gpa, cl.id, cl.view, dump, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); + var buf: [4096]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + Witness.fieldsDiffer(boot.core(), rp.core(), &w); + std.debug.print("seed {d} rtt {d} ms: the pair's own boot is not the session's core at its join; differ: {s}", .{ s.r.seed, s.r.rtt_ms, w.buffered() }); + return rp; + } + /// At quiescence: every client's digest the session's, its terminals /// the session's cell for cell, its files' texts byte for byte. fn finish(s: *Soak) !void { @@ -2115,6 +2156,12 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { var clients: [8]Client = undefined; for (clients[0..r.clients], 0..) |*cl, c| { cl.* = .{ .kind = if (c % 2 == 0) .tty else .window, .left = r.steps, .cols = @intCast(random.intRangeAtMost(u16, 60, 120)), .rows = @intCast(random.intRangeAtMost(u16, 16, 40)) }; + // A pair's client is the terminal the session was started at the + // size of (main.zig `pair`). + if (pair_mode and c == 0) { + cl.cols = 100; + cl.rows = 30; + } var name: [300]u8 = undefined; cl.check = try gpa.dupeSentinel(u8, try std.fmt.bufPrint(&name, "{s}/check-{d}", .{ dir, c }), 0); _ = libc.unlink(cl.check); @@ -2158,9 +2205,23 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { .flood = flood, .yes_len = yes.len, .own_left = r.steps, - .typing = r.clients + 1, + .typing = r.clients + @intFromBool(!pair_mode), .media = &media, }; + // A pair's client's link, made before the session (main.zig `pair`): + // its end handed to the session's options, the soak reading the other + // as for a dial (`.dial`). + if (pair_mode) { + const cl = &clients[0]; + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + _ = libc.fcntl(fds[0], libc.F.SETFL, @as(c_int, @bitCast(libc.O{ .NONBLOCK = true }))); + cl.sfd = fds[0]; + cl.cfd = fds[1]; + cl.handed = libc.dup(fds[0]); + if (cl.handed < 0) return error.NoSocket; + soak.pair_fd = cl.handed; + } // The session: THE session loop, from its start to its end. try SessionLoop.run(init, .{ .cols = 100, .rows = 30, .boot = .tty }, &soak); if (soak.err) |err| return err; @@ -3249,6 +3310,10 @@ pub fn main(init: std.process.Init) !void { witness_always = true; continue; } + if (std.mem.eql(u8, name, "--pair")) { + pair_mode = true; + continue; + } if (i + 1 >= args.len) return error.MissingValue; i += 1; const value = args[i]; @@ -3314,6 +3379,8 @@ pub fn main(init: std.process.Init) !void { pardes.Term.reloads = .{}; for (first..first + seeds) |seed| try run(init, .{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .flood_mb = flood_mb }, dir, &t); std.debug.print("rtt {d}: {d} reloads of a terminal at joins, every side (the session's own too), {d} us each, {d} us at worst; {d} kitty images in clients at quiescence, each the session's pixels\n", .{ rtt, pardes.Term.reloads.n, pardes.Term.reloads.ns / @max(1, pardes.Term.reloads.n) / 1000, pardes.Term.reloads.worst_ns / 1000, t.images_equal }); + if (pair_mode) std.debug.print("rtt {d}: the pair's client booted its own core {d} times, {d} of them not the session's at its join\n", .{ rtt, t.boots, t.boots_unequal }); + if (t.boots_unequal > 0) t.unequal += t.boots_unequal; const rollbacks = t.mispredicted + t.foreign; std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ rtt, t.inputs, t.speculated, -- cgit v1.3