summaryrefslogtreecommitdiff
path: root/anim/src/segments.zig
blob: db9d28fab3ba06ea42e28377ef3c3c295db2e85b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
//! `segments` (design/README.md, "segments"; motion.md, "The address space
//! enlarged"; mocks 30 and 34, treatment A): the user half of the address
//! space to true scale, a slim bar, and each cluster of mappings enlarged
//! once, linear, a box beside it with every named band's start and end
//! addresses and its size. Everything from the recording's mappings at one
//! moment (`Moment.mappings`, `mapping`): nothing is placed by hand.
//!
//! Drawn in the program column (x 64, 880 wide), between `top` and `foot`
//! (the column's 84 to 888, the working frame's 80 to 864: `form`); the
//! scene plays its clocks (frames, linear): `whole` (the bar, its ends, the
//! mappings' lines), each cluster's `enlarge`, a band's `ink` echo. `Place`
//! is a dump's place in it: the `tie` bracket and the `link` onto the
//! stack's box.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const zt = @import("ztrace");
const ui = @import("ui.zig");
const Recording = @import("Recording.zig");
const theme = ui.theme;
const Rect = ui.Rect;
const Vec = ui.Vec;
const t = theme.color.text;
const region = theme.color.region;

/// The user half: 0 to 7fffffffffff.
pub const user_end: u64 = 1 << 47;

/// Touching mappings of one file and one permission (an anonymous one right
/// after a file's, its bss, with it).
pub const Band = struct { lo: u64, hi: u64, prot: u64, name: []const u8, ink: u24 };
/// What has a row of names: a file once (the program by segment, each band its own).
pub const Unit = struct { lo: u64, hi: u64, prot: ?u64, name: []const u8, ink: u24, program: bool };
pub const Cluster = struct { lo: u64, hi: u64, bands: [2]u16, units: [2]u16 };

pub const Space = struct {
    bands: []Band,
    units: []Unit,
    clusters: []Cluster,
    program: []const u8,
};

const Kind = zt.telescope.Region;

fn inkOf(kind: Kind) u24 {
    return switch (kind) {
        .code, .rwx => region.code_,
        .stack => region.stack,
        .heap => region.heap,
        .data => region.data,
        .rodata => region.rodata,
        .none => region.number,
    };
}

/// A mapping's file as a name: its path's last part, without the kernel's ` (deleted)`.
fn nameOf(path: []const u8) []const u8 {
    const base = if (std.mem.lastIndexOfScalar(u8, path, '/')) |k| path[k + 1 ..] else path;
    return if (std.mem.endsWith(u8, base, " (deleted)")) base[0 .. base.len - " (deleted)".len] else base;
}

/// The address space at moment `at`: its bands, the units named, the
/// clusters top down (highest first). A cluster is mappings with less than
/// 16 MiB between them; a band more than eight times the rest of its
/// cluster is a cluster of its own (the tracer's ring under libc: in the
/// libraries' box it would leave libc 12 units tall).
pub fn read(rec: *const Recording, at: u64, arena: std.mem.Allocator) Space {
    const mo = rec.moment(at);
    const maps = mo.mappings(arena) catch &.{};
    var bands: std.ArrayList(Band) = .empty;
    var path_buf: [512]u8 = undefined;
    // The tracer's own (its ring, its preloaded agent, and an agent's bss
    // after it) are not the program's: left out, as mock 34 has them.
    var tracers_end: u64 = 0;
    for (maps) |mp| {
        if (mp.addr >= user_end) continue;
        var path: []const u8 = "";
        _ = mo.mapping(mp.addr, &path, &path_buf) catch null;
        const name = arena.dupe(u8, nameOf(path)) catch "";
        if (std.mem.indexOf(u8, name, "ztrace") != null or (name.len == 0 and mp.addr == tracers_end)) {
            tracers_end = mp.addr + mp.len;
            continue;
        }
        if (bands.items.len > 0) {
            const last = &bands.items[bands.items.len - 1];
            if (last.hi == mp.addr and last.prot == mp.prot and (std.mem.eql(u8, last.name, name) or (name.len == 0 and last.name.len > 0 and last.name[0] != '['))) {
                last.hi = mp.addr + mp.len;
                continue;
            }
        }
        bands.append(arena, .{ .lo = mp.addr, .hi = mp.addr + mp.len, .prot = mp.prot, .name = name, .ink = inkOf(Kind.of(mp, name)) }) catch break;
    }
    // The program: the file the pc is in.
    var pc_path: []const u8 = "";
    _ = mo.mapping(rec.register(at, "rip"), &pc_path, &path_buf) catch null;
    const program = arena.dupe(u8, nameOf(pc_path)) catch "";

    // Units: the program's bands each, every other file once.
    var units: std.ArrayList(Unit) = .empty;
    var k: usize = 0;
    while (k < bands.items.len) {
        const b = bands.items[k];
        if (b.name.len == 0) {
            k += 1;
            continue;
        }
        if (std.mem.eql(u8, b.name, program)) {
            units.append(arena, .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = true }) catch break;
            k += 1;
            continue;
        }
        var u: Unit = .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = false };
        k += 1;
        while (k < bands.items.len and bands.items[k].lo == u.hi and std.mem.eql(u8, bands.items[k].name, u.name)) : (k += 1) {
            u.hi = bands.items[k].hi;
            if (u.prot != bands.items[k].prot) {
                u.prot = null;
                u.ink = region.rodata;
            }
        }
        units.append(arena, u) catch break;
    }

    // Clusters, low to high first, then turned top down.
    var clusters: std.ArrayList(Cluster) = .empty;
    var first: usize = 0;
    for (bands.items, 0..) |b, i| {
        const last = i + 1 == bands.items.len or bands.items[i + 1].lo - b.hi >= 16 << 20;
        if (!last) continue;
        splitOff(arena, &clusters, bands.items, first, i + 1);
        first = i + 1;
    }
    std.mem.reverse(Cluster, clusters.items);
    for (clusters.items) |*c| {
        var lo_u: u16 = @intCast(units.items.len);
        var hi_u: u16 = 0;
        for (units.items, 0..) |u, i| if (u.lo >= c.lo and u.hi <= c.hi) {
            lo_u = @min(lo_u, @as(u16, @intCast(i)));
            hi_u = @max(hi_u, @as(u16, @intCast(i + 1)));
        };
        c.units = if (hi_u > lo_u) .{ lo_u, hi_u } else .{ 0, 0 };
    }
    return .{ .bands = bands.items, .units = units.items, .clusters = clusters.items, .program = program };
}

/// Bands `a` to `b` as a cluster, or three where one band outweighs the rest eightfold.
fn splitOff(arena: std.mem.Allocator, out: *std.ArrayList(Cluster), bands: []const Band, a: usize, b: usize) void {
    var big = a;
    for (a..b) |i| if (bands[i].hi - bands[i].lo > bands[big].hi - bands[big].lo) {
        big = i;
    };
    const whole = bands[b - 1].hi - bands[a].lo;
    const len = bands[big].hi - bands[big].lo;
    if (b - a > 1 and len > 8 * (whole - len)) {
        for ([_][2]usize{ .{ a, big }, .{ big, big + 1 }, .{ big + 1, b } }) |r| if (r[1] > r[0]) {
            out.append(arena, .{ .lo = bands[r[0]].lo, .hi = bands[r[1] - 1].hi, .bands = .{ @intCast(r[0]), @intCast(r[1]) }, .units = .{ 0, 0 } }) catch {};
        };
        return;
    }
    out.append(arena, .{ .lo = bands[a].lo, .hi = bands[b - 1].hi, .bands = .{ @intCast(a), @intCast(b) }, .units = .{ 0, 0 } }) catch {};
}

/// A size as the names give it: `4 KiB`, `132 KiB`, `2.1 MiB`, `64 MiB`, `128 TiB`.
pub fn size(buf: []u8, bytes: u64) []const u8 {
    const units = [_][]const u8{ "KiB", "MiB", "GiB", "TiB" };
    var v: f64 = @as(f64, @floatFromInt(bytes)) / 1024;
    var u: usize = 0;
    while (v >= 1024 and u + 1 < units.len) : (u += 1) v /= 1024;
    if (u > 0 and v < 10 and @abs(v - @round(v)) >= 0.05) return std.fmt.bufPrint(buf, "{d:.1} {s}", .{ v, units[u] }) catch "";
    return std.fmt.bufPrint(buf, "{d} {s}", .{ @as(u64, @intFromFloat(@round(v))), units[u] }) catch "";
}

/// `name` cut with `…` to fit `room` (a library's long file name in the program column).
fn cut(u: ui.Ui, name: []const u8, room: f32) []const u8 {
    const most: usize = @intFromFloat(@max(room / ui.advance(m.mono), 2));
    return if (name.len > most) u.print("{s}\u{2026}", .{name[0 .. most - 1]}) else name;
}

fn perms(prot: u64) [3]u8 {
    return .{ if (prot & 1 != 0) 'r' else '-', if (prot & 2 != 0) 'w' else '-', if (prot & 4 != 0) 'x' else '-' };
}

/// Rows `pitch` apart, each as near its target as the others let it
/// (crowded ones pooled round their mean), in order, centres in `lo` to `hi`.
pub fn pool(targets: []const f32, out: []f32, lo: f32, hi: f32, pitch: f32) void {
    const n = targets.len;
    if (n == 0) return;
    var sums: [64]f32 = undefined;
    var counts: [64]f32 = undefined;
    var nb: usize = 0;
    for (targets, 0..) |target, i| {
        sums[nb] = target - pitch * @as(f32, @floatFromInt(i));
        counts[nb] = 1;
        nb += 1;
        while (nb > 1 and sums[nb - 2] / counts[nb - 2] > sums[nb - 1] / counts[nb - 1]) {
            sums[nb - 2] += sums[nb - 1];
            counts[nb - 2] += counts[nb - 1];
            nb -= 1;
        }
    }
    var i: usize = 0;
    const top = @max(hi - pitch * @as(f32, @floatFromInt(n - 1)), lo);
    for (sums[0..nb], counts[0..nb]) |s, c| for (0..@intFromFloat(c)) |_| {
        out[i] = std.math.clamp(s / c, lo, top) + pitch * @as(f32, @floatFromInt(i));
        i += 1;
    };
}

// ------------------------------------------------------------- the drawing

/// The program column's measures (mock 30): the bar 20 wide (inside 16),
/// the boxes 108 wide inside, the leaders' room, then the names.
const m = struct {
    const bar = 160;
    const bar_w = 20;
    const box = 304;
    const box_w = 108;
    const leader = 8;
    const address = 476;
    const indent = 24;
    const size_end = 632;
    const name = 656;
    const pitch = 24;
    const mono = theme.size.mono.context;
    /// Box heights: a cluster of one band; the rest share what is left.
    const one = 72;
    const gap_min = 24;
};

/// Frames of `enlarge` (motion.md): the line framed, the ride, the page lines, the names.
const e = struct {
    const frame = 6;
    const ride = 72;
    const pages = 12;
    const names = 90;
    const apart = 2;
    const leader = 6;
    const fade = 12;
};

/// Under the camera (nearer than x1), nothing drawn under the stage (y 888):
/// the caption's band is the caption's. Returns the clip it replaced.
pub fn clipStage(f: *sk.Frame, placed: anim.Placed) ??sk.Rect {
    const w = @floor(f.in.window_width);
    const h = @floor(f.in.window_height);
    const one = @min(w / theme.canvas.width, h / theme.canvas.height);
    if (one <= 0 or placed.scale / one < 1.01) return null;
    const was = ui.bound;
    const stage: sk.Rect = .{ .x = 0, .y = 0, .w = w, .h = (h - theme.canvas.height * one) / 2 + (theme.canvas.stage.y + theme.canvas.stage.h) * one };
    ui.bound = stage;
    f.clip(stage);
    return was;
}

pub fn unclipStage(f: *sk.Frame, was: ??sk.Rect) void {
    const old = was orelse return;
    ui.bound = old;
    if (old) |r| f.clip(r) else f.clip(null);
}

pub const Segments = struct {
    rec: *const Recording,
    at: u64,
    x: f32 = theme.layout.program.x,
    /// 0: the column's height (84 to 888); 1: the working frame's (80 to 864).
    form: anim.Value,
    /// Frames into the whole's build (negative: not begun).
    whole: anim.Value,
    /// Frames into each cluster's `enlarge`, top down (negative: not begun).
    rides: [6]anim.Value,
    /// `ink`'s echoes: the band holding `addr` framed as read, `shows` of it.
    echoes: [16]Echo = undefined,
    n_echoes: usize = 0,

    pub const Echo = struct { addr: u64, shows: anim.Value };

    pub fn init(s: *anim.Frame, rec: *const Recording, at: u64) Segments {
        var rides: [6]anim.Value = undefined;
        for (&rides) |*r| r.* = s.value(-1);
        return .{ .rec = rec, .at = at, .form = s.value(0), .whole = s.value(-1), .rides = rides };
    }

    /// The bar's top and foot at `form` (0 the column, 1 the working frame).
    fn span(form: f32) [2]f32 {
        const k = theme.ease(.glide, form);
        return .{ std.math.lerp(84, 80, k), std.math.lerp(888, 864, k) };
    }

    pub const Layout = struct {
        space: Space,
        top: f32,
        foot: f32,
        /// Each cluster's box, as it lands (inside its frame).
        boxes: [6]Rect,
        /// Each cluster's line on the whole.
        lines: [6]f32,
    };

    /// Where everything is at `form` (`Segments.form`'s value).
    pub fn layout(g: *const Segments, form: f32, arena: std.mem.Allocator) Layout {
        const ends = span(form);
        const space = read(g.rec, g.at, arena);
        var out: Layout = .{ .space = space, .top = ends[0], .foot = ends[1], .boxes = undefined, .lines = undefined };
        const n = @min(space.clusters.len, 6);
        // Heights: a cluster of one band 72; the program's as its rows; the
        // libraries' (the most units but the program's) what is left.
        var heights: [6]f32 = @splat(0);
        var fixed: f32 = 0;
        var rest: ?usize = null;
        for (space.clusters[0..n], 0..) |c, i| {
            const lines: f32 = @floatFromInt(2 * (c.units[1] - c.units[0]) + 1);
            const has_program = for (space.units[c.units[0]..c.units[1]]) |u| {
                if (u.program) break true;
            } else false;
            if (c.bands[1] - c.bands[0] == 1 or lines <= 3) heights[i] = m.one else if (has_program) heights[i] = m.pitch * lines - 12 else if (rest == null) {
                rest = i;
                continue;
            } else heights[i] = m.pitch * lines - 12;
            fixed += heights[i];
        }
        const gaps: f32 = @floatFromInt(@max(n, 1) - 1);
        const room = ends[1] - ends[0];
        var gap: f32 = 44;
        if (rest) |r| heights[r] = @max(room - fixed - gap * gaps, m.one) else gap = @max((room - fixed) / @max(gaps, 1), m.gap_min);
        var y = ends[0];
        for (0..n) |i| {
            out.boxes[i] = .{ .x = g.x + m.box, .y = y, .w = m.box_w, .h = heights[i] };
            y += heights[i] + gap;
            // The lines on the whole: the top clusters 2 apart under its top, the foot's over its foot.
            out.lines[i] = if (space.clusters[i].lo > user_end / 2) ends[0] + 2 + 2 * @as(f32, @floatFromInt(i)) else ends[1] - 4 - 2 * @as(f32, @floatFromInt(n - 1 - i));
        }
        return out;
    }

    /// Where `addr` is in its cluster's box once it has landed; null outside every box.
    pub fn place(l: Layout, addr: u64) ?Vec {
        for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) {
            const b = l.boxes[i];
            return .{ b.x + b.w / 2, b.y + b.h * @as(f32, @floatCast(@as(f64, @floatFromInt(c.hi - addr)) / @as(f64, @floatFromInt(c.hi - c.lo)))) };
        };
        return null;
    }

    pub fn draw(g: *const Segments, f: *sk.Frame, placed: anim.Placed) void {
        const u: ui.Ui = .{ .f = f, .at = placed };
        const l = g.layout(placed.value(g.form), f.scratch);
        const whole = placed.value(g.whole);
        if (whole < 0) return;
        const was = clipStage(f, placed);
        defer unclipStage(f, was);
        const bar_x = g.x + m.bar;
        const h = l.foot - l.top;
        // The bar, from the top down (72 f), framed 2 in `dim`, coming in over its first 6 f.
        const grown = h * theme.ease(.standard, std.math.clamp(whole / 72, 0, 1));
        u.faded(std.math.clamp(whole / 6, 0, 1)).frame(.{ .x = bar_x, .y = l.top, .w = m.bar_w, .h = @max(grown, 4) }, t.dim, theme.stroke.rule);
        // Its ends' addresses and its size (18 f), ranged right 12 left of it.
        const ends_in = std.math.clamp((whole - 72) / 18, 0, 1);
        if (ends_in > 0) {
            const v = u.faded(ends_in);
            var buf: [16]u8 = undefined;
            for ([_]struct { []const u8, f32 }{ .{ "7fffffffffff", l.top }, .{ "0", l.foot }, .{ size(&buf, user_end), (l.top + l.foot) / 2 } }) |row|
                v.text(.mono, m.mono, bar_x - 12 - v.width(.mono, m.mono, row[0]), row[1] + 7.2, row[0], t.dim);
        }
        // Every mapping's line inside its end (12 f each, across 72 f).
        const n = @min(l.space.clusters.len, 6);
        for (0..n) |i| {
            const shows = std.math.clamp((whole - 90 - 60 * @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(@max(n, 2) - 1))) / 12, 0, 1);
            if (shows > 0) u.faded(shows).fill(.{ .x = bar_x + 2, .y = l.lines[i], .w = m.bar_w - 4, .h = 2 }, biggest(l.space, l.space.clusters[i]).ink);
        }
        for (0..n) |i| g.enlarged(u, l, i, placed.value(g.rides[i]));
        for (g.echoes[0..g.n_echoes]) |echo| g.echoed(u, l, echo.addr, placed.value(echo.shows));
    }

    /// The ink of a cluster's largest band (its line on the whole).
    fn biggest(space: Space, c: Cluster) Band {
        var best = space.bands[c.bands[0]];
        for (space.bands[c.bands[0]..c.bands[1]]) |b| if (b.hi - b.lo > best.hi - best.lo) {
            best = b;
        };
        return best;
    }

    /// Cluster `i`'s `enlarge`, `clock` frames in.
    fn enlarged(g: *const Segments, line_u: ui.Ui, l: Layout, i: usize, clock: f32) void {
        if (clock < 0) return;
        const c = l.space.clusters[i];
        const land = l.boxes[i];
        const bar_r = g.x + m.bar + m.bar_w;
        const line_y = l.lines[i] + 1;
        // The line framed bright (2 units, 3 out), going as the box lands.
        const bright = std.math.clamp(clock / e.frame, 0, 1) * (1 - std.math.clamp((clock - 66) / 12, 0, 1));
        if (bright > 0) line_u.faded(bright).frame(.{ .x = g.x + m.bar - 1, .y = line_y - 5, .w = m.bar_w + 2, .h = 10 }, t.bright, theme.stroke.rule);
        // The ride: the box's left corners along the magnification's lines.
        const p = theme.ease(.glide, std.math.clamp((clock - e.frame) / e.ride, 0, 1));
        if (p <= 0) return;
        const box: Rect = .{
            .x = std.math.lerp(bar_r, land.x, p),
            .y = std.math.lerp(line_y, land.y, p),
            .w = std.math.lerp(m.bar_w - 4, land.w, p),
            .h = std.math.lerp(0, land.h, p),
        };
        // Out of the line: the box comes in over the ride's first frames, never in one.
        const u = line_u.faded(std.math.clamp(p * 8, 0, 1));
        u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y - 1 }, theme.color.rule, theme.stroke.rule);
        u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y + box.h + 1 }, theme.color.rule, theme.stroke.rule);
        // Its bands at its scale all the way: a mapping a 2-unit line until it is taller.
        const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
        const pages = std.math.clamp((clock - e.frame - e.ride) / e.pages, 0, 1);
        u.clip(.{ .x = box.x, .y = box.y - 1, .w = box.w, .h = box.h + 2 });
        for (l.space.bands[c.bands[0]..c.bands[1]], c.bands[0]..) |b, k| {
            const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
            const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2);
            u.fill(.{ .x = box.x, .y = top, .w = box.w, .h = bh }, ui.mix(theme.color.page, b.ink, 0.3));
            // Between two bands of one file a page line, where both are 6 tall.
            if (k + 1 < c.bands[1] and pages > 0) {
                const below = l.space.bands[k + 1];
                if (below.hi == b.lo and std.mem.eql(u8, below.name, b.name) and b.name.len > 0 and bh >= 6 and @as(f32, @floatFromInt(below.hi - below.lo)) * scale >= 6)
                    u.faded(pages).fill(.{ .x = box.x, .y = top + bh - 1, .w = box.w, .h = 2 }, theme.color.page);
            }
        }
        u.unclip();
        u.frame(.{ .x = box.x - 2, .y = box.y - 2, .w = box.w + 4, .h = box.h + 4 }, t.dim, theme.stroke.rule);
        // Its names, rows top down, once it has landed.
        g.names(u, l, i, clock - e.names, scale);
    }

    const Row = struct { kind: enum { edge, unit }, addr: u64, unit: usize, target: f32, ink: u24 };

    /// Cluster `i`'s rows of names: an edge of every named unit, each unit
    /// between its edges, top down (highest address first).
    fn rows(l: Layout, i: usize, arena: std.mem.Allocator) []Row {
        const c = l.space.clusters[i];
        const box = l.boxes[i];
        const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
        var out: std.ArrayList(Row) = .empty;
        var k = c.units[1];
        while (k > c.units[0]) {
            k -= 1;
            const un = l.space.units[k];
            const tall = @as(f32, @floatFromInt(un.hi - un.lo)) * scale;
            if (!un.program and tall < 6) continue;
            const yh = box.y + @as(f32, @floatFromInt(c.hi - un.hi)) * scale;
            const yl = box.y + @as(f32, @floatFromInt(c.hi - un.lo)) * scale;
            const last = if (out.items.len > 0) out.items[out.items.len - 1] else null;
            if (last == null or last.?.kind != .edge or last.?.addr != un.hi) out.append(arena, .{ .kind = .edge, .addr = un.hi, .unit = k, .target = yh, .ink = un.ink }) catch {};
            out.append(arena, .{ .kind = .unit, .addr = un.lo, .unit = k, .target = std.math.clamp((yh + yl) / 2, box.y, box.y + box.h), .ink = un.ink }) catch {};
            out.append(arena, .{ .kind = .edge, .addr = un.lo, .unit = k, .target = yl, .ink = un.ink }) catch {};
        }
        // An edge two units share takes the ink of the one that starts there (its lowest address).
        for (out.items) |*r| if (r.kind == .edge) for (l.space.units[c.units[0]..c.units[1]]) |un| if (un.lo == r.addr) {
            r.ink = un.ink;
        };
        return out.items;
    }

    /// Where cluster `i`'s rows may stand: its share of the column, down to the gaps' middles.
    fn share(l: Layout, i: usize) [2]f32 {
        const n = @min(l.space.clusters.len, 6);
        const lo = if (i == 0) l.top else (l.boxes[i - 1].y + l.boxes[i - 1].h + l.boxes[i].y) / 2 + 12;
        const hi = if (i + 1 == n) l.foot + 12 else (l.boxes[i].y + l.boxes[i].h + l.boxes[i + 1].y) / 2 - 12;
        return .{ lo, hi };
    }

    fn names(g: *const Segments, u: ui.Ui, l: Layout, i: usize, clock: f32, scale: f32) void {
        if (clock < 0) return;
        const all = rows(l, i, u.f.scratch);
        if (all.len == 0) return;
        var targets: [64]f32 = undefined;
        var ys: [64]f32 = undefined;
        const n = @min(all.len, 64);
        for (all[0..n], 0..) |r, k| targets[k] = r.target;
        const room = share(l, i);
        pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch);
        const box = l.boxes[i];
        const right = box.x + box.w + 2;
        const ax = g.x + m.address;
        var buf: [32]u8 = undefined;
        var first_program: ?usize = null;
        var last_program: usize = 0;
        for (all[0..n], 0..) |r, k| {
            const at = clock - @as(f32, @floatFromInt(k)) * e.apart;
            if (at <= 0) break;
            const y = ys[k];
            const un = l.space.units[r.unit];
            switch (r.kind) {
                .edge => {
                    const drawn = std.math.clamp(at / e.leader, 0, 1);
                    u.route(&.{ .{ right, r.target }, .{ right + m.leader, r.target }, .{ ax - 20, y }, .{ ax - 8, y } }, theme.color.rule, .{ .width = theme.stroke.rule, .head = null, .progress = drawn, .radius = 0 });
                    const shows = std.math.clamp((at - e.leader) / e.fade, 0, 1);
                    if (shows > 0) _ = u.faded(shows).address(ax, y + 7.2, r.addr, .{ .ink = ui.rgb(r.ink) });
                },
                .unit => {
                    const v = u.faded(std.math.clamp(at / e.fade, 0, 1));
                    if (un.prot) |p| v.text(.mono, m.mono, ax + m.indent, y + 7.2, &perms(p), t.dim);
                    const sz = size(&buf, un.hi - un.lo);
                    v.text(.mono, m.mono, g.x + m.size_end - v.width(.mono, m.mono, sz), y + 7.2, sz, t.dim);
                    if (un.program) {
                        if (first_program == null) first_program = k;
                        last_program = k;
                    } else v.text(.mono, m.mono, g.x + m.name, y + 7.2, cut(v, un.name, theme.layout.program.w - 12 - m.name), t.primary);
                    // A file whose permissions differ: each part's written inside its band, where it is 28 tall.
                    if (un.prot == null) g.insides(v, l, i, un, scale);
                },
            }
        }
        // The program's bracket and name, with its last row.
        if (first_program) |a| {
            const at = clock - @as(f32, @floatFromInt(last_program)) * e.apart;
            const v = u.faded(std.math.clamp(at / e.fade, 0, 1));
            const bx = g.x + m.name - 14;
            v.bracket(bx, ys[a] - 8, ys[last_program] + 8, theme.color.rule, theme.metric.bracket.quiet, -1, false);
            v.text(.mono, m.mono, g.x + m.name, (ys[a] + ys[last_program]) / 2 + 7.2, l.space.program, t.primary);
        }
    }

    fn insides(_: *const Segments, v: ui.Ui, l: Layout, i: usize, un: Unit, scale: f32) void {
        const c = l.space.clusters[i];
        const box = l.boxes[i];
        for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (b.lo >= un.lo and b.hi <= un.hi) {
            const bh = @as(f32, @floatFromInt(b.hi - b.lo)) * scale;
            if (bh < 28) continue;
            const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
            v.label(.mono, m.mono, .{ .x = box.x, .y = top, .w = box.w, .h = bh }, .center, &perms(b.prot), t.primary);
        };
    }

    /// `ink`'s echo: the band holding `addr` framed as read inside its box
    /// (2 units, 4 in from its sides, 6 over and under), its row of names with it.
    fn echoed(g: *const Segments, u: ui.Ui, l: Layout, addr: u64, shows: f32) void {
        if (shows <= 0) return;
        const v = u.faded(shows);
        const read_ink = theme.mark(.read).solid;
        for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) {
            const box = l.boxes[i];
            const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
            for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (addr >= b.lo and addr < b.hi) {
                const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
                const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2);
                v.frame(.{ .x = box.x + 4, .y = top - 6, .w = box.w - 8, .h = bh + 12 }, read_ink, theme.stroke.rule);
            };
            // Its row of names: the unit holding it.
            const all = rows(l, i, u.f.scratch);
            var targets: [64]f32 = undefined;
            var ys: [64]f32 = undefined;
            const n = @min(all.len, 64);
            for (all[0..n], 0..) |r, k| targets[k] = r.target;
            const room = share(l, i);
            pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch);
            for (all[0..n], 0..) |r, k| if (r.kind == .unit) {
                const un = l.space.units[r.unit];
                if (addr < un.lo or addr >= un.hi) continue;
                const x0 = g.x + m.address + m.indent - 6;
                v.frame(.{ .x = x0, .y = ys[k] - 12, .w = g.x + m.size_end + 6 - x0, .h = 24 }, read_ink, theme.stroke.rule);
            };
        };
    }
};

/// A dump's place (design/README.md, `segments`, "a dump's place"): the
/// `tie` bracket on its rows in the stack's ink (6 f), then the `link`
/// from the bracket's top up into the stage's top margin (y 56), across,
/// and down onto the stack's box at the dump's address, head first (36 f,
/// `standard`); the stack's band framed bright as it lands (6 f in, 12
/// out); then both quiet to standing (24 f, 60% toward the page).
pub const Place = struct {
    segments: *const Segments,
    addr: u64,
    /// The bracket: its x and its rows' top and foot.
    x: f32,
    y0: f32,
    y1: f32,
    /// Frames into the tie (negative: not begun); frames into its quieting.
    clock: anim.Value,
    quiet: anim.Value,

    pub fn draw(p: *const Place, f: *sk.Frame, placed: anim.Placed) void {
        const clock = placed.value(p.clock);
        if (clock < 0) return;
        const was = clipStage(f, placed);
        defer unclipStage(f, was);
        const u: ui.Ui = .{ .f = f, .at = placed };
        const k = 1 - (1 - theme.metric.arc.standing) * theme.ease(.glide, std.math.clamp(placed.value(p.quiet) / 24, 0, 1));
        const ink = ui.mix(theme.color.page, region.stack, k);
        const v = u.faded(std.math.clamp(clock / 6, 0, 1));
        v.bracket(p.x, p.y0, p.y1, ink, theme.metric.bracket.tie, 1, false);
        const l = p.segments.layout(placed.value(p.segments.form), f.scratch);
        const to = Segments.place(l, p.addr) orelse return;
        const top: f32 = 56;
        const drawn = theme.ease(.standard, std.math.clamp((clock - 6) / 36, 0, 1));
        u.route(&.{ .{ p.x, p.y0 }, .{ p.x, top }, .{ to[0], top }, .{ to[0], to[1] } }, ink, .{ .width = theme.stroke.tie.link, .head = .{ 10, 10 }, .progress = drawn, .radius = 8 });
        // The stack's band framed bright as the head lands.
        const bright = std.math.clamp((clock - 42) / 6, 0, 1) * (1 - std.math.clamp((clock - 48) / 12, 0, 1));
        if (bright > 0) for (l.space.clusters, 0..) |c, i| if (i < 6 and p.addr >= c.lo and p.addr < c.hi) {
            const b = l.boxes[i];
            u.faded(bright).frame(.{ .x = b.x - 5, .y = b.y - 5, .w = b.w + 10, .h = b.h + 10 }, t.bright, theme.stroke.line);
        };
    }
};

test "sizes as the names give them" {
    var buf: [16]u8 = undefined;
    try std.testing.expectEqualStrings("4 KiB", size(&buf, 4096));
    try std.testing.expectEqualStrings("132 KiB", size(&buf, 0x21000));
    try std.testing.expectEqualStrings("2.1 MiB", size(&buf, 0x223000));
    try std.testing.expectEqualStrings("64 MiB", size(&buf, 0x4002000));
    try std.testing.expectEqualStrings("128 TiB", size(&buf, user_end));
}

test "rows pooled round their mean, in order, inside their room" {
    var ys: [4]f32 = undefined;
    pool(&.{ 100, 100, 100, 300 }, &ys, 0, 1000, 24);
    try std.testing.expectEqual(@as(f32, 76), ys[0]);
    try std.testing.expectEqual(@as(f32, 124), ys[2]);
    try std.testing.expectEqual(@as(f32, 300), ys[3]);
    pool(&.{ 0, 0, 0, 0 }, &ys, 50, 1000, 24);
    try std.testing.expectEqual(@as(f32, 50), ys[0]);
    try std.testing.expectEqual(@as(f32, 122), ys[3]);
}