authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-03 16:39:08-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-15 10:48:13-07:00
log2996eb558756c697e2ccfe9691356e536c88916b
tree769ca1781323bcc5dcc612d6032934764958d786
parent80d1976db9efd21920a45890cbf368d808b166e5

std.Build.RunStep: add maxrss, duration, and cached status


5 files changed, 58 insertions(+), 11 deletions(-)

lib/std/Build/ObjCopyStep.zig+1-1
......@@ -113,7 +113,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
113113 };
114114
115115 try argv.appendSlice(&.{ full_src_path, full_dest_path });
116 _ = try step.spawnZigProcess(argv.items, prog_node);
116 _ = try step.evalZigProcess(argv.items, prog_node);
117117
118118 self.output_file.path = full_dest_path;
119119 try man.writeManifest();
lib/std/Build/Step.zig+9-1
......@@ -234,10 +234,12 @@ pub fn evalZigProcess(
234234 child.stdin_behavior = .Pipe;
235235 child.stdout_behavior = .Pipe;
236236 child.stderr_behavior = .Pipe;
237 child.request_resource_usage_statistics = true;
237238
238239 child.spawn() catch |err| return s.fail("unable to spawn {s}: {s}", .{
239240 argv[0], @errorName(err),
240241 });
242 var timer = try std.time.Timer.start();
241243
242244 var poller = std.io.poll(gpa, enum { stdout, stderr }, .{
243245 .stdout = child.stdout.?,
......@@ -301,7 +303,10 @@ pub fn evalZigProcess(
301303 sub_prog_node.?.activate();
302304 },
303305 .emit_bin_path => {
304 result = try arena.dupe(u8, body);
306 const EbpHdr = std.zig.Server.Message.EmitBinPath;
307 const ebp_hdr = @ptrCast(*align(1) const EbpHdr, body);
308 s.result_cached = ebp_hdr.flags.cache_hit;
309 result = try arena.dupe(u8, body[@sizeOf(EbpHdr)..]);
305310 },
306311 _ => {
307312 // Unrecognized message.
......@@ -323,6 +328,9 @@ pub fn evalZigProcess(
323328 const term = child.wait() catch |err| {
324329 return s.fail("unable to wait for {s}: {s}", .{ argv[0], @errorName(err) });
325330 };
331 s.result_duration_ns = timer.read();
332 s.result_peak_rss = child.resource_usage_statistics.getMaxRss() orelse 0;
333
326334 try handleChildProcessTerm(s, term, null, argv);
327335
328336 if (s.result_error_bundle.errorMessageCount() > 0) {
lib/std/zig/Server.zig+12-1
......@@ -12,7 +12,7 @@ pub const Message = struct {
1212 error_bundle,
1313 /// Body is a UTF-8 string.
1414 progress,
15 /// Body is a UTF-8 string.
15 /// Body is a EmitBinPath.
1616 emit_bin_path,
1717 _,
1818 };
......@@ -25,4 +25,15 @@ pub const Message = struct {
2525 extra_len: u32,
2626 string_bytes_len: u32,
2727 };
28
29 /// Trailing:
30 /// * the file system path the emitted binary can be found
31 pub const EmitBinPath = extern struct {
32 flags: Flags,
33
34 pub const Flags = packed struct(u8) {
35 cache_hit: bool,
36 reserved: u7 = 0,
37 };
38 };
2839};
src/Compilation.zig+3
......@@ -100,6 +100,7 @@ job_queued_compiler_rt_lib: bool = false,
100100job_queued_compiler_rt_obj: bool = false,
101101alloc_failure_occurred: bool = false,
102102formatted_panics: bool = false,
103last_update_was_cache_hit: bool = false,
103104
104105c_source_files: []const CSourceFile,
105106clang_argv: []const []const u8,
......@@ -1860,6 +1861,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
18601861 defer tracy_trace.end();
18611862
18621863 comp.clearMiscFailures();
1864 comp.last_update_was_cache_hit = false;
18631865
18641866 var man: Cache.Manifest = undefined;
18651867 defer if (comp.whole_cache_manifest != null) man.deinit();
......@@ -1887,6 +1889,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
18871889 return err;
18881890 };
18891891 if (is_hit) {
1892 comp.last_update_was_cache_hit = true;
18901893 log.debug("CacheMode.whole cache hit for {s}", .{comp.bin_file.options.root_name});
18911894 const digest = man.final();
18921895
src/main.zig+33-8
......@@ -3578,9 +3578,11 @@ fn serve(
35783578 var arena_instance = std.heap.ArenaAllocator.init(gpa);
35793579 defer arena_instance.deinit();
35803580 const arena = arena_instance.allocator();
3581 var output_path: []const u8 = undefined;
3582 try cmdTranslateC(comp, arena, &output_path);
3583 try serveStringMessage(out, .emit_bin_path, output_path);
3581 var output: TranslateCOutput = undefined;
3582 try cmdTranslateC(comp, arena, &output);
3583 try serveEmitBinPath(out, output.path, .{
3584 .flags = .{ .cache_hit = output.cache_hit },
3585 });
35843586 continue;
35853587 }
35863588
......@@ -3760,10 +3762,26 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {
37603762 } else if (comp.bin_file.options.emit) |emit| {
37613763 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
37623764 defer gpa.free(full_path);
3763 try serveStringMessage(out, .emit_bin_path, full_path);
3765 try serveEmitBinPath(out, full_path, .{
3766 .flags = .{ .cache_hit = comp.last_update_was_cache_hit },
3767 });
37643768 }
37653769}
37663770
3771fn serveEmitBinPath(
3772 out: fs.File,
3773 fs_path: []const u8,
3774 header: std.zig.Server.Message.EmitBinPath,
3775) !void {
3776 try serveMessage(out, .{
3777 .tag = .emit_bin_path,
3778 .bytes_len = @intCast(u32, fs_path.len + @sizeOf(std.zig.Server.Message.EmitBinPath)),
3779 }, &.{
3780 std.mem.asBytes(&header),
3781 fs_path,
3782 });
3783}
3784
37673785fn serveStringMessage(out: fs.File, tag: std.zig.Server.Message.Tag, s: []const u8) !void {
37683786 try serveMessage(out, .{
37693787 .tag = tag,
......@@ -4115,7 +4133,12 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
41154133 }
41164134}
41174135
4118fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8) !void {
4136const TranslateCOutput = struct {
4137 path: []const u8,
4138 cache_hit: bool,
4139};
4140
4141fn cmdTranslateC(comp: *Compilation, arena: Allocator, fancy_output: ?*TranslateCOutput) !void {
41194142 if (!build_options.have_llvm)
41204143 fatal("cannot translate-c: compiler built without LLVM extensions", .{});
41214144
......@@ -4126,14 +4149,16 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8
41264149
41274150 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();
41284151 man.want_shared_lock = false;
4129 defer if (output_path != null) man.deinit();
4152 defer man.deinit();
41304153
41314154 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
41324155 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {
41334156 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });
41344157 };
41354158
4159 if (fancy_output) |p| p.cache_hit = true;
41364160 const digest = if (try man.hit()) man.final() else digest: {
4161 if (fancy_output) |p| p.cache_hit = false;
41374162 var argv = std.ArrayList([]const u8).init(arena);
41384163 try argv.append(""); // argv[0] is program name, actual args start at [1]
41394164
......@@ -4229,11 +4254,11 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8
42294254 break :digest digest;
42304255 };
42314256
4232 if (output_path) |out_path| {
4257 if (fancy_output) |p| {
42334258 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{
42344259 "o", &digest, translated_zig_basename,
42354260 });
4236 out_path.* = full_zig_path;
4261 p.path = full_zig_path;
42374262 } else {
42384263 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });
42394264 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {