summaryrefslogtreecommitdiff
path: root/test/rollback_bench.zig
blob: 1260f47ec75bdd8e5dc008976de4585ae1340f09 (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
//! What a replica's rollback copy costs (docs/next-daemon.md, open point
//! 3, src/rollback.zig), and what a fork of the authority stalls it for
//! (open point 5). A typical session and a big one are built in process, on
//! a rollback epoch's allocator as a replica's core is; for each:
//!   - the copy taken and let go: its time, the bytes it saves, the bytes
//!     it pins (the journal's entries, a register's texts);
//!   - a key that edits a file (`x`) applied on top, as a speculation, and
//!     the copy put back: the apply's time and the restore's.
//! Then a fork of the big one, child gone at once, and the authority's
//! copy-on-write after it. Last, a session's authority with a 100 MB file
//! open (src/replica.zig): a key into it, a unit as a replica is sent it,
//! and a digest (src/walk.zig: a big text by its length).
//!
//!     zig build rollback-bench -Dplatform=tui -Doptimize=ReleaseFast
const std = @import("std");
const libc = std.c;
const pardes = @import("pardes");

const gpa = std.heap.c_allocator;

fn nowNs() u64 {
    var ts: libc.timespec = undefined;
    _ = libc.clock_gettime(.MONOTONIC, &ts);
    return @as(u64, @intCast(ts.sec)) *| 1_000_000_000 +| @as(u64, @intCast(ts.nsec));
}

const File = struct { lines: usize, copies: usize = 1 };

const Session = struct {
    name: []const u8,
    files: []const File,
    /// The shell's scrollback, fed through its terminal.
    scrollback_kb: usize,
    /// Insert sessions (`i x Esc`), each an undo step, spread over the files.
    edits: usize,
};

// 60-column lines: 1k lines is 60 KB, 50k is 3 MB, 300k is 18 MB.
const sessions = [_]Session{
    .{ .name = "typical", .files = &.{ .{ .lines = 1_000, .copies = 3 }, .{ .lines = 50_000 } }, .scrollback_kb = 1024, .edits = 40 },
    .{ .name = "big", .files = &.{ .{ .lines = 1_000, .copies = 8 }, .{ .lines = 50_000, .copies = 4 }, .{ .lines = 300_000 } }, .scrollback_kb = 8 * 1024, .edits = 200 },
};

fn pump(core: *pardes.Pardes) void {
    while (core.nextEffect()) |_| {}
}

fn key(core: *pardes.Pardes, cp: u21, text: []const u8) void {
    core.update(.{ .key = .{ .cp = cp, .text = text } });
    pump(core);
}

fn writeFile(path: []const u8, lines: usize) !void {
    var out: std.Io.Writer.Allocating = .init(gpa);
    defer out.deinit();
    for (0..lines) |n| try out.writer.print("const value_{d:0>7}: u32 = {d:0>10}; // a line of sixty\n", .{ n, n *% 2654435761 });
    var pathbuf: [4096]u8 = undefined;
    const fd = libc.open(try std.fmt.bufPrintSentinel(&pathbuf, "{s}", .{path}, 0), .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, @as(libc.mode_t, 0o644));
    if (fd < 0) return error.OpenFailed;
    defer _ = libc.close(fd);
    const text = out.written();
    var off: usize = 0;
    while (off < text.len) {
        const n = libc.write(fd, text.ptr + off, text.len - off);
        if (n <= 0) return error.WriteFailed;
        off += @intCast(n);
    }
}

fn build(s: Session, dir: []const u8, a: std.mem.Allocator) !*pardes.Pardes {
    const core = try pardes.Pardes.init(a, .{ .tty_only = true, .image_allocator = a, .pdf_allocator = a, .tree_sitter_allocator = a, .frame_allocator = a });
    core.update(.{ .resize = .{ .cols = 200, .rows = 60 } });
    pump(core);
    // The shell: pane 0 of a tty boot, its output fed as a pty's.
    var chunk: std.Io.Writer.Allocating = .init(gpa);
    defer chunk.deinit();
    var n: usize = 0;
    while (chunk.written().len < s.scrollback_kb * 1024) : (n += 1)
        try chunk.writer.print("\x1b[90m[{d:0>6}]\x1b[0m compiling module_{d} ... \x1b[32mok\x1b[0m\r\n", .{ n, n });
    var off: usize = 0;
    while (off < chunk.written().len) {
        const end = @min(off + 8192, chunk.written().len);
        core.update(.{ .output = .{ .pane = 0, .bytes = chunk.written()[off..end] } });
        pump(core);
        off = end;
    }
    var opened: [pardes.MAX_PANES]usize = undefined;
    var nopened: usize = 0;
    for (s.files, 0..) |f, fi| for (0..f.copies) |c| {
        var buf: [512]u8 = undefined;
        const path = try std.fmt.bufPrint(&buf, "{s}/f{d}-{d}.zig", .{ dir, fi, c });
        try writeFile(path, f.lines);
        pardes.look.lookAt(core, 0, path);
        pump(core);
        for (core.panes, 0..) |slot, id| if (slot) |pane| if (pane.file) |file| if (std.mem.eql(u8, file.path, path)) {
            opened[nopened] = id;
            nopened += 1;
        };
    };
    if (nopened == 0) return error.NothingOpened;
    for (0..s.edits) |e| {
        core.active = opened[e % nopened];
        key(core, 'i', "i");
        key(core, 'x', "x");
        key(core, pardes.Key.escape, "");
    }
    var arena: std.heap.ArenaAllocator = .init(gpa);
    defer arena.deinit();
    _ = try core.render(arena.allocator());
    return core;
}

fn median(samples: []u64) u64 {
    std.mem.sort(u64, samples, {}, std.sort.asc(u64));
    return samples[samples.len / 2];
}

fn rssKb() usize {
    var ru: libc.rusage = undefined;
    _ = libc.getrusage(libc.rusage.SELF, &ru);
    return @intCast(ru.maxrss);
}

const reps = 15;

/// The `p`th percentile of `samples`, sorted in place.
fn pct(samples: []u64, p: usize) u64 {
    std.mem.sort(u64, samples, {}, std.sort.asc(u64));
    return samples[@min(samples.len - 1, samples.len * p / 100)];
}

/// A unit of a key into a big file, and a digest.
fn digestBench(dir: []const u8, mb: usize) !void {
    var path_buf: [512]u8 = undefined;
    const path = try std.fmt.bufPrint(&path_buf, "{s}/big-file.txt", .{dir});
    try writeFile(path, (mb << 20) / 60);
    const NoHost = struct {
        fn watched(_: ?*anyopaque) bool {
            return false;
        }
        const vtable: pardes.Host.VTable = .{ .watched = watched };
    };
    const a = try pardes.replica.Authority.init(gpa, .{ .cols = 120, .rows = 40, .file = path }, .{ .vtable = &NoHost.vtable }, true);
    defer a.deinit();
    _ = try a.unit(0, 0);
    var unit_ns: [40]u64 = undefined;
    var digest_ns: [40]u64 = undefined;
    for (&unit_ns, &digest_ns, 0..) |*u, *d, k| {
        const t0 = nowNs();
        _ = try a.local(.{ .key = .{ .cp = if (k % 2 == 0) 'j' else 'k', .text = if (k % 2 == 0) "j" else "k" } });
        u.* = nowNs() - t0;
        const t1 = nowNs();
        _ = pardes.replica.digestOf(a);
        d.* = nowNs() - t1;
    }
    std.debug.print("{d} MB file: a unit p50 {d} us p99 {d} us; a digest p50 {d} us p99 {d} us\n", .{ mb, pct(&unit_ns, 50) / 1000, pct(&unit_ns, 99) / 1000, pct(&digest_ns, 50) / 1000, pct(&digest_ns, 99) / 1000 });
}

pub fn main(init: std.process.Init) !void {
    var dir_buf: [256]u8 = undefined;
    const tmp = std.mem.span(libc.getenv("TMPDIR") orelse "/tmp");
    const dir = try std.fmt.bufPrint(&dir_buf, "{s}/rollback-bench-{d}", .{ tmp, @as(u32, @intCast(libc.getpid())) });
    try std.Io.Dir.createDirAbsolute(init.io, dir, .default_dir);
    defer std.Io.Dir.cwd().deleteTree(init.io, dir) catch {};
    std.debug.print("session  panes  copy KB  pinned MB  take ms  release ms  edit ms  restore ms\n", .{});
    var big: ?*pardes.Pardes = null;
    var epochs: [sessions.len]*pardes.rollback.Epoch = undefined;
    for (sessions, &epochs) |s, *e| {
        e.* = try gpa.create(pardes.rollback.Epoch);
        e.*.* = .{};
        const core = try build(s, dir, e.*.allocator());
        var c: pardes.rollback.Copy = .{};
        defer c.deinit();
        var take: [reps]u64 = undefined;
        var release: [reps]u64 = undefined;
        for (&take, &release) |*x, *y| {
            const t0 = nowNs();
            try c.take(e.*, core);
            const t1 = nowNs();
            c.release();
            x.* = t1 - t0;
            y.* = nowNs() - t1;
        }
        var edit: [reps]u64 = undefined;
        var restore: [reps]u64 = undefined;
        for (&edit, &restore) |*x, *y| {
            try c.take(e.*, core);
            const t0 = nowNs();
            key(core, 'x', "x");
            const t1 = nowNs();
            _ = c.restore();
            x.* = t1 - t0;
            y.* = nowNs() - t1;
        }
        // A join (docs/next-daemon.md, Phase 5 part B notes): the session
        // dumped at a step boundary, which is what the authority pays, and
        // the joiner's load of it.
        var dump: [5]u64 = undefined;
        var load: [5]u64 = undefined;
        var dump_len: usize = 0;
        for (&dump, &load) |*x, *y| {
            const t0 = nowNs();
            const bytes = try pardes.dump.dumpBytes(core);
            const t1 = nowNs();
            dump_len = bytes.len;
            const loaded = try pardes.dump.load(gpa, .{ .tty_only = true, .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }, bytes, null);
            y.* = nowNs() - t1;
            x.* = t1 - t0;
            loaded.deinit();
            core.gpa.free(bytes);
        }
        std.debug.print("{s:<8} join: dump {d:.1} MB in {d:.2} ms (the session's step), load {d:.2} ms (the joiner's)\n", .{ s.name, @as(f64, @floatFromInt(dump_len)) / 1e6, @as(f64, @floatFromInt(median(&dump))) * 1e-6, @as(f64, @floatFromInt(median(&load))) * 1e-6 });
        var panes: usize = 0;
        for (core.panes) |slot| panes += @intFromBool(slot != null);
        const ms = 1e-6;
        std.debug.print("{s:<8} {d:>5}  {d:>7.1}  {d:>9.1}  {d:>7.3}  {d:>10.3}  {d:>7.3}  {d:>10.3}\n", .{
            s.name,                                        panes,
            @as(f64, @floatFromInt(c.savedBytes())) / 1e3, @as(f64, @floatFromInt(c.pinnedBytes())) / 1e6,
            @as(f64, @floatFromInt(median(&take))) * ms,   @as(f64, @floatFromInt(median(&release))) * ms,
            @as(f64, @floatFromInt(median(&edit))) * ms,   @as(f64, @floatFromInt(median(&restore))) * ms,
        });
        if (big) |b| b.deinit();
        big = core;
    }
    // The join writer as a fork child: what fork() holds the authority for,
    // and its copy-on-write after, the child alive. The child waits on a
    // pipe and exits.
    const core = big.?;
    defer core.deinit();
    var t: [reps]u64 = undefined;
    var cow: [reps]u64 = undefined;
    const touch = try gpa.alloc(u8, 64 << 20);
    defer gpa.free(touch);
    @memset(touch, 1);
    for (&t, &cow) |*x, *c| {
        var pipe: [2]c_int = undefined;
        if (libc.pipe(&pipe) != 0) return error.Pipe;
        const t0 = nowNs();
        const pid = libc.fork();
        if (pid == 0) {
            var one: [1]u8 = undefined;
            _ = libc.read(pipe[0], &one, 1);
            libc._exit(0);
        }
        x.* = nowNs() - t0;
        // 64 MB the authority writes while the child holds its pages.
        const t1 = nowNs();
        var i: usize = 0;
        while (i < touch.len) : (i += 4096) touch[i] +%= 1;
        c.* = nowNs() - t1;
        _ = libc.close(pipe[0]);
        _ = libc.write(pipe[1], "x", 1);
        _ = libc.close(pipe[1]);
        var status: c_int = 0;
        _ = libc.waitpid(pid, &status, 0);
    }
    std.debug.print("fork of the big session (max RSS {d} MB): {d:.3} ms; then 64 MB written under the child: {d:.3} ms\n", .{
        rssKb() / 1024, @as(f64, @floatFromInt(median(&t))) / 1e6, @as(f64, @floatFromInt(median(&cow))) / 1e6,
    });
    try digestBench(dir, 100);
}