aboutsummaryrefslogtreecommitdiff
path: root/lib/build-web/time_report.zig
blob: c19a5abcc9ff7690a0bad35d19105b07f14f0db8 (plain)
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
const std = @import("std");
const gpa = std.heap.wasm_allocator;
const abi = std.Build.abi.time_report;
const fmtEscapeHtml = @import("root").fmtEscapeHtml;
const step_list = &@import("root").step_list;

const js = struct {
    extern "time_report" fn updateGeneric(
        /// The index of the step.
        step_idx: u32,
        // The HTML which will be used to populate the template slots.
        inner_html_ptr: [*]const u8,
        inner_html_len: usize,
    ) void;
    extern "time_report" fn updateCompile(
        /// The index of the step.
        step_idx: u32,
        // The HTML which will be used to populate the template slots.
        inner_html_ptr: [*]const u8,
        inner_html_len: usize,
        // The HTML which will populate the <tbody> of the file table.
        file_table_html_ptr: [*]const u8,
        file_table_html_len: usize,
        // The HTML which will populate the <tbody> of the decl table.
        decl_table_html_ptr: [*]const u8,
        decl_table_html_len: usize,
        /// Whether the LLVM backend was used. If not, LLVM-specific statistics are hidden.
        use_llvm: bool,
    ) void;
    extern "time_report" fn updateRunTest(
        /// The index of the step.
        step_idx: u32,
        // The HTML which will populate the <tbody> of the test table.
        table_html_ptr: [*]const u8,
        table_html_len: usize,
    ) void;
};

pub fn genericResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
    if (msg_bytes.len != @sizeOf(abi.GenericResult)) @panic("malformed GenericResult message");
    const msg: *const abi.GenericResult = @ptrCast(msg_bytes);
    if (msg.step_idx >= step_list.*.len) @panic("malformed GenericResult message");
    const inner_html = try std.fmt.allocPrint(gpa,
        \\<code slot="step-name">{[step_name]f}</code>
        \\<span slot="stat-total-time">{[stat_total_time]D}</span>
    , .{
        .step_name = fmtEscapeHtml(step_list.*[msg.step_idx].name),
        .stat_total_time = msg.ns_total,
    });
    defer gpa.free(inner_html);
    js.updateGeneric(msg.step_idx, inner_html.ptr, inner_html.len);
}

pub fn compileResultMessage(msg_bytes: []u8) error{ OutOfMemory, WriteFailed }!void {
    const max_table_rows = 500;

    if (msg_bytes.len < @sizeOf(abi.CompileResult)) @panic("malformed CompileResult message");
    const hdr: *const abi.CompileResult = @ptrCast(msg_bytes[0..@sizeOf(abi.CompileResult)]);
    if (hdr.step_idx >= step_list.*.len) @panic("malformed CompileResult message");
    var trailing = msg_bytes[@sizeOf(abi.CompileResult)..];

    const llvm_pass_timings = trailing[0..hdr.llvm_pass_timings_len];
    trailing = trailing[hdr.llvm_pass_timings_len..];

    const FileTimeReport = struct {
        name: []const u8,
        ns_sema: u64,
        ns_codegen: u64,
        ns_link: u64,
    };
    const DeclTimeReport = struct {
        file_name: []const u8,
        name: []const u8,
        sema_count: u32,
        ns_sema: u64,
        ns_codegen: u64,
        ns_link: u64,
    };

    const slowest_files = try gpa.alloc(FileTimeReport, hdr.files_len);
    defer gpa.free(slowest_files);

    const slowest_decls = try gpa.alloc(DeclTimeReport, hdr.decls_len);
    defer gpa.free(slowest_decls);

    for (slowest_files) |*file_out| {
        const i = std.mem.indexOfScalar(u8, trailing, 0) orelse @panic("malformed CompileResult message");
        file_out.* = .{
            .name = trailing[0..i],
            .ns_sema = 0,
            .ns_codegen = 0,
            .ns_link = 0,
        };
        trailing = trailing[i + 1 ..];
    }

    for (slowest_decls) |*decl_out| {
        const i = std.mem.indexOfScalar(u8, trailing, 0) orelse @panic("malformed CompileResult message");
        const file_idx = std.mem.readInt(u32, trailing[i..][1..5], .little);
        const sema_count = std.mem.readInt(u32, trailing[i..][5..9], .little);
        const sema_ns = std.mem.readInt(u64, trailing[i..][9..17], .little);
        const codegen_ns = std.mem.readInt(u64, trailing[i..][17..25], .little);
        const link_ns = std.mem.readInt(u64, trailing[i..][25..33], .little);
        const file = &slowest_files[file_idx];
        decl_out.* = .{
            .file_name = file.name,
            .name = trailing[0..i],
            .sema_count = sema_count,
            .ns_sema = sema_ns,
            .ns_codegen = codegen_ns,
            .ns_link = link_ns,
        };
        trailing = trailing[i + 33 ..];
        file.ns_sema += sema_ns;
        file.ns_codegen += codegen_ns;
        file.ns_link += link_ns;
    }

    const S = struct {
        fn fileLessThan(_: void, lhs: FileTimeReport, rhs: FileTimeReport) bool {
            const lhs_ns = lhs.ns_sema + lhs.ns_codegen + lhs.ns_link;
            const rhs_ns = rhs.ns_sema + rhs.ns_codegen + rhs.ns_link;
            return lhs_ns > rhs_ns; // flipped to sort in reverse order
        }
        fn declLessThan(_: void, lhs: DeclTimeReport, rhs: DeclTimeReport) bool {
            //if (true) return lhs.sema_count > rhs.sema_count;
            const lhs_ns = lhs.ns_sema + lhs.ns_codegen + lhs.ns_link;
            const rhs_ns = rhs.ns_sema + rhs.ns_codegen + rhs.ns_link;
            return lhs_ns > rhs_ns; // flipped to sort in reverse order
        }
    };
    std.mem.sort(FileTimeReport, slowest_files, {}, S.fileLessThan);
    std.mem.sort(DeclTimeReport, slowest_decls, {}, S.declLessThan);

    const stats = hdr.stats;
    const inner_html = try std.fmt.allocPrint(gpa,
        \\<code slot="step-name">{[step_name]f}</code>
        \\<span slot="stat-reachable-files">{[stat_reachable_files]d}</span>
        \\<span slot="stat-imported-files">{[stat_imported_files]d}</span>
        \\<span slot="stat-generic-instances">{[stat_generic_instances]d}</span>
        \\<span slot="stat-inline-calls">{[stat_inline_calls]d}</span>
        \\<span slot="stat-compilation-time">{[stat_compilation_time]D}</span>
        \\<span slot="cpu-time-parse">{[cpu_time_parse]D}</span>
        \\<span slot="cpu-time-astgen">{[cpu_time_astgen]D}</span>
        \\<span slot="cpu-time-sema">{[cpu_time_sema]D}</span>
        \\<span slot="cpu-time-codegen">{[cpu_time_codegen]D}</span>
        \\<span slot="cpu-time-link">{[cpu_time_link]D}</span>
        \\<span slot="real-time-files">{[real_time_files]D}</span>
        \\<span slot="real-time-decls">{[real_time_decls]D}</span>
        \\<span slot="real-time-llvm-emit">{[real_time_llvm_emit]D}</span>
        \\<span slot="real-time-link-flush">{[real_time_link_flush]D}</span>
        \\<pre slot="llvm-pass-timings"><code>{[llvm_pass_timings]f}</code></pre>
        \\
    , .{
        .step_name = fmtEscapeHtml(step_list.*[hdr.step_idx].name),
        .stat_reachable_files = stats.n_reachable_files,
        .stat_imported_files = stats.n_imported_files,
        .stat_generic_instances = stats.n_generic_instances,
        .stat_inline_calls = stats.n_inline_calls,
        .stat_compilation_time = hdr.ns_total,

        .cpu_time_parse = stats.cpu_ns_parse,
        .cpu_time_astgen = stats.cpu_ns_astgen,
        .cpu_time_sema = stats.cpu_ns_sema,
        .cpu_time_codegen = stats.cpu_ns_codegen,
        .cpu_time_link = stats.cpu_ns_link,
        .real_time_files = stats.real_ns_files,
        .real_time_decls = stats.real_ns_decls,
        .real_time_llvm_emit = stats.real_ns_llvm_emit,
        .real_time_link_flush = stats.real_ns_link_flush,

        .llvm_pass_timings = fmtEscapeHtml(llvm_pass_timings),
    });
    defer gpa.free(inner_html);

    var file_table_html: std.Io.Writer.Allocating = .init(gpa);
    defer file_table_html.deinit();

    for (slowest_files[0..@min(max_table_rows, slowest_files.len)]) |file| {
        try file_table_html.writer.print(
            \\<tr>
            \\  <th scope="row"><code>{f}</code></th>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\</tr>
            \\
        , .{
            fmtEscapeHtml(file.name),
            file.ns_sema,
            file.ns_codegen,
            file.ns_link,
            file.ns_sema + file.ns_codegen + file.ns_link,
        });
    }
    if (slowest_files.len > max_table_rows) {
        try file_table_html.writer.print(
            \\<tr><td colspan="4">{d} more rows omitted</td></tr>
            \\
        , .{slowest_files.len - max_table_rows});
    }

    var decl_table_html: std.Io.Writer.Allocating = .init(gpa);
    defer decl_table_html.deinit();

    for (slowest_decls[0..@min(max_table_rows, slowest_decls.len)]) |decl| {
        try decl_table_html.writer.print(
            \\<tr>
            \\  <th scope="row"><code>{f}</code></th>
            \\  <th scope="row"><code>{f}</code></th>
            \\  <td>{d}</td>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\  <td>{D}</td>
            \\</tr>
            \\
        , .{
            fmtEscapeHtml(decl.file_name),
            fmtEscapeHtml(decl.name),
            decl.sema_count,
            decl.ns_sema,
            decl.ns_codegen,
            decl.ns_link,
            decl.ns_sema + decl.ns_codegen + decl.ns_link,
        });
    }
    if (slowest_decls.len > max_table_rows) {
        try decl_table_html.writer.print(
            \\<tr><td colspan="6">{d} more rows omitted</td></tr>
            \\
        , .{slowest_decls.len - max_table_rows});
    }

    js.updateCompile(
        hdr.step_idx,
        inner_html.ptr,
        inner_html.len,
        file_table_html.written().ptr,
        file_table_html.written().len,
        decl_table_html.written().ptr,
        decl_table_html.written().len,
        hdr.flags.use_llvm,
    );
}

pub fn runTestResultMessage(msg_bytes: []u8) error{OutOfMemory}!void {
    if (msg_bytes.len < @sizeOf(abi.RunTestResult)) @panic("malformed RunTestResult message");
    const hdr: *const abi.RunTestResult = @ptrCast(msg_bytes[0..@sizeOf(abi.RunTestResult)]);
    if (hdr.step_idx >= step_list.*.len) @panic("malformed RunTestResult message");
    const trailing = msg_bytes[@sizeOf(abi.RunTestResult)..];

    const durations: []align(1) const u64 = @ptrCast(trailing[0 .. hdr.tests_len * 8]);
    var offset: usize = hdr.tests_len * 8;

    var table_html: std.ArrayList(u8) = .empty;
    defer table_html.deinit(gpa);

    for (durations) |test_ns| {
        const test_name_len = std.mem.indexOfScalar(u8, trailing[offset..], 0) orelse @panic("malformed RunTestResult message");
        const test_name = trailing[offset..][0..test_name_len];
        offset += test_name_len + 1;
        try table_html.print(gpa, "<tr><th scope=\"row\"><code>{f}</code></th>", .{fmtEscapeHtml(test_name)});
        if (test_ns == std.math.maxInt(u64)) {
            try table_html.appendSlice(gpa, "<td class=\"empty-cell\"></td>"); // didn't run
        } else {
            try table_html.print(gpa, "<td>{D}</td>", .{test_ns});
        }
        try table_html.appendSlice(gpa, "</tr>\n");
    }

    if (offset != trailing.len) @panic("malformed RunTestResult message");

    js.updateRunTest(
        hdr.step_idx,
        table_html.items.ptr,
        table_html.items.len,
    );
}