Lock-free updates for Histogram (#2951)

This commit is contained in:
Utkarsh Umesan Pillai 2022-03-01 20:23:21 -08:00 committed by GitHub
parent b6dcfe8e09
commit e7b0257605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 5 deletions

View File

@ -2,9 +2,13 @@
## Unreleased
* Make `IResourceDetector` public to allow custom implementations of resource detectors.
* Make `IResourceDetector` public to allow custom implementations of resource
detectors.
([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897))
* Perf improvement for Histogram, by implementing lock-free updates.
([2951](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2951))
## 1.2.0-rc2
Released 2022-Feb-02

View File

@ -32,6 +32,8 @@ namespace OpenTelemetry.Metrics
internal double SnapshotSum;
internal int IsCriticalSectionOccupied = 0;
internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = explicitBounds;

View File

@ -323,11 +323,23 @@ namespace OpenTelemetry.Metrics
}
}
lock (this.histogramBuckets.LockObject)
var sw = default(SpinWait);
while (true)
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
}
this.histogramBuckets.IsCriticalSectionOccupied = 0;
break;
}
sw.SpinOnce();
}
break;