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

[Metrics] Reduce CPU consumption of metrics creation #9735

Merged
merged 2 commits into from
Feb 27, 2021

Conversation

lhotari
Copy link
Member

@lhotari lhotari commented Feb 26, 2021

Motivation

String.format is consuming most CPU when generating metrics:
image
(Flamegraph from Pulsar user, using Pulsar 2.7.0)

Besides direct CPU consumption, it creates a lot of garbage which then causes more CPU being spent in GC.

Modifications

It is possible to get rid of the key creation and avoid String.format calls and String object allocation, by using a similar approach as @rdhabalia used in NamespaceStats:

static {
// create static ref for add-latency-bucket keys to avoid new object allocation on every stats call.
for (int i = 0; i < ENTRY_LATENCY_BUCKETS_USEC.length + 1; i++) {
String key;
// example of key : "<metric_key>_0.0_0.5"
if (i == 0 && ENTRY_LATENCY_BUCKETS_USEC.length > 0) {
key = String.format("%s_0.0_%1.1f",
BRK_ADD_ENTRY_LATENCY_PREFIX, ENTRY_LATENCY_BUCKETS_USEC[i] / 1000.0);
} else if (i < ENTRY_LATENCY_BUCKETS_USEC.length) {
key = String.format("%s_%1.1f_%1.1f",
BRK_ADD_ENTRY_LATENCY_PREFIX, ENTRY_LATENCY_BUCKETS_USEC[i - 1] / 1000.0,
ENTRY_LATENCY_BUCKETS_USEC[i] / 1000.0);
} else {
key = String.format("%s_OVERFLOW", BRK_ADD_ENTRY_LATENCY_PREFIX);
}
ADD_LATENCY_BUCKET_KEYS[i] = key;
}
}
.
Since there are multiple sets of buckets, there is a need for a class that handles the logic.

@lhotari lhotari force-pushed the lh-reduce-metrics-cpu-consumption branch 2 times, most recently from 859a400 to 0285382 Compare February 26, 2021 11:10
Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

awesome

@lhotari lhotari force-pushed the lh-reduce-metrics-cpu-consumption branch from 0285382 to b96c44e Compare February 26, 2021 12:40
Copy link
Member

@michaeljmarshall michaeljmarshall left a comment

Choose a reason for hiding this comment

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

Good find. LGTM.

@merlimat merlimat added the type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages label Feb 26, 2021
@merlimat merlimat added this to the 2.8.0 milestone Feb 26, 2021
@lhotari
Copy link
Member Author

lhotari commented Feb 26, 2021

/pulsarbot run-failure-checks

1 similar comment
@lhotari
Copy link
Member Author

lhotari commented Feb 26, 2021

/pulsarbot run-failure-checks

@merlimat merlimat merged commit 3ff7989 into apache:master Feb 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants