-
Notifications
You must be signed in to change notification settings - Fork 484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI: Print memory usage more accurately #1882
Conversation
More accurately print memory usage at startup. Before: ``` info(main): 0: Allocated 4255MiB in 16 regions during replica init ``` After: ``` info(main): 0: Allocated 3404MiB in 16 regions during replica init ``` To be very clear, the actual memory allocated has not changed. Before we were effectively printing the virtual memory – over-allocation by the arena allocator which will never be page-faulted in. The new number is _closer_ to RSS (though still overestimates it – arena doesn't store enough info for us to be more accurate).
Alternatively, maybe write a small allocator wrapper that tracks the total number of bytes allocated? This exact approach feels like a half-measure -- an improvement, but not a principled solution. Tracking allocated bytes directly is of course incorrect (as what you are interested in are pages), but you can get the pages by looking at the RSS anyway, and having both numbers, external and internal memory usage, should give the most info. |
src/counting_allocator.zig
Outdated
|
||
fn resize(ctx: *anyopaque, buf: []u8, buf_align: u8, new_len: usize, ret_addr: usize) bool { | ||
const self: *Self = @alignCast(@ptrCast(ctx)); | ||
self.size += new_len - buf.len; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
self.size = (self.size - buf.len) + new_len
resize can either grow or shrink
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, great point! I forgot the resize could shrink.
} | ||
|
||
fn free(ctx: *anyopaque, buf: []u8, buf_align: u8, ret_addr: usize) void { | ||
const self: *Self = @alignCast(@ptrCast(ctx)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sWe probably want to -=
here? I am no sure if some of our internal structs don't do internal reallocations?
src/tigerbeetle/main.zig
Outdated
@@ -163,10 +163,12 @@ const Command = struct { | |||
} | |||
|
|||
pub fn start(arena: *std.heap.ArenaAllocator, args: *const cli.Command.Start) !void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might change ArenaAllocator
just for Allocator
here? We needed arena to print stats, but we now print stats without arena. And I think we can drop "in N regions", as that's about the impl detail of the arena, rather than about our own code.
7f5f7b2
to
4210b3c
Compare
This still does not perfectly reflect RSS, since it doesn't know about pages.
4210b3c
to
593bec2
Compare
tracer.TracyAllocator("tracy").init(counting_allocator.allocator()) | ||
else | ||
arena; | ||
&counting_allocator; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the types are different now? One is concreet allocator, the other is allocator vtable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I mean, due to comptime it doesn't matter that the types are different, but this should break downstream code? Although tests pass, so I must be wrong. Could you double check that it still compiles with tracy?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, no, just can't read as usual!
More accurately print memory usage at startup.
Before:
After:
To be very clear, the actual memory allocated has not changed. Before we were effectively printing the virtual memory – over-allocation by the arena allocator which will never be page-faulted in. The new number is closer to RSS (though still overestimates it – arena doesn't store enough info for us to be more accurate).