mirror of
https://github.com/ziglang/zig.git
synced 2025-12-06 06:13:07 +00:00
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.
214 lines
7.0 KiB
Zig
214 lines
7.0 KiB
Zig
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),
|
|
};
|
|
}
|
|
};
|