diff options
| -rw-r--r-- | src/gui/gui.zig | 14 | ||||
| -rw-r--r-- | src/replica.zig | 40 | ||||
| -rw-r--r-- | src/tape.zig | 15 | ||||
| -rw-r--r-- | src/terminal.zig | 16 |
4 files changed, 67 insertions, 18 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index be287647..dd21680f 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -2627,7 +2627,14 @@ const Client = struct { /// What the window's input made, applied at once and sent. fn feed(cl: *Client) void { - for (cl.pending.items) |ev| cl.link.input(ev); + for (cl.pending.items) |ev| { + const r = cl.link.r; + const predicted = r.echo.stats.predicted; + cl.link.input(ev); + // PARDES_TEST_LATENCY: the input, and whether it shows at once + // (applied, or its echo predicted), for the frame that shows it. + latencyNote("I {d} {d} {d}\n", .{ r.next_seq - 1, monoNs(), @intFromBool(r.applied == r.pending.items.len or r.echo.stats.predicted > predicted) }); + } cl.pending.clearRetainingCapacity(); _ = cl.texts.reset(.retain_capacity); } @@ -2651,6 +2658,11 @@ const Client = struct { // PARDES_TEST_LATENCY: when a terminal's delta came (an echo's). if (msg == .screen) latencyNote("S {d}\n", .{monoNs()}); cl.link.take(msg); + // And after a unit, the first of its own inputs not confirmed. + if (msg == .unit) { + const r = cl.link.r; + latencyNote("U {d} {d}\n", .{ monoNs(), if (r.pending.items.len > 0) r.pending.items[0].seq else r.next_seq }); + } } cl.taken.clearRetainingCapacity(); if (ended) { diff --git a/src/replica.zig b/src/replica.zig index 1cf68db2..a88873ac 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -480,6 +480,30 @@ pub const Stats = struct { copies: u32 = 0, /// Nanoseconds: a terminal's delta applied. apply_ns: std.ArrayListUnmanaged(u64) = .empty, + /// What stopped them (`Tape.poisoned_by`), a count a name. + stops: [16]Stop = @splat(.{}), + + const Stop = struct { name: []const u8 = "", n: u32 = 0 }; + + fn stop(s: *Stats, name: []const u8) void { + for (&s.stops) |*st| if (st.n == 0 or std.mem.eql(u8, st.name, name)) { + st.name = name; + st.n += 1; + return; + }; + } + + /// `name:n,...` (spaces as dashes), or `-`. + fn writeStops(s: *const Stats, w: *std.Io.Writer) !void { + var any = false; + for (s.stops) |st| if (st.n > 0) { + if (any) try w.writeByte(','); + any = true; + for (st.name) |ch| try w.writeByte(if (ch == ' ') '-' else ch); + try w.print(":{d}", .{st.n}); + }; + if (!any) try w.writeByte('-'); + } pub fn deinit(s: *Stats, gpa: Allocator) void { s.spec_ns.deinit(gpa); @@ -854,7 +878,7 @@ pub const Replica = struct { // emulator (rows, cursor, modes), which here is the last delta's and // there what the pty has said since. It waits; phase 6 predicts the // echo. - if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison(); + if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison("a terminal has the keyboard"); c.update(in.event); c.step(); const clean = tape.endSpeculation(&r.t); @@ -865,7 +889,8 @@ pub const Replica = struct { // Undone, and the earlier ones applied again on the confirmed // position: this one waits for the authority. r.stats.stopped += 1; - if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call: it waits\n", .{ r.id, p.seq, in.event }); + r.stats.stop(r.t.poisoned_by); + if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("replica {d}: its input {d} ({t}) stopped at a host call ({s}): it waits\n", .{ r.id, p.seq, in.event, r.t.poisoned_by }); r.blocked = p.seq; const upto = r.applied; r.toConfirmed(); @@ -2796,17 +2821,22 @@ pub const Link = struct { /// went, a line at its end (test/daemon.py, the gates). pub fn writeStats(c: *Link, path: []const u8) void { const st = &c.r.stats; - var buf: [600]u8 = undefined; + var buf: [1024]u8 = undefined; const e = &c.r.echo.stats; - const line = std.fmt.bufPrint(&buf, "inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}\n", .{ + var w: std.Io.Writer = .fixed(&buf); + w.print("inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d} echo {d}/{d} confirmed {d} missed {d}", .{ st.inputs, st.speculated, st.stopped, st.units, st.fast, c.r.neutral, st.mispredicted, st.foreign, c.visible, c.r.rtt_ns / 1000, pctOf(st.spec_ns.items, 50) / 1000, pctOf(st.spec_ns.items, 99) / 1000, pctOf(st.copy_ns.items, 50) / 1000, pctOf(st.copy_ns.items, 99) / 1000, pctOf(st.rollback_ns.items, 50) / 1000, pctOf(st.rollback_ns.items, 99) / 1000, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk, c.join.sent_bytes >> 10, e.predicted, e.predicted + e.unpredicted, e.confirmed, e.mispredicted, }) catch return; + // What stopped its inputs, last: a word a pair before it. + w.writeAll(" stops ") catch return; + st.writeStops(&w) catch return; + w.writeByte('\n') catch return; var z: [4096]u8 = undefined; - appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, line); + appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, w.buffered()); } /// The link ended: whether the client joins again, and why in its diff --git a/src/tape.zig b/src/tape.zig index 1ca064bc..7b7c02f2 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -216,6 +216,9 @@ pub const Tape = struct { /// checks. speculating: bool = false, poisoned: bool = false, + /// What stopped it: a host function's or a site's name, or a poison's + /// reason (PARDES_REPLICA_STATS counts them). + poisoned_by: []const u8 = "", /// The allocator of the core a replay makes (a replica's rollback epoch, /// src/rollback.zig), when it is not the tape's own. core_gpa: ?std.mem.Allocator = null, @@ -801,6 +804,7 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype return r; } t.poisoned = true; + t.poisoned_by = name; return poisonOf(R); } const real = @field(t.real.vtable.*, name).?; @@ -839,7 +843,7 @@ pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@Ty /// lives to the step's end (scratch, or the authority's own), and the /// caller copies what it keeps. pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - poison(); + poison("the authority's own state"); return value(.authority, f, args); } @@ -872,6 +876,7 @@ fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anyt // the authority's answer. if (yields and t.speculating) { t.poisoned = true; + t.poisoned_by = @tagName(s); return poisonOf(R); } if (comptime s == .clock or s == .mtime) if (t.speculating) if (t.guess) |g| { @@ -995,11 +1000,12 @@ pub fn screenAuthority() bool { return if (on) |t| t.screen_state and !t.replay and !t.speculating else false; } -/// What a speculation reached is the authority's: the input is undone and -/// waits for the authority's order. -pub fn poison() void { +/// What a speculation reached is the authority's (`why`): the input is +/// undone and waits for the authority's order. +pub fn poison(why: []const u8) void { if (on) |t| if (t.speculating) { t.poisoned = true; + t.poisoned_by = why; }; } @@ -1098,6 +1104,7 @@ pub fn beginSpeculation(t: *Tape) void { t.replay = false; t.speculating = true; t.poisoned = false; + t.poisoned_by = ""; t.no_digest = true; t.buf.clearRetainingCapacity(); t.seeFields(); diff --git a/src/terminal.zig b/src/terminal.zig index e9d628aa..be886044 100644 --- a/src/terminal.zig +++ b/src/terminal.zig @@ -607,16 +607,16 @@ pub const emulator_gpa = std.heap.c_allocator; /// A replica's speculation leaves an emulator be: it is the authority's /// (docs/next-daemon.md, phase 5), so the input waits for its order. -fn authorityOnly() bool { +fn authorityOnly(comptime why: []const u8) bool { if (!pardes.tape.speculating()) return false; - pardes.tape.poison(); + pardes.tape.poison(why); return true; } /// Move the emulator's viewport by `delta` shell rows (negative scrolls back). pub fn scrollGrid(pane: *Pane, delta: i32) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("scroll a terminal")) return; const state = pane.terminal orelse return; state.vt.screens.active.scroll(.{ .delta_row = delta }); state.changes +%= 1; @@ -632,7 +632,7 @@ pub fn atBottom(pane: *const Pane) bool { /// Snap the viewport back onto live output. pub fn followOutput(pane: *Pane) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("follow a terminal")) return; const state = pane.terminal orelse return; const moved = state.vt.screens.active.pages.viewport != .active; state.vt.screens.active.scroll(.active); @@ -643,7 +643,7 @@ pub fn followOutput(pane: *Pane) void { /// a scrollback; the next resize retries with the same numbers. pub fn resizeGrid(pane: *Pane, gpa: std.mem.Allocator, cols: u16, rows: u16) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("resize a terminal")) return; const state = pane.terminal orelse return; if (cols != pane.cols) for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); @@ -868,7 +868,7 @@ pub fn screenTextLen(pane: *Pane) usize { /// Release the emulator's heap. The Pane allocation itself is the core's. pub fn deinitEmulator(pane: *Pane, gpa: std.mem.Allocator) void { if (comptime !enabled) return; - if (authorityOnly()) return; + if (authorityOnly("close a terminal")) return; const state = pane.terminal orelse return; if (comptime images_enabled) state.images.deinit(gpa); state.stream.deinit(); @@ -898,7 +898,7 @@ fn emulator(gpa: std.mem.Allocator, cols: u16, rows: u16) !ghostty_vt.Terminal { /// Allocate the live emulator half of a terminal pane. Slot ownership, serial /// assignment, and spawn effects remain core lifecycle invariants. pub fn create(gpa: std.mem.Allocator, cols: u16, rows: u16) !*Pane { - if (authorityOnly()) return error.OutOfMemory; + if (authorityOnly("open a terminal")) return error.OutOfMemory; const pane = try createDoc(gpa, cols, rows); errdefer gpa.destroy(pane); if (comptime !enabled) return pane; @@ -1163,7 +1163,7 @@ pub fn feedOutput(p: *Pardes, pane: *Pane, bytes: []const u8) void { // There are no pty reads at all without an emulator to parse them into. if (comptime !enabled) return; if (pane.terminal == null) return; - if (authorityOnly()) return; + if (authorityOnly("a terminal's output")) return; const has_positions = pane.ovl != null or pane.body.cur_pinned or pane.body.vsel.active or pane.body.msel.active or pane.body.nsel > 0 or pane.body.last_edit != null or pane.look_at != null or pane.body.ed_undo_len > 0 or pane.body.ed_redo_len > 0 or pointerRowCount(pane) > 0; |
