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
|
//! What a recording costs a frame: `zig build bench` opens each recording
//! (which indexes it) and times the queries components make of a moment,
//! walking forward a moment at a time as a playing scene does, and jumping
//! about as a seek does. Always built ReleaseFast.
//! `zig build bench -- NAME BY [FUNCTION]` lists a recording's steps instead.
const std = @import("std");
const canim = @import("canim");
const recordings = @import("recordings");
fn now() u64 {
var ts: std.os.linux.timespec = undefined;
_ = std.os.linux.clock_gettime(.MONOTONIC, &ts);
return @as(u64, @intCast(ts.sec)) * std.time.ns_per_s + @as(u64, @intCast(ts.nsec));
}
/// `bench NAME BY [FUNCTION]`: every stop of kind BY (moment, line, over) in
/// recording NAME (from FUNCTION's first call on), with what the step to it changed. What a scene's author needs to
/// know to pace it.
fn steps(rec: *canim.Recording, by: canim.Recording.Stop, function: ?[]const u8) void {
var at: u64 = if (function) |name| rec.call(name, 0).enter else 0;
var n: usize = 0;
while (at < rec.end()) : (n += 1) {
const to = rec.next(by, at);
var bytes: u64 = 0;
for (rec.written(at, to)) |w| bytes += w.len;
std.debug.print("{d:>4} moment {d:>5} line {d:>3} depth {d} wrote {d:>3} bytes in {d} read {d} regs", .{ n, to, rec.own[to], rec.depths[to], bytes, rec.written(at, to).len, rec.read(at, to).len });
const mask = rec.registersChanged(at, to);
for (@import("ztrace").Regs.names, 0..) |reg, i| if (mask >> @intCast(i) & 1 != 0) std.debug.print(" {s}", .{reg});
for (rec.syscalls) |call| if (call.enter > at and call.enter <= to) std.debug.print(" syscall {d}", .{call.nr});
std.debug.print("\n", .{});
at = to;
}
}
/// `bench relations`: the relations' passes on tables of a working frame's
/// size and of a map's (design/README.md, "4. Ties", "Cost": some 4,000
/// places and 6,000 values), against the table they replace (a list
/// searched from the start for each lookup, so a join is places times values).
fn relationsCost(gpa: std.mem.Allocator) !void {
const rel = canim.relations;
std.debug.print("{s:<8} {s:>6} {s:>6} {s:>6} | {s:>10} {s:>10} {s:>10} | {s:>12}\n", .{ "tables", "places", "values", "pairs", "sort ns", "join ns", "passes ns", "old scan ns" });
for ([_][2]usize{ .{ 300, 120 }, .{ 4000, 6000 } }, [_][]const u8{ "working", "map" }) |size, label| {
var rng: std.Random.DefaultPrng = .init(7);
const r = rng.random();
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const rounds = 200;
var took: [4]u64 = @splat(0);
var pairs_n: usize = 0;
for (0..rounds) |_| {
_ = arena.reset(.retain_capacity);
const a = arena.allocator();
var t: rel.Tables = .{};
// Places: rows of code (kind 14) and words of the stack (kind 3), and some of every other kind.
for (0..size[0]) |i| {
const kind: u8 = if (i % 3 == 0) 14 else if (i % 3 == 1) 3 else @intCast(1 + r.uintLessThan(u8, 16));
const addr: u64 = if (kind == 14) 0x401000 + 4 * @as(u64, i) else 0x7fffffffe000 + 8 * @as(u64, i);
t.add(a, .{ .key = rel.keyOf(kind, addr), .box = .{ 0, @floatFromInt(i), 880, @floatFromInt(i + 1) }, .pane = .disasm });
}
// Values: a third of them code addresses on screen, the rest anything.
for (0..size[1]) |i| {
const v: u64 = if (i % 3 == 0) 0x401000 + 4 * r.uintLessThan(u64, size[0]) else r.int(u64);
t.show(a, .{ .value = v, .box = .{ 0, 0, 0, 0 }, .pane = .stack, .at = 0x7fffffffe000 + 8 * @as(u64, i) });
}
var clock = rel.clock();
const by_key = t.byKey(a);
const by_value = t.byValue(a);
took[0] += rel.clock() - clock;
clock = rel.clock();
var pairs: std.ArrayList(rel.Pair) = .empty;
rel.join(t.values.items(.value), by_value, t.places.items(.key), by_key, 14, ~@as(u64, 0), &pairs, a);
took[1] += rel.clock() - clock;
pairs_n = pairs.items.len;
// The passes over a candidate a pair (the knob on), then cut to the budget.
clock = rel.clock();
const cands = try a.alloc(rel.Candidate, pairs.items.len);
for (cands, pairs.items) |*c, p| c.* = .{ .kind = .code, .rank = p.value };
var kept = rel.filter(cands, rel.Knobs.initMany(&.{ .code, .always }));
rel.byPriority(kept);
kept = rel.cut(kept);
took[2] += rel.clock() - clock;
std.mem.doNotOptimizeAway(kept.len);
// The old way: each value's lookup a scan of the places from the start.
clock = rel.clock();
const keys = t.places.items(.key);
var found: usize = 0;
for (t.values.items(.value)) |v| {
for (keys) |key| if (key == rel.keyOf(14, v)) {
found += 1;
break;
};
}
std.mem.doNotOptimizeAway(found);
took[3] += rel.clock() - clock;
}
std.debug.print("{s:<8} {d:>6} {d:>6} {d:>6} | {d:>10} {d:>10} {d:>10} | {d:>12}\n", .{ label, size[0], size[1], pairs_n, took[0] / rounds, took[1] / rounds, took[2] / rounds, took[3] / rounds });
}
}
pub fn main(init: std.process.Init) !void {
const gpa = init.gpa;
const args = try init.minimal.args.toSlice(init.arena.allocator());
if (args.len == 2 and std.mem.eql(u8, args[1], "relations")) return relationsCost(gpa);
if (args.len >= 3) {
inline for (@typeInfo(recordings).@"struct".decl_names) |name| {
if (@TypeOf(@field(recordings, name)) == recordings.Program and std.mem.eql(u8, name, args[1]))
return steps(canim.open(@field(recordings, name)), std.meta.stringToEnum(canim.Recording.Stop, args[2]) orelse return error.NoSuchStop, if (args.len > 3) args[3] else null);
}
return error.NoSuchRecording;
}
std.debug.print("{s:<8} {s:>7} {s:>9} {s:>8} | ns a query, moments in order: {s:>6} {s:>6} {s:>6} {s:>6} {s:>6} {s:>6} | {s:>9}\n", .{ "", "moments", "bytes", "open ms", "regs", "read64", "vars", "heap", "stack", "reach", "seek us" });
inline for (@typeInfo(recordings).@"struct".decl_names) |name| {
if (@TypeOf(@field(recordings, name)) != recordings.Program) continue;
const program = @field(recordings, name);
const start = now();
const rec = canim.open(program);
const opened = now() - start;
// Somewhere memory was written, to read back.
const addr: u64 = if (rec.write_spans.all.len > 0) rec.write_spans.all[rec.write_spans.all.len / 2].addr else 0;
var took: [6]u64 = @splat(0);
var arena: std.heap.ArenaAllocator = .init(gpa);
defer arena.deinit();
const rounds = @max(1, 20_000 / rec.len());
for (0..rounds) |_| for (0..rec.len()) |i| {
_ = arena.reset(.retain_capacity);
const m = rec.moment(i);
var t = now();
_ = try m.regs(rec.tid);
took[0] += now() - t;
t = now();
var buf: [64]u8 = undefined;
var known: [64]bool = undefined;
try m.read(addr, &buf, &known);
took[1] += now() - t;
t = now();
_ = try m.variables(arena.allocator());
took[2] += now() - t;
t = now();
_ = try m.heap(arena.allocator());
took[3] += now() - t;
t = now();
_ = rec.stack(arena.allocator(), i);
took[4] += now() - t;
t = now();
_ = canim.objects.reach(arena.allocator(), rec, m, null);
took[5] += now() - t;
};
// A seek: moments far apart, each replayed from its keyframe.
var rng: std.Random.DefaultPrng = .init(1);
const seeks = 200;
const seek_start = now();
for (0..seeks) |_| _ = try rec.moment(rng.random().uintLessThan(u64, rec.len())).regs(rec.tid);
const seek = (now() - seek_start) / seeks;
const n = rounds * rec.len();
std.debug.print("{s:<8} {d:>7} {d:>9} {d:>8.2} | {s:>29} {d:>6} {d:>6} {d:>6} {d:>6} {d:>6} {d:>6} | {d:>9.1}\n", .{ name, rec.len(), program.trace.len, @as(f64, @floatFromInt(opened)) / 1e6, "", took[0] / n, took[1] / n, took[2] / n, took[3] / n, took[4] / n, took[5] / n, @as(f64, @floatFromInt(seek)) / 1e3 });
}
}
|