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 lock for Histogram #3547

Closed
Closed
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: 0 additions & 2 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ public class HistogramBuckets

internal double SnapshotSum;

internal int IsCriticalSectionOccupied = 0;

internal HistogramBuckets(double[] explicitBounds)
{
this.ExplicitBounds = explicitBounds;
Expand Down
36 changes: 9 additions & 27 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -336,46 +336,28 @@ internal void Update(double number)
}
}

var sw = default(SpinWait);
while (true)
lock (this.histogramBuckets.LockObject)
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
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]++;
}

sw.SpinOnce();
}

break;
}

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

this.histogramBuckets.IsCriticalSectionOccupied = 0;
break;
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
}

sw.SpinOnce();
}

break;
Expand Down
39 changes: 39 additions & 0 deletions test/Benchmarks/Metrics/TestBenchmarks.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// <copyright file="TestBenchmarks.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Threading;
using BenchmarkDotNet.Attributes;

namespace Benchmarks.Metrics
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ignore this file.

{
[MemoryDiagnoser]
public class TestBenchmarks
{
private int isLocked = 1;

[Benchmark]
public void Exchange()
{
var temp = Interlocked.Exchange(ref this.isLocked, 1) == 0;
}

[Benchmark]
public void CompareExchange()
{
var temp = Interlocked.CompareExchange(ref this.isLocked, 1, 0) == 0;
}
}
}