summaryrefslogtreecommitdiff
path: root/anim/src/heap.zig
blob: e9a0dcf30ba2ab99d3732513299734031c8e90ae (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
//! The heap, to scale: a strip along the addresses malloc handed out, each
//! live allocation a green block with the name of what points at it, each
//! freed one hollow and dashed, the allocator's own bytes between them
//! filled, and the bytes this step wrote in vermilion where they are.
//! Reads `Moment.heap`, and the recording's index of every allocation made.
const std = @import("std");
const sk = @import("sketch");
const anim = @import("anim");
const ui = @import("ui.zig");
const cursors = @import("cursor.zig");
const Cursor = cursors.Cursor;
const Recording = @import("Recording.zig");
const objects = @import("objects.zig");
const theme = ui.theme;
const Rect = ui.Rect;
const t = theme.color.text;

pub const Heap = struct {
    cursor: Cursor,
    box: Rect,
    /// Variables each with a pigment (by its place here), as elsewhere.
    track: []const []const u8 = &.{},
    focus: bool = false,

    pub fn draw(h: *const Heap, f: *sk.Frame, at: anim.Placed) void {
        const u: ui.Ui = .{ .f = f, .at = at };
        const now = h.cursor.now(at);
        const rec = h.cursor.rec;
        // The heap is memory: what a step did to it shows when memory strikes.
        const moment = now.shown(.memory);
        const at_moment = now.stop(.memory);
        // Not "heap": a grid of the same bytes beside it has that name.
        const body = u.panel(h.box.w, h.box.h, "heap map", .{ .focus = h.focus, .note = "to scale · low addresses on the left" });
        if (rec.allocations.len == 0) return;
        // The whole run's span, so nothing moves as chunks come and go.
        var lo: u64 = std.math.maxInt(u64);
        var hi: u64 = 0;
        for (rec.allocations) |a| {
            lo = @min(lo, a.ptr);
            hi = @max(hi, a.ptr + a.size);
        }
        lo -|= 16;
        const pad = theme.metric.panel.pad_x + 8;
        const scale = @min(theme.metric.heap.units_per_byte, (body.w - 2 * pad - 60) / @as(f32, @floatFromInt(hi - lo)));
        const strip_h: f32 = if (body.h < 220) theme.metric.heap.strip_h_stacked else theme.metric.heap.strip_h;
        const y = body.y + (body.h - strip_h) / 2 - 6;
        const live = moment.heap(f.scratch) catch return;

        // Never used: a rule frame. The allocator's own bytes, filled:
        // whatever lies between what it has handed out so far.
        var used = lo;
        for (rec.allocations) |a| {
            if (a.moment <= at_moment) used = @max(used, a.ptr + a.size);
        }
        u.frame(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(hi - lo)) * scale, .h = strip_h }, theme.color.rule, theme.stroke.rule);
        u.fill(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(used - lo)) * scale, .h = strip_h }, theme.color.rule);
        // Where it goes on.
        for (0..3) |i| u.fill(.{ .x = pad + @as(f32, @floatFromInt(hi - lo)) * scale + 16 + @as(f32, @floatFromInt(i)) * 14, .y = y + strip_h / 2 - 3, .w = 6, .h = 6 }, t.faint);

        for (rec.allocations, 0..) |a, i| {
            if (a.moment > at_moment) continue;
            // The newest allocation at this address is the one to draw.
            const newest = for (rec.allocations[i + 1 ..]) |later| {
                if (later.ptr == a.ptr and later.moment <= at_moment) break false;
            } else true;
            if (!newest) continue;
            const r: Rect = .{ .x = pad + @as(f32, @floatFromInt(a.ptr - lo)) * scale, .y = y, .w = @as(f32, @floatFromInt(a.size)) * scale - 2, .h = strip_h };
            const is_live = for (live) |l| {
                if (l.ptr == a.ptr and l.born == a.event) break true;
            } else false;
            u.fill(r, theme.color.page);
            if (is_live) {
                const mark = theme.mark(.allocated);
                const fresh = a.moment > now.stops[1];
                const p = if (fresh) now.moving(.memory, 0, theme.time.quick) else 1;
                u.faded(p).fill(r, mark.ember);
                u.faded(p).frame(r, mark.solid, theme.stroke.line);
                // The bytes this step wrote, where they are.
                var b: u64 = 0;
                while (b < a.size) : (b += 8) {
                    // The same strike as the words beside it, at scale: no arm, no pulse.
                    var heat = now.written(a.ptr + b, @min(8, a.size - b), .memory, 0);
                    heat.arm = 0;
                    heat.out = 0;
                    if (heat.level > 0) _ = u.written(.{ .x = r.x + @as(f32, @floatFromInt(b)) * scale + 3, .y = y + 3, .w = @as(f32, @floatFromInt(@min(8, a.size - b))) * scale - 4, .h = strip_h - 6 }, heat, null);
                }
                // Who points at it.
                var x = r.x + 6;
                for (objects.pointing(f.scratch, rec, moment, a.ptr)) |v| {
                    const name = rec.name(v.name);
                    const w = u.width(.mono, theme.size.mono.small, name);
                    if (x + w > r.x + r.w - 4) break;
                    const tracked = for (h.track, 0..) |want, k| {
                        if (std.mem.eql(u8, want, name)) break k;
                    } else null;
                    if (tracked) |k| u.fill(.{ .x = x - 4, .y = y + strip_h - 36, .w = w + 8, .h = 30 }, theme.color.pigment[k % theme.color.pigment.len]);
                    u.text(.mono, theme.size.mono.small, x, y + strip_h - 12, name, t.primary);
                    x += w + 12;
                }
            } else u.dashedFrame(r, theme.mark(.freed).solid, theme.stroke.line);
            // Its address under it: the end that tells chunks apart.
            u.text(.mono, theme.size.mono.small, r.x, y + strip_h + 30, u.print("{x:0>4}", .{a.ptr & 0xffff}), t.faint);
        }
    }
};