summaryrefslogtreecommitdiff
path: root/anim/tools/bench.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/tools/bench.zig')
-rw-r--r--anim/tools/bench.zig91
1 files changed, 91 insertions, 0 deletions
diff --git a/anim/tools/bench.zig b/anim/tools/bench.zig
new file mode 100644
index 0000000..ad6fe4b
--- /dev/null
+++ b/anim/tools/bench.zig
@@ -0,0 +1,91 @@
+//! 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;
+ }
+}
+
+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 >= 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 });
+ }
+}