-
Notifications
You must be signed in to change notification settings - Fork 786
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 Volatile instead of Interlocked where appropriate #6051
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,15 @@ | |
|
||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
using OpenTelemetry.Internal; | ||
|
||
namespace OpenTelemetry.Metrics; | ||
|
||
/// <summary> | ||
/// Represents a metric data point. | ||
/// </summary> | ||
[StructLayout(LayoutKind.Auto)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using auto-layout together with the field type change for the two enums below reduces the struct size from 72 bytes to 64 bytes. |
||
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 | ||
|
||
/// <summary> | ||
/// Gets the sum long value associated with the metric point. | ||
/// </summary> | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious, if this shows improvement in the metric stress tests? |
||
break; | ||
} | ||
|
||
|
@@ -451,7 +455,7 @@ internal void UpdateWithExemplar(long number, ReadOnlySpan<KeyValuePair<string, | |
case AggregationType.LongSumIncomingCumulative: | ||
case AggregationType.LongGauge: | ||
{ | ||
Interlocked.Exchange(ref this.runningValue.AsLong, number); | ||
Volatile.Write(ref this.runningValue.AsLong, number); | ||
break; | ||
} | ||
|
||
|
@@ -510,7 +514,7 @@ internal void Update(double number) | |
case AggregationType.DoubleSumIncomingCumulative: | ||
case AggregationType.DoubleGauge: | ||
{ | ||
Interlocked.Exchange(ref this.runningValue.AsDouble, number); | ||
Volatile.Write(ref this.runningValue.AsDouble, number); | ||
break; | ||
} | ||
|
||
|
@@ -567,7 +571,7 @@ internal void UpdateWithExemplar(double number, ReadOnlySpan<KeyValuePair<string | |
case AggregationType.DoubleSumIncomingCumulative: | ||
case AggregationType.DoubleGauge: | ||
{ | ||
Interlocked.Exchange(ref this.runningValue.AsDouble, number); | ||
Volatile.Write(ref this.runningValue.AsDouble, number); | ||
break; | ||
} | ||
|
||
|
@@ -622,7 +626,7 @@ internal void TakeSnapshot(bool outputDelta) | |
{ | ||
if (outputDelta) | ||
{ | ||
long initValue = Interlocked.Read(ref this.runningValue.AsLong); | ||
long initValue = Volatile.Read(ref this.runningValue.AsLong); | ||
this.snapshotValue.AsLong = initValue - this.deltaLastValue.AsLong; | ||
this.deltaLastValue.AsLong = initValue; | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
@@ -631,12 +635,12 @@ internal void TakeSnapshot(bool outputDelta) | |
// This ensures no Updates get Lost. | ||
if (initValue != Interlocked.Read(ref this.runningValue.AsLong)) | ||
{ | ||
this.MetricPointStatus = MetricPointStatus.CollectPending; | ||
Volatile.Write(ref this.status, (byte)MetricPointStatus.CollectPending); | ||
} | ||
} | ||
else | ||
{ | ||
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong); | ||
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong); | ||
} | ||
|
||
break; | ||
|
@@ -647,51 +651,38 @@ internal void TakeSnapshot(bool outputDelta) | |
{ | ||
if (outputDelta) | ||
{ | ||
double initValue = InterlockedHelper.Read(ref this.runningValue.AsDouble); | ||
double initValue = Volatile.Read(ref this.runningValue.AsDouble); | ||
this.snapshotValue.AsDouble = initValue - this.deltaLastValue.AsDouble; | ||
this.deltaLastValue.AsDouble = initValue; | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
||
// Check again if value got updated, if yes reset status. | ||
// This ensures no Updates get Lost. | ||
if (initValue != InterlockedHelper.Read(ref this.runningValue.AsDouble)) | ||
long initValueLong = BitConverter.DoubleToInt64Bits(initValue); | ||
if (initValueLong != Interlocked.Read(ref this.runningValue.AsLong)) | ||
{ | ||
this.MetricPointStatus = MetricPointStatus.CollectPending; | ||
Volatile.Write(ref this.status, (byte)MetricPointStatus.CollectPending); | ||
} | ||
} | ||
else | ||
{ | ||
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble); | ||
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong); | ||
} | ||
|
||
break; | ||
} | ||
|
||
case AggregationType.LongGauge: | ||
{ | ||
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong); | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
||
// Check again if value got updated, if yes reset status. | ||
// This ensures no Updates get Lost. | ||
if (this.snapshotValue.AsLong != Interlocked.Read(ref this.runningValue.AsLong)) | ||
{ | ||
this.MetricPointStatus = MetricPointStatus.CollectPending; | ||
} | ||
|
||
break; | ||
} | ||
|
||
case AggregationType.DoubleGauge: | ||
{ | ||
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble); | ||
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong); | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
||
// Check again if value got updated, if yes reset status. | ||
// This ensures no Updates get Lost. | ||
if (this.snapshotValue.AsDouble != InterlockedHelper.Read(ref this.runningValue.AsDouble)) | ||
if (this.snapshotValue.AsLong != Interlocked.Read(ref this.runningValue.AsLong)) | ||
{ | ||
this.MetricPointStatus = MetricPointStatus.CollectPending; | ||
Volatile.Write(ref this.status, (byte)MetricPointStatus.CollectPending); | ||
} | ||
|
||
break; | ||
|
@@ -1078,7 +1069,7 @@ private void CompleteUpdate() | |
// it had no update. | ||
// TODO: For Delta, this can be mitigated | ||
// by ignoring Zero points | ||
this.MetricPointStatus = MetricPointStatus.CollectPending; | ||
Volatile.Write(ref this.status, (byte)MetricPointStatus.CollectPending); | ||
|
||
this.CompleteUpdateWithoutMeasurement(); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to consider the memory ordering guarantees of
Volatile.Write
. WithInterlocked
methods, the read/writes would not be moved before or after a givenInterlocked
method.With volatile writes, read/writes that happen after a given
Volatile.Write
method can be moved before thatVolatile.Write
method. We need to evaluate if that affects the correctness of our code. There are some write operations that we do after releasing the locks (for exemplar and MetricPoint updates):OnCollected
for Exemplars which resets the internal measurement stateMetricStatus
toCollectPending
when updatingMetricPoint
sThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went over all uses of
Interlocked
and also checked the code flow after these lock releases. I now found a few places where the current code incorrectly relies on the preceding interlocked operation for memory ordering, for example in case ofMetricPoint.UpdateWithExemplar
the order of operations is currently:With this PR: