From b2f7c605c279b2dea0c25180730c3d2e7b4b7d45 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Mon, 16 Nov 2026 23:31:18 -0300 Subject: Make snapshots named marks and recordings named clips of sessions The state file keeps live state only (SKETCH03, one slot). save NAME is a keyframe and a mark in the sessions database, synced to disk; load, diff, snapshot and expect-snapshot take mark names, through the keyframe's own layout migrated to today's, compared over the leaves migration kept. A file from before (SKETCH01/02) has its used snapshot slots imported as marks "1".."7" at open, then drops them; a run that records nothing leaves them alone. record NAME, stop and play NAME make and play clips: a frame range of a session and its start keyframe, nothing overwritten, nothing invalidated by a later save, no capacity. The 64 MiB .inputs file, its --fresh memset and Timeline's Bank are gone; numbers still work as names, so every existing script runs unchanged. Event times in a clip are the session's, not restarted at record. reload.script also checks that a field a reload drops is still in the keyframe taken before it (expect-key). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- src/host/Console.zig | 220 ++++++++++++++++++++++++++---------- src/host/Host.zig | 108 ++++++++++++++---- src/host/Sessions.zig | 76 ++++++++----- src/host/memory.zig | 102 ++++++++++++----- src/host/probe.zig | 2 +- src/host/root.zig | 10 +- src/host/script.zig | 49 ++++++-- src/host/timeline.zig | 308 +++++++++++++++++--------------------------------- 8 files changed, 517 insertions(+), 358 deletions(-) (limited to 'src') diff --git a/src/host/Console.zig b/src/host/Console.zig index 82911ad..88e4864 100644 --- a/src/host/Console.zig +++ b/src/host/Console.zig @@ -1,20 +1,20 @@ //! The developer console: a line editor, a short log, and the host commands -//! (snapshots, recordings, replay, field inspection). Lines the host does not -//! know go to the sketch's `command`. +//! (marks, clips, replay, sessions, field inspection). Lines the host does +//! not know go to the sketch's `command`. const Console = @This(); const std = @import("std"); const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); const memory = @import("memory.zig"); -const timeline = @import("timeline.zig"); +const Sessions = @import("Sessions.zig"); pub const panel_height = 144; pub const Key = enum { left, right, home, end, backspace, delete, clear, tab, submit }; const Line = struct { bytes: [118]u8 = @splat(0), len: usize = 0 }; const host_commands = [_][]const u8{ - "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", + "help", "overlay memory", "overlay sound", "get", "set", "reset", "save", "load", "snapshot", "recording", "marks", "clips", "record", "stop", "play", "pause", "step", "seek start", "seek end", "diff", "quit", "exit", "overlay timing", "sessions", "timing on", "timing off", "timing frame", "timing work", "timing reset", "timing stats", }; @@ -24,11 +24,34 @@ buffer: [96]u8 = @splat(0), len: usize = 0, cursor: usize = 0, lines: [5]Line = @splat(.{}), -snapshot: usize = 1, -recording: usize = 0, +/// The mark `save`, `load`, `diff` and `record` use when given none. +snapshot: Name = .of("1"), +/// The clip `record`, `play`, `seek` and `step` use when given none. +recording: Name = .of("0"), overlay_memory: bool = false, overlay_sound: bool = false, +pub const Name = struct { + bytes: [Sessions.max_name]u8 = undefined, + len: usize = 0, + + pub fn of(text_: []const u8) Name { + var n: Name = .{ .len = text_.len }; + @memcpy(n.bytes[0..text_.len], text_); + return n; + } + pub fn get(n: *const Name) []const u8 { + return n.bytes[0..n.len]; + } +}; + +/// A mark or clip name: a word of at most `Sessions.max_name` bytes. +fn nameOr(arg: ?[]const u8, default: *const Name) ![]const u8 { + const n = arg orelse return default.get(); + if (n.len > Sessions.max_name) return error.NameTooLong; + return n; +} + pub fn text(self: *const Console) []const u8 { return self.buffer[0..self.len]; } @@ -134,8 +157,8 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const rest = words.rest(); if (eq(command, "help")) { - self.log("save|load [1..7] snapshot N | record|play [0..15] recording N | stop | pause | step N (frames) | seek start|end|"); - self.log("diff [N] | reset | get | set | overlay memory|sound | quit"); + self.log("save|load|diff [MARK] snapshot MARK marks | record|play [CLIP] recording CLIP clips | stop | pause"); + self.log("step N (frames) | seek start|end| | sessions | reset | get|set [] | overlay memory|sound | quit"); self.log("timing [on|off|frame|work|reset|stats] | overlay timing"); if (host.forward(self, line, now) == .unknown) self.log("Backtick hides the console. Tab completes."); return &.{}; @@ -172,72 +195,96 @@ pub fn execute(self: *Console, host: *Host, line: []const u8, now: u64) ![]const const path = arg orelse return error.ExpectedField; const leaf = host.state.layout.find(path) orelse return error.UnknownField; if (rest.len == 0) return error.ExpectedValue; - h.stop(now); + try stop(host); try fields.set(leaf, host.live(), std.mem.trim(u8, rest, " ")); self.print("{s} = {s}", .{ path, std.mem.trim(u8, rest, " ") }); } else if (eq(command, "quit") or eq(command, "exit")) { - h.stop(now); + try stop(host); host.quit = true; } else if (eq(command, "reset")) { - h.stop(now); + try stop(host); host.resetState(); self.log("Live state reset. Snapshots are kept."); - } else if (eq(command, "snapshot")) { - self.snapshot = try slot(arg orelse return error.ExpectedSlot, 1, memory.slot_count - 1); - } else if (eq(command, "recording")) { - self.recording = try slot(arg orelse return error.ExpectedSlot, 0, timeline.recording_count - 1); - } else if (eq(command, "save") or eq(command, "load")) { - const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; - if (eq(command, "load") and !h.bank.hasSnapshot(index)) return error.EmptySnapshot; - h.stop(now); - if (eq(command, "save")) { - h.bank.save(index); - self.print("Saved snapshot {d}. Recordings from its old contents are invalidated.", .{index}); - } else { - try h.restore(index); - self.print("Loaded snapshot {d}.", .{index}); - } - } else if (eq(command, "record") or eq(command, "play")) { - const index = if (arg) |a| try slot(a, 0, timeline.recording_count - 1) else self.recording; - if (eq(command, "play")) _ = try h.bank.get(index); - h.stop(now); - if (eq(command, "record")) try h.startRecording(index, self.snapshot, now, host.build) else try h.play(index, now); - self.recording = index; - self.print("{s} recording {d} from snapshot {d}.", .{ if (eq(command, "record")) "Started" else "Playing", index, h.bank.recording(index).snapshot }); - if (eq(command, "play")) self.warnBuild(host, h.bank.recording(index)); - } else if (eq(command, "sessions")) { - if (arg != null) return error.UnexpectedArgument; - try self.listSessions(host); + } else if (eq(command, "snapshot") or eq(command, "recording")) { + const n = try nameOr(arg orelse return error.ExpectedName, &self.snapshot); + (if (eq(command, "snapshot")) &self.snapshot else &self.recording).* = .of(n); + } else if (eq(command, "save")) { + const n = try nameOr(arg, &self.snapshot); + try stop(host); + _ = try host.sessions.mark(n, .mark, host.live()); + self.print("Saved snapshot {s}.", .{n}); + } else if (eq(command, "load")) { + const n = try nameOr(arg, &self.snapshot); + const key = try loadMark(host, n); + defer key.free(host.gpa); + try stop(host); + try h.restore(key); + self.print("Loaded snapshot {s}.", .{n}); + } else if (eq(command, "record")) { + const n = try nameOr(arg, &self.recording); + const from = self.snapshot.get(); + try stop(host); + // An unused mark captures the current state; an existing one is + // restored, so several clips can branch from exactly those bytes. + const k = if (try host.sessions.findMark(from) == null) try host.sessions.mark(from, .mark, host.live()) else blk: { + const key = try loadMark(host, from); + defer key.free(host.gpa); + try h.restore(key); + break :blk try host.sessions.keyframe(.jump, host.live()); + }; + // The input held now is restored first in the clip (Host.restoredInput). + h.restored = true; + try host.sessions.startClip(n, k); + h.startRecording(); + self.recording = .of(n); + self.print("Started recording {s} from snapshot {s}.", .{ n, from }); + } else if (eq(command, "play")) { + const n = try nameOr(arg, &self.recording); + try stop(host); + try loadClip(host, n); + try h.play(now); + self.recording = .of(n); + self.print("Playing recording {s}.", .{n}); + self.warnBuild(host); } else if (eq(command, "stop")) { - h.stop(now); + try stop(host); } else if (eq(command, "pause")) { if (h.mode != .playing and h.mode != .paused) return error.NotReplaying; h.pause(now); } else if (eq(command, "step") or eq(command, "seek")) { const value = arg orelse return error.ExpectedPosition; - _ = try h.bank.get(if (h.mode == .live or h.mode == .recording) self.recording else h.selected); - if (h.mode == .recording) h.stop(now); - if (h.mode == .live) try h.play(self.recording, now); - const r = try h.bank.get(h.selected); - if (h.mode == .playing and h.cursor == 0) self.warnBuild(host, r); + if (h.mode == .live or h.mode == .recording) { + try stop(host); + try loadClip(host, self.recording.get()); + try h.play(now); + self.warnBuild(host); + } if (eq(command, "step")) return h.step(std.fmt.parseInt(i64, value, 10) catch return error.InvalidPosition); if (eq(value, "start")) return h.seekCount(0); - if (eq(value, "end")) return h.seekCount(r.count); + if (eq(value, "end")) return h.seekCount(h.events.items.len); const ms = std.fmt.parseInt(u64, value, 10) catch return error.InvalidPosition; return h.seekTime(ms *| std.time.ns_per_ms); } else if (eq(command, "diff")) { - const index = if (arg) |a| try slot(a, 1, memory.slot_count - 1) else self.snapshot; - if (!h.bank.hasSnapshot(index)) return error.EmptySnapshot; - const diff = h.bank.compare(0, index); + const n = try nameOr(arg, &self.snapshot); + var arena: std.heap.ArenaAllocator = .init(host.gpa); + defer arena.deinit(); + const saved = try markState(host, arena.allocator(), n); + const diff = memory.compareKept(saved.layout, host.state.layout, host.live(), saved.bytes); if (diff.first) |first| { var buf: [96]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); first.leaf.name(first.offset, &w) catch {}; - self.print("Live vs snapshot {d}: {d} differing values, first {s}", .{ index, diff.count, w.buffered() }); - } else self.print("Live vs snapshot {d}: same", .{index}); + self.print("Live vs snapshot {s}: {d} differing values, first {s}", .{ n, diff.count, w.buffered() }); + } else self.print("Live vs snapshot {s}: same", .{n}); + } else if (eq(command, "sessions")) { + if (arg != null) return error.UnexpectedArgument; + try self.listSessions(host); + } else if (eq(command, "marks") or eq(command, "clips")) { + if (arg != null) return error.UnexpectedArgument; + try self.listNamed(host, eq(command, "marks")); } else { // Sketch commands edit state outside the recorded input. - h.stop(now); + try stop(host); switch (host.forward(self, line, now)) { .unknown => return error.UnknownCommand, .failed => return error.CommandFailed, @@ -254,8 +301,8 @@ pub fn draw(self: *const Console, host: *Host, out: *std.ArrayList(abi.Command), const width: f32 = @floatFromInt(host.canvas[0]); const h = &host.timeline; try out.append(arena, .{ .rect = .{ .dst = .{ .x = 0, .y = top, .w = width, .h = panel_height }, .color = .{ .r = 24, .g = 28, .b = 28 } } }); - const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {d} | recording {d} (active {d}) | event {d} | {d}ms | rec {t}", .{ - host.name, h.mode, self.snapshot, self.recording, h.selected, h.cursor, h.position / std.time.ns_per_ms, host.sessions.mode(), + const status = try std.fmt.allocPrint(arena, "{s} | {t} | snapshot {s} | recording {s} | event {d} | {d}ms | rec {t}", .{ + host.name, h.mode, self.snapshot.get(), self.recording.get(), h.cursor, h.position / std.time.ns_per_ms, host.sessions.mode(), }); try out.append(arena, .{ .text = .{ .x = 12, .y = top + 8, .scale = 1, .color = hint, .bytes = status } }); for (&self.lines, 0..) |*line, i| { @@ -291,20 +338,73 @@ fn listSessions(self: *Console, host: *Host) !void { } } -/// A replay of a recording made by other code may diverge: say so. -fn warnBuild(self: *Console, host: *const Host, r: *const timeline.Recording) void { - if (r.build() != 0 and host.build != 0 and r.build() != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +/// Ends a clip being recorded or played: live from here. +fn stop(host: *Host) !void { + if (host.timeline.mode == .recording) try host.sessions.stopClip(); + host.timeline.stop(); +} + +/// The keyframe of the newest mark `n`, or of mark number K for "#K". +pub fn loadMark(host: *Host, n: []const u8) !Sessions.Loaded { + const s = &host.sessions; + const found = if (n.len > 1 and n[0] == '#') + try s.markAt(std.fmt.parseInt(u64, n[1..], 10) catch return error.InvalidName) + else + try s.findMark(n) orelse return error.EmptySnapshot; + return s.load(found.session, found.key); +} + +/// Mark `n`'s State in today's layout (`arena`'s), and the layout it was +/// taken in: compare them over the leaves migration kept (`memory.compareKept`). +pub fn markState(host: *Host, arena: std.mem.Allocator, n: []const u8) !struct { bytes: []u8, layout: memory.Layout } { + const key = try loadMark(host, n); + defer key.free(host.gpa); + const bytes = try arena.alignedAlloc(u8, .fromByteUnits(memory.page), host.state.layout.size); + const layout = try memory.restore(arena, key.layout, key.state, host.state.layout, bytes, host.timeline.defaults); + return .{ .bytes = bytes, .layout = layout }; +} + +/// Makes clip `n` the one the timeline plays: its keyframe and frames. +fn loadClip(host: *Host, n: []const u8) !void { + const s = &host.sessions; + const clip = try s.findClip(n) orelse return error.EmptyRecording; + const key = try s.load(clip.session, clip.key); + errdefer key.free(host.gpa); + var events: std.ArrayList(abi.Event) = .empty; + errdefer events.deinit(host.gpa); + // A clip a crash left open ends where its session does. + const end = if (clip.end != 0) clip.end else (try s.summary(clip.session)).frames; + try s.events(clip.session, key.batch, clip.start, end, &events); + host.timeline.load(key, events); +} + +/// A replay of a clip made by other code may diverge: say so. +fn warnBuild(self: *Console, host: *const Host) void { + const key = host.timeline.key orelse return; + if (key.build != 0 and host.build != 0 and key.build != host.build) self.log("Warning: recorded by another build of the sketch; the replay may differ."); +} + +/// The last marks or clips: index, name, session and frame. +fn listNamed(self: *Console, host: *Host, marks: bool) !void { + const s = &host.sessions; + const n = try s.count(if (marks) .marks else .clips); + if (n == 0) return self.log(if (marks) "No marks: save NAME makes one." else "No clips: record NAME makes one."); + var i = n -| 5; + while (i < n) : (i += 1) { + const e = if (marks) try s.markAt(i) else try s.clipAt(i); + const key = try s.load(e.session, e.key); + defer key.free(host.gpa); + const label = e.name(); + if (marks) { + self.print("#{d} {s}: session {d} frame {d}", .{ i, label, e.session, key.frame }); + } else self.print("#{d} {s}: session {d} frames {d}..{d}", .{ i, label, e.session, e.start, e.end }); + } } fn eq(a: []const u8, b: []const u8) bool { return std.mem.eql(u8, a, b); } -fn slot(value: []const u8, min: usize, max: usize) !usize { - const index = std.fmt.parseInt(usize, value, 10) catch return error.InvalidSlot; - if (index < min or index > max) return error.InvalidSlot; - return index; -} test "line editing, completion and submission" { var c: Console = .{}; diff --git a/src/host/Host.zig b/src/host/Host.zig index e579c87..1fe1db2 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -9,7 +9,6 @@ const abi = @import("abi"); const memory = @import("memory.zig"); const playback = @import("timeline.zig"); const Timeline = playback.Timeline; -const mapRecordings = playback.mapRecordings; const gfx = @import("gfx.zig"); const sound = @import("audio.zig"); const Gpu = @import("gpu"); @@ -78,7 +77,6 @@ store: memory.StoreFile, /// Images and buffers the sketch made (`newImage`, `decodeImage`, /// `newBuffer`): Session holds their handles, so they go when it resets. made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, -recordings: []align(memory.page) u8, timeline: Timeline, /// This run's session and the ones before it (Sessions.zig). sessions: Sessions, @@ -90,8 +88,8 @@ library: ?Library = null, watch: ?Watch = null, reloads: usize = 0, /// Identifies the running build of the sketch (its library or executable -/// file's inode, size and modification time): recordings carry it, and -/// `play` warns on a mismatch. +/// file's inode, size and modification time): sessions' stretches carry it, +/// and `play` warns on a mismatch. build: u64 = 0, /// Copies of the sketch's info strings, replaced on every load. info_arena: std.heap.ArenaAllocator, @@ -169,9 +167,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); - var inputs_buf: [std.fs.max_path_bytes]u8 = undefined; - const recordings = try mapRecordings(try std.mem.print(&inputs_buf, "{s}.inputs", .{options.state})); - errdefer std.posix.munmap(recordings); var store_buf: [std.fs.max_path_bytes]u8 = undefined; var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); @@ -193,7 +188,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .state = state, .session = .init(gpa), .store = store, - .recordings = recordings, .timeline = undefined, .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, @@ -202,7 +196,7 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .frame_arena = .init(gpa), .input = .{ .exporting = options.export_to != null }, }; - self.timeline = .{ .bank = .{ .state = &self.state, .inputs = recordings } }; + self.timeline = .{ .gpa = gpa, .state = &self.state, .defaults = sketch.defaults }; self.painter.images = &self.images; if (gpu) |device| self.passes = .init(gpa, device); if (options.profile_gpu) if (self.passes) |*g| { @@ -217,12 +211,12 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std _ = c.SDL_StartTextInput(self.window); self.build = fileId(io, options.library orelse "/proc/self/exe"); try self.adopt(sketch); - if (options.fresh) { - self.state.header().used = 0; - @memset(self.recordings, 0); - self.resetState(); - } self.openSessions(info.record); + // Before --fresh: it never touches the history. + if (importSnapshots(&self.state, &self.sessions)) |n| { + if (n > 0) self.print("{d} snapshots of {s} are marks now", .{ n, options.state }); + } else |err| self.print("snapshots of {s} kept in it: {t}", .{ options.state, err }); + if (options.fresh) self.resetState(); if (!options.headless and options.export_to == null) { self.presentation = Presentation.init(gpa, self.window) catch null; if (options.timing_marker) self.print("presentation tracing: {s}", .{if (self.presentation) |p| p.status else "disabled_or_allocation_failed"}); @@ -250,7 +244,7 @@ pub fn deinit(self: *Host) void { std.posix.munmap(entry.value_ptr.*); } self.files.deinit(self.gpa); - std.posix.munmap(self.recordings); + self.timeline.deinit(); self.session.deinit(self.gpa); self.store.close(); self.made.images.deinit(self.gpa); @@ -294,7 +288,7 @@ pub fn run(self: *Host) !void { frameTime(wall, if (self.presentation) |p| p.sampleNs(wall) else wall, self.last_now, self.timeline.mode, self.pending.items); try self.frame(now); } - self.timeline.stop(c.SDL_GetTicksNS()); + self.timeline.stop(); } // Live animation uses the display clock, but a tick must not predate its input. @@ -352,12 +346,11 @@ pub fn frame(self: *Host, now: u64) !void { const why: ?Sessions.Why = if (self.sessions.frames == 0) .start else if (self.reloaded) .after_reload else if (self.jumped or self.sessions.away) .jump else null; self.sessions.away = false; self.sessions.frame(self.pending.items, self.live(), why) catch |err| self.print("session: {t}", .{err}); - h.capture(self.pending.items) catch |err| self.print("Recording stopped: {t}", .{err}); self.update(self.pending.items); }, } self.pending.clearRetainingCapacity(); - const work = try self.draw(if (h.mode == .live or h.mode == .recording) now else h.position, started); + const work = try self.draw(if (h.mode == .live or h.mode == .recording) now else h.base + h.position, started); const ended = c.SDL_GetTicksNS(); self.timing.add(.{ .frame = ended - (if (self.last_frame_end == 0) started else self.last_frame_end), .work = work }); self.last_frame_end = ended; @@ -398,7 +391,7 @@ fn restoredInput(self: *Host, now: u64) !void { self.input.mouse_y = 0; self.input.buttons = 0; self.input.keys = .empty; - if (h.mode != .recording or h.bank.recording(h.selected).count != 0) return; + if (h.mode != .recording) return; var seed: std.ArrayList(abi.Event) = .empty; defer seed.deinit(self.gpa); if (was.mouse_x != 0 or was.mouse_y != 0) try seed.append(self.gpa, .{ .kind = .mouse_move, .time = now, .x = was.mouse_x, .y = was.mouse_y }); @@ -654,7 +647,9 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { defer zone.end(); const replay = self.console.execute(self, line, now) catch |err| { // The sketch has printed its own error already. - if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); + if (err == error.NotRecording) { + self.console.log("Not recording (a release build: .record = true collects): no marks or clips."); + } else if (err != error.CommandFailed) self.console.print("{t}. Type help for usage.", .{err}); if (self.timeline.restored) self.jumped = true; try self.restoredInput(now); return err; @@ -672,7 +667,7 @@ pub fn runLine(self: *Host, line: []const u8, now: u64) !void { fn movesState(line: []const u8) bool { var words = std.mem.tokenizeAny(u8, line, " \t"); const verb = words.next() orelse return false; - const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions" }; + const still = [_][]const u8{ "help", "overlay", "timing", "get", "snapshot", "recording", "diff", "stop", "pause", "save", "quit", "exit", "sessions", "marks", "clips" }; for (still) |w| if (std.mem.eql(u8, verb, w)) return false; return true; } @@ -733,6 +728,7 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { for (completions, info.commands) |*to, from| to.* = try arena.dupe(u8, from); self.completions = completions; self.sketch = sketch; + self.timeline.defaults = sketch.defaults; self.reloaded = true; switch (adoption) { .unchanged => {}, @@ -916,6 +912,19 @@ fn openSessions(self: *Host, record: bool) void { } } +/// Snapshots of a state file from before sessions become marks "1".."7", +/// then the file holds live state only. While nothing records, they stay. +fn importSnapshots(state: *memory.StateFile, sessions: *Sessions) !usize { + if (state.slots == 1 or sessions.mode() == .off) return 0; + var n: usize = 0; + for (1..state.slots) |i| if (state.used(i)) { + _ = try sessions.mark(&.{'0' + @as(u8, @intCast(i))}, .import, state.slot(i)); + n += 1; + }; + try state.dropSnapshots(); + return n; +} + fn wallClock(io: std.Io) u64 { return @intCast(@max(0, std.Io.Timestamp.now(io, .real).nanoseconds)); } @@ -1176,3 +1185,60 @@ test "setup writeBuffer propagates unavailable, unknown and out-of-bounds upload try host.passes.?.buffers.append(a, .{ .buffer = undefined, .size = 4 }); try std.testing.expectError(error.UploadOutOfBounds, client.writeBuffer(@fromBackingInt(@intCast(1)), 2, "data")); } + +test "a state file's snapshots from before sessions become marks, and the file one slot" { + const State = extern struct { v: u32 = 0 }; + const D = struct { + fn f(_: abi.Memory, p: [*]u8) callconv(.c) void { + @as(*State, @ptrCast(@alignCast(p))).* = .{}; + } + }; + const gpa = std.testing.allocator; + const io = std.testing.io; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var buf: [std.fs.max_path_bytes]u8 = undefined; + const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/old.state", .{&tmp.sub_path}); + // An eight-slot SKETCH02 file, slots 0, 1 and 3 used: built from a new + // one-slot file's header. + { + var file = try memory.StateFile.open(gpa, path); + defer file.close(); + _ = try file.adopt(gpa, abi.layout.of(State, .state), D.f); + } + const one = try std.Io.Dir.cwd().readFileAlloc(io, path, gpa, .unlimited); + defer gpa.free(one); + const stride = memory.page; + const old = try gpa.alloc(u8, memory.header_len + memory.old_slot_count * stride); + defer gpa.free(old); + @memset(old, 0); + @memcpy(old[0..memory.header_len], one[0..memory.header_len]); + const h: *memory.Header = @ptrCast(@alignCast(old.ptr)); + h.magic = memory.file_magic_v2; + h.used = 0b1011; + for ([_][2]u32{ .{ 0, 5 }, .{ 1, 11 }, .{ 3, 33 } }) |slot| std.mem.writeInt(u32, old[memory.header_len + slot[0] * stride ..][0..4], slot[1], .little); + try std.Io.Dir.cwd().writeFile(io, .{ .sub_path = path, .data = old }); + + var state = try memory.StateFile.open(gpa, path); + defer state.close(); + try std.testing.expectEqual(memory.old_slot_count, state.slots); + var sessions = try Sessions.open(gpa, io, .memory, "", .{ .build = 1, .source = "", .layout = state.layoutBytes() }, 0); + defer sessions.deinit(); + try std.testing.expectEqual(@as(usize, 2), try importSnapshots(&state, &sessions)); + for ([_][2]u32{ .{ '1', 11 }, .{ '3', 33 } }) |want| { + const m = (try sessions.findMark(&.{@intCast(want[0])})).?; + const key = try sessions.load(m.session, m.key); + defer key.free(gpa); + try std.testing.expectEqual(Sessions.Why.import, key.why); + try std.testing.expectEqual(want[1], std.mem.readInt(u32, key.state[0..4], .little)); + } + try std.testing.expectEqual(@as(?Sessions.Entry, null), try sessions.findMark("2")); + // One slot now, live state kept, and it reopens so. + try std.testing.expectEqual(@as(usize, 1), state.slots); + try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, state.slot(0)[0..4], .little)); + var again = try memory.StateFile.open(gpa, path); + defer again.close(); + try std.testing.expectEqualStrings(&memory.file_magic, &again.header().magic); + try std.testing.expectEqual(@as(usize, 1), again.slots); + try std.testing.expectEqual(@as(u32, 5), std.mem.readInt(u32, again.slot(0)[0..4], .little)); +} diff --git a/src/host/Sessions.zig b/src/host/Sessions.zig index 809f160..6f10bba 100644 --- a/src/host/Sessions.zig +++ b/src/host/Sessions.zig @@ -161,6 +161,7 @@ pub fn deinit(s: *Sessions) void { pub fn close(s: *Sessions, state: []const u8, ended: u64) void { defer s.deinit(); if (s.db == .off) return; + s.stopClip() catch {}; _ = s.keyframe(.exit, state) catch {}; s.commitSynced(.{ .end = ended }) catch |err| std.log.warn("sessions: last commit failed: {t}", .{err}); } @@ -227,10 +228,12 @@ pub fn stretch(s: *Sessions, st: Stretch) !void { s.stretches += 1; } -/// Mark `name`: a keyframe of `state`, synced to disk. -pub fn mark(s: *Sessions, name: []const u8, state: []const u8) !void { - const k = try s.keyframe(.mark, state); +/// Mark `name`: a keyframe of `state` (`why`: `.mark`, or `.import`), +/// synced to disk. Returns the keyframe's index. +pub fn mark(s: *Sessions, name: []const u8, why: Why, state: []const u8) !u64 { + const k = try s.keyframe(why, state); try s.commitSynced(.{ .mark = .{ .name = name, .key = k } }); + return k; } /// Starts clip `name` at the next frame, played from keyframe `k`. @@ -366,8 +369,19 @@ fn Commit(comptime DB: type) type { // --------------------------------------------------------------------------- // Reading -pub const Found = struct { session: u64, key: u64 }; -pub const Clip = struct { session: u64, key: u64, start: u64, end: u64 }; +/// A mark (`start` and `end` 0) or a clip. +pub const Entry = struct { + session: u64, + key: u64, + start: u64 = 0, + end: u64 = 0, + bytes: [max_name]u8 = undefined, + len: usize = 0, + + pub fn name(e: *const Entry) []const u8 { + return e.bytes[0..e.len]; + } +}; /// A keyframe, its bytes `gpa`'s (`free`). pub const Loaded = struct { frame: u64, @@ -387,44 +401,44 @@ pub const Loaded = struct { } }; +pub fn markAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.marks, i); +} +pub fn clipAt(s: *Sessions, i: u64) !Entry { + return s.entryAt(.clips, i); +} /// The newest mark named `name`. -pub fn findMark(s: *Sessions, name: []const u8) !?Found { - const n = try s.count(.marks); - var i = n; - while (i > 0) { - i -= 1; - const e = try s.entry(.marks, i, name) orelse continue; - return .{ .session = e.session, .key = e.key }; - } - return null; +pub fn findMark(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.marks, name); } - /// The newest clip named `name`. -pub fn findClip(s: *Sessions, name: []const u8) !?Clip { - var i = try s.count(.clips); +pub fn findClip(s: *Sessions, name: []const u8) !?Entry { + return s.findNamed(.clips, name); +} + +fn findNamed(s: *Sessions, which: K, name: []const u8) !?Entry { + var i = try s.count(which); while (i > 0) { i -= 1; - return try s.entry(.clips, i, name) orelse continue; + const e = try s.entryAt(which, i); + if (std.mem.eql(u8, e.name(), name)) return e; } return null; } -/// Entry `i` of `marks` or `clips` when it is named `name`. -fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { +fn entryAt(s: *Sessions, which: K, i: u64) !Entry { switch (s.db) { .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); - const e = try R.at(try R.list(try R.root(db), which) orelse return null, i); - var buf: [max_name]u8 = undefined; - const got = (try R.field(e, .name)).readBytes(&buf) catch return null; - if (!std.mem.eql(u8, got, name)) return null; - return .{ - .session = try R.uint(e, .session), - .key = try R.uint(e, .key), - .start = if (which == .clips) try R.uint(e, .start) else 0, - .end = if (which == .clips) try R.uint(e, .end) else 0, - }; + const e = try R.at(try R.list(try R.root(db), which) orelse return error.NoSuchEntry, i); + var out: Entry = .{ .session = try R.uint(e, .session), .key = try R.uint(e, .key) }; + out.len = ((try R.field(e, .name)).readBytes(&out.bytes) catch return error.CorruptSession).len; + if (which == .clips) { + out.start = try R.uint(e, .start); + out.end = try R.uint(e, .end); + } + return out; }, } } @@ -432,7 +446,7 @@ fn entry(s: *Sessions, which: K, i: u64, name: []const u8) !?Clip { /// Entries in the root list `which`, or sessions. pub fn count(s: *Sessions, which: K) !u64 { switch (s.db) { - .off => return 0, + .off => return error.NotRecording, inline .memory, .file => |*db| { const R = Read(@TypeOf(db.*)); return if (try R.list(try R.root(db), which)) |l| try l.count() else 0; diff --git a/src/host/memory.zig b/src/host/memory.zig index 11ace5e..044fc3b 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -1,7 +1,7 @@ //! Sketch memory by lifetime. //! -//! `StateFile`: one `MAP_SHARED` file holding the live state and its snapshot -//! slots, plus a header describing the layout, so the state outlives restarts, +//! `StateFile`: one `MAP_SHARED` file holding the live state, plus a header +//! describing the layout, so the state outlives restarts, //! a probe can read it from another process, and a reload with a changed //! layout can carry the old values over field by field. //! @@ -17,13 +17,16 @@ const abi = @import("abi"); const field_value = @import("fields.zig"); pub const page = std.heap.page_size_min; -/// Slot 0 is live; 1..7 are snapshots. -pub const slot_count = 8; pub const header_len = 64 * 1024; -/// Layouts with element leaves (`list[].x`, `StoredField` with `dims`). -pub const file_magic = "SKETCH02".*; +/// One slot, the live state: snapshots are marks in the sessions database. +pub const file_magic = "SKETCH03".*; +/// Before sessions: slot 0 live and 1..7 snapshots (`old_slot_count`), which +/// the host imports as marks and then drops (`dropSnapshots`). +pub const file_magic_v2 = "SKETCH02".*; +pub const old_slot_count = 8; /// What hosts before element leaves wrote: arrays are whole leaves -/// (`StoredFieldV1`). Read, and rewritten as `file_magic` on adopt. +/// (`StoredFieldV1`), and eight slots. Read, and rewritten as +/// `file_magic_v2` on adopt. pub const file_magic_v1 = "SKETCH01".*; pub const Header = extern struct { @@ -31,7 +34,8 @@ pub const Header = extern struct { size: u64, alignment: u64, hash: u64, - /// Distance between slots, page aligned. + /// The slot's size, page aligned (and the distance between an old + /// file's slots). stride: u64, /// Bit `i`: slot `i` holds state. used: u32, @@ -260,9 +264,14 @@ pub const Diff = struct { /// Compares two copies of state leaf by leaf, so padding (a Debug /// `@Vector`'s spare lane, the gaps between fields) never differs. pub fn compare(layout: Layout, a: []const u8, b: []const u8) Diff { + return compareKept(layout, layout, a, b); +} + +/// `compare` over the leaves `migrate` from `from` keeps (`keeps`). +pub fn compareKept(from: Layout, layout: Layout, a: []const u8, b: []const u8) Diff { var diff: Diff = .{}; for (layout.fields) |leaf| { - if (leaf.whole()) continue; + if (leaf.whole() or (from.hash != layout.hash and !keeps(from, leaf))) continue; var it = leaf.elements(); while (it.next()) |offset| { if (std.mem.eql(u8, a[offset..][0..leaf.size], b[offset..][0..leaf.size])) continue; @@ -285,6 +294,8 @@ fn remapEnum(from: Layout.Leaf, old: []const u8, to: Layout.Leaf, new: []u8) boo pub const StateFile = struct { fd: posix.fd_t, map: []align(page) u8, + /// 1, or `old_slot_count` in a file from before sessions. + slots: usize = 1, /// Layout of the bytes in the file, owned by `arena`. layout: Layout, arena: std.heap.ArenaAllocator, @@ -329,7 +340,7 @@ pub const StateFile = struct { } pub fn slot(self: *StateFile, index: usize) []align(page) u8 { - std.debug.assert(index < slot_count); + std.debug.assert(index < self.slots); const h = self.header(); const start = header_len + index * h.stride; return @alignCast(self.map[start..][0..h.size]); @@ -361,7 +372,7 @@ pub const StateFile = struct { self.arena = .init(gpa); self.layout = try Layout.copy(self.arena.allocator(), new); try writeLayout(self.map[0..header_len], self.layout); - self.header().magic = file_magic; + self.header().magic = file_magic_v2; } return .unchanged; } @@ -371,8 +382,8 @@ pub const StateFile = struct { var old_arena: std.heap.ArenaAllocator = .init(gpa); defer old_arena.deinit(); const old_layout = self.layout; - var old_slots: [slot_count]?[]u8 = @splat(null); - for (&old_slots, 0..) |*copy, i| { + var old_slots: [old_slot_count]?[]u8 = @splat(null); + for (old_slots[0..self.slots], 0..) |*copy, i| { if (self.used(i)) copy.* = try old_arena.allocator().dupe(u8, self.slot(i)); } const old_used = self.header().used; @@ -385,7 +396,7 @@ pub const StateFile = struct { const stride = std.mem.alignForward(usize, @max(new.size, 1), page); try self.resize(stride); const h = self.header(); - h.* = .{ .magic = file_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; + h.* = .{ .magic = if (self.slots == 1) file_magic else file_magic_v2, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = stride, .used = old_used, .field_count = 0, .strings_len = 0 }; try writeLayout(self.map[0..header_len], self.layout); var total: Migration = .{}; @@ -405,14 +416,29 @@ pub const StateFile = struct { return .{ .migrated = total }; } + /// Makes a file from before sessions one of a single slot, its + /// snapshots (imported as marks by now) dropped. + pub fn dropSnapshots(self: *StateFile) !void { + if (self.slots == 1) return; + const stride = self.header().stride; + self.slots = 1; + try self.resize(stride); + self.header().magic = file_magic; + self.header().used &= 1; + } + fn check(self: *StateFile) !void { const h = self.header(); - if (!std.mem.eql(u8, &h.magic, &file_magic) and !std.mem.eql(u8, &h.magic, &file_magic_v1)) return error.NotAStateFile; - if (self.map.len < header_len + slot_count * h.stride or h.size > h.stride) return error.CorruptStateFile; + if (std.mem.eql(u8, &h.magic, &file_magic)) { + self.slots = 1; + } else if (std.mem.eql(u8, &h.magic, &file_magic_v2) or std.mem.eql(u8, &h.magic, &file_magic_v1)) { + self.slots = old_slot_count; + } else return error.NotAStateFile; + if (self.map.len < header_len + self.slots * h.stride or h.size > h.stride) return error.CorruptStateFile; } fn resize(self: *StateFile, stride: usize) !void { - const len = header_len + slot_count * stride; + const len = header_len + self.slots * stride; if (self.map.len != 0) posix.munmap(self.map); self.map = &.{}; switch (posix.errno(posix.system.ftruncate(self.fd, @intCast(len)))) { @@ -423,6 +449,36 @@ pub const StateFile = struct { } }; +/// Fills `live` (laid out as `to`) from `image`, state kept in another +/// layout, described by `layout_bytes` (a header as `StateFile.layoutBytes` +/// gives it): `defaults`, then every leaf `migrate` keeps. Returns the +/// image's layout, `arena`'s. +pub fn restore(arena: std.mem.Allocator, layout_bytes: []const u8, image: []const u8, to: Layout, live: []u8, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Layout { + const from = try layoutOf(arena, layout_bytes); + if (image.len != from.size) return error.CorruptKeyframe; + @memset(live, 0); + defaults(.state, live.ptr); + _ = migrate(from, image, to, live); + return from; +} + +/// The layout in a header as `StateFile.layoutBytes` gives it, `arena`'s. +pub fn layoutOf(arena: std.mem.Allocator, layout_bytes: []const u8) !Layout { + if (layout_bytes.len < @sizeOf(Header) or layout_bytes.len > header_len) return error.CorruptKeyframe; + const region = try arena.alignedAlloc(u8, .of(Header), header_len); + @memset(region, 0); + @memcpy(region[0..layout_bytes.len], layout_bytes); + return try readLayout(arena, region) orelse .{}; +} + +/// Whether `migrate` from `from` keeps `leaf`'s value: compared on these +/// only, state restored across a layout change differs nowhere it was reset. +pub fn keeps(from: Layout, leaf: Layout.Leaf) bool { + const before = from.pattern(leaf.path) orelse return false; + if (before.dims.len != leaf.dims.len) return false; + return (before.hash == leaf.hash and before.size == leaf.size) or (before.kind == .@"enum" and leaf.kind == .@"enum"); +} + /// Writes `layout`'s leaves after the `Header` at the start of `region`, /// in the current format (`StoredField`); the caller sets the magic. fn writeLayout(region: []u8, layout: Layout) !void { @@ -865,7 +921,7 @@ test "migration keeps unchanged fields and defaults the rest" { try std.testing.expectEqual(@as(usize, 1), m.reset); } -test "state file migrates live state and snapshots, and a reopen reads the layout back" { +test "state file migrates live state, and a reopen reads the layout back" { const V1 = extern struct { score: u32 = 0, name: [4]u8 = "none".* }; const V2 = extern struct { level: u16 = 1, score: u32 = 0 }; const D = struct { @@ -887,19 +943,13 @@ test "state file migrates live state and snapshots, and a reopen reads the layou try std.testing.expectEqual(StateFile.Adoption.created, try file.adopt(gpa, abi.layout.of(V1, .state), D.v1)); file.markUsed(0, true); @as(*V1, @ptrCast(file.slot(0).ptr)).score = 99; - @memcpy(file.slot(3), file.slot(0)); - file.markUsed(3, true); } var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(abi.layout.hash(V1), file.layout.hash); const adoption = try file.adopt(gpa, abi.layout.of(V2, .state), D.v2); try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); - for ([_]usize{ 0, 3 }) |i| { - const s: *V2 = @ptrCast(file.slot(i).ptr); - try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, s.*); - } - try std.testing.expect(!file.used(1)); + try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, @as(*V2, @ptrCast(file.slot(0).ptr)).*); } test "a store keeps its root and what it points to across a reopen, and migrates by field" { @@ -1090,7 +1140,7 @@ test "a state file from before element leaves (drat-meout's) keeps every value" var file = try StateFile.open(gpa, path); defer file.close(); try std.testing.expectEqual(StateFile.Adoption.unchanged, try file.adopt(gpa, same, NoDefaults.f)); - try std.testing.expectEqualStrings(&file_magic, &file.header().magic); + try std.testing.expectEqualStrings(&file_magic_v2, &file.header().magic); } var file = try StateFile.open(gpa, path); defer file.close(); diff --git a/src/host/probe.zig b/src/host/probe.zig index d8ed166..4b61153 100644 --- a/src/host/probe.zig +++ b/src/host/probe.zig @@ -31,7 +31,7 @@ pub fn main(init: std.process.Init) !void { const map = memory.mapReadOnly(file); if (map.len < memory.header_len) fatal("not a sketchbook state file"); const magic = map[0..8]; - if (!std.mem.eql(u8, magic, &memory.file_magic) and !std.mem.eql(u8, magic, &memory.file_magic_v1)) fatal("not a sketchbook state file"); + if (!std.mem.eql(u8, magic, &memory.file_magic) and !std.mem.eql(u8, magic, &memory.file_magic_v2) and !std.mem.eql(u8, magic, &memory.file_magic_v1)) fatal("not a sketchbook state file"); defer std.posix.munmap(map); var arena: std.heap.ArenaAllocator = .init(init.gpa); defer arena.deinit(); diff --git a/src/host/root.zig b/src/host/root.zig index 7d4b967..618512b 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -5,7 +5,7 @@ //! --state FILE persistent state (default: .state) //! --headless offscreen video, silent audio //! --script FILE play a script instead of reading devices -//! --fresh start from default state: no snapshots or recordings +//! --fresh start from default state (sessions, marks and clips are kept) //! --timing-marker encode frame ID and draw sample time for video analysis //! --profile-gpu time each GPU command alone, print the times at exit //! --console LINE run a console line once the sketch is loaded (repeatable) @@ -94,15 +94,15 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { // A scripted run from fresh state works on files of its own, moved to // the given path at exit: two runs of one build step (two `zig build`s - // at once share its output path) never share a state, store or - // recordings file, nor fail on the store's lock. An export's are + // at once share its output path) never share a state or store file, + // nor fail on the store's lock (its sessions are in memory). An export's are // removed instead: nothing reads them, and a player may have the given // ones open. var private_buf: [std.fs.max_path_bytes]u8 = undefined; const given = options.state; const private = (script_path != null or options.export_to != null) and options.fresh; if (private) options.state = std.mem.print(&private_buf, "{s}.{d}", .{ given, std.os.linux.getpid() }) catch fatal("--state path too long"); - defer if (private) for ([_][]const u8{ "", ".inputs", ".store" }) |suffix| { + defer if (private) for ([_][]const u8{ "", ".store" }) |suffix| { var from: [std.fs.max_path_bytes + 16]u8 = undefined; var to: [std.fs.max_path_bytes + 16]u8 = undefined; const a = std.mem.printSentinel(&from, "{s}{s}", .{ options.state, suffix }, 0) catch continue; @@ -131,7 +131,7 @@ const usage = \\ --state FILE persistent state (default: sketch.state) \\ --headless offscreen video, silent audio, no vsync \\ --script FILE play a script instead of reading devices - \\ --fresh start from default state: no snapshots or recordings + \\ --fresh start from default state (sessions, marks and clips are kept) \\ --timing-marker encode frame ID and draw sample time for video analysis \\ --profile-gpu time each GPU command alone, print the times at exit \\ --console LINE run a console line once the sketch is loaded (repeatable) diff --git a/src/host/script.zig b/src/host/script.zig index 12774b6..9e1dddf 100644 --- a/src/host/script.zig +++ b/src/host/script.zig @@ -18,8 +18,13 @@ //! await FIELD VALUE [~T] run frames until it holds (a minute at most) //! print FIELD write a field's value to stderr (a vector, //! array or struct: every value in it), no frame -//! expect-snapshot N live state equals snapshot N, leaf by leaf -//! (padding ignored), exactly +//! expect-snapshot NAME live state equals the newest mark NAME, leaf +//! by leaf (padding ignored, and leaves a layout +//! change reset), exactly +//! expect-key WHY FIELD VALUE +//! the newest keyframe of this session taken for +//! WHY (`Sessions.Why`: before_reload, mark...) +//! holds VALUE, read in its own layout //! expect-log TEXT a recent console line contains TEXT //! expect-no-log TEXT no recent console line does //! expect-audio KEY stream KEY has received samples @@ -49,6 +54,8 @@ const abi = @import("abi"); const Host = @import("Host.zig"); const fields = @import("fields.zig"); const memory = @import("memory.zig"); +const Console = @import("Console.zig"); +const Sessions = @import("Sessions.zig"); pub const frame_ns: u64 = std.time.ns_per_s / 60; /// The most frames `await` plays: a minute. @@ -150,10 +157,31 @@ fn step(host: *Host, arena: std.mem.Allocator, command: []const u8, rest: []cons } return; } else if (eq(u8, command, "expect-snapshot")) { - const index = std.fmt.parseInt(usize, rest, 10) catch return error.InvalidSlot; - if (!host.timeline.bank.hasSnapshot(index)) return error.EmptySnapshot; - if (!same(host, index)) return error.ExpectationFailed; + const saved = try Console.markState(host, arena, rest); + if (!same(host, saved.layout, saved.bytes, rest)) return error.ExpectationFailed; return; + } else if (eq(u8, command, "expect-key")) { + const why = std.meta.stringToEnum(Sessions.Why, words.next() orelse "") orelse return error.UnknownWhy; + const path = words.next() orelse return error.ExpectedField; + const want = std.mem.trim(u8, words.rest(), " \t"); + const s = &host.sessions; + try s.commit(.none); + var k = s.keys; + while (k > 0) { + k -= 1; + const key = try s.load(s.session, k); + defer key.free(host.gpa); + if (key.why != why) continue; + const leaf = (try memory.layoutOf(arena, key.layout)).find(path) orelse return error.UnknownField; + if (fields.matches(leaf, key.state, want)) return; + var buf: [128]u8 = undefined; + var w: std.Io.Writer = .fixed(&buf); + fields.format(leaf, key.state, &w) catch {}; + std.debug.print(" {s} keyframe {d}: {s} expected {s}, found {s}\n", .{ @tagName(why), k, path, want, w.buffered() }); + return error.ExpectationFailed; + } + std.debug.print(" no {s} keyframe in this session\n", .{@tagName(why)}); + return error.ExpectationFailed; } else if (eq(u8, command, "expect-log") or eq(u8, command, "expect-no-log")) { var found = false; for (&host.console.lines) |*l| { @@ -263,15 +291,14 @@ fn holds(host: *Host, path: []const u8, want: []const u8, tolerance: ?f64) bool return @abs(value - target) <= t; } -/// Live state against snapshot `index`, leaf by leaf; prints the first few -/// differences. -fn same(host: *Host, index: usize) bool { +/// Live state against mark `name`'s (`saved`, taken in layout `from`), leaf +/// by leaf over the leaves migration kept; prints the first few differences. +fn same(host: *Host, from: memory.Layout, saved: []const u8, name: []const u8) bool { const layout = host.state.layout; const live = host.live(); - const saved = host.timeline.bank.slot(index); var count: usize = 0; for (layout.fields) |leaf| { - if (leaf.whole()) continue; + if (leaf.whole() or (from.hash != layout.hash and !memory.keeps(from, leaf))) continue; var it = leaf.elements(); while (it.next()) |offset| { if (std.mem.eql(u8, live[offset..][0..leaf.size], saved[offset..][0..leaf.size])) continue; @@ -282,7 +309,7 @@ fn same(host: *Host, index: usize) bool { leaf.name(offset, &w) catch {}; w.writeAll(": live ") catch {}; fields.format(leaf.at(offset), live, &w) catch {}; - w.print(", snapshot {d} ", .{index}) catch {}; + w.print(", snapshot {s} ", .{name}) catch {}; fields.format(leaf.at(offset), saved, &w) catch {}; std.debug.print(" {s}\n", .{w.buffered()}); } diff --git a/src/host/timeline.zig b/src/host/timeline.zig index b52c74d..697a9df 100644 --- a/src/host/timeline.zig +++ b/src/host/timeline.zig @@ -1,47 +1,18 @@ -//! Snapshots, input recordings and replay: generic over state bytes and -//! `abi.Event`s, so any sketch gets time travel for free. +//! Clips and replay: generic over state bytes and `abi.Event`s, so any +//! sketch gets time travel for free. //! //! Playback is host state, not sketch state: wall clocks, modes and cursors //! must not contaminate byte comparisons between two runs of a sketch. //! -//! Recordings are frames: each frame's events, then its `tick`. Playback, -//! seek and step only ever stop between frames, and `frames` cuts a run of -//! events into them, so `update` sees exactly the frames it saw live, one -//! call each. +//! A clip is whole frames of a session (Sessions.zig) and the keyframe they +//! start from: each frame's events, then its `tick`. Playback, seek and step +//! only ever stop between frames, and `frames` cuts a run of events into +//! them, so `update` sees exactly the frames it saw live, one call each, +//! at the times it saw. const std = @import("std"); -const posix = std.posix; const abi = @import("abi"); const memory = @import("memory.zig"); - -pub const recording_len = 4 * 1024 * 1024; -pub const recording_count = 16; -pub const recordings_len = recording_len * recording_count; -const recording_magic = 0x52454305; -pub const capacity = (recording_len - 32) / @sizeOf(abi.Event); - -pub const Recording = extern struct { - duration: u64, - count: u32, - snapshot: u32, - magic: u32, - /// The build that recorded it (`build`, two halves), 0 when unknown. - build_low: u32, - build_high: u32, - reserved: u32, - events: [capacity]abi.Event, - slack: [recording_len - 32 - capacity * @sizeOf(abi.Event)]u8, - - pub fn slice(r: *const Recording) []const abi.Event { - return r.events[0..r.count]; - } - pub fn build(r: *const Recording) u64 { - return @as(u64, r.build_high) << 32 | r.build_low; - } - /// Events up to the end of the last whole frame. - pub fn whole(r: *const Recording) usize { - return frameStart(r.slice(), r.count); - } -}; +const Sessions = @import("Sessions.zig"); /// The largest frame boundary at or before `index`: 0, or just after a tick. pub fn frameStart(events: []const abi.Event, index: usize) usize { @@ -72,119 +43,69 @@ pub const Frames = struct { } }; -comptime { - std.debug.assert(@sizeOf(Recording) == recording_len); -} - -/// Snapshot slots of a state file, and the recordings that start from them. -pub const Bank = struct { - state: *memory.StateFile, - inputs: []align(memory.page) u8, - - pub fn slot(self: Bank, index: usize) []u8 { - return self.state.slot(index); - } - pub fn recording(self: Bank, index: usize) *Recording { - std.debug.assert(index < recording_count); - return @ptrCast(@alignCast(self.inputs[index * recording_len ..].ptr)); - } - pub fn hasSnapshot(self: Bank, index: usize) bool { - return index > 0 and index < memory.slot_count and self.state.used(index); - } - pub fn save(self: Bank, index: usize) void { - std.debug.assert(index > 0 and index < memory.slot_count); - for (0..recording_count) |i| { - const r = self.recording(i); - if (r.snapshot == index) r.magic = 0; - } - @memcpy(self.slot(index), self.slot(0)); - self.state.markUsed(index, true); - } - pub fn restore(self: Bank, index: usize) !void { - if (!self.hasSnapshot(index)) return error.EmptySnapshot; - @memcpy(self.slot(0), self.slot(index)); - } - pub fn get(self: Bank, index: usize) !*Recording { - const r = self.recording(index); - if (r.magic != recording_magic or r.count > capacity or !self.hasSnapshot(r.snapshot)) return error.EmptyRecording; - return r; - } - /// Leaf by leaf (`memory.compare`): padding never differs. - pub fn compare(self: Bank, a: usize, b: usize) memory.Diff { - return memory.compare(self.state.layout, self.slot(a), self.slot(b)); - } -}; - pub const Timeline = struct { - bank: Bank, + gpa: std.mem.Allocator, + /// Live state is its slot 0. + state: *memory.StateFile, + /// The running sketch's: a restore starts from its defaults. + defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void, mode: abi.Mode = .live, - selected: usize = 0, + /// The clip loaded: its start keyframe and its frames, `gpa`'s. + key: ?Sessions.Loaded = null, + events: std.ArrayList(abi.Event) = .empty, + /// The clip's first tick: positions count from it. + base: u64 = 0, cursor: usize = 0, position: u64 = 0, clock: u64 = 0, - /// Set whenever live state is restored from a snapshot, for the host + /// Set whenever live state is restored from a keyframe, for the host /// to reset the input state that went with the old bytes. restored: bool = false, - /// Restores a snapshot into live state. - pub fn restore(self: *Timeline, index: usize) !void { - try self.bank.restore(index); + pub fn deinit(self: *Timeline) void { + if (self.key) |k| k.free(self.gpa); + self.events.deinit(self.gpa); + } + + /// Puts a keyframe, in whatever layout it was taken, into live state. + pub fn restore(self: *Timeline, key: Sessions.Loaded) !void { + var arena: std.heap.ArenaAllocator = .init(self.gpa); + defer arena.deinit(); + _ = try memory.restore(arena.allocator(), key.layout, key.state, self.state.layout, self.state.slot(0), self.defaults); self.restored = true; } - /// `build` identifies the code recording (0: unknown); `play` warns - /// when it differs. - pub fn startRecording(self: *Timeline, index: usize, snapshot: usize, now: u64, build: u64) !void { - // An unused snapshot captures the current state; an existing one is - // restored, so several recordings can branch from exactly those bytes. - if (!self.bank.hasSnapshot(snapshot)) self.bank.save(snapshot); - try self.restore(snapshot); - const r = self.bank.recording(index); - @memset(std.mem.asBytes(r)[0..32], 0); - r.magic = recording_magic; - r.snapshot = @intCast(snapshot); - r.build_low = @truncate(build); - r.build_high = @truncate(build >> 32); - self.selected = index; + /// Makes a clip, its start keyframe and its frames, the one played; both + /// become the timeline's. + pub fn load(self: *Timeline, key: Sessions.Loaded, events: std.ArrayList(abi.Event)) void { + self.deinit(); + self.key = key; + self.events = events; + var it = frames(events.items); + const first = it.next(); + self.base = if (first) |f| f[f.len - 1].time else 0; + } + + fn at(self: *const Timeline, i: usize) u64 { + return self.events.items[i].time -| self.base; + } + + /// The last whole frame's tick, from the first. + fn duration(self: *const Timeline) u64 { + const end = frameStart(self.events.items, self.events.items.len); + return if (end == 0) 0 else self.at(end - 1); + } + + pub fn startRecording(self: *Timeline) void { self.cursor = 0; self.position = 0; - self.clock = now; self.mode = .recording; } - /// Records one frame (its events, then its tick) and rewrites their - /// times as recorded, so live `update` sees exactly what a replay will. - /// A frame that does not fit ends the recording before it. - pub fn capture(self: *Timeline, frame: []abi.Event) !void { - if (self.mode != .recording) return; - const r = self.bank.recording(self.selected); - if (frame.len > capacity - r.count) { - self.mode = .live; - return error.RecordingFull; - } - for (frame) |*e| { - // Queued events can predate the start command. Keep insertion - // order and nondecreasing times, including several events at the - // same timestamp. - e.time = @max(e.time -| self.clock, r.duration); - r.events[r.count] = e.*; - r.count += 1; - r.duration = e.time; - } - self.position = r.duration; - self.cursor = r.count; - } - pub fn stop(self: *Timeline, now: u64) void { - if (self.mode == .recording) { - const r = self.bank.recording(self.selected); - r.duration = @max(r.duration, now -| self.clock); - self.position = r.duration; - } + pub fn stop(self: *Timeline) void { self.mode = .live; } - pub fn play(self: *Timeline, index: usize, now: u64) !void { - const r = try self.bank.get(index); - try self.restore(r.snapshot); - self.selected = index; + pub fn play(self: *Timeline, now: u64) !void { + try self.restore(self.key orelse return error.EmptyRecording); self.cursor = 0; self.position = 0; self.clock = now; @@ -203,35 +124,33 @@ pub const Timeline = struct { /// due). Feed them to the same `update` that live input goes through. pub fn tick(self: *Timeline, now: u64) []const abi.Event { if (self.mode != .playing) return &.{}; - const r = self.bank.recording(self.selected); - self.position = @min(r.duration, self.position +| (now -| self.clock)); + const end = self.duration(); + self.position = @min(end, self.position +| (now -| self.clock)); self.clock = now; const start = self.cursor; var i = self.cursor; - while (i < r.count and r.events[i].time <= self.position) : (i += 1) { - if (r.events[i].kind == .tick) self.cursor = i + 1; + while (i < self.events.items.len and self.at(i) <= self.position) : (i += 1) { + if (self.events.items[i].kind == .tick) self.cursor = i + 1; } - if (self.position == r.duration) self.mode = .paused; - return r.events[start..self.cursor]; + if (self.position == end) self.mode = .paused; + return self.events.items[start..self.cursor]; } - /// Restores the snapshot and returns the frames that make up the first - /// `count` events (rounded down to a whole frame) to replay, so seeking - /// backwards works too. + /// Restores the start keyframe and returns the frames that make up the + /// first `count` events (rounded down to a whole frame) to replay, so + /// seeking backwards works too. pub fn seekCount(self: *Timeline, count: usize) ![]const abi.Event { - const r = try self.bank.get(self.selected); - try self.restore(r.snapshot); - self.cursor = frameStart(r.slice(), count); - self.position = if (self.cursor == 0) 0 else r.events[self.cursor - 1].time; + try self.restore(self.key orelse return error.EmptyRecording); + self.cursor = frameStart(self.events.items, count); + self.position = if (self.cursor == 0) 0 else self.at(self.cursor - 1); self.mode = .paused; - return r.events[0..self.cursor]; + return self.events.items[0..self.cursor]; } - /// To the last frame whose tick is at or before `time`. + /// To the last frame whose tick is at or before `time` into the clip. pub fn seekTime(self: *Timeline, time: u64) ![]const abi.Event { - const r = try self.bank.get(self.selected); - const position = @min(time, r.duration); + const position = @min(time, self.duration()); var count: usize = 0; - for (r.slice(), 0..) |e, i| { - if (e.time > position) break; + for (self.events.items, 0..) |e, i| { + if (self.at(i) > position) break; if (e.kind == .tick) count = i + 1; } const events = try self.seekCount(count); @@ -240,13 +159,12 @@ pub const Timeline = struct { } /// Moves by `steps` frames, forwards or back. pub fn step(self: *Timeline, steps: i64) ![]const abi.Event { - const r = try self.bank.get(self.selected); - const events = r.slice(); + const events = self.events.items; var cursor = frameStart(events, self.cursor); var remaining = @abs(steps); + const end = frameStart(events, events.len); while (remaining > 0) : (remaining -= 1) { if (steps > 0) { - const end = r.whole(); if (cursor == end) break; cursor += 1; while (cursor < end and events[cursor - 1].kind != .tick) cursor += 1; @@ -259,62 +177,46 @@ pub const Timeline = struct { } }; -/// Maps the recordings file next to a state file, creating it when missing. -pub fn mapRecordings(path: []const u8) ![]align(memory.page) u8 { - const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); - defer _ = posix.system.close(fd); - if (try memory.fileSize(fd) != recordings_len) switch (posix.errno(posix.system.ftruncate(fd, recordings_len))) { - .SUCCESS => {}, - else => |err| return posix.unexpectedErrno(err), - }; - return memory.mapFile(fd, recordings_len, .{ .READ = true, .WRITE = true }); -} - -test "frames are whole: input waits for its tick, live and in playback" { - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - const a = arena.allocator(); - // A bank on plain memory: one slot of 8 bytes, snapshot 1 used. +test "frames are whole: input waits for its tick, in playback, seek and step" { + const gpa = std.testing.allocator; + // Live state on plain memory: 8 bytes, no fields. var file: memory.StateFile = undefined; - file.map = try a.alignedAlloc(u8, .fromByteUnits(memory.page), memory.header_len + memory.slot_count * memory.page); + file.map = try gpa.alignedAlloc(u8, .fromByteUnits(memory.page), memory.header_len + memory.page); + defer gpa.free(file.map); @memset(file.map, 0); - file.header().* = .{ .size = 8, .alignment = 1, .hash = 1, .stride = memory.page, .used = 0b11, .field_count = 0, .strings_len = 0 }; - file.layout = .{}; - const inputs = try a.alignedAlloc(u8, .fromByteUnits(memory.page), recording_len); - @memset(inputs, 0); - var t: Timeline = .{ .bank = .{ .state = &file, .inputs = inputs } }; - // Frame k: a move at 10k ms, its tick at 10k + 5 ms; then a move with - // no tick (a frame cut off). - try t.startRecording(0, 1, 1000, 7); - try std.testing.expect(t.restored); + file.slots = 1; + file.header().* = .{ .size = 8, .alignment = 1, .hash = 1, .stride = memory.page, .used = 1, .field_count = 0, .strings_len = 0 }; + file.layout = .{ .size = 8, .hash = 1 }; + const D = struct { + fn f(_: abi.Memory, _: [*]u8) callconv(.c) void {} + }; + var t: Timeline = .{ .gpa = gpa, .state = &file, .defaults = D.f }; + defer t.deinit(); + // Frame k: a move at 10k ms, its tick 5 ms later; then a move with no + // tick (a frame cut off). + var events: std.ArrayList(abi.Event) = .empty; for (0..4) |k| { - var frame = [_]abi.Event{ - .{ .kind = .mouse_move, .time = 1000 + k * 10_000_000, .x = @floatFromInt(k) }, - abi.Event.tick(1000 + k * 10_000_000 + 5_000_000, 16_666_667), - }; - try t.capture(&frame); - // Live update sees the recorded times. - try std.testing.expectEqual(@as(u64, k * 10_000_000 + 5_000_000), frame[1].time); - try std.testing.expectEqual(abi.Event.tick(0, 16_666_667).tickDt(), frame[1].tickDt()); + try events.append(gpa, .{ .kind = .mouse_move, .time = 1000 + k * 10_000_000, .x = @floatFromInt(k) }); + try events.append(gpa, abi.Event.tick(1000 + k * 10_000_000 + 5_000_000, 16_666_667)); } - const r = t.bank.recording(0); - r.events[r.count] = .{ .kind = .mouse_move, .time = 40_000_000 }; - r.count += 1; - t.stop(1000 + 41_000_000); - try std.testing.expectEqual(@as(u64, 7), r.build()); + try events.append(gpa, .{ .kind = .mouse_move, .time = 40_000_000 }); + const key: Sessions.Loaded = .{ .frame = 0, .batch = 0, .why = .mark, .time = 0, .stretch = 0, .build = 7, .state = try gpa.dupe(u8, &@as([8]u8, @splat(0))), .line = try gpa.dupe(u8, ""), .source = try gpa.dupe(u8, ""), .layout = try gpa.dupe(u8, file.layoutBytes()) }; + t.load(key, events); - try t.play(0, 0); + try t.play(0); + try std.testing.expect(t.restored); var delivered: usize = 0; // Every moment between 0 and the end, 1 ms apart. var now: u64 = 0; while (t.mode == .playing) : (now += 1_000_000) { - const events = t.tick(now); - if (events.len == 0) continue; - try std.testing.expectEqual(abi.Event.Kind.tick, events[events.len - 1].kind); - var it = frames(events); + const due = t.tick(now); + if (due.len == 0) continue; + try std.testing.expectEqual(abi.Event.Kind.tick, due[due.len - 1].kind); + var it = frames(due); while (it.next()) |frame| { try std.testing.expectEqual(@as(usize, 2), frame.len); - try std.testing.expectEqual(abi.Event.tick(0, 16_666_667).tickDt(), frame[1].tickDt()); + // Fed at the times it was recorded at. + try std.testing.expectEqual(1000 + delivered * 10_000_000 + 5_000_000, frame[1].time); delivered += 1; } } @@ -322,13 +224,13 @@ test "frames are whole: input waits for its tick, live and in playback" { try std.testing.expectEqual(@as(usize, 8), t.cursor); // A seek between a frame's input and its tick stops before the frame. - try std.testing.expectEqual(@as(usize, 4), (try t.seekTime(22_000_000)).len); - try std.testing.expectEqual(@as(usize, 8), (try t.seekCount(r.count)).len); + try std.testing.expectEqual(@as(usize, 6), (try t.seekTime(27_000_000)).len); + try std.testing.expectEqual(@as(usize, 8), (try t.seekCount(t.events.items.len)).len); try std.testing.expectEqual(@as(usize, 6), (try t.step(-1)).len); try std.testing.expectEqual(@as(usize, 8), (try t.step(5)).len); try std.testing.expectEqual(@as(usize, 2), (try t.step(-3)).len); // The cut-off frame is never fed. - var it = frames(r.slice()); + var it = frames(t.events.items); var n: usize = 0; while (it.next()) |_| n += 1; try std.testing.expectEqual(@as(usize, 4), n); -- cgit v1.3