diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 05:58:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 10:44:19 -0300 |
| commit | ddb2554fff073eed0b73a43825b2c007c895bc4d (patch) | |
| tree | 77db8d82afd23dc6e0395bc4ba21525bc6c8dfcd /src | |
| parent | 541ebfa62ed966d74e1b7e93eb0d62e536d50e7d (diff) | |
| download | pardes-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')
| -rw-r--r-- | src/detached/wire.zig | 56 | ||||
| -rw-r--r-- | src/main.zig | 14 | ||||
| -rw-r--r-- | src/replica.zig | 857 | ||||
| -rw-r--r-- | src/tty/tty.zig | 69 | ||||
| -rw-r--r-- | src/walk.zig | 15 |
5 files changed, 944 insertions, 67 deletions
diff --git a/src/detached/wire.zig b/src/detached/wire.zig index 78f6e76e..2f2b95c8 100644 --- a/src/detached/wire.zig +++ b/src/detached/wire.zig @@ -9,11 +9,13 @@ const builtin = @import("builtin"); const pardes = @import("../pardes.zig"); const config = @import("pardes_config"); -/// 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 +/// 16: a far client says so, the session sends it its blobs' hashes before +/// the rest and only the blobs it asks for, all of it in zstd (phase 6's +/// content cache and zstd notes); 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; +pub const version: u16 = 16; /// 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 @@ -60,6 +62,11 @@ pub const ClientTag = enum(u8) { /// What is the session's digest, in stream order (a replica checks /// itself against it once quiet). check = 0x05, + /// A client's first word after its hello: whether it is far (through + /// ssh), so that it holds blobs by their hash and reads zstd. + far = 0x06, + /// The join's blobs a far client lacks, a bit each in `blobs` order. + want = 0x07, key = 0x10, mouse = 0x11, @@ -128,19 +135,28 @@ pub const ServerTag = enum(u8) { screen = 0x06, /// The session's digest (u64le), after every unit before it. check = 0x07, + /// Part of the join's list of blobs, to a far client: each one's place + /// in the state, its length and its BLAKE3 hash (src/replica.zig). + blobs = 0x08, + /// A piece of a far client's stream from here on, zstd-compressed: the + /// messages in it are framed as these are. Opaque here. + zstd = 0x09, }; pub const ServerMsg = union(ServerTag) { /// 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, semantics: u64 = 0 }, + /// guesses the session's time of day). A far client is told how many + /// blobs the dump holds (`blobs`). + welcome: struct { pid: u32, session: []const u8, client: u16 = 0, view: u8 = 0, dump_len: u64 = 0, gmtoff: i32 = 0, digest: u64 = 0, semantics: u64 = 0, blobs: u32 = 0 }, refused: []const u8, pong: u64, dump: []const u8, unit: []const u8, screen: []const u8, check: u64, + blobs: []const u8, + zstd: []const u8, }; /// A message's header, for a payload sent on its own (a unit, a dump's @@ -165,6 +181,7 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .gmtoff = try r.takeInt(i32, .little), .digest = try r.takeInt(u64, .little), .semantics = try r.takeInt(u64, .little), + .blobs = try r.takeInt(u32, .little), } }, .refused => .{ .refused = try r.take(try r.takeInt(u16, .little)) }, .pong => .{ .pong = try r.takeInt(u64, .little) }, @@ -172,6 +189,8 @@ pub fn decodeServer(tag: u8, payload: []const u8) Error!ServerMsg { .unit => return .{ .unit = payload }, .screen => return .{ .screen = payload }, .check => .{ .check = try r.takeInt(u64, .little) }, + .blobs => return .{ .blobs = payload }, + .zstd => return .{ .zstd = payload }, }; try end(&r); return msg; @@ -190,10 +209,11 @@ pub fn encodeServer(out: []u8, msg: ServerMsg) Error![]const u8 { try w.writeInt(i32, h.gmtoff, .little); try w.writeInt(u64, h.digest, .little); try w.writeInt(u64, h.semantics, .little); + try w.writeInt(u32, h.blobs, .little); }, .refused => |words| try putSlice16(&w, words), .pong => |v| try w.writeInt(u64, v, .little), - .dump, .unit, .screen => |bytes| try w.writeAll(bytes), + .dump, .unit, .screen, .blobs, .zstd => |bytes| try w.writeAll(bytes), .check => |v| try w.writeInt(u64, v, .little), } try finishMessage(&w, at); @@ -227,6 +247,8 @@ pub const ClientMsg = union(enum) { ping: u64, screen_ack: []const u8, check, + far: bool, + want: []const u8, event: pardes.Event, }; @@ -436,8 +458,9 @@ 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), + .screen_ack, .want => |bytes| try w.writeAll(bytes), .check => {}, + .far => |far| try w.writeByte(@intFromBool(far)), .event => |ev| switch (ev) { .key => |k| { try w.writeInt(u32, k.cp, .little); @@ -506,6 +529,8 @@ pub fn decodeClient(tag: u8, payload: []const u8) Error!ClientMsg { .ping => .{ .ping = try r.takeInt(u64, .little) }, .screen_ack => return .{ .screen_ack = payload }, .check => .check, + .far => .{ .far = try getBool(&r) }, + .want => return .{ .want = payload }, .key => blk: { const cp = try r.takeInt(u32, .little); // `Key.cp` is a u21, and the specials live in the private-use @@ -566,8 +591,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, .check => 0, - .screen_ack => |bytes| bytes.len, + .bye, .ping, .check, .far => 0, + .screen_ack, .want => |bytes| bytes.len, .event => |ev| switch (ev) { .mouse, .resize, .pdf_scroll, .pinch, .touch_scroll, .pointer_leave, .focus => 0, .key => |k| k.text.len, @@ -622,7 +647,7 @@ 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', 0, 0, 0, 0, 0, 0, 0, 0, 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); + 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, 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); @@ -703,12 +728,13 @@ test "detached wire: a session is never told to do a frontend's remembering" { // 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", + "hello", "bye", "ping", "screen_ack", "check", + // ...and how far it is, and which of the join's blobs it lacks. + "far", "want", // ...and everything a person can do to a window. - "key", "mouse", "resize", - "paste", "command", "pdf_scroll", - "pinch", "touch_scroll", "pointer_leave", - "focus", + "key", "mouse", "resize", + "paste", "command", "pdf_scroll", "pinch", "touch_scroll", + "pointer_leave", "focus", }; // One: the tag set is exactly that, named rather than counted, so diff --git a/src/main.zig b/src/main.zig index ce1c5bba..fafafc08 100644 --- a/src/main.zig +++ b/src/main.zig @@ -884,7 +884,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, } }; if (std.mem.indexOfScalar(u8, where, '/') != null) { - @import("tty/tty.zig").remote(init, opts, where, name) catch |err| Say.no(io, "{t}", .{err}); + @import("tty/tty.zig").remote(init, opts, where, name, false) catch |err| Say.no(io, "{t}", .{err}); std.process.exit(0); } // ssh -L <here>:<there> <host>, until the socket here answers. @@ -902,10 +902,12 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, var argv: [48:null]?[*:0]const u8 = @splat(null); var n: usize = 0; var words_buf: [1024]u8 = undefined; - // Compressed by default: the join's dump is text, and the FreeBSD box's - // link carries 170 to 400 KB/s (a 3.9 MB join: 7.8 s plain, 5.4 with - // -C). $PARDES_SSH, when set, is the whole command. - var words = std.mem.tokenizeScalar(u8, std.mem.span(std.c.getenv("PARDES_SSH") orelse "ssh -C"), ' '); + // Not `ssh -C`: what the session sends a far client is zstd already + // (replica.zig), and zlib over it costs both ends' time for nothing. + // ponytail: what the client sends goes as it is, keys and a paste + // alike; a big paste through a slow link is the ceiling, and would be + // an envelope of its own. $PARDES_SSH, when set, is the whole command. + 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", .{}); @@ -953,7 +955,7 @@ fn remoteAttach(init: std.process.Init, opts: pardes.Options, where: []const u8, 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| { + @import("tty/tty.zig").remote(init, opts, here, name, true) catch |err| { Ssh.end(); Say.no(io, "{t}", .{err}); }; diff --git a/src/replica.zig b/src/replica.zig index 2e14cb6f..9d12de70 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -29,6 +29,7 @@ const rollback = @import("rollback.zig"); const walk = @import("walk.zig"); const wire = @import("detached/wire.zig"); const screen_state = @import("screen_state.zig"); +const c_heap = @import("c_heap"); const terminal = pardes.panes.terminal; const Allocator = std.mem.Allocator; const Pardes = pardes.Pardes; @@ -1047,6 +1048,9 @@ const nosignal: u32 = if (builtin.os.tag == .linux) libc.MSG.NOSIGNAL else 0; /// PARDES_REPLICA_LAG_KB says another (a test's). var lag_max: usize = 64 << 20; const dump_piece = 1 << 20; +/// A blob in a far join's list (wire.zig `blobs`): its place in the state +/// (u64le), its length (u64le) and its BLAKE3 hash. +const blob_entry = 48; 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; @@ -1082,6 +1086,60 @@ fn writePieces(fd: c_int, bytes: []const u8) bool { return true; } +// ---- zstd (libzstd, vendored: build.zig) ---- + +/// A far client's stream from its welcome on (`Peer.farJoin`): its join in +/// one frame at level 6 over an 8 MiB window, since an undo snapshot is a +/// near copy of its file's text, a file's length before or after it (the +/// join's numbers: Phase 6's content cache and zstd notes); what follows in +/// a frame at level 3 over 1 MiB. Each envelope (wire.zig `zstd`) is a +/// flush of at most `piece` of input, so that a reader unpacks one whole. +const zstd = struct { + const CCtx = opaque {}; + const DCtx = opaque {}; + const In = extern struct { src: ?*const anyopaque, size: usize, pos: usize }; + const Out = extern struct { dst: ?*anyopaque, size: usize, pos: usize }; + const Mem = extern struct { + alloc: *const fn (?*anyopaque, usize) callconv(.c) ?*anyopaque, + free: *const fn (?*anyopaque, ?*anyopaque) callconv(.c) void, + ctx: ?*anyopaque = null, + }; + extern fn ZSTD_createCCtx_advanced(mem: Mem) ?*CCtx; + extern fn ZSTD_freeCCtx(c: ?*CCtx) usize; + extern fn ZSTD_CCtx_setParameter(c: *CCtx, param: c_int, value: c_int) usize; + extern fn ZSTD_compressStream2(c: *CCtx, out: *Out, in: *In, op: c_int) usize; + extern fn ZSTD_createDCtx_advanced(mem: Mem) ?*DCtx; + extern fn ZSTD_freeDCtx(d: ?*DCtx) usize; + extern fn ZSTD_decompressStream(d: *DCtx, out: *Out, in: *In) usize; + extern fn ZSTD_isError(code: usize) c_uint; + const level_param = 100; + const window_param = 101; + const Op = enum(c_int) { more = 0, flush = 1, end = 2 }; + const piece = 1 << 20; + + /// What zstd allocates is a Zig allocator's (c_heap): a test's own. + var gpa: Allocator = std.heap.c_allocator; + const heap = c_heap.Heap(&gpa); + fn alloc(_: ?*anyopaque, n: usize) callconv(.c) ?*anyopaque { + return heap.malloc(n); + } + fn free(_: ?*anyopaque, p: ?*anyopaque) callconv(.c) void { + heap.free(p); + } + const mem: Mem = .{ .alloc = alloc, .free = free }; + + fn compressor(level: c_int, window_log: c_int) ?*CCtx { + const c = ZSTD_createCCtx_advanced(mem) orelse return null; + if (ZSTD_isError(ZSTD_CCtx_setParameter(c, level_param, level)) != 0 or + ZSTD_isError(ZSTD_CCtx_setParameter(c, window_param, window_log)) != 0) + { + _ = ZSTD_freeCCtx(c); + return null; + } + return c; + } +}; + fn readSome(fd: c_int, buf: []u8) usize { while (true) { const n = libc.read(fd, buf.ptr, buf.len); @@ -1095,21 +1153,34 @@ fn readSome(fd: c_int, buf: []u8) usize { pub const Framer = struct { buf: std.ArrayListUnmanaged(u8) = .empty, len: usize = 0, + /// What zstd envelopes (wire.zig `zstd`) unpacked to, its messages not + /// taken yet, and the stream's decompressor: a far client's. + inner: std.ArrayListUnmanaged(u8) = .empty, + z: ?*zstd.DCtx = null, + bad: bool = false, pub fn deinit(f: *Framer, gpa: Allocator) void { f.buf.deinit(gpa); + f.inner.deinit(gpa); + if (f.z) |z| _ = zstd.ZSTD_freeDCtx(z); + f.z = null; } /// Framed messages off `fd` (detached/wire.zig) handed to `take` until - /// the stream ends, `take` says stop, or a message is malformed. + /// the stream ends, `take` says stop, or a message is malformed. An + /// envelope's are handed on as if they had come bare. fn run(fr: *Framer, gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { + if (fr.bad or !fr.drain(ctx, take)) return; 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 (f.tag == @backingInt(wire.ServerTag.zstd)) { + fr.bad = !fr.unpack(gpa, f.payload); + stop = fr.bad or !fr.drain(ctx, take); + } else 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]); @@ -1126,6 +1197,43 @@ pub const Framer = struct { fr.len += n; } } + + /// An envelope's bytes onto `inner`. Its writer flushed at most + /// `zstd.piece` of input into it, so it unpacks to no more than that. + fn unpack(fr: *Framer, gpa: Allocator, payload: []const u8) bool { + if (fr.z == null) fr.z = zstd.ZSTD_createDCtx_advanced(zstd.mem); + const z = fr.z orelse return false; + var in: zstd.In = .{ .src = payload.ptr, .size = payload.len, .pos = 0 }; + while (true) { + fr.inner.ensureUnusedCapacity(gpa, 256 << 10) catch return false; + const spare = fr.inner.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + if (zstd.ZSTD_isError(zstd.ZSTD_decompressStream(z, &out, &in)) != 0) return false; + fr.inner.items.len += out.pos; + if (in.pos == in.size and out.pos < out.size) return true; + } + } + + /// The whole messages in `inner` handed to `take`; false when it says + /// stop (what is left waits for the next run) or one is malformed. + fn drain(fr: *Framer, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) bool { + var at: usize = 0; + var stop = false; + while (!stop) { + const f = (wire.framed(fr.inner.items[at..]) catch { + fr.bad = true; + return false; + }) orelse break; + at += f.total; + stop = !take(ctx, f.tag, f.payload); + } + if (at > 0) { + const rest = fr.inner.items.len - at; + std.mem.copyForwards(u8, fr.inner.items[0..rest], fr.inner.items[at..]); + fr.inner.items.len = rest; + } + return !stop; + } }; fn readFramed(gpa: Allocator, fd: c_int, ctx: anytype, comptime take: fn (@TypeOf(ctx), u8, []const u8) bool) void { @@ -1163,9 +1271,25 @@ pub const Peer = struct { dump: []u8 = &.{}, blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty, dump_gpa: Allocator = undefined, + /// Its first word after its hello: whether it is far (through ssh), its + /// blobs asked for by hash and its stream in zstd (`farJoin`). + far: ?bool = null, + /// A far client's answer to its join's list: the blobs it lacks. + want: ?[]u8 = null, + /// A far client's stream's compressor, and an envelope's worth of its + /// output; the input since the last envelope. + z: ?*zstd.CCtx = null, + zout: std.ArrayListUnmanaged(u8) = .empty, + zin: usize = 0, fn writeJoin(peer: *Peer) bool { if (!writeAll(peer.fd, peer.welcome[0..peer.welcome_len])) return false; + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.far == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const far = peer.far; + peer.lock.unlock(io); + if (far orelse return false) return peer.farJoin(); var pos: usize = 0; for (peer.blobs.items) |b| { if (!writePieces(peer.fd, peer.dump[pos..b.at]) or !writePieces(peer.fd, b.bytes)) return false; @@ -1174,6 +1298,95 @@ pub const Peer = struct { return writePieces(peer.fd, peer.dump[pos..]); } + /// A far client's join, in one zstd frame: the list of the dump's blobs + /// (each one's place in the state, its length and its BLAKE3 hash), the + /// state, then the blobs it says it lacks; what follows the join goes + /// in a cheaper frame. The hashing is this thread's, never the step's. + /// ponytail: a blob is hashed again at every far join, 3.6 GB/s; a + /// session's join after its first would keep them, beside its words. + fn farJoin(peer: *Peer) bool { + const gpa = peer.peers.gpa; + peer.z = zstd.compressor(6, 23) orelse return false; + var list: std.ArrayListUnmanaged(u8) = .empty; + defer list.deinit(gpa); + for (peer.blobs.items) |b| { + var entry: [blob_entry]u8 = undefined; + std.mem.writeInt(u64, entry[0..8], b.at, .little); + std.mem.writeInt(u64, entry[8..16], b.bytes.len, .little); + std.crypto.hash.Blake3.hash(b.bytes, entry[16..48], .{}); + list.appendSlice(gpa, &entry) catch return false; + if (list.items.len + blob_entry > dump_piece) { + if (!peer.zMessage(.blobs, list.items)) return false; + list.clearRetainingCapacity(); + } + } + if (list.items.len > 0 and !peer.zMessage(.blobs, list.items)) return false; + if (!peer.zPieces(peer.dump) or !peer.zWrite(&.{}, .flush)) return false; + if (peer.blobs.items.len > 0) { + const io = peer.peers.io; + peer.lock.lockUncancelable(io); + while (peer.want == null and !peer.closing and !peer.cut) peer.cond.waitUncancelable(io, &peer.lock); + const want = peer.want; + peer.lock.unlock(io); + const bits = want orelse return false; + if (bits.len != (peer.blobs.items.len + 7) / 8) return false; + for (peer.blobs.items, 0..) |b, i| if (bits[i / 8] >> @intCast(i % 8) & 1 != 0) { + if (!peer.zPieces(b.bytes)) return false; + }; + } + if (!peer.zWrite(&.{}, .end)) return false; + _ = zstd.ZSTD_freeCCtx(peer.z); + peer.z = zstd.compressor(3, 20); + return peer.z != null; + } + + /// One message into its stream's compressor. + fn zMessage(peer: *Peer, tag: wire.ServerTag, payload: []const u8) bool { + const head = wire.header(@backingInt(tag), payload.len); + return peer.zWrite(&head, .more) and peer.zWrite(payload, .more); + } + + /// `bytes` as dump pieces (1 MiB at most each), into the compressor. + fn zPieces(peer: *Peer, bytes: []const u8) bool { + var at: usize = 0; + while (at < bytes.len) : (at += dump_piece) { + if (!peer.zMessage(.dump, bytes[at..@min(bytes.len, at + dump_piece)])) return false; + } + return true; + } + + /// `bytes` into its stream's compressor, and what comes out written as + /// envelopes: one at each `zstd.piece` of input, and one now if `op` + /// says flush or end (the frame's). + fn zWrite(peer: *Peer, bytes: []const u8, op: zstd.Op) bool { + const z = peer.z orelse return false; + var rest = bytes; + while (true) { + const n = @min(rest.len, zstd.piece - peer.zin); + const full = peer.zin + n == zstd.piece; + const now: zstd.Op = if (n < rest.len or (full and op == .more)) .flush else op; + var in: zstd.In = .{ .src = rest.ptr, .size = n, .pos = 0 }; + while (true) { + peer.zout.ensureUnusedCapacity(peer.peers.gpa, 128 << 10) catch return false; + const spare = peer.zout.unusedCapacitySlice(); + var out: zstd.Out = .{ .dst = spare.ptr, .size = spare.len, .pos = 0 }; + const left = zstd.ZSTD_compressStream2(z, &out, &in, @backingInt(now)); + if (zstd.ZSTD_isError(left) != 0) return false; + peer.zout.items.len += out.pos; + if (if (now == .more) in.pos == in.size else left == 0) break; + } + peer.zin += n; + if (now != .more) { + const head = wire.header(@backingInt(wire.ServerTag.zstd), peer.zout.items.len); + if (!writeAll(peer.fd, &head) or !writeAll(peer.fd, peer.zout.items)) return false; + peer.zout.clearRetainingCapacity(); + peer.zin = 0; + } + rest = rest[n..]; + if (rest.len == 0) return true; + } + } + /// `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. @@ -1220,7 +1433,7 @@ pub const Peer = struct { // 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); + const ok = if (peer.z != null) peer.zWrite(peer.sending.items, .flush) else writeAll(peer.fd, peer.sending.items); peer.sending.clearRetainingCapacity(); if (!ok) { _ = libc.shutdown(peer.fd, 2); @@ -1251,6 +1464,23 @@ pub const Peer = struct { peer.peers.post(.{ .check = peer }); return true; }, + // What its join's writer waits for (`writeJoin`, `farJoin`). + .far, .want => { + const io = peer.peers.io; + const msg = wire.decodeClient(tag, payload) catch return false; + const bits = if (msg == .want) peer.peers.gpa.dupe(u8, msg.want) catch return false else null; + peer.lock.lockUncancelable(io); + defer peer.lock.unlock(io); + if (bits) |b| { + if (peer.want != null) { + peer.peers.gpa.free(b); + return false; + } + peer.want = b; + } else peer.far = msg.far; + peer.cond.signal(io); + return true; + }, else => {}, } peer.seq += 1; @@ -1569,7 +1799,7 @@ pub const Peers = struct { if (ps.next_id == 0) ps.next_id = 1; // The session's digest as dumped, for the joiner to check its load by. const digest = tape.digestOf(core); - peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics } }) catch unreachable).len; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = @intCast(libc.getpid()), .session = ps.name[0..@min(ps.name.len, wire.max_name)], .client = peer.id, .view = k, .dump_len = whole, .gmtoff = gmtoff(), .digest = digest, .semantics = wire.semantics, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; 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) { @@ -1618,6 +1848,9 @@ pub const Peers = struct { _ = libc.close(peer.fd); peer.out.deinit(ps.gpa); peer.sending.deinit(ps.gpa); + if (peer.want) |b| ps.gpa.free(b); + if (peer.z) |z| _ = zstd.ZSTD_freeCCtx(z); + peer.zout.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; @@ -1772,12 +2005,24 @@ pub const Joined = struct { semantics: u64 = 0, /// What was read past the dump: the stream's (`readStream`). rest: Framer = .{}, + /// A far join's blobs that came over the link, for `Have.store` (the C + /// heap's), and how it went: its blobs, those it held already (from the + /// core it left, from disk) and the bytes of the ones it was sent. + got: []Got = &.{}, + blobs: u32 = 0, + from_memory: u32 = 0, + from_disk: u32 = 0, + sent_bytes: u64 = 0, }; +/// A blob a far join was sent: where it is in the dump, and its hash. +pub const Got = struct { pos: usize, len: usize, hash: [32]u8 }; + /// 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 { +/// A far client (`have`) is sent its dump's blobs by hash first, and asks +/// for those it holds nowhere. Errors are said in `why`. +pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8, have: ?*Have) ?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) }; @@ -1790,24 +2035,51 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J _ = libc.close(fd); return said(why, "nothing answers at {s}", .{path}); } - var out: [wire.hello_max]u8 = undefined; + return joinOn(gpa, fd, session, why, have); +} + +/// `dial`'s, on a connection made: the hello, then the join. The +/// connection is closed if the join fails. +fn joinOn(gpa: Allocator, fd: c_int, session: []const u8, why: []u8, have: ?*Have) ?Joined { + var out: [wire.hello_max + 16]u8 = undefined; const hello = wire.encodeClient(&out, .{ .hello = .{ .session = session } }) catch { _ = libc.close(fd); return said(why, "that session's name is too long", .{}); }; + // Its first word, in the same write: whether it is far. + const far = wire.encodeClient(out[hello.len..], .{ .far = have != null }) catch unreachable; 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, + have: ?*Have, failed: ?[]const u8 = null, welcomed: bool = false, + done: bool = false, + /// The join's list of blobs (`blob_entry` each), as it comes. + list: std.ArrayListUnmanaged(u8) = .empty, + /// Where what comes fills the dump, in order (each a place and a + /// length): the state's runs between the blobs, then each blob + /// asked for. A blob listed twice is copied once the dump is whole. + runs: std.ArrayListUnmanaged([2]usize) = .empty, + run: usize = 0, + run_at: usize = 0, + dups: std.ArrayListUnmanaged([3]usize) = .empty, + + fn deinit(r: *@This()) void { + r.list.deinit(r.gpa); + r.runs.deinit(r.gpa); + r.dups.deinit(r.gpa); + } + + fn fail(r: *@This(), words: []const u8) bool { + r.failed = words; + return 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; - }; + const msg = wire.decodeServer(tag, payload) catch return r.fail("the session's answer was not understood"); switch (msg) { .welcome => |w| { r.j.pid = w.pid; @@ -1816,42 +2088,109 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J r.j.gmtoff = w.gmtoff; r.j.digest = w.digest; r.j.semantics = w.semantics; - 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.j.dump = r.gpa.alloc(u8, @intCast(w.dump_len)) catch return r.fail("out of memory for the session's dump"); r.welcomed = true; - return w.dump_len > 0; + r.j.blobs = if (r.have != null) w.blobs else 0; + // A near client's dump comes whole, and a far one's + // without blobs is its state. + if (r.j.blobs == 0) r.runs.append(r.gpa, .{ 0, r.j.dump.len }) catch return r.fail("out of memory for the session's dump"); + r.done = w.dump_len == 0; + return !r.done; }, .refused => |words| { r.failed = std.fmt.bufPrint(r.why, "{s}\x00", .{words}) catch "refused"; return false; }, + .blobs => |entries| { + const whole = @as(usize, r.j.blobs) * blob_entry; + if (r.runs.items.len > 0 or r.list.items.len + entries.len > whole) return r.fail("the session sent more of its blobs' list than it said"); + r.list.appendSlice(r.gpa, entries) catch return r.fail("out of memory for the session's blobs' list"); + if (r.list.items.len == whole) return r.lacking(); + return true; + }, .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; + if (r.runs.items.len == 0) return r.fail("the session sent its dump before its blobs' list"); + var p = piece; + while (p.len > 0) { + if (r.run == r.runs.items.len) return r.fail("the session sent more dump than it said"); + const run = r.runs.items[r.run]; + const n = @min(p.len, run[1] - r.run_at); + @memcpy(r.j.dump[run[0] + r.run_at ..][0..n], p[0..n]); + r.run_at += n; + p = p[n..]; + if (r.run_at == run[1]) { + r.run += 1; + r.run_at = 0; + } } - @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"; + if (r.run < r.runs.items.len) return true; + for (r.dups.items) |d| @memcpy(r.j.dump[d[0]..][0..d[2]], r.j.dump[d[1]..][0..d[2]]); + r.done = true; return false; }, + else => return r.fail("the session sent a unit before its dump"), } } + + /// The list whole: each blob put in place from what the client + /// holds (`Have.find`), or asked for, and the runs to fill made. + fn lacking(r: *@This()) bool { + const h = r.have.?; + const dump = r.j.dump; + const n: usize = r.j.blobs; + const bits = r.gpa.alloc(u8, (n + 7) / 8) catch return r.fail("out of memory for the session's blobs' list"); + defer r.gpa.free(bits); + @memset(bits, 0); + var got: std.ArrayListUnmanaged(Got) = .empty; + defer got.deinit(std.heap.c_allocator); + var firsts: std.AutoHashMapUnmanaged([32]u8, [2]usize) = .empty; + defer firsts.deinit(r.gpa); + var shift: usize = 0; + var end: usize = 0; + for (0..n) |i| { + const e = r.list.items[i * blob_entry ..][0..blob_entry]; + const len = std.math.cast(usize, std.mem.readInt(u64, e[8..16], .little)) orelse return r.fail("the session's list of blobs is malformed"); + const pos = std.math.add(usize, std.math.cast(usize, std.mem.readInt(u64, e[0..8], .little)) orelse maxInt, shift) catch maxInt; + if (pos < end or pos > dump.len or len > dump.len - pos) return r.fail("the session's list of blobs is malformed"); + shift += len; + if (pos > end) r.runs.append(r.gpa, .{ end, pos - end }) catch return r.fail("out of memory for the session's blobs' list"); + end = pos + len; + const hash = e[16..48]; + const first = firsts.getOrPut(r.gpa, hash.*) catch return r.fail("out of memory for the session's blobs' list"); + if (first.found_existing and first.value_ptr[1] == len) { + r.dups.append(r.gpa, .{ pos, first.value_ptr[0], len }) catch return r.fail("out of memory for the session's blobs' list"); + continue; + } + first.value_ptr.* = .{ pos, len }; + if (h.find(hash, dump[pos..][0..len])) |where| switch (where) { + .memory => r.j.from_memory += 1, + .disk => r.j.from_disk += 1, + } else { + bits[i / 8] |= @as(u8, 1) << @intCast(i % 8); + got.append(std.heap.c_allocator, .{ .pos = pos, .len = len, .hash = hash.* }) catch return r.fail("out of memory for the session's blobs' list"); + r.j.sent_bytes += len; + } + } + if (dump.len > end) r.runs.append(r.gpa, .{ end, dump.len - end }) catch return r.fail("out of memory for the session's blobs' list"); + for (got.items) |g| r.runs.append(r.gpa, .{ g.pos, g.len }) catch return r.fail("out of memory for the session's blobs' list"); + const head = wire.header(@backingInt(wire.ClientTag.want), bits.len); + if (!writeAll(r.j.fd, &head) or !writeAll(r.j.fd, bits)) return r.fail("the session hung up"); + r.j.got = got.toOwnedSlice(std.heap.c_allocator) catch return r.fail("out of memory for the session's blobs' list"); + return true; + } + const maxInt = std.math.maxInt(usize); }; - if (!writeAll(fd, hello)) { + if (!writeAll(fd, out[0 .. hello.len + far.len])) { _ = libc.close(fd); return said(why, "the session hung up", .{}); } - var r: Read = .{ .j = &j, .gpa = gpa, .why = why }; + var r: Read = .{ .j = &j, .gpa = gpa, .why = why, .have = have }; + defer r.deinit(); j.rest.run(gpa, fd, &r, Read.take); - if (r.failed != null or !r.welcomed or r.got < j.dump.len) { + if (r.failed != null or !r.done) { j.rest.deinit(gpa); if (j.dump.len > 0) gpa.free(j.dump); + std.heap.c_allocator.free(j.got); _ = libc.close(fd); const text = r.failed orelse "the session hung up before the dump was whole"; if (text.ptr == why.ptr) return null; @@ -1860,6 +2199,251 @@ pub fn dial(gpa: Allocator, path: []const u8, session: []const u8, why: []u8) ?J return j; } +// ---- a far client's blobs ---- + +/// What a far client holds of a join's blobs already, by content (`dial`): +/// the cache on disk, and on a rejoin the blobs of the core it leaves. +pub const Have = struct { + io: std.Io, + cache: ?Cache = null, + /// The core it leaves (`leaving`), each blob hashed once when one of its + /// length is looked for. ponytail: a scan a lookup, fine to some + /// thousands of blobs; a map by length would take over past that. + old: std.ArrayListUnmanaged([]const u8) = .empty, + old_hash: std.ArrayListUnmanaged(?[32]u8) = .empty, + /// The thread writing the last join's blobs to disk (`store`). + storing: ?std.Thread = null, + + pub fn init(io: std.Io) Have { + return .{ .io = io, .cache = Cache.open(io) }; + } + + pub fn deinit(h: *Have) void { + h.wait(); + h.left(); + h.old.deinit(std.heap.c_allocator); + h.old_hash.deinit(std.heap.c_allocator); + if (h.cache) |c| c.dir.close(h.io); + } + + fn wait(h: *Have) void { + if (h.storing) |t| t.join(); + h.storing = null; + } + + /// `core`'s blobs, looked at by the next join until `left`: a rejoin + /// takes what it held, by content, before the core goes. + pub fn leaving(h: *Have, core: *const Pardes) void { + h.left(); + walk.blobs(Pardes, core, &h.old); + h.old_hash.appendNTimes(std.heap.c_allocator, null, h.old.items.len) catch h.old.clearRetainingCapacity(); + } + + pub fn left(h: *Have) void { + h.old.clearRetainingCapacity(); + h.old_hash.clearRetainingCapacity(); + } + + pub const Where = enum { memory, disk }; + + /// The blob of `hash` put in `out` (its length), from the core it + /// leaves or the cache; null when it holds it nowhere. + fn find(h: *Have, hash: *const [32]u8, out: []u8) ?Where { + for (h.old.items, h.old_hash.items) |b, *word| { + if (b.len != out.len) continue; + if (word.* == null) { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + word.* = d; + } + if (!std.mem.eql(u8, &word.*.?, hash)) continue; + @memcpy(out, b); + return .memory; + } + if (h.cache) |c| if (c.get(h.io, hash, out)) return .disk; + return null; + } + + /// The blobs a join was sent, written to the cache on a thread of their + /// own, which frees the dump (`gpa`'s) and the list after: the first + /// frame waits for none of it. + pub fn store(h: *Have, gpa: Allocator, j: *Joined) void { + const dump = j.dump; + const got = j.got; + j.dump = &.{}; + j.got = &.{}; + h.wait(); + const Store = struct { + fn run(c: Cache, io: std.Io, a: Allocator, bytes: []u8, list: []Got) void { + defer { + a.free(bytes); + std.heap.c_allocator.free(list); + } + var incoming: u64 = 0; + for (list) |g| incoming += g.len; + c.trim(io, @min(incoming, c.cap)); + var put: u64 = 0; + for (list) |g| { + if (put + g.len > c.cap) continue; + put += g.len; + c.put(io, &g.hash, bytes[g.pos..][0..g.len]); + } + } + }; + const c = h.cache orelse { + gpa.free(dump); + std.heap.c_allocator.free(got); + return; + }; + h.storing = std.Thread.spawn(.{}, Store.run, .{ c, h.io, gpa, dump, got }) catch { + Store.run(c, h.io, gpa, dump, got); + return; + }; + } + + /// `core`'s blobs the cache lacks, written there: what this client made + /// since it joined (an edit's text, its undo), for its next start. + pub fn keep(h: *Have, core: *const Pardes) void { + h.wait(); + const c = h.cache orelse return; + var all: std.ArrayListUnmanaged([]const u8) = .empty; + defer all.deinit(std.heap.c_allocator); + walk.blobs(Pardes, core, &all); + var missing: std.ArrayListUnmanaged(struct { bytes: []const u8, hash: [32]u8 }) = .empty; + defer missing.deinit(std.heap.c_allocator); + var incoming: u64 = 0; + for (all.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b, &d, .{}); + if (c.has(h.io, &d, b.len)) continue; + missing.append(std.heap.c_allocator, .{ .bytes = b, .hash = d }) catch return; + incoming += b.len; + } + if (missing.items.len == 0) return; + c.trim(h.io, @min(incoming, c.cap)); + var put: u64 = 0; + for (missing.items) |m| { + if (put + m.bytes.len > c.cap) continue; + put += m.bytes.len; + c.put(h.io, &m.hash, m.bytes); + } + } +}; + +/// A far client's blobs on disk, a file each named by its BLAKE3 hash in +/// hex, in $XDG_CACHE_HOME/pardes/blobs (else ~/.cache/pardes/blobs): what +/// a client started again, or a second one on this machine, is not sent +/// again. The cache directory and not the runtime one: the blobs are worth +/// keeping across a logout or a reboot and safe to lose, and they can be +/// hundreds of megabytes, which the runtime directory (a tmpfs, often +/// capped at a tenth of memory and emptied at logout) is not for. A file +/// read back is hashed again: one cut short, corrupted or planted is a +/// miss and goes, never wrong content. +pub const Cache = struct { + dir: std.Io.Dir, + /// Bytes it keeps at most: PARDES_BLOB_CACHE_MB, else 1 GiB. + cap: u64, + + pub fn open(io: std.Io) ?Cache { + var buf: [4096]u8 = undefined; + const base = if (std.c.getenv("XDG_CACHE_HOME")) |x| std.mem.span(x) else ""; + const path = if (base.len > 0 and base[0] == '/') + std.fmt.bufPrint(&buf, "{s}/pardes/blobs", .{base}) catch return null + else if (std.c.getenv("HOME")) |home| + std.fmt.bufPrint(&buf, "{s}/.cache/pardes/blobs", .{std.mem.span(home)}) catch return null + else + return null; + const cwd = std.Io.Dir.cwd(); + cwd.createDirPath(io, std.fs.path.dirname(path).?) catch return null; + cwd.createDir(io, path, .fromMode(0o700)) catch |err| if (err != error.PathAlreadyExists) return null; + const mb = if (std.c.getenv("PARDES_BLOB_CACHE_MB")) |v| std.fmt.parseInt(u64, std.mem.span(v), 10) catch 1024 else 1024; + return .{ .dir = cwd.openDir(io, path, .{ .iterate = true }) catch return null, .cap = mb << 20 }; + } + + fn name(hash: *const [32]u8) [64]u8 { + return std.fmt.bytesToHex(hash.*, .lower); + } + + /// The blob of `hash` read into `out` (its length) and hashed again. + /// A file of another length or content is a miss, and goes; a hit's + /// time is made now, so the least recently used go first (`trim`). + pub fn get(c: Cache, io: std.Io, hash: *const [32]u8, out: []u8) bool { + const file = name(hash); + var f = c.dir.openFile(io, &file, .{}) catch return false; + const ok = ok: { + defer f.close(io); + const st = f.stat(io) catch break :ok false; + if (st.size != out.len) break :ok false; + var reader = f.reader(io, &.{}); + const n = reader.interface.readSliceShort(out) catch break :ok false; + if (n != out.len) break :ok false; + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(out, &d, .{}); + break :ok std.mem.eql(u8, &d, hash); + }; + if (!ok) { + c.dir.deleteFile(io, &file) catch {}; + return false; + } + c.dir.setTimestampsNow(io, &file, .{}) catch {}; + return true; + } + + /// Whether a file of `len` bytes is there under `hash` (not read). + pub fn has(c: Cache, io: std.Io, hash: *const [32]u8, len: usize) bool { + const file = name(hash); + const st = c.dir.statFile(io, &file, .{}) catch return false; + return st.size == len; + } + + var tmp_serial: std.atomic.Value(u32) = .init(0); + + /// `bytes` kept under `hash`: written aside and renamed into place, so + /// that a reader never meets half a file (and would hash it wrong). + pub fn put(c: Cache, io: std.Io, hash: *const [32]u8, bytes: []const u8) void { + const file = name(hash); + if (c.has(io, hash, bytes.len)) return; + var buf: [96]u8 = undefined; + const tmp = std.fmt.bufPrint(&buf, ".tmp-{d}-{d}-{s}", .{ std.c.getpid(), tmp_serial.fetchAdd(1, .monotonic), file[0..16] }) catch return; + c.dir.writeFile(io, .{ .sub_path = tmp, .data = bytes }) catch { + c.dir.deleteFile(io, tmp) catch {}; + return; + }; + c.dir.rename(tmp, c.dir, &file, io) catch c.dir.deleteFile(io, tmp) catch {}; + } + + /// Room for `incoming` more bytes under the cap, the least recently + /// used files deleted first (a hit makes its file's time now). + /// ponytail: the directory scanned and sorted at each join's store; + /// fine to tens of thousands of blobs, where an index would take over. + pub fn trim(c: Cache, io: std.Io, incoming: u64) void { + const File = struct { name: [80]u8, len: u8, size: u64, mtime: i96 }; + var files: std.ArrayListUnmanaged(File) = .empty; + defer files.deinit(std.heap.c_allocator); + var total: u64 = 0; + var it = c.dir.iterate(); + while (it.next(io) catch null) |entry| { + if (entry.kind != .file or entry.name.len > 80) continue; + const st = c.dir.statFile(io, entry.name, .{}) catch continue; + var f: File = .{ .name = undefined, .len = @intCast(entry.name.len), .size = st.size, .mtime = st.mtime.nanoseconds }; + @memcpy(f.name[0..f.len], entry.name); + files.append(std.heap.c_allocator, f) catch return; + total += st.size; + } + if (total + incoming <= c.cap) return; + std.mem.sort(File, files.items, {}, struct { + fn older(_: void, a: File, b: File) bool { + return a.mtime < b.mtime; + } + }.older); + for (files.items) |f| { + if (total + incoming <= c.cap) break; + c.dir.deleteFile(io, f.name[0..f.len]) catch continue; + total -= f.size; + } + } +}; + fn said(why: []u8, comptime fmt: []const u8, args: anytype) ?Joined { _ = std.fmt.bufPrint(why, fmt ++ "\x00", args) catch {}; return null; @@ -2207,3 +2791,218 @@ test "a join's dump, its blobs put back where they go, is the session's dump" { try joined.appendSlice(gpa, state[pos..]); try std.testing.expectEqualSlices(u8, whole, joined.items); } + +test "a cached blob is hashed again as it is read: one cut short, corrupted, planted or longer is a miss, and goes" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 1 << 20 }; + var text: [6000]u8 = undefined; + for (&text, 0..) |*b, i| b.* = @truncate(i *% 31 +% i / 7); + var hash: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(&text, &hash, .{}); + const file = Cache.name(&hash); + var out: [6000]u8 = undefined; + try std.testing.expect(!c.get(io, &hash, &out)); + c.put(io, &hash, &text); + try std.testing.expect(c.has(io, &hash, text.len)); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); + var corrupted = text; + corrupted[4000] ^= 0x20; + const planted: [6000]u8 = @splat('x'); + var longer: [6001]u8 = undefined; + @memcpy(longer[0..6000], &text); + longer[6000] = '\n'; + for ([_][]const u8{ text[0..3000], &corrupted, &planted, &longer }) |bad| { + try tmp.dir.writeFile(io, .{ .sub_path = &file, .data = bad }); + @memset(&out, 0); + try std.testing.expect(!c.get(io, &hash, &out)); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, &file, .{})); + } + // ...and put back, it is read again. + c.put(io, &hash, &text); + try std.testing.expect(c.get(io, &hash, &out)); + try std.testing.expectEqualSlices(u8, &text, &out); +} + +test "the blob cache stays under its cap, the least recently used going first" { + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + const c: Cache = .{ .dir = tmp.dir, .cap = 3 * 5000 }; + var blobs: [3][5000]u8 = undefined; + var hashes: [3][32]u8 = undefined; + for (&blobs, &hashes, 0..) |*b, *h, k| { + @memset(b, @intCast('a' + k)); + std.crypto.hash.Blake3.hash(b, h, .{}); + c.put(io, h, b); + // Each older than the next, a thousand seconds apart. + const name = Cache.name(h); + try tmp.dir.setTimestamps(io, &name, .{ .modify_timestamp = .{ .new = .{ .nanoseconds = @as(i96, @intCast(k + 1)) * 1000 * std.time.ns_per_s } } }); + } + // The oldest read: it is the newest now. + var out: [5000]u8 = undefined; + try std.testing.expect(c.get(io, &hashes[0], &out)); + c.trim(io, 5000); + try std.testing.expect(c.has(io, &hashes[0], 5000)); + try std.testing.expect(!c.has(io, &hashes[1], 5000)); + try std.testing.expect(c.has(io, &hashes[2], 5000)); + // A file not a blob's counts, and goes by its time as well. + const junk: [9000]u8 = @splat(0); + try tmp.dir.writeFile(io, .{ .sub_path = ".tmp-1-1-0123456789abcdef", .data = &junk }); + try tmp.dir.setTimestamps(io, ".tmp-1-1-0123456789abcdef", .{ .modify_timestamp = .{ .new = .{ .nanoseconds = 500 * std.time.ns_per_s } } }); + c.trim(io, 5000); + try std.testing.expectError(error.FileNotFound, tmp.dir.statFile(io, ".tmp-1-1-0123456789abcdef", .{})); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Under the cap with room to spare: nothing goes. + c.trim(io, 0); + try std.testing.expect(c.has(io, &hashes[0], 5000) and c.has(io, &hashes[2], 5000)); + // Room for the whole cap: everything goes, the older first. + c.trim(io, c.cap); + try std.testing.expect(!c.has(io, &hashes[0], 5000) and !c.has(io, &hashes[2], 5000)); +} + +/// A far join over a socket pair, the session's writer and reader threads +/// against the client's `joinOn`, as the daemon hands a connection over: +/// its hello read off first. +fn farJoinOnce(ps: *Peers, core: *Pardes, have: ?*Have) !Joined { + var fds: [2]c_int = undefined; + if (libc.socketpair(libc.AF.UNIX, libc.SOCK.STREAM | libc.SOCK.CLOEXEC, 0, &fds) != 0) return error.NoSocket; + var blobs: std.ArrayListUnmanaged(walk.Deferred) = .empty; + const state = pardes.dump.dumpJoin(core, &blobs) catch |err| { + _ = libc.close(fds[0]); + _ = libc.close(fds[1]); + return err; + }; + var whole = state.len; + for (blobs.items) |b| whole += b.bytes.len; + const peer = try ps.gpa.create(Peer); + peer.* = .{ .peers = ps, .fd = fds[0], .id = 1, .view = 0, .dump = state, .blobs = blobs, .dump_gpa = core.gpa }; + peer.welcome_len = (wire.encodeServer(&peer.welcome, .{ .welcome = .{ .pid = 1, .session = "t", .dump_len = whole, .blobs = @intCast(blobs.items.len) } }) catch unreachable).len; + const Serve = struct { + fn run(p: *Peer) void { + var hello: [wire.header_len + 13]u8 = undefined; + var got: usize = 0; + while (got < hello.len) { + const n = libc.read(p.fd, hello[got..].ptr, hello.len - got); + if (n <= 0) return p.peers.post(.{ .gone = p }); + got += @intCast(n); + } + p.readLoop(); + } + }; + peer.writer = try std.Thread.spawn(.{}, Peer.writeLoop, .{peer}); + peer.reader = try std.Thread.spawn(.{}, Serve.run, .{peer}); + defer ps.letGo(peer); + var why: [300]u8 = @splat(0); + return joinOn(std.testing.allocator, fds[1], "t", &why, have) orelse { + std.debug.print("the join failed: {s}\n", .{std.mem.sliceTo(&why, 0)}); + return error.JoinFailed; + }; +} + +fn endJoin(j: *Joined, have: ?*Have) void { + _ = libc.close(j.fd); + j.rest.deinit(std.testing.allocator); + if (have) |h| { + h.store(std.testing.allocator, j); + h.wait(); + } else { + std.testing.allocator.free(j.dump); + std.heap.c_allocator.free(j.got); + } +} + +test "a far join is sent each blob once: joined again with them on disk or in the core it leaves, the state alone; a cached file gone bad, that blob again" { + const gpa = std.testing.allocator; + const io = std.testing.io; + zstd.gpa = gpa; + defer zstd.gpa = std.heap.c_allocator; + const a = try Authority.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml" }, TestHost.host(), false); + defer a.deinit(); + const text = try gpa.alloc(u8, 30000); + defer gpa.free(text); + for (text, 0..) |*b, i| b.* = if (i % 61 == 60) '\n' else @intCast('a' + (i * 7 + i / 13) % 26); + _ = try a.local(.{ .key = .{ .cp = 'i', .text = "i" } }); + _ = try a.local(.{ .paste = text }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + _ = try a.local(.{ .key = .{ .cp = 'o', .text = "o" } }); + _ = try a.local(.{ .paste = text[0..9000] }); + _ = try a.local(.{ .key = .{ .cp = pardes.Key.escape, .text = "" } }); + const was = Stepping.begin(&a.t, null); + defer was.end(); + const whole = try pardes.dump.dumpBytes(a.core); + defer a.core.gpa.free(whole); + // What the session's list will say: its blobs, those of one text once. + var listed: std.ArrayListUnmanaged(walk.Deferred) = .empty; + defer listed.deinit(a.core.gpa); + a.core.gpa.free(try pardes.dump.dumpJoin(a.core, &listed)); + var distinct: std.AutoHashMapUnmanaged([32]u8, usize) = .empty; + defer distinct.deinit(gpa); + var distinct_bytes: u64 = 0; + var biggest: [32]u8 = undefined; + var biggest_len: usize = 0; + for (listed.items) |b| { + var d: [32]u8 = undefined; + std.crypto.hash.Blake3.hash(b.bytes, &d, .{}); + if ((try distinct.getOrPut(gpa, d)).found_existing) continue; + distinct_bytes += b.bytes.len; + if (b.bytes.len > biggest_len) { + biggest = d; + biggest_len = b.bytes.len; + } + } + try std.testing.expect(distinct.count() >= 3); + + var tmp = std.testing.tmpDir(.{ .iterate = true }); + defer tmp.cleanup(); + var have: Have = .{ .io = io, .cache = .{ .dir = tmp.dir, .cap = 64 << 20 } }; + defer { + have.cache = null; + have.deinit(); + } + var ps: Peers = .{ .gpa = gpa, .io = io }; + defer ps.deinit(); + + // A near client: the dump whole, as before. + var j = try farJoinOnce(&ps, a.core, null); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(@as(u32, 0), j.blobs); + endJoin(&j, null); + // The first far join: every blob sent, once. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(listed.items.len, j.blobs); + try std.testing.expectEqual(@as(u32, 0), j.from_disk + j.from_memory); + try std.testing.expectEqual(distinct_bytes, j.sent_bytes); + endJoin(&j, &have); + // Again: all of them on disk. + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_disk); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); + // The biggest's file corrupted: it alone is sent again. + const name = Cache.name(&biggest); + const junk = try gpa.alloc(u8, biggest_len); + defer gpa.free(junk); + @memset(junk, 'z'); + try tmp.dir.writeFile(io, .{ .sub_path = &name, .data = junk }); + j = try farJoinOnce(&ps, a.core, &have); + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count() - 1, j.from_disk); + try std.testing.expectEqual(@as(u64, biggest_len), j.sent_bytes); + endJoin(&j, &have); + // A rejoin, no disk: what the core it leaves holds. + const cache = have.cache; + have.cache = null; + have.leaving(a.core); + j = try farJoinOnce(&ps, a.core, &have); + have.left(); + have.cache = cache; + try std.testing.expectEqualSlices(u8, whole, j.dump); + try std.testing.expectEqual(distinct.count(), j.from_memory); + try std.testing.expectEqual(@as(u64, 0), j.sent_bytes); + endJoin(&j, &have); +} 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 {}; } } diff --git a/src/walk.zig b/src/walk.zig index 3556ce88..1c5bbbfc 100644 --- a/src/walk.zig +++ b/src/walk.zig @@ -111,6 +111,10 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { const b = blob_words.get(addr) orelse break :kept null; break :kept if (b.len == items.len) b.word else null; } else null; + if (blobs_out) |list| { + list.append(std.heap.c_allocator, std.mem.sliceAsBytes(items)) catch {}; + break :word 0; + } if (!rehash) if (kept) |w| break :word w; if (E == u8) if (unhashed_out) |list| if (kept == null) { list.append(std.heap.c_allocator, items) catch {}; @@ -136,6 +140,17 @@ fn hashBlob(comptime E: type, items: []const E, h: *Hasher) void { /// Where `unhashed` notes the texts a digest would hash for want of a word. threadlocal var unhashed_out: ?*std.ArrayListUnmanaged([]const u8) = null; +/// Where `blobs` notes every blob, hashing none. +threadlocal var blobs_out: ?*std.ArrayListUnmanaged([]const u8) = null; + +/// Every blob of `v` a digest would take as one (`blob_min` bytes and up), +/// as its bytes, onto `out` (the C heap's), hashed by none: a replica +/// client's, which knows its blobs by content (src/replica.zig `Have`). +pub fn blobs(comptime T: type, v: *const T, out: *std.ArrayListUnmanaged([]const u8)) void { + blobs_out = out; + defer blobs_out = null; + _ = digest(T, v); +} /// The blob texts a digest of `v` would hash now, having no word yet, onto /// `out` (the C heap's): a walk with no blob hashed, which `makeWords` then |
