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 {...@@ -113,7 +113,7 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
113 };113 };
114114
115 try argv.appendSlice(&.{ full_src_path, full_dest_path });115 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
118 self.output_file.path = full_dest_path;118 self.output_file.path = full_dest_path;
119 try man.writeManifest();119 try man.writeManifest();
lib/std/Build/Step.zig+9-1
...@@ -234,10 +234,12 @@ pub fn evalZigProcess(...@@ -234,10 +234,12 @@ pub fn evalZigProcess(
234 child.stdin_behavior = .Pipe;234 child.stdin_behavior = .Pipe;
235 child.stdout_behavior = .Pipe;235 child.stdout_behavior = .Pipe;
236 child.stderr_behavior = .Pipe;236 child.stderr_behavior = .Pipe;
237 child.request_resource_usage_statistics = true;
237238
238 child.spawn() catch |err| return s.fail("unable to spawn {s}: {s}", .{239 child.spawn() catch |err| return s.fail("unable to spawn {s}: {s}", .{
239 argv[0], @errorName(err),240 argv[0], @errorName(err),
240 });241 });
242 var timer = try std.time.Timer.start();
241243
242 var poller = std.io.poll(gpa, enum { stdout, stderr }, .{244 var poller = std.io.poll(gpa, enum { stdout, stderr }, .{
243 .stdout = child.stdout.?,245 .stdout = child.stdout.?,
...@@ -301,7 +303,10 @@ pub fn evalZigProcess(...@@ -301,7 +303,10 @@ pub fn evalZigProcess(
301 sub_prog_node.?.activate();303 sub_prog_node.?.activate();
302 },304 },
303 .emit_bin_path => {305 .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)..]);
305 },310 },
306 _ => {311 _ => {
307 // Unrecognized message.312 // Unrecognized message.
...@@ -323,6 +328,9 @@ pub fn evalZigProcess(...@@ -323,6 +328,9 @@ pub fn evalZigProcess(
323 const term = child.wait() catch |err| {328 const term = child.wait() catch |err| {
324 return s.fail("unable to wait for {s}: {s}", .{ argv[0], @errorName(err) });329 return s.fail("unable to wait for {s}: {s}", .{ argv[0], @errorName(err) });
325 };330 };
331 s.result_duration_ns = timer.read();
332 s.result_peak_rss = child.resource_usage_statistics.getMaxRss() orelse 0;
333
326 try handleChildProcessTerm(s, term, null, argv);334 try handleChildProcessTerm(s, term, null, argv);
327335
328 if (s.result_error_bundle.errorMessageCount() > 0) {336 if (s.result_error_bundle.errorMessageCount() > 0) {
lib/std/zig/Server.zig+12-1
...@@ -12,7 +12,7 @@ pub const Message = struct {...@@ -12,7 +12,7 @@ pub const Message = struct {
12 error_bundle,12 error_bundle,
13 /// Body is a UTF-8 string.13 /// Body is a UTF-8 string.
14 progress,14 progress,
15 /// Body is a UTF-8 string.15 /// Body is a EmitBinPath.
16 emit_bin_path,16 emit_bin_path,
17 _,17 _,
18 };18 };
...@@ -25,4 +25,15 @@ pub const Message = struct {...@@ -25,4 +25,15 @@ pub const Message = struct {
25 extra_len: u32,25 extra_len: u32,
26 string_bytes_len: u32,26 string_bytes_len: u32,
27 };27 };
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 };
28};39};
src/Compilation.zig+3
...@@ -100,6 +100,7 @@ job_queued_compiler_rt_lib: bool = false,...@@ -100,6 +100,7 @@ job_queued_compiler_rt_lib: bool = false,
100job_queued_compiler_rt_obj: bool = false,100job_queued_compiler_rt_obj: bool = false,
101alloc_failure_occurred: bool = false,101alloc_failure_occurred: bool = false,
102formatted_panics: bool = false,102formatted_panics: bool = false,
103last_update_was_cache_hit: bool = false,
103104
104c_source_files: []const CSourceFile,105c_source_files: []const CSourceFile,
105clang_argv: []const []const u8,106clang_argv: []const []const u8,
...@@ -1860,6 +1861,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void...@@ -1860,6 +1861,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
1860 defer tracy_trace.end();1861 defer tracy_trace.end();
18611862
1862 comp.clearMiscFailures();1863 comp.clearMiscFailures();
1864 comp.last_update_was_cache_hit = false;
18631865
1864 var man: Cache.Manifest = undefined;1866 var man: Cache.Manifest = undefined;
1865 defer if (comp.whole_cache_manifest != null) man.deinit();1867 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...@@ -1887,6 +1889,7 @@ pub fn update(comp: *Compilation, main_progress_node: *std.Progress.Node) !void
1887 return err;1889 return err;
1888 };1890 };
1889 if (is_hit) {1891 if (is_hit) {
1892 comp.last_update_was_cache_hit = true;
1890 log.debug("CacheMode.whole cache hit for {s}", .{comp.bin_file.options.root_name});1893 log.debug("CacheMode.whole cache hit for {s}", .{comp.bin_file.options.root_name});
1891 const digest = man.final();1894 const digest = man.final();
18921895
src/main.zig+33-8
...@@ -3578,9 +3578,11 @@ fn serve(...@@ -3578,9 +3578,11 @@ fn serve(
3578 var arena_instance = std.heap.ArenaAllocator.init(gpa);3578 var arena_instance = std.heap.ArenaAllocator.init(gpa);
3579 defer arena_instance.deinit();3579 defer arena_instance.deinit();
3580 const arena = arena_instance.allocator();3580 const arena = arena_instance.allocator();
3581 var output_path: []const u8 = undefined;3581 var output: TranslateCOutput = undefined;
3582 try cmdTranslateC(comp, arena, &output_path);3582 try cmdTranslateC(comp, arena, &output);
3583 try serveStringMessage(out, .emit_bin_path, output_path);3583 try serveEmitBinPath(out, output.path, .{
3584 .flags = .{ .cache_hit = output.cache_hit },
3585 });
3584 continue;3586 continue;
3585 }3587 }
35863588
...@@ -3760,10 +3762,26 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {...@@ -3760,10 +3762,26 @@ fn serveUpdateResults(out: fs.File, comp: *Compilation) !void {
3760 } else if (comp.bin_file.options.emit) |emit| {3762 } else if (comp.bin_file.options.emit) |emit| {
3761 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});3763 const full_path = try emit.directory.join(gpa, &.{emit.sub_path});
3762 defer gpa.free(full_path);3764 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 });
3764 }3768 }
3765}3769}
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
3767fn serveStringMessage(out: fs.File, tag: std.zig.Server.Message.Tag, s: []const u8) !void {3785fn serveStringMessage(out: fs.File, tag: std.zig.Server.Message.Tag, s: []const u8) !void {
3768 try serveMessage(out, .{3786 try serveMessage(out, .{
3769 .tag = tag,3787 .tag = tag,
...@@ -4115,7 +4133,12 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void...@@ -4115,7 +4133,12 @@ fn updateModule(gpa: Allocator, comp: *Compilation, hook: AfterUpdateHook) !void
4115 }4133 }
4116}4134}
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 {
4119 if (!build_options.have_llvm)4142 if (!build_options.have_llvm)
4120 fatal("cannot translate-c: compiler built without LLVM extensions", .{});4143 fatal("cannot translate-c: compiler built without LLVM extensions", .{});
41214144
...@@ -4126,14 +4149,16 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8...@@ -4126,14 +4149,16 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8
41264149
4127 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();4150 var man: Cache.Manifest = comp.obtainCObjectCacheManifest();
4128 man.want_shared_lock = false;4151 man.want_shared_lock = false;
4129 defer if (output_path != null) man.deinit();4152 defer man.deinit();
41304153
4131 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects4154 man.hash.add(@as(u16, 0xb945)); // Random number to distinguish translate-c from compiling C objects
4132 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {4155 Compilation.cache_helpers.hashCSource(&man, c_source_file) catch |err| {
4133 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });4156 fatal("unable to process '{s}': {s}", .{ c_source_file.src_path, @errorName(err) });
4134 };4157 };
41354158
4159 if (fancy_output) |p| p.cache_hit = true;
4136 const digest = if (try man.hit()) man.final() else digest: {4160 const digest = if (try man.hit()) man.final() else digest: {
4161 if (fancy_output) |p| p.cache_hit = false;
4137 var argv = std.ArrayList([]const u8).init(arena);4162 var argv = std.ArrayList([]const u8).init(arena);
4138 try argv.append(""); // argv[0] is program name, actual args start at [1]4163 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...@@ -4229,11 +4254,11 @@ fn cmdTranslateC(comp: *Compilation, arena: Allocator, output_path: ?*[]const u8
4229 break :digest digest;4254 break :digest digest;
4230 };4255 };
42314256
4232 if (output_path) |out_path| {4257 if (fancy_output) |p| {
4233 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{4258 const full_zig_path = try comp.local_cache_directory.join(arena, &[_][]const u8{
4234 "o", &digest, translated_zig_basename,4259 "o", &digest, translated_zig_basename,
4235 });4260 });
4236 out_path.* = full_zig_path;4261 p.path = full_zig_path;
4237 } else {4262 } else {
4238 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });4263 const out_zig_path = try fs.path.join(arena, &[_][]const u8{ "o", &digest, translated_zig_basename });
4239 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {4264 const zig_file = comp.local_cache_directory.handle.openFile(out_zig_path, .{}) catch |err| {