Skip to content
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

Merged
merged 2 commits into from
Apr 26, 2024
Merged

Conversation

sentientwaffle
Copy link
Member

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).

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).
@sentientwaffle sentientwaffle marked this pull request as ready for review April 26, 2024 15:08
@matklad
Copy link
Member

matklad commented Apr 26, 2024

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.


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;
Copy link
Member

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

Copy link
Member Author

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));
Copy link
Member

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?

@@ -163,10 +163,12 @@ const Command = struct {
}

pub fn start(arena: *std.heap.ArenaAllocator, args: *const cli.Command.Start) !void {
Copy link
Member

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.

@sentientwaffle sentientwaffle force-pushed the dj-cli-arena-rss-not-virt branch 2 times, most recently from 7f5f7b2 to 4210b3c Compare April 26, 2024 16:42
This still does not perfectly reflect RSS, since it doesn't know about pages.
Comment on lines +169 to +171
tracer.TracyAllocator("tracy").init(counting_allocator.allocator())
else
arena;
&counting_allocator;
Copy link
Member

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

Copy link
Member

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?

Copy link
Member

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!

@sentientwaffle sentientwaffle added this pull request to the merge queue Apr 26, 2024
Merged via the queue into main with commit ad017e0 Apr 26, 2024
26 checks passed
@sentientwaffle sentientwaffle deleted the dj-cli-arena-rss-not-virt branch April 26, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants