summaryrefslogtreecommitdiff
path: root/src/tty
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-30 05:58:13 -0300
committerGabriel Schneider <[email protected]>2026-10-30 10:44:19 -0300
commitddb2554fff073eed0b73a43825b2c007c895bc4d (patch)
tree77db8d82afd23dc6e0395bc4ba21525bc6c8dfcd /src/tty
parent541ebfa62ed966d74e1b7e93eb0d62e536d50e7d (diff)
downloadpardes-ddb2554fff073eed0b73a43825b2c007c895bc4d.tar.gz
pardes-ddb2554fff073eed0b73a43825b2c007c895bc4d.zip
replica: a far client's content cache and zstd: the session lists its join's blobs by BLAKE3 hash first (wire 16 `blobs`), the client finds each in the core it leaves or on disk ($XDG_CACHE_HOME/pardes/blobs, hashed again on load, a bad file a miss, the least recently used evicted past 1 GiB) and asks for the rest (`want`), and everything after the welcome goes in zstd envelopes (level 6 over 8 MiB for the join, an undo snapshot being a near copy of its text, then level 3), so `ssh -C` leaves the default command; a far client (through ssh, or PARDES_REPLICA_FAR) keeps what it was sent and, at its end, what it made; a hangup ends a remote client the normal way; through the box a client holding the blobs is drawn in 2.4 s where main took 7.0 to 11.9, and a first join of a session with undo in 7.1 s where main took 11.9; libzstd 1.5.7 vendored, its allocations on c_heap, a second copy refused by shared-libs-check; tests for the cache's verify-on-load, its eviction and a far join's blobs sent once; test/daemon.py's far clients
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src/tty')
-rw-r--r--src/tty/tty.zig69
1 files changed, 52 insertions, 17 deletions
diff --git a/src/tty/tty.zig b/src/tty/tty.zig
index 994ebd92..2007c6f7 100644
--- a/src/tty/tty.zig
+++ b/src/tty/tty.zig
@@ -1309,12 +1309,17 @@ const Remote = struct {
test_diverge: u32 = 0,
/// Put off so in this join: its checks are not written (they differ).
put_off: bool = false,
+ /// How its last join went (replica.Joined's counts), and how long its
+ /// dial and load took (PARDES_REPLICA_STATS).
+ join: struct { blobs: u32 = 0, from_memory: u32 = 0, from_disk: u32 = 0, sent_bytes: u64 = 0 } = .{},
+ join_ms: u64 = 0,
/// The replica the dial's dump makes, checked against the session as
- /// it was dumped; null, and why in `why`. The dump is let go.
- fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8) ?*replica.Replica {
+ /// it was dumped; null, and why in `why`. The dump is let go, a far
+ /// join's to the thread that keeps the blobs it was sent (`Have.store`).
+ fn load(gpa: std.mem.Allocator, joined: *replica.Joined, o: pardes.Options, why: []u8, have: ?*replica.Have) ?*replica.Replica {
defer {
- gpa.free(joined.dump);
+ if (have) |h| h.store(gpa, joined) else gpa.free(joined.dump);
joined.dump = &.{};
}
const r = replica.Replica.initDump(gpa, joined.client, joined.view, joined.dump, o) catch |err| {
@@ -1358,10 +1363,12 @@ const Remote = struct {
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,
+ const line = std.fmt.bufPrint(&buf, "inputs {d} at_once {d} stopped {d} units {d} fast {d} neutral {d} mispredicted {d} foreign {d} visible {d} rtt_us {d} spec_us {d}/{d} copy_us {d}/{d} rollback_us {d}/{d} join_ms {d} blobs {d} from_memory {d} from_disk {d} sent_kb {d}\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, c.join_ms, c.join.blobs, c.join.from_memory, c.join.from_disk,
+ c.join.sent_bytes >> 10,
}) catch return;
var z: [4096]u8 = undefined;
const pz = std.fmt.bufPrintSentinel(&z, "{s}", .{path}, 0) catch return;
@@ -1446,7 +1453,7 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo
l.postEvent(.{ .refused = bytes }) catch std.heap.c_allocator.free(bytes);
return false;
},
- .pong, .welcome, .dump => {},
+ .pong, .welcome, .dump, .blobs, .zstd => {},
}
return true;
}
@@ -1461,7 +1468,7 @@ fn linkReader(gpa: std.mem.Allocator, fd: posix.fd_t, rest: *replica.Framer, loo
/// 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 {
+pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, session: []const u8, far_link: bool) !void {
const io = init.io;
const gpa = init.gpa;
const Said = struct {
@@ -1472,7 +1479,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
}
};
var why: [600]u8 = @splat(0);
- var joined = replica.dial(gpa, path, session, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
+ // Far (through ssh, or PARDES_REPLICA_FAR=1): its join's blobs by hash,
+ // those it holds kept from the link, and its stream in zstd. A client
+ // on this machine's own socket is sent the dump whole, as before: a
+ // hash and a compression there cost more than the bytes.
+ const far = if (init.environ_map.get("PARDES_REPLICA_FAR")) |v| std.mem.eql(u8, v, "1") else far_link;
+ var have_store: replica.Have = if (far) .init(io) else .{ .io = io };
+ defer have_store.deinit();
+ const have: ?*replica.Have = if (far) &have_store else null;
+ var dial_t0 = std.Io.Clock.awake.now(io).nanoseconds;
+ var joined = replica.dial(gpa, path, session, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
if (joined.semantics != wire.semantics) Said.no(io, "{s}", .{upgrade_words});
const allocs = pardes.memory.init(gpa);
@@ -1488,10 +1504,16 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
var o = opts;
o.image_allocator = allocs.image;
o.pdf_allocator = allocs.pdf;
- var r = Remote.load(gpa, &joined, o, &why) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
+ var r = Remote.load(gpa, &joined, o, &why, have) orelse Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
+ // Its join's time, the dial and the load (PARDES_REPLICA_STATS).
+ var dial_ms: u64 = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms));
var winch_set = posix.sigemptyset();
posix.sigaddset(&winch_set, posix.SIG.WINCH);
+ // A hangup ends it the normal way (`quit`), what it made kept on disk
+ // (`Have.keep`) and its ssh forward's socket unlinked (main.zig).
+ posix.sigaddset(&winch_set, posix.SIG.HUP);
+ hangup_quits = true;
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);
@@ -1539,6 +1561,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
sh.terms[joined.view] = &screen;
screen.feed.view = joined.view;
link.reset(r, joined.fd);
+ link.join = .{ .blobs = joined.blobs, .from_memory = joined.from_memory, .from_disk = joined.from_disk, .sent_bytes = joined.sent_bytes };
+ link.join_ms = dial_ms;
var reader_done: std.atomic.Value(bool) = .init(false);
const reader = try std.Thread.spawn(.{}, linkReader, .{ gpa, joined.fd, &joined.rest, &loop, &reader_done });
// Its size is its view's first input.
@@ -1610,6 +1634,8 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
std.heap.c_allocator.free(words);
link.refused = null;
} else {
+ // What it made since it joined, kept for its next start.
+ if (have) |h| h.keep(r.core());
r.deinit();
if (link.gone) {
screen.leave();
@@ -1617,13 +1643,15 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
}
return;
}
- r.deinit();
- // The session again: an upgrade's takes the time its exec and load
+ // The session again, the core it leaves held meanwhile: its blobs
+ // are not sent again. An upgrade's takes the time its exec and load
// take, and its daemon's.
+ if (have) |h| h.leaving(r.core());
var tries: u32 = 0;
+ dial_t0 = std.Io.Clock.awake.now(io).nanoseconds;
joined = while (true) : (tries += 1) {
why = @splat(0);
- if (replica.dial(gpa, path, session, &why)) |j| {
+ if (replica.dial(gpa, path, session, &why, have)) |j| {
if (j.semantics == wire.semantics) break j;
replica.bye(j.fd);
_ = libc.close(j.fd);
@@ -1645,10 +1673,13 @@ pub fn remote(init: std.process.Init, opts: pardes.Options, path: []const u8, se
const ts: libc.timespec = .{ .sec = 0, .nsec = 100 * std.time.ns_per_ms };
_ = libc.nanosleep(&ts, null);
};
- r = Remote.load(gpa, &joined, o, &why) orelse {
+ if (have) |h| h.left();
+ r.deinit();
+ r = Remote.load(gpa, &joined, o, &why, have) orelse {
screen.leave();
Said.no(io, "{s}", .{std.mem.sliceTo(&why, 0)});
};
+ dial_ms = @intCast(@divTrunc(std.Io.Clock.awake.now(io).nanoseconds - dial_t0, std.time.ns_per_ms));
}
}
@@ -3453,16 +3484,20 @@ fn watchFiles(io: std.Io, fd: c_int, loop: *Loop) anyerror!void {
fn keepSignal(_: posix.SIG) callconv(.c) void {}
+/// A replica client's (`remote`): a hangup is its end, whoever sent it.
+var hangup_quits = false;
+
fn winchWatch(loop: *Loop) void {
var set = posix.sigemptyset();
posix.sigaddset(&set, posix.SIG.WINCH);
- if (daemon.link >= 0) posix.sigaddset(&set, posix.SIG.HUP);
+ if (daemon.link >= 0 or hangup_quits) posix.sigaddset(&set, posix.SIG.HUP);
while (true) {
var sig: c_int = 0;
if (libc.sigwait(&set, &sig) != 0) continue;
// The terminals are the loop's: it asks each its size, and takes a
// hangup as the end of the one that hung up.
- loop.postEvent(if (sig == @backingInt(posix.SIG.HUP)) .hangup else .winched) catch {};
+ const ev: @TypeOf(Command.value) = if (sig != @backingInt(posix.SIG.HUP)) .winched else if (hangup_quits) .quit else .hangup;
+ loop.postEvent(ev) catch {};
}
}