diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Kind.zig | 10 | ||||
| -rw-r--r-- | src/gui/gui.zig | 11 | ||||
| -rw-r--r-- | src/ninep/events.zig | 5 | ||||
| -rw-r--r-- | src/panes.zig | 4 | ||||
| -rw-r--r-- | src/replica.zig | 141 | ||||
| -rw-r--r-- | src/tape.zig | 4 |
6 files changed, 141 insertions, 34 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index aae6145a..b4e04d6d 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -215,8 +215,10 @@ pub const Slot = struct { /// A client's join: its plain kind's file, until the pane is opened /// from it (dump.plainHere). file: ?[]u8 = null, - /// Debug: the kind's allocations, which paint must leave as they are - /// (the list the page allocator's: a region's first is slow in Debug). + /// Debug: the kind's allocations, which paint must leave as they are. + /// The slot's allocator's, as the slot is: a client's rollback puts + /// the list back with the slot that holds it (on the page allocator, + /// the list put back was one a speculation had grown and freed). used: if (checked) std.ArrayList([]u8) else void = if (checked) .empty else {}, pub const Job = struct { token: u32, in: []u8 }; @@ -236,7 +238,7 @@ pub const Slot = struct { pub fn free(s: *Slot) void { for (&s.jobs) |*j| if (j.*) |job| s.gpa.free(job.in); s.gpa.free(s.selection); - if (checked) s.used.deinit(std.heap.page_allocator); + if (checked) s.used.deinit(s.gpa); if (s.plain) |b| if (b.len > 0) s.gpa.free(b); s.arena.deinit(); s.gpa.free(s.path); @@ -1083,7 +1085,7 @@ pub const api: c.pa_api = .{ break :mem cl.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null; }; @memset(mem, 0); - if (checked) cl.slot.used.append(std.heap.page_allocator, mem) catch {}; + if (checked) cl.slot.used.append(cl.slot.gpa, mem) catch {}; return mem.ptr; } }.f, diff --git a/src/gui/gui.zig b/src/gui/gui.zig index a8816494..4f6a58c2 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -1216,6 +1216,10 @@ const Client = struct { inbox: std.ArrayListUnmanaged(replica.Said) = .empty, taken: std.ArrayListUnmanaged(replica.Said) = .empty, ended: bool = false, + /// The size the window last posted while its view is not that size + /// yet (the session has not taken it, or speculation stopped at it): + /// posted once, not again each pass while it waits. + posted: ?@FieldType(pardes.Event, "resize") = null, fn deinit(cl: *Client) void { cl.pending.deinit(cl.gpa); @@ -1330,7 +1334,11 @@ const OwnView = struct { if (scrollRows(g, v.in, v.s.gpa)) v.s.client.?.link.dirty = true; if (fitTaglineFont(g, core.settings.font.tagline_percent)) v.s.client.?.link.dirty = true; const geom = windowCells(g); - if (resizeOf(core.view(), geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) |ev| v.in.post(ev); + const cl = v.s.client.?; + if (resizeOf(core.view(), geom.cols, geom.rows, g.cell_w, g.cell_h, g.tagline_width, g.tagline_height)) |ev| { + if (cl.posted == null or !std.meta.eql(cl.posted.?, ev.resize)) v.in.post(ev); + cl.posted = ev.resize; + } else cl.posted = null; if (v.pointer) refreshPresentedPointer(g, v.in); runDemo(g, v.in, core); if (v.s.test_mode) { @@ -1424,6 +1432,7 @@ fn clientSession(init: std.process.Init, g: *Gui, opts_in: pardes.Options, test_ g.hit_core = core; frame_probe = &core.frames; link.reset(r, &joined); + client.posted = null; client.ended = false; // ponytail: why it joined again is the window's title (and its // log) until a key, where a terminal's client draws it over its diff --git a/src/ninep/events.zig b/src/ninep/events.zig index 9e6df93d..2af86108 100644 --- a/src/ninep/events.zig +++ b/src/ninep/events.zig @@ -154,8 +154,11 @@ pub fn announce(p: *Pardes) void { /// The panes present at boot are the log's first records: something said /// before the first update ends (an init file's notice) waits for them. +/// The log is the session's (`p.fs`, the authority's): a replica client's, +/// joined from a dump, is empty, and its step must not announce (taped +/// reads, Recent's) where the session's, booted long ago, does not. fn bootFirst(p: *Pardes) void { - if (newest(p) == null) announce(p); + if (newest(p) == null and !pardes.tape.replaying()) announce(p); } /// Records `<kind> <serial> <name>`. diff --git a/src/panes.zig b/src/panes.zig index 237693fa..e9c9b1d3 100644 --- a/src/panes.zig +++ b/src/panes.zig @@ -945,7 +945,9 @@ pub const Pane = struct { /// replica by its authority (the next delta says), on such an authority /// once the step's unit is out (`resizeDeferred`). pub fn resizeGrid(pane: *Pane, cols: u16, rows: u16) void { - if (tape.authorityOnly("resize a terminal")) return; + // A screen-state client's speculation does what its replay does + // (its selections, never the grid): it waits for no round trip. + if (!tape.screenState() and tape.authorityOnly("resize a terminal")) return; const state = pane.terminal orelse return; if (cols != pane.cols) for (0..pane.sel.len) |button| { pane.clearPointerSelection(button); diff --git a/src/replica.zig b/src/replica.zig index 16c6b15d..98426949 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -43,6 +43,11 @@ const stamp_len = wire.header_len + 16; /// Confirmed units fast-pathed under one copy before it is taken afresh: /// a rollback replays them all. const rebase_after = 32; +/// A client compares its core's digest with the session's at most once in +/// this many confirmed units (a unit that carries one, its state the +/// confirmed one), and once quiet (`Link.quietCheck`): a full digest is a +/// walk of the whole core, an edit's worth of work. +const check_every = 32; fn header(buf: []u8, who: u16, seq: u32) void { std.mem.writeInt(u16, buf[0..2], who, .little); @@ -173,6 +178,8 @@ pub const Authority = struct { a.t.screen_state = screens; // One word a digest: every pty read is a unit. a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; + // A digest at a unit's end, as the session's (Peers.unit). + a.t.no_digest = true; const was = Stepping.begin(&a.t, null); defer was.end(); a.core = try Pardes.init(gpa, opts); @@ -193,6 +200,9 @@ pub const Authority = struct { /// What was recorded since the last unit, as one (the boot's, a setup's /// done straight on the core). pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 { + const was = Stepping.begin(&a.t, null); + tape.recordDigest(&a.t); + was.end(); a.out.clearRetainingCapacity(); try a.out.appendNTimes(a.gpa, 0, unit_head); unitHeader(a.out.items, origin, seq); @@ -233,20 +243,25 @@ pub const Authority = struct { /// authority's rolls back a replica's pending inputs), and its digest /// once a unit. pub fn output(a: *Authority, ev: pardes.Event) void { - a.t.no_digest = true; - defer a.t.no_digest = false; a.apply(ev); a.batched += 1; } + /// A pass of its host's loop with no input (a frame's wake): its pump + /// alone, its clock advanced and what is due ticked, into the open + /// batch. + pub fn pump(a: *Authority) void { + const was = Stepping.begin(&a.t, null); + a.core.pump(a.host) catch {}; + was.end(); + a.batched += 1; + } + /// The open batch as a unit, or null: what the host sends before it /// steps any other input, and once a frame. pub fn pending(a: *Authority) !?[]const u8 { if (a.batched == 0) return null; a.batched = 0; - const was = Stepping.begin(&a.t, null); - tape.recordDigest(&a.t); - was.end(); a.seq += 1; return try a.unit(0, a.seq); } @@ -557,6 +572,8 @@ pub const Stats = struct { foreign: u32 = 0, rebases: u32 = 0, units: u32 = 0, + /// Its digest compared with the session's (a unit's, or quiet). + checks: u32 = 0, /// The samples below kept: an in-process harness's replica, or a /// client's with PARDES_REPLICA_STATS. Without, nothing is: a client /// runs for days, and they grew a word an input and a delta. @@ -654,6 +671,8 @@ pub const Replica = struct { last_digest: std.ArrayListUnmanaged(u8) = .empty, /// Units of another's taken so, no rollback made. neutral: u32 = 0, + /// Confirmed units since its digest was last compared (`check_every`). + since_check: u32 = 0, /// What it shows changed since the host last drew it: a unit replayed /// on it, a rollback (a unit that confirmed what it applied, or moved /// nothing, changes nothing to draw). @@ -708,6 +727,7 @@ pub const Replica = struct { r.* = .{ .gpa = gpa, .id = id, .t = tape.memory(gpa), .stats = .{ .samples = true } }; r.t.screen_state = screens; r.t.core_gpa = r.epoch.allocator(); + r.t.checks = false; r.confirm(first); if (r.t.core == null) { r.deinit(); @@ -728,6 +748,7 @@ pub const Replica = struct { const r = try gpa.create(Replica); r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true }; r.t.skips_render = true; + r.t.checks = false; // Its terminals are the session's deltas (Peers sends them). r.t.screen_state = true; errdefer { @@ -844,6 +865,7 @@ pub const Replica = struct { if (r.diverged != null) return; const u = parseUnit(msg) orelse return r.fail("a unit too short to read"); r.stats.units += 1; + r.since_check +|= 1; if (u.origin == r.id) if (r.t.core) |c| r.echo.confirmed(c, u.seq); // A copy taken ahead (`ready`), nothing on top: it stays the point a // rollback goes back to, the unit replayed on it and kept in `since`, @@ -865,11 +887,9 @@ pub const Replica = struct { { r.stats.fast += 1; // The state it ends on, which a unit of another's that moves - // nothing ends on again (`neutral`). - if (lastDigest(u.tape)) |d| { - r.last_digest.clearRetainingCapacity(); - r.last_digest.appendSlice(r.gpa, d) catch {}; - } + // nothing ends on again (`neutral`); none, not known. + r.last_digest.clearRetainingCapacity(); + if (lastDigest(u.tape)) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; if (r.pending.items[0].terminal and r.applied > 1) { // Right, but what it did read a terminal: replayed later (a // rollback's `since`) it would read the deltas that came @@ -886,17 +906,21 @@ pub const Replica = struct { r.applied -= 1; if (r.applied == 0) { // Nothing speculative on top any more: the state is the - // confirmed one, which its digest says. A step of its own - // that read what the session's did not (a frame's, a - // derived field's) shows here, at the input that did it, - // not at some later unit replayed. - if (lastDigest(u.tape)) |d| { + // confirmed one, which its digest says, when it carries one + // and a check is due. A step of its own that read what the + // session's did not (a frame's, a derived field's) shows at + // the next check, or quiet (`Link.quietCheck`). + if (r.checkDue()) if (lastDigest(u.tape)) |d| { + r.t.checks = true; r.t.check(d); + r.t.checks = false; + r.since_check = 0; + r.stats.checks += 1; if (r.t.diverged) |why| { trace(r.gpa, "replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }, &.{u.tape}); return r.fail(why); } - } + }; // The copy stays the point a rollback goes back to, this // input kept to replay over it as a unit of the session's // that comes while idle is: a step writes some bytes of the @@ -914,10 +938,12 @@ pub const Replica = struct { return r.keepSince(u.tape); } const ends = lastDigest(u.tape); - defer if (ends) |d| { + // None: where it ended is not known, and the next unit is not + // taken as moving nothing from it. + defer { r.last_digest.clearRetainingCapacity(); - r.last_digest.appendSlice(r.gpa, d) catch {}; - }; + if (ends) |d| r.last_digest.appendSlice(r.gpa, d) catch {}; + } if (r.applied > 0 and u.origin != r.id and ends != null and std.mem.eql(u8, ends.?, r.last_digest.items)) { // Nothing a digest covers moved: what was applied on top is // what the session will apply on top of it. Replayed when the @@ -947,10 +973,20 @@ pub const Replica = struct { for (c.panes, &serials) |slot, *n| n.* = if (slot) |pane| pane.serial else 0; for (c.retired_panes.current, c.retired_panes.previous) |x, y| retiring = retiring or x != null or y != null; } - if (tape.replayUnit(&r.t, u.tape)) |why| { + // Its digest compared when one is due (the units kept since and + // replayed by a rollback were, if at all, as they came). + const check = ends != null and r.checkDue(); + r.t.checks = check; + const replayed = tape.replayUnit(&r.t, u.tape); + r.t.checks = false; + if (replayed) |why| { trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape}); return r.fail(why); } + if (check) { + r.since_check = 0; + r.stats.checks += 1; + } if (u.origin == r.id) { if (r.pending.items.len == 0 or r.pending.items[0].seq != u.seq) return r.fail("the authority confirmed an input this replica did not send next"); if (r.blocked == u.seq) r.blocked = null; @@ -975,6 +1011,11 @@ pub const Replica = struct { r.keepSince(u.tape); } + /// Its digest is due a comparison (`check_every`; every unit traced). + fn checkDue(r: *const Replica) bool { + return r.since_check >= check_every or std.c.getenv("PARDES_REPLICA_TRACE") != null; + } + fn fail(r: *Replica, why: []const u8) void { if (r.diverged == null) r.diverged = r.gpa.dupe(u8, why) catch return; } @@ -2963,9 +3004,10 @@ pub const Link = struct { /// The last frame was held for the frontend's answer (a terminal's /// FrameAck, a window's skipped image). owed: bool = false, - /// PARDES_REPLICA_CHECK=<file>: once quiet a while after a change, the - /// session's digest is asked, and whether this replica's agrees is - /// written there, a line a check. + /// Once quiet a while after a change, the session's digest is asked + /// (`quietCheck`): another than this replica's is a divergence, and it + /// joins again. PARDES_REPLICA_CHECK=<file>: whether they agree is + /// written there too, a line a check. check: ?[:0]const u8 = null, changed: bool = false, asked: bool = false, @@ -3118,7 +3160,7 @@ pub const Link = struct { /// window's test feed, sends a unit a frame; the check's own wakes it /// once). The wake it wants, ms. pub fn quietCheck(c: *Link) ?u32 { - if (c.check == null or !c.changed or c.asked or !c.r.quiet()) return null; + if (!c.changed or c.asked or !c.r.quiet()) return null; const quiet_ns = 300 * std.time.ns_per_ms; const since = nowNs() -| @max(c.input_ns, c.asked_ns); if (since < quiet_ns) return @intCast((quiet_ns - since) / std.time.ns_per_ms + 1); @@ -3129,12 +3171,20 @@ pub const Link = struct { return null; } - /// The session's digest against this replica's, if it is quiet still. + /// The session's digest against this replica's, if it is quiet still: + /// another is a divergence (the replica joins again). fn checked(c: *Link, theirs: u64) void { c.asked = false; - const path = c.check orelse return; - if (!c.r.quiet() or c.put_off) return; + if (!c.r.quiet() or c.r.diverged != null) return; const mine = c.r.digest(); + c.r.stats.checks += 1; + c.r.since_check = 0; + if (mine != theirs) { + c.r.fail("its digest, quiet, is not the session's"); + c.quit = true; + } + const path = c.check orelse return; + if (c.put_off) return; var buf: [96]u8 = undefined; const line = (if (mine == theirs) std.fmt.bufPrint(&buf, "agree {x:0>16}\n", .{mine}) else std.fmt.bufPrint(&buf, "differ {x:0>16} {x:0>16}\n", .{ mine, theirs })) catch return; appendTo(path, line); @@ -4076,6 +4126,43 @@ test "a replica joined from the session's dump replays the units after it" { try std.testing.expectEqual(digestOf(a), r.digest()); } +test "a replica joined from a dump replays a step that opens a file's pane and says it has no room for it: its /log, empty, is not the session's" { + const gpa = std.testing.allocator; + // No rows for a second pane: the Look's pane of a kind is made, the + // placement says it has no room (a message in the step that made the + // pane, which was not in /log yet), and the pane goes. + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 5, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + var words: [64]pardes.Kind.LoadedWord = undefined; + const zon = pardes.Kind.loadedOf(&Zon.ops, "/x/zon.so", @splat(9), &words); + _ = try a.local(.{ .addon = .{ .op = .load, .pane = 0, .kind = &zon } }); + const r = try joinedNow(a, 1, 0); + defer r.deinit(); + try std.testing.expectEqual(digestOf(a), r.digest()); + var whole: [libc.PATH_MAX]u8 = undefined; + var look: [libc.PATH_MAX + 8]u8 = undefined; + const path = std.mem.span(libc.realpath("build.zig.zon", &whole) orelse return error.SkipZigTest); + r.confirm(try a.local(.{ .command = try std.fmt.bufPrint(&look, "Look {s}", .{path}) })); + if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why}); + try std.testing.expect(r.diverged == null); + try std.testing.expectEqual(digestOf(a), r.digest()); +} + +/// A kind of `.zon` files, for a replica's Look of one (a picture's, +/// as a kind's pane is made). +const Zon = struct { + const pa = pardes.Kind.c; + fn open(_: [*c]const u8, _: [*c]const u8, _: u64) callconv(.c) ?*anyopaque { + return pardes.Kind.api.alloc.?(4); + } + fn paint(_: ?*anyopaque, _: [*c]const pa.pa_window, _: [*c]pa.pa_paint) callconv(.c) void {} + const ops = std.mem.zeroInit(pardes.Kind.Ops, .{ + .head = std.mem.zeroInit(pa.pa_head, .{ .size = @sizeOf(pa.pa_head), .abi = pardes.Kind.abi, .name = "zon".* ++ @as([13]u8, @splat(0)), .version = 1, .exts = ".zon" }), + .open = open, + .paint = paint, + }); +}; + test "a replica joined from a themed dump paints its taglines in the restored theme" { const gpa = std.testing.allocator; const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "build.zig" }, TestHost.host(), false); diff --git a/src/tape.zig b/src/tape.zig index 2875b156..9337ff9f 100644 --- a/src/tape.zig +++ b/src/tape.zig @@ -286,6 +286,9 @@ pub const Tape = struct { /// A digest only when asked (`recordDigest`): the replica stream's, /// one a unit, not one after each of its inputs. unit_digests: bool = false, + /// A digest record replayed is compared with the core's own; a replica + /// client's compares one now and then (src/replica.zig `check_every`). + checks: bool = true, /// A replica client's guess of the session's clocks for a speculation /// (src/replica.zig): its clock reads (a message's stamp, a file's /// mtime) answer this instead of the clocks here. @@ -570,6 +573,7 @@ pub const Tape = struct { /// A digest record's payload (a unit's last) against the core's own: a /// difference is `diverged`, its fields named when the record has them. pub fn check(t: *Tape, payload: []const u8) void { + if (!t.checks) return; var r: walk.Reader = .{ .bytes = payload }; const want = walk.decode([]const u64, &r, t.arena.allocator()) catch return; const p = t.core orelse return; |
