diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 00:51:28 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 02:25:18 -0300 |
| commit | f8bc2dfb5c26b034643b22232624defe87517c20 (patch) | |
| tree | 61200173e62b2cd9bf4006819511b17acfb8d6c9 /test/replicas.zig | |
| parent | 4bcf2055cb97d0c69fc94643b3e43b579d6fa30c (diff) | |
| download | pardes-f8bc2dfb5c26b034643b22232624defe87517c20.tar.gz pardes-f8bc2dfb5c26b034643b22232624defe87517c20.zip | |
test/replicas: --bytes shows kitty images by direct data, a file, a temporary file and shared memory at the end of the flood, twice: answered (ids 5 to 8, each OK counted once on the session's pty, with the query's) and kept (ids 15 to 18, q=1: the soak's tty echoes an answer back as output, and an echoed `ESC _ G i=5;OK ESC \` is a failed transmission of image 5 that takes it away), the kept shared-memory image 600x600 RGBA so its read crosses by hash; at quiescence every client's kept images hashed pixel for pixel against the session's, and every temporary file and object gone (the session's read deleted them, no client read them); the file image stored only (a=t) before, which no client loaded, goes; --clients 5 --bytes --seeds 8 at 30/80/207 ms (load 11): diverged 0, unequal 0, quiet checks 1513/0 1120/0 1691/0, 84/92/92 kitty images in clients at quiescence, each the session's pixels, corpus 22 of 22, no leak, stops off may_wait 1/0/0 (seed 2 at 30 ms, a client's key d stopped at the host's `now`: Del's question reads the host clock, exec.clockNow; the same stop once in the 4-seed run; the core change alone under the old soak: 0 over 8 seeds); the screen-state soak beside it: diverged 0, unequal 0, deltas 1874/0 1937/0 1828/0, quiet 597/0 673/0 792/0, stops off may_wait 0, corpus 22 of 22
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test/replicas.zig')
| -rw-r--r-- | test/replicas.zig | 141 |
1 files changed, 118 insertions, 23 deletions
diff --git a/test/replicas.zig b/test/replicas.zig index 70edea75..4994d9a5 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -246,6 +246,9 @@ const Totals = struct { /// Deltas the replicas applied while the flood ran. flood_deltas: u64 = 0, flood_us: u64 = 0, + /// Kitty images a client held at quiescence, the session's pixels + /// (`--bytes`, `images`). + images_equal: u64 = 0, flood_delta_bytes: u64 = 0, flood_unit_bytes: u64 = 0, fetched: u64 = 0, @@ -795,9 +798,13 @@ const Soak = struct { /// encoding cost. framed: bool = false, encode_ns: u64 = 0, - /// The session's pty told the answer to the program's kitty query - /// (`questions`): once, whatever the clients. - kitty_answers: u32 = 0, + /// The session's pty told the answers to the program's kitty query and + /// its four images (`questions`, `images`, `kitty_oks`): each once, whatever the + /// clients. + kitty_answers: [kitty_oks.len]u32 = @splat(0), + /// The temporary file and the shared memory object an image was shown + /// by (`images`): gone once the session read them. + media: *const Media = undefined, /// A Peer's queue, swapped out as its writer swaps it (the Peers' /// allocator's). sending: std.ArrayListUnmanaged(u8) = .empty, @@ -818,7 +825,9 @@ const Soak = struct { const s = of(ctx); // Only the session's emulator answers its program: a client's // answers are its tape's, never written (`questions`). - if (std.mem.indexOf(u8, bytes, "\x1b_Gi=31;OK") != null) s.kitty_answers += 1; + for (kitty_oks, &s.kitty_answers) |ok, *n| if (std.mem.indexOf(u8, bytes, ok) != null) { + n.* += 1; + }; s.echo.append(gpa, .{ .pane = pane, .bytes = gpa.dupe(u8, bytes) catch return }) catch {}; } @@ -1469,10 +1478,16 @@ const Soak = struct { for (s.flood_deltas[0..s.clients.len]) |d| totals.flood_deltas += d; const want = pardes.tape.digestOf(core); if (bytes_kind) std.debug.print("seed {d} rtt {d} ms: a join's step at worst {d} us (the session's reloads and its dump), the last dump {d} KB ({d} KB of state)\n", .{ s.r.seed, s.r.rtt_ms, s.l.peers.join_ns / 1000, s.l.peers.dump_bytes >> 10, s.l.peers.state_bytes >> 10 }); - if (bytes_kind and s.kitty_answers != 1) { + if (bytes_kind) for (kitty_oks, s.kitty_answers) |ok, n| if (n != 1) { totals.unequal += 1; - std.debug.print("seed {d} rtt {d} ms: the program's kitty query was answered {d} times on the session's pty\n", .{ s.r.seed, s.r.rtt_ms, s.kitty_answers }); - } + std.debug.print("seed {d} rtt {d} ms: the program's kitty {f} was answered {d} times on the session's pty\n", .{ s.r.seed, s.r.rtt_ms, std.ascii.hexEscape(ok, .lower), n }); + }; + if (bytes_kind) for (&s.media.temp, &s.media.object) |*temp, *object| { + for ([_][:0]const u8{ temp, object }, [_]bool{ libc.access(temp, libc.F_OK) == 0, shm.shm_open(object, @bitCast(libc.O{ .ACCMODE = .RDONLY }), 0) >= 0 }) |name, left| if (left) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: {s}, an image's medium, is still there\n", .{ s.r.seed, s.r.rtt_ms, name }); + }; + }; for (s.clients, 1..) |*cl, id| { totals.resizes += cl.resizes; totals.wakes += cl.wakes; @@ -1499,7 +1514,8 @@ const Soak = struct { totals.unequal += 1; std.debug.print("seed {d} rtt {d} ms: client {d} at quiescence: {s}\n", .{ s.r.seed, s.r.rtt_ms, id, if (rp.quiet()) "another digest" else "inputs still pending" }); } - // At quiescence, every terminal the session's, cell for cell. + // At quiescence, every terminal the session's, cell for cell, + // and its kitty images the session's, pixel for pixel. for (core.panes, 0..) |slot, pid| { const pane = slot orelse continue; const state = pane.terminal orelse continue; @@ -1511,6 +1527,13 @@ const Soak = struct { totals.unequal += 1; std.debug.print("seed {d} rtt {d} ms: client {d}'s terminal {d} at quiescence is not the session's\n", .{ s.r.seed, s.r.rtt_ms, id, pid }); } + const theirs = imagesHash(state); + if (!std.mem.eql(u64, &theirs, &imagesHash(mine))) { + totals.unequal += 1; + std.debug.print("seed {d} rtt {d} ms: client {d}'s kitty images in terminal {d} are not the session's\n", .{ s.r.seed, s.r.rtt_ms, id, pid }); + } else for (theirs) |h| { + if (h != 0) totals.images_equal += 1; + } } // ...and every file's text the session's, byte for byte: a digest // takes a text of 4 KiB and up by its length (core/walk.zig). @@ -1586,11 +1609,20 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { defer gpa.free(tui); const session_only = try screen_state.shellStream(gpa); defer gpa.free(session_only); - const asked = if (bytes_kind) try questions(dir) else ""; - defer if (bytes_kind) gpa.free(asked); - const shell_session = try std.mem.concat(gpa, u8, &.{ session_only, asked }); + const shell_session = try std.mem.concat(gpa, u8, &.{ session_only, if (bytes_kind) questions else "" }); defer gpa.free(shell_session); - const yes = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); + var media: Media = undefined; + const shown = if (bytes_kind) try images(dir, r.seed, &media) else ""; + defer if (bytes_kind) { + gpa.free(shown); + for (&media.temp, &media.object) |*temp, *object| { + _ = libc.unlink(temp); + _ = shm.shm_unlink(object); + } + }; + const yes_only = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); + defer gpa.free(yes_only); + const yes = try std.mem.concat(gpa, u8, &.{ yes_only, shown }); defer gpa.free(yes); var soak: Soak = .{ @@ -1606,27 +1638,90 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { .yes = yes, .own_left = r.steps, .typing = r.clients + 1, + .media = &media, }; // The session: THE session loop, from its start to its end. try SessionLoop.run(init, .{ .cols = 100, .rows = 30, .boot = .tty }, &soak); if (soak.err) |err| return err; } +/// What `questions`'s kitty query and `images`'s four are answered with. +const kitty_oks = [_][]const u8{ "\x1b_Gi=31;OK", "\x1b_Gi=5;OK", "\x1b_Gi=6;OK", "\x1b_Gi=7;OK", "\x1b_Gi=8;OK" }; + +/// The temporary files and the shared memory objects `images` shows its +/// images by, which the session's emulator removes when it reads them. +const Media = struct { temp: [2][300:0]u8, object: [2][64:0]u8 }; + +const shm = struct { + extern "c" fn shm_open(name: [*:0]const u8, flag: c_int, mode: libc.mode_t) c_int; + extern "c" fn shm_unlink(name: [*:0]const u8) c_int; + extern "c" fn ftruncate(fd: c_int, len: i64) c_int; +}; + +/// The pixels of each of `images`'s kept images a terminal holds, hashed +/// (0: not held). +fn imagesHash(state: *pardes.Term.State) [4]u64 { + var out: [4]u64 = @splat(0); + const screen = pardes.Term.emulatorOf(state).vt.screens.active; + for (&out, 15..) |*h, id| if (screen.kitty_images.imageById(@intCast(id))) |img| { + h.* = std.hash.Wyhash.hash(1, img.data.bytes() orelse continue); + }; + return out; +} + /// What the shell's program asks its terminal at the end of its session /// (`--bytes`): device attributes, the cursor's place, the cell's size in -/// pixels, a kitty query; then a kitty image by direct data, shown, and one -/// by a file of the session's machine, stored (a=t), which no client loads -/// (P32): absent there, as every live terminal's image is under screen -/// state. Shown (a=T) it would move the session's cursor past it and not a -/// client's: a divergence until the file's bytes are taped (a later step). -fn questions(dir: []const u8) ![]u8 { +/// pixels, a kitty query. +const questions = "\x1b[c\x1b[6n\x1b[16t\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\r\n$ "; + +/// What the flood's program shows at its end (`--bytes`; after it, so the +/// clients still hold them at quiescence): kitty images shown (a=T) by +/// direct data, by a file of the session's machine, by a temporary file +/// (which its reader deletes) and by shared memory (which its reader +/// unlinks), twice: answered (ids 5 to 8, `kitty_oks`), and kept (ids 15 +/// to 18, q=1: the soak's tty echoes an answer back as output, and an +/// echoed `ESC _ G i=5;OK` is a transmission of image 5 that fails and +/// takes it away), the kept one by shared memory 1.4 MB, so its read +/// crosses to a client by hash. Each medium is the session's to read and +/// every client is handed its bytes (Term.media): each image in every +/// client, pixel for pixel; the program answered once; each temporary file +/// and object gone once. +fn images(dir: []const u8, seed: u64, media: *Media) ![]u8 { var path_buf: [300]u8 = undefined; const path = try std.fmt.bufPrintSentinel(&path_buf, "{s}/pixels.rgb", .{dir}, 0); try writeText(path, &@as([12]u8, @splat(0x7f))); - var b64: [400]u8 = undefined; - const name = std.base64.standard.Encoder.encode(&b64, path); - return std.fmt.allocPrint(gpa, "\x1b[c\x1b[6n\x1b[16t\x1b_Gi=31,s=1,v=1,a=q,t=d,f=24;AAAA\x1b\\\r\n" ++ - "\x1b_Ga=T,i=5,f=24,s=2,v=2;AAAAAAAAAAAAAAAA\x1b\\\r\n\x1b_Ga=t,i=6,t=f,f=24,s=2,v=2;{s}\x1b\\\r\n$ ", .{name}); + var out: std.ArrayListUnmanaged(u8) = .empty; + errdefer out.deinit(gpa); + var b64: [3][400]u8 = undefined; + const enc = std.base64.standard.Encoder; + for (0..2, [_]u32{ 2, 600 }, [_][]const u8{ "", ",q=1" }) |round, side, quiet| { + const temp = try std.fmt.bufPrintSentinel(&media.temp[round], "{s}/tty-graphics-protocol-{d}-{d}.rgb", .{ dir, seed, round }, 0); + try writeText(temp, &@as([12]u8, @splat(0x3f))); + const object = try std.fmt.bufPrintSentinel(&media.object[round], "/pardes-soak-{d}-{d}-{d}", .{ libc.getpid(), seed, round }, 0); + const fd = shm.shm_open(object, @bitCast(libc.O{ .ACCMODE = .RDWR, .CREAT = true, .TRUNC = true }), 0o600); + if (fd < 0) return error.ShmOpenFailed; + defer _ = libc.close(fd); + // Sized first: FreeBSD's write does not grow a shared memory object. + if (shm.ftruncate(fd, side * side * 4) != 0) return error.WriteFailed; + var row: [600 * 4]u8 = undefined; + for (0..side) |y| { + for (row[0 .. side * 4], 0..) |*c, i| c.* = @truncate(i * 7 + y * 13 + seed); + if (libc.write(fd, &row, side * 4) != side * 4) return error.WriteFailed; + } + const id = 5 + 10 * round; + try out.print(gpa, "\x1b_Ga=T,i={d},f=24,s=2,v=2{s};AAAAAAAAAAAAAAAA\x1b\\\r\n\x1b_Ga=T,i={d},t=f,f=24,s=2,v=2{s};{s}\x1b\\\r\n" ++ + "\x1b_Ga=T,i={d},t=t,f=24,s=2,v=2{s};{s}\x1b\\\r\n\x1b_Ga=T,i={d},t=s,f=32,s={d},v={d},c=8,r=4{s};{s}\x1b\\\r\n", .{ + id, quiet, + id + 1, quiet, + enc.encode(&b64[0], path), id + 2, + quiet, enc.encode(&b64[1], temp), + id + 3, side, + side, quiet, + enc.encode(&b64[2], object), + }); + } + try out.appendSlice(gpa, "$ "); + return out.toOwnedSlice(gpa); } /// A typist: keys 50-200 ms apart, a pause now and then. @@ -2838,7 +2933,7 @@ pub fn main(init: std.process.Init) !void { var t: Totals = .{}; pardes.Term.reloads = .{}; for (first..first + seeds) |seed| try run(init, .{ .seed = seed, .rtt_ms = rtt, .steps = steps, .clients = clients, .flood_mb = flood_mb }, dir, &t); - if (bytes_kind) std.debug.print("rtt {d}: {d} reloads of a terminal at joins, every side (the session's own too), {d} us each, {d} us at worst\n", .{ rtt, pardes.Term.reloads.n, pardes.Term.reloads.ns / @max(1, pardes.Term.reloads.n) / 1000, pardes.Term.reloads.worst_ns / 1000 }); + if (bytes_kind) std.debug.print("rtt {d}: {d} reloads of a terminal at joins, every side (the session's own too), {d} us each, {d} us at worst; {d} kitty images in clients at quiescence, each the session's pixels\n", .{ rtt, pardes.Term.reloads.n, pardes.Term.reloads.ns / @max(1, pardes.Term.reloads.n) / 1000, pardes.Term.reloads.worst_ns / 1000, t.images_equal }); const rollbacks = t.mispredicted + t.foreign; std.debug.print("{d:>4} {d:>6} {d:>7} {d:>7} {d:>6} {d:>6} {d:>6} ({d}+{d}) {d:>5.1}% {d:>6} {d:>5}/{d:<6} {d:>5}/{d:<5} ({d}) {d:>7}/{d:<7} {d:>8} {d:>7}\n", .{ rtt, t.inputs, t.speculated, |
