-
Notifications
You must be signed in to change notification settings - Fork 598
[SDK] Replace SpinLockMutex with std::mutex in the metrics library #4416
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
Open
dbarker
wants to merge
4
commits into
open-telemetry:main
Choose a base branch
from
dbarker:sdk_swap_spinlock_for_mutex_part_2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9c8c279
[SDK] Replace SpinLockMutex with std::mutex in the metrics library
dbarker 8800812
Merge branch 'main' into sdk_swap_spinlock_for_mutex_part_2
dbarker 274b3f0
add changelog entry
dbarker ceed5d5
Merge branch 'main' into sdk_swap_spinlock_for_mutex_part_2
dbarker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,6 @@ | |
| # include <unordered_set> | ||
| # include <vector> | ||
|
|
||
| # include "opentelemetry/common/spin_lock_mutex.h" | ||
| # include "opentelemetry/sdk/common/global_log_handler.h" | ||
| # include "opentelemetry/sdk/metrics/aggregation/aggregation.h" | ||
| # include "opentelemetry/sdk/metrics/data/exemplar_data.h" | ||
|
|
@@ -66,7 +65,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, | |
| bool can_erase = false; | ||
| if (it->second.use_count() == 1) | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> g(it->second->lock_); | ||
| std::lock_guard<std::mutex> g(it->second->lock_); | ||
| can_erase = !it->second->dirty_; | ||
| } | ||
| if (can_erase) | ||
|
|
@@ -103,7 +102,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, | |
| std::unique_ptr<Aggregation> rotated; | ||
| MetricAttributes attrs_copy; | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> g(entry->lock_); | ||
| std::lock_guard<std::mutex> g(entry->lock_); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The above comments still says |
||
| if (!entry->dirty_) | ||
| { | ||
| continue; | ||
|
|
@@ -160,7 +159,7 @@ bool SyncMetricStorage::Collect(CollectorHandle *collector, | |
| // satisfy the documented invariant on dirty_. | ||
| bool entry_dirty = false; | ||
| { | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> g(it->second->lock_); | ||
| std::lock_guard<std::mutex> g(it->second->lock_); | ||
| entry_dirty = it->second->dirty_; | ||
| } | ||
| if (entry_dirty) | ||
|
|
@@ -228,7 +227,7 @@ void SyncMetricStorage::BoundEntry::RecordLong(int64_t value) noexcept | |
| "is not long"); | ||
| return; | ||
| } | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(lock_); | ||
| std::lock_guard<std::mutex> guard(lock_); | ||
| current_->Aggregate(value); | ||
| dirty_ = true; | ||
| } | ||
|
|
@@ -242,7 +241,7 @@ void SyncMetricStorage::BoundEntry::RecordDouble(double value) noexcept | |
| "is not double"); | ||
| return; | ||
| } | ||
| std::lock_guard<opentelemetry::common::SpinLockMutex> guard(lock_); | ||
| std::lock_guard<std::mutex> guard(lock_); | ||
| current_->Aggregate(value); | ||
| dirty_ = true; | ||
| } | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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've made the swap to std::mutex here also to be consistent throughout the SDK. This use case for sum aggregation could be a good use for a spinlock but testing showed it was ~10ns difference with the mutex in the uncontested case. This performance delta is not significant compared to the attribute hash map lookup.
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.
The attribute-map comparison makes sense for the unbound path, but the bound path skips that lookup. It now takes the
BoundEntrymutex and then this mutex for every sum update, so the reported 10 ns may matter more there.Could we capture before-and-after results for the existing bound and unbound counter benchmarks, including a contended case? Since #4317 originally kept this spinlock and requested benchmarks for part 2, I think we should have that evidence before changing this hot path.
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.
This could increase memory usage significantly. Measured with MSVC x64 but it should similar for Linux.
LongSumAggregationLongLastValueAggregationLongHistogramAggregationThe driver is that
sizeof(std::mutex)is 80 bytes on MSVC and 40 on glibc, whilesizeof(std::atomic<bool>)is 1. One aggregation object is allocated per time series, and the default cardinality limit is 2000. That alone is 2000 x 72 = ~144 KB extra per counter instrument.TemporalMetricStoragekeeps a per-collector stash of previously reported aggregations, so a configuration with one reader roughly doubles it, reaching near 300 KB per instrument.