-
Notifications
You must be signed in to change notification settings - Fork 794
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 1 commit
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 |
---|---|---|
|
@@ -394,7 +394,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? 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. @pentp If there is a reasonable improvement in performance, I will spend time analyzing the complete metrics SDK to understand how these changes affect it. |
||
break; | ||
} | ||
|
||
|
@@ -451,7 +451,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 +510,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 +567,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 +622,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; | ||
|
@@ -636,7 +636,7 @@ internal void TakeSnapshot(bool outputDelta) | |
} | ||
else | ||
{ | ||
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong); | ||
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong); | ||
} | ||
|
||
break; | ||
|
@@ -647,7 +647,7 @@ 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; | ||
|
@@ -661,15 +661,15 @@ internal void TakeSnapshot(bool outputDelta) | |
} | ||
else | ||
{ | ||
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble); | ||
this.snapshotValue.AsDouble = Volatile.Read(ref this.runningValue.AsDouble); | ||
} | ||
|
||
break; | ||
} | ||
|
||
case AggregationType.LongGauge: | ||
{ | ||
this.snapshotValue.AsLong = Interlocked.Read(ref this.runningValue.AsLong); | ||
this.snapshotValue.AsLong = Volatile.Read(ref this.runningValue.AsLong); | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
||
// Check again if value got updated, if yes reset status. | ||
|
@@ -684,7 +684,7 @@ internal void TakeSnapshot(bool outputDelta) | |
|
||
case AggregationType.DoubleGauge: | ||
{ | ||
this.snapshotValue.AsDouble = InterlockedHelper.Read(ref this.runningValue.AsDouble); | ||
this.snapshotValue.AsDouble = Volatile.Read(ref this.runningValue.AsDouble); | ||
this.MetricPointStatus = MetricPointStatus.NoCollectPending; | ||
|
||
// Check again if value got updated, if yes reset status. | ||
|
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: