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

Minor metrics memory optimizations #2554

Conversation

StephanDollberg
Copy link
Contributor

The metrics subsystem is quite a heavy memory user at high metric counts.

This PR features two smaller improvements reducing memory usage by removing some redundant storage. I have a higher impact change following that internalizes labels.

Two commits (details in commit):

  • Don't store full metric_id in the metric_groups_impl
  • Store impl SP in the metric_groups_impl and not each registered_metric

The list of registered metrics that each `metric_groups_impl` stores is
represented by a `metric_id` per registered metric. `metric_id` is quite
a large struct as it contains the full name and label set. Hence, this
list causes a measurable memory overhead.

Instead just store a shared_ptr to the registration itself which we can
use to reference the `metric_id` and hence save a lot of memory per
registration (16 bytes vs. 136+ bytes).
Each registered metric stores a shared pointer to the metrics impl such
that the metrics impl stays alive while there are metrics still
registered.

This adds 16 bytes of overhead per `registered_metric`. To reduce that
we can move the shared_ptr to `metric_groups_impl` which is alive as
long as the registered metrics are alive.
@StephanDollberg
Copy link
Contributor Author

@amnonh

@mykaul mykaul requested a review from amnonh November 26, 2024 10:08
public:
metric_groups_impl() = default;
metric_groups_impl();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a leftover from some mid-code changes; the contractor still uses the default implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you are right. I had this at first when I tried initing the impl SP in the constructor directly but then had to move it because of the bug mentioned in the comment.

@amnonh
Copy link
Contributor

amnonh commented Nov 26, 2024

Overall, it's a nice optimization. It's not going to have a significant impact on memory consumption, but it's good and worth adding.
I had one nitpick
I think a more significant impact will be using some kind of String Interning (like a dictionary or shard ptr) for labels and their values, but that is a much bigger change (especially as it requires handling the lifecycle of the labels)

metric_groups_impl::~metric_groups_impl() {
for (const auto& i : _registration) {
unregister_metric(i->info().id);
}
}

metric_groups_impl& metric_groups_impl::add_metric(group_name_type name, const metric_definition& md) {
// We could just do this in the constructor but some metric groups (like the
// smp queue ones) are actually constructed on a different shard originally
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a bug. Of course, you're not required to fix it as part of the series.

@avikivity avikivity closed this in 59dbb71 Nov 26, 2024
@StephanDollberg
Copy link
Contributor Author

I think a more significant impact will be using some kind of String Interning (like a dictionary or shard ptr) for labels and their values, but that is a much bigger change (especially as it requires handling the lifecycle of the labels)

Yes, I do actually have a related patch for that as well which makes a big difference for us. I will put it up for review shortly.

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