build system: replace fuzzing UI with build UI, add time report
This commit replaces the "fuzzer" UI, previously accessed with the `--fuzz` and `--port` flags, with a more interesting web UI which allows more interactions with the Zig build system. Most notably, it allows accessing the data emitted by a new "time report" system, which allows users to see which parts of Zig programs take the longest to compile. The option to expose the web UI is `--webui`. By default, it will listen on `[::1]` on a random port, but any IPv6 or IPv4 address can be specified with e.g. `--webui=[::1]:8000` or `--webui=127.0.0.1:8000`. The options `--fuzz` and `--time-report` both imply `--webui` if not given. Currently, `--webui` is incompatible with `--watch`; specifying both will cause `zig build` to exit with a fatal error. When the web UI is enabled, the build runner spawns the web server as soon as the configure phase completes. The frontend code consists of one HTML file, one JavaScript file, two CSS files, and a few Zig source files which are built into a WASM blob on-demand -- this is all very similar to the old fuzzer UI. Also inherited from the fuzzer UI is that the build system communicates with web clients over a WebSocket connection. When the build finishes, if `--webui` was passed (i.e. if the web server is running), the build runner does not terminate; it continues running to serve web requests, allowing interactive control of the build system. In the web interface is an overall "status" indicating whether a build is currently running, and also a list of all steps in this build. There are visual indicators (colors and spinners) for in-progress, succeeded, and failed steps. There is a "Rebuild" button which will cause the build system to reset the state of every step (note that this does not affect caching) and evaluate the step graph again. If `--time-report` is passed to `zig build`, a new section of the interface becomes visible, which associates every build step with a "time report". For most steps, this is just a simple "time taken" value. However, for `Compile` steps, the compiler communicates with the build system to provide it with much more interesting information: time taken for various pipeline phases, with a per-declaration and per-file breakdown, sorted by slowest declarations/files first. This feature is still in its early stages: the data can be a little tricky to understand, and there is no way to, for instance, sort by different properties, or filter to certain files. However, it has already given us some interesting statistics, and can be useful for spotting, for instance, particularly complex and slow compile-time logic. Additionally, if a compilation uses LLVM, its time report includes the "LLVM pass timing" information, which was previously accessible with the (now removed) `-ftime-report` compiler flag. To make time reports more useful, ZIR and compilation caches are ignored by the Zig compiler when they are enabled -- in other words, `Compile` steps *always* run, even if their result should be cached. This means that the flag can be used to analyze a project's compile time without having to repeatedly clear cache directory, for instance. However, when using `-fincremental`, updates other than the first will only show you the statistics for what changed on that particular update. Notably, this gives us a fairly nice way to see exactly which declarations were re-analyzed by an incremental update. If `--fuzz` is passed to `zig build`, another section of the web interface becomes visible, this time exposing the fuzzer. This is quite similar to the fuzzer UI this commit replaces, with only a few cosmetic tweaks. The interface is closer than before to supporting multiple fuzz steps at a time (in line with the overall strategy for this build UI, the goal will be for all of the fuzz steps to be accessible in the same interface), but still doesn't actually support it. The fuzzer UI looks quite different under the hood: as a result, various bugs are fixed, although other bugs remain. For instance, viewing the source code of any file other than the root of the main module is completely broken (as on master) due to some bogus file-to-module assignment logic in the fuzzer UI. Implementation notes: * The `lib/build-web/` directory holds the client side of the web UI. * The general server logic is in `std.Build.WebServer`. * Fuzzing-specific logic is in `std.Build.Fuzz`. * `std.Build.abi` is the new home of `std.Build.Fuzz.abi`, since it now relates to the build system web UI in general. * The build runner now has an **actual** general-purpose allocator, because thanks to `--watch` and `--webui`, the process can be arbitrarily long-lived. The gpa is `std.heap.DebugAllocator`, but the arena remains backed by `std.heap.page_allocator` for efficiency. I fixed several crashes caused by conflation of `gpa` and `arena` in the build runner and `std.Build`, but there may still be some I have missed. * The I/O logic in `std.Build.WebServer` is pretty gnarly; there are a *lot* of threads involved. I anticipate this situation improving significantly once the `std.Io` interface (with concurrency support) is introduced.
This commit is contained in:
377
lib/build-web/fuzz.zig
Normal file
377
lib/build-web/fuzz.zig
Normal file
@@ -0,0 +1,377 @@
|
||||
// Server timestamp.
|
||||
var start_fuzzing_timestamp: i64 = undefined;
|
||||
|
||||
const js = struct {
|
||||
extern "fuzz" fn requestSources() void;
|
||||
extern "fuzz" fn ready() void;
|
||||
|
||||
extern "fuzz" fn updateStats(html_ptr: [*]const u8, html_len: usize) void;
|
||||
extern "fuzz" fn updateEntryPoints(html_ptr: [*]const u8, html_len: usize) void;
|
||||
extern "fuzz" fn updateSource(html_ptr: [*]const u8, html_len: usize) void;
|
||||
extern "fuzz" fn updateCoverage(covered_ptr: [*]const SourceLocationIndex, covered_len: u32) void;
|
||||
};
|
||||
|
||||
pub fn sourceIndexMessage(msg_bytes: []u8) error{OutOfMemory}!void {
|
||||
Walk.files.clearRetainingCapacity();
|
||||
Walk.decls.clearRetainingCapacity();
|
||||
Walk.modules.clearRetainingCapacity();
|
||||
recent_coverage_update.clearRetainingCapacity();
|
||||
selected_source_location = null;
|
||||
|
||||
js.requestSources();
|
||||
|
||||
const Header = abi.fuzz.SourceIndexHeader;
|
||||
const header: Header = @bitCast(msg_bytes[0..@sizeOf(Header)].*);
|
||||
|
||||
const directories_start = @sizeOf(Header);
|
||||
const directories_end = directories_start + header.directories_len * @sizeOf(Coverage.String);
|
||||
const files_start = directories_end;
|
||||
const files_end = files_start + header.files_len * @sizeOf(Coverage.File);
|
||||
const source_locations_start = files_end;
|
||||
const source_locations_end = source_locations_start + header.source_locations_len * @sizeOf(Coverage.SourceLocation);
|
||||
const string_bytes = msg_bytes[source_locations_end..][0..header.string_bytes_len];
|
||||
|
||||
const directories: []const Coverage.String = @alignCast(std.mem.bytesAsSlice(Coverage.String, msg_bytes[directories_start..directories_end]));
|
||||
const files: []const Coverage.File = @alignCast(std.mem.bytesAsSlice(Coverage.File, msg_bytes[files_start..files_end]));
|
||||
const source_locations: []const Coverage.SourceLocation = @alignCast(std.mem.bytesAsSlice(Coverage.SourceLocation, msg_bytes[source_locations_start..source_locations_end]));
|
||||
|
||||
start_fuzzing_timestamp = header.start_timestamp;
|
||||
try updateCoverageSources(directories, files, source_locations, string_bytes);
|
||||
js.ready();
|
||||
}
|
||||
|
||||
var coverage = Coverage.init;
|
||||
/// Index of type `SourceLocationIndex`.
|
||||
var coverage_source_locations: std.ArrayListUnmanaged(Coverage.SourceLocation) = .empty;
|
||||
/// Contains the most recent coverage update message, unmodified.
|
||||
var recent_coverage_update: std.ArrayListAlignedUnmanaged(u8, .of(u64)) = .empty;
|
||||
|
||||
fn updateCoverageSources(
|
||||
directories: []const Coverage.String,
|
||||
files: []const Coverage.File,
|
||||
source_locations: []const Coverage.SourceLocation,
|
||||
string_bytes: []const u8,
|
||||
) !void {
|
||||
coverage.directories.clearRetainingCapacity();
|
||||
coverage.files.clearRetainingCapacity();
|
||||
coverage.string_bytes.clearRetainingCapacity();
|
||||
coverage_source_locations.clearRetainingCapacity();
|
||||
|
||||
try coverage_source_locations.appendSlice(gpa, source_locations);
|
||||
try coverage.string_bytes.appendSlice(gpa, string_bytes);
|
||||
|
||||
try coverage.files.entries.resize(gpa, files.len);
|
||||
@memcpy(coverage.files.entries.items(.key), files);
|
||||
try coverage.files.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items });
|
||||
|
||||
try coverage.directories.entries.resize(gpa, directories.len);
|
||||
@memcpy(coverage.directories.entries.items(.key), directories);
|
||||
try coverage.directories.reIndexContext(gpa, .{ .string_bytes = coverage.string_bytes.items });
|
||||
}
|
||||
|
||||
pub fn coverageUpdateMessage(msg_bytes: []u8) error{OutOfMemory}!void {
|
||||
recent_coverage_update.clearRetainingCapacity();
|
||||
recent_coverage_update.appendSlice(gpa, msg_bytes) catch @panic("OOM");
|
||||
try updateStats();
|
||||
try updateCoverage();
|
||||
}
|
||||
|
||||
var entry_points: std.ArrayListUnmanaged(SourceLocationIndex) = .empty;
|
||||
|
||||
pub fn entryPointsMessage(msg_bytes: []u8) error{OutOfMemory}!void {
|
||||
const header: abi.fuzz.EntryPointHeader = @bitCast(msg_bytes[0..@sizeOf(abi.fuzz.EntryPointHeader)].*);
|
||||
const slis: []align(1) const SourceLocationIndex = @ptrCast(msg_bytes[@sizeOf(abi.fuzz.EntryPointHeader)..]);
|
||||
assert(slis.len == header.locsLen());
|
||||
try entry_points.resize(gpa, slis.len);
|
||||
@memcpy(entry_points.items, slis);
|
||||
try updateEntryPoints();
|
||||
}
|
||||
|
||||
/// Index into `coverage_source_locations`.
|
||||
const SourceLocationIndex = enum(u32) {
|
||||
_,
|
||||
|
||||
fn haveCoverage(sli: SourceLocationIndex) bool {
|
||||
return @intFromEnum(sli) < coverage_source_locations.items.len;
|
||||
}
|
||||
|
||||
fn ptr(sli: SourceLocationIndex) *Coverage.SourceLocation {
|
||||
return &coverage_source_locations.items[@intFromEnum(sli)];
|
||||
}
|
||||
|
||||
fn sourceLocationLinkHtml(
|
||||
sli: SourceLocationIndex,
|
||||
out: *std.ArrayListUnmanaged(u8),
|
||||
focused: bool,
|
||||
) Allocator.Error!void {
|
||||
const sl = sli.ptr();
|
||||
try out.writer(gpa).print("<code{s}>", .{
|
||||
@as([]const u8, if (focused) " class=\"status-running\"" else ""),
|
||||
});
|
||||
try sli.appendPath(out);
|
||||
try out.writer(gpa).print(":{d}:{d} </code><button class=\"linkish\" onclick=\"wasm_exports.fuzzSelectSli({d});\">View</button>", .{
|
||||
sl.line,
|
||||
sl.column,
|
||||
@intFromEnum(sli),
|
||||
});
|
||||
}
|
||||
|
||||
fn appendPath(sli: SourceLocationIndex, out: *std.ArrayListUnmanaged(u8)) Allocator.Error!void {
|
||||
const sl = sli.ptr();
|
||||
const file = coverage.fileAt(sl.file);
|
||||
const file_name = coverage.stringAt(file.basename);
|
||||
const dir_name = coverage.stringAt(coverage.directories.keys()[file.directory_index]);
|
||||
try html_render.appendEscaped(out, dir_name);
|
||||
try out.appendSlice(gpa, "/");
|
||||
try html_render.appendEscaped(out, file_name);
|
||||
}
|
||||
|
||||
fn toWalkFile(sli: SourceLocationIndex) ?Walk.File.Index {
|
||||
var buf: std.ArrayListUnmanaged(u8) = .empty;
|
||||
defer buf.deinit(gpa);
|
||||
sli.appendPath(&buf) catch @panic("OOM");
|
||||
return @enumFromInt(Walk.files.getIndex(buf.items) orelse return null);
|
||||
}
|
||||
|
||||
fn fileHtml(
|
||||
sli: SourceLocationIndex,
|
||||
out: *std.ArrayListUnmanaged(u8),
|
||||
) error{ OutOfMemory, SourceUnavailable }!void {
|
||||
const walk_file_index = sli.toWalkFile() orelse return error.SourceUnavailable;
|
||||
const root_node = walk_file_index.findRootDecl().get().ast_node;
|
||||
var annotations: std.ArrayListUnmanaged(html_render.Annotation) = .empty;
|
||||
defer annotations.deinit(gpa);
|
||||
try computeSourceAnnotations(sli.ptr().file, walk_file_index, &annotations, coverage_source_locations.items);
|
||||
html_render.fileSourceHtml(walk_file_index, out, root_node, .{
|
||||
.source_location_annotations = annotations.items,
|
||||
}) catch |err| {
|
||||
fatal("unable to render source: {s}", .{@errorName(err)});
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
fn computeSourceAnnotations(
|
||||
cov_file_index: Coverage.File.Index,
|
||||
walk_file_index: Walk.File.Index,
|
||||
annotations: *std.ArrayListUnmanaged(html_render.Annotation),
|
||||
source_locations: []const Coverage.SourceLocation,
|
||||
) !void {
|
||||
// Collect all the source locations from only this file into this array
|
||||
// first, then sort by line, col, so that we can collect annotations with
|
||||
// O(N) time complexity.
|
||||
var locs: std.ArrayListUnmanaged(SourceLocationIndex) = .empty;
|
||||
defer locs.deinit(gpa);
|
||||
|
||||
for (source_locations, 0..) |sl, sli_usize| {
|
||||
if (sl.file != cov_file_index) continue;
|
||||
const sli: SourceLocationIndex = @enumFromInt(sli_usize);
|
||||
try locs.append(gpa, sli);
|
||||
}
|
||||
|
||||
std.mem.sortUnstable(SourceLocationIndex, locs.items, {}, struct {
|
||||
pub fn lessThan(context: void, lhs: SourceLocationIndex, rhs: SourceLocationIndex) bool {
|
||||
_ = context;
|
||||
const lhs_ptr = lhs.ptr();
|
||||
const rhs_ptr = rhs.ptr();
|
||||
if (lhs_ptr.line < rhs_ptr.line) return true;
|
||||
if (lhs_ptr.line > rhs_ptr.line) return false;
|
||||
return lhs_ptr.column < rhs_ptr.column;
|
||||
}
|
||||
}.lessThan);
|
||||
|
||||
const source = walk_file_index.get_ast().source;
|
||||
var line: usize = 1;
|
||||
var column: usize = 1;
|
||||
var next_loc_index: usize = 0;
|
||||
for (source, 0..) |byte, offset| {
|
||||
if (byte == '\n') {
|
||||
line += 1;
|
||||
column = 1;
|
||||
} else {
|
||||
column += 1;
|
||||
}
|
||||
while (true) {
|
||||
if (next_loc_index >= locs.items.len) return;
|
||||
const next_sli = locs.items[next_loc_index];
|
||||
const next_sl = next_sli.ptr();
|
||||
if (next_sl.line > line or (next_sl.line == line and next_sl.column >= column)) break;
|
||||
try annotations.append(gpa, .{
|
||||
.file_byte_offset = offset,
|
||||
.dom_id = @intFromEnum(next_sli),
|
||||
});
|
||||
next_loc_index += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export fn fuzzUnpackSources(tar_ptr: [*]u8, tar_len: usize) void {
|
||||
const tar_bytes = tar_ptr[0..tar_len];
|
||||
log.debug("received {d} bytes of sources.tar", .{tar_bytes.len});
|
||||
|
||||
unpackSourcesInner(tar_bytes) catch |err| {
|
||||
fatal("unable to unpack sources.tar: {s}", .{@errorName(err)});
|
||||
};
|
||||
}
|
||||
|
||||
fn unpackSourcesInner(tar_bytes: []u8) !void {
|
||||
var tar_reader: std.Io.Reader = .fixed(tar_bytes);
|
||||
var file_name_buffer: [1024]u8 = undefined;
|
||||
var link_name_buffer: [1024]u8 = undefined;
|
||||
var it: std.tar.Iterator = .init(&tar_reader, .{
|
||||
.file_name_buffer = &file_name_buffer,
|
||||
.link_name_buffer = &link_name_buffer,
|
||||
});
|
||||
while (try it.next()) |tar_file| {
|
||||
switch (tar_file.kind) {
|
||||
.file => {
|
||||
if (tar_file.size == 0 and tar_file.name.len == 0) break;
|
||||
if (std.mem.endsWith(u8, tar_file.name, ".zig")) {
|
||||
log.debug("found file: '{s}'", .{tar_file.name});
|
||||
const file_name = try gpa.dupe(u8, tar_file.name);
|
||||
if (std.mem.indexOfScalar(u8, file_name, '/')) |pkg_name_end| {
|
||||
const pkg_name = file_name[0..pkg_name_end];
|
||||
const gop = try Walk.modules.getOrPut(gpa, pkg_name);
|
||||
const file: Walk.File.Index = @enumFromInt(Walk.files.entries.len);
|
||||
if (!gop.found_existing or
|
||||
std.mem.eql(u8, file_name[pkg_name_end..], "/root.zig") or
|
||||
std.mem.eql(u8, file_name[pkg_name_end + 1 .. file_name.len - ".zig".len], pkg_name))
|
||||
{
|
||||
gop.value_ptr.* = file;
|
||||
}
|
||||
const file_bytes = tar_reader.take(@intCast(tar_file.size)) catch unreachable;
|
||||
it.unread_file_bytes = 0; // we have read the whole thing
|
||||
assert(file == try Walk.add_file(file_name, file_bytes));
|
||||
}
|
||||
} else {
|
||||
log.warn("skipping: '{s}' - the tar creation should have done that", .{tar_file.name});
|
||||
}
|
||||
},
|
||||
else => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn updateStats() error{OutOfMemory}!void {
|
||||
@setFloatMode(.optimized);
|
||||
|
||||
if (recent_coverage_update.items.len == 0) return;
|
||||
|
||||
const hdr: *abi.fuzz.CoverageUpdateHeader = @alignCast(@ptrCast(
|
||||
recent_coverage_update.items[0..@sizeOf(abi.fuzz.CoverageUpdateHeader)],
|
||||
));
|
||||
|
||||
const covered_src_locs: usize = n: {
|
||||
var n: usize = 0;
|
||||
const covered_bits = recent_coverage_update.items[@sizeOf(abi.fuzz.CoverageUpdateHeader)..];
|
||||
for (covered_bits) |byte| n += @popCount(byte);
|
||||
break :n n;
|
||||
};
|
||||
const total_src_locs = coverage_source_locations.items.len;
|
||||
|
||||
const avg_speed: f64 = speed: {
|
||||
const ns_elapsed: f64 = @floatFromInt(nsSince(start_fuzzing_timestamp));
|
||||
const n_runs: f64 = @floatFromInt(hdr.n_runs);
|
||||
break :speed n_runs / (ns_elapsed / std.time.ns_per_s);
|
||||
};
|
||||
|
||||
const html = try std.fmt.allocPrint(gpa,
|
||||
\\<span slot="stat-total-runs">{d}</span>
|
||||
\\<span slot="stat-unique-runs">{d} ({d:.1}%)</span>
|
||||
\\<span slot="stat-coverage">{d} / {d} ({d:.1}%)</span>
|
||||
\\<span slot="stat-speed">{d:.0}</span>
|
||||
, .{
|
||||
hdr.n_runs,
|
||||
hdr.unique_runs,
|
||||
@as(f64, @floatFromInt(hdr.unique_runs)) / @as(f64, @floatFromInt(hdr.n_runs)),
|
||||
covered_src_locs,
|
||||
total_src_locs,
|
||||
@as(f64, @floatFromInt(covered_src_locs)) / @as(f64, @floatFromInt(total_src_locs)),
|
||||
avg_speed,
|
||||
});
|
||||
defer gpa.free(html);
|
||||
|
||||
js.updateStats(html.ptr, html.len);
|
||||
}
|
||||
|
||||
fn updateEntryPoints() error{OutOfMemory}!void {
|
||||
var html: std.ArrayListUnmanaged(u8) = .empty;
|
||||
defer html.deinit(gpa);
|
||||
for (entry_points.items) |sli| {
|
||||
try html.appendSlice(gpa, "<li>");
|
||||
try sli.sourceLocationLinkHtml(&html, selected_source_location == sli);
|
||||
try html.appendSlice(gpa, "</li>\n");
|
||||
}
|
||||
js.updateEntryPoints(html.items.ptr, html.items.len);
|
||||
}
|
||||
|
||||
fn updateCoverage() error{OutOfMemory}!void {
|
||||
if (recent_coverage_update.items.len == 0) return;
|
||||
const want_file = (selected_source_location orelse return).ptr().file;
|
||||
|
||||
var covered: std.ArrayListUnmanaged(SourceLocationIndex) = .empty;
|
||||
defer covered.deinit(gpa);
|
||||
|
||||
// This code assumes 64-bit elements, which is incorrect if the executable
|
||||
// being fuzzed is not a 64-bit CPU. It also assumes little-endian which
|
||||
// can also be incorrect.
|
||||
comptime assert(abi.fuzz.CoverageUpdateHeader.trailing[0] == .pc_bits_usize);
|
||||
const n_bitset_elems = (coverage_source_locations.items.len + @bitSizeOf(u64) - 1) / @bitSizeOf(u64);
|
||||
const covered_bits = std.mem.bytesAsSlice(
|
||||
u64,
|
||||
recent_coverage_update.items[@sizeOf(abi.fuzz.CoverageUpdateHeader)..][0 .. n_bitset_elems * @sizeOf(u64)],
|
||||
);
|
||||
var sli: SourceLocationIndex = @enumFromInt(0);
|
||||
for (covered_bits) |elem| {
|
||||
try covered.ensureUnusedCapacity(gpa, 64);
|
||||
for (0..@bitSizeOf(u64)) |i| {
|
||||
if ((elem & (@as(u64, 1) << @intCast(i))) != 0) {
|
||||
if (sli.ptr().file == want_file) {
|
||||
covered.appendAssumeCapacity(sli);
|
||||
}
|
||||
}
|
||||
sli = @enumFromInt(@intFromEnum(sli) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
js.updateCoverage(covered.items.ptr, covered.items.len);
|
||||
}
|
||||
|
||||
fn updateSource() error{OutOfMemory}!void {
|
||||
if (recent_coverage_update.items.len == 0) return;
|
||||
const file_sli = selected_source_location.?;
|
||||
var html: std.ArrayListUnmanaged(u8) = .empty;
|
||||
defer html.deinit(gpa);
|
||||
file_sli.fileHtml(&html) catch |err| switch (err) {
|
||||
error.OutOfMemory => |e| return e,
|
||||
error.SourceUnavailable => {},
|
||||
};
|
||||
js.updateSource(html.items.ptr, html.items.len);
|
||||
}
|
||||
|
||||
var selected_source_location: ?SourceLocationIndex = null;
|
||||
|
||||
/// This function is not used directly by `main.js`, but a reference to it is
|
||||
/// emitted by `SourceLocationIndex.sourceLocationLinkHtml`.
|
||||
export fn fuzzSelectSli(sli: SourceLocationIndex) void {
|
||||
if (!sli.haveCoverage()) return;
|
||||
selected_source_location = sli;
|
||||
updateEntryPoints() catch @panic("out of memory"); // highlights the selected one green
|
||||
updateSource() catch @panic("out of memory");
|
||||
updateCoverage() catch @panic("out of memory");
|
||||
}
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
const Coverage = std.debug.Coverage;
|
||||
const abi = std.Build.abi;
|
||||
const assert = std.debug.assert;
|
||||
const gpa = std.heap.wasm_allocator;
|
||||
|
||||
const Walk = @import("Walk");
|
||||
const html_render = @import("html_render");
|
||||
|
||||
const nsSince = @import("main.zig").nsSince;
|
||||
const Slice = @import("main.zig").Slice;
|
||||
const fatal = @import("main.zig").fatal;
|
||||
const log = std.log;
|
||||
const String = Slice(u8);
|
||||
202
lib/build-web/index.html
Normal file
202
lib/build-web/index.html
Normal file
@@ -0,0 +1,202 @@
|
||||
<!doctype html>
|
||||
|
||||
<meta charset="utf-8">
|
||||
<title>Zig Build System</title>
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<!-- Highly compressed 32x32 Zig logo -->
|
||||
<link rel="icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABSklEQVRYw8WWXbLDIAiFP5xuURYpi+Q+VDvJTYxaY8pLJ52EA5zDj/AD8wRABCw8DeyJBDiAKMiDGaecNYCKYgCvh4EBjPgGh0UVqAB/MEU3D57efDRMiRhWddprCljRAECPCE0Uw4iz4Jn3tP2zFYAB6on4/8NBM1Es+9kl0aKgaMRnwHPpT5MIDb6YzLzp57wNIyIC7iCCdijeL3gv78jZe6cVENn/drRbXbxl4lXSmB3FtbY0iNrjIEwMm6u2VFFjWQCN0qtov6+wANxG/IV7eR8DHw6gzft4NuEXvA8HcDfv31SgyvsMeDUA90/WTd47bsCdv8PUrWzDyw02uIYv13ktgOVr+IqCouila7gWgNYuly/BfVSEdsP5Vdqyiz7pPC40C+p2e21bL5/dByGtAD6eZPuzeznwjoIN748BfyqwmVDyJHCxPwLSkjUkraEXAAAAAElFTkSuQmCC">
|
||||
|
||||
<!-- Templates, to be cloned into shadow DOMs by JavaScript -->
|
||||
|
||||
<template id="timeReportEntryTemplate">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<link rel="stylesheet" href="time_report.css">
|
||||
<details>
|
||||
<summary><slot name="step-name"></slot></summary>
|
||||
<div id="genericReport">
|
||||
<div class="stats">
|
||||
Time: <slot name="stat-total-time"></slot><br>
|
||||
</div>
|
||||
</div>
|
||||
<div id="compileReport">
|
||||
<div class="stats">
|
||||
Files Discovered: <slot name="stat-reachable-files"></slot><br>
|
||||
Files Analyzed: <slot name="stat-imported-files"></slot><br>
|
||||
Generic Instances Analyzed: <slot name="stat-generic-instances"></slot><br>
|
||||
Inline Calls Analyzed: <slot name="stat-inline-calls"></slot><br>
|
||||
Compilation Time: <slot name="stat-compilation-time"></slot><br>
|
||||
</div>
|
||||
<table class="time-stats">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">Pipeline Component</th>
|
||||
<th scope="col" class="tooltip">CPU Time
|
||||
<span class="tooltip-content">Sum across all threads of the time spent in this pipeline component</span>
|
||||
</th>
|
||||
<th scope="col" class="tooltip">Real Time
|
||||
<span class="tooltip-content">Wall-clock time elapsed between the start and end of this compilation phase</span>
|
||||
</th>
|
||||
<th scope="col">Compilation Phase</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th scope="row" class="tooltip">Parsing
|
||||
<span class="tooltip-content"><code>tokenize</code> converts a file of Zig source code into a sequence of tokens, which are then processed by <code>Parse</code> into an Abstract Syntax Tree (AST).</span>
|
||||
</th>
|
||||
<td><slot name="cpu-time-parse"></slot></td>
|
||||
<td rowspan="2"><slot name="real-time-files"></slot></td>
|
||||
<th scope="row" rowspan="2" class="tooltip">File Lower
|
||||
<span class="tooltip-content">Tokenization, parsing, and lowering of Zig source files to a high-level IR.<br><br>Starting from module roots, every file theoretically accessible through a chain of <code>@import</code> calls is processed. Individual source files are processed serially, but different files are processed in parallel by a thread pool.<br><br>The results of this phase of compilation are cached on disk per source file, meaning the time spent here is typically only relevant to "clean" builds.</span>
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="tooltip">AST Lowering
|
||||
<span class="tooltip-content"><code>AstGen</code> converts a file's AST into a high-level SSA IR named Zig Intermediate Representation (ZIR). The resulting ZIR code is cached on disk to avoid, for instance, re-lowering all source files in the Zig standard library each time the compiler is invoked.</span>
|
||||
</th>
|
||||
<td><slot name="cpu-time-astgen"></slot></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="tooltip">Semantic Analysis
|
||||
<span class="tooltip-content"><code>Sema</code> interprets ZIR to perform type checking, compile-time code execution, and type resolution, collectively termed "semantic analysis". When a runtime function body is analyzed, it emits Analyzed Intermediate Representation (AIR) code to be sent to the next pipeline component. Semantic analysis is currently entirely single-threaded.</span>
|
||||
</th>
|
||||
<td><slot name="cpu-time-sema"></slot></td>
|
||||
<td rowspan="3"><slot name="real-time-decls"></slot></td>
|
||||
<th scope="row" rowspan="3" class="tooltip">Declaration Lower
|
||||
<span class="tooltip-content">Semantic analysis, code generation, and linking, at the granularity of individual declarations (as opposed to whole source files).<br><br>These components are run in parallel with one another. Semantic analysis is almost always the bottleneck, as it is complex and currently can only run single-threaded.<br><br>This phase completes when a work queue empties, but semantic analysis may add work by one declaration referencing another.<br><br>This is the main phase of compilation, typically taking significantly longer than File Lower (even in a clean build).</span>
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="tooltip">Code Generation
|
||||
<span class="tooltip-content"><code>CodeGen</code> converts AIR from <code>Sema</code> into machine instructions in the form of Machine Intermediate Representation (MIR). This work is usually highly parallel, since in most cases, arbitrarily many functions can be run through <code>CodeGen</code> simultaneously.</span>
|
||||
</th>
|
||||
<td><slot name="cpu-time-codegen"></slot></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row" class="tooltip">Linking
|
||||
<span class="tooltip-content"><code>link</code> converts MIR from <code>CodeGen</code>, as well as global constants and variables from <code>Sema</code>, and places them in the output binary. MIR is converted to a finished sequence of real instruction bytes.<br><br>When using the LLVM backend, most of this work is instead deferred to the "LLVM Emit" phase.</span>
|
||||
</th>
|
||||
<td><slot name="cpu-time-link"></slot></td>
|
||||
</tr>
|
||||
<tr class="llvm-only">
|
||||
<th class="empty-cell"></th>
|
||||
<td class="empty-cell"></td>
|
||||
<td><slot name="real-time-llvm-emit"></slot></td>
|
||||
<th scope="row" class="tooltip">LLVM Emit
|
||||
<span class="tooltip-content"><b>Only applicable when using the LLVM backend.</b><br><br>Conversion of generated LLVM bitcode to an object file, including any optimization passes.<br><br>When using LLVM, this phase of compilation is typically the slowest by a significant margin. Unfortunately, the Zig compiler implementation has essentially no control over it.</span>
|
||||
</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<th class="empty-cell"></th>
|
||||
<td class="empty-cell"></td>
|
||||
<td><slot name="real-time-link-flush"></slot></td>
|
||||
<th scope="row" class="tooltip">Linker Flush
|
||||
<span class="tooltip-content">Finalizing the emitted binary, and ensuring it is fully written to disk.<br><br>When using LLD, this phase represents the entire linker invocation. Otherwise, the amount of work performed here is dependent on details of Zig's linker implementation for the particular output format, but typically aims to be fairly minimal.</span>
|
||||
</th>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<details class="section">
|
||||
<summary>Files</summary>
|
||||
<table class="time-stats">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">File</th>
|
||||
<th scope="col">Semantic Analysis</th>
|
||||
<th scope="col">Code Generation</th>
|
||||
<th scope="col">Linking</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<!-- HTML does not allow placing a 'slot' inside of a 'tbody' for backwards-compatibility
|
||||
reasons, so we unfortunately must template on the `id` here. -->
|
||||
<tbody id="fileTableBody"></tbody>
|
||||
</table>
|
||||
</details>
|
||||
<details class="section">
|
||||
<summary>Declarations</summary>
|
||||
<table class="time-stats">
|
||||
<thead>
|
||||
<tr>
|
||||
<th scope="col">File</th>
|
||||
<th scope="col">Declaration</th>
|
||||
<th scope="col" class="tooltip">Analysis Count
|
||||
<span class="tooltip-content">The number of times the compiler analyzed some part of this declaration. If this is a function, <code>inline</code> and <code>comptime</code> calls to it are <i>not</i> included here. Typically, this value is approximately equal to the number of instances of a generic declaration.</span>
|
||||
</th>
|
||||
<th scope="col">Semantic Analysis</th>
|
||||
<th scope="col">Code Generation</th>
|
||||
<th scope="col">Linking</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<!-- HTML does not allow placing a 'slot' inside of a 'tbody' for backwards-compatibility
|
||||
reasons, so we unfortunately must template on the `id` here. -->
|
||||
<tbody id="declTableBody"></tbody>
|
||||
</table>
|
||||
</details>
|
||||
<details class="section llvm-only">
|
||||
<summary>LLVM Pass Timings</summary>
|
||||
<div><slot name="llvm-pass-timings"></slot></div>
|
||||
</details>
|
||||
</div>
|
||||
</details>
|
||||
</template>
|
||||
|
||||
<template id="fuzzEntryTemplate">
|
||||
<link rel="stylesheet" href="style.css">
|
||||
<ul>
|
||||
<li>Total Runs: <slot name="stat-total-runs"></slot></li>
|
||||
<li>Unique Runs: <slot name="stat-unique-runs"></slot></li>
|
||||
<li>Speed: <slot name="stat-speed"></slot> runs/sec</li>
|
||||
<li>Coverage: <slot name="stat-coverage"></slot></li>
|
||||
</ul>
|
||||
<!-- I have observed issues in Firefox clicking frequently-updating slotted links, so the entry
|
||||
point list is handled separately since it rarely changes. -->
|
||||
<ul id="entryPointList" class="no-marker"></ul>
|
||||
<div id="source" class="hidden">
|
||||
<h2>Source Code</h2>
|
||||
<pre><code id="sourceText"></code></pre>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<!-- The actual body: fairly minimal, content populated by JavaScript -->
|
||||
|
||||
<p id="connectionStatus">Loading JavaScript...</p>
|
||||
<p class="hidden" id="firefoxWebSocketBullshitExplainer">
|
||||
If you are using Firefox and <code>zig build --listen</code> is definitely running, you may be experiencing an unreasonably aggressive exponential
|
||||
backoff for WebSocket connection attempts, which is enabled by default and can block connection attempts for up to a minute. To disable this limit,
|
||||
open <code>about:config</code> and set the <code>network.websocket.delay-failed-reconnects</code> option to <code>false</code>.
|
||||
</p>
|
||||
<main class="hidden">
|
||||
<h1>Zig Build System</h1>
|
||||
|
||||
<p><span id="summaryStatus"></span> | <span id="summaryStepCount"></span> steps</p>
|
||||
<button class="big-btn" id="buttonRebuild" disabled>Rebuild</button>
|
||||
|
||||
<ul class="no-marker" id="stepList"></ul>
|
||||
|
||||
<hr>
|
||||
|
||||
<div id="timeReport" class="hidden">
|
||||
<h1>Time Report</h1>
|
||||
<div id="timeReportList"></div>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<div id="fuzz" class="hidden">
|
||||
<h1>Fuzzer</h1>
|
||||
<p id="fuzzStatus"></p>
|
||||
<div id="fuzzEntries"></div>
|
||||
<hr>
|
||||
</div>
|
||||
|
||||
<h1>Help</h1>
|
||||
<p>This is the Zig Build System web interface. It allows live interaction with the build system.</p>
|
||||
<p>The following <code>zig build</code> flags can expose extra features of this interface:</p>
|
||||
<ul>
|
||||
<li><code>--time-report</code>: collect and show statistics about the time taken to evaluate a build graph</li>
|
||||
<li><code>--fuzz</code>: enable the fuzzer for any Zig test binaries in the build graph (experimental)</li>
|
||||
</ul>
|
||||
</main>
|
||||
|
||||
<!-- JavaScript at the very end -->
|
||||
|
||||
<script src="main.js"></script>
|
||||
346
lib/build-web/main.js
Normal file
346
lib/build-web/main.js
Normal file
@@ -0,0 +1,346 @@
|
||||
const domConnectionStatus = document.getElementById("connectionStatus");
|
||||
const domFirefoxWebSocketBullshitExplainer = document.getElementById("firefoxWebSocketBullshitExplainer");
|
||||
|
||||
const domMain = document.getElementsByTagName("main")[0];
|
||||
const domSummary = {
|
||||
stepCount: document.getElementById("summaryStepCount"),
|
||||
status: document.getElementById("summaryStatus"),
|
||||
};
|
||||
const domButtonRebuild = document.getElementById("buttonRebuild");
|
||||
const domStepList = document.getElementById("stepList");
|
||||
let domSteps = [];
|
||||
|
||||
let wasm_promise = fetch("main.wasm");
|
||||
let wasm_exports = null;
|
||||
|
||||
const text_decoder = new TextDecoder();
|
||||
const text_encoder = new TextEncoder();
|
||||
|
||||
domButtonRebuild.addEventListener("click", () => wasm_exports.rebuild());
|
||||
|
||||
setConnectionStatus("Loading WebAssembly...", false);
|
||||
WebAssembly.instantiateStreaming(wasm_promise, {
|
||||
core: {
|
||||
log: function(ptr, len) {
|
||||
const msg = decodeString(ptr, len);
|
||||
console.log(msg);
|
||||
},
|
||||
panic: function (ptr, len) {
|
||||
const msg = decodeString(ptr, len);
|
||||
throw new Error("panic: " + msg);
|
||||
},
|
||||
timestamp: function () {
|
||||
return BigInt(new Date());
|
||||
},
|
||||
hello: hello,
|
||||
updateBuildStatus: updateBuildStatus,
|
||||
updateStepStatus: updateStepStatus,
|
||||
sendWsMessage: (ptr, len) => ws.send(new Uint8Array(wasm_exports.memory.buffer, ptr, len)),
|
||||
},
|
||||
fuzz: {
|
||||
requestSources: fuzzRequestSources,
|
||||
ready: fuzzReady,
|
||||
updateStats: fuzzUpdateStats,
|
||||
updateEntryPoints: fuzzUpdateEntryPoints,
|
||||
updateSource: fuzzUpdateSource,
|
||||
updateCoverage: fuzzUpdateCoverage,
|
||||
},
|
||||
time_report: {
|
||||
updateCompile: timeReportUpdateCompile,
|
||||
updateGeneric: timeReportUpdateGeneric,
|
||||
},
|
||||
}).then(function(obj) {
|
||||
setConnectionStatus("Connecting to WebSocket...", true);
|
||||
connectWebSocket();
|
||||
|
||||
wasm_exports = obj.instance.exports;
|
||||
window.wasm = obj; // for debugging
|
||||
});
|
||||
|
||||
function connectWebSocket() {
|
||||
const host = document.location.host;
|
||||
const pathname = document.location.pathname;
|
||||
const isHttps = document.location.protocol === 'https:';
|
||||
const match = host.match(/^(.+):(\d+)$/);
|
||||
const defaultPort = isHttps ? 443 : 80;
|
||||
const port = match ? parseInt(match[2], 10) : defaultPort;
|
||||
const hostName = match ? match[1] : host;
|
||||
const wsProto = isHttps ? "wss:" : "ws:";
|
||||
const wsUrl = wsProto + '//' + hostName + ':' + port + pathname;
|
||||
ws = new WebSocket(wsUrl);
|
||||
ws.binaryType = "arraybuffer";
|
||||
ws.addEventListener('message', onWebSocketMessage, false);
|
||||
ws.addEventListener('error', onWebSocketClose, false);
|
||||
ws.addEventListener('close', onWebSocketClose, false);
|
||||
ws.addEventListener('open', onWebSocketOpen, false);
|
||||
}
|
||||
function onWebSocketOpen() {
|
||||
setConnectionStatus("Waiting for data...", false);
|
||||
}
|
||||
function onWebSocketMessage(ev) {
|
||||
const jsArray = new Uint8Array(ev.data);
|
||||
const ptr = wasm_exports.message_begin(jsArray.length);
|
||||
const wasmArray = new Uint8Array(wasm_exports.memory.buffer, ptr, jsArray.length);
|
||||
wasmArray.set(jsArray);
|
||||
wasm_exports.message_end();
|
||||
}
|
||||
function onWebSocketClose() {
|
||||
setConnectionStatus("WebSocket connection closed. Re-connecting...", true);
|
||||
ws.removeEventListener('message', onWebSocketMessage, false);
|
||||
ws.removeEventListener('error', onWebSocketClose, false);
|
||||
ws.removeEventListener('close', onWebSocketClose, false);
|
||||
ws.removeEventListener('open', onWebSocketOpen, false);
|
||||
ws = null;
|
||||
setTimeout(connectWebSocket, 1000);
|
||||
}
|
||||
|
||||
function setConnectionStatus(msg, is_websocket_connect) {
|
||||
domConnectionStatus.textContent = msg;
|
||||
if (msg.length > 0) {
|
||||
domConnectionStatus.classList.remove("hidden");
|
||||
domMain.classList.add("hidden");
|
||||
} else {
|
||||
domConnectionStatus.classList.add("hidden");
|
||||
domMain.classList.remove("hidden");
|
||||
}
|
||||
if (is_websocket_connect) {
|
||||
domFirefoxWebSocketBullshitExplainer.classList.remove("hidden");
|
||||
} else {
|
||||
domFirefoxWebSocketBullshitExplainer.classList.add("hidden");
|
||||
}
|
||||
}
|
||||
|
||||
function hello(
|
||||
steps_len,
|
||||
build_status,
|
||||
time_report,
|
||||
) {
|
||||
domSummary.stepCount.textContent = steps_len;
|
||||
updateBuildStatus(build_status);
|
||||
setConnectionStatus("", false);
|
||||
|
||||
{
|
||||
let entries = [];
|
||||
for (let i = 0; i < steps_len; i += 1) {
|
||||
const step_name = unwrapString(wasm_exports.stepName(i));
|
||||
const code = document.createElement("code");
|
||||
code.textContent = step_name;
|
||||
const li = document.createElement("li");
|
||||
li.appendChild(code);
|
||||
entries.push(li);
|
||||
}
|
||||
domStepList.replaceChildren(...entries);
|
||||
for (let i = 0; i < steps_len; i += 1) {
|
||||
updateStepStatus(i);
|
||||
}
|
||||
}
|
||||
|
||||
if (time_report) timeReportReset(steps_len);
|
||||
fuzzReset();
|
||||
}
|
||||
|
||||
function updateBuildStatus(s) {
|
||||
let text;
|
||||
let active = false;
|
||||
let reset_time_reports = false;
|
||||
if (s == 0) {
|
||||
text = "Idle";
|
||||
} else if (s == 1) {
|
||||
text = "Watching for changes...";
|
||||
} else if (s == 2) {
|
||||
text = "Running...";
|
||||
active = true;
|
||||
reset_time_reports = true;
|
||||
} else if (s == 3) {
|
||||
text = "Starting fuzzer...";
|
||||
active = true;
|
||||
} else {
|
||||
console.log(`bad build status: ${s}`);
|
||||
}
|
||||
domSummary.status.textContent = text;
|
||||
if (active) {
|
||||
domSummary.status.classList.add("status-running");
|
||||
domSummary.status.classList.remove("status-idle");
|
||||
domButtonRebuild.disabled = true;
|
||||
} else {
|
||||
domSummary.status.classList.remove("status-running");
|
||||
domSummary.status.classList.add("status-idle");
|
||||
domButtonRebuild.disabled = false;
|
||||
}
|
||||
if (reset_time_reports) {
|
||||
// Grey out and collapse all the time reports
|
||||
for (const time_report_host of domTimeReportList.children) {
|
||||
const details = time_report_host.shadowRoot.querySelector(":host > details");
|
||||
details.classList.add("pending");
|
||||
details.open = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
function updateStepStatus(step_idx) {
|
||||
const li = domStepList.children[step_idx];
|
||||
const step_status = wasm_exports.stepStatus(step_idx);
|
||||
li.classList.remove("step-wip", "step-success", "step-failure");
|
||||
if (step_status == 0) {
|
||||
// pending
|
||||
} else if (step_status == 1) {
|
||||
li.classList.add("step-wip");
|
||||
} else if (step_status == 2) {
|
||||
li.classList.add("step-success");
|
||||
} else if (step_status == 3) {
|
||||
li.classList.add("step-failure");
|
||||
} else {
|
||||
console.log(`bad step status: ${step_status}`);
|
||||
}
|
||||
}
|
||||
|
||||
function decodeString(ptr, len) {
|
||||
if (len === 0) return "";
|
||||
return text_decoder.decode(new Uint8Array(wasm_exports.memory.buffer, ptr, len));
|
||||
}
|
||||
function getU32Array(ptr, len) {
|
||||
if (len === 0) return new Uint32Array();
|
||||
return new Uint32Array(wasm_exports.memory.buffer, ptr, len);
|
||||
}
|
||||
function unwrapString(bigint) {
|
||||
const ptr = Number(bigint & 0xffffffffn);
|
||||
const len = Number(bigint >> 32n);
|
||||
return decodeString(ptr, len);
|
||||
}
|
||||
|
||||
const time_report_entry_template = document.getElementById("timeReportEntryTemplate").content;
|
||||
const domTimeReport = document.getElementById("timeReport");
|
||||
const domTimeReportList = document.getElementById("timeReportList");
|
||||
function timeReportReset(steps_len) {
|
||||
let entries = [];
|
||||
for (let i = 0; i < steps_len; i += 1) {
|
||||
const step_name = unwrapString(wasm_exports.stepName(i));
|
||||
const host = document.createElement("div");
|
||||
const shadow = host.attachShadow({ mode: "open" });
|
||||
shadow.appendChild(time_report_entry_template.cloneNode(true));
|
||||
shadow.querySelector(":host > details").classList.add("pending");
|
||||
const slotted_name = document.createElement("code");
|
||||
slotted_name.setAttribute("slot", "step-name");
|
||||
slotted_name.textContent = step_name;
|
||||
host.appendChild(slotted_name);
|
||||
entries.push(host);
|
||||
}
|
||||
domTimeReportList.replaceChildren(...entries);
|
||||
domTimeReport.classList.remove("hidden");
|
||||
}
|
||||
function timeReportUpdateCompile(
|
||||
step_idx,
|
||||
inner_html_ptr,
|
||||
inner_html_len,
|
||||
file_table_html_ptr,
|
||||
file_table_html_len,
|
||||
decl_table_html_ptr,
|
||||
decl_table_html_len,
|
||||
use_llvm,
|
||||
) {
|
||||
const inner_html = decodeString(inner_html_ptr, inner_html_len);
|
||||
const file_table_html = decodeString(file_table_html_ptr, file_table_html_len);
|
||||
const decl_table_html = decodeString(decl_table_html_ptr, decl_table_html_len);
|
||||
|
||||
const host = domTimeReportList.children.item(step_idx);
|
||||
const shadow = host.shadowRoot;
|
||||
|
||||
shadow.querySelector(":host > details").classList.remove("pending", "no-llvm");
|
||||
|
||||
shadow.getElementById("genericReport").classList.add("hidden");
|
||||
shadow.getElementById("compileReport").classList.remove("hidden");
|
||||
|
||||
if (!use_llvm) shadow.querySelector(":host > details").classList.add("no-llvm");
|
||||
host.innerHTML = inner_html;
|
||||
shadow.getElementById("fileTableBody").innerHTML = file_table_html;
|
||||
shadow.getElementById("declTableBody").innerHTML = decl_table_html;
|
||||
}
|
||||
function timeReportUpdateGeneric(
|
||||
step_idx,
|
||||
inner_html_ptr,
|
||||
inner_html_len,
|
||||
) {
|
||||
const inner_html = decodeString(inner_html_ptr, inner_html_len);
|
||||
const host = domTimeReportList.children.item(step_idx);
|
||||
const shadow = host.shadowRoot;
|
||||
shadow.querySelector(":host > details").classList.remove("pending", "no-llvm");
|
||||
shadow.getElementById("genericReport").classList.remove("hidden");
|
||||
shadow.getElementById("compileReport").classList.add("hidden");
|
||||
host.innerHTML = inner_html;
|
||||
}
|
||||
|
||||
const fuzz_entry_template = document.getElementById("fuzzEntryTemplate").content;
|
||||
const domFuzz = document.getElementById("fuzz");
|
||||
const domFuzzStatus = document.getElementById("fuzzStatus");
|
||||
const domFuzzEntries = document.getElementById("fuzzEntries");
|
||||
let domFuzzInstance = null;
|
||||
function fuzzRequestSources() {
|
||||
domFuzzStatus.classList.remove("hidden");
|
||||
domFuzzStatus.textContent = "Loading sources tarball...";
|
||||
fetch("sources.tar").then(function(response) {
|
||||
if (!response.ok) throw new Error("unable to download sources");
|
||||
domFuzzStatus.textContent = "Parsing fuzz test sources...";
|
||||
return response.arrayBuffer();
|
||||
}).then(function(buffer) {
|
||||
if (buffer.length === 0) throw new Error("sources.tar was empty");
|
||||
const js_array = new Uint8Array(buffer);
|
||||
const ptr = wasm_exports.alloc(js_array.length);
|
||||
const wasm_array = new Uint8Array(wasm_exports.memory.buffer, ptr, js_array.length);
|
||||
wasm_array.set(js_array);
|
||||
wasm_exports.fuzzUnpackSources(ptr, js_array.length);
|
||||
domFuzzStatus.textContent = "";
|
||||
domFuzzStatus.classList.add("hidden");
|
||||
});
|
||||
}
|
||||
function fuzzReady() {
|
||||
domFuzz.classList.remove("hidden");
|
||||
|
||||
// TODO: multiple fuzzer instances
|
||||
if (domFuzzInstance !== null) return;
|
||||
|
||||
const host = document.createElement("div");
|
||||
const shadow = host.attachShadow({ mode: "open" });
|
||||
shadow.appendChild(fuzz_entry_template.cloneNode(true));
|
||||
|
||||
domFuzzInstance = host;
|
||||
domFuzzEntries.appendChild(host);
|
||||
}
|
||||
function fuzzReset() {
|
||||
domFuzz.classList.add("hidden");
|
||||
domFuzzEntries.replaceChildren();
|
||||
domFuzzInstance = null;
|
||||
}
|
||||
function fuzzUpdateStats(stats_html_ptr, stats_html_len) {
|
||||
if (domFuzzInstance === null) throw new Error("fuzzUpdateStats called when fuzzer inactive");
|
||||
const stats_html = decodeString(stats_html_ptr, stats_html_len);
|
||||
const host = domFuzzInstance;
|
||||
host.innerHTML = stats_html;
|
||||
}
|
||||
function fuzzUpdateEntryPoints(entry_points_html_ptr, entry_points_html_len) {
|
||||
if (domFuzzInstance === null) throw new Error("fuzzUpdateEntryPoints called when fuzzer inactive");
|
||||
const entry_points_html = decodeString(entry_points_html_ptr, entry_points_html_len);
|
||||
const domEntryPointList = domFuzzInstance.shadowRoot.getElementById("entryPointList");
|
||||
domEntryPointList.innerHTML = entry_points_html;
|
||||
}
|
||||
function fuzzUpdateSource(source_html_ptr, source_html_len) {
|
||||
if (domFuzzInstance === null) throw new Error("fuzzUpdateSource called when fuzzer inactive");
|
||||
const source_html = decodeString(source_html_ptr, source_html_len);
|
||||
const domSourceText = domFuzzInstance.shadowRoot.getElementById("sourceText");
|
||||
domSourceText.innerHTML = source_html;
|
||||
domFuzzInstance.shadowRoot.getElementById("source").classList.remove("hidden");
|
||||
}
|
||||
function fuzzUpdateCoverage(covered_ptr, covered_len) {
|
||||
if (domFuzzInstance === null) throw new Error("fuzzUpdateCoverage called when fuzzer inactive");
|
||||
const shadow = domFuzzInstance.shadowRoot;
|
||||
const domSourceText = shadow.getElementById("sourceText");
|
||||
const covered = getU32Array(covered_ptr, covered_len);
|
||||
for (let i = 0; i < domSourceText.children.length; i += 1) {
|
||||
const childDom = domSourceText.children[i];
|
||||
if (childDom.id != null && childDom.id[0] == "l") {
|
||||
childDom.classList.add("l");
|
||||
childDom.classList.remove("c");
|
||||
}
|
||||
}
|
||||
for (const sli of covered) {
|
||||
shadow.getElementById(`l${sli}`).classList.add("c");
|
||||
}
|
||||
}
|
||||
213
lib/build-web/main.zig
Normal file
213
lib/build-web/main.zig
Normal file
@@ -0,0 +1,213 @@
|
||||
const std = @import("std");
|
||||
const assert = std.debug.assert;
|
||||
const abi = std.Build.abi;
|
||||
const gpa = std.heap.wasm_allocator;
|
||||
const log = std.log;
|
||||
const Allocator = std.mem.Allocator;
|
||||
|
||||
const fuzz = @import("fuzz.zig");
|
||||
const time_report = @import("time_report.zig");
|
||||
|
||||
/// Nanoseconds.
|
||||
var server_base_timestamp: i64 = 0;
|
||||
/// Milliseconds.
|
||||
var client_base_timestamp: i64 = 0;
|
||||
|
||||
pub var step_list: []Step = &.{};
|
||||
/// Not accessed after initialization, but must be freed alongside `step_list`.
|
||||
pub var step_list_data: []u8 = &.{};
|
||||
|
||||
const Step = struct {
|
||||
name: []const u8,
|
||||
status: abi.StepUpdate.Status,
|
||||
};
|
||||
|
||||
const js = struct {
|
||||
extern "core" fn log(ptr: [*]const u8, len: usize) void;
|
||||
extern "core" fn panic(ptr: [*]const u8, len: usize) noreturn;
|
||||
extern "core" fn timestamp() i64;
|
||||
extern "core" fn hello(
|
||||
steps_len: u32,
|
||||
status: abi.BuildStatus,
|
||||
time_report: bool,
|
||||
) void;
|
||||
extern "core" fn updateBuildStatus(status: abi.BuildStatus) void;
|
||||
extern "core" fn updateStepStatus(step_idx: u32) void;
|
||||
extern "core" fn sendWsMessage(ptr: [*]const u8, len: usize) void;
|
||||
};
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
.logFn = logFn,
|
||||
};
|
||||
|
||||
pub fn panic(msg: []const u8, st: ?*std.builtin.StackTrace, addr: ?usize) noreturn {
|
||||
_ = st;
|
||||
_ = addr;
|
||||
log.err("panic: {s}", .{msg});
|
||||
@trap();
|
||||
}
|
||||
|
||||
fn logFn(
|
||||
comptime message_level: log.Level,
|
||||
comptime scope: @TypeOf(.enum_literal),
|
||||
comptime format: []const u8,
|
||||
args: anytype,
|
||||
) void {
|
||||
const level_txt = comptime message_level.asText();
|
||||
const prefix2 = if (scope == .default) ": " else "(" ++ @tagName(scope) ++ "): ";
|
||||
var buf: [500]u8 = undefined;
|
||||
const line = std.fmt.bufPrint(&buf, level_txt ++ prefix2 ++ format, args) catch l: {
|
||||
buf[buf.len - 3 ..][0..3].* = "...".*;
|
||||
break :l &buf;
|
||||
};
|
||||
js.log(line.ptr, line.len);
|
||||
}
|
||||
|
||||
export fn alloc(n: usize) [*]u8 {
|
||||
const slice = gpa.alloc(u8, n) catch @panic("OOM");
|
||||
return slice.ptr;
|
||||
}
|
||||
|
||||
var message_buffer: std.ArrayListAlignedUnmanaged(u8, .of(u64)) = .empty;
|
||||
|
||||
/// Resizes the message buffer to be the correct length; returns the pointer to
|
||||
/// the query string.
|
||||
export fn message_begin(len: usize) [*]u8 {
|
||||
message_buffer.resize(gpa, len) catch @panic("OOM");
|
||||
return message_buffer.items.ptr;
|
||||
}
|
||||
|
||||
export fn message_end() void {
|
||||
const msg_bytes = message_buffer.items;
|
||||
|
||||
const tag: abi.ToClientTag = @enumFromInt(msg_bytes[0]);
|
||||
switch (tag) {
|
||||
_ => @panic("malformed message"),
|
||||
|
||||
.hello => return helloMessage(msg_bytes) catch @panic("OOM"),
|
||||
.status_update => return statusUpdateMessage(msg_bytes) catch @panic("OOM"),
|
||||
.step_update => return stepUpdateMessage(msg_bytes) catch @panic("OOM"),
|
||||
|
||||
.fuzz_source_index => return fuzz.sourceIndexMessage(msg_bytes) catch @panic("OOM"),
|
||||
.fuzz_coverage_update => return fuzz.coverageUpdateMessage(msg_bytes) catch @panic("OOM"),
|
||||
.fuzz_entry_points => return fuzz.entryPointsMessage(msg_bytes) catch @panic("OOM"),
|
||||
|
||||
.time_report_generic_result => return time_report.genericResultMessage(msg_bytes) catch @panic("OOM"),
|
||||
.time_report_compile_result => return time_report.compileResultMessage(msg_bytes) catch @panic("OOM"),
|
||||
}
|
||||
}
|
||||
|
||||
const String = Slice(u8);
|
||||
|
||||
pub fn Slice(T: type) type {
|
||||
return packed struct(u64) {
|
||||
ptr: u32,
|
||||
len: u32,
|
||||
|
||||
pub fn init(s: []const T) @This() {
|
||||
return .{
|
||||
.ptr = @intFromPtr(s.ptr),
|
||||
.len = s.len,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
pub fn fatal(comptime format: []const u8, args: anytype) noreturn {
|
||||
var buf: [500]u8 = undefined;
|
||||
const line = std.fmt.bufPrint(&buf, format, args) catch l: {
|
||||
buf[buf.len - 3 ..][0..3].* = "...".*;
|
||||
break :l &buf;
|
||||
};
|
||||
js.panic(line.ptr, line.len);
|
||||
}
|
||||
|
||||
fn helloMessage(msg_bytes: []align(4) u8) Allocator.Error!void {
|
||||
if (msg_bytes.len < @sizeOf(abi.Hello)) @panic("malformed Hello message");
|
||||
const hdr: *const abi.Hello = @ptrCast(msg_bytes[0..@sizeOf(abi.Hello)]);
|
||||
const trailing = msg_bytes[@sizeOf(abi.Hello)..];
|
||||
|
||||
client_base_timestamp = js.timestamp();
|
||||
server_base_timestamp = hdr.timestamp;
|
||||
|
||||
const steps = try gpa.alloc(Step, hdr.steps_len);
|
||||
errdefer gpa.free(steps);
|
||||
|
||||
const step_name_lens: []align(1) const u32 = @ptrCast(trailing[0 .. steps.len * 4]);
|
||||
|
||||
const step_name_data_len: usize = len: {
|
||||
var sum: usize = 0;
|
||||
for (step_name_lens) |n| sum += n;
|
||||
break :len sum;
|
||||
};
|
||||
const step_name_data: []const u8 = trailing[steps.len * 4 ..][0..step_name_data_len];
|
||||
const step_status_bits: []const u8 = trailing[steps.len * 4 + step_name_data_len ..];
|
||||
|
||||
const duped_step_name_data = try gpa.dupe(u8, step_name_data);
|
||||
errdefer gpa.free(duped_step_name_data);
|
||||
|
||||
var name_off: usize = 0;
|
||||
for (steps, step_name_lens, 0..) |*step_out, name_len, step_idx| {
|
||||
step_out.* = .{
|
||||
.name = duped_step_name_data[name_off..][0..name_len],
|
||||
.status = @enumFromInt(@as(u2, @truncate(step_status_bits[step_idx / 4] >> @intCast((step_idx % 4) * 2)))),
|
||||
};
|
||||
name_off += name_len;
|
||||
}
|
||||
|
||||
gpa.free(step_list);
|
||||
gpa.free(step_list_data);
|
||||
step_list = steps;
|
||||
step_list_data = duped_step_name_data;
|
||||
|
||||
js.hello(step_list.len, hdr.status, hdr.flags.time_report);
|
||||
}
|
||||
fn statusUpdateMessage(msg_bytes: []u8) Allocator.Error!void {
|
||||
if (msg_bytes.len < @sizeOf(abi.StatusUpdate)) @panic("malformed StatusUpdate message");
|
||||
const msg: *const abi.StatusUpdate = @ptrCast(msg_bytes[0..@sizeOf(abi.StatusUpdate)]);
|
||||
js.updateBuildStatus(msg.new);
|
||||
}
|
||||
fn stepUpdateMessage(msg_bytes: []u8) Allocator.Error!void {
|
||||
if (msg_bytes.len < @sizeOf(abi.StepUpdate)) @panic("malformed StepUpdate message");
|
||||
const msg: *const abi.StepUpdate = @ptrCast(msg_bytes[0..@sizeOf(abi.StepUpdate)]);
|
||||
if (msg.step_idx >= step_list.len) @panic("malformed StepUpdate message");
|
||||
step_list[msg.step_idx].status = msg.bits.status;
|
||||
js.updateStepStatus(msg.step_idx);
|
||||
}
|
||||
|
||||
export fn stepName(idx: usize) String {
|
||||
return .init(step_list[idx].name);
|
||||
}
|
||||
export fn stepStatus(idx: usize) u8 {
|
||||
return @intFromEnum(step_list[idx].status);
|
||||
}
|
||||
|
||||
export fn rebuild() void {
|
||||
const msg: abi.Rebuild = .{};
|
||||
const raw: []const u8 = @ptrCast(&msg);
|
||||
js.sendWsMessage(raw.ptr, raw.len);
|
||||
}
|
||||
|
||||
/// Nanoseconds passed since a server timestamp.
|
||||
pub fn nsSince(server_timestamp: i64) i64 {
|
||||
const ms_passed = js.timestamp() - client_base_timestamp;
|
||||
const ns_passed = server_base_timestamp - server_timestamp;
|
||||
return ns_passed + ms_passed * std.time.ns_per_ms;
|
||||
}
|
||||
|
||||
pub fn fmtEscapeHtml(unescaped: []const u8) HtmlEscaper {
|
||||
return .{ .unescaped = unescaped };
|
||||
}
|
||||
const HtmlEscaper = struct {
|
||||
unescaped: []const u8,
|
||||
pub fn format(he: HtmlEscaper, w: *std.Io.Writer) !void {
|
||||
for (he.unescaped) |c| switch (c) {
|
||||
'&' => try w.writeAll("&"),
|
||||
'<' => try w.writeAll("<"),
|
||||
'>' => try w.writeAll(">"),
|
||||
'"' => try w.writeAll("""),
|
||||
'\'' => try w.writeAll("'"),
|
||||
else => try w.writeByte(c),
|
||||
};
|
||||
}
|
||||
};
|
||||
240
lib/build-web/style.css
Normal file
240
lib/build-web/style.css
Normal file
@@ -0,0 +1,240 @@
|
||||
body {
|
||||
font-family: system-ui, -apple-system, Roboto, "Segoe UI", sans-serif;
|
||||
color: #000000;
|
||||
padding: 1em 10%;
|
||||
}
|
||||
ul.no-marker {
|
||||
list-style-type: none;
|
||||
padding-left: 0;
|
||||
}
|
||||
hr {
|
||||
margin: 2em 0;
|
||||
}
|
||||
.hidden {
|
||||
display: none;
|
||||
}
|
||||
.empty-cell {
|
||||
background: #ccc;
|
||||
}
|
||||
table.time-stats > tbody > tr > th {
|
||||
text-align: left;
|
||||
}
|
||||
table.time-stats > tbody > tr > td {
|
||||
text-align: right;
|
||||
}
|
||||
details > summary {
|
||||
cursor: pointer;
|
||||
font-size: 1.5em;
|
||||
}
|
||||
.tooltip {
|
||||
text-decoration: underline;
|
||||
cursor: help;
|
||||
}
|
||||
.tooltip-content {
|
||||
border-radius: 6px;
|
||||
display: none;
|
||||
position: absolute;
|
||||
background: #fff;
|
||||
border: 1px solid black;
|
||||
max-width: 500px;
|
||||
padding: 1em;
|
||||
text-align: left;
|
||||
font-weight: normal;
|
||||
pointer-events: none;
|
||||
}
|
||||
.tooltip:hover > .tooltip-content {
|
||||
display: block;
|
||||
}
|
||||
table {
|
||||
margin: 1.0em auto 1.5em 0;
|
||||
border-collapse: collapse;
|
||||
}
|
||||
th, td {
|
||||
padding: 0.5em 1em 0.5em 1em;
|
||||
border: 1px solid;
|
||||
border-color: black;
|
||||
}
|
||||
a, button {
|
||||
color: #2A6286;
|
||||
}
|
||||
button {
|
||||
background: #eee;
|
||||
cursor: pointer;
|
||||
border: none;
|
||||
border-radius: 3px;
|
||||
padding: 0.2em 0.5em;
|
||||
}
|
||||
button.big-btn {
|
||||
font-size: 1.3em;
|
||||
}
|
||||
button.linkish {
|
||||
background: none;
|
||||
text-decoration: underline;
|
||||
padding: 0;
|
||||
}
|
||||
button:disabled {
|
||||
color: #888;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
pre {
|
||||
font-family: "Source Code Pro", monospace;
|
||||
font-size: 1em;
|
||||
background-color: #F5F5F5;
|
||||
padding: 1em;
|
||||
margin: 0;
|
||||
overflow-x: auto;
|
||||
}
|
||||
:not(pre) > code {
|
||||
white-space: break-spaces;
|
||||
}
|
||||
code {
|
||||
font-family: "Source Code Pro", monospace;
|
||||
font-size: 0.9em;
|
||||
}
|
||||
code a {
|
||||
color: #000000;
|
||||
}
|
||||
kbd {
|
||||
color: #000;
|
||||
background-color: #fafbfc;
|
||||
border-color: #d1d5da;
|
||||
border-bottom-color: #c6cbd1;
|
||||
box-shadow-color: #c6cbd1;
|
||||
display: inline-block;
|
||||
padding: 0.3em 0.2em;
|
||||
font: 1.2em monospace;
|
||||
line-height: 0.8em;
|
||||
vertical-align: middle;
|
||||
border: solid 1px;
|
||||
border-radius: 3px;
|
||||
box-shadow: inset 0 -1px 0;
|
||||
cursor: default;
|
||||
}
|
||||
.status-running { color: #181; }
|
||||
.status-idle { color: #444; }
|
||||
.step-success { color: #181; }
|
||||
.step-failure { color: #d11; }
|
||||
.step-wip::before {
|
||||
content: '';
|
||||
position: absolute;
|
||||
margin-left: -1.5em;
|
||||
width: 1em;
|
||||
text-align: center;
|
||||
animation-name: spinner;
|
||||
animation-duration: 0.5s;
|
||||
animation-iteration-count: infinite;
|
||||
animation-timing-function: step-start;
|
||||
}
|
||||
@keyframes spinner {
|
||||
0% { content: '|'; }
|
||||
25% { content: '/'; }
|
||||
50% { content: '-'; }
|
||||
75% { content: '\\'; }
|
||||
100% { content: '|'; }
|
||||
}
|
||||
|
||||
.l {
|
||||
display: inline-block;
|
||||
background: red;
|
||||
width: 1em;
|
||||
height: 1em;
|
||||
border-radius: 1em;
|
||||
}
|
||||
.c {
|
||||
background-color: green;
|
||||
}
|
||||
|
||||
.tok-kw {
|
||||
color: #333;
|
||||
font-weight: bold;
|
||||
}
|
||||
.tok-str {
|
||||
color: #d14;
|
||||
}
|
||||
.tok-builtin {
|
||||
color: #0086b3;
|
||||
}
|
||||
.tok-comment {
|
||||
color: #777;
|
||||
font-style: italic;
|
||||
}
|
||||
.tok-fn {
|
||||
color: #900;
|
||||
font-weight: bold;
|
||||
}
|
||||
.tok-null {
|
||||
color: #008080;
|
||||
}
|
||||
.tok-number {
|
||||
color: #008080;
|
||||
}
|
||||
.tok-type {
|
||||
color: #458;
|
||||
font-weight: bold;
|
||||
}
|
||||
|
||||
@media (prefers-color-scheme: dark) {
|
||||
body {
|
||||
background-color: #111;
|
||||
color: #ddd;
|
||||
}
|
||||
pre {
|
||||
background-color: #222;
|
||||
}
|
||||
a, button {
|
||||
color: #88f;
|
||||
}
|
||||
button {
|
||||
background: #333;
|
||||
}
|
||||
button:disabled {
|
||||
color: #555;
|
||||
}
|
||||
code a {
|
||||
color: #eee;
|
||||
}
|
||||
th, td {
|
||||
border-color: white;
|
||||
}
|
||||
.empty-cell {
|
||||
background: #000;
|
||||
}
|
||||
.tooltip-content {
|
||||
background: #060606;
|
||||
border-color: white;
|
||||
}
|
||||
.status-running { color: #90ee90; }
|
||||
.status-idle { color: #bbb; }
|
||||
.step-success { color: #90ee90; }
|
||||
.step-failure { color: #f66; }
|
||||
.l {
|
||||
background-color: red;
|
||||
}
|
||||
.c {
|
||||
background-color: green;
|
||||
}
|
||||
.tok-kw {
|
||||
color: #eee;
|
||||
}
|
||||
.tok-str {
|
||||
color: #2e5;
|
||||
}
|
||||
.tok-builtin {
|
||||
color: #ff894c;
|
||||
}
|
||||
.tok-comment {
|
||||
color: #aa7;
|
||||
}
|
||||
.tok-fn {
|
||||
color: #B1A0F8;
|
||||
}
|
||||
.tok-null {
|
||||
color: #ff8080;
|
||||
}
|
||||
.tok-number {
|
||||
color: #ff8080;
|
||||
}
|
||||
.tok-type {
|
||||
color: #68f;
|
||||
}
|
||||
}
|
||||
43
lib/build-web/time_report.css
Normal file
43
lib/build-web/time_report.css
Normal file
@@ -0,0 +1,43 @@
|
||||
:host > details {
|
||||
padding: 0.5em 1em;
|
||||
background: #f2f2f2;
|
||||
margin-bottom: 1.0em;
|
||||
overflow-x: scroll;
|
||||
}
|
||||
:host > details.pending {
|
||||
pointer-events: none;
|
||||
background: #fafafa;
|
||||
color: #666;
|
||||
}
|
||||
:host > details > div {
|
||||
margin: 1em 2em;
|
||||
overflow: scroll; /* we'll try to avoid overflow, but if it does happen, this makes sense */
|
||||
}
|
||||
.stats {
|
||||
font-size: 1.2em;
|
||||
}
|
||||
details.section {
|
||||
margin: 1.0em 0 0 0;
|
||||
}
|
||||
details.section > summary {
|
||||
font-weight: bold;
|
||||
}
|
||||
details.section > :not(summary) {
|
||||
margin-left: 2em;
|
||||
}
|
||||
:host > details.no-llvm .llvm-only {
|
||||
display: none;
|
||||
}
|
||||
@media (prefers-color-scheme: dark) {
|
||||
:host > details {
|
||||
background: #222;
|
||||
}
|
||||
:host > details.pending {
|
||||
background: #181818;
|
||||
color: #888;
|
||||
}
|
||||
}
|
||||
th {
|
||||
max-width: 20em; /* don't let the 'file' column get crazy long */
|
||||
overflow-wrap: anywhere; /* avoid overflow where possible */
|
||||
}
|
||||
234
lib/build-web/time_report.zig
Normal file
234
lib/build-web/time_report.zig
Normal file
@@ -0,0 +1,234 @@
|
||||
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;
|
||||
};
|
||||
|
||||
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}!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.ArrayListUnmanaged(u8) = .empty;
|
||||
defer file_table_html.deinit(gpa);
|
||||
for (slowest_files[0..@min(max_table_rows, slowest_files.len)]) |file| {
|
||||
try file_table_html.writer(gpa).print(
|
||||
\\<tr>
|
||||
\\ <th scope="row"><code>{f}</code></th>
|
||||
\\ <td>{D}</td>
|
||||
\\ <td>{D}</td>
|
||||
\\ <td>{D}</td>
|
||||
\\</tr>
|
||||
\\
|
||||
, .{
|
||||
fmtEscapeHtml(file.name),
|
||||
file.ns_sema,
|
||||
file.ns_codegen,
|
||||
file.ns_link,
|
||||
});
|
||||
}
|
||||
if (slowest_files.len > max_table_rows) {
|
||||
try file_table_html.writer(gpa).print(
|
||||
\\<tr><td colspan="4">{d} more rows omitted</td></tr>
|
||||
\\
|
||||
, .{slowest_files.len - max_table_rows});
|
||||
}
|
||||
|
||||
var decl_table_html: std.ArrayListUnmanaged(u8) = .empty;
|
||||
defer decl_table_html.deinit(gpa);
|
||||
|
||||
for (slowest_decls[0..@min(max_table_rows, slowest_decls.len)]) |decl| {
|
||||
try decl_table_html.writer(gpa).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>
|
||||
\\</tr>
|
||||
\\
|
||||
, .{
|
||||
fmtEscapeHtml(decl.file_name),
|
||||
fmtEscapeHtml(decl.name),
|
||||
decl.sema_count,
|
||||
decl.ns_sema,
|
||||
decl.ns_codegen,
|
||||
decl.ns_link,
|
||||
});
|
||||
}
|
||||
if (slowest_decls.len > max_table_rows) {
|
||||
try decl_table_html.writer(gpa).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.items.ptr,
|
||||
file_table_html.items.len,
|
||||
decl_table_html.items.ptr,
|
||||
decl_table_html.items.len,
|
||||
hdr.flags.use_llvm,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user