Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/browser/mimalloc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,45 @@ pub fn destroy() void {
heap = null;
}

pub fn getRSS() i64 {
if (@import("builtin").mode != .Debug) {
// just don't trust my implementation, plus a caller might not know
// that this requires parsing some unstructured data
@compileError("Only available in debug builds");
}
var buf: [4096]u8 = undefined;
var fba = std.heap.FixedBufferAllocator.init(&buf);
var writer = std.Io.Writer.Allocating.init(fba.allocator());

c.mi_stats_print_out(struct {
fn print(msg: [*c]const u8, data: ?*anyopaque) callconv(.c) void {
const w: *std.Io.Writer = @ptrCast(@alignCast(data.?));
w.writeAll(std.mem.span(msg)) catch |err| {
std.debug.print("Failed to write mimalloc data: {}", .{err});
};
}
}.print, &writer.writer);

const data = writer.written();
const index = std.mem.indexOf(u8, data, "rss: ") orelse return -1;
const sep = std.mem.indexOfScalarPos(u8, data, index + 5, ' ') orelse return -2;
const value = std.fmt.parseFloat(f64, data[index+5..sep]) catch return -3;
const unit = data[sep+1..];
if (std.mem.startsWith(u8, unit, "KiB,")) {
return @as(i64, @intFromFloat(value)) * 1024;
}

if (std.mem.startsWith(u8, unit, "MiB,")) {
return @as(i64, @intFromFloat(value)) * 1024 * 1024;
}

if (std.mem.startsWith(u8, unit, "GiB,")) {
return @as(i64, @intFromFloat(value)) * 1024 * 1024 * 1024;
}

return -4;
}

pub export fn m_alloc(size: usize) callconv(.c) ?*anyopaque {
std.debug.assert(heap != null);
return c.mi_heap_malloc(heap.?, size);
Expand Down
6 changes: 4 additions & 2 deletions src/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ pub const std_options = std.Options{
};

pub var js_runner_duration: usize = 0;
pub var v8_peak_memory: usize = 0;
pub var libdom_memory: i64 = 0;
pub var tracking_allocator: Allocator = undefined;

pub fn main() !void {
Expand Down Expand Up @@ -194,13 +196,13 @@ pub fn main() !void {
.duration = js_runner_duration,
.alloc_nb = 0,
.realloc_nb = 0,
.alloc_size = 0,
.alloc_size = libdom_memory,
} },
.{ .name = "v8", .bench = .{
.duration = js_runner_duration,
.alloc_nb = 0,
.realloc_nb = 0,
.alloc_size = 0,
.alloc_size = v8_peak_memory,
} },
.{ .name = "main", .bench = .{
.duration = js_runner_duration,
Expand Down
2 changes: 2 additions & 0 deletions src/testing.zig
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,8 @@ pub fn setup() !void {
test_session = try test_browser.newSession();
}
pub fn shutdown() void {
@import("root").v8_peak_memory = test_browser.env.isolate.getHeapStatistics().total_physical_size;
@import("root").libdom_memory = @import("browser/mimalloc.zig").getRSS();
test_browser.deinit();
test_app.deinit();
}
Expand Down