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

Finer concurrency in 'MetricType.observe' / 'MetricType.collect' #59

Merged
merged 3 commits into from
Aug 24, 2021

Conversation

avolokhov
Copy link
Contributor

@avolokhov avolokhov commented Aug 23, 2021

Every metric type has a lock that's taken for the whole duration of consuming a metric value, and also for the whole duration of observe. This PR puts as much computations out of critical section as we can afford, which results in a significant performance boost.
With these changes, Lock.withLock takes only 25% of PromHistogram.observe, of which half is locking/unlocking and the other half is collection.filter.
Reducing the scope of locking helps bringing lock.withLock to 50% of PromHistogram.observe. Changing subHistograms from array to map, reduces it further to 25%

Checklist

  • [ + ] The provided tests still run.
  • [ N/A ] I've created new tests where needed.
  • [ N/A ] I've updated the documentation if necessary.

Motivation and Context

This PR brings concurrency optimisations that result in better emission latencies.

Description

Every MetricType's lock is now only held during access to corresponding state in a thread safe manner, all computations are moved outside critical section.
subHistograms is now a Map instead of array.

@avolokhov avolokhov changed the title Finer concurrency Finer concurrency in 'MetricType.observe' / 'MetricType.collect' Aug 23, 2021
return histogram
} else {
let newHistogram = PromHistogram<T, U>(histogram.name, histogram.help, labels, Buckets(histogram.upperBounds), self)
histogram.subHistograms.append(newHistogram)
guard let prometheus = prometheus else { fatalError("Lingering Histogram") }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm can this actually ever happen or just annoyance because that weak there that shoulnd't really manifest in reality?

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, this is a weak reference thing. I think reference to Prometheus is redundant, I'll try to track down its origins and potentially remove if indeed unnecessary.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah i see the error just moved around, it was there before.

Copy link
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

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

Nice this looks very good, thanks for measuring and the PR Anton!

return histogram
} else {
let newHistogram = PromHistogram<T, U>(histogram.name, histogram.help, labels, Buckets(histogram.upperBounds), self)
histogram.subHistograms.append(newHistogram)
guard let prometheus = prometheus else { fatalError("Lingering Histogram") }
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah i see the error just moved around, it was there before.

subHistogram.labels.le = ""
let (buckets, subHistograms, labels) = self.lock.withLock {
(self.buckets, self.subHistograms, self.labels)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@ktoso ktoso added this to the V 1.0.0 Alpha 15 milestone Aug 23, 2021
Copy link
Collaborator

@MrLotU MrLotU left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for the PR!

@MrLotU MrLotU merged commit b31a6e6 into swift-server:master Aug 24, 2021
@avolokhov avolokhov mentioned this pull request Sep 1, 2021
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