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

Use SpinLock for Histogram updates #3550

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 49 additions & 29 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ public struct MetricPoint

private MetricPointValueStorage deltaLastValue;

private SpinLock spinLock = new SpinLock(true); // Enable owner tracking

internal MetricPoint(
AggregatorStore aggregatorStore,
AggregationType aggType,
Expand Down Expand Up @@ -336,46 +338,46 @@ internal void Update(double number)
}
}

var sw = default(SpinWait);
while (true)
bool lockTaken = false;
try
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
this.spinLock.Enter(ref lockTaken);
unchecked
{
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
break;
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
}
}
finally
{
if (lockTaken)
{
this.spinLock.Exit();
}

sw.SpinOnce();
}

break;
}

case AggregationType.HistogramSumCount:
{
var sw = default(SpinWait);
while (true)
bool lockTaken = false;
try
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
this.spinLock.Enter(ref lockTaken);
unchecked
{
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
break;
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
}
}
finally
{
if (lockTaken)
{
this.spinLock.Exit();
}

sw.SpinOnce();
}

break;
Expand Down Expand Up @@ -497,8 +499,10 @@ internal void TakeSnapshot(bool outputDelta)

case AggregationType.Histogram:
{
lock (this.histogramBuckets.LockObject)
bool lockTaken = false;
try
{
this.spinLock.Enter(ref lockTaken);
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
Expand All @@ -518,14 +522,23 @@ internal void TakeSnapshot(bool outputDelta)

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
}
finally
{
if (lockTaken)
{
this.spinLock.Exit();
}
}

break;
}

case AggregationType.HistogramSumCount:
{
lock (this.histogramBuckets.LockObject)
bool lockTaken = false;
try
{
this.spinLock.Enter(ref lockTaken);
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
Expand All @@ -536,6 +549,13 @@ internal void TakeSnapshot(bool outputDelta)

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
}
finally
{
if (lockTaken)
{
this.spinLock.Exit();
}
}

break;
}
Expand Down