diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-20 01:08:45 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-20 01:55:33 -0300 |
| commit | daee60fa42540864a89357a28b73033158158425 (patch) | |
| tree | 4f5ec7a5d3ef7b28939b7ad3941867ce457e114d /test | |
| parent | b55b92456883e3fda113651f53e5fa2bc4a37304 (diff) | |
| download | pardes-daee60fa42540864a89357a28b73033158158425.tar.gz pardes-daee60fa42540864a89357a28b73033158158425.zip | |
test/replicas: P32's far case in the soak (step 3, item 3): the third client's link 1 Mbit/s and 200 ms each way, what it carries counted as the far stream's zstd makes it (level 3, a join's dump 6), at most 64 KiB of it ahead of what has crossed and the rest in the session's queue for it, as a writer blocked on its socket leaves it (each Peer's writer the soak's own now: a batch at a time, a wake when the link has room, a cut's words then the link's end); the flood `yes` then as much of a `cat` of this file's ASCII lines at 1 MB/s (8 s); the units' heads carry the soak's clock (the session's here; the clients guess no clock from them, in process); measured and printed: the far client's joins, the seconds between its refreshes and from the flood's end to its final screen (a run that never shows it fails), the session's queue for it at most, clients cut off, and what a join's reloads cost the replicas already there (the session's step, each client's unit, ns of this process's clock). THE FAULT, on the tree before Link.behind (bytes, seed 0, 80 ms): the far client never lets go: one join, the session's queue for it 6.5 MB, its final screen 17.7 s after the flood's end; diverged 0, unequal 0
Co-Authored-By: Claude Opus <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T
Diffstat (limited to 'test')
| -rw-r--r-- | test/replicas.zig | 346 |
1 files changed, 285 insertions, 61 deletions
diff --git a/test/replicas.zig b/test/replicas.zig index 70edea75..637af0b3 100644 --- a/test/replicas.zig +++ b/test/replicas.zig @@ -13,8 +13,8 @@ //! are refused until one is let go. //! //! Terminals cross as screen state (core/term/screen_state.zig): the session's -//! shell pane runs a full-screen program and then a `yes` flood (`--flood` -//! MB of it) while the clients type, its output stepped by the authority +//! shell pane runs a full-screen program and then a flood (`--flood` MB of +//! `yes`, then as much of a `cat` of source text) while the clients type, its output stepped by the authority //! and sent to each replica as deltas, once a frame and before any input //! the authority steps, each replica acknowledging them over its link and //! asking for the rows it scrolls back into and does not hold. @@ -178,6 +178,10 @@ const Kind = enum { /// session's down it. up, blob, + /// The session's words in a client's stream (a cut's: `Peer.send`). + word, + /// Room in the far link again: its Peer's writer goes on (`Soak.write`). + drain, }; const Msg = struct { @@ -201,6 +205,8 @@ const Msg = struct { const Link = struct { last: u64 = 0, + /// When what was sent down it before has crossed (`Net.cross`). + free: u64 = 0, /// When a message sent at `now` arrives: half the RTT, jitter on top, /// and never before the one sent ahead of it (a stream keeps order). fn arrive(l: *Link, random: std.Random, now: u64, one_way: u64, jitter: u64) u64 { @@ -269,6 +275,20 @@ const Totals = struct { detach_words: u64 = 0, /// Panes of each plain kind the session opened (image, life). kind_panes: [3]u64 = @splat(0), + /// The far client (`throttle_*`): its joins from a dump, the time between + /// two from the flood's start on, and from the flood's end to the first + /// frame of its final screen (us); the most the session's queue for it + /// held (bytes); the clients the session cut off. + far_joins: u64 = 0, + far_gaps: std.ArrayListUnmanaged(u64) = .empty, + far_final: std.ArrayListUnmanaged(u64) = .empty, + queue_max: u64 = 0, + cut: u64 = 0, + /// What a join costs the replicas already there, ns of this process's + /// clock: the session's join step (its reloads and its dump), and each + /// client's unit that reloaded its terminals. + join_session_ns: std.ArrayListUnmanaged(u64) = .empty, + join_client_ns: std.ArrayListUnmanaged(u64) = .empty, fn add(t: *Totals, s: *const replica.Stats) !void { t.inputs += s.inputs; @@ -506,6 +526,17 @@ const Client = struct { distrusted: pardes.Addon.Distrusted = .{}, /// What stopped its inputs as last judged (`judgeStops`). stops: @FieldType(replica.Stats, "stops") = @splat(.{}), + /// Its Peer's writer's (`Soak.write`): the batch swapped out of the + /// session's queue for it (the Peers' allocator's), how much of it went + /// down the link, whether a cut's words end it, a wake asked for room; + /// how much of the queue carries the soak's clock (`Soak.stamp`). + sending: std.ArrayListUnmanaged(u8) = .empty, + at: usize = 0, + last: bool = false, + draining: bool = false, + marked: usize = 0, + /// The session's unit its last join's dump came after. + join_seq: u32 = 0, /// Where it is (`clients[c]`), the net its wakes go through, and the /// soak's time now. c: usize = 0, @@ -611,6 +642,17 @@ const Net = struct { jitter: u64, up: [8]Link = @splat(.{}), down: [8]Link = @splat(.{}), + /// The far client, whose link is `throttle_*`'s. + throttled: ?usize = null, + + /// When `bytes` sent at `now` down or up client `c`'s link `l` arrive: + /// the far one's cross at its rate as the far stream's zstd at `level` + /// makes them, after what was sent before them, then its delay. + fn cross(net: *Net, l: *Link, c: usize, now: u64, bytes: []const u8, level: c_int) u64 { + if (net.throttled != @as(?usize, c)) return l.arrive(net.random, now, net.one_way, net.jitter); + l.free = @max(now, l.free) + zstdLen(bytes, level) * std.time.us_per_s / throttle_rate; + return l.arrive(net.random, l.free, throttle_one_way, throttle_one_way / 20); + } fn send(net: *Net, m: Msg) !void { var mm = m; @@ -621,10 +663,32 @@ const Net = struct { /// Down client `c`'s link (the session's stream to it), `bytes` its. fn toClient(net: *Net, c: usize, now: u64, kind: Kind, gen: u32, bytes: []u8) !void { - try net.send(.{ .at = net.down[c].arrive(net.random, now, net.one_way, net.jitter), .n = 0, .kind = kind, .who = @intCast(c + 1), .bytes = bytes, .gen = gen }); + try net.send(.{ .at = net.cross(&net.down[c], c, now, bytes, if (kind == .dump) 6 else 3), .n = 0, .kind = kind, .who = @intCast(c + 1), .bytes = bytes, .gen = gen }); } }; +/// P32's far case: a link of 1 Mbit/s and 200 ms each way, what it carries +/// counted as the far stream's zstd makes it (core/replica.zig `zstd`), the +/// third client's (it joins before the flood); at most `throttle_window` of it ahead of +/// what has crossed (its sockets' and the route's buffers), the rest in the +/// session's queue for it, as a writer blocked on its socket leaves it. +const throttle_rate: u64 = 125_000; +const throttle_one_way: u64 = 200_000; +const throttle_window: u64 = 64 << 10; + +extern fn ZSTD_compressBound(n: usize) usize; +extern fn ZSTD_compress(dst: [*]u8, cap: usize, src: [*]const u8, n: usize, level: c_int) usize; +extern fn ZSTD_isError(code: usize) c_uint; + +/// `bytes` through zstd at `level`, alone: the far stream's (3, and 6 for a +/// join's), which a message shares with those before it. +fn zstdLen(bytes: []const u8, level: c_int) u64 { + const room = gpa.alloc(u8, ZSTD_compressBound(bytes.len)) catch return bytes.len; + defer gpa.free(room); + const n = ZSTD_compress(room.ptr, room.len, bytes.ptr, bytes.len, level); + return if (ZSTD_isError(n) != 0) bytes.len else n; +} + /// What client `c` wrote to its socket since (its inputs and their stamps, /// its terminal messages, a check asked, its bye), read off the session's /// end, a frame a message up its link: the session's Peer takes each as it @@ -639,7 +703,7 @@ fn drain(cl: *Client, c: usize, net: *Net, now: u64) !void { } var at: usize = 0; while (try pardes.wire.framed(cl.got.items[at..])) |f| : (at += f.total) { - try net.send(.{ .at = net.up[c].arrive(net.random, now, net.one_way, net.jitter), .n = 0, .kind = .up, .who = @intCast(c + 1), .bytes = try gpa.dupe(u8, cl.got.items[at..][0..f.total]), .gen = cl.live }); + try net.send(.{ .at = net.cross(&net.up[c], c, now, cl.got.items[at..][0..f.total], 3), .n = 0, .kind = .up, .who = @intCast(c + 1), .bytes = try gpa.dupe(u8, cl.got.items[at..][0..f.total]), .gen = cl.live }); } std.mem.copyForwards(u8, cl.got.items[0 .. cl.got.items.len - at], cl.got.items[at..]); cl.got.shrinkRetainingCapacity(cl.got.items.len - at); @@ -708,7 +772,7 @@ fn ended(cl: *Client, c: usize, net: *Net, now: u64, totals: *Totals, run_: Run, if (link.rejoins()) { cl.back = r.view; cl.dialling = true; - return net.send(.{ .at = net.up[c].arrive(net.random, now, net.one_way, net.jitter), .n = 0, .kind = .dial, .who = @intCast(c + 1), .bytes = &.{} }); + return net.send(.{ .at = net.cross(&net.up[c], c, now, &.{}, 3), .n = 0, .kind = .dial, .who = @intCast(c + 1), .bytes = &.{} }); } try totals.add(&r.stats); r.deinit(); @@ -753,7 +817,9 @@ const Soak = struct { /// What the shell's program prints (`.output`). tui: []const u8, shell_session: []const u8, - yes: []const u8, + /// The flood: `yes`, then a `cat` (its first `yes_len` bytes `yes`). + flood: []const u8, + yes_len: usize, l: *SessionLoop = undefined, /// What failed inside the loop (its wait returns nothing): the run's. err: ?anyerror = null, @@ -798,9 +864,13 @@ const Soak = struct { /// The session's pty told the answer to the program's kitty query /// (`questions`): once, whatever the clients. kitty_answers: u32 = 0, - /// A Peer's queue, swapped out as its writer swaps it (the Peers' - /// allocator's). - sending: std.ArrayListUnmanaged(u8) = .empty, + /// The session's unit with the flood's last bytes; the far client's + /// last join, and whether it has shown the final screen; a join's step + /// at worst. + flood_seq: u32 = 0, + far_joined: u64 = 0, + far_final: bool = false, + join_worst: u64 = 0, fn of(ctx: ?*anyopaque) *Soak { return @ptrCast(@alignCast(ctx.?)); @@ -905,13 +975,13 @@ const Soak = struct { cl.loop.link.check = null; cl.loop.link.deinit(gpa); } + cl.sending.deinit(s.l.peers.gpa); } while (s.net.q.pop()) |m| if (m.bytes.len > 0) gpa.free(m.bytes); if (s.held) |m| if (m.bytes.len > 0) gpa.free(m.bytes); for (s.echo.items) |e| gpa.free(e.bytes); s.echo.deinit(gpa); s.versions.deinit(gpa); - s.sending.deinit(s.l.peers.gpa); } pub fn resumePtys(_: *Soak, _: anytype) void {} @@ -1027,6 +1097,15 @@ const Soak = struct { cl.peer = peer; cl.shut = false; cl.read_done = false; + cl.join_seq = ps.seq; + cl.sending.clearRetainingCapacity(); + cl.at = 0; + cl.last = false; + cl.marked = 0; + // What the join cost the session's step (Peers.join's measure). + s.join_worst = @max(s.join_worst, ps.join_ns); + try s.totals.join_session_ns.append(gpa, ps.join_ns); + ps.join_ns = 0; cl.gen += 1; cl.id = welcome.client; cl.view = welcome.view.index(); @@ -1041,49 +1120,19 @@ const Soak = struct { cl.shut = true; }; s.detach_view = null; - const flooding = now >= flood_at and s.flood_end == 0; + // The unit with the flood's last bytes: what the far client's final + // screen shows. + if (s.flood_end != 0 and s.flood_seq == 0) s.flood_seq = ps.seq; for (s.clients, 0..) |*cl, c| if (cl.peer) |peer| { - if (peer.cut) cl.shut = true; - const io = ps.io; - peer.lock.lockUncancelable(io); - std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &s.sending); - peer.lock.unlock(io); - defer s.sending.clearRetainingCapacity(); if (cl.shut) { - if (!cl.read_done) { - cl.read_done = true; - ps.post(.{ .gone = peer }); - try net.toClient(c, now, .gone, cl.gen, &.{}); - } + // What was queued for it is let go with its link. + peer.out.clearRetainingCapacity(); + try s.shut(cl, c); continue; } - var at: usize = 0; - while (try pardes.wire.framed(s.sending.items[at..])) |f| : (at += f.total) { - const tag = std.enums.fromInt(pardes.wire.ServerTag, f.tag) orelse return error.BadFrame; - switch (tag) { - .unit => { - s.totals.unit_bytes += f.payload.len; - if (flooding) s.totals.flood_unit_bytes += f.payload.len; - try net.toClient(c, now, .to_replica, cl.gen, try gpa.dupe(u8, f.payload)); - }, - .screen => { - if (f.payload[5] == 1) { - s.totals.deltas += 1; - s.totals.delta_bytes += f.payload.len; - if (flooding) s.totals.flood_delta_bytes += f.payload.len; - } else s.totals.filled_bytes += f.payload.len; - try net.toClient(c, now, .screen_down, cl.gen, try gpa.dupe(u8, f.payload)); - }, - .check => { - const d = try gpa.alloc(u8, 8); - std.mem.writeInt(u64, d[0..8], (try pardes.wire.decodeServer(f.tag, f.payload)).check, .little); - try net.toClient(c, now, .checked, cl.gen, d); - }, - .blob => try net.toClient(c, now, .blob, cl.gen, try gpa.dupe(u8, f.payload)), - else => return error.UnexpectedFrame, - } - } - if (at != s.sending.items.len) return error.TornFrame; + s.stamp(cl, peer); + if (net.throttled == @as(?usize, c)) s.totals.queue_max = @max(s.totals.queue_max, peer.out.items.len); + try s.write(cl, c); }; // The kinds' panes it has opened, each counted once. for (core.panes) |slot| if (slot) |pane| if (pane.addon) |k| if (pane.serial > s.kinds_seen) { @@ -1098,6 +1147,97 @@ const Soak = struct { } } + /// The units the session queued for client `cl` since, their heads + /// carrying the soak's clock, the session's here (its host's `now`): + /// what the client's lag is measured by. Its replica guesses no clock + /// from them (`joined`: in process, both read one). + fn stamp(s: *Soak, cl: *Client, peer: *replica.Peer) void { + const out = peer.out.items; + var at = cl.marked; + while (pardes.wire.framed(out[at..]) catch null) |f| : (at += f.total) { + if (f.tag != @backingInt(pardes.wire.ServerTag.unit)) continue; + const head = at + f.total - f.payload.len; + std.mem.writeInt(i64, out[head + 14 ..][0..8], @intCast(s.now * std.time.ns_per_us), .little); + } + cl.marked = at; + } + + /// Client `c`'s Peer's writer (Peer.writeLoop's): the session's queue + /// for it swapped out a batch at a time and handed down its link, a + /// frame a message. The far link takes no more than `throttle_window` + /// ahead of what has crossed: the rest waits in the queue, as it does + /// for a writer blocked on its socket, until room comes (`.drain`). A + /// cut's words end a batch, and its link after them. + fn write(s: *Soak, cl: *Client, c: usize) !void { + const peer = cl.peer orelse return; + const net = s.net; + const window_us = throttle_window * std.time.us_per_s / throttle_rate; + while (true) { + if (cl.at == cl.sending.items.len) { + cl.sending.clearRetainingCapacity(); + cl.at = 0; + if (cl.last) return s.shut(cl, c); + const io = s.l.peers.io; + peer.lock.lockUncancelable(io); + std.mem.swap(std.ArrayListUnmanaged(u8), &peer.out, &cl.sending); + cl.last = peer.cut; + peer.lock.unlock(io); + cl.marked = 0; + if (cl.sending.items.len == 0) return; + } + if (net.throttled == @as(?usize, c) and net.down[c].free > s.now + window_us) { + if (!cl.draining) try net.send(.{ .at = net.down[c].free - window_us, .n = 0, .kind = .drain, .who = @intCast(c + 1), .bytes = &.{} }); + cl.draining = true; + return; + } + const f = (try pardes.wire.framed(cl.sending.items[cl.at..])) orelse return error.TornFrame; + cl.at += f.total; + try s.forward(cl, c, f); + } + } + + /// One frame of client `c`'s stream, down its link. + fn forward(s: *Soak, cl: *Client, c: usize, f: pardes.wire.Framed) !void { + const net = s.net; + const now = s.now; + const flooding = now >= flood_at and s.flood_end == 0; + switch (std.enums.fromInt(pardes.wire.ServerTag, f.tag) orelse return error.BadFrame) { + .unit => { + s.totals.unit_bytes += f.payload.len; + if (flooding) s.totals.flood_unit_bytes += f.payload.len; + try net.toClient(c, now, .to_replica, cl.gen, try gpa.dupe(u8, f.payload)); + }, + .screen => { + if (f.payload[5] == 1) { + s.totals.deltas += 1; + s.totals.delta_bytes += f.payload.len; + if (flooding) s.totals.flood_delta_bytes += f.payload.len; + } else s.totals.filled_bytes += f.payload.len; + try net.toClient(c, now, .screen_down, cl.gen, try gpa.dupe(u8, f.payload)); + }, + .check => { + const d = try gpa.alloc(u8, 8); + std.mem.writeInt(u64, d[0..8], (try pardes.wire.decodeServer(f.tag, f.payload)).check, .little); + try net.toClient(c, now, .checked, cl.gen, d); + }, + .blob => try net.toClient(c, now, .blob, cl.gen, try gpa.dupe(u8, f.payload)), + .refused => try net.toClient(c, now, .word, cl.gen, try gpa.dupe(u8, (try pardes.wire.decodeServer(f.tag, f.payload)).refused)), + else => return error.UnexpectedFrame, + } + } + + /// Client `c`'s link ended at the session's side (a Detach typed in its + /// view, or the words of a cut sent): it hears its end and nothing after, + /// and its reader's end is said (the next boundary lets it go). + fn shut(s: *Soak, cl: *Client, c: usize) !void { + cl.shut = true; + cl.last = false; + if (cl.read_done) return; + cl.read_done = true; + s.l.peers.post(.{ .gone = cl.peer.? }); + try s.net.toClient(c, s.now, .gone, cl.gen, &.{}); + } + /// Each terminal at its version as the session's screen shows it now /// (after a flush: `Peers.flushed`). fn flushed(ctx: *anyopaque, core: *pardes.Pardes, screens: *replica.Screens) void { @@ -1170,7 +1310,7 @@ const Soak = struct { // tape is stepped in it between its steps (tape.enter's check), // and no rollback epoch's trees are held. const in_client = switch (m.kind) { - .refused, .dump, .input, .to_replica, .screen_down, .checked, .blob, .wake, .quit, .gone => true, + .refused, .dump, .input, .to_replica, .screen_down, .checked, .blob, .wake, .quit, .gone, .word => true, .draw => Draws.jobs.items.len > 0 and Draws.jobs.items[0].draw != null and Draws.jobs.items[0].draw != @as(?*const pardes.Pardes, core), else => false, }; @@ -1209,15 +1349,17 @@ const Soak = struct { if (s.program.at == s.program.bytes.len) { s.program = .{ .bytes = switch (m.who) { 0 => s.tui, - 1 => s.yes, + 1 => s.flood, else => s.shell_session, } }; } const k = @min(s.program.bytes.len - s.program.at, if (m.who == 1) @as(usize, 4096) else 700); const chunk = s.program.bytes[s.program.at..][0..k]; s.program.at += k; + // `yes` a read every 450 us, the `cat` a read every 4 ms + // (1 MB/s: the far link falls behind, many seconds long). if (s.program.at < s.program.bytes.len) - try net.send(.{ .at = m.at + if (m.who == 1) @as(u64, 450) else 3_000, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) + try net.send(.{ .at = m.at + if (m.who != 1) @as(u64, 3_000) else if (s.program.at < s.yes_len) @as(u64, 450) else 4_000, .n = 0, .kind = .output, .who = m.who, .bytes = &.{} }) else if (m.who == 1) s.flood_end = m.at; core.update(.{ .output = .{ .pane = .at(s.shell), .bytes = chunk } }); took_output = true; @@ -1238,6 +1380,12 @@ const Soak = struct { return false; } }, + // Room in the far link: its Peer's writer goes on. + .drain => { + cl.draining = false; + try s.write(cl, c); + if (s.l.peers.urgent.swap(false, .acq_rel)) return false; + }, // A client's connection, handed over by the daemon (tui.zig // `.replica`): joined at the session's next step boundary, // on a socket pair of its own. @@ -1320,7 +1468,7 @@ const Soak = struct { cl.r = null; } cl.tries += 1; - if (cl.left > 0 and cl.tries < 40) try net.send(.{ .at = net.up[c].arrive(s.random, m.at + 500_000, net.one_way, net.jitter), .n = 0, .kind = .dial, .who = m.who, .bytes = &.{} }) else { + if (cl.left > 0 and cl.tries < 40) try net.send(.{ .at = net.cross(&net.up[c], c, m.at + 500_000, &.{}, 3), .n = 0, .kind = .dial, .who = m.who, .bytes = &.{} }) else { // Given up on: its keys are never typed. cl.dialling = false; if (cl.left > 0) s.typing -= 1; @@ -1328,7 +1476,7 @@ const Soak = struct { } }, .dump => try s.joined(cl, c, m), - .input, .to_replica, .screen_down, .checked, .blob, .wake, .quit, .gone => { + .input, .to_replica, .screen_down, .checked, .blob, .wake, .quit, .gone, .word => { if (m.kind == .input and cl.left == 0) continue; const rp = cl.r orelse if (m.kind == .input and cl.dialling) { try net.send(.{ .at = m.at + 50_000, .n = 0, .kind = .input, .who = m.who, .bytes = &.{} }); @@ -1356,7 +1504,19 @@ const Soak = struct { if (c == 0) try net.send(.{ .at = m.at + 300_000, .n = 0, .kind = .quit, .who = m.who, .bytes = &.{} }); } }, - .to_replica => cl.loop.take(.{ .unit = try std.heap.c_allocator.dupe(u8, m.bytes) }), + .to_replica => { + if (net.throttled == @as(?usize, c) and !s.far_final and s.flood_seq != 0) if (replica.parseUnit(m.bytes)) |u| if (u.origin == 0 and u.seq >= s.flood_seq) { + s.far_final = true; + try s.totals.far_final.append(gpa, m.at - s.flood_end); + }; + // A join's reloads on a replica there before it: + // what its unit cost, on this process's clock. + const reloads = pardes.Term.reloads.n; + const t0 = monoNs(); + cl.loop.take(.{ .unit = try std.heap.c_allocator.dupe(u8, m.bytes) }); + if (pardes.Term.reloads.n != reloads) try s.totals.join_client_ns.append(gpa, monoNs() - t0); + }, + .word => cl.loop.take(.{ .refused = try std.heap.c_allocator.dupe(u8, m.bytes) }), .blob => cl.loop.take(.{ .blob = try std.heap.c_allocator.dupe(u8, m.bytes) }), .screen_down => { const pane = m.bytes[0]; @@ -1426,6 +1586,18 @@ const Soak = struct { } const rp = try replica.Replica.initDump(gpa, cl.id, cl.view, m.bytes, .{ .cols = cl.cols, .rows = cl.rows, .distrusted = &cl.distrusted }); cl.r = rp; + // In process: its clock reads are the session's machine's, and the + // units' heads carry the soak's clock (`stamp`). + rp.guess = false; + if (s.net.throttled == @as(?usize, c)) { + s.totals.far_joins += 1; + if (m.at >= flood_at and s.far_joined != 0) try s.totals.far_gaps.append(gpa, m.at - s.far_joined); + s.far_joined = m.at; + if (!s.far_final and s.flood_seq != 0 and cl.join_seq >= s.flood_seq) { + s.far_final = true; + try s.totals.far_final.append(gpa, m.at - s.flood_end); + } + } // Its terminals its own emulators, fed what the units carry. if (bytes_kind) { rp.t.screen_state = false; @@ -1468,7 +1640,8 @@ const Soak = struct { totals.flood_us += s.flood_end -| flood_at; 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 }); + totals.cut += s.l.peers.cut; + 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.join_worst / 1000, s.l.peers.dump_bytes >> 10, s.l.peers.state_bytes >> 10 }); if (bytes_kind and s.kitty_answers != 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 }); @@ -1563,7 +1736,7 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { nlooks += 1; }; - var net: Net = .{ .random = random, .one_way = r.rtt_ms * 1000 / 2, .jitter = r.rtt_ms * 1000 / 20 }; + var net: Net = .{ .random = random, .one_way = r.rtt_ms * 1000 / 2, .jitter = r.rtt_ms * 1000 / 20, .throttled = if (r.clients >= 3) 2 else null }; defer net.q.deinit(gpa); pardes.Kind.drawer = .{ .ctx = null, .go = Draws.go }; Draws.net = &net; @@ -1581,7 +1754,7 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { // What the shell's program prints: a full-screen program, a shell's // session (prompts, titles, directories, a clear now and then), then a - // flood of `yes`, read 4 KB at a time as a pty gives it. + // flood of `yes` and of a `cat`, read 4 KB at a time as a pty gives it. const tui = try screen_state.tuiStream(gpa); defer gpa.free(tui); const session_only = try screen_state.shellStream(gpa); @@ -1592,6 +1765,8 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { defer gpa.free(shell_session); const yes = try screen_state.yesStream(gpa, @intCast(r.flood_mb * (1 << 20) / 3)); defer gpa.free(yes); + const flood = try catAfter(yes, yes.len); + defer gpa.free(flood); var soak: Soak = .{ .r = r, @@ -1603,7 +1778,8 @@ fn run(init: std.process.Init, r: Run, dir: []const u8, totals: *Totals) !void { .with_pdf = with_pdf, .tui = tui, .shell_session = shell_session, - .yes = yes, + .flood = flood, + .yes_len = yes.len, .own_left = r.steps, .typing = r.clients + 1, }; @@ -1629,6 +1805,32 @@ fn questions(dir: []const u8) ![]u8 { "\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}); } +/// `before`, then `n` bytes of what a `cat` of source text prints through a +/// pty: this file's lines, CR LF after each, again and again. +fn catAfter(before: []const u8, n: usize) ![]u8 { + var out: std.ArrayListUnmanaged(u8) = .empty; + errdefer out.deinit(gpa); + try out.appendSlice(gpa, before); + while (out.items.len < before.len + n) { + var lines = std.mem.splitScalar(u8, @embedFile("replicas.zig"), '\n'); + // Its lines of ASCII: one with a wide character, the scrollback + // reloaded at joins between reflows, still parts a client from the + // session (reports/pardes-term-lag.md), a fault of its own. + next: while (lines.next()) |line| { + for (line) |ch| if (ch >= 0x80) continue :next; + try out.print(gpa, "{s}\r\n", .{line}); + } + } + out.shrinkRetainingCapacity(before.len + n); + return out.toOwnedSlice(gpa); +} + +fn monoNs() u64 { + var ts: libc.timespec = undefined; + _ = libc.clock_gettime(.MONOTONIC, &ts); + return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec)); +} + /// A typist: keys 50-200 ms apart, a pause now and then. fn typistGap(random: std.Random) u64 { return if (random.uintLessThan(u8, 12) == 0) random.intRangeAtMost(u64, 300_000, 1_500_000) else random.intRangeAtMost(u64, 50_000, 200_000); @@ -2474,7 +2676,7 @@ fn runEcho(seed: u64, rtt_ms: u64, on: bool, et: *EchoTotals) !void { .screen_up => { if (try screens.take(a.core, peer, m.bytes)) |answer| try push(&queue, &n, .{ .at = down[0].arrive(random, m.at, one_way, jitter), .n = 0, .kind = .screen_down, .who = 1, .bytes = answer }); }, - .output, .own, .dial, .dump, .refused, .check_up, .checked, .bye_up, .wake, .quit, .gone, .draw, .up, .blob => {}, + .output, .own, .dial, .dump, .refused, .check_up, .checked, .bye_up, .wake, .quit, .gone, .draw, .up, .blob, .word, .drain => {}, } } const s = rp.echo.stats; @@ -2573,6 +2775,14 @@ fn echoMain(seeds: u64, first: u64, rtts: []const u64) !bool { } +fn secs(us: u64) f64 { + return @as(f64, @floatFromInt(us)) / std.time.us_per_s; +} + +fn msOf(ns: u64) f64 { + return @as(f64, @floatFromInt(ns)) / std.time.ns_per_ms; +} + fn pct(samples: []u64, p: usize) u64 { if (samples.len == 0) return 0; std.mem.sort(u64, samples, {}, std.sort.asc(u64)); @@ -2826,7 +3036,7 @@ pub fn main(init: std.process.Init) !void { // files (runtime/session.zig opens them), never the user's. var runtime_buf: [300]u8 = undefined; if (setenv("XDG_RUNTIME_DIR", try std.fmt.bufPrintSentinel(&runtime_buf, "{s}", .{dir}, 0), 1) != 0) return error.NoRuntimeDir; - std.debug.print("{d} seeds x {d} inputs from the session's own frontend and each of {d} clients (terminals and windows by turns, joined from a dump, at most {d} at once), a TUI then {d} MB of yes in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, pardes.views.max - 1, flood_mb, frame_us }); + std.debug.print("{d} seeds x {d} inputs from the session's own frontend and each of {d} clients (terminals and windows by turns, joined from a dump, at most {d} at once), a TUI then {d} MB of yes and as much of a cat in the shell; costs in us (frame {d})\n", .{ seeds, steps, clients, pardes.views.max - 1, flood_mb, frame_us }); std.debug.print(" rtt inputs at-once stopped units fast rollback(mis+foreign) rate rebase spec p50/p99 copy p50/p99 (KB) rollback p50/p99 diverged unequal\n", .{}); var term_lines: std.ArrayList(u8) = .empty; defer term_lines.deinit(gpa); @@ -2834,6 +3044,8 @@ pub fn main(init: std.process.Init) !void { defer join_lines.deinit(gpa); var stop_lines: std.ArrayList(u8) = .empty; defer stop_lines.deinit(gpa); + var far_lines: std.ArrayList(u8) = .empty; + defer far_lines.deinit(gpa); for (rtts) |rtt| { var t: Totals = .{}; pardes.Term.reloads = .{}; @@ -2870,6 +3082,16 @@ pub fn main(init: std.process.Init) !void { for (t.stops) |st| if (st.n > 0) try stop_lines.print(gpa, "{s}:{d} ", .{ st.name, st.n }); try stop_lines.append(gpa, '\n'); try join_lines.print(gpa, "{d:>4} {d:>5} {d:>7} {d:>8} {d:>8} {d:>6} {d:>5}/{d:<5} {d:>5}/{d:<6} {d:>7} {d:>5}/{d}/{d}\n", .{ rtt, t.joins, t.refused, t.rejoined, t.detached, t.detach_words, t.agree, t.differ, t.checks, t.units, t.resizes, t.kind_panes[0], t.kind_panes[1], t.kind_panes[2] }); + try far_lines.print(gpa, "{d:>4} {d:>5} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} {d:>8} {d:>3} {d:>6.1}/{d:<6.1} {d:>6.1}/{d:<6.1} ({d})\n", .{ + rtt, t.far_joins, + secs(pct(t.far_gaps.items, 50)), secs(pct(t.far_gaps.items, 100)), + secs(pct(t.far_final.items, 50)), secs(pct(t.far_final.items, 100)), + t.queue_max >> 10, t.cut, + msOf(pct(t.join_session_ns.items, 50)), msOf(pct(t.join_session_ns.items, 100)), + msOf(pct(t.join_client_ns.items, 50)), msOf(pct(t.join_client_ns.items, 100)), + t.join_client_ns.items.len, + }); + if (t.far_final.items.len < seeds and clients >= 5) failed = true; if (t.divergences > 0 or t.unequal > 0 or t.screens_wrong > 0 or t.differ > 0 or t.unlisted > 0) failed = true; } std.debug.print("terminals: deltas a replica applied a second under the flood; a peer's bytes a second under the flood (deltas, units); encode a frame of the flood for every peer, apply a delta (us); rows fetched, KB of rows sent before the step that read them; resets; deltas checked/wrong\n", .{}); @@ -2878,6 +3100,8 @@ pub fn main(init: std.process.Init) !void { std.debug.print(" rtt joins refused rejoined detached detach agree/differ checks/units resizes kinds\n{s}", .{join_lines.items}); std.debug.print("stops: the wakes the clients' loops asked, the frames they drew, the yanks, pastes and links their frontends did (y/p/l), and what stopped their inputs (a stop counted again as its input is applied again), by name; those at a name replica.may_wait does not list fail the run\n", .{}); std.debug.print(" rtt unlisted wakes draws y/p/l stops\n{s}", .{stop_lines.items}); + std.debug.print("far: the third client's link 1 Mbit/s and 200 ms each way (zstd's bytes): its joins from a dump, s between two from the flood's start (p50/max), s from the flood's end to its final screen (p50/max: a run that never shows it fails), the session's queue for it at most, clients cut off; a join's cost to the replicas there before it, ms on this process's clock (p50/max): the session's step (reloads and dump), a client's unit (reloads), how many\n", .{}); + std.debug.print(" rtt joins refresh s final s queue KB cut session ms client ms\n{s}", .{far_lines.items}); if (failed) std.process.exit(1); } |
