Skip to content

Commit

Permalink
feat(debug): Use std.fmt helpers to format size and duration in debug…
Browse files Browse the repository at this point in the history
… outputs
  • Loading branch information
giann committed May 8, 2024
1 parent 116d7af commit 167c37d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 46 deletions.
24 changes: 8 additions & 16 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,18 @@ fn runFile(allocator: Allocator, file_name: []const u8, args: [][:0]u8, flavor:
}

if (BuildOptions.show_perf and flavor != .Check and flavor != .Fmt) {
const parsing_ms = @as(f64, @floatFromInt(parsing_time)) / 1000000;
const codegen_ms = @as(f64, @floatFromInt(codegen_time)) / 1000000;
const running_ms = @as(f64, @floatFromInt(running_time)) / 1000000;
const gc_ms = @as(f64, @floatFromInt(gc.gc_time)) / 1000000;
const jit_ms = if (vm.jit) |jit|
@as(f64, @floatFromInt(jit.jit_time)) / 1000000
else
0;
std.debug.print(
"\u{001b}[2mParsing: {d} ms\nCodegen: {d} ms\nRun: {d} ms\nJIT: {d} ms\nGC: {d} ms\nTotal: {d} ms\nFull GC: {} | GC: {} | Max allocated: {} bytes\n\u{001b}[0m",
"\u{001b}[2mParsing: {d}\nCodegen: {d}\nRun: {d}\nJIT: {d}\nGC: {d}\nTotal: {d}\nFull GC: {} | GC: {} | Max allocated: {}\n\u{001b}[0m",
.{
parsing_ms,
codegen_ms,
running_ms,
jit_ms,
gc_ms,
@as(f64, @floatFromInt(if (!is_wasm) total_timer.read() else 0)) / 1000000,
std.fmt.fmtDuration(parsing_time),
std.fmt.fmtDuration(codegen_time),
std.fmt.fmtDuration(running_time),
std.fmt.fmtDuration(if (vm.jit) |jit| jit.jit_time else 0),
std.fmt.fmtDuration(gc.gc_time),
std.fmt.fmtDuration(if (!is_wasm) total_timer.read() else 0),
gc.full_collection_count,
gc.light_collection_count,
gc.max_allocated,
std.fmt.fmtIntSizeDec(gc.max_allocated),
},
);
}
Expand Down
43 changes: 30 additions & 13 deletions src/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,13 @@ pub const GarbageCollector = struct {
const allocated = try self.allocator.create(T);

if (BuildOptions.gc_debug) {
std.debug.print("Allocated @{} {}\n", .{ @intFromPtr(allocated), T });
std.debug.print(
"Allocated @{} {}\n",
.{
std.fmt.fmtIntSizeDec(@intFromPtr(allocated)),
T,
},
);
}

if (!is_wasm) {
Expand Down Expand Up @@ -375,7 +381,11 @@ pub const GarbageCollector = struct {
if (BuildOptions.gc_debug) {
std.debug.print(
"(from {}), collected {}, {} allocated\n",
.{ self.bytes_allocated + @sizeOf(T), @sizeOf(T), self.bytes_allocated },
.{
std.fmt.fmtIntSizeDec(self.bytes_allocated + @sizeOf(T)),
std.fmt.fmtIntSizeDec(@sizeOf(T)),
std.fmt.fmtIntSizeDec(self.bytes_allocated),
},
);
}

Expand All @@ -399,9 +409,9 @@ pub const GarbageCollector = struct {
std.debug.print(
"(from {}), collected {}, {} allocated\n",
.{
self.bytes_allocated + n,
n,
self.bytes_allocated,
std.fmt.fmtIntSizeDec(self.bytes_allocated + n),
std.fmt.fmtIntSizeDec(n),
std.fmt.fmtIntSizeDec(self.bytes_allocated),
},
);
}
Expand Down Expand Up @@ -902,11 +912,11 @@ pub const GarbageCollector = struct {
}

std.debug.print(
"\nSwept {} objects for {} bytes, now {} bytes\n",
"\nSwept {} objects for {}, now {}\n",
.{
obj_count,
@max(swept, self.bytes_allocated) - self.bytes_allocated,
self.bytes_allocated,
std.fmt.fmtIntSizeDec(@max(swept, self.bytes_allocated) - self.bytes_allocated),
std.fmt.fmtIntSizeDec(self.bytes_allocated),
},
);
}
Expand All @@ -930,7 +940,14 @@ pub const GarbageCollector = struct {
const mode: Mode = if (self.bytes_allocated > self.next_full_gc and self.last_gc != null) .Full else .Young;

if (BuildOptions.gc_debug or BuildOptions.gc_debug_light) {
std.debug.print("-- gc starts mode {}, {} bytes, {} objects\n", .{ mode, self.bytes_allocated, self.objects.len });
std.debug.print(
"-- gc starts mode {s}, {}, {} objects\n",
.{
@tagName(mode),
std.fmt.fmtIntSizeDec(self.bytes_allocated),
self.objects.len,
},
);

// var it = self.active_vms.iterator();
// dumpStack(it.next().?.key_ptr.*);
Expand Down Expand Up @@ -974,12 +991,12 @@ pub const GarbageCollector = struct {

if (BuildOptions.gc_debug or BuildOptions.gc_debug_light) {
std.debug.print(
"-- gc end, {} bytes, {} objects, next_gc {}, next_full_gc {}\n",
"-- gc end, {}, {} objects, next_gc {}, next_full_gc {}\n",
.{
self.bytes_allocated,
std.fmt.fmtIntSizeDec(self.bytes_allocated),
self.objects.len,
self.next_gc,
self.next_full_gc,
std.fmt.fmtIntSizeDec(self.next_gc),
std.fmt.fmtIntSizeDec(self.next_full_gc),
},
);
}
Expand Down
24 changes: 8 additions & 16 deletions src/repl.zig
Original file line number Diff line number Diff line change
Expand Up @@ -292,26 +292,18 @@ fn runSource(
}

if (BuildOptions.show_perf) {
const parsing_ms: f64 = @as(f64, @floatFromInt(parsing_time)) / 1000000;
const codegen_ms: f64 = @as(f64, @floatFromInt(codegen_time)) / 1000000;
const running_ms: f64 = @as(f64, @floatFromInt(running_time)) / 1000000;
const gc_ms: f64 = @as(f64, @floatFromInt(gc.gc_time)) / 1000000;
const jit_ms: f64 = if (vm.jit) |jit|
@as(f64, @floatFromInt(jit.jit_time)) / 1000000
else
0;
std.debug.print(
"\u{001b}[2mParsing: {d} ms\nCodegen: {d} ms\nRun: {d} ms\nJIT: {d} ms\nGC: {d} ms\nTotal: {d} ms\nFull GC: {} | GC: {} | Max allocated: {} bytes\n\u{001b}[0m",
"\u{001b}[2mParsing: {d}\nCodegen: {d}\nRun: {d}\nJIT: {d}\nGC: {d}\nTotal: {d}\nFull GC: {} | GC: {} | Max allocated: {} bytes\n\u{001b}[0m",
.{
parsing_ms,
codegen_ms,
running_ms,
jit_ms,
gc_ms,
@as(f64, @floatFromInt(total_timer.read())) / 1000000,
std.fmt.fmtDuration(parsing_time),
std.fmt.fmtDuration(codegen_time),
std.fmt.fmtDuration(running_time),
std.fmt.fmtDuration(if (vm.jit) |jit| jit.jit_time else 0),
std.fmt.fmtDuration(gc.gc_time),
std.fmt.fmtDuration(total_timer.read()),
gc.full_collection_count,
gc.light_collection_count,
gc.max_allocated,
std.fmt.fmtIntSizeDec(gc.max_allocated),
},
);
}
Expand Down
9 changes: 8 additions & 1 deletion src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1956,7 +1956,14 @@ pub const VM = struct {
fn OP_LIST(self: *Self, _: *CallFrame, _: u32, _: OpCode, arg: u24) void {
var list: *ObjList = self.gc.allocateObject(
ObjList,
ObjList.init(self.gc.allocator, self.readConstant(arg).obj().access(ObjTypeDef, .Type, self.gc).?),
ObjList.init(
self.gc.allocator,
self.readConstant(arg).obj().access(
ObjTypeDef,
.Type,
self.gc,
).?,
),
) catch |e| {
vmPanic(e);
unreachable;
Expand Down

0 comments on commit 167c37d

Please sign in to comment.