summaryrefslogtreecommitdiff
path: root/core/tape.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/tape.zig')
-rw-r--r--core/tape.zig128
1 files changed, 62 insertions, 66 deletions
diff --git a/core/tape.zig b/core/tape.zig
index 7f28621e..09985d15 100644
--- a/core/tape.zig
+++ b/core/tape.zig
@@ -114,7 +114,6 @@ pub const Site = enum(u16) {
fonts,
// Read in place (`value`).
clock,
- budget,
wake,
env,
interpreter,
@@ -281,6 +280,8 @@ pub const Tape = struct {
/// checks.
speculating: bool = false,
poisoned: bool = false,
+ /// The turn's yields when the speculation began (`stray`).
+ yields: u64 = 0,
/// What stopped it: a host function's or a site's name, or a poison's
/// reason (PARDES_REPLICA_STATS counts them).
poisoned_by: []const u8 = "",
@@ -410,7 +411,7 @@ pub const Tape = struct {
if (!t.host_side) return .{};
t.host_side = false;
t.depth += 1;
- if (t.replay) return .{ .outer = true };
+ if (t.replay) return .{ .tape = t };
if (p) |core| {
if (t.core == null) t.core = core;
t.noteWrites();
@@ -422,7 +423,7 @@ pub const Tape = struct {
bare.update.args[0].output.bytes = &.{};
t.record(.input, Input, &bare);
} else t.record(.input, Input, input);
- return .{ .outer = true };
+ return .{ .tape = t };
}
fn leaveInput(t: *Tape, p: ?*Pardes) void {
@@ -665,6 +666,9 @@ pub const Tape = struct {
defer t.applying = outer;
t.host_side = true;
defer t.host_side = true;
+ // Not at the end: the dump checked there reads through no tape.
+ const yields = pardes.turn.yields;
+ defer if (input != .deinit) t.stray(yields);
switch (input) {
.init, .init_from_dump => {
var opts = switch (input) {
@@ -768,12 +772,6 @@ pub const Tape = struct {
return t.value(.authority, f, args);
}
- /// A budget of `budget_ns` from `since_ns` on the monotonic clock is spent:
- /// what the clock said is taped, so a replica stops its work where this did.
- pub fn over(t: *Tape, since_ns: u64, budget_ns: u64) bool {
- return t.value(.budget, overNow, .{ since_ns, budget_ns });
- }
-
/// A body whose first parameter is an `std.Io` its caller does not pass
/// is handed the tape's (`args` are the rest): the only way core/ comes
/// to hold one.
@@ -865,11 +863,13 @@ pub const Tape = struct {
return t.replay or t.speculating;
}
- /// A syscall of the core's that is no site, made by a replica: what it
- /// returns is the authority's to say. A speculation waits for it, and a
- /// replay has diverged.
- pub fn stray(t: *Tape) void {
- if (t.through) return;
+ /// A syscall of the core's that is no site, made by a replica since the
+ /// turn's count of yields was `since`: what it returned is the
+ /// authority's to say. A speculation waits for it, and a replay has
+ /// diverged (it joins again), never the process (a window client's
+ /// `Term fish`, H17).
+ fn stray(t: *Tape, since: u64) void {
+ if (pardes.turn.yields == since) return;
if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{});
}
@@ -1023,44 +1023,45 @@ fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []cons
return said.items;
}
-/// A call into the core: the host's, and recorded, or the core's own.
-pub const Entered = struct { outer: bool = false };
+/// A call into the core: the host's, and recorded, or the core's own. The
+/// tape it entered (null: the core's own, or nobody records), so `leave`
+/// needs neither the global nor a core `init` did not make or `deinit` freed.
+pub const Entered = struct { tape: ?*Tape = null };
pub inline fn enter(p: ?*Pardes, input: Input) Entered {
- // A core's tape is the one `on` says while both are (P20: the global
- // goes at the last step): every entry a safe build makes checks it.
- // With none on, a client's core is entered between its steps (its own
- // frame drawn, Replica.draw), its tape idle.
- //
- // The idle tape, the rule for the step that has this read the field
- // (P20's last): a client's tape between its steps is neither replaying
- // nor speculating, and its core is entered all the same (its frame);
- // `enter` must not record into an idle replica's tape. Only a tape
- // stepping its core records. The test "a client's frame drawn between
- // its steps records nothing into its idle tape" (this file's end)
- // fails when it does.
- if (std.debug.runtime_safety) if (p) |core| if (if (on) |t| core.tape != t else core.tape.stepping())
+ // While the global is (P20: the terminal's questions read it), a core
+ // stepped is the one it says: every entry a safe build makes checks it.
+ if (std.debug.runtime_safety) if (p) |core| if (on) |was| if (core.tape != was)
@panic("tape: a core's field is not the tape being stepped (tape.on)");
- const t = on orelse return .{};
+ const t = tapeOf(p, &input);
+ // A client's tape between its steps records nothing (its frame drawn,
+ // its core freed: the test "a client's frame drawn between its steps
+ // records nothing into its idle tape"); an authority's records every
+ // entry, in a Stepping scope or not, as a real session's.
+ if (!records(t)) return .{};
return t.enterInput(p, &input);
}
-pub inline fn leave(p: ?*Pardes, e: Entered) void {
- if (e.outer) on.?.leaveInput(p);
-}
-
-// The entry points on `on`, the methods' (`Tape.site` and the rest).
-
-pub fn adopt(p: *Pardes) void {
- (on orelse &none).adopt(p);
+/// The tape an entry steps: its core's; for a session not made yet, the one
+/// its options name (`Pardes.create`'s).
+fn tapeOf(p: ?*Pardes, input: *const Input) *Tape {
+ if (p) |core| return core.tape;
+ const opts = switch (input.*) {
+ .init => |o| o,
+ .init_from_dump => |d| d.opts,
+ else => return &none,
+ };
+ return opts.tape orelse &none;
}
-pub fn current(p: *const Pardes) bool {
- return (on orelse &none).current(p);
+/// Whether an entry on `t` is recorded: not on the tape nobody records, nor
+/// on a replica's between its steps (a client's frame drawn, its core freed).
+fn records(t: *const Tape) bool {
+ return !(t.through or (t.replica and !t.replaying()));
}
-pub fn forget(p: *Pardes) void {
- (on orelse &none).forget(p);
+pub inline fn leave(p: ?*Pardes, e: Entered) void {
+ if (e.tape) |t| t.leaveInput(p);
}
/// An entry written once (P22): its public name made from the private
@@ -1324,14 +1325,6 @@ pub fn resync(t: *Tape) void {
t.real = .{};
}
-pub fn over(since_ns: u64, budget_ns: u64) bool {
- return (on orelse &none).over(since_ns, budget_ns);
-}
-
-fn overNow(since_ns: u64, budget_ns: u64) bool {
- return pardes.FrameStats.nowNs() -| since_ns > budget_ns;
-}
-
/// One host call, `f(args)`, made out of the turn (pardes.turn).
pub fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? {
pardes.turn.yield();
@@ -1343,10 +1336,6 @@ pub fn replaying() bool {
return (on orelse &none).replaying();
}
-pub fn stray() void {
- (on orelse &none).stray();
-}
-
/// A replica speculating (core/replica.zig): state only the authority
/// changes (a terminal's emulator) is left alone, and the input waits.
pub fn speculating() bool {
@@ -1477,12 +1466,14 @@ pub fn beginSpeculation(t: *Tape) void {
t.poisoned_by = "";
t.touched = false;
t.no_digest = true;
+ t.yields = pardes.turn.yields;
t.buf.clearRetainingCapacity();
t.seeFields();
}
/// Whether it stayed clean (no host call, no syscall, no authority state).
pub fn endSpeculation(t: *Tape) bool {
+ t.stray(t.yields);
t.speculating = false;
t.no_digest = false;
return !t.poisoned;
@@ -1588,19 +1579,21 @@ pub fn digestOf(p: *const Pardes) u64 {
return walk.digest(Pardes, p);
}
-/// PARDES_RECORD set: the tape is written from the session's first call.
-pub fn startFromEnv() void {
- if (on != null) return;
- const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return);
- if (prefix.len == 0) return;
+/// PARDES_RECORD set: the tape a session is written to from its first
+/// call, which whoever starts it hands in `Options.tape` (runtime/session.zig,
+/// the Mac's start). `on` too, while the terminal's questions read it (P20).
+pub fn fromEnv() ?*Tape {
+ const prefix = std.mem.span(libc.getenv("PARDES_RECORD") orelse return null);
+ if (prefix.len == 0) return null;
const gpa = std.heap.c_allocator;
var path_buf: [4096]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return;
+ const path = std.fmt.bufPrintSentinel(&path_buf, "{s}{d}.tape", .{ prefix, libc.getpid() }, 0) catch return null;
const fd = libc.open(path, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true, .CLOEXEC = true }, @as(libc.mode_t, 0o644));
- if (fd < 0) return;
- const t = gpa.create(Tape) catch return;
+ if (fd < 0) return null;
+ const t = gpa.create(Tape) catch return null;
t.* = .{ .gpa = gpa, .replay = false, .fd = fd, .scratch = .init(gpa), .arena = .init(gpa), .every = libc.getenv("PARDES_RECORD_EVERY") != null };
on = t;
+ return t;
}
/// `PARDES_REPLAY=<tape>`: the tape replayed into three replicas, each
@@ -1657,16 +1650,19 @@ test "a syscall that is no site, made replaying, diverges the replay and poisons
defer t.scratch.deinit();
defer t.arena.deinit();
defer if (t.diverged) |why| gpa.free(why);
- on = &t;
- defer on = null;
+ const since = pardes.turn.yields;
+ t.stray(since);
+ try std.testing.expect(t.diverged == null);
pardes.turn.yield();
pardes.turn.back();
+ t.stray(since);
try std.testing.expect(t.diverged != null);
t.replay = false;
t.speculating = true;
+ t.yields = pardes.turn.yields;
pardes.turn.yield();
pardes.turn.back();
- try std.testing.expect(t.poisoned);
+ try std.testing.expect(!endSpeculation(&t));
}
test "a replay that finds no answer where its core reads one diverges, and the read goes on harmless" {
@@ -1696,7 +1692,7 @@ test "a replay that finds no answer where its core reads one diverges, and the r
/// The functions of core/ that set `on` and leave a core's field be, each
/// with why the field is that tape already.
const sets_on_alone = [_]struct { []const u8, []const u8, []const u8 }{
- .{ "tape.zig", "startFromEnv", "no core yet: the one made next takes `on` (Pardes.create)" },
+ .{ "tape.zig", "fromEnv", "no core yet: its starter hands the tape in Options.tape" },
.{ "tape.zig", "replayBytes", "its replay's core is made with this tape in its options (apply)" },
.{ "replica.zig", "begin", "Stepping: the core stepped holds this tape already (adoptCore), which `enter` checks in a safe build" },
.{ "replica.zig", "end", "Stepping: `on` put back as `begin` found it" },