diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/replica.zig | 44 |
1 files changed, 16 insertions, 28 deletions
diff --git a/src/replica.zig b/src/replica.zig index 4ddb3bb4..fc27fbd1 100644 --- a/src/replica.zig +++ b/src/replica.zig @@ -132,6 +132,17 @@ const Stepping = struct { const nowNs = pardes.FrameStats.nowNs; +/// With PARDES_REPLICA_TRACE, a line on stderr, each of `tapes` described +/// after it. +fn trace(gpa: Allocator, comptime fmt: []const u8, args: anytype, tapes: []const []const u8) void { + if (std.c.getenv("PARDES_REPLICA_TRACE") == null) return; + var w: std.Io.Writer.Allocating = .init(gpa); + defer w.deinit(); + w.writer.print(fmt, args) catch {}; + for (tapes) |t| tape.describe(t, &w.writer); + std.debug.print("{s}", .{w.written()}); +} + /// The `p`th percentile of `samples` (sorted here); 100 is the worst. fn pctOf(samples: []u64, p: usize) u64 { if (samples.len == 0) return 0; @@ -790,13 +801,7 @@ pub const Replica = struct { if (lastDigest(u.tape)) |d| { tape.checkDigest(&r.t, d); if (r.t.diverged) |why| { - if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { - var w: std.Io.Writer.Allocating = .init(r.gpa); - defer w.deinit(); - w.writer.print("replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }) catch {}; - tape.describe(u.tape, &w.writer); - std.debug.print("{s}", .{w.written()}); - } + trace(r.gpa, "replica {d}: its input {d} came to another state than the session's: {s}\n", .{ r.id, u.seq, why }, &.{u.tape}); return r.fail(why); } } @@ -836,26 +841,15 @@ pub const Replica = struct { if (r.applied > 0) { if (u.origin == r.id) r.stats.mispredicted += 1 else r.stats.foreign += 1; if (u.origin == r.id and std.c.getenv("PARDES_REPLICA_TRACE") != null) { - var w: std.Io.Writer.Allocating = .init(r.gpa); - defer w.deinit(); - w.writer.print("replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }) catch {}; - tape.describe(r.pending.items[0].spec.?, &w.writer); - w.writer.print("and the authority recorded:\n", .{}) catch {}; - tape.describe(u.tape, &w.writer); - std.debug.print("{s}", .{w.written()}); + trace(r.gpa, "replica {d}: its input {d} was recorded otherwise; it guessed:\n", .{ r.id, u.seq }, &.{r.pending.items[0].spec.?}); + trace(r.gpa, "and the authority recorded:\n", .{}, &.{u.tape}); } r.recovering = nowNs(); r.toConfirmed(); } r.moved = true; if (tape.replayUnit(&r.t, u.tape)) |why| { - if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { - var w: std.Io.Writer.Allocating = .init(r.gpa); - defer w.deinit(); - w.writer.print("replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }) catch {}; - tape.describe(u.tape, &w.writer); - std.debug.print("{s}", .{w.written()}); - } + trace(r.gpa, "replica {d}: the unit of {d}/{d} diverged: {s}\n", .{ r.id, u.origin, u.seq, why }, &.{u.tape}); return r.fail(why); } if (u.origin == r.id) { @@ -1791,13 +1785,7 @@ pub const Peers = struct { tape.recordDigest(&ps.t); const bytes = tape.taken(&ps.t); unitHeader(&ps.head, origin, seq); - if (std.c.getenv("PARDES_REPLICA_TRACE") != null) { - var w: std.Io.Writer.Allocating = .init(ps.gpa); - defer w.deinit(); - w.writer.print("session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }) catch {}; - tape.describe(bytes, &w.writer); - std.debug.print("{s}", .{w.written()}); - } + trace(ps.gpa, "session: unit {d}/{d}, {d} bytes, to {d} clients:\n", .{ origin, seq, bytes.len, ps.count() }, &.{bytes}); const head = wire.header(@backingInt(wire.ServerTag.unit), unit_head + bytes.len); for (ps.list) |slot| if (slot) |peer| peer.send(&.{ &head, &ps.head, bytes }); tape.clearTaken(&ps.t); |
