Skip to content

Commit

Permalink
Lock-free updates for Histogram (#2951)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Mar 2, 2022
1 parent b6dcfe8 commit e7b0257
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class HistogramBuckets

internal double SnapshotSum;

internal int IsCriticalSectionOccupied = 0;

internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = explicitBounds;
Expand Down
20 changes: 16 additions & 4 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,23 @@ internal void Update(double number)
}
}

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;
Expand Down

0 comments on commit e7b0257

Please sign in to comment.