summaryrefslogtreecommitdiff
path: root/src/replica.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-04 23:51:08 -0300
committerGabriel Schneider <[email protected]>2026-11-05 09:54:56 -0300
commitfb00a8785df8050b6bd39851201c2ef8a506e926 (patch)
tree156a367dc9683ef9a1ea59be2b4a514b96e191ea /src/replica.zig
parent10cff7a2194aa7d7f4b65180e51522b4518fa601 (diff)
downloadpardes-fb00a8785df8050b6bd39851201c2ef8a506e926.tar.gz
pardes-fb00a8785df8050b6bd39851201c2ef8a506e926.zip
replica: a trace line with the tapes it describes is one trace(), where four places each built a writer, described and printed under their own PARDES_REPLICA_TRACE test
Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/replica.zig')
-rw-r--r--src/replica.zig44
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);