diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Core.zig | 19 | ||||
| -rw-r--r-- | core/Names.zig | 20 | ||||
| -rw-r--r-- | core/Places.zig | 21 | ||||
| -rw-r--r-- | core/Turn.zig | 10 | ||||
| -rw-r--r-- | core/dump.zig | 7 | ||||
| -rw-r--r-- | core/replica.zig | 44 | ||||
| -rw-r--r-- | core/tape.zig | 128 |
7 files changed, 138 insertions, 111 deletions
diff --git a/core/Core.zig b/core/Core.zig index 7ebcc020..e736d43b 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -528,9 +528,9 @@ pub const perform = @import("Effect.zig").perform; /// second copy of this drifted once, leaving restored tags without an /// allocator and every 9P mtime 0. pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes { - // The global still set beside the field (P20): a core made while - // a tape records (PARDES_RECORD, a replica's step) is its. - const tape = opts.tape orelse pardes.tape.on orelse &pardes.tape.none; + // Its options' (a recording session's, an authority's, a replay's), or + // the one nobody records. + const tape = opts.tape orelse &pardes.tape.none; const p = try gpa.create(Pardes); p.* = .{ .gpa = gpa, @@ -561,10 +561,9 @@ pub fn create(gpa: std.mem.Allocator, opts: Options) !*Pardes { return p; } -/// A new session, its tape written from here when PARDES_RECORD asks. +/// A new session, recorded from here on its options' tape. pub fn init(gpa: std.mem.Allocator, opts: Options) !*Pardes { // By hand, not `taped`: no core yet to enter on; the tape adopts the one made. - pardes.tape.startFromEnv(); const taped = pardes.tape.enter(null, .{ .init = opts }); // The tape adopts the core made, none on a failure. var made: ?*Pardes = null; @@ -821,8 +820,8 @@ fn recentFound(p: *Pardes, hash: u64) ?recent_files.Entry { pub fn deinit(p: *Pardes) void { // By hand, not `taped`: what it records is no argument (`current`), and it leaves on no core. - const taped = pardes.tape.enter(p, .{ .deinit = pardes.tape.current(p) }); - pardes.tape.forget(p); + const taped = pardes.tape.enter(p, .{ .deinit = p.tape.current(p) }); + p.tape.forget(p); defer pardes.tape.leave(null, taped); if (std.c.getenv("PARDES_FRAMES")) |path| p.frames.dump(std.mem.span(path)); // Every file open now is closed with its dot, and the list kept. @@ -2284,6 +2283,10 @@ pub const Options = struct { /// first effects (runtime/session.zig); a client's session is on it, /// its first join made there with no hello (replica.join). pair: ?c_int = null, + /// A client exec'd at an Upgrade (runtime/client.zig `execAgain`, + /// PARDES_JOIN_VIEW): the view it had, which the session kept for it + /// (views.away), joined again for as a rejoin dials (replica.join). + join_view: ?u8 = null, mounts: []const filesystem.Mount = &.{}, config_dir: ?[]const u8 = null, /// Where what outlives the session is kept (Db.zig): the user's state @@ -2299,7 +2302,7 @@ pub const Options = struct { /// A client's: the kinds it caught breaking PA_PLAIN, its frontend's /// list, which outlives each core it joins with. In no tape and no dump. distrusted: ?*pardes.Addon.Distrusted = null, - /// The core's tape (`Pardes.tape`); null: `tape.on`'s, or `tape.none`. + /// The core's tape (`Pardes.tape`); null: `tape.none`. tape: ?*pardes.tape.Tape = null, /// What the host's scan of the addon directories found (Addon.scan). addons: []const pardes.Kind.Loaded = &.{}, diff --git a/core/Names.zig b/core/Names.zig index fff2c84f..2fbbec37 100644 --- a/core/Names.zig +++ b/core/Names.zig @@ -2,7 +2,7 @@ //! directory (listed by the disk engine, Stream.zig), the recent files, the open panes, //! a file's declarations. Each keystroke scores them again with a small //! fzf-style scorer (subsequence, Smith-Waterman with affine gaps, bonuses -//! at the starts of words and path segments), a frame's budget at a time, +//! at the starts of words and path segments), a frame's count at a time, //! the best kept first. Lifted from the pick prototype (Pick.zig), whose //! scorer took ~1 ms for 20k paths in a release build. Embedded in a //! Query; the rows it shows are the Query's to make. @@ -56,8 +56,12 @@ pub const max_query = 64; /// A candidate is scored over its first this many bytes. const max_text = 512; const max_cands = 200_000; -/// A keystroke's scoring, and a frame's: what is left waits a frame. -const score_budget_ns = 4 * std.time.ns_per_ms; +/// A keystroke's scoring, and a frame's: at most this many of the +/// scorer's cells (a key's bytes times the query's and one: what `match` +/// can visit of a candidate); what is left waits a frame. A count, not the +/// clock: a client's guess of a key scores as far as the session's step +/// (P27, P30). About 4 ms in a release build (`pardes-perf --search`). +const score_cells = 3_000_000; pub fn deinit(n: *Names, gpa: std.mem.Allocator) void { n.bytes.deinit(gpa); @@ -136,12 +140,12 @@ pub fn typed(n: *Names, gpa: std.mem.Allocator, text: []const u8) void { } /// Scores the pending candidates, then `cands[scored..]`, into `hits`, and -/// sorts them: at most `score_budget_ns` of it a call, the rest on the -/// next frames while the best so far is shown. +/// sorts them: at most `score_cells` of it a call, the rest on the next +/// frames while the best so far is shown. pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const q = n.queryText(); const empty = std.mem.trim(u8, q, " ").len == 0; - const started = pardes.FrameStats.nowNs(); + var left: usize = score_cells; const had = n.hits.items.len; const was_scored = n.scored; var count: usize = 0; @@ -157,8 +161,8 @@ pub fn rescore(n: *Names, gpa: std.mem.Allocator) void { const key = n.bytes.items[c.start + c.key ..][0..c.key_len]; const s = if (empty) 0 else match(q, key, n.lower.items[c.start + c.key ..][0..c.key_len], c.base, null, null); if (s) |score| n.hits.append(gpa, .{ .cand = i, .score = score }) catch break; - // The clock every 256 candidates: reading it is not free either. - if (!empty and count % 256 == 255 and pardes.tape.over(started, score_budget_ns)) break; + left -|= @as(usize, @min(c.key_len, max_text)) * (q.len + 1); + if (!empty and left == 0) break; } // An empty query keeps the source's order (the walk's, recency): no // row moves as the walk goes on, it only grows at the foot. diff --git a/core/Places.zig b/core/Places.zig index 9ffbff88..35b79805 100644 --- a/core/Places.zig +++ b/core/Places.zig @@ -46,13 +46,14 @@ pub const Provenance = enum { typed, search, list }; /// Places past this many are not kept. ponytail: 16 bytes a place, 16 MB /// at the cap; a sparse count past it if a million is ever too few. pub const max_hits = 1 << 20; -/// What one frame spends finding places: about 4 ms on the clock, checked -/// between pieces of the text; mvzr's steps (Regex.budget is about 300 ms -/// of them, so this is about 5 ms), which bound one slow piece; and bytes -/// read, the bound where there is no clock. -pub const frame_ns: u64 = 4 * std.time.ns_per_ms; +/// What one frame spends finding places: bytes of the text read (and the +/// places an insert above the view moves, as many bytes), about 4 ms in a +/// release build (`pardes-perf --search`); and mvzr's steps (Regex.budget +/// is about 300 ms of them, so this is about 5 ms), which bound one slow +/// piece. Counts, not the clock: a replica finds as many places a frame as +/// its session (P27, P30). pub const frame_steps: u64 = regexp.Regex.budget / 64; -pub const frame_bytes: usize = 8 << 20; +pub const frame_bytes: usize = 512 << 10; /// The text is searched in pieces of about this much, each ending at a /// line's end, so that a budget runs out between pieces, not inside one. /// A folding set lowers its copy of the text a piece at a time too. @@ -405,7 +406,6 @@ fn restart(set: *Places, p: *Pardes, f: *File.State) void { /// been searched, make it current. Run after every step (with no budget: /// the view only) and every tick (a frame's). pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) void { - const started_ns = pardes.FrameStats.nowNs(); if (!set.started) return; set.derive(p.gpa); const f = if (pane.file) |*file| file else return set.clear(p.gpa); @@ -490,17 +490,14 @@ pub fn fill(set: *Places, p: *Pardes, pane: *Pane, bytes: usize, steps: u64) voi } if (forward) set.hi = at else { set.hits.insertSlice(gpa, set.nhead, head.items) catch return; + // With many places below, moving them is most of the work. + left -|= (set.hits.items.len - set.nhead - head.items.len) * @sizeOf(Hit); if (set.current) |*c| c.* += head.items.len; set.nhead += head.items.len; set.head_to = at; } left -|= at - from; if (short) break; - // A frame's time spent (a whole search's for n and N, whose steps - // are mvzr's whole budget). - // The clock's verdict is the session's (core/tape.zig): a replica - // finds as many places a frame as this did. - if (started_ns != 0 and bytes != std.math.maxInt(usize) and pardes.tape.over(started_ns, frame_ns)) break; } if (set.hits.items.len != found_before) { set.gen +%= 1; diff --git a/core/Turn.zig b/core/Turn.zig index 687e287f..249c6795 100644 --- a/core/Turn.zig +++ b/core/Turn.zig @@ -17,7 +17,6 @@ //! come from a shell performing a save between steps -- so a parked request //! is never the syscall's own. const std = @import("std"); -const tape = @import("tape.zig"); const Turn = @This(); @@ -46,6 +45,10 @@ restores: u64 = 0, lsp_answers: u64 = 0, /// All of the above, and `out` reaching zero. settled: std.Io.Condition = .init, +/// Every `yield`, ever: a replica's step makes none (each syscall of its +/// core is a tape site), which its tape checks by this count across a +/// speculation and a replayed input (core/tape.zig `stray`). +yields: u64 = 0, /// The editor's thread takes the turn for good; it has it until `stop`. pub fn start(t: *Turn, io: std.Io) void { @@ -79,10 +82,7 @@ pub fn wake(t: *Turn) void { /// a step. Nests: a directory listing that resolves each entry yields /// inside a yield, and only the outermost one lets go. pub fn yield(t: *Turn) void { - // A replica makes no syscall: each is a site of the tape's. One that - // is none stops a speculation and diverges a replay (it joins - // again), never the process (a window client's `Term fish`, H17). - if (tape.replaying()) tape.stray(); + t.yields +%= 1; const io = t.io orelse return; yield_depth += 1; if (yield_depth != 1) return; diff --git a/core/dump.zig b/core/dump.zig index c348dec2..7ce54bce 100644 --- a/core/dump.zig +++ b/core/dump.zig @@ -531,17 +531,20 @@ fn restoreInner(p: *Pardes, bytes: []const u8, from: []const u8) !*Pardes { var opts = p.opts; opts.cols = p.view().screen_w; opts.rows = p.view().screen_h; + // Loaded on the session's tape (one with clients has it from no + // options), which the replacement's options do not keep. + opts.tape = p.tape; const replacement = try loadAndRevive(p.gpa, opts, bytes, p, null); + replacement.opts.tape = p.opts.tape; replacement.pointRestoreAt(from); // The tape follows the replacement from here (it is the session). - pardes.tape.adopt(replacement); + p.tape.adopt(replacement); pardes.ctlfs.events.notePath(replacement, "restore", from); return replacement; } pub fn initFromDump(gpa: std.mem.Allocator, opts: Options, bytes: []const u8) !*Pardes { // By hand, not `taped`: no core yet to enter on, as `Pardes.init`. - pardes.tape.startFromEnv(); const taped = pardes.tape.enter(null, .{ .init_from_dump = .{ .opts = opts, .bytes = bytes } }); // The tape adopts the core made, none on a failure. var made: ?*Pardes = null; diff --git a/core/replica.zig b/core/replica.zig index 781bd211..49787c76 100644 --- a/core/replica.zig +++ b/core/replica.zig @@ -182,6 +182,7 @@ pub const Authority = struct { a.t.whole_digest = screens and std.c.getenv("PARDES_REPLICA_TRACE") == null; // A digest at a unit's end, as the session's (Peers.unit). a.t.no_digest = true; + opts.tape = &a.t; const was = Stepping.begin(&a.t, null); defer was.end(); a.core = try Pardes.init(gpa, opts); @@ -1672,6 +1673,8 @@ pub const Peer = struct { /// Its bye said it joins again for its view (a divergence's rejoin): /// kept for it (views.away), not closed. Its reader's, read after its end. again: bool = false, + /// ...or that its window was closed (Peers.closed). + close: bool = false, /// Its number in the session's `Screens`. sid: u16 = 0, /// Inputs read (each one's seq, as the client numbered them). @@ -1931,7 +1934,9 @@ pub const Peer = struct { switch (t) { .hello => return false, .bye => { - peer.again = (wire.decodeClient(tag, payload) catch return false).bye.again; + const b = (wire.decodeClient(tag, payload) catch return false).bye; + peer.again = b.again; + peer.close = b.close; return false; }, .ping => { @@ -2054,6 +2059,10 @@ pub const Peers = struct { /// No frontend of its own (the host says): with no client either, the /// session is unattended. detached: bool = false, + /// Its last client left by closing its window, and no Detach was typed + /// in any (`kept`): a pair's session ends there (runtime/session.zig). + closed: bool = false, + kept: bool = false, /// The worst a join cost the session's step, and the last one's dump. join_ns: u64 = 0, each_join: std.ArrayListUnmanaged(u64) = .empty, @@ -2353,6 +2362,7 @@ pub const Peers = struct { core.undrawn &= ~(@as(u8, 1) << @intCast(peer.view)); ps.closeViewOf(core, peer, peer.again); if (ps.count() == 0) core.unattended = ps.detached; + if (ps.count() == 0 and peer.close and !ps.kept) ps.closed = true; ps.seq += 1; ps.unit(0, ps.seq); ps.letGo(peer); @@ -2457,6 +2467,7 @@ pub const Peers = struct { /// (`left`), its last word; the client goes, and its reader says so. pub fn detach(ps: *Peers, k: u8) bool { for (ps.list) |slot| if (slot) |peer| if (peer.view == k) { + ps.kept = true; var out: [wire.max_name + 8]u8 = undefined; peer.last(wire.encodeServer(&out, .{ .left = ps.name[0..@min(ps.name.len, wire.max_name)] }) catch unreachable); return true; @@ -2927,10 +2938,11 @@ pub fn askCheck(fd: c_int) bool { return writeAll(fd, wire.encodeClient(&out, .check) catch unreachable); } -/// The client's last word: `again`, it joins again for its view at once. -pub fn bye(fd: c_int, again: bool) void { +/// The client's last word: `again`, it joins again for its view at once; +/// `close`, its window was closed. +pub fn bye(fd: c_int, how: @FieldType(wire.ClientMsg, "bye")) void { var out: [16]u8 = undefined; - if (wire.encodeClient(&out, .{ .bye = .{ .again = again } })) |b| _ = writeAll(fd, b) else |_| {} + if (wire.encodeClient(&out, .{ .bye = how })) |b| _ = writeAll(fd, b) else |_| {} } /// What the session streams a client after its dump, as its frontend's loop @@ -2984,6 +2996,13 @@ pub fn ended(why: []const u8) bool { /// The words a client goes with when its session runs another build now. const upgrade_words = "session upgraded to a newer pardes: upgrade this client too"; +/// A join's failure that is its session's running another build now: a +/// client whose own start path holds a newer one execs that instead +/// (runtime/client.zig). +pub fn otherBuild(why: []const u8) bool { + return std.mem.endsWith(u8, std.mem.sliceTo(why, 0), upgrade_words); +} + /// A replica client's join, the terminal's and the window's alike (their /// `runClient`): the session at `path` dialled (or, first, the one on its /// pair, `o.pair`), its build checked to be this @@ -3008,7 +3027,10 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o }; if (have) |h| h.left(); prev.deinit(); - } else j.* = sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why) orelse return null; + } else j.* = (if (o.join_view) |v| + redial(gpa, path, session, why, have, true, v) + else + sameBuild(gpa, (if (o.pair) |fd| joinOn(gpa, fd, null, why, have, null) else dial(gpa, path, session, why, have, null)) orelse return null, why)) orelse return null; defer { if (have) |h| h.store(gpa, j) else gpa.free(j.dump); j.dump = &.{}; @@ -3043,7 +3065,7 @@ pub fn join(gpa: Allocator, j: *Joined, path: []const u8, session: []const u8, o /// an upgrade under way may not have said yet. fn sameBuild(gpa: Allocator, j: Joined, why: []u8) ?Joined { if (j.semantics == wire.semantics) return j; - bye(j.fd, false); + bye(j.fd, .{}); _ = libc.close(j.fd); var jj = j; jj.rest.deinit(gpa); @@ -3064,7 +3086,10 @@ fn redial(gpa: Allocator, path: []const u8, session: []const u8, why: *[600]u8, while (true) : (tries += 1) { why.* = @splat(0); if (dial(gpa, path, session, why, have, view)) |j| return sameBuild(gpa, j, why); - if (std.mem.indexOf(u8, std.mem.sliceTo(why, 0), "runs another build") != null) { + // Another build, or another protocol (the daemon moved with its + // session): no dial again will do. + const words = std.mem.sliceTo(why, 0); + if (std.mem.indexOf(u8, words, "runs another build") != null or std.mem.indexOf(u8, words, "run the same pardes at both ends") != null) { _ = std.fmt.bufPrint(why, "{s}\x00", .{upgrade_words}) catch {}; return null; } @@ -3081,6 +3106,8 @@ pub const Link = struct { r: *Replica, fd: c_int, quit: bool = false, + /// ...because its window was closed: said in its bye. + close: bool = false, /// The session hung up. gone: bool = false, /// Something to draw: an input applied, a unit taken. @@ -6772,9 +6799,6 @@ test "a client joined from a dump lands a click on a rail's tick as the session r.confirm(try a.take(m)); r.settle(); } - // (The key may be mispredicted: a step's places are a frame's time's - // worth, `tape.over`, the session's verdict, and its drawn caches let - // it find more in that time than the client's speculation did.) try agrees(a, r); try std.testing.expectEqual(a.core.panes[owner].?.places.current, r.core().panes[owner].?.places.current); } 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" }, |
