summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig310
1 files changed, 310 insertions, 0 deletions
diff --git a/src/replica.zig b/src/replica.zig
index 9d12de70..32fd4fbe 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -910,6 +910,16 @@ pub const Replica = struct {
pardes.views.drawOne(r.core(), r.view, present) catch {};
}
+ /// `f(ctx, core)` with its own view live, outside any step, as `draw`
+ /// draws it (views.within): what its frontend reads of its own view
+ /// (its pointer's cells, its size) while another client's input was the
+ /// session's last.
+ pub fn within(r: *Replica, ctx: anytype, comptime f: fn (@TypeOf(ctx), *Pardes) anyerror!void) void {
+ rollback.Epoch.select(&r.epoch);
+ defer rollback.Epoch.select(null);
+ pardes.views.within(r.core(), r.view, ctx, f) catch {};
+ }
+
/// One of the authority's terminal messages (`Screens.flush`, or an
/// answer to `screenRequests`), taken between units in stream order: a
/// delta written into its pane's emulator, or fetched rows. The
@@ -2486,6 +2496,306 @@ pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, compti
rest.run(gpa, fd, ctx, take);
}
+/// What the session streams a client after its dump, as its frontend's loop
+/// takes it: a unit's and a terminal message's bytes and a refusal's words
+/// on the C heap, the loop's to free.
+pub const Said = union(enum) { unit: []u8, screen: []u8, checked: u64, refused: []u8 };
+
+/// The session's stream off `fd`, each message handed to `post` until the
+/// link ends (a refusal ends it too); `post` says whether it took it (one
+/// it did not is freed here).
+pub fn readLink(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime post: fn (@TypeOf(ctx), Said) bool) void {
+ const Take = struct {
+ fn take(x: @TypeOf(ctx), tag: u8, payload: []const u8) bool {
+ const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false;
+ const msg: Said = switch (t) {
+ .unit => .{ .unit = std.heap.c_allocator.dupe(u8, payload) catch return false },
+ .screen => .{ .screen = std.heap.c_allocator.dupe(u8, payload) catch return false },
+ .check => .{ .checked = (wire.decodeServer(tag, payload) catch return false).check },
+ .refused => .{ .refused = std.heap.c_allocator.dupe(u8, (wire.decodeServer(tag, payload) catch return false).refused) catch return false },
+ .pong, .welcome, .dump, .blobs, .zstd => return true,
+ };
+ if (!post(x, msg)) switch (msg) {
+ .unit, .screen, .refused => |b| std.heap.c_allocator.free(b),
+ .checked => {},
+ };
+ return msg != .refused;
+ }
+ };
+ rest.run(gpa, fd, ctx, Take.take);
+}
+
+/// A replica client's link to its session, the terminal's (tty.zig
+/// `remote`) and the window's (gui.zig `remote`): its replica, its socket,
+/// what the session said, and what it checks. Drawing is the frontend's.
+pub const Link = struct {
+ r: *Replica,
+ fd: c_int,
+ quit: bool = false,
+ /// The session hung up.
+ gone: bool = false,
+ /// Something to draw: an input applied, a unit taken.
+ dirty: bool = true,
+ /// 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.
+ check: ?[:0]const u8 = null,
+ changed: bool = false,
+ asked: bool = false,
+ /// When its own last input was, and its last check asked (`quietCheck`
+ /// waits a while after each).
+ input_ns: u64 = 0,
+ asked_ns: u64 = 0,
+ /// How many of its own inputs the session recorded otherwise when it
+ /// last drew: a frame after one that draws something is a rollback that
+ /// may have been seen (counted so: an upper bound, the frame shows the
+ /// inputs after it too).
+ mispredicted: u32 = 0,
+ visible: u32 = 0,
+ /// What the session said as it let this client go (the C heap's).
+ refused: ?[]u8 = null,
+ /// A line over its frame, until its next key (`notify`): why it joined
+ /// again. The client's own, never the session's core.
+ notice_buf: [240]u8 = undefined,
+ notice_len: usize = 0,
+ /// PARDES_TEST_DIVERGE=<n>: once quiet after its n-th unit, its core
+ /// is put off the session's (a digested counter moved), the next unit's
+ /// digest finds it, and it joins again (test/daemon.py).
+ test_diverge: u32 = 0,
+ /// Put off so in this join: its checks are not written (they differ).
+ put_off: bool = false,
+ /// How its last join went (Joined's counts), and how long its dial and
+ /// load took (PARDES_REPLICA_STATS).
+ join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{},
+ join_ms: u64 = 0,
+
+ pub const upgrade_words = "session upgraded to a newer pardes: upgrade this client too";
+
+ /// The replica the dial's dump makes, checked against the session as
+ /// it was dumped; null, and why in `why`. The dump is let go, a far
+ /// join's to the thread that keeps the blobs it was sent (`Have.store`).
+ pub fn load(gpa: Allocator, joined: *Joined, o: pardes.Options, why: []u8, have: ?*Have) ?*Replica {
+ defer {
+ if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump);
+ joined.dump = &.{};
+ }
+ const r = Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
+ _ = std.fmt.bufPrint(why, "the session's dump would not load: {t}\x00", .{err}) catch {};
+ return null;
+ };
+ // PARDES_NO_ECHO: no predicted echo (the gates' baseline).
+ if (std.c.getenv("PARDES_NO_ECHO") != null) r.echo.on = false;
+ if (!r.loaded(joined.digest)) {
+ r.deinit();
+ _ = std.fmt.bufPrint(why, "the session's dump loaded as another session (a build's dump is not complete)\x00", .{}) catch {};
+ return null;
+ }
+ return r;
+ }
+
+ /// The test knobs the environment sets (PARDES_REPLICA_CHECK,
+ /// PARDES_TEST_DIVERGE).
+ pub fn fromEnv(c: *Link, gpa: Allocator, env: *const std.process.Environ.Map) !void {
+ if (env.get("PARDES_REPLICA_CHECK")) |file| c.check = try gpa.dupeSentinel(u8, file, 0);
+ if (env.get("PARDES_TEST_DIVERGE")) |n| c.test_diverge = std.fmt.parseInt(u32, n, 10) catch 0;
+ }
+
+ pub fn deinit(c: *Link, gpa: Allocator) void {
+ if (c.check) |file| gpa.free(file);
+ if (c.refused) |words| std.heap.c_allocator.free(words);
+ }
+
+ /// How its join went, its dial and load `ms`.
+ pub fn joinedAs(c: *Link, j: *const Joined, ms: u64) void {
+ c.join = .{ .blobs = j.blobs, .from_memory = j.from_memory, .from_disk = j.from_disk, .sent_bytes = j.sent_bytes };
+ c.join_ms = ms;
+ }
+
+ /// A join's link, from its start.
+ pub fn reset(c: *Link, r: *Replica, fd: c_int) void {
+ c.r = r;
+ c.fd = fd;
+ c.quit = false;
+ c.gone = false;
+ c.dirty = true;
+ c.owed = false;
+ c.changed = false;
+ c.asked = false;
+ c.mispredicted = 0;
+ c.put_off = false;
+ }
+
+ pub fn notify(c: *Link, comptime fmt: []const u8, args: anytype) void {
+ const text = std.fmt.bufPrint(&c.notice_buf, fmt, args) catch c.notice_buf[0..];
+ for (text) |*b| if (b.* < 0x20 or b.* > 0x7e) {
+ b.* = '?';
+ };
+ c.notice_len = text.len;
+ }
+
+ pub fn notice(c: *const Link) []const u8 {
+ return c.notice_buf[0..c.notice_len];
+ }
+
+ /// The frontend's input: applied at once, and sent to the session.
+ pub fn input(c: *Link, ev: pardes.Event) void {
+ // Its notice read: a key of its own puts it away.
+ if (c.notice_len > 0 and (ev == .key or ev == .paste)) c.notice_len = 0;
+ c.input_ns = nowNs();
+ const msg = c.r.input(ev) catch return;
+ if (!sendInput(c.fd, msg)) c.quit = true;
+ c.dirty = true;
+ }
+
+ /// What the session streamed, in its order; the bytes are let go.
+ pub fn take(c: *Link, msg: Said) void {
+ switch (msg) {
+ .unit => |bytes| {
+ defer std.heap.c_allocator.free(bytes);
+ c.r.confirm(bytes);
+ if (c.test_diverge != 0 and c.r.stats.units >= c.test_diverge and c.r.quiet()) {
+ c.test_diverge = 0;
+ c.put_off = true;
+ c.r.core().next_serial +%= 1000;
+ }
+ if (c.r.moved) c.dirty = true;
+ c.r.moved = false;
+ c.changed = true;
+ },
+ .screen => |bytes| {
+ defer std.heap.c_allocator.free(bytes);
+ if (c.r.screen(bytes)) |reply| if (!sendScreen(c.fd, reply)) {
+ c.quit = true;
+ };
+ c.dirty = true;
+ },
+ .checked => |d| c.checked(d),
+ .refused => |words| {
+ if (c.refused) |old| std.heap.c_allocator.free(old);
+ c.refused = words;
+ c.quit = true;
+ },
+ }
+ if (c.r.diverged != null) c.quit = true;
+ }
+
+ /// Its own view drawn, by the frontend (`drew` is whether anything was
+ /// written to it): a rollback it may have shown counted, and the
+ /// history rows it shows and does not hold (a scroll back) asked for,
+ /// blank until they come.
+ pub fn drawn(c: *Link, drew: bool) void {
+ if (c.r.stats.mispredicted > c.mispredicted) {
+ c.mispredicted = c.r.stats.mispredicted;
+ if (drew) c.visible += 1;
+ }
+ c.dirty = false;
+ while (c.r.screenRequest()) |req| if (!sendScreen(c.fd, req)) {
+ c.quit = true;
+ break;
+ };
+ }
+
+ /// Each pass of the frontend's loop: quiet (nothing of its own
+ /// pending) 300 ms after its last input and its last check, and
+ /// changed since, the session's digest is asked once, answered in
+ /// stream order after the units before it (a session that pumps on, a
+ /// 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;
+ 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);
+ c.changed = false;
+ c.asked = askCheck(c.fd);
+ c.asked_ns = nowNs();
+ return null;
+ }
+
+ /// The session's digest against this replica's, if it is quiet still.
+ 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;
+ const mine = c.r.digest();
+ 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);
+ }
+
+ /// PARDES_REPLICA_STATS=<file>: how its inputs and the session's units
+ /// 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;
+ 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", .{
+ 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;
+ var z: [4096]u8 = undefined;
+ appendTo(std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return, line);
+ }
+
+ /// The link ended: whether the client joins again, and why in its
+ /// notice (it diverged, or the session let it go to upgrade).
+ pub fn rejoins(c: *Link) bool {
+ if (c.r.diverged) |reason| {
+ c.notify("joined again: this client diverged from the session ({s})", .{reason});
+ return true;
+ }
+ if (c.refused) |words| {
+ c.notify("joined again: {s}", .{words});
+ std.heap.c_allocator.free(words);
+ c.refused = null;
+ return true;
+ }
+ return false;
+ }
+
+ /// The session again, after a divergence or an upgrade: an upgrade's
+ /// takes the time its exec and load take, and its daemon's, so it is
+ /// dialled every 100 ms for 20 s. A far client's (`have`) holds the
+ /// core it leaves meanwhile (`Have.leaving`): its blobs are not sent
+ /// again. Null, and why in `why`.
+ pub fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, have: ?*Have) ?Joined {
+ var tries: u32 = 0;
+ while (true) : (tries += 1) {
+ why.* = @splat(0);
+ if (dial(gpa, path, session, why, have)) |j| {
+ if (j.semantics == wire.semantics) return j;
+ bye(j.fd);
+ _ = libc.close(j.fd);
+ var jj = j;
+ jj.rest.deinit(gpa);
+ gpa.free(j.dump);
+ _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
+ return null;
+ }
+ if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) {
+ _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {};
+ return null;
+ }
+ if (tries > 200) return null;
+ const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
+ _ = libc.nanosleep(&ts, null);
+ }
+ }
+};
+
+fn appendTo(path: [:0]const u8, line: []const u8) void {
+ const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .APPEND = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
+ if (fd < 0) return;
+ _ = libc.write(fd, line.ptr, line.len);
+ _ = libc.close(fd);
+}
+
pub fn digestOf(a: *Authority) u64 {
const was = Stepping.begin(&a.t, null);
defer was.end();