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

Lock-free updates for Histogram #2951

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 UsingHistogram = 0;
utpilla marked this conversation as resolved.
Show resolved Hide resolved

internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = explicitBounds;
Expand Down
17 changes: 13 additions & 4 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,20 @@ 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.UsingHistogram, 1) == 0)
{
this.runningValue.AsLong++;
Copy link
Member

Choose a reason for hiding this comment

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

I haven't look into the underlying code, wonder if this ++ might throw (e.g. integer overflow case). If that's the case, we might need to make sure we release the spinlock.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't acquire any lock in the first place to be released. SpinWait is used to just smartly apply context switch for the thread.

Copy link
Member

Choose a reason for hiding this comment

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

one Ln335 we set this.histogramBuckets.UsingHistogram = 0, if we throw before this, all other threads would spin I guess?

Copy link
Member

@reyang reyang Feb 25, 2022

Choose a reason for hiding this comment

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

I think unchecked would solve the problem here.

Copy link
Member

Choose a reason for hiding this comment

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

this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;

this.histogramBuckets.UsingHistogram = 0;
break;
}

sw.SpinOnce();
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
}

break;
Expand Down