summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-29 20:56:03 -0300
committerGabriel Schneider <[email protected]>2026-10-30 03:13:15 -0300
commitfdf74e199de7d0fc648f912875323f3dc920ffc2 (patch)
tree7e175c6fc9cb653200ff2abb7bcc5cf0fda3dc9f
parent57aa8d2f9ae851fa9806abbd1fd04abf1fd2b57f (diff)
downloadpardes-fdf74e199de7d0fc648f912875323f3dc920ffc2.tar.gz
pardes-fdf74e199de7d0fc648f912875323f3dc920ffc2.zip
replica clients over a socket: the session streams each its dump and every unit after it (replica.zig `Peers`, a writer and a reader thread a client, units at each step boundary, a client's input stepped there in its own view), and `pardes --attach=<socket>:<name>` and `--attach=<host>:<name>` (through `ssh -tt -L`) are a client in the tty host, its keys applied at once by its own replica and drawn by its own process
wire.zig version 15: welcome (the client's id and view, the dump's length, the session's UTC offset and digest), dump pieces, units (with the authority's clocks), terminal messages both ways, a check. A unit carries one digest, at its end (`unit_digests`). The client: tty.zig `Shell.remote`, `feed` (a terminal's input to the replica and the session); the session's clock guessed from the last unit's (tape.zig `Guess`); a pump woken only by a client's post reads no new time (`mail_only`), and a unit of another's ending where the last ended leaves the speculation as it is (`neutral`); the rollback copy taken while idle (`Replica.ready`); views a frontend here does not draw left undrawn. A lagging client never holds the session: its queue is cut off past 64 MiB (PARDES_REPLICA_LAG_KB). Detach in a client ends that client; a Restore lets every client go. daemon.zig: a replica the daemon hands over is the session's loop's (a note), not a stub thread's. A window session refuses one in words. test/daemon.py `--clients`: two clients (one through the delay relay) type into a detached session and agree with it, one killed outright, one detached, a third typing into a shell. Co-Authored-By: Claude Opus 5.5 <[email protected]>
-rw-r--r--src/daemon.zig61
-rw-r--r--src/detached/wire.zig94
-rw-r--r--src/gui/gui.zig3
-rw-r--r--src/main.zig101
-rw-r--r--src/replica.zig883
-rw-r--r--src/rollback.zig23
-rw-r--r--src/tape.zig15
-rw-r--r--src/tty/tty.zig425
-rw-r--r--test/daemon.py153
-rw-r--r--test/rollback_bench.zig18
10 files changed, 1659 insertions, 117 deletions
diff --git a/src/daemon.zig b/src/daemon.zig
index 785f4afd..d2ae012c 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -357,19 +357,10 @@ pub fn adopt() void {
}
/// This session is about to exec another build (upgrade.zig): the daemon
-/// is asked to move too, and every replica told why it is let go.
-/// ponytail: a pong its thread writes at the same moment may split the
-/// words; the client reconnects either way.
+/// is asked to move too. Its replica clients were told why they are let go
+/// (replica.zig `Peers.refuseAll`).
pub fn upgraded() void {
tell("upgrade", &.{});
- var out: [600]u8 = undefined;
- const said = wire.encodeServer(&out, .{ .refused = "the session is upgrading to another build of pardes: connect again with that build" }) catch return;
- for (&replica_fds) |*slot| {
- const fd = slot.load(.acquire);
- if (fd < 0) continue;
- _ = writeAll(fd, said);
- _ = libc.shutdown(fd, 2);
- }
}
/// An upgraded session's link and stub as its old process left them: its
@@ -388,6 +379,8 @@ pub const Note = union(enum) {
/// stub's pipe; for a window session, both -1: open a window.
attach: struct { tty: c_int, stub: c_int },
adopted: struct { pids: [max_fds]libc.pid_t, fds: [max_fds]c_int, n: usize },
+ /// A replica's connection, its hello taken (src/replica.zig `Peers`).
+ replica: c_int,
};
/// Hands every note the daemon sends on to `deliver`, on a thread of its
@@ -416,9 +409,7 @@ fn readNotes(fd: c_int, ctx: *anyopaque, deliver: *const fn (ctx: *anyopaque, no
} });
for (got.fds[@min(got.nfds, 2)..got.nfds]) |f| _ = libc.close(f);
} else if (std.mem.eql(u8, verb, "replica")) {
- if (got.nfds >= 1) {
- if (std.Thread.spawn(.{}, serveReplica, .{got.fds[0]})) |t| t.detach() else |_| _ = libc.close(got.fds[0]);
- }
+ if (got.nfds >= 1) deliver(ctx, .{ .replica = got.fds[0] });
for (got.fds[@min(got.nfds, 1)..got.nfds]) |f| _ = libc.close(f);
} else if (std.mem.eql(u8, verb, "adopted")) {
var note: Note = .{ .adopted = .{ .pids = undefined, .fds = undefined, .n = 0 } };
@@ -450,43 +441,11 @@ fn writeAll(fd: c_int, bytes: []const u8) bool {
return true;
}
-var replica_fds: [max_replicas]std.atomic.Value(c_int) = @splat(.init(-1));
-
-/// A replica the daemon handed over, its hello accepted: welcomed, its pings
-/// answered, until it says bye or hangs up. ponytail: phase 5 proper makes it
-/// a replica (the log, the dump, terminal state); this stub is what times a
-/// round trip through the daemon's handoff and `ssh -L`. A message larger
-/// than its buffer ends it.
-fn serveReplica(fd: c_int) void {
- defer _ = libc.close(fd);
- // Where an upgrade finds it to say why it ends (`upgraded`).
- const slot = for (&replica_fds) |*r| {
- if (r.cmpxchgStrong(-1, fd, .acq_rel, .acquire) == null) break r;
- } else null;
- defer if (slot) |r| r.store(-1, .release);
- log.info("a replica connected to '{s}'", .{name()});
- defer log.info("a replica of '{s}' left", .{name()});
- var out: [wire.hello_max]u8 = undefined;
- const pid: u32 = @intCast(libc.getpid());
- if (!writeAll(fd, wire.encodeServer(&out, .{ .welcome = .{ .pid = pid, .session = name() } }) catch return)) return;
- var buf: [msg_max]u8 = undefined;
- var len: usize = 0;
- while (true) {
- while (wire.framed(buf[0..len]) catch return) |f| {
- switch (wire.decodeClient(f.tag, f.payload) catch return) {
- .ping => |v| if (!writeAll(fd, wire.encodeServer(&out, .{ .pong = v }) catch return)) return,
- .bye, .hello => return,
- .event => {},
- }
- std.mem.copyForwards(u8, &buf, buf[f.total..len]);
- len -= f.total;
- }
- if (len == buf.len) return;
- const n = libc.read(fd, buf[len..].ptr, buf.len - len);
- if (n < 0 and libc.errno(n) == .INTR) continue;
- if (n <= 0) return;
- len += @intCast(n);
- }
+/// A replica refused by a session that takes none (a window's, so far).
+pub fn refuseReplica(fd: c_int, why: []const u8) void {
+ var out: [600]u8 = undefined;
+ if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {}
+ _ = libc.close(fd);
}
/// A session ended by a signal (TERM, HUP, INT): its shells go with it, as
diff --git a/src/detached/wire.zig b/src/detached/wire.zig
index 097cbee7..347d45d1 100644
--- a/src/detached/wire.zig
+++ b/src/detached/wire.zig
@@ -9,9 +9,11 @@ const builtin = @import("builtin");
const pardes = @import("../pardes.zig");
const config = @import("pardes_config");
-/// 14: the hello names a session and carries the semantics hash, and a
-/// ping is answered (phase 5's groundwork); 13: the frames went (phase 3).
-pub const version: u16 = 14;
+/// 15: the session streams a replica its dump and units, and terminal
+/// deltas both ways (phase 5 part B); 14: the hello names a session and
+/// carries the semantics hash, and a ping is answered (phase 5's
+/// groundwork); 13: the frames went (phase 3).
+pub const version: u16 = 15;
/// What makes two cores step differently, hashed: a replica's hello carries
/// its own, and one of another build is refused. The protocol, the release
@@ -52,6 +54,12 @@ pub const ClientTag = enum(u8) {
hello = 0x01,
bye = 0x02,
ping = 0x03,
+ /// A replica's word about a terminal's state (src/screen_state.zig): the
+ /// delta it holds, or rows it asks for. Opaque here.
+ screen_ack = 0x04,
+ /// What is the session's digest, in stream order (a replica checks
+ /// itself against it once quiet).
+ check = 0x05,
key = 0x10,
mouse = 0x11,
@@ -109,14 +117,65 @@ pub const ServerTag = enum(u8) {
refused = 0x02,
/// A ping's value back.
pong = 0x03,
+ /// A piece of the join's dump (src/dump.zig): the welcome says how long
+ /// it is whole.
+ dump = 0x04,
+ /// A unit of the authority's stream (src/replica.zig): its origin, seq,
+ /// clocks and tape.
+ unit = 0x05,
+ /// A terminal's delta, or rows asked for (src/screen_state.zig), applied
+ /// before the next unit. Opaque here.
+ screen = 0x06,
+ /// The session's digest (u64le), after every unit before it.
+ check = 0x07,
};
pub const ServerMsg = union(ServerTag) {
- welcome: struct { pid: u32, session: []const u8 },
+ /// Who the client is now (its id and view in the session), how long the
+ /// dump that follows is, and the session's offset from UTC (a replica
+ /// guesses the session's time of day).
+ welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0 },
refused: []const u8,
pong: u64,
+ dump: []const u8,
+ unit: []const u8,
+ screen: []const u8,
+ check: u64,
};
+/// A message's header, for a payload sent on its own (a unit, a dump's
+/// piece): no copy of it into a message buffer.
+pub fn header(tag: u8, len: usize) [header_len]u8 {
+ var h: [header_len]u8 = undefined;
+ h[0] = tag;
+ std.mem.writeInt(u32, h[1..5], @intCast(len), .little);
+ return h;
+}
+
+/// The other way: what a client is told, its slices borrowing `payload`.
+pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg {
+ var r: std.Io.Reader = .fixed(payload);
+ const msg: ServerMsg = switch (std.enums.fromInt(ServerTag, tag) orelse return error.BadTag) {
+ .welcome => .{ .welcome = .{
+ .pid = try r.takeInt(u32, .little),
+ .session = try r.take(try r.takeInt(u16, .little)),
+ .client = try r.takeInt(u16, .little),
+ .view = try r.takeByte(),
+ .dump_len = try r.takeInt(u64, .little),
+ .gmtoff = try r.takeInt(i32, .little),
+ .digest = try r.takeInt(u64, .little),
+ } },
+ .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) },
+ .pong => .{ .pong = try r.takeInt(u64, .little) },
+ .dump => return .{ .dump = payload },
+ .unit => return .{ .unit = payload },
+ .screen => return .{ .screen = payload },
+ .check => .{ .check = try r.takeInt(u64, .little) },
+ };
+ try end(&r);
+ return msg;
+}
+
pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 {
var w: std.Io.Writer = .fixed(out);
const at = try beginMessage(&w, @backingInt(std.meta.activeTag(msg)));
@@ -124,9 +183,16 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 {
.welcome => |h| {
try w.writeInt(u32, h.pid, .little);
try putSlice16(&w, h.session);
+ try w.writeInt(u16, h.client, .little);
+ try w.writeByte(h.view);
+ try w.writeInt(u64, h.dump_len, .little);
+ try w.writeInt(i32, h.gmtoff, .little);
+ try w.writeInt(u64, h.digest, .little);
},
.refused => |words| try putSlice16(&w, words),
.pong => |v| try w.writeInt(u64, v, .little),
+ .dump, .unit, .screen => |bytes| try w.writeAll(bytes),
+ .check => |v| try w.writeInt(u64, v, .little),
}
try finishMessage(&w, at);
return w.buffered();
@@ -157,6 +223,8 @@ pub const ClientMsg = union(enum) {
bye,
/// A round trip's probe, answered `pong` with the same value.
ping: u64,
+ screen_ack: []const u8,
+ check,
event: pardes.Event,
};
@@ -366,6 +434,8 @@ pub fn encodeClient(out: []u8, msg: ClientMsg) Error![]const u8 {
},
.bye => {},
.ping => |v| try w.writeInt(u64, v, .little),
+ .screen_ack => |bytes| try w.writeAll(bytes),
+ .check => {},
.event => |ev| switch (ev) {
.key => |k| {
try w.writeInt(u32, k.cp, .little);
@@ -432,6 +502,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg {
} },
.bye => .bye,
.ping => .{ .ping = try r.takeInt(u64, .little) },
+ .screen_ack => return .{ .screen_ack = payload },
+ .check => .check,
.key => blk: {
const cp = try r.takeInt(u32, .little);
// `Key.cp` is a u21, and the specials live in the private-use
@@ -492,7 +564,8 @@ const msg_slack = header_len + 64;
pub fn clientBound(msg: ClientMsg) usize {
return msg_slack + switch (msg) {
.hello => |h| h.session.len,
- .bye, .ping => 0,
+ .bye, .ping, .check => 0,
+ .screen_ack => |bytes| bytes.len,
.event => |ev| switch (ev) {
.mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0,
.key => |k| k.text.len,
@@ -547,7 +620,10 @@ test "detached wire: every Event variant round-trips" {
const bytes = try encodeServer(&buf, .{ .welcome = .{ .pid = 42, .session = "work" } });
const f = (try framed(bytes)).?;
try testing.expectEqual(@as(u8, 0x01), f.tag);
- try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k' }, f.payload);
+ try testing.expectEqualSlices(u8, &.{ 42, 0, 0, 0, 4, 0, 'w', 'o', 'r', 'k', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, f.payload);
+ const back = (try decodeServer(f.tag, f.payload)).welcome;
+ try testing.expectEqualStrings("work", back.session);
+ try testing.expectEqual(@as(u32, 42), back.pid);
try testing.expectEqual(@as(u8, 0x03), (try framed(try encodeServer(&buf, .{ .pong = 7 }))).?.tag);
}
@@ -622,8 +698,10 @@ test "detached wire: a session is never told to do a frontend's remembering" {
// them, and the machine that reports them is the one already holding the
// core.
const allowed = [_][]const u8{
- // Who this connection is, its round trip, and that it is finished.
- "hello", "bye", "ping",
+ // Who this connection is, its round trip, and that it is finished,
+ // and which terminal state its replica holds (a protocol's word,
+ // as a ping is).
+ "hello", "bye", "ping", "screen_ack", "check",
// ...and everything a person can do to a window.
"key", "mouse", "resize",
"paste", "command", "pdf_scroll",
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index e4d69281..a88a7335 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4884,6 +4884,9 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void {
for (a.pids[0..a.n], a.fds[0..a.n]) |pid, fd| q.push(.{ .adopted = .{ .pid = pid, .fd = fd } });
q.push(.{ .adopted = .{ .pid = 0, .fd = -1 } });
},
+ // ponytail: a window session serves no replica yet; a terminal's
+ // does (tty.zig).
+ .replica => |fd| daemon.refuseReplica(fd, "a window session takes no replica yet: run the session in a terminal, or --detach"),
}
}
diff --git a/src/main.zig b/src/main.zig
index a6144475..ab5047ce 100644
--- a/src/main.zig
+++ b/src/main.zig
@@ -108,6 +108,13 @@ const help_text =
\\ the session detaches or ends
\\ --attach=<name> ...the session called <name> (its --9p or
\\ --detach name, else its pid)
+ \\ --attach=<host>:<name> a client of session <name> on another machine,
+ \\ over `ssh -L` to its pardes-replica.sock
+ \\ ($PARDES_SSH: the ssh command, words; and
+ \\ $PARDES_REMOTE_RUNTIME: its runtime directory,
+ \\ if not this one's path). Its keys show at once
+ \\ --attach=<sock>:<name> ...through that socket (a path, with a '/'),
+ \\ one forwarded already, or this machine's
\\ --geometry=WxH[+X+Y] the window's size in points, and where it goes
\\ where the display lets a window say (X11)
\\ -h, --help show this help and exit
@@ -738,6 +745,10 @@ fn nativeMain(std_init: std.process.Init) !void {
opts.config_dir = found.dir;
opts.store_path = storePath(init.io, arena, init.environ_map);
if (found.dir) |d| @import("crash.zig").setDir(d);
+ if (attach) |name| if (std.mem.lastIndexOfScalar(u8, name, ':')) |colon| {
+ if (comptime pardes.platform != .tty) badArgs(init.io, "a window is no replica client yet: run `pardes --attach={s}` in a terminal", .{name});
+ remoteAttach(init, opts, name[0..colon], name[colon + 1 ..]);
+ };
if (attach) |name| if (!resuming) attachTo(init.io, name);
if (detach) |name| {
// Said plainly, never a Zig error trace: a name a live session's
@@ -815,6 +826,96 @@ fn nativeMain(std_init: std.process.Init) !void {
daemon.tellStubExit(0);
}
+extern "c" fn execvp(file: [*:0]const u8, argv: [*:null]const ?[*:0]const u8) c_int;
+
+/// `pardes --attach=<where>:<name>`: a replica client (tty.zig `remote`),
+/// through the socket `where` names, or through `ssh -L` of host `where`'s
+/// replica socket to one of this process's.
+fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, name: []const u8) noreturn {
+ const io = init.io;
+ if (comptime pardes.platform != .tty) unreachable;
+ const Say = struct {
+ fn no(o: std.Io, comptime fmt: []const u8, args: anytype) noreturn {
+ var buf: [700]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(o, std.fmt.bufPrint(&buf, "pardes: --attach: " ++ fmt ++ "\n", args) catch "pardes: --attach failed\n") catch {};
+ std.process.exit(1);
+ }
+ };
+ if (std.mem.indexOfScalar(u8, where, '/') != null) {
+ @import("tty/tty.zig").remote(init, opts, where, name) catch |err| Say.no(io, "{t}", .{err});
+ std.process.exit(0);
+ }
+ // ssh -L <here>:<there> <host>, until the socket here answers.
+ var here_buf: [ninep_io.sun_path_len:0]u8 = undefined;
+ const tmp = std.mem.span(std.c.getenv("TMPDIR") orelse "/tmp");
+ const here = std.fmt.bufPrintSentinel(&here_buf, "{s}/pardes-ssh-{d}.sock", .{ tmp, std.c.getpid() }, 0) catch Say.no(io, "TMPDIR is too long for a socket's path", .{});
+ var there_buf: [ninep_io.sun_path_len + 64:0]u8 = undefined;
+ var dir_buf: [ninep_io.sun_path_len:0]u8 = undefined;
+ const dir = if (std.c.getenv("PARDES_REMOTE_RUNTIME")) |d| std.mem.span(d) else ninep_io.socketDir(&dir_buf) orelse Say.no(io, "no runtime directory to name the remote socket by: set PARDES_REMOTE_RUNTIME", .{});
+ var forward_buf: [2 * ninep_io.sun_path_len + 64]u8 = undefined;
+ const there = std.fmt.bufPrintSentinel(&there_buf, "{s}/pardes-replica.sock", .{dir}, 0) catch unreachable;
+ const forward = std.fmt.bufPrintSentinel(&forward_buf, "{s}:{s}", .{ here, there }, 0) catch unreachable;
+ var host_buf: [256]u8 = undefined;
+ const host = std.fmt.bufPrintSentinel(&host_buf, "{s}", .{where}, 0) catch Say.no(io, "that host's name is too long", .{});
+ var argv: [48:null]?[*:0]const u8 = @splat(null);
+ var n: usize = 0;
+ var words_buf: [1024]u8 = undefined;
+ var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh"), ' ');
+ var at: usize = 0;
+ while (words.next()) |w| {
+ if (n >= 40 or at + w.len + 1 > words_buf.len) Say.no(io, "PARDES_SSH has too many words", .{});
+ @memcpy(words_buf[at..][0..w.len], w);
+ words_buf[at + w.len] = 0;
+ argv[n] = @ptrCast(&words_buf[at]);
+ n += 1;
+ at += w.len + 1;
+ }
+ // A terminal asked for and a command that waits, not -N: only an
+ // interactive connection gets TCP_NODELAY from ssh and sshd, and with
+ // Nagle each key in a burst waited for the last one's ACK, a round trip.
+ for ([_][*:0]const u8{ "-tt", "-q", "-o", "ExitOnForwardFailure=yes", "-L", forward.ptr, host.ptr, "exec sleep 2147483647" }) |a| {
+ argv[n] = a;
+ n += 1;
+ }
+ _ = std.c.unlink(here);
+ const pid = std.c.fork();
+ if (pid < 0) Say.no(io, "no fork for ssh", .{});
+ if (pid == 0) {
+ const null_fd = std.c.open("/dev/null", .{ .ACCMODE = .RDONLY }, @as(std.c.mode_t, 0));
+ if (null_fd >= 0) _ = std.c.dup2(null_fd, 0);
+ _ = execvp(argv[0].?, &argv);
+ std.c._exit(127);
+ }
+ const Ssh = struct {
+ var child: std.c.pid_t = 0;
+ var path: [:0]const u8 = "";
+ fn end() void {
+ _ = std.c.kill(child, .TERM);
+ _ = std.c.waitpid(child, null, 0);
+ _ = std.c.unlink(path);
+ }
+ };
+ Ssh.child = pid;
+ Ssh.path = here;
+ var waited: u32 = 0;
+ while (std.c.access(here, std.c.F_OK) != 0) : (waited += 1) {
+ var status: c_int = 0;
+ if (std.c.waitpid(pid, &status, std.c.W.NOHANG) == pid) Say.no(io, "ssh to {s} ended before its forward was up", .{where});
+ if (waited > 3000) {
+ Ssh.end();
+ Say.no(io, "ssh to {s} set up no forward in 30 s", .{where});
+ }
+ const ts: std.c.timespec = .{ .sec = 0, .nsec = 10 * std.time.ns_per_ms };
+ _ = std.c.nanosleep(&ts, null);
+ }
+ @import("tty/tty.zig").remote(init, opts, here, name) catch |err| {
+ Ssh.end();
+ Say.no(io, "{t}", .{err});
+ };
+ Ssh.end();
+ std.process.exit(0);
+}
+
/// `pardes --attach[=name]` in a terminal: this terminal handed (through
/// the daemon) to the detached session of that name, or the only one, and
/// this process its stub until the session leaves it. `pardes-gui
diff --git a/src/replica.zig b/src/replica.zig
index 032e50af..5c965b2b 100644
--- a/src/replica.zig
+++ b/src/replica.zig
@@ -44,11 +44,34 @@ fn header(buf: []u8, who: u16, seq: u32) void {
std.mem.writeInt(u32, buf[2..6], seq, .little);
}
-pub const Unit = struct { origin: u16, seq: u32, tape: []const u8 };
+/// A unit's head: its origin and seq, then the authority's clocks when it
+/// was made (realtime and monotonic, ns), which a replica's guesses of the
+/// session's clock start from.
+const unit_head = head_len + 16;
+
+pub const Unit = struct { origin: u16, seq: u32, real_ns: i64 = 0, mono_ns: i64 = 0, tape: []const u8 };
pub fn parseUnit(msg: []const u8) ?Unit {
- if (msg.len < head_len) return null;
- return .{ .origin = std.mem.readInt(u16, msg[0..2], .little), .seq = std.mem.readInt(u32, msg[2..6], .little), .tape = msg[head_len..] };
+ if (msg.len < unit_head) return null;
+ return .{
+ .origin = std.mem.readInt(u16, msg[0..2], .little),
+ .seq = std.mem.readInt(u32, msg[2..6], .little),
+ .real_ns = std.mem.readInt(i64, msg[6..14], .little),
+ .mono_ns = std.mem.readInt(i64, msg[14..22], .little),
+ .tape = msg[unit_head..],
+ };
+}
+
+fn clockNs(id: std.c.clockid_t) i64 {
+ var ts: std.c.timespec = undefined;
+ if (std.c.clock_gettime(id, &ts) != 0) return 0;
+ return @as(i64, ts.sec) * std.time.ns_per_s + ts.nsec;
+}
+
+fn unitHeader(buf: []u8, origin: u16, seq: u32) void {
+ header(buf, origin, seq);
+ std.mem.writeInt(i64, buf[6..14], clockNs(.REALTIME), .little);
+ std.mem.writeInt(i64, buf[14..22], clockNs(.MONOTONIC), .little);
}
pub const Input = struct { client: u16, seq: u32, event: pardes.Event };
@@ -87,6 +110,13 @@ fn nowNs() u64 {
return pardes.FrameStats.nowNs();
}
+/// The `p`th percentile of `samples` (sorted here); 100 is the worst.
+fn pctOf(samples: []u64, p: usize) u64 {
+ if (samples.len == 0) return 0;
+ std.mem.sort(u64, samples, {}, std.sort.asc(u64));
+ return samples[@min(samples.len - 1, samples.len * p / 100)];
+}
+
// ---- the authority ----
pub const Authority = struct {
@@ -142,8 +172,8 @@ pub const Authority = struct {
/// done straight on the core).
pub fn unit(a: *Authority, origin: u16, seq: u32) ![]const u8 {
a.out.clearRetainingCapacity();
- try a.out.appendNTimes(a.gpa, 0, head_len);
- header(a.out.items, origin, seq);
+ try a.out.appendNTimes(a.gpa, 0, unit_head);
+ unitHeader(a.out.items, origin, seq);
try a.out.appendSlice(a.gpa, tape.taken(&a.t));
tape.clearTaken(&a.t);
return a.out.items;
@@ -212,7 +242,7 @@ pub const Authority = struct {
const was = Stepping.begin(&a.t, null);
a.core.toView(a.view_of[in.client]);
a.core.update(in.event);
- a.core.pump(a.host) catch {};
+ a.core.step();
was.end();
return a.unit(in.client, in.seq);
}
@@ -437,7 +467,7 @@ pub const Stats = struct {
}
};
-const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null };
+const Pending = struct { seq: u32, msg: []u8, spec: ?[]u8 = null, sent_ns: u64 = 0 };
pub const Replica = struct {
gpa: Allocator,
@@ -464,6 +494,26 @@ pub const Replica = struct {
out: std.ArrayListUnmanaged(u8) = .empty,
/// Nanoseconds of the rollback being made, until its reapply.
recovering: ?u64 = null,
+ /// The digest the last confirmed unit ended on (its last record's
+ /// bytes): a unit of another's that ends on it again (the session's
+ /// pump that took this replica's input in, and moved nothing) leaves
+ /// the speculation on top as it was.
+ last_digest: std.ArrayListUnmanaged(u8) = .empty,
+ /// Units of another's taken so, no rollback made.
+ neutral: 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).
+ moved: bool = false,
+ /// A client of a session elsewhere (`initDump`): the session's clock is
+ /// guessed, never read here -- its last unit's reading, plus the time
+ /// since it came, plus a round trip (the input reaches the session that
+ /// much later). In process, both read one clock.
+ guess: bool = false,
+ stamp: struct { real_ns: i64 = 0, mono_ns: i64 = 0, at_ns: u64 = 0 } = .{},
+ rtt_ns: u64 = 0,
+ gmtoff: i32 = 0,
+
/// Its terminals' screen state, as the authority's deltas made it.
screens: [pardes.MAX_PANES]?ScreenOf = @splat(null),
screen_out: std.ArrayListUnmanaged(u8) = .empty,
@@ -497,6 +547,43 @@ pub const Replica = struct {
return r;
}
+ /// A replica of a session elsewhere, as its walker dump (src/dump.zig)
+ /// is: what a client of a socket joins with. `opts` are this process's
+ /// (a replica's own allocators and names); its core's allocator is the
+ /// rollback epoch's, but for images and PDFs, whose workers allocate on
+ /// threads of their own. ponytail: a picture's pixels freed while a copy
+ /// is held are not put off; a rollback over one shows an image a frame
+ /// late at worst... or reads freed pixels, which no test session has.
+ pub fn initDump(gpa: Allocator, id: u16, view: u8, bytes: []const u8, opts: pardes.Options) !*Replica {
+ const r = try gpa.create(Replica);
+ r.* = .{ .gpa = gpa, .id = id, .view = view, .t = tape.memory(gpa), .guess = true };
+ r.t.headless = true;
+ errdefer {
+ tape.release(&r.t);
+ r.epoch.deinit();
+ gpa.destroy(r);
+ }
+ const epoch = r.epoch.allocator();
+ r.t.core_gpa = epoch;
+ var o = opts;
+ o.frame_allocator = epoch;
+ o.tree_sitter_allocator = epoch;
+ o.image_allocator = o.image_allocator orelse std.heap.c_allocator;
+ o.pdf_allocator = o.pdf_allocator orelse std.heap.c_allocator;
+ // The session's store and Recent are the authority's.
+ o.store_path = null;
+ const c = try pardes.dump.load(epoch, o, bytes, null);
+ // What loading queued (a watch, a theme asked for) is the
+ // authority's errand, done there already: the session was dumped
+ // between steps, with nothing queued.
+ c.effects_len = 0;
+ c.effects_head = 0;
+ c.in_len = 0;
+ c.in_head = 0;
+ tape.adoptCore(&r.t, c);
+ return r;
+ }
+
pub fn deinit(r: *Replica) void {
for (&r.screens) |*slot| if (slot.*) |*sc| sc.rep.deinit(terminal.emulator_gpa);
if (r.t.core) |c| {
@@ -512,6 +599,7 @@ pub const Replica = struct {
r.since.deinit(r.gpa);
r.out.deinit(r.gpa);
r.screen_out.deinit(r.gpa);
+ r.last_digest.deinit(r.gpa);
r.stats.deinit(r.gpa);
tape.release(&r.t);
if (r.diverged) |why| r.gpa.free(why);
@@ -539,7 +627,7 @@ pub const Replica = struct {
const n = (try wire.encodeClient(room[head_len..], .{ .event = ev })).len;
header(room, r.id, seq);
const msg = try r.gpa.dupe(u8, room[0 .. head_len + n]);
- try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg });
+ try r.pending.append(r.gpa, .{ .seq = seq, .msg = msg, .sent_ns = nowNs() });
r.dirty = true;
r.settle();
if (r.t.screen_state and r.diverged == null) r.echo.typed(r.core(), r.view, ev, seq, r.applied == r.pending.items.len);
@@ -554,6 +642,21 @@ pub const Replica = struct {
const u = parseUnit(msg) orelse return r.fail("a unit too short to read");
r.stats.units += 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: the unit moves the
+ // state it was of.
+ if (r.applied == 0 and r.copy.held) {
+ r.copy.release();
+ rollback.Epoch.select(null);
+ r.clearSince();
+ }
+ if (r.guess) {
+ r.stamp = .{ .real_ns = u.real_ns, .mono_ns = u.mono_ns, .at_ns = nowNs() };
+ if (u.origin == r.id) for (r.pending.items) |p| if (p.seq == u.seq) {
+ // Sent to confirmed: what reaching the session takes, and back.
+ if (p.sent_ns != 0) r.rtt_ns = nowNs() -| p.sent_ns;
+ break;
+ };
+ }
const was = Stepping.begin(&r.t, &r.epoch);
defer was.end();
if (u.origin == r.id and r.pending.items.len > 0 and r.pending.items[0].seq == u.seq and r.applied > 0 and
@@ -579,6 +682,25 @@ pub const Replica = struct {
}
return;
}
+ const ends = lastDigest(u.tape);
+ defer if (ends) |d| {
+ r.last_digest.clearRetainingCapacity();
+ 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
+ // copy is next put back.
+ r.neutral += 1;
+ const kept = r.gpa.dupe(u8, u.tape) catch return r.fail("out of memory");
+ r.since.append(r.gpa, kept) catch return r.fail("out of memory");
+ if (r.since.items.len >= rebase_after) {
+ r.stats.rebases += 1;
+ r.toConfirmed();
+ r.dirty = true;
+ }
+ return;
+ }
if (r.applied > 0) {
if (u.origin == r.id) r.stats.mispredicted += 1 else r.stats.foreign += 1;
if (u.origin == r.id and std.c.getenv("PARDES_REPLICA_TRACE") != null) {
@@ -593,6 +715,7 @@ pub const Replica = struct {
r.recovering = nowNs();
r.toConfirmed();
}
+ r.moved = true;
if (tape.replayUnit(&r.t, u.tape)) |why| {
if (std.c.getenv("PARDES_REPLICA_TRACE") != null) {
var w: std.Io.Writer.Allocating = .init(r.gpa);
@@ -657,17 +780,10 @@ pub const Replica = struct {
/// pending[applied] applied on top: whether it was.
fn speculate(r: *Replica) bool {
const p = &r.pending.items[r.applied];
+ // Every kind, a view's own (a resize, the pointer) too: each client
+ // has its own view, and what reaches the authority's (a pty's size,
+ // the pointer over a sliding pane) stops it as any host call does.
const in = parseInput(p.msg) orelse return false;
- switch (in.event) {
- .key, .paste, .command => {},
- else => {
- // The view's (a resize, the pointer): the authority's for
- // now, phase 4's views to come.
- r.stats.unspeculated += 1;
- r.blocked = p.seq;
- return false;
- },
- }
const c = r.core();
if (!r.copy.held) {
const t0 = nowNs();
@@ -683,6 +799,10 @@ pub const Replica = struct {
if (checked) terminalDigests(c, &terminals);
const t0 = nowNs();
tape.beginSpeculation(&r.t);
+ r.t.guess = if (r.guess) guess: {
+ const since: i64 = @intCast(nowNs() -| r.stamp.at_ns + r.rtt_ns);
+ break :guess .{ .real_ns = r.stamp.real_ns + since, .mono_ns = r.stamp.mono_ns + since, .gmtoff = r.gmtoff };
+ } else null;
c.toView(r.view);
// Input into a terminal is the authority's: its step reads the
// emulator (rows, cursor, modes), which here is the last delta's and
@@ -690,7 +810,7 @@ pub const Replica = struct {
// echo.
if (r.t.screen_state) if (c.panes[c.active]) |pane| if (pane.isTerminal()) tape.poison();
c.update(in.event);
- c.pump(c.host) catch {};
+ c.step();
const clean = tape.endSpeculation(&r.t);
const dt = nowNs() - t0;
// Where a blob was written, should one be: the input that did.
@@ -699,6 +819,7 @@ 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.blocked = p.seq;
const upto = r.applied;
r.toConfirmed();
@@ -732,9 +853,41 @@ pub const Replica = struct {
}
/// Nothing pending and nothing applied: the replica is the authority's
- /// confirmed state, which `digest` says.
+ /// confirmed state, which `digest` says (a copy taken ahead, `ready`,
+ /// has nothing on top of it).
pub fn quiet(r: *const Replica) bool {
- return r.pending.items.len == 0 and r.applied == 0 and !r.copy.held;
+ return r.pending.items.len == 0 and r.applied == 0;
+ }
+
+ /// The copy taken now, while nothing is pending (the host is idle, its
+ /// frame drawn), so that the next input of its own is applied at once
+ /// without taking it: a copy is a megabyte saved. Let go when a unit
+ /// comes first.
+ pub fn ready(r: *Replica) void {
+ if (r.copy.held or r.pending.items.len > 0 or r.diverged != null) return;
+ const c = r.core();
+ const t0 = nowNs();
+ rollback.Epoch.select(&r.epoch);
+ defer rollback.Epoch.select(null);
+ r.copy.take(&r.epoch, c) catch return;
+ r.stats.copy_ns.append(r.gpa, nowNs() - t0) catch {};
+ r.stats.copy_bytes += r.copy.savedBytes();
+ r.stats.copies += 1;
+ }
+
+ /// Whether its core loaded from the dump is the session as it was
+ /// dumped, by the welcome's `digest`.
+ pub fn loaded(r: *Replica, want: u64) bool {
+ return tape.digestOf(r.core()) == want;
+ }
+
+ /// Its own view drawn and presented through `present`, between steps
+ /// (views.drawOne): nothing the digest covers moves. The epoch's trees
+ /// held while a copy is (a render can make or drop a syntax tree).
+ pub fn draw(r: *Replica, present: pardes.views.Present) void {
+ rollback.Epoch.select(&r.epoch);
+ defer rollback.Epoch.select(null);
+ pardes.views.drawOne(r.core(), r.view, present) catch {};
}
/// One of the authority's terminal messages (`Screens.flush`, or an
@@ -827,6 +980,20 @@ pub const Replica = struct {
}
};
+/// The bytes of a unit's last digest record: the state it ended on; null
+/// for one that says what the host wrote or which host it is (a replica's
+/// core takes those only by replaying the unit).
+fn lastDigest(bytes: []const u8) ?[]const u8 {
+ var r: walk.Reader = .{ .bytes = bytes };
+ var last: ?[]const u8 = null;
+ while (true) {
+ const head = r.take(5) catch return last;
+ const payload = r.take(std.mem.bytesToValue(u32, head[1..5])) catch return last;
+ if (head[0] == tape.digest_kind) last = payload;
+ if (head[0] == tape.set_kind) return null;
+ }
+}
+
fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void {
for (p.panes, out) |slot, *d| {
d.* = 0;
@@ -838,6 +1005,647 @@ fn terminalDigests(p: *const Pardes, out: *[pardes.MAX_PANES]u64) void {
}
}
+// ---- over a socket (docs/next-daemon.md, "Phase 5 part B notes") ----
+//
+// The session's half: each client handed over by the daemon (daemon.zig) is
+// a Peer, with a view of its own, a thread reading its inputs and one
+// writing what it is sent. The recorder is on while any is. At each step
+// boundary of the session's loop (`Peers.boundary`, after a pump):
+// - the pump's unit goes to every client;
+// - a client that came is joined: its view opened, the session dumped
+// (src/dump.zig) and sent ahead of every unit after;
+// - each input that came is a unit of its own: its view, the input, a
+// `step` (what the client's own core applied at once);
+// - a client gone takes its view with it.
+// A client's link only ever holds bytes queued for it: the session's step
+// never waits on one. One that lets `lag_max` pile up is cut off.
+
+const libc = std.c;
+const log = std.log.scoped(.replica);
+const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0;
+/// What a client may leave unread, past its join's dump, before it is cut
+/// off: minutes of typing, a screenful of a flood's deltas many times over.
+/// PARDES_REPLICA_LAG_KB says another (a test's).
+var lag_max: usize = 64 << 20;
+const dump_piece = 1 << 20;
+
+const Tm = extern struct { sec: c_int, min: c_int, hour: c_int, mday: c_int, mon: c_int, year: c_int, wday: c_int, yday: c_int, isdst: c_int, gmtoff: c_long, zone: ?[*:0]const u8 };
+extern "c" fn localtime_r(t: *const libc.time_t, tm: *Tm) ?*Tm;
+
+/// This zone's offset from UTC now, seconds.
+fn gmtoff() i32 {
+ var ts: libc.timespec = undefined;
+ _ = libc.clock_gettime(.REALTIME, &ts);
+ const secs: libc.time_t = ts.sec;
+ var tm: Tm = undefined;
+ return if (localtime_r(&secs, &tm) != null) @intCast(tm.gmtoff) else 0;
+}
+
+fn writeAll(fd: c_int, bytes: []const u8) bool {
+ var at: usize = 0;
+ while (at < bytes.len) {
+ const n = libc.send(fd, bytes[at..].ptr, bytes.len - at, nosignal);
+ if (n < 0 and libc.errno(n) == .INTR) continue;
+ if (n <= 0) return false;
+ at += @intCast(n);
+ }
+ return true;
+}
+
+fn readSome(fd: c_int, buf: []u8) usize {
+ while (true) {
+ const n = libc.read(fd, buf.ptr, buf.len);
+ if (n < 0 and libc.errno(n) == .INTR) continue;
+ return if (n <= 0) 0 else @intCast(n);
+ }
+}
+
+/// What a stream's reader holds of it, read and not yet taken: a dump's
+/// reader leaves the units read with its last piece to the stream's.
+pub const Framer = struct {
+ buf: std.ArrayListUnmanaged(u8) = .empty,
+ len: usize = 0,
+
+ pub fn deinit(f: *Framer, gpa: Allocator) void {
+ f.buf.deinit(gpa);
+ }
+
+ /// Framed messages off `fd` (detached/wire.zig) handed to `take` until
+ /// the stream ends, `take` says stop, or a message is malformed.
+ fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void {
+ while (true) {
+ var at: usize = 0;
+ var stop = false;
+ while (!stop) {
+ const f = (wire.framed(fr.buf.items[at..fr.len]) catch break) orelse break;
+ at += f.total;
+ stop = !take(ctx, f.tag, f.payload);
+ }
+ if (at > 0) {
+ std.mem.copyForwards(u8, fr.buf.items[0 .. fr.len - at], fr.buf.items[at..fr.len]);
+ fr.len -= at;
+ }
+ if (stop) return;
+ if (wire.framed(fr.buf.items[0..fr.len])) |_| {} else |_| return;
+ // Room for what the head says is coming, or 64 KiB more.
+ const want = if (fr.len >= wire.header_len) wire.header_len + @as(usize, std.mem.readInt(u32, fr.buf.items[1..5], .little)) else 0;
+ const room = @max(want, fr.len + (64 << 10));
+ if (fr.buf.items.len < room) fr.buf.resize(gpa, room) catch return;
+ const n = readSome(fd, fr.buf.items[fr.len..]);
+ if (n == 0) return;
+ fr.len += n;
+ }
+ }
+};
+
+fn readFramed(gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void {
+ var fr: Framer = .{};
+ defer fr.deinit(gpa);
+ fr.run(gpa, fd, ctx, take);
+}
+
+pub const Peer = struct {
+ peers: *Peers,
+ fd: c_int,
+ id: u16,
+ view: u8,
+ /// Inputs read (each one's seq, as the client numbered them).
+ seq: u32 = 0,
+ reader: ?std.Thread = null,
+ writer: ?std.Thread = null,
+ lock: std.Io.Mutex = .init,
+ cond: std.Io.Condition = .init,
+ /// What it is sent, not written yet, and what the writer writes now.
+ out: std.ArrayListUnmanaged(u8) = .empty,
+ sending: std.ArrayListUnmanaged(u8) = .empty,
+ limit: usize = 64 << 20,
+ closing: bool = false,
+ cut: bool = false,
+
+ /// `parts` queued for it, in order; never waits. A client whose queue
+ /// passes its limit is cut off: its link is shut down, and its reader
+ /// says it is gone.
+ fn send(peer: *Peer, parts: []const []const u8) void {
+ const io = peer.peers.io;
+ peer.lock.lockUncancelable(io);
+ defer peer.lock.unlock(io);
+ if (peer.cut or peer.closing) return;
+ var n: usize = peer.out.items.len;
+ for (parts) |p| n += p.len;
+ if (n > peer.limit) {
+ log.info("client {d} is {d} MB behind: cut off", .{ peer.id, n >> 20 });
+ peer.peers.cut += 1;
+ peer.cut = true;
+ _ = libc.shutdown(peer.fd, 2);
+ peer.cond.signal(io);
+ return;
+ }
+ for (parts) |p| peer.out.appendSlice(peer.peers.gpa, p) catch {
+ peer.cut = true;
+ _ = libc.shutdown(peer.fd, 2);
+ break;
+ };
+ peer.cond.signal(io);
+ }
+
+ fn writeLoop(peer: *Peer) void {
+ const io = peer.peers.io;
+ while (true) {
+ peer.lock.lockUncancelable(io);
+ while (peer.out.items.len == 0 and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock);
+ if (peer.out.items.len == 0 or peer.cut) {
+ peer.lock.unlock(io);
+ return;
+ }
+ std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &peer.sending);
+ // The limit was for the dump and what is queued behind it.
+ peer.limit = lag_max;
+ peer.lock.unlock(io);
+ const ok = writeAll(peer.fd, peer.sending.items);
+ peer.sending.clearRetainingCapacity();
+ if (!ok) {
+ _ = libc.shutdown(peer.fd, 2);
+ return;
+ }
+ }
+ }
+
+ fn take(peer: *Peer, tag: u8, payload: []const u8) bool {
+ const t = std.enums.fromInt(wire.ClientTag, tag) orelse return false;
+ switch (t) {
+ .hello, .bye => return false,
+ .ping => {
+ var out: [16]u8 = undefined;
+ const v = (wire.decodeClient(tag, payload) catch return false).ping;
+ const pong = wire.encodeServer(&out, .{ .pong = v }) catch return false;
+ peer.send(&.{pong});
+ return true;
+ },
+ // ponytail: rterm's acks (src/screen_state.zig) go to its
+ // session state once terminals cross as screens.
+ .screen_ack => return true,
+ .check => {
+ peer.peers.post(.{ .check = peer });
+ return true;
+ },
+ else => {},
+ }
+ peer.seq += 1;
+ const msg = peer.peers.gpa.alloc(u8, head_len + wire.header_len + payload.len) catch return false;
+ header(msg, peer.id, peer.seq);
+ msg[head_len] = tag;
+ std.mem.writeInt(u32, msg[head_len + 1 ..][0..4], @intCast(payload.len), .little);
+ @memcpy(msg[head_len + wire.header_len ..], payload);
+ if (parseInput(msg) == null) {
+ peer.peers.gpa.free(msg);
+ return false;
+ }
+ peer.peers.post(.{ .input = .{ .peer = peer, .msg = msg } });
+ return true;
+ }
+
+ fn readLoop(peer: *Peer) void {
+ readFramed(peer.peers.gpa, peer.fd, peer, take);
+ peer.peers.post(.{ .gone = peer });
+ }
+};
+
+pub const Peers = struct {
+ gpa: Allocator = std.heap.c_allocator,
+ io: std.Io = undefined,
+ /// This session's name and pid, for a welcome.
+ name: []const u8 = "",
+ t: tape.Tape = undefined,
+ recording: bool = false,
+ list: [pardes.views.max]?*Peer = @splat(null),
+ next_id: u16 = 1,
+ seq: u32 = 0,
+ lock: std.Io.Mutex = .init,
+ inbox: std.ArrayListUnmanaged(Note) = .empty,
+ taken: std.ArrayListUnmanaged(Note) = .empty,
+ /// A reader posted: the loop's one test a step while nothing is attached.
+ mail: std.atomic.Value(bool) = .init(false),
+ /// Wakes the session's loop (its wait for input) from a reader.
+ wake_ctx: ?*anyopaque = null,
+ wake: ?*const fn (?*anyopaque) void = null,
+ /// What the session would be with no client: unattended without a
+ /// terminal of its own (the host says).
+ alone_unattended: bool = false,
+ /// The worst a join cost the session's step, and the last one's dump.
+ join_ns: u64 = 0,
+ dump_bytes: usize = 0,
+ /// What each client input cost the session (its step, its unit sent),
+ /// and each boundary (its pump's unit sent, what came taken in), ns:
+ /// PARDES_REPLICA_STATS writes them at the session's end.
+ input_ns: std.ArrayListUnmanaged(u64) = .empty,
+ boundary_ns: std.ArrayListUnmanaged(u64) = .empty,
+ cut: u32 = 0,
+ head: [unit_head]u8 = undefined,
+
+ pub const Note = union(enum) { join: c_int, input: struct { peer: *Peer, msg: []u8 }, gone: *Peer, check: *Peer };
+
+ pub fn count(ps: *const Peers) usize {
+ var n: usize = 0;
+ for (ps.list) |slot| n += @intFromBool(slot != null);
+ return n;
+ }
+
+ fn post(ps: *Peers, note: Note) void {
+ ps.lock.lockUncancelable(ps.io);
+ ps.inbox.append(ps.gpa, note) catch {
+ ps.lock.unlock(ps.io);
+ switch (note) {
+ .join => |fd| _ = libc.close(fd),
+ .input => |in| ps.gpa.free(in.msg),
+ .gone, .check => {},
+ }
+ return;
+ };
+ ps.mail.store(true, .release);
+ ps.lock.unlock(ps.io);
+ if (ps.wake) |w| w(ps.wake_ctx);
+ }
+
+ /// A replica's connection the daemon handed over (its hello taken
+ /// there): joined at the next step boundary. Any thread.
+ pub fn handed(ps: *Peers, fd: c_int) void {
+ ps.post(.{ .join = fd });
+ }
+
+ /// What was recorded since the last unit, to every client as one, its
+ /// digest at its end.
+ fn unit(ps: *Peers, origin: u16, seq: u32) void {
+ if (tape.taken(&ps.t).len == 0) return;
+ tape.recordDigest(&ps.t);
+ const bytes = tape.taken(&ps.t);
+ unitHeader(&ps.head, origin, seq);
+ if (std.c.getenv("PARDES_REPLICA_TRACE") != null) {
+ var w: std.Io.Writer.Allocating = .init(ps.gpa);
+ defer w.deinit();
+ w.writer.print("session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }) catch {};
+ tape.describe(bytes, &w.writer);
+ std.debug.print("{s}", .{w.written()});
+ }
+ const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len);
+ for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes });
+ tape.clearTaken(&ps.t);
+ }
+
+ /// After each of the session's pumps: its unit sent, the clients that
+ /// came joined, their inputs taken in, the ones gone let go.
+ pub fn boundary(ps: *Peers, core: *Pardes) void {
+ if (!ps.recording and !ps.mail.load(.acquire)) return;
+ const t0 = nowNs();
+ defer if (ps.recording) ps.boundary_ns.append(ps.gpa, nowNs() - t0) catch {};
+ if (ps.recording) {
+ ps.seq += 1;
+ ps.unit(0, ps.seq);
+ }
+ if (!ps.mail.load(.acquire)) return;
+ ps.lock.lockUncancelable(ps.io);
+ std.mem.swap(std.ArrayListUnmanaged(Note), &ps.inbox, &ps.taken);
+ ps.mail.store(false, .release);
+ ps.lock.unlock(ps.io);
+ for (ps.taken.items) |note| switch (note) {
+ .join => |fd| ps.join(core, fd),
+ .input => |in| {
+ ps.input(core, in.peer, in.msg);
+ ps.gpa.free(in.msg);
+ },
+ .gone => |peer| ps.leave(core, peer),
+ .check => |peer| {
+ var out: [16]u8 = undefined;
+ peer.send(&.{wire.encodeServer(&out, .{ .check = tape.digestOf(core) }) catch unreachable});
+ },
+ };
+ ps.taken.clearRetainingCapacity();
+ }
+
+ fn input(ps: *Peers, core: *Pardes, peer: *Peer, msg: []const u8) void {
+ const in = parseInput(msg) orelse return;
+ const t0 = nowNs();
+ defer ps.input_ns.append(ps.gpa, nowNs() - t0) catch {};
+ core.toView(peer.view);
+ core.update(in.event);
+ core.step();
+ ps.unit(peer.id, in.seq);
+ }
+
+ fn refuse(fd: c_int, why: []const u8) void {
+ var out: [600]u8 = undefined;
+ if (wire.encodeServer(&out, .{ .refused = why })) |bytes| _ = writeAll(fd, bytes) else |_| {}
+ _ = libc.close(fd);
+ }
+
+ fn join(ps: *Peers, core: *Pardes, fd: c_int) void {
+ const t0 = nowNs();
+ defer ps.join_ns = @max(ps.join_ns, nowNs() - t0);
+ const slot = for (&ps.list) |*s| {
+ if (s.* == null) break s;
+ } else return refuse(fd, "that session has as many clients as it takes");
+ if (!ps.recording) {
+ // A session recording to a file (PARDES_RECORD) has its tape.
+ if (tape.on != null) return refuse(fd, "that session records a tape of its own (PARDES_RECORD)");
+ ps.t = tape.memory(ps.gpa);
+ ps.t.unit_digests = true;
+ tape.on = &ps.t;
+ ps.recording = true;
+ }
+ // Someone looks at it now: its messages go by keys, not the clock.
+ core.unattended = false;
+ const k = core.openView() orelse {
+ core.unattended = ps.count() == 0 and ps.alone_unattended;
+ if (ps.count() == 0) ps.stop(core);
+ return refuse(fd, "that session has as many views as it takes");
+ };
+ // Its own process draws it.
+ core.undrawn |= @as(u8, 1) << @intCast(k);
+ ps.seq += 1;
+ ps.unit(0, ps.seq);
+ const bytes = pardes.dump.dumpBytes(core) catch |err| {
+ core.undrawn &= ~(@as(u8, 1) << @intCast(k));
+ core.closeView(k);
+ ps.unit(0, ps.seq);
+ if (ps.count() == 0) ps.stop(core);
+ var why: [96]u8 = undefined;
+ return refuse(fd, std.fmt.bufPrint(&why, "that session could not be dumped: {t}", .{err}) catch "that session could not be dumped");
+ };
+ // The core's, as a Dump's is.
+ defer core.gpa.free(bytes);
+ ps.dump_bytes = bytes.len;
+ // The joiner's core is the dump's: the host this tape wraps, and
+ // what the host wrote in the core, are said again at the next input.
+ tape.resync(&ps.t);
+ const peer = ps.gpa.create(Peer) catch return refuse(fd, "out of memory");
+ if (std.c.getenv("PARDES_REPLICA_LAG_KB")) |kb| lag_max = (std.fmt.parseInt(usize, std.mem.span(kb), 10) catch 64 << 10) << 10;
+ peer.* = .{ .peers = ps, .fd = fd, .id = ps.next_id, .view = k, .limit = bytes.len + lag_max };
+ ps.next_id +%= 1;
+ if (ps.next_id == 0) ps.next_id = 1;
+ var out: [wire.hello_max + 64]u8 = undefined;
+ // The session's digest as dumped, for the joiner to check its load by.
+ const digest = tape.digestOf(core);
+ const welcome = wire.encodeServer(&out, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = bytes.len, .gmtoff = gmtoff(), .digest = digest } }) catch unreachable;
+ peer.send(&.{welcome});
+ var at: usize = 0;
+ while (at < bytes.len) : (at += dump_piece) {
+ const piece = bytes[at..@min(bytes.len, at + dump_piece)];
+ const head = wire.header(@backingInt(wire.ServerTag.dump), piece.len);
+ peer.send(&.{ &head, piece });
+ }
+ peer.writer = std.Thread.spawn(.{}, Peer.writeLoop, .{peer}) catch null;
+ peer.reader = std.Thread.spawn(.{}, Peer.readLoop, .{peer}) catch null;
+ if (peer.writer == null or peer.reader == null) {
+ ps.letGo(peer);
+ core.undrawn &= ~(@as(u8, 1) << @intCast(k));
+ core.closeView(k);
+ ps.unit(0, ps.seq);
+ return;
+ }
+ slot.* = peer;
+ log.info("client {d} joined '{s}' as view {d}: a {d} KB dump in {d} us", .{ peer.id, ps.name, k, bytes.len >> 10, (nowNs() - t0) / 1000 });
+ if (std.c.getenv("PARDES_REPLICA_TRACE") != null) std.debug.print("session: client {d} joined after unit {d}\n", .{ peer.id, ps.seq });
+ }
+
+ fn leave(ps: *Peers, core: *Pardes, peer: *Peer) void {
+ for (&ps.list) |*slot| if (slot.* == peer) {
+ slot.* = null;
+ };
+ log.info("client {d} left '{s}'", .{ peer.id, ps.name });
+ core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
+ core.closeView(peer.view);
+ if (ps.count() == 0) core.unattended = ps.alone_unattended;
+ ps.seq += 1;
+ ps.unit(0, ps.seq);
+ ps.letGo(peer);
+ if (ps.count() == 0) ps.stop(core);
+ }
+
+ /// Its threads ended and joined, its link closed.
+ fn letGo(ps: *Peers, peer: *Peer) void {
+ const io = ps.io;
+ peer.lock.lockUncancelable(io);
+ peer.closing = true;
+ peer.cond.signal(io);
+ peer.lock.unlock(io);
+ _ = libc.shutdown(peer.fd, 2);
+ if (peer.writer) |w| w.join();
+ if (peer.reader) |r| r.join();
+ _ = libc.close(peer.fd);
+ peer.out.deinit(ps.gpa);
+ peer.sending.deinit(ps.gpa);
+ // What its reader posted after it was let go (its end) is nobody's.
+ ps.lock.lockUncancelable(io);
+ var i: usize = 0;
+ while (i < ps.inbox.items.len) {
+ const mine = switch (ps.inbox.items[i]) {
+ .input => |in| in.peer == peer,
+ .gone, .check => |p| p == peer,
+ .join => false,
+ };
+ if (!mine) {
+ i += 1;
+ continue;
+ }
+ if (ps.inbox.items[i] == .input) ps.gpa.free(ps.inbox.items[i].input.msg);
+ _ = ps.inbox.orderedRemove(i);
+ }
+ ps.lock.unlock(io);
+ ps.gpa.destroy(peer);
+ }
+
+ /// Detach typed in client view `k`'s: its link ends (its reader says
+ /// it is gone, and the next boundary lets it go).
+ pub fn detach(ps: *Peers, k: u8) bool {
+ for (ps.list) |slot| if (slot) |peer| if (peer.view == k) {
+ _ = libc.shutdown(peer.fd, 2);
+ return true;
+ };
+ return false;
+ }
+
+ fn stop(ps: *Peers, core: *Pardes) void {
+ if (!ps.recording) return;
+ if (core.host.vtable == &ps.t.vtable) core.host = ps.t.real;
+ tape.on = null;
+ tape.release(&ps.t);
+ ps.recording = false;
+ }
+
+ /// Every client let go (the session's end, a Restore): each sees its
+ /// link end.
+ pub fn dropAll(ps: *Peers, core: *Pardes) void {
+ for (&ps.list) |*slot| if (slot.*) |peer| {
+ slot.* = null;
+ core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view));
+ core.closeView(peer.view);
+ ps.letGo(peer);
+ };
+ ps.stop(core);
+ ps.lock.lockUncancelable(ps.io);
+ for (ps.inbox.items) |note| switch (note) {
+ .join => |fd| _ = libc.close(fd),
+ .input => |in| ps.gpa.free(in.msg),
+ .gone, .check => {},
+ };
+ ps.inbox.clearRetainingCapacity();
+ ps.lock.unlock(ps.io);
+ core.unattended = ps.alone_unattended;
+ }
+
+ pub fn deinit(ps: *Peers) void {
+ if (std.c.getenv("PARDES_REPLICA_STATS")) |path| if (ps.input_ns.items.len > 0) ps.writeStats(std.mem.span(path));
+ ps.inbox.deinit(ps.gpa);
+ ps.taken.deinit(ps.gpa);
+ ps.input_ns.deinit(ps.gpa);
+ ps.boundary_ns.deinit(ps.gpa);
+ }
+
+ fn writeStats(ps: *Peers, path: []const u8) void {
+ var buf: [400]u8 = undefined;
+ const line = std.fmt.bufPrint(&buf, "session inputs {d} input_us {d}/{d}/{d} boundaries {d} boundary_us {d}/{d}/{d} join_us {d} dump_kb {d} cut {d}\n", .{
+ ps.input_ns.items.len, pctOf(ps.input_ns.items, 50) / 1000, pctOf(ps.input_ns.items, 99) / 1000, pctOf(ps.input_ns.items, 100) / 1000,
+ ps.boundary_ns.items.len, pctOf(ps.boundary_ns.items, 50) / 1000, pctOf(ps.boundary_ns.items, 99) / 1000, pctOf(ps.boundary_ns.items, 100) / 1000,
+ ps.join_ns / 1000, ps.dump_bytes >> 10, ps.cut,
+ }) catch return;
+ var z: [4096]u8 = undefined;
+ const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return;
+ const fd = libc.open(pz, .{ .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);
+ }
+};
+
+// ---- the client's half ----
+
+pub const Joined = struct {
+ fd: c_int,
+ pid: u32,
+ client: u16,
+ view: u8,
+ gmtoff: i32,
+ dump: []u8,
+ /// The session's digest as dumped (`Replica.loaded`).
+ digest: u64 = 0,
+ /// What was read past the dump: the stream's (`readStream`).
+ rest: Framer = .{},
+};
+
+/// Connected to the replica socket at `path`, its hello said for
+/// `session`, welcomed, and the dump read whole: what a client joins with.
+/// Errors are said in `why`.
+pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?Joined {
+ const fd = libc.socket(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0);
+ if (fd < 0) return said(why, "no socket", .{});
+ var addr: libc.sockaddr.un = .{ .path = @splat(0) };
+ if (path.len >= addr.path.len) {
+ _ = libc.close(fd);
+ return said(why, "the socket's path is too long: {s}", .{path});
+ }
+ @memcpy(addr.path[0..path.len], path);
+ if (libc.connect(fd, @ptrCast(&addr), @sizeOf(libc.sockaddr.un)) != 0) {
+ _ = libc.close(fd);
+ return said(why, "nothing answers at {s}", .{path});
+ }
+ var out: [wire.hello_max]u8 = undefined;
+ const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch {
+ _ = libc.close(fd);
+ return said(why, "that session's name is too long", .{});
+ };
+ var j: Joined = .{ .fd = fd, .pid = 0, .client = 0, .view = 0, .gmtoff = 0, .dump = &.{} };
+ const Read = struct {
+ j: *Joined,
+ gpa: Allocator,
+ why: []u8,
+ got: usize = 0,
+ failed: ?[]const u8 = null,
+ welcomed: bool = false,
+ fn take(r: *@This(), tag: u8, payload: []const u8) bool {
+ const msg = wire.decodeServer(tag, payload) catch {
+ r.failed = "the session's answer was not understood";
+ return false;
+ };
+ switch (msg) {
+ .welcome => |w| {
+ r.j.pid = w.pid;
+ r.j.client = w.client;
+ r.j.view = w.view;
+ r.j.gmtoff = w.gmtoff;
+ r.j.digest = w.digest;
+ r.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch {
+ r.failed = "out of memory for the session's dump";
+ return false;
+ };
+ r.welcomed = true;
+ return w.dump_len > 0;
+ },
+ .refused => |words| {
+ r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused";
+ return false;
+ },
+ .dump => |piece| {
+ if (!r.welcomed or r.got + piece.len > r.j.dump.len) {
+ r.failed = "the session sent more dump than it said";
+ return false;
+ }
+ @memcpy(r.j.dump[r.got..][0..piece.len], piece);
+ r.got += piece.len;
+ return r.got < r.j.dump.len;
+ },
+ else => {
+ r.failed = "the session sent a unit before its dump";
+ return false;
+ },
+ }
+ }
+ };
+ if (!writeAll(fd, hello)) {
+ _ = libc.close(fd);
+ return said(why, "the session hung up", .{});
+ }
+ var r: Read = .{ .j = &j, .gpa = gpa, .why = why };
+ j.rest.run(gpa, fd, &r, Read.take);
+ if (r.failed != null or !r.welcomed or r.got < j.dump.len) {
+ j.rest.deinit(gpa);
+ if (j.dump.len > 0) gpa.free(j.dump);
+ _ = libc.close(fd);
+ const text = r.failed orelse "the session hung up before the dump was whole";
+ if (text.ptr == why.ptr) return null;
+ return said(why, "{s}", .{text});
+ }
+ return j;
+}
+
+fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined {
+ _ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {};
+ return null;
+}
+
+/// A message `Replica.input` made, to the session: the event's frame (its
+/// client and seq are the connection's).
+pub fn sendInput(fd: c_int, msg: []const u8) bool {
+ return writeAll(fd, msg[head_len..]);
+}
+
+/// Asks the session's digest, answered in stream order.
+pub fn askCheck(fd: c_int) bool {
+ var out: [16]u8 = undefined;
+ return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable);
+}
+
+/// The client's last word.
+pub fn bye(fd: c_int) void {
+ var out: [16]u8 = undefined;
+ if (wire.encodeClient(&out, .bye)) |b| _ = writeAll(fd, b) else |_| {}
+}
+
+/// What follows the dump on the client's connection, each message handed
+/// to `take` (a unit, a terminal's delta) until the link ends; `rest` is
+/// what the dump's reader read past it.
+pub fn readStream(gpa: Allocator, fd: c_int, rest: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void {
+ rest.run(gpa, fd, ctx, take);
+}
+
pub fn digestOf(a: *Authority) u64 {
const was = Stepping.begin(&a.t, null);
defer was.end();
@@ -1078,3 +1886,36 @@ test "the authority's units with a big file open: its blobs hashed once (PARDES_
std.debug.print("{d} MB file, blobs {s}: a unit p50 {d} us p99 {d} us; a digest p50 {d} us p99 {d} us\n", .{ mb, if (on) "hashed once" else "hashed whole", us[0] / 1000, us[1] / 1000, ds[0] / 1000, ds[1] / 1000 });
}
}
+
+test "a replica joined from the session's dump replays the units after it" {
+ const gpa = std.testing.allocator;
+ const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false);
+ defer a.deinit();
+ _ = try a.local(.{ .key = .{ .cp = 'j', .text = "j" } });
+ const was = Stepping.begin(&a.t, null);
+ const bytes = try pardes.dump.dumpBytes(a.core);
+ tape.resync(&a.t);
+ was.end();
+ defer a.core.gpa.free(bytes);
+ const r = try Replica.initDump(gpa, 1, 0, bytes, .{ .cols = 80, .rows = 24 });
+ defer r.deinit();
+ try std.testing.expectEqual(digestOf(a), r.digest());
+ r.confirm(try a.local(.{ .key = .{ .cp = 'j', .text = "j" } }));
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ var sent: std.ArrayListUnmanaged([]u8) = .empty;
+ defer {
+ for (sent.items) |m| gpa.free(m);
+ sent.deinit(gpa);
+ }
+ for ("oab") |k| try key(r, k, &sent);
+ try key(r, pardes.Key.escape, &sent);
+ for (sent.items) |m| {
+ r.confirm(try a.take(m));
+ r.settle();
+ }
+ if (r.diverged) |why| std.debug.print("diverged: {s}\n", .{why});
+ try std.testing.expect(r.diverged == null);
+ try std.testing.expect(r.quiet());
+ try std.testing.expectEqual(digestOf(a), r.digest());
+}
diff --git a/src/rollback.zig b/src/rollback.zig
index 7216664c..b0394d22 100644
--- a/src/rollback.zig
+++ b/src/rollback.zig
@@ -40,11 +40,22 @@ const Pardes = pardes.Pardes;
const blob_min = 4096;
const checked = builtin.mode == .debug or builtin.mode == .safe;
+/// An address as a hash map's key: a multiply, not a Wyhash of its bytes
+/// (every allocation of a replica's core is a lookup).
+const Addr = struct {
+ pub fn hash(_: Addr, a: usize) u64 {
+ return @as(u64, a) *% 0x9e3779b97f4a7c15;
+ }
+ pub fn eql(_: Addr, a: usize, b: usize) bool {
+ return a == b;
+ }
+};
+
pub const Epoch = struct {
child: Allocator = std.heap.c_allocator,
/// Its own bookkeeping, never the epoch's.
meta: Allocator = std.heap.c_allocator,
- live: std.AutoHashMapUnmanaged(usize, Entry) = .empty,
+ live: std.HashMapUnmanaged(usize, Entry, Addr, std.hash_map.default_max_load_percentage) = .empty,
on: bool = false,
id: u32 = 0,
born: std.ArrayListUnmanaged(usize) = .empty,
@@ -190,7 +201,7 @@ pub const Copy = struct {
meta: Allocator = std.heap.c_allocator,
regions: std.ArrayListUnmanaged(Region) = .empty,
bytes: std.ArrayListUnmanaged(u8) = .empty,
- seen: std.AutoHashMapUnmanaged(Seen, void) = .empty,
+ seen: std.HashMapUnmanaged(Seen, void, SeenContext, std.hash_map.default_max_load_percentage) = .empty,
pins: std.ArrayListUnmanaged(Pin) = .empty,
held: bool = false,
epoch: ?*Epoch = null,
@@ -202,6 +213,14 @@ pub const Copy = struct {
const Region = struct { addr: usize, off: usize, len: usize };
const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 };
const Seen = struct { addr: usize, ty: usize };
+ const SeenContext = struct {
+ pub fn hash(_: SeenContext, k: Seen) u64 {
+ return (@as(u64, k.addr) *% 0x9e3779b97f4a7c15) ^ (@as(u64, k.ty) *% 0xc2b2ae3d27d4eb4f);
+ }
+ pub fn eql(_: SeenContext, a: Seen, b: Seen) bool {
+ return a.addr == b.addr and a.ty == b.ty;
+ }
+ };
pub const Error = Allocator.Error;
pub fn deinit(c: *Copy) void {
diff --git a/src/tape.zig b/src/tape.zig
index c2ddbb9d..b30096f5 100644
--- a/src/tape.zig
+++ b/src/tape.zig
@@ -167,6 +167,9 @@ pub const Tape = struct {
/// output carries one, at the pty's read rate), not one a field; what
/// differs is then not named.
whole_digest: bool = false,
+ /// 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 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.
@@ -275,7 +278,7 @@ pub const Tape = struct {
t.wrap(core);
};
t.record(.done, void, &{});
- if ((t.depth == 0 or t.every) and !t.no_digest) if (t.core) |core| {
+ if ((t.depth == 0 or t.every) and !t.no_digest and !t.unit_digests) if (t.core) |core| {
var parts: Parts = undefined;
const whole = [1]u64{digestOf(core)};
const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
@@ -802,6 +805,16 @@ fn guessed(comptime R: type, g: Guess) R {
@compileError("tape: no guess of a clock read as " ++ @typeName(R));
}
+/// The session's digest recorded now (a unit's end: the replica stream's
+/// one, `unit_digests`).
+pub fn recordDigest(t: *Tape) void {
+ const core = t.core orelse return;
+ var parts: Parts = undefined;
+ const whole = [1]u64{digestOf(core)};
+ const d: []const u64 = if (t.whole_digest) &whole else digestParts(core, &parts);
+ t.record(.digest, []const u64, &d);
+}
+
/// A core made outside the tape (a replica client's, from a dump) is the
/// one it replays into.
pub fn adoptCore(t: *Tape, p: *Pardes) void {
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index 14dc8042..43994645 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -17,6 +17,8 @@ const panel_compositor = @import("panel_compositor.zig");
const daemon = @import("../daemon.zig");
const host_io = @import("../host_io.zig");
const upgrade = @import("../upgrade.zig");
+const replica = @import("../replica.zig");
+const wire = @import("../detached/wire.zig");
extern "c" fn setenv(name: [*:0]const u8, value: [*:0]const u8, overwrite: c_int) c_int;
@@ -150,6 +152,15 @@ pub const Command = struct {
search_rows: pardes.Stream.Batch,
files_changed,
fs_ready,
+ /// A replica's connection, handed over by the daemon (Peers).
+ replica: posix.fd_t,
+ /// A replica's reader posted: the step boundary takes it in.
+ replica_mail,
+ /// A replica client's: what the session streamed (replica.Client).
+ unit: []u8,
+ /// The session's digest, asked by PARDES_REPLICA_CHECK.
+ checked: u64,
+ link_gone,
} = .nop;
};
const Loop = vaxis.Loop(@TypeOf(Command.value));
@@ -838,14 +849,18 @@ fn terminalCellPixels(total_pixels: u16, cells: u16, fallback: u32) u32 {
}
fn updateCoreTerminalSize(core: *pardes.Pardes, cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) void {
- core.update(.{ .resize = .{
+ core.update(.{ .resize = terminalSize(cols, rows, pixel_w, pixel_h) });
+}
+
+fn terminalSize(cols: u16, rows: u16, pixel_w: u16, pixel_h: u16) @FieldType(pardes.Event, "resize") {
+ return .{
.cols = cols,
.rows = rows,
.cell_pixels = .{
.w = @intCast(terminalCellPixels(pixel_w, cols, 8)),
.h = @intCast(terminalCellPixels(pixel_h, rows, 16)),
},
- } });
+ };
}
/// A terminal pane's image fragment (`native.exact`): its `src` into its
@@ -1255,6 +1270,268 @@ pub fn run(init: std.process.Init, opts: pardes.Options, term: Term) !void {
try localSession(init, opts, &screen, &loop, &paste_buf);
}
+/// A replica client's link to its session (`remote`).
+const Remote = struct {
+ r: *replica.Replica,
+ fd: posix.fd_t,
+ 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 terminal's answer (FrameAck).
+ 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,
+ /// How many of its own inputs the session recorded otherwise when it
+ /// last drew: a frame after one that writes to the terminal 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,
+
+ /// PARDES_REPLICA_STATS=<file>: how its inputs and the session's units
+ /// went, a line at its end (test/daemon.py, the gates).
+ fn writeStats(c: *Remote, path: []const u8) void {
+ const st = &c.r.stats;
+ var buf: [600]u8 = undefined;
+ 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}\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, pct(st.spec_ns.items, 50) / 1000,
+ pct(st.spec_ns.items, 99) / 1000, pct(st.copy_ns.items, 50) / 1000, pct(st.copy_ns.items, 99) / 1000, pct(st.rollback_ns.items, 50) / 1000, pct(st.rollback_ns.items, 99) / 1000,
+ }) catch return;
+ var z: [4096]u8 = undefined;
+ const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return;
+ const fd = libc.open(pz, .{ .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);
+ }
+
+ fn pct(samples: []u64, p: usize) u64 {
+ if (samples.len == 0) return 0;
+ std.mem.sort(u64, samples, {}, std.sort.asc(u64));
+ return samples[@min(samples.len - 1, samples.len * p / 100)];
+ }
+
+ /// The session's digest against this replica's, if it is quiet still.
+ fn checked(c: *Remote, theirs: u64) void {
+ c.asked = false;
+ const path = c.check orelse return;
+ if (!c.r.quiet()) 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;
+ 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);
+ }
+
+ /// The client's own view drawn on its terminal, outside any step: the
+ /// replica's digest stays the session's (views.drawOne). What the host
+ /// writes in the core as it presents (a frame skipped, one owed) is put
+ /// back, as the frame statistics are this process's.
+ fn draw(c: *Remote, s: *Shell) void {
+ const core = c.r.core();
+ const skipped = core.present_skipped;
+ const needs = core.needs_frame;
+ core.frames.mark_start = pardes.FrameStats.nowNs();
+ core.frames.mark_update = core.frames.mark_start;
+ c.r.draw(.{ .ctx = s, .f = Shell.present });
+ core.frames.mark_render = core.frames.mark_update;
+ if (c.r.stats.mispredicted > c.mispredicted) {
+ c.mispredicted = c.r.stats.mispredicted;
+ if (core.frames.bytes > 0) c.visible += 1;
+ }
+ c.owed = core.present_skipped;
+ core.frames.record(pardes.FrameStats.nowNs(), false, c.owed);
+ core.present_skipped = skipped;
+ core.needs_frame = needs;
+ c.dirty = false;
+ }
+};
+
+/// The session's stream, onto the loop: its units, and its end.
+fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loop: *Loop, done: *std.atomic.Value(bool)) void {
+ defer done.store(true, .release);
+ const Take = struct {
+ fn take(l: *Loop, tag: u8, payload: []const u8) bool {
+ const t = std.enums.fromInt(wire.ServerTag, tag) orelse return false;
+ switch (t) {
+ .unit => {
+ const bytes = std.heap.c_allocator.dupe(u8, payload) catch return false;
+ l.postEvent(.{ .unit = bytes }) catch std.heap.c_allocator.free(bytes);
+ },
+ .check => {
+ const d = (wire.decodeServer(tag, payload) catch return false).check;
+ l.postEvent(.{ .checked = d }) catch {};
+ },
+ .refused => return false,
+ // ponytail: a terminal's delta (rterm's, src/screen_state.zig)
+ // applies here once terminals cross as screens.
+ .screen, .pong, .welcome, .dump => {},
+ }
+ return true;
+ }
+ };
+ replica.readStream(gpa, fd, rest, loop, Take.take);
+ loop.postEvent(.link_gone) catch {};
+}
+
+/// `pardes --attach=<socket>:<session>` (and `<host>:<session>`, through
+/// ssh, main.zig): this terminal a replica client of a session elsewhere
+/// (docs/next-daemon.md, "Phase 5 part B notes"). Its own core, loaded
+/// from the session's dump, applies its keys at once (src/replica.zig);
+/// the session's units are the truth it rolls back to. Its own view of the
+/// session is drawn here, as the session draws its terminals'.
+pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8) !void {
+ const io = init.io;
+ const gpa = init.gpa;
+ var why: [600]u8 = @splat(0);
+ var joined = replica.dial(gpa, path, session, &why) orelse {
+ var said: [700]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: {s}\n", .{std.mem.sliceTo(&why, 0)}) catch "pardes: --attach failed\n") catch {};
+ std.process.exit(1);
+ };
+ defer _ = libc.close(joined.fd);
+ defer joined.rest.deinit(gpa);
+
+ const allocs = pardes.memory.init(gpa);
+ defer pardes.memory.deinit();
+ pardes.image.start(io, allocs.image);
+ if (comptime pardes.pdf_enabled) pardes.pdf.start(allocs.pdf);
+ pardes.syntax.start(allocs.tree_sitter);
+ defer {
+ pardes.image.stop();
+ if (comptime pardes.pdf_enabled) pardes.pdf.stop();
+ pardes.syntax.stop();
+ }
+ var o = opts;
+ o.image_allocator = allocs.image;
+ o.pdf_allocator = allocs.pdf;
+ const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
+ gpa.free(joined.dump);
+ var said: [200]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: the session's dump would not load: {t}\n", .{err}) catch "pardes: --attach: the dump would not load\n") catch {};
+ std.process.exit(1);
+ };
+ gpa.free(joined.dump);
+ defer r.deinit();
+ r.gmtoff = joined.gmtoff;
+ if (!r.loaded(joined.digest)) {
+ std.Io.File.stderr().writeStreamingAll(io, "pardes: --attach: the session's dump loaded as another session (a build's dump is not complete)\n") catch {};
+ std.process.exit(1);
+ }
+
+ var winch_set = posix.sigemptyset();
+ posix.sigaddset(&winch_set, posix.SIG.WINCH);
+ if (comptime builtin.os.tag == .freebsd)
+ posix.sigaction(.WINCH, &.{ .handler = .{ .handler = keepSignal }, .mask = posix.sigemptyset(), .flags = 0 }, null);
+ posix.sigprocmask(posix.SIG.BLOCK, &winch_set, null);
+ var tty_buf: [0x10000]u8 = undefined;
+ var screen: Screen = .{ .io = io, .gpa = gpa, .env = init.environ_map, .buf = &tty_buf, .kitty = .init(gpa), .primary = true };
+ defer screen.kitty.deinit();
+ try screen.enter(try vaxis.Tty.init(io, &tty_buf));
+ defer screen.leave();
+ defer screen.glide.deinit(gpa);
+ screen.feed.view = joined.view;
+ var paste_buf: std.Io.Writer.Allocating = .init(gpa);
+ defer paste_buf.deinit();
+ var loop: Loop = .init(io, &screen.tty, &screen.vx);
+ var frame_arena: std.heap.ArenaAllocator = .init(allocs.frame);
+ defer frame_arena.deinit();
+ var link: Remote = .{ .r = r, .fd = joined.fd };
+ if (init.environ_map.get("PARDES_REPLICA_CHECK")) |file| link.check = try gpa.dupeSentinel(u8, file, 0);
+ defer if (link.check) |file| gpa.free(file);
+ var sh: Shell = .{
+ .io = io,
+ .gpa = gpa,
+ .lsp_gpa = allocs.lsp,
+ .core = r.core(),
+ .prompt_rcs = undefined,
+ .loop = &loop,
+ .frame = &frame_arena,
+ .paste_buf = &paste_buf,
+ .inotify_fd = -1,
+ .from = joined.view,
+ .remote = &link,
+ };
+ sh.terms[joined.view] = &screen;
+
+ try startInput(&loop, &screen);
+ defer stopInput(io, &screen.thread);
+ (try std.Thread.spawn(.{}, winchWatch, .{&loop})).detach();
+ var reader_done: std.atomic.Value(bool) = .init(false);
+ const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done });
+ if (link.check != null) (try std.Thread.spawn(.{}, wakeWatch, .{ io, &loop })).detach();
+ try screen.vx.queryTerminalSend(screen.tty.writer());
+ // Its size is its view's first input.
+ sh.winched();
+ while (!link.quit) {
+ if (link.dirty) link.draw(&sh);
+ const first = (loop.tryEvent() catch null) orelse idle: {
+ // Idle, its frame drawn: the next key needs no copy taken.
+ r.ready();
+ break :idle loop.nextEvent() catch break;
+ };
+ var ev = first;
+ var batch: usize = 1;
+ while (true) {
+ _ = sh.apply(ev);
+ if (link.quit or (!sh.in_paste and batch >= 64)) break;
+ ev = (loop.tryEvent() catch null) orelse break;
+ batch += 1;
+ }
+ r.settle();
+ if (screen.caps_pending and screen.vx.queries_done.load(.unordered)) {
+ screen.caps_pending = false;
+ screen.vx.enableDetectedFeatures(screen.tty.writer()) catch {};
+ screen.vx.queueRefresh();
+ link.dirty = true;
+ }
+ if (link.owed) link.dirty = true;
+ if (link.check != null) {
+ // Quiet a while after a change: asked once (a wake, then the
+ // next quiet one).
+ if (first == .wake and link.changed and !link.asked and r.quiet()) {
+ link.changed = false;
+ link.asked = replica.askCheck(joined.fd);
+ } else if (link.changed and r.quiet()) requestWake(io, 300);
+ }
+ if (r.diverged) |reason| {
+ screen.leave();
+ var said: [700]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: this replica diverged from '{s}': {s}\n", .{ session, reason }) catch "pardes: --attach: diverged\n") catch {};
+ std.process.exit(1);
+ }
+ }
+ if (init.environ_map.get("PARDES_REPLICA_STATS")) |file| link.writeStats(file);
+ if (!link.gone) replica.bye(joined.fd);
+ // The link's reader ends with the link, posting as it goes: what it
+ // and the terminal posted is let go until it has.
+ _ = libc.shutdown(joined.fd, 2);
+ while (true) {
+ while (loop.tryEvent() catch null) |ev| switch (ev) {
+ .unit => |b| std.heap.c_allocator.free(b),
+ .paste => |b| gpa.free(@constCast(b)),
+ else => {},
+ };
+ if (reader_done.load(.acquire)) break;
+ std.Thread.yield() catch {};
+ }
+ reader.join();
+ if (link.gone) {
+ screen.leave();
+ var said: [300]u8 = undefined;
+ std.Io.File.stderr().writeStreamingAll(io, std.fmt.bufPrint(&said, "pardes: --attach: '{s}' let this client go (a Detach in it, or the session's end)\n", .{session}) catch "pardes: --attach: the session let this client go\n") catch {};
+ }
+}
+
/// A terminal a session draws on, while it has one: entered (raw, the
/// alternate screen, mouse and paste reports) and left (all of it put back,
/// the terminal closed) at the session's start and end, and at a detach and
@@ -1462,6 +1739,10 @@ fn localSession(
.test_clock = host_io.testClock(),
};
sh.terms[0] = screen;
+ sh.peers = .{ .gpa = gpa, .io = io, .name = daemon.name(), .wake_ctx = loop, .wake = wakeForReplica };
+ defer sh.peers.deinit();
+ // Before the core goes: each client sees its link end.
+ defer sh.peers.dropAll(sh.core);
pardes.lsp.setStatusSink(&sh, lspStatusSink);
host_io.setExitWake(loop, wakeForExit);
defer screen.glide.deinit(gpa);
@@ -1524,7 +1805,7 @@ fn localSession(
// An upgraded session's terminals, its own again.
if (comptime !builtin.is_test) if (upgrade.resumed) |*rec| sh.resumeTerminals(rec, screen);
// Nobody to see it, and nobody to size it: `size` sets it (ctl.zig).
- core.unattended = !sh.attached();
+ sh.setAttended(sh.attached());
while (core.nextEffect()) |effect| core.perform(effect);
// ...and its shells, once a fresh session's own has a directory to
// adopt them in.
@@ -1564,6 +1845,8 @@ fn localSession(
while (!core.quit) {
pardes.turn.bump(&pardes.turn.restores);
try core.pump(host);
+ // Its unit to each replica client, and theirs taken in.
+ sh.peers.boundary(core);
upgrade.locate();
// Asked in the step (the word), done between steps: the session whole.
if (sh.upgrade_asked) |pane| sh.upgradeNow(pane);
@@ -1572,6 +1855,9 @@ fn localSession(
// off a startup's way.
daemon.watch(loop, deliverNote);
if (core.takeRestore()) |rp| blk: {
+ // ponytail: a Restore replaces the core its clients replicate;
+ // they are let go, and attach again.
+ sh.peers.dropAll(core);
const bytes = filesystem.readRestore(gpa, rp, core.settings.dump_dir.get()) catch |err| {
core.reportError(core.active, "Restore", err);
break :blk;
@@ -1620,6 +1906,7 @@ fn localSession(
if (live.on) clearNativeImages(&live.kitty, &live.vx, &live.tty);
nc.native_images = live.on and live.vx.caps.kitty_graphics;
nc.unattended = !live.on;
+ sh.peers.alone_unattended = !live.on;
if (fs) |f| f.reset(nc);
core.deinit();
core = nc;
@@ -1664,9 +1951,21 @@ fn deliverNote(ctx: *anyopaque, note: daemon.Note) void {
};
loop.postEvent(.{ .adopted = .{ .pid = 0, .fd = -1 } }) catch {};
},
+ .replica => |fd| loop.postEvent(.{ .replica = fd }) catch {
+ _ = libc.close(fd);
+ },
}
}
+/// A replica reader's post: the session's wait ends, and its step
+/// boundary takes the post in.
+fn wakeForReplica(ctx: ?*anyopaque) void {
+ const loop: *Loop = @ptrCast(@alignCast(ctx.?));
+ // Never waits: a full queue wakes the loop anyway, and a reader the
+ // loop is joining (a client let go) must not wait on it.
+ _ = loop.tryPostEvent(.replica_mail) catch {};
+}
+
const Shell = struct {
io: std.Io,
gpa: std.mem.Allocator,
@@ -1718,9 +2017,42 @@ const Shell = struct {
upgrade_asked: ?u8 = null,
/// Its pty readers stopped for it: their end of file is not a shell's.
upgrading: bool = false,
+ /// The session's replica clients (src/replica.zig), on sockets.
+ peers: replica.Peers = .{},
+ /// This wait took only a replica's post (its input, its join): the
+ /// pump after it reads no new time, so it moves nothing the replicas'
+ /// speculations stand on (a replica takes such a unit as is).
+ mail_only: bool = false,
+ waited: bool = false,
+ /// This process a replica client of a session elsewhere (`remote`):
+ /// its inputs go to the replica, never straight to the core.
+ remote: ?*Remote = null,
const Leaving = enum { detach, exit };
+ /// A terminal's input, to the core, or to a replica client's engine
+ /// (applied at once there, and sent to the session).
+ fn feed(s: *Shell, ev: pardes.Event) void {
+ const c = s.remote orelse return s.core.update(ev);
+ const msg = c.r.input(ev) catch return;
+ if (!replica.sendInput(c.fd, msg)) c.quit = true;
+ c.dirty = true;
+ }
+
+ /// Whether a terminal of this process's shows the session, or
+ /// nothing (a test) can.
+ fn local(s: *const Shell) bool {
+ for (s.terms) |slot| if (slot != null) return true;
+ return false;
+ }
+
+ /// Whether anyone looks at the session: a terminal here, or a replica
+ /// client (Peers keeps it attended while one is there).
+ fn setAttended(s: *Shell, on: bool) void {
+ s.peers.alone_unattended = !on;
+ s.core.unattended = !on and s.peers.count() == 0;
+ }
+
fn of(ctx: ?*anyopaque) *Shell {
return @ptrCast(@alignCast(ctx.?));
}
@@ -1770,6 +2102,10 @@ const Shell = struct {
fn clockNow(ctx: ?*anyopaque) u64 {
const s = of(ctx);
+ if (s.waited) {
+ s.waited = false;
+ if (s.mail_only and s.peers.count() > 0 and s.core.clock_started) return s.core.now_ns;
+ }
return s.test_clock orelse @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
}
@@ -1783,15 +2119,23 @@ const Shell = struct {
return screen.on;
}
- /// Drawn only while a terminal shows it.
+ /// Drawn only while a terminal shows it: a replica client's view live
+ /// (its input the last), whichever terminal here shows another.
fn watched(ctx: ?*anyopaque) bool {
- return of(ctx).attached();
+ const s = of(ctx);
+ if (s.term() == null and s.local()) {
+ for (s.terms) |slot| if (slot) |sc| if (sc.on) return true;
+ return false;
+ }
+ return s.attached();
}
/// Detach: the terminal put back and let go, the stub told (it ends,
/// and the shell has its terminal again), the session left running.
fn detach(ctx: ?*anyopaque) void {
const s = of(ctx);
+ // A replica client's: its link ends.
+ if (s.peers.detach(s.core.view)) return;
if (!s.attached()) return s.core.reportFailure(s.core.active, "Detach: no terminal is attached");
// One of several: its view goes, and the session stays on the rest.
if (pardes.views.count(s.core) > 1) return s.dropView(s.core.view, .detach);
@@ -1835,7 +2179,7 @@ const Shell = struct {
stopInput(s.io, &screen.thread);
screen.leave();
s.core.native_images = false;
- s.core.unattended = true;
+ s.setAttended(false);
// The stub's stdin, stdout and stderr were its terminal too.
const null_fd = libc.open("/dev/null", .{ .ACCMODE = .RDWR, .CLOEXEC = true }, @as(libc.mode_t, 0));
if (null_fd >= 0) {
@@ -1883,7 +2227,7 @@ const Shell = struct {
s.terms[k] = screen;
}
screen.setStub(stub);
- s.core.unattended = false;
+ s.setAttended(true);
startInput(s.loop, screen) catch {};
screen.vx.queryTerminalSend(screen.tty.writer()) catch {};
s.core.needs_frame = true;
@@ -2124,6 +2468,8 @@ const Shell = struct {
}
s.input_seen = false;
s.output_seen = false;
+ s.mail_only = true;
+ defer s.waited = true;
// 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.
@@ -2215,15 +2561,16 @@ const Shell = struct {
else => {},
}
if (s.input_seen) s.input_ns = @intCast(std.Io.Clock.awake.now(s.io).nanoseconds);
+ if (event != .replica_mail) s.mail_only = false;
// A terminal's input goes into its view; a closed one's last is
// dropped.
switch (event) {
.key_press, .mouse, .paste, .paste_start, .paste_end, .winsize, .focus_in, .focus_out, .quit => {
- if (s.terms[s.from] == null and s.term() != null) {
+ if (s.terms[s.from] == null and s.local()) {
if (event == .paste) s.gpa.free(@constCast(event.paste));
return false;
}
- if (s.term() != null) core.toView(s.from);
+ if (s.local() and s.remote == null) core.toView(s.from);
},
else => {},
}
@@ -2242,6 +2589,10 @@ const Shell = struct {
virtual.* = @max(virtual.*, s.wake_to);
},
.quit => {
+ if (s.remote) |c| {
+ c.quit = true;
+ return true;
+ }
// One of several: its view goes with it.
if (pardes.views.count(core) > 1) {
s.dropView(core.view, .detach);
@@ -2261,15 +2612,37 @@ const Shell = struct {
},
.attach => |a| s.attachTerminal(a.tty, a.stub, null),
.adopted => |a| s.adoptShell(a.pid, a.fd),
+ .replica => |fd| {
+ s.peers.handed(fd);
+ s.input_seen = true;
+ },
+ .replica_mail => s.input_seen = true,
+ .unit => |bytes| {
+ if (s.remote) |c| {
+ c.r.confirm(bytes);
+ if (c.r.moved) c.dirty = true;
+ c.r.moved = false;
+ c.changed = true;
+ }
+ std.heap.c_allocator.free(bytes);
+ },
+ .checked => |d| if (s.remote) |c| c.checked(d),
+ .link_gone => if (s.remote) |c| {
+ c.quit = true;
+ c.gone = true;
+ return true;
+ },
.focus_in => {
- core.update(.{ .focus = true });
- upgrade.noticeNewer(core);
+ s.feed(.{ .focus = true });
+ // A client's core is the session's: the notice is the session's to give.
+ // ponytail: not while clients replicate it (set outside a step).
+ if (s.remote == null and !s.peers.recording) upgrade.noticeNewer(core);
},
.focus_out => {
- core.update(.pointer_leave);
- core.update(.{ .focus = false });
+ s.feed(.pointer_leave);
+ s.feed(.{ .focus = false });
},
- .winsize => |ws| if (s.attached()) s.resized(ws),
+ .winsize => |ws| if (s.attached()) s.resized(s.terms[s.from], ws),
.pty_read => |pr| {
tookRead(s.io, pr.id);
s.output_seen = true;
@@ -2306,10 +2679,10 @@ const Shell = struct {
const bytes = pasteBytes(key);
const room = max_paste_bytes -| s.paste_buf.written().len;
s.paste_buf.writer.writeAll(bytes[0..@min(bytes.len, room)]) catch {};
- } else core.update(vaxis_input.keyEvent(key)),
+ } else s.feed(vaxis_input.keyEvent(key)),
.mouse => |m| {
const pdf_before = nativePdfWheelTarget(core, m);
- if (mouseEvent(m)) |ev| core.update(ev);
+ if (mouseEvent(m)) |ev| s.feed(ev);
if (pdf_before) |before| {
if (core.panes[before.pane]) |pane| {
if (pane.pdfPage()) |page| {
@@ -2319,7 +2692,7 @@ const Shell = struct {
}
},
.paste => |bytes| {
- core.update(.{ .paste = bytes });
+ s.feed(.{ .paste = bytes });
s.gpa.free(@constCast(bytes));
},
.paste_start => {
@@ -2329,7 +2702,7 @@ const Shell = struct {
.paste_end => {
s.in_paste = false;
const pasted = s.paste_buf.written();
- if (pasted.len > 0) core.update(.{ .paste = pasted });
+ if (pasted.len > 0) s.feed(.{ .paste = pasted });
s.paste_buf.clearRetainingCapacity();
},
.files_changed => s.check_files = true,
@@ -2366,17 +2739,13 @@ const Shell = struct {
return false;
}
- /// The live view's terminal is `ws` big.
- fn resized(s: *Shell, ws: vaxis.Winsize) void {
- const core = s.core;
- if (s.term()) |sc| {
+ /// Terminal `screen` (the live view's) is `ws` big.
+ fn resized(s: *Shell, screen: ?*Screen, ws: vaxis.Winsize) void {
+ if (screen) |sc| {
sc.size = ws;
sc.vx.resize(s.gpa, sc.tty.writer(), ws) catch {};
}
- if (comptime pardes.pdf_enabled)
- updateCoreTerminalSize(core, ws.cols, ws.rows, ws.x_pixel, ws.y_pixel)
- else
- core.update(.{ .resize = .{ .cols = ws.cols, .rows = ws.rows } });
+ s.feed(.{ .resize = if (comptime pardes.pdf_enabled) terminalSize(ws.cols, ws.rows, ws.x_pixel, ws.y_pixel) else .{ .cols = ws.cols, .rows = ws.rows } });
}
/// A SIGWINCH: each terminal whose size it changed is resized, in its
@@ -2387,8 +2756,8 @@ const Shell = struct {
if (!sc.on or sc.vx.state.in_band_resize) continue;
const ws = sc.tty.getWinsize() catch continue;
if (sc.size) |was| if (std.meta.eql(was, ws)) continue;
- s.core.toView(@intCast(k));
- s.resized(ws);
+ if (s.remote == null) s.core.toView(@intCast(k));
+ s.resized(sc, ws);
}
}
diff --git a/test/daemon.py b/test/daemon.py
index ce19b502..b54b2b54 100644
--- a/test/daemon.py
+++ b/test/daemon.py
@@ -7,8 +7,12 @@ private HOME and runtime directory:
- a session killed outright: its shells live on, held by the daemon, and
another session adopts them as terminal panes;
- a replica's hello on the daemon's stream socket (phase 5): one of this
- build handed to its session, which answers it and its pings; another
- build's refused, and a session nobody has;
+ build handed to its session, which welcomes it, sends its dump and
+ answers its pings; another build's refused, and a session nobody has;
+- replica clients (`pardes --attach=<socket>:<name>`, phase 5 part B) of a
+ detached session, one through the delay relay: each one's keys in the
+ session's text, every client's digest the session's once quiet, one
+ killed outright while the other types on;
- nothing left behind: no session, shell, daemon or socket.
python3 -B test/daemon.py <pardes> [cycles]
@@ -17,6 +21,8 @@ private HOME and runtime directory:
python3 -B test/daemon.py --ping <socket> <session> <pardes> [n]
a hello of <pardes>'s build and n pings on a replica socket (one
`ssh -L` forwards, say), timed
+ python3 -B test/daemon.py --clients <pardes>
+ the replica clients alone
"""
import fcntl
import os
@@ -170,7 +176,7 @@ def semantics(binary):
class Replica:
"""A replica's connection (src/detached/wire.zig): length-prefixed
messages, a tag byte and a little-endian u32 length."""
- WIRE = 14
+ WIRE = 15
def __init__(self, path, timeout=10):
self.s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
@@ -194,22 +200,34 @@ class Replica:
return tag, self.take(n)
def hello(self, session, build):
- """('welcome', pid, name) or ('refused', why)."""
+ """('welcome', pid, name) or ('refused', why); a welcome's dump
+ is read, whole."""
name = session.encode()
self.send(0x01, struct.pack('<HQH', self.WIRE, build, len(name)) + name)
tag, body = self.read()
if tag == 0x01:
pid, n = struct.unpack_from('<IH', body)
+ client, view, dump_len, gmtoff, digest = struct.unpack_from("<HBQiQ", body, 6 + n)
+ got = 0
+ while got < dump_len:
+ tag, piece = self.read()
+ assert tag == 0x04, tag
+ got += len(piece)
+ self.dump_len = dump_len
return 'welcome', pid, body[6:6 + n].decode()
assert tag == 0x02, (tag, body)
n, = struct.unpack_from('<H', body)
return 'refused', body[2:2 + n].decode()
def ping(self, value):
- """One round trip, in ms."""
+ """One round trip, in ms (the session's units on the way are
+ passed over)."""
t = time.monotonic()
self.send(0x03, struct.pack('<Q', value))
- tag, body = self.read()
+ while True:
+ tag, body = self.read()
+ if tag != 0x05:
+ break
assert (tag, body) == (0x03, struct.pack('<Q', value)), (tag, body)
return (time.monotonic() - t) * 1000
@@ -258,6 +276,126 @@ def ping(path, session, binary, n):
f'{rtt[0]:.2f}/{statistics.median(rtt):.2f}/{rtt[int(n * .9) - 1]:.2f}/{rtt[-1]:.2f} ms')
+def checks(path):
+ """A replica client's checks against its session (PARDES_REPLICA_CHECK)."""
+ try:
+ return path.read_text().splitlines()
+ except FileNotFoundError:
+ return []
+
+
+def agreed(path, after):
+ """A check after the first `after` agreed; one that differed fails."""
+ lines = checks(path)
+ bad = [line for line in lines if line.startswith('differ')]
+ assert not bad, (path.name, bad)
+ return len(lines) > after
+
+
+def clients(binary):
+ """Replica clients of a detached session: one on the replica socket,
+ one through the delay relay; each one's keys in the session's text and
+ every client's digest the session's once quiet; one killed outright
+ while the other types on; nothing left behind."""
+ scratch = Path(__file__).resolve().parent.parent.parent / '.scratch' / 'daemon' / 'dmn'
+ scratch.mkdir(parents=True, exist_ok=True)
+ root = Path(tempfile.mkdtemp(prefix='rc-', dir=scratch))
+ env = {k: v for k, v in os.environ.items() if not k.startswith('PARDES_') and 'PAGER' not in k}
+ env.update(HOME=str(root), XDG_RUNTIME_DIR=str(root), XDG_CONFIG_HOME=str(root / 'config'),
+ TERM='xterm-256color', SHELL=shutil.which('bash') or '/bin/bash', PARDES_NOTIME='1')
+ (root / 'config' / 'pardes').mkdir(parents=True)
+ (root / 'config' / 'pardes' / 'init').write_text('Shell bash\nMessageLinger 0\n')
+ notes = root / 'notes.txt'
+ notes.write_text(''.join(f'line {i} of the notes\n' for i in range(300)))
+ started = []
+ terms = []
+ try:
+ sess = subprocess.Popen([binary, '--detach=files', str(notes)], env=env, cwd=root, stdin=subprocess.DEVNULL,
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, start_new_session=True)
+ started.append(sess.pid)
+ sock = root / 'pardes-replica.sock'
+ wait_for('the replica socket', sock.exists)
+ wait_for('the session\'s 9P socket', (root / 'pardes-9p-files.sock').exists)
+ nine = Client(root / 'pardes-9p-files.sock')
+ pane = next(int(row.split()[0]) for row in nine.read('/index').splitlines() if row.split()[1] == b'text')
+ alone = fds(sess.pid)
+ relay = subprocess.Popen([sys.executable, '-B', str(Path(__file__).parent / 'delay_relay.py'),
+ str(root / 'relay.sock'), str(sock), '--delay', '40'],
+ stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
+ started.append(relay.pid)
+ wait_for('the relay', (root / 'relay.sock').exists)
+ ta, tb = Term(30, 100), Term(24, 80)
+ terms += [ta, tb]
+ a = ta.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'a.check')))
+ b = tb.spawn([binary, f'--attach={root / "relay.sock"}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'b.check')))
+ started += [a.pid, b.pid]
+ for t in (ta, tb):
+ wait_for('a client\'s first frame', lambda: b'line 3 of the notes' in t.text(), timeout=20)
+ os.write(ta.master, b'Ofrom-A\x1b')
+ os.write(tb.master, b'jjofrom-B\x1b')
+ wait_for('both clients\' keys in the session\'s text', lambda: b'from-A' in nine.read(f'/pane/{pane}/body')
+ and b'from-B' in nine.read(f'/pane/{pane}/body'))
+ wait_for('client A agrees once quiet', lambda: agreed(root / 'a.check', 0))
+ wait_for('client B agrees once quiet', lambda: agreed(root / 'b.check', 0))
+ # Killed outright: the session and the other client carry on.
+ os.kill(b.pid, signal.SIGKILL)
+ b.wait(timeout=10)
+ seen = len(checks(root / 'a.check'))
+ os.write(ta.master, b'jjjAagain-A\x1b')
+ wait_for('client A\'s keys after B went', lambda: b'again-A' in nine.read(f'/pane/{pane}/body'))
+ wait_for('client A agrees again', lambda: agreed(root / 'a.check', seen))
+ assert sess.poll() is None
+ # Detach typed in it: it alone goes.
+ os.write(ta.master, b' sD')
+ wait_for('client A let go', lambda: a.poll() is not None, timeout=10)
+ assert b'let this client go' in ta.text(), ta.text()[-300:]
+ # A third client, a shell: its keys wait for the session (a pty
+ # write), its output comes back in units, the same on both sides.
+ nine.write(f'/pane/{pane}/exec', b'Tty\n')
+ shell = wait_for('a shell pane', lambda: next((int(row.split()[0]) for row in nine.read('/index').splitlines()
+ if row.split()[1] == b'term'), None))
+ wait_for('the shell\'s prompt', lambda: b'$' in nine.read(f'/pane/{shell}/body') or b'#' in nine.read(f'/pane/{shell}/body'))
+ alone = fds(sess.pid)
+ tc = Term(30, 100)
+ terms.append(tc)
+ c = tc.spawn([binary, f'--attach={sock}:files'], dict(env, PARDES_REPLICA_CHECK=str(root / 'c.check')))
+ started.append(c.pid)
+ wait_for('client C\'s first frame', lambda: b'$' in tc.text(), timeout=20)
+ run(nine, shell, b'echo from-the-session\n')
+ os.write(tc.master, b'echo from-C\r')
+ wait_for('client C\'s keys in the shell', lambda: b'from-C' in nine.read(f'/pane/{shell}/body'))
+ wait_for('client C agrees with a shell\'s output', lambda: agreed(root / 'c.check', 0))
+ # Its terminal hung up: it dies, and the session carries on.
+ c.send_signal(signal.SIGHUP)
+ c.wait(timeout=10)
+ relay.kill()
+ relay.wait()
+ wait_for('the session\'s fds level', lambda: fds(sess.pid) == alone, timeout=10)
+ print(f'daemon: two replica clients (one through a 40 ms relay) typed into a detached session; '
+ f'digests agreed {len(checks(root / "a.check"))} and {len(checks(root / "b.check"))} times; '
+ f'one killed, the other typed on and detached; a third typed into a shell, agreed '
+ f'{len(checks(root / "c.check"))} times, and was hung up')
+ try:
+ nine.write(f'/pane/{pane}/exec', b'Exit\n')
+ except OSError as e:
+ # An edited file asks before it goes.
+ assert 'Exit again' in str(e), e
+ nine.write(f'/pane/{pane}/exec', b'Exit\n')
+ sess.wait(timeout=10)
+ nine.socket.close()
+ wait_for('nothing left', lambda: not [p for p in processes(root) if alive(p)], timeout=10)
+ print('daemon: replica clients\' session ended, nothing left behind')
+ finally:
+ for t in terms:
+ t.close()
+ for pid in started + processes(root):
+ try:
+ os.kill(pid, signal.SIGKILL)
+ except OSError:
+ pass
+ shutil.rmtree(root, ignore_errors=True)
+
+
def terminals(client):
return [int(row.split()[0]) for row in client.read('/index').splitlines() if row.split()[1] == b'term']
@@ -290,6 +428,7 @@ def main(binary, cycles):
daemon = daemons[0]
started.append(daemon)
replicas(root, binary, session, daemon)
+ clients(binary)
shell = terminals(client)[0]
shell_pid = int(run(client, shell, b'echo $$\n').split(b'\n')[1])
started.append(shell_pid)
@@ -856,6 +995,8 @@ if __name__ == '__main__':
gui_upgrades(Path(sys.argv[3]).resolve(), Path(sys.argv[4]).resolve())
elif sys.argv[1:2] == ['--upgrade']:
upgrades(Path(sys.argv[2]).resolve(), Path(sys.argv[3]).resolve())
+ elif sys.argv[1:2] == ['--clients']:
+ clients(sys.argv[2])
elif sys.argv[1:2] == ['--ping']:
ping(sys.argv[2], sys.argv[3], sys.argv[4], int(sys.argv[5]) if len(sys.argv) > 5 else 50)
elif '--gui' in sys.argv:
diff --git a/test/rollback_bench.zig b/test/rollback_bench.zig
index 38fe011d..0f0c77a4 100644
--- a/test/rollback_bench.zig
+++ b/test/rollback_bench.zig
@@ -158,6 +158,24 @@ pub fn main(init: std.process.Init) !void {
x.* = t1 - t0;
y.* = nowNs() - t1;
}
+ // A join (docs/next-daemon.md, Phase 5 part B notes): the session
+ // dumped at a step boundary, which is what the authority pays, and
+ // the joiner's load of it.
+ var dump: [5]u64 = undefined;
+ var load: [5]u64 = undefined;
+ var dump_len: usize = 0;
+ for (&dump, &load) |*x, *y| {
+ const t0 = nowNs();
+ const bytes = try pardes.dump.dumpBytes(core);
+ const t1 = nowNs();
+ dump_len = bytes.len;
+ const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null);
+ y.* = nowNs() - t1;
+ x.* = t1 - t0;
+ loaded.deinit();
+ core.gpa.free(bytes);
+ }
+ std.debug.print("{s:<8} join: dump {d:.1} MB in {d:.2} ms (the session's step), load {d:.2} ms (the joiner's)\n", .{ s.name, @as(f64, @floatFromInt(dump_len)) / 1e6, @as(f64, @floatFromInt(median(&dump))) * 1e-6, @as(f64, @floatFromInt(median(&load))) * 1e-6 });
var panes: usize = 0;
for (core.panes) |slot| panes += @intFromBool(slot != null);
const ms = 1e-6;