diff options
Diffstat (limited to 'core/tape.zig')
| -rw-r--r-- | core/tape.zig | 394 |
1 files changed, 242 insertions, 152 deletions
diff --git a/core/tape.zig b/core/tape.zig index 270147c2..52eb6c03 100644 --- a/core/tape.zig +++ b/core/tape.zig @@ -15,8 +15,11 @@ //! made, and each digest is checked against the recorded one -- by two //! replicas at different addresses and a third that never draws. //! -//! The local path pays a null check a call: `on` is null unless -//! PARDES_RECORD names where to write, `<prefix><pid>.tape`. +//! The local path pays a check a call: `on` is null, and a core's tape is +//! `none` (it records nothing), unless PARDES_RECORD names where to write, +//! `<prefix><pid>.tape`. A core holds its tape (`Pardes.tape`) as it holds +//! its allocator; the free functions read `on`, set beside it, until the +//! last of their callers is handed the tape (P20). const std = @import("std"); const pardes = @import("root.zig"); const walk = @import("walk.zig"); @@ -27,6 +30,10 @@ const libc = std.c; pub var on: ?*Tape = null; +/// The tape of a session nobody records (`Tape.through`): a core's when it +/// is given none, so `Pardes.tape` is never null. Nothing writes it. +pub var none: Tape = .{ .gpa = std.heap.page_allocator, .replay = false, .through = true, .scratch = .init(std.heap.page_allocator), .arena = .init(std.heap.page_allocator) }; + /// Every call into the core a host makes (a 9P connection task's too). pub const Input = union(enum) { init: pardes.Options, @@ -217,6 +224,12 @@ const set_mask = 0xff; pub const Tape = struct { gpa: std.mem.Allocator, + /// Zig's `io`, handed to a site's body: in core/, a body is the only + /// code that holds one. + io: std.Io = std.Io.Threaded.global_single_threaded.io(), + /// A session nobody records (`none`): every call goes through as it + /// is, and nothing is taped. + through: bool = false, replay: bool, /// The core whose digest is taken (a Restore replaces it). core: ?*Pardes = null, @@ -598,7 +611,10 @@ pub const Tape = struct { defer t.gpa.free(bytes); var notes: std.ArrayList(u8) = .empty; defer notes.deinit(t.gpa); - const q = pardes.dump.load(t.gpa, p.opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); + // Its own core, on no tape (the session's options name this one). + var opts = p.opts; + opts.tape = &none; + const q = pardes.dump.load(t.gpa, opts, bytes, ¬es) catch |err| return t.diverge("dump load failed: {t}", .{err}); defer q.deinit(); if (notes.items.len > 0) return t.diverge("dump of this build had notes: {s}", .{notes.items}); var want: Parts = undefined; @@ -657,6 +673,7 @@ pub const Tape = struct { opts.image_allocator = gpa; opts.tree_sitter_allocator = gpa; opts.frame_allocator = gpa; + opts.tape = t; t.core = switch (input) { .init => Pardes.init(gpa, opts), .init_from_dump => |d| pardes.dump.initFromDump(gpa, opts, d.bytes), @@ -673,6 +690,7 @@ pub const Tape = struct { .restore => |rs| { const nc = pardes.dump.restore(p, rs.bytes, rs.from) catch |err| return t.diverge("restore failed: {t}", .{err}); t.core = nc; + nc.tape = t; nc.host = t.hostOf(t.mask); }, // The session is over (or the core a Restore replaced is @@ -728,6 +746,199 @@ pub const Tape = struct { } t.finishInput(); } + + // ---- the entry points, on the handle a step is given (`Pardes.tape`, + // or a `tape: *Tape` parameter); the free functions below are the + // same on `on` until the last of their callers is moved ---- + + /// A syscall of the core's own, `f(args)`, made out of the turn (a 9P + /// request through a mount of this very session is answered meanwhile): + /// recorded with what it returned, or answered from the tape and never + /// made. A result with slices is decoded into `a` on replay, the allocator + /// `f` would have used. + pub fn site(t: *Tape, comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return t.call(s, a, f, args, true); + } + + /// A read of the host's that is no syscall out of the turn (the clock, the + /// environment) or yields for itself: as `site`. + pub fn value(t: *Tape, comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + return t.call(s, null, f, args, false); + } + + /// A read of what the authority alone holds (`.authority` state a step + /// makes digested state from: the session's closed panes, and Recent, which + /// every session of its user writes; the 9P server's readers and socket), + /// taped as `value` is; a speculation that reaches it waits for the + /// authority's answer, as at a host call. A replica's own is never read. + /// What it returns is the tape's arena's on a replay: `f` returns what + /// lives to the step's end (scratch, or the authority's own), and the + /// caller copies what it keeps. + /// + /// A yes or no (whether a script reads a pane's events, whether the + /// session serves a socket) is guessed by a speculation, mosh's way: the + /// authority's last answer to that question, as a replayed unit gave it, + /// recorded as the answer, so its unit confirms the guess or rolls it back. + /// A Look or a click's command in a pane then shows at once, not a round + /// trip late. ponytail: bools alone; a list (the closed panes, Recent) + /// changes with what the user does, and is asked of the authority. + pub fn authority(t: *Tape, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) { + if (t.speculating) if (t.answers.get(question(f, args))) |was| { + const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority); + t.record(.host, u16, &id); + t.record(.ret, bool, &was); + return was; + }; + if (t.replay) { + const got = t.value(.authority, f, args); + t.answers.put(t.gpa, question(f, args), got) catch {}; + return got; + } + } + t.poison("the authority's own state"); + 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 }); + } + + fn call(t: *Tape, comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { + const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; + comptime { + const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; + for (results) |T| { + if (T == Listed) break; + } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); + } + // No tape where there is no host to record or replay one. + if (t.through) return if (yields) out(f, args) else @call(.auto, f, args); + const id: u16 = @as(u16, host_fns.len) + @backingInt(s); + if (comptime s == .read_file) t.reading = args[1]; + defer if (comptime s == .read_file) { + t.reading = null; + }; + if (t.replay) { + // Diverged (said in `diverged`): what a speculation gets, harmless, + // for the rest of the input. A replica joins again; a replay says so. + if (!t.expectHost(id, @tagName(s))) return poisonOf(R); + var ret = t.last_ret; + return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { + t.diverge("the tape's answer to {t} is short", .{s}); + return poisonOf(R); + }; + } + // The host's own (a shell's spawn asking the shell), or one inside a + // site's syscall: the outer call is the record. + if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); + // A syscall a speculating replica must not make: its input waits for + // the authority's answer. + if (yields and t.speculating) { + t.poisoned = true; + t.poisoned_by = @tagName(s); + return poisonOf(R); + } + if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |g| { + const r = guessed(R, g); + t.record(.host, u16, &id); + t.record(.ret, R, &r); + return r; + }; + t.record(.host, u16, &id); + t.seeFields(); + t.host_side = true; + const r = if (yields) out(f, args) else @call(.auto, f, args); + t.host_side = false; + t.noteWrites(); + t.record(.ret, R, &r); + return r; + } + + /// Asked by `turn.yield`: a syscall a replica must never make. + pub fn replaying(t: *const Tape) bool { + 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; + if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); + } + + /// Inside a step: one being recorded, a replica's replay of one, or its + /// speculation. A read made between steps (a frame drawn) is none. + pub fn stepping(t: *const Tape) bool { + return t.depth > 0 or t.replay or t.speculating; + } + + /// A replica's core under screen-state terminals: its emulators are + /// written by `apply` alone, never by its own steps. + pub fn screenReplica(t: *const Tape) bool { + return t.screen_state and (t.replay or t.speculating); + } + + /// An authority recording for screen-state replicas. + pub fn screenAuthority(t: *const Tape) bool { + return t.screen_state and !t.replay and !t.speculating; + } + + /// What a speculation reached is the authority's (`why`): the input is + /// undone and waits for the authority's order. + pub fn poison(t: *Tape, why: []const u8) void { + if (t.speculating) { + t.poisoned = true; + t.poisoned_by = why; + } + } + + /// A replica's speculation leaves what is the authority's be (a terminal's + /// emulator, a kind's state): true, and the input waits for its order. + pub fn authorityOnly(t: *Tape, comptime why: []const u8) bool { + if (!t.speculating) return false; + t.poison(why); + return true; + } + + /// A speculation that moves a terminal's view: a screen-state replica's + /// may (its rollback puts the view back, replica.zig `views`, and a delta + /// sets it), any other stops (`authorityOnly`). + pub fn viewOnly(t: *Tape, comptime why: []const u8) bool { + if (!t.speculating) return false; + if (t.screen_state) { + t.touched = true; + return false; + } + t.poison(why); + return true; + } + + /// A speculation read a terminal's rows or moved its view (`Tape.touched`). + pub fn touchTerminal(t: *Tape) void { + if (t.speculating) t.touched = true; + } + + /// The core a Restore made is the session's from here. + pub fn adopt(t: *Tape, p: *Pardes) void { + p.tape = t; + if (t.replay or t.through) return; + t.core = p; + t.wrap(p); + } + + /// Whether `p` is the core being recorded (a Restore's old one is not). + pub fn current(t: *const Tape, p: *const Pardes) bool { + return t.core == null or t.core == p; + } + + /// The session's core is freed: no digest of it after. + pub fn forget(t: *Tape, p: *Pardes) void { + if (t.core == p) t.core = null; + } }; const pardes_fields = walk.fieldNames(Pardes); @@ -818,6 +1029,12 @@ fn partsDiffer(got: []const u64, want: []const u64, a: std.mem.Allocator) []cons pub const Entered = struct { outer: bool = false }; 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. + if (std.debug.runtime_safety) if (p) |core| if (if (on) |t| core.tape != t else core.tape.stepping()) + @panic("tape: a core's field is not the tape being stepped (tape.on)"); const t = on orelse return .{}; return t.enterInput(p, &input); } @@ -826,24 +1043,18 @@ pub inline fn leave(p: ?*Pardes, e: Entered) void { if (e.outer) on.?.leaveInput(p); } -/// The core a Restore made is the session's from here. +// The entry points on `on`, the methods' (`Tape.site` and the rest). + pub fn adopt(p: *Pardes) void { - const t = on orelse return; - if (t.replay) return; - t.core = p; - t.wrap(p); + (on orelse &none).adopt(p); } -/// Whether `p` is the core being recorded (a Restore's old one is not). pub fn current(p: *const Pardes) bool { - const t = on orelse return true; - return t.core == null or t.core == p; + return (on orelse &none).current(p); } -/// The session's core is freed: no digest of it after. pub fn forget(p: *Pardes) void { - const t = on orelse return; - if (t.core == p) t.core = null; + (on orelse &none).forget(p); } /// The Host function `i`, through the tape. @@ -999,54 +1210,16 @@ fn hostCall(comptime i: usize, comptime R: type, ctx: ?*anyopaque, args: anytype return r; } -/// A syscall of the core's own, `f(args)`, made out of the turn (a 9P -/// request through a mount of this very session is answered meanwhile): -/// recorded with what it returned, or answered from the tape and never -/// made. A result with slices is decoded into `a` on replay, the allocator -/// `f` would have used. pub fn site(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - return call(s, a, f, args, true); + return (on orelse &none).site(s, a, f, args); } -/// A read of the host's that is no syscall out of the turn (the clock, the -/// environment) or yields for itself: as `site`. pub fn value(comptime s: Site, comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - return call(s, null, f, args, false); + return (on orelse &none).value(s, f, args); } -/// A read of what the authority alone holds (`.authority` state a step -/// makes digested state from: the session's closed panes, and Recent, which -/// every session of its user writes; the 9P server's readers and socket), -/// taped as `value` is; a speculation that reaches it waits for the -/// authority's answer, as at a host call. A replica's own is never read. -/// What it returns is the tape's arena's on a replay: `f` returns what -/// lives to the step's end (scratch, or the authority's own), and the -/// caller copies what it keeps. -/// -/// A yes or no (whether a script reads a pane's events, whether the -/// session serves a socket) is guessed by a speculation, mosh's way: the -/// authority's last answer to that question, as a replayed unit gave it, -/// recorded as the answer, so its unit confirms the guess or rolls it back. -/// A Look or a click's command in a pane then shows at once, not a round -/// trip late. ponytail: bools alone; a list (the closed panes, Recent) -/// changes with what the user does, and is asked of the authority. pub fn authority(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - if (comptime @typeInfo(@TypeOf(f)).@"fn".return_type.? == bool) if (on) |t| { - const key = question(f, args); - if (t.speculating) if (t.answers.get(key)) |was| { - const id: u16 = @as(u16, host_fns.len) + @backingInt(Site.authority); - t.record(.host, u16, &id); - t.record(.ret, bool, &was); - return was; - }; - if (t.replay) { - const got = value(.authority, f, args); - t.answers.put(t.gpa, key, got) catch {}; - return got; - } - }; - poison("the authority's own state"); - return value(.authority, f, args); + return (on orelse &none).authority(f, args); } /// What `authority` was asked: `f` and its arguments as they are. @@ -1056,57 +1229,6 @@ fn question(comptime f: anytype, args: anytype) u64 { return h.final(); } -fn call(comptime s: Site, a: ?std.mem.Allocator, comptime f: anytype, args: anytype, comptime yields: bool) @typeInfo(@TypeOf(f)).@"fn".return_type.? { - const R = @typeInfo(@TypeOf(f)).@"fn".return_type.?; - comptime { - const Listed = if (@typeInfo(R) == .error_union) anyerror!@typeInfo(R).error_union.payload else R; - for (results) |T| { - if (T == Listed) break; - } else @compileError("tape: the site ." ++ @tagName(s) ++ " returns " ++ @typeName(R) ++ ", which tape.results does not list"); - } - // No tape where there is no host to record or replay one. - const t = on orelse return if (yields) out(f, args) else @call(.auto, f, args); - const id: u16 = @as(u16, host_fns.len) + @backingInt(s); - if (comptime s == .read_file) t.reading = args[1]; - defer if (comptime s == .read_file) { - t.reading = null; - }; - if (t.replay) { - // Diverged (said in `diverged`): what a speculation gets, harmless, - // for the rest of the input. A replica joins again; a replay says so. - if (!t.expectHost(id, @tagName(s))) return poisonOf(R); - var ret = t.last_ret; - return walk.decode(R, &ret, a orelse t.arena.allocator()) catch { - t.diverge("the tape's answer to {t} is short", .{s}); - return poisonOf(R); - }; - } - // The host's own (a shell's spawn asking the shell), or one inside a - // site's syscall: the outer call is the record. - if (t.host_side) return if (yields) out(f, args) else @call(.auto, f, args); - // A syscall a speculating replica must not make: its input waits for - // the authority's answer. - if (yields and t.speculating) { - t.poisoned = true; - t.poisoned_by = @tagName(s); - return poisonOf(R); - } - if (comptime s == .clock or s == .mtime) if (if (t.speculating) t.guess else t.stamp) |g| { - const r = guessed(R, g); - t.record(.host, u16, &id); - t.record(.ret, R, &r); - return r; - }; - t.record(.host, u16, &id); - t.seeFields(); - t.host_side = true; - const r = if (yields) out(f, args) else @call(.auto, f, args); - t.host_side = false; - t.noteWrites(); - t.record(.ret, R, &r); - return r; -} - pub const Guess = struct { real_ns: i64, mono_ns: i64, gmtoff: i32 }; /// A record's bytes by hash kept for a replica's replays (`files`, the @@ -1141,6 +1263,7 @@ pub fn recordDigest(t: *Tape) void { /// one it replays into. pub fn adoptCore(t: *Tape, p: *Pardes) void { t.core = p; + p.tape = t; p.host = t.hostOf(t.mask); } @@ -1155,10 +1278,8 @@ pub fn resync(t: *Tape) void { t.real = .{}; } -/// 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(since_ns: u64, budget_ns: u64) bool { - return value(.budget, overNow, .{ since_ns, budget_ns }); + return (on orelse &none).over(since_ns, budget_ns); } fn overNow(since_ns: u64, budget_ns: u64) bool { @@ -1172,88 +1293,57 @@ pub fn out(comptime f: anytype, args: anytype) @typeInfo(@TypeOf(f)).@"fn".retur return @call(.auto, f, args); } -/// Asked by `turn.yield`: a syscall a replica must never make. pub fn replaying() bool { - return if (on) |t| t.replay or t.speculating else false; + return (on orelse &none).replaying(); } -/// 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() void { - const t = on orelse return; - if (t.speculating) t.poisoned = true else t.diverge("a syscall that is no tape site, made replaying", .{}); + (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 { - return if (on) |t| t.speculating else false; + return (on orelse &none).speculating; } -/// Inside a step: one being recorded, a replica's replay of one, or its -/// speculation. A read made between steps (a frame drawn) is none. pub fn stepping() bool { - return if (on) |t| t.depth > 0 or t.replay or t.speculating else false; + return (on orelse &none).stepping(); } /// Screen-state terminals, on either side (`Tape.screen_state`). pub fn screenState() bool { - return if (on) |t| t.screen_state else false; + return (on orelse &none).screen_state; } -/// A replica's core under screen-state terminals: its emulators are -/// written by `apply` alone, never by its own steps. pub fn screenReplica() bool { - return if (on) |t| t.screen_state and (t.replay or t.speculating) else false; + return (on orelse &none).screenReplica(); } -/// An authority recording for screen-state replicas. pub fn screenAuthority() bool { - return if (on) |t| t.screen_state and !t.replay and !t.speculating else false; + return (on orelse &none).screenAuthority(); } -/// What a speculation reached is the authority's (`why`): the input is -/// undone and waits for the authority's order. pub fn poison(why: []const u8) void { - if (on) |t| if (t.speculating) { - t.poisoned = true; - t.poisoned_by = why; - }; + (on orelse &none).poison(why); } /// A replica's replay came to another state than the authority's (`why`): -/// it joins again. +/// it joins again. (No method: `Tape.diverged` is the field it writes.) pub fn diverged(why: []const u8) void { if (on) |t| if (t.replay) t.diverge("{s}", .{why}); } -/// A replica's speculation leaves what is the authority's be (a terminal's -/// emulator, a kind's state): true, and the input waits for its order. pub fn authorityOnly(comptime why: []const u8) bool { - if (!speculating()) return false; - poison(why); - return true; + return (on orelse &none).authorityOnly(why); } -/// A speculation that moves a terminal's view: a screen-state replica's -/// may (its rollback puts the view back, replica.zig `views`, and a delta -/// sets it), any other stops (`authorityOnly`). pub fn viewOnly(comptime why: []const u8) bool { - if (!speculating()) return false; - if (screenState()) { - touchTerminal(); - return false; - } - poison(why); - return true; + return (on orelse &none).viewOnly(why); } -/// A speculation read a terminal's rows or moved its view (`Tape.touched`). pub fn touchTerminal() void { - if (on) |t| if (t.speculating) { - t.touched = true; - }; + (on orelse &none).touchTerminal(); } /// A harmless stand-in for what a host call or a syscall would have |
