diff --git a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs
index 2bbc352817d..26c607abb7f 100644
--- a/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs
+++ b/src/OpenTelemetry.Exporter.OpenTelemetryProtocol/PersistentStorage/DirectorySizeTracker.cs
@@ -36,14 +36,14 @@ public DirectorySizeTracker(long maxSizeInBytes, string path)
/// True if space is available else false.
public bool IsSpaceAvailable(out long currentSizeInBytes)
{
- currentSizeInBytes = Interlocked.Read(ref this.directoryCurrentSizeInBytes);
+ currentSizeInBytes = Volatile.Read(ref this.directoryCurrentSizeInBytes);
return currentSizeInBytes < this.maxSizeInBytes;
}
public void RecountCurrentSize()
{
var size = CalculateFolderSize(this.path);
- Interlocked.Exchange(ref this.directoryCurrentSizeInBytes, size);
+ Volatile.Write(ref this.directoryCurrentSizeInBytes, size);
}
internal static long CalculateFolderSize(string path)
diff --git a/src/OpenTelemetry/Internal/InterlockedHelper.cs b/src/OpenTelemetry/Internal/InterlockedHelper.cs
index 98639d234a4..f2ee147081e 100644
--- a/src/OpenTelemetry/Internal/InterlockedHelper.cs
+++ b/src/OpenTelemetry/Internal/InterlockedHelper.cs
@@ -10,30 +10,9 @@ internal static class InterlockedHelper
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Add(ref double location, double value)
{
- // Note: Not calling InterlockedHelper.Read here on purpose because it
- // is too expensive for fast/happy-path. If the first attempt fails
- // we'll end up in an Interlocked.CompareExchange loop anyway.
double currentValue = Volatile.Read(ref location);
-
- var returnedValue = Interlocked.CompareExchange(ref location, currentValue + value, currentValue);
- if (returnedValue != currentValue)
- {
- AddRare(ref location, value, returnedValue);
- }
- }
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static double Read(ref double location)
- => Interlocked.CompareExchange(ref location, double.NaN, double.NaN);
-
- [MethodImpl(MethodImplOptions.NoInlining)]
- private static void AddRare(ref double location, double value, double currentValue)
- {
- var sw = default(SpinWait);
while (true)
{
- sw.SpinOnce();
-
var returnedValue = Interlocked.CompareExchange(ref location, currentValue + value, currentValue);
if (returnedValue == currentValue)
{
diff --git a/src/OpenTelemetry/Internal/SelfDiagnosticsConfigRefresher.cs b/src/OpenTelemetry/Internal/SelfDiagnosticsConfigRefresher.cs
index 3d14dc16e0b..c132e69289a 100644
--- a/src/OpenTelemetry/Internal/SelfDiagnosticsConfigRefresher.cs
+++ b/src/OpenTelemetry/Internal/SelfDiagnosticsConfigRefresher.cs
@@ -164,7 +164,7 @@ private void UpdateMemoryMappedFileFromConfiguration()
private void CloseLogFile()
{
- MemoryMappedFile? mmf = Interlocked.CompareExchange(ref this.memoryMappedFile, null, this.memoryMappedFile);
+ MemoryMappedFile? mmf = Interlocked.Exchange(ref this.memoryMappedFile, null);
if (mmf != null)
{
// Each thread has its own MemoryMappedViewStream created from the only one MemoryMappedFile.
@@ -181,10 +181,7 @@ private void CloseLogFile()
mmf.Dispose();
}
- FileStream? fs = Interlocked.CompareExchange(
- ref this.underlyingFileStreamForMemoryMappedFile,
- null,
- this.underlyingFileStreamForMemoryMappedFile);
+ FileStream? fs = Interlocked.Exchange(ref this.underlyingFileStreamForMemoryMappedFile, null);
fs?.Dispose();
}
diff --git a/src/OpenTelemetry/Logs/LogRecord.cs b/src/OpenTelemetry/Logs/LogRecord.cs
index 824e95bf0c1..ee0bbbedcd1 100644
--- a/src/OpenTelemetry/Logs/LogRecord.cs
+++ b/src/OpenTelemetry/Logs/LogRecord.cs
@@ -455,7 +455,7 @@ internal ref LogRecordData GetDataRef()
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void ResetReferenceCount()
{
- this.PoolReferenceCount = 1;
+ Volatile.Write(ref this.PoolReferenceCount, 1);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
diff --git a/src/OpenTelemetry/Metrics/AggregationType.cs b/src/OpenTelemetry/Metrics/AggregationType.cs
index 13d3a03ad5c..b4d23f4984b 100644
--- a/src/OpenTelemetry/Metrics/AggregationType.cs
+++ b/src/OpenTelemetry/Metrics/AggregationType.cs
@@ -5,11 +5,6 @@ namespace OpenTelemetry.Metrics;
internal enum AggregationType
{
- ///
- /// Invalid.
- ///
- Invalid = -1,
-
///
/// Calculate SUM from incoming delta measurements.
///
diff --git a/src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs b/src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs
index d1801afdd39..6f32e43d1d3 100644
--- a/src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs
+++ b/src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs
@@ -135,7 +135,7 @@ internal void Update(in ExemplarMeasurement measurement)
this.StoreRawTags(measurement.Tags);
}
- Interlocked.Exchange(ref this.isCriticalSectionOccupied, 0);
+ Volatile.Write(ref this.isCriticalSectionOccupied, 0);
}
internal void Reset()
@@ -168,7 +168,7 @@ internal void Collect(ref Exemplar destination, bool reset)
destination.Reset();
}
- Interlocked.Exchange(ref this.isCriticalSectionOccupied, 0);
+ Volatile.Write(ref this.isCriticalSectionOccupied, 0);
}
internal readonly void Copy(ref Exemplar destination)
diff --git a/src/OpenTelemetry/Metrics/Exemplar/SimpleFixedSizeExemplarReservoir.cs b/src/OpenTelemetry/Metrics/Exemplar/SimpleFixedSizeExemplarReservoir.cs
index d7f53e934c4..bda1bebffed 100644
--- a/src/OpenTelemetry/Metrics/Exemplar/SimpleFixedSizeExemplarReservoir.cs
+++ b/src/OpenTelemetry/Metrics/Exemplar/SimpleFixedSizeExemplarReservoir.cs
@@ -37,7 +37,7 @@ protected override void OnCollected()
// Reset internal state irrespective of temporality.
// This ensures incoming measurements have fair chance
// of making it to the reservoir.
- this.measurementState = DefaultMeasurementState;
+ Volatile.Write(ref this.measurementState, DefaultMeasurementState);
}
private void Offer(in ExemplarMeasurement measurement)
diff --git a/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs
index 8c0ad5951fc..9f4502e87d6 100644
--- a/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs
+++ b/src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs
@@ -3,6 +3,7 @@
using System.Diagnostics;
using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
using OpenTelemetry.Internal;
namespace OpenTelemetry.Metrics;
@@ -10,6 +11,7 @@ namespace OpenTelemetry.Metrics;
///
/// Represents a metric data point.
///
+[StructLayout(LayoutKind.Auto)]
public struct MetricPoint
{
// Represents the number of update threads using this MetricPoint at any given point of time.
@@ -22,8 +24,9 @@ public struct MetricPoint
private const int DefaultSimpleReservoirPoolSize = 1;
private readonly AggregatorStore aggregatorStore;
+ private readonly byte type;
- private readonly AggregationType aggType;
+ private byte status;
private MetricPointOptionalComponents? mpComponents;
@@ -48,7 +51,7 @@ internal MetricPoint(
Debug.Assert(histogramExplicitBounds != null, "Histogram explicit Bounds was null.");
Debug.Assert(!aggregatorStore!.OutputDelta || lookupData != null, "LookupData was null.");
- this.aggType = aggType;
+ this.type = (byte)aggType;
this.Tags = new ReadOnlyTagCollection(tagKeysAndValues);
this.runningValue = default;
this.snapshotValue = default;
@@ -145,11 +148,8 @@ public readonly ReadOnlyTagCollection Tags
internal MetricPointStatus MetricPointStatus
{
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- readonly get;
-
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private set;
+ readonly get => (MetricPointStatus)this.status;
+ private set => this.status = (byte)value;
}
// When the AggregatorStore is reclaiming MetricPoints, this serves the purpose of validating the a given thread is using the right
@@ -160,6 +160,10 @@ internal MetricPointStatus MetricPointStatus
internal readonly bool IsInitialized => this.aggregatorStore != null;
+#pragma warning disable SA1300 // TEMP naming supression (will rename to Type)
+ private readonly AggregationType aggType => (AggregationType)this.type;
+#pragma warning restore SA1300
+
///
/// Gets the sum long value associated with the metric point.
///
@@ -394,7 +398,7 @@ internal void Update(long number)
case AggregationType.LongSumIncomingCumulative:
case AggregationType.LongGauge:
{
- Interlocked.Exchange(ref this.runningValue.AsLong, number);
+ Volatile.Write(ref this.runningValue.AsLong, number);
break;
}
@@ -451,7 +455,7 @@ internal void UpdateWithExemplar(long number, ReadOnlySpan