Skip to content

Commit

Permalink
Tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Feb 16, 2024
1 parent b21ba8d commit ea2f149
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
16 changes: 9 additions & 7 deletions src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Runtime.CompilerServices;
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using OpenTelemetry.Internal;
#endif

using System.Diagnostics;

namespace OpenTelemetry.Metrics;

#if EXPOSE_EXPERIMENTAL_FEATURES
Expand All @@ -32,7 +34,7 @@ struct Exemplar
private int tagCount;
private KeyValuePair<string, object?>[]? tagStorage;
private MetricPointValueStorage valueStorage;
private volatile int isCriticalSectionOccupied;
private int isCriticalSectionOccupied;

/// <summary>
/// Gets the timestamp.
Expand Down Expand Up @@ -76,7 +78,7 @@ public readonly ReadOnlyFilteredTagCollection FilteredTags
internal void Update<T>(in ExemplarMeasurement<T> measurement)
where T : struct
{
if (Interlocked.CompareExchange(ref this.isCriticalSectionOccupied, 1, 0) != 0)
if (Interlocked.Exchange(ref this.isCriticalSectionOccupied, 1) != 0)
{
// Some other thread is already writing, abort.
return;
Expand Down Expand Up @@ -112,7 +114,7 @@ internal void Update<T>(in ExemplarMeasurement<T> measurement)

this.StoreRawTags(measurement.Tags);

this.isCriticalSectionOccupied = 0;
Volatile.Write(ref this.isCriticalSectionOccupied, 0);
}

internal void Reset()
Expand All @@ -122,7 +124,7 @@ internal void Reset()

internal readonly bool IsUpdated()
{
if (this.isCriticalSectionOccupied != 0)
if (Volatile.Read(ref Unsafe.AsRef(in this.isCriticalSectionOccupied)) != 0)
{
this.WaitForUpdateToCompleteRare();
return true;
Expand Down Expand Up @@ -171,6 +173,6 @@ private readonly void WaitForUpdateToCompleteRare()
{
spinWait.SpinOnce();
}
while (this.isCriticalSectionOccupied != 0);
while (Volatile.Read(ref Unsafe.AsRef(in this.isCriticalSectionOccupied)) != 0);
}
}
10 changes: 5 additions & 5 deletions src/OpenTelemetry/Metrics/Exemplar/FixedSizeExemplarReservoir.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal abstract class FixedSizeExemplarReservoir : ExemplarReservoir
{
private readonly Exemplar[] bufferA;
private readonly Exemplar[] bufferB;
private volatile Exemplar[]? activeBuffer;
private Exemplar[]? activeBuffer;

protected FixedSizeExemplarReservoir(int capacity)
{
Expand All @@ -34,11 +34,11 @@ protected FixedSizeExemplarReservoir(int capacity)
/// <returns><see cref="ReadOnlyExemplarCollection"/>.</returns>
public sealed override ReadOnlyExemplarCollection Collect()
{
var currentBuffer = this.activeBuffer;
var currentBuffer = Volatile.Read(ref this.activeBuffer);

Debug.Assert(currentBuffer != null, "currentBuffer was null");

this.activeBuffer = null;
Volatile.Write(ref this.activeBuffer, null);

var inactiveBuffer = currentBuffer == this.bufferA
? this.bufferB
Expand All @@ -54,7 +54,7 @@ public sealed override ReadOnlyExemplarCollection Collect()

this.OnCollectionCompleted();

this.activeBuffer = inactiveBuffer;
Volatile.Write(ref this.activeBuffer, inactiveBuffer);

return new(currentBuffer!);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ protected virtual void OnCollectionCompleted()
protected void UpdateExemplar<T>(int exemplarIndex, in ExemplarMeasurement<T> measurement)
where T : struct
{
var activeBuffer = this.activeBuffer;
var activeBuffer = Volatile.Read(ref this.activeBuffer);
if (activeBuffer == null)
{
// Note: This is expected to happen when we race with a collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OpenTelemetry.Metrics;
/// </remarks>
internal sealed class SimpleFixedSizeExemplarReservoir : FixedSizeExemplarReservoir
{
private volatile int measurementsSeen;
private int measurementsSeen;

public SimpleFixedSizeExemplarReservoir()
: this(Environment.ProcessorCount)
Expand All @@ -41,7 +41,7 @@ protected override void OnCollectionCompleted()
// Reset internal state irrespective of temporality.
// This ensures incoming measurements have fair chance
// of making it to the reservoir.
this.measurementsSeen = 0;
Volatile.Write(ref this.measurementsSeen, 0);
}

private void Offer<T>(in ExemplarMeasurement<T> measurement)
Expand Down

0 comments on commit ea2f149

Please sign in to comment.