diff options
Diffstat (limited to 'src/Kind.zig')
| -rw-r--r-- | src/Kind.zig | 299 |
1 files changed, 203 insertions, 96 deletions
diff --git a/src/Kind.zig b/src/Kind.zig index 9785a1a3..e3d74b24 100644 --- a/src/Kind.zig +++ b/src/Kind.zig @@ -38,9 +38,9 @@ pub const abi: u32 = c.PA_ABI; comptime { if (@sizeOf(usize) == 8) for (.{ .{ c.pa_cell, 20 }, .{ c.pa_window, 24 }, .{ c.pa_rect, 16 }, .{ c.pa_cells, 8 }, - .{ c.pa_placement, 80 }, .{ c.pa_paint, 128 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 16 }, - .{ c.pa_api, 104 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, - .{ c.pa_ops, 200 }, .{ c.pa_view, 32 }, + .{ c.pa_placement, 80 }, .{ c.pa_paint, 32 }, .{ c.pa_key, 8 }, .{ c.pa_mouse, 16 }, + .{ c.pa_api, 112 }, .{ c.pa_out, 16 }, .{ c.pa_word, 32 }, .{ c.pa_head, 56 }, + .{ c.pa_ops, 208 }, .{ c.pa_view, 32 }, .{ c.pa_tick, 128 }, }) |r| if (@sizeOf(r[0]) != r[1]) @compileError(std.fmt.comptimePrint("{s} is {d} bytes, the header says {d}", .{ @typeName(r[0]), @sizeOf(r[0]), r[1] })); } @@ -57,11 +57,10 @@ pub const Caps = packed struct(u16) { jobs: bool = false, load: bool = false, search: bool = false, - /// Esc comes to its `key` first (2.1). - esc: bool = false, address: bool = false, - /// 2.2's: a wheel's amount, the pointer in paint, PA_FRAMED. - v22: bool = false, + view: bool = false, + /// PA_PICTURES: its tag says how its pictures show, from the first frame. + pictures: bool = false, pad: u6 = 0, }; @@ -158,40 +157,41 @@ pub const Slot = struct { /// A placeholder's: the kind's record and its `where`, kept whole. record: ?[]u8 = null, where_text: Buf(32) = .{}, - /// What the kind said refusing its open (or a load). - refused: Buf(96) = .{}, + /// What the kind said refusing its open (or a load), or `<path> is not here`. + refused: Buf(160) = .{}, /// Its words' last values, a `Word arg` line each, which a kind with /// no `dump` is given again when its pane comes back. values: Buf(256) = .{}, - /// Its last frame's prefix, and whether it placed a picture; where its - /// window is in its whole (the rail's thumb), 0 and 0 for none. + /// Its prefix as its last tick set it; where its window is in its + /// whole (the rail's thumb), 0 and 0 for none, as its last frame says. prefix: Buf(96) = .{}, - placed: bool = false, /// Its tag's prefix as the last step read it (the authority's): what a /// Shadow says in a step, and what a dump keeps of it, so a replica /// joining lays its tag out as the session did. told: Buf(400) = .{}, thumb_at: f32 = 0, thumb_span: f32 = 0, - /// The pointer its last frame asked for over it (PA_POINTER_*, 2.2), - /// and whether a frame was painted since its last tick. + /// The pointer its last frame asked for over it (PA_POINTER_*), and + /// whether a frame was painted since its last tick. pointer: u8 = 0, painted: bool = false, - /// What its view was (`api.view`) when `tick` was last told: a change - /// ticks it again (2.1). - view: u64 = 0, + /// Each view's facts (a hash of its pa_view) as `view` was last told + /// them, 0 for none: a change tells it again. + views: [views.max]u64 = @splat(0), + /// The jobs whose answers it cancelled (`api.cancel`), dropped as they come. + cancelled: [4]u32 = @splat(0), + /// Debug: the sites a call over budget (or a raster over the bound) was said at. + over: u32 = 0, /// What it has selected (`api.select`): a yank's, a chord's, a /// keyboard Look's. selection: []u8 = &.{}, /// Its tag's default words: Term, its kind's words for its panes, /// Collapse Del. tail: Buf(160) = .{}, - /// Its last frame changed its tag: a step is owed to lay the tag out - /// again (the tag is laid out before the body paints). - retag: bool = false, /// A replica's frame of it, as the authority painted it. shadow: ?*@import("screen_state.zig").Shadow = null, - /// Debug: the kind's allocations, which paint must leave as they are. + /// Debug: the kind's allocations, which paint must leave as they are + /// (the list the page allocator's: a region's first is slow in Debug). used: if (checked) std.ArrayList([]u8) else void = if (checked) .empty else {}, pub const Job = struct { token: u32, in: []u8 }; @@ -218,7 +218,7 @@ pub const Slot = struct { pub fn free(s: *Slot) void { for (&s.jobs) |*j| if (j.*) |job| s.gpa.free(job.in); s.gpa.free(s.selection); - if (checked) s.used.deinit(s.gpa); + if (checked) s.used.deinit(std.heap.page_allocator); if (s.shadow) |sh| sh.deinit(s.gpa); s.arena.deinit(); s.gpa.free(s.path); @@ -278,7 +278,8 @@ pub fn reopen(p: ?*Pardes, id: usize, s: *Slot, bytes: ?[]const u8) bool { s.tail.len = @intCast(w.buffered().len); }; s.opened = if (bytes) |b| std.hash.Wyhash.hash(0, b) else 0; - if (s.ops) |ops| { + // Its file not read (not here): it waits for it, a placeholder. + if (s.ops != null and bytes == null and s.path.len > 0) _ = cl.refused.print("{s} is not here", .{s.path}) else if (s.ops) |ops| { const path_z = s.arena.allocator().dupeSentinel(u8, s.path, 0) catch ""; cl.opening = true; if (ops.open) |f| s.st = f(if (s.path.len > 0) path_z.ptr else null, if (bytes) |b| b.ptr else null, if (bytes) |b| b.len else 0); @@ -320,7 +321,12 @@ fn poke(p: *const Pardes, s: *Slot) void { pub fn fileChanged(p: *Pardes, id: usize, _: []const u8) bool { const pane = p.panes[id] orelse return false; const s = pane.addon orelse return false; - if (!s.live or s.path.len == 0) return false; + if (s.path.len == 0) return false; + // A placeholder waiting for its file: here now, it opens. + if (!s.live) { + @import("dump.zig").reviveSlot(p, id, s); + return s.live; + } // Both cores read it and reopen it alike (the read and its open's // outcome on the tape); the kind's record only the authority has. const bytes: ?[]u8 = @import("fs.zig").read(p, s.path) catch null; @@ -375,7 +381,8 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa } else if (slot.ops == null) { var line: [160]u8 = undefined; const why = if (slot.refused.len > 0) slot.refused.slice() else "Addon load <path>"; - const text = std.fmt.bufPrint(&line, "addon {s} {d} is not loaded: {s}", .{ slot.nameOf(), slot.version, why }) catch &line; + // Its kind here, it waits for its file (`<path> is not here`). + const text = if (p.kinds.byName(slot.nameOf()) != null) why else std.fmt.bufPrint(&line, "addon {s} {d} is not loaded: {s}", .{ slot.nameOf(), slot.version, why }) catch &line; _ = s.print(x, y, cols, text, .{ .fg = fg, .bg = bg }); return; } else frame.cells = (paint(p, slot, id, arena, cols, rows, &frame) orelse return); @@ -387,6 +394,11 @@ pub fn draw(p: *Pardes, s: *surface.Surface, arena: std.mem.Allocator, pane: *Pa }; for (frame.places[0..frame.n]) |pl| { if (pl.rgba == null or pl.w == 0 or pl.h == 0) continue; + if (@as(u64, pl.w) * pl.h * 4 > c.PA_RASTER_MAX) { + if (slot.over & 1 << 31 == 0) std.log.warn("{s}: a picture of {d} MB, over {d} MiB", .{ slot.nameOf(), @as(u64, pl.w) * pl.h * 4 >> 20, c.PA_RASTER_MAX >> 20 }); + slot.over |= 1 << 31; + continue; + } const dx = @min(pl.dst.x, cols); const dy = @min(pl.dst.y, rows); _ = p.appendPlacement(.{ @@ -412,16 +424,16 @@ pub const Frame = struct { buf: [64]c.pa_placement = undefined, }; -/// The one paint of a frame: its cells (in `arena`) and placements into -/// `frame`, its prefix and whether it placed a picture into the slot. -/// Debug checks it changed none of the kind's memory. +/// The one paint of a frame in the live view: its cells (in `arena`) and +/// placements into `frame`, its thumb and pointer into the slot. Debug +/// checks it changed none of the kind's memory. pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, cols: u16, rows: u16, frame: *Frame) ?[]c.pa_cell { const ops = slot.ops orelse return null; const cells = arena.alloc(c.pa_cell, @as(usize, cols) * rows) catch return null; @memset(std.mem.sliceAsBytes(cells), 0); var out: c.pa_paint = std.mem.zeroInit(c.pa_paint, .{ .placements = &frame.buf, .cap = frame.buf.len }); const px = p.view().cell_pixels; - const win = std.mem.zeroInit(c.pa_window, .{ .cells = cells.ptr, .stride = cols, .rows = rows, .cols = cols, .cell_w = px.w, .cell_h = px.h, .focused = @intFromBool(focusedAnywhere(p, id)) }); + const win = std.mem.zeroInit(c.pa_window, .{ .cells = cells.ptr, .stride = cols, .rows = rows, .cols = cols, .cell_w = px.w, .cell_h = px.h, .focused = @intFromBool(focusedAnywhere(p, id)), .view = p.live + 1, .glyphs = @intFromBool(!p.view().frontend.native_images) }); var cl: Call = .{ .p = null, .id = 0, .slot = slot, .quiet = true }; const prev = cl.begin("paint"); const before = if (checked) usedHash(slot) else {}; @@ -430,16 +442,9 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator, _ = cl.end(prev); frame.places = &frame.buf; frame.n = @min(out.n, frame.buf.len); - const own = out.prefix[0..@min(out.prefix_len, out.prefix.len)]; - const placed = for (frame.buf[0..frame.n]) |pl| { - if (pl.rgba != null and pl.w > 0 and pl.h > 0) break true; - } else false; - if (placed != slot.placed or !std.mem.eql(u8, own, slot.prefix.slice())) slot.retag = true; - slot.prefix.set(own); - slot.placed = placed; slot.thumb_at = std.math.clamp(out.at, 0, 1); slot.thumb_span = std.math.clamp(out.span, 0, 1); - slot.pointer = if (caps(p, slot).v22) @intCast(@min(out.pointer, 255)) else 0; + slot.pointer = @intCast(@min(out.pointer, 255)); slot.painted = true; return cells; } @@ -471,9 +476,9 @@ fn colorOf(v: u32, default: surface.Color) surface.Color { }; } -/// The start of its tag: its kind's prefix from its last frame (else the -/// kind's name), the core's art words when that frame placed a picture, -/// and its path. The authority's: a step reads it off the tape on a replica. +/// The start of its tag: its kind's prefix from its last tick (else the +/// kind's name), the core's art words for a kind of PA_PICTURES, and its +/// path. The authority's: a step reads it off the tape on a replica. pub fn prefix(p: *const Pardes, arena: std.mem.Allocator, pane: *const Pane) ![]const u8 { const slot = pane.addon.?; var buf: [400]u8 = undefined; @@ -491,11 +496,13 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { // step's, a notch behind its frames until the next step. if (slot.ops == null and slot.told.len > 0) return slot.told.slice(); const own = if (slot.shadow) |sh| sh.prefix.slice() else slot.prefix.slice(); - const placed = if (slot.shadow) |sh| sh.placed else slot.placed; var w: std.Io.Writer = .fixed(buf); w.writeAll(if (own.len > 0) own else slot.nameOf()) catch {}; - if (placed) w.print(" petscii:{s} palette:{t} ascii:{s}", .{ - if (slot.art.glyph or !p.view().frontend.native_images) "on" else "off", + // The same bytes whichever view's step lays the tag out (17.2): glyph + // art when Petscii says or the session's first view shows no pictures. + const first = &p.views[@min(@ctz(p.views_open), views.max - 1)].?; + if (caps(p, slot).pictures) w.print(" petscii:{s} palette:{t} ascii:{s}", .{ + if (slot.art.glyph or !first.frontend.native_images) "on" else "off", slot.art.palette, if (slot.art.ascii) "on" else "off", }) catch {}; @@ -508,9 +515,8 @@ fn prefixOf(p: *const Pardes, slot: *const Slot, buf: *[400]u8) []const u8 { pub fn key(p: *Pardes, id: usize, k: pardes.Key) bool { const slot = (p.panes[id] orelse return false).addon orelse return false; if (!caps(p, slot).key) return false; - if (k.cp == pardes.Key.escape and !caps(p, slot).esc) return false; if (authorityOnly("a key into a kind's pane")) return true; - const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .pad = 0 }; + const pk: c.pa_key = .{ .cp = k.cp, .ctrl = @intFromBool(k.ctrl), .alt = @intFromBool(k.alt), .shift = @intFromBool(k.shift), .view = p.live + 1 }; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("key"); if (slot.ops) |o| cl.ret = o.key.?(slot.st, &pk); @@ -527,15 +533,16 @@ pub fn takesKeys(p: *const Pardes, s: *const Slot) bool { return caps(p, s).key; } -/// The kind's own rows for the core's `/` (2.1's `search`), in `buf`; null -/// for a kind with none. -pub fn search(p: *Pardes, id: usize, pat: []const u8, buf: []u8) ?[]const u8 { +/// The kind's own rows for the core's `/`, those it found in the call +/// (`api.found`; later ones go to its +Search as they come); null for a +/// kind with no search. +pub fn search(p: *Pardes, id: usize, pat: []const u8) ?[]const u8 { const slot = (p.panes[id] orelse return null).addon orelse return null; if (!caps(p, slot).search) return null; if (authorityOnly("a kind's search")) return null; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("search"); - if (slot.ops) |o| cl.rows = buf[0..@min(buf.len, o.search.?(slot.st, pat.ptr, @intCast(pat.len), buf.ptr, @intCast(buf.len)))]; + if (slot.ops) |o| o.search.?(slot.st, pat.ptr, @intCast(pat.len)); // The rows are the authority's, on the tape: a replica's are those. const o = cl.end(prev); poke(p, slot); @@ -586,7 +593,7 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { .ctrl = @intFromBool(m.ctrl), .shift = @intFromBool(m.shift), .alt = @intFromBool(m.alt), - .pad = 0, + .view = p.live + 1, .col = col, .row = row, .amount = m.amount, @@ -597,24 +604,31 @@ pub fn mouse(p: *Pardes, id: usize, m: pardes.Mouse, col: u16, row: u16) bool { return taken(p, slot, cl.end(prev)); } -/// The end of a step: each pane of a kind whose focus changed, or whose -/// time has come, ticks (16.2). +/// The end of a step: each pane of a kind told of each view whose facts +/// changed (17.1), and each whose focus changed, or whose time has come, +/// ticked (16.2). pub fn step(p: *Pardes) void { const now = p.clock.stepped_ns; - // A tick is the authority's kind's alone: a speculation whose step - // would tick one waits for the authority's unit (guessed untouched, the - // unit rolled it back, a visible rollback an input). + // A kind's call is the authority's kind's alone: a speculation whose + // step would make one waits for the authority's unit (guessed + // untouched, the unit rolled it back, a visible rollback an input). if (tape.speculating()) return if (wouldTick(p, now)) tape.poison("a kind's tick"); for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { - // Its tag laid out again by this step's sync, and drawn. - if (slot.retag) p.needs_frame = true; - slot.retag = false; + if (caps(p, slot).view) for (0..views.max) |k| { + var v = viewOf(p, @intCast(k), id); + if (seen(&v) == slot.views[k]) continue; + slot.views[k] = seen(&v); + var cl: Call = .{ .p = p, .id = id, .slot = slot }; + const prev = cl.begin("view"); + if (slot.ops) |o| o.view.?(slot.st, &v); + _ = cl.end(prev); + poke(p, slot); + p.needs_frame = true; + }; if (!caps(p, slot).tick) continue; const focused = focusedAnywhere(p, id); - const seen = std.hash.Wyhash.hash(0, std.mem.asBytes(&viewOf(p, id))); - if (focused != slot.focused or seen != slot.view) { + if (focused != slot.focused) { slot.focused = focused; - slot.view = seen; slot.next_tick = 1; } // 1 is the next step, whatever the clock says (a session whose @@ -622,25 +636,36 @@ pub fn step(p: *Pardes) void { if (slot.next_tick == 0 or (slot.next_tick > 1 and now < slot.next_tick)) continue; var cl: Call = .{ .p = p, .id = id, .slot = slot }; const prev = cl.begin("tick"); - // A frame drawn since its last tick (2.2): a fling's frames are - // counted by them. - const flags = (if (focused) c.PA_FOCUSED else 0) | (if (slot.painted and caps(p, slot).v22) c.PA_FRAMED else 0); + // A frame drawn since its last tick: a fling's frames are counted by them. + const flags = (if (focused) c.PA_FOCUSED else 0) | (if (slot.painted) c.PA_FRAMED else 0); slot.painted = false; // The clock's now for a tick after an event (a key, a focus): the // stepped frame lags it while the next wake is still to come. - const at = if (slot.next_tick == 1) @max(now, p.clock.now_ns) else now; - if (slot.ops) |o| cl.ret = @bitCast(o.tick.?(slot.st, at, flags)); + var t = std.mem.zeroInit(c.pa_tick, .{ .now = if (slot.next_tick == 1) @max(now, p.clock.now_ns) else now, .flags = flags }); + if (slot.ops) |o| { + o.tick.?(slot.st, &t); + cl.ret = @bitCast(t.next); + slot.prefix.set(t.prefix[0..@min(t.prefix_len, t.prefix.len)]); + } slot.next_tick = @bitCast(cl.end(prev).ret); p.needs_frame = true; repaint(p, slot, id); }; } -/// Its prefix and whether it places a picture as its tick left it, for -/// this step's tag: a frame's paint set them, a step behind the tick (a -/// PDF to its text layer, or back), and a replica guessing its next input -/// took the tag this step laid out. The live view's frame of it, painted -/// and let go; not a frame drawn (`painted` kept). +/// Whether some view's facts are not what its kind was told. +pub fn viewOwed(p: *const Pardes, slot: *const Slot, id: usize) bool { + if (caps(p, slot).view) for (0..views.max) |k| if (seen(&viewOf(p, @intCast(k), id)) != slot.views[k]) return true; + return false; +} + +/// A view's facts as `view` is told them: 0 for one that shows it not. +fn seen(v: *const c.pa_view) u64 { + return if (v.cols == 0) 0 else std.hash.Wyhash.hash(0, std.mem.asBytes(v)); +} + +/// Its thumb and pointer as its tick left them, the live view's frame of +/// it painted and let go; not a frame drawn (`painted` kept). fn repaint(p: *Pardes, slot: *Slot, id: usize) void { const pane = p.panes[id] orelse return; if (slot.ops == null or pane.collapsed) return; @@ -656,23 +681,30 @@ fn repaint(p: *Pardes, slot: *Slot, id: usize) void { _ = paint(p, slot, id, arena.allocator(), cols, rows, &frame); } -/// Whether `step` would tick some pane of a kind now (its rule, read only). +/// Whether `step` would call some pane's kind now (its rule, read only). fn wouldTick(p: *const Pardes, now: u64) bool { for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { + if (viewOwed(p, slot, id)) return true; if (!caps(p, slot).tick) continue; - if (focusedAnywhere(p, id) != slot.focused or std.hash.Wyhash.hash(0, std.mem.asBytes(&viewOf(p, id))) != slot.view) return true; + if (focusedAnywhere(p, id) != slot.focused) return true; if (slot.next_tick == 1 or (slot.next_tick > 1 and now >= slot.next_tick)) return true; }; return false; } -/// Pane `id` as its next paint sees it: its body below its notices, as -/// draw.zig paints it, a cell's pixels, the theme's colours and the -/// raster this frontend wants a picture at. -pub fn viewOf(p: *const Pardes, id: usize) c.pa_view { - const v = p.view(); - const pane = p.panes[id] orelse return std.mem.zeroes(c.pa_view); +/// Pane `id` as view `k` shows it (its id k + 1): its body below its +/// notices, as draw.zig paints it, a cell's pixels, the theme's colours and +/// the raster that view's frontend wants a picture at; no cells when the +/// view is closed or shows it not. +pub fn viewOf(p: *const Pardes, k: u8, id: usize) c.pa_view { + var out = std.mem.zeroes(c.pa_view); + out.id = k + 1; + const v = if (views.isOpen(p, k)) &(p.views[k] orelse return out) else return out; + const pane = p.panes[id] orelse return out; const r = v.rects[id]; + // The live view's share of the pane is in its own fields, another's parked. + const collapsed, const tag_rows, const notices = if (k == p.live) .{ pane.collapsed, pane.tag_rows, pane.notices.span() } else .{ pane.pv[k].collapsed, pane.pv[k].tag_rows, pane.pv[k].notices.span() }; + if (collapsed) return out; const th = p.theme(); const rgb = struct { fn f(x: [3]u8) u32 { @@ -681,8 +713,8 @@ pub fn viewOf(p: *const Pardes, id: usize) c.pa_view { }.f; const policy = pardes.rasterPolicy(v.frontend.windowed); return .{ - .cols = if (pane.collapsed) 0 else r.w -| pardes.config.GUTTER, - .rows = if (pane.collapsed) 0 else (r.h -| pane.tag_rows) -| pane.notices.span(), + .cols = r.w -| pardes.config.GUTTER, + .rows = (r.h -| tag_rows) -| notices, .cell_w = v.cell_pixels.w, .cell_h = v.cell_pixels.h, .fg = rgb(th.fg orelse th.tag_fg), @@ -693,7 +725,8 @@ pub fn viewOf(p: *const Pardes, id: usize) c.pa_view { .max_px = policy.max_dimension, .fill = @intFromBool(policy.match_viewport), .glyphs = @intFromBool(!v.frontend.native_images), - .pad = @splat(0), + .id = k + 1, + .pad = 0, }; } @@ -703,12 +736,12 @@ fn focusedAnywhere(p: *const Pardes, id: usize) bool { return false; } -/// When the kinds next want a step: the soonest deadline (now, for a -/// tag to lay out again), or null. +/// When the kinds next want a step: the soonest deadline (now, for a view +/// whose facts a step's layout changed after the kind was told them), or null. pub fn wake(p: *const Pardes) ?u64 { var soonest: ?u64 = null; - for (p.panes) |pslot| if (pslot) |pane| if (pane.addon) |slot| { - const due = if (slot.retag) @max(1, p.clock.stepped_ns) else slot.next_tick; + for (p.panes, 0..) |pslot, id| if (pslot) |pane| if (pane.addon) |slot| { + const due = if (viewOwed(p, slot, id)) @max(1, p.clock.stepped_ns) else slot.next_tick; if (due != 0) soonest = @min(soonest orelse due, due); }; return soonest; @@ -720,6 +753,11 @@ pub fn jobDone(p: *Pardes, pane_id: u8, serial: u32, token: u32, gen: u32, bytes const pane = p.panes[pane_id] orelse return; const slot = pane.addon orelse return; if (pane.serial != serial or slot.gen != gen or !caps(p, slot).jobs) return; + // Cancelled: dropped, on every core (the cancel was on the tape). + if (std.mem.indexOfScalar(u32, &slot.cancelled, token)) |i| { + slot.cancelled[i] = 0; + return; + } var cl: Call = .{ .p = p, .id = pane_id, .slot = slot }; const prev = cl.begin("job_done"); if (slot.ops) |o| o.job_done.?(slot.st, token, bytes.ptr, @intCast(bytes.len)); @@ -829,8 +867,10 @@ pub const Outcome = struct { selected: ?[]const u8 = null, rows: []const u8 = "", /// The jobs it asked for (their tokens, 0 past the last): every core - /// emits them, so a replica's effects are the authority's. + /// emits them, so a replica's effects are the authority's; and those + /// it cancelled, every core drops. jobs: [8]u32 = @splat(0), + cancels: [4]u32 = @splat(0), }; /// The call under way: what `api` acts on, and what it gathers. @@ -856,9 +896,13 @@ const Call = struct { selected: ?[]const u8 = null, rows: []const u8 = "", jobs: [8]u32 = @splat(0), + cancels: [4]u32 = @splat(0), + /// Debug: when it began, for the budget. + began: u64 = 0, fn begin(cl: *Call, comptime name: []const u8) ?*Call { cl.name = name; + if (checked) cl.began = @import("FrameStats.zig").nowNs(); const prev = current; current = cl; return prev; @@ -867,9 +911,18 @@ const Call = struct { /// The call's outcome, as the authority's took it, done. fn end(cl: *Call, prev: ?*Call) Outcome { current = prev; + if (checked) overBudget(cl); if (cl.quiet) return .{}; - const o = tape.value(.addon, same, .{Outcome{ .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .jobs = cl.jobs }}); + const o = tape.value(.addon, same, .{Outcome{ .ret = cl.ret, .text = cl.refused.slice(), .said = cl.said.slice(), .err = cl.err, .look = cl.look.slice(), .pane_name = cl.pane_name.slice(), .pane_text = cl.pane_text, .exec = cl.exec.slice(), .selected = cl.selected, .rows = cl.rows, .jobs = cl.jobs, .cancels = cl.cancels }}); if (cl.p) |p| if (p.panes[cl.id]) |pane| for (o.jobs) |token| if (token != 0) p.emit(.{ .job = .{ .pane = @intCast(cl.id), .serial = pane.serial, .token = token } }); + for (o.cancels) |token| if (token != 0) { + for (&cl.slot.jobs) |*j| if (j.*) |job| if (job.token == token) { + cl.slot.gpa.free(job.in); + j.* = null; + }; + std.mem.copyForwards(u32, cl.slot.cancelled[1..], cl.slot.cancelled[0..3]); + cl.slot.cancelled[0] = token; + }; if (o.selected) |sel| { cl.slot.gpa.free(cl.slot.selection); cl.slot.selection = cl.slot.gpa.dupe(u8, sel) catch &.{}; @@ -883,11 +936,44 @@ const Call = struct { if (o.pane_name.len > 0) textPane(p, cl.id, cl.slot, o.pane_name, o.pane_text); if (o.look.len > 0) @import("look.zig").lookAt(p, cl.id, o.look); if (o.exec.len > 0) @import("mouse.zig").dispatchPointerBuiltin(p, cl.id, pardes.config.exec_cmd, o.exec, null); + // Rows found after its search (a job's): onto its +Search. + if (o.rows.len > 0 and !std.mem.eql(u8, cl.name, "search")) moreFound(p, cl.id, o.rows); return o; } }; var current: ?*Call = null; +/// A call's budget, in Debug: a frame (17.4; 2 ms in ReleaseFast, which +/// Debug's kinds run three to four times slower than). +const budget_ns = 16 * std.time.ns_per_ms; +const sites = [_][]const u8{ "open", "close", "tick", "paint", "view", "key", "mouse", "body", "address", "where", "ctl", "ctl_read", "job_done", "dump", "search" }; +/// The first call over budget, `pdf: search took 118 ms, over 16 ms`: +/// what `--addon-test` fails on. +pub var breach: Buf(96) = .{}; + +fn overBudget(cl: *Call) void { + const took = @import("FrameStats.zig").nowNs() -| cl.began; + const site = for (sites, 0..) |n, i| { + if (std.mem.eql(u8, n, cl.name)) break i; + } else return; + if (took <= budget_ns or cl.slot.over & @as(u32, 1) << @intCast(site) != 0) return; + cl.slot.over |= @as(u32, 1) << @intCast(site); + var line: Buf(96) = .{}; + std.log.warn("{s}", .{line.print("{s}: {s} took {d} ms, over {d} ms", .{ cl.slot.nameOf(), cl.name, took / std.time.ns_per_ms, budget_ns / std.time.ns_per_ms })}); + if (breach.len == 0) breach = line; +} + +/// Rows a kind found after its search answered, appended to the pane's +/// +Search (the list its last `/` made), as a file search's stream in. +fn moreFound(p: *Pardes, id: usize, rows: []const u8) void { + const pane = p.panes[id] orelse return; + const rp = p.panes[pane.search_pane orelse return] orelse return; + const rf = if (rp.file) |*f| f else return; + const o = if (rf.output) |*x| x else return; + const content = std.mem.concat(p.gpa, u8, &.{ rf.content, rows }) catch return; + pardes.panes.Output.fillResults(p, id, std.fs.path.dirname(rf.path) orelse p.sessionDir(), .search, o.arg(), content, null) catch |err| p.reportError(id, "found", err); +} + fn same(o: Outcome) Outcome { return o; } @@ -906,9 +992,15 @@ pub const api: c.pa_api = .{ .alloc = struct { fn f(n: usize) callconv(.c) ?*anyopaque { const cl = current orelse return null; + // Its time the core's, not the kind's budget's (a region's + // first allocation is slow in Debug), as a job's copy is. + const t0 = if (checked) @import("FrameStats.zig").nowNs(); + defer if (checked) { + cl.began += @import("FrameStats.zig").nowNs() -| t0; + }; const mem = cl.slot.arena.allocator().alignedAlloc(u8, .@"16", @max(n, 1)) catch return null; @memset(mem, 0); - if (checked) cl.slot.used.append(cl.slot.gpa, mem) catch {}; + if (checked) cl.slot.used.append(std.heap.page_allocator, mem) catch {}; return mem.ptr; } }.f, @@ -928,6 +1020,10 @@ pub const api: c.pa_api = .{ fn f(in: ?*const anyopaque, n: u32) callconv(.c) u32 { const cl = current orelse return 0; if (cl.p == null or cl.quiet) return 0; + const t0 = if (checked) @import("FrameStats.zig").nowNs(); + defer if (checked) { + cl.began += @import("FrameStats.zig").nowNs() -| t0; + }; const s = cl.slot; const free = for (&s.jobs) |*j| { if (j.* == null) break j; @@ -987,12 +1083,6 @@ pub const api: c.pa_api = .{ return kind_heap.realloc(ptr, n); } }.f, - .view = struct { - fn f(out: [*c]c.pa_view) callconv(.c) void { - const cl = current orelse return; - out.* = if (cl.p) |p| viewOf(p, cl.id) else std.mem.zeroes(c.pa_view); - } - }.f, .exec = struct { fn f(text: [*c]const u8, n: u32) callconv(.c) void { const cl = current orelse return; @@ -1007,6 +1097,23 @@ pub const api: c.pa_api = .{ cl.selected = p.scratch.allocator().dupe(u8, if (text == null) "" else text[0..n]) catch return; } }.f, + .cancel = struct { + fn f(token: u32) callconv(.c) void { + const cl = current orelse return; + if (cl.quiet or token == 0) return; + const free = std.mem.indexOfScalar(u32, &cl.cancels, 0) orelse return; + cl.cancels[free] = token; + } + }.f, + .found = struct { + /// Gathered in the call: the search's answer, or onto its +Search after. + fn f(rows: [*c]const u8, n: u32) callconv(.c) void { + const cl = current orelse return; + const p = cl.p orelse return; + if (cl.quiet or rows == null) return; + cl.rows = std.mem.concat(p.scratch.allocator(), u8, &.{ cl.rows, rows[0..n] }) catch return; + } + }.f, }; /// What `api.heap` hands out: memory that outlives a call, from any @@ -1035,7 +1142,7 @@ pub const test_kind = struct { out.*.placements[0] = std.mem.zeroInit(c.pa_placement, .{ .rgba = &pixel, .w = 1, .h = 1, .src = .{ .x = 0, .y = 0, .w = 1, .h = 1 }, .dst = .{ .x = 0, .y = 0, .w = win.*.cols, .h = win.*.rows } }); out.*.n = 1; } - const ops = std.mem.zeroInit(Ops, .{ .head = std.mem.zeroInit(c.pa_head, .{ .size = @sizeOf(c.pa_head), .abi = abi, .name = "test".* ++ @as([12]u8, @splat(0)), .version = 1 }), .paint = picture }); + const ops = std.mem.zeroInit(Ops, .{ .head = std.mem.zeroInit(c.pa_head, .{ .size = @sizeOf(c.pa_head), .abi = abi, .name = "test".* ++ @as([12]u8, @splat(0)), .version = 1, .flags = c.PA_PICTURES }), .paint = picture }); /// Its index in `p`'s kinds, taken in at the first ask. pub fn of(p: *Pardes) u8 { |
