From 1dec4818237567ecd6912e8a3e925c21393150cc Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Fri, 25 Feb 2022 14:26:43 -0800 Subject: [PATCH 1/5] Lock-free updates for Histogram --- src/OpenTelemetry/Metrics/HistogramBuckets.cs | 2 ++ src/OpenTelemetry/Metrics/MetricPoint.cs | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/OpenTelemetry/Metrics/HistogramBuckets.cs b/src/OpenTelemetry/Metrics/HistogramBuckets.cs index a68033d4300..3245e82e7ab 100644 --- a/src/OpenTelemetry/Metrics/HistogramBuckets.cs +++ b/src/OpenTelemetry/Metrics/HistogramBuckets.cs @@ -32,6 +32,8 @@ public class HistogramBuckets internal double SnapshotSum; + internal int UsingHistogram = 0; + internal HistogramBuckets(double[] explicitBounds) { this.ExplicitBounds = explicitBounds; diff --git a/src/OpenTelemetry/Metrics/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint.cs index 4d8ce31036e..0f9774207b2 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint.cs @@ -323,11 +323,20 @@ internal void Update(double number) } } - lock (this.histogramBuckets.LockObject) + var sw = default(SpinWait); + while (true) { - this.runningValue.AsLong++; - this.histogramBuckets.RunningSum += number; - this.histogramBuckets.RunningBucketCounts[i]++; + if (Interlocked.Exchange(ref this.histogramBuckets.UsingHistogram, 1) == 0) + { + this.runningValue.AsLong++; + this.histogramBuckets.RunningSum += number; + this.histogramBuckets.RunningBucketCounts[i]++; + + this.histogramBuckets.UsingHistogram = 0; + break; + } + + sw.SpinOnce(); } break; From ad8c345c5952183fd67a3487f9b20f2adca7acf3 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Fri, 25 Feb 2022 16:57:53 -0800 Subject: [PATCH 2/5] Add unchecked --- src/OpenTelemetry/Metrics/MetricPoint.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/OpenTelemetry/Metrics/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint.cs index 0f9774207b2..f80bcaa6cac 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint.cs @@ -328,9 +328,12 @@ internal void Update(double number) { if (Interlocked.Exchange(ref this.histogramBuckets.UsingHistogram, 1) == 0) { - this.runningValue.AsLong++; - this.histogramBuckets.RunningSum += number; - this.histogramBuckets.RunningBucketCounts[i]++; + unchecked + { + this.runningValue.AsLong++; + this.histogramBuckets.RunningSum += number; + this.histogramBuckets.RunningBucketCounts[i]++; + } this.histogramBuckets.UsingHistogram = 0; break; From c680c49e61d65c7058f70c1507a4fc0418433276 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Tue, 1 Mar 2022 16:36:13 -0800 Subject: [PATCH 3/5] Address PR comments --- src/OpenTelemetry/CHANGELOG.md | 6 +++++- src/OpenTelemetry/Metrics/HistogramBuckets.cs | 2 +- src/OpenTelemetry/Metrics/MetricPoint.cs | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index 4b8511ce1ed..fa7cbb3f99a 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,9 +2,13 @@ ## Unreleased -* Make `IResourceDetector` public to allow custom implementations of resource detectors. +* Make `IResourceDetector` public to allow custom implementations of resource + detectors. ([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897)) +* Implement lock-free updates for Histogram + ([2951](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2951)) + ## 1.2.0-rc2 Released 2022-Feb-02 diff --git a/src/OpenTelemetry/Metrics/HistogramBuckets.cs b/src/OpenTelemetry/Metrics/HistogramBuckets.cs index 3245e82e7ab..71e1b52eb21 100644 --- a/src/OpenTelemetry/Metrics/HistogramBuckets.cs +++ b/src/OpenTelemetry/Metrics/HistogramBuckets.cs @@ -32,7 +32,7 @@ public class HistogramBuckets internal double SnapshotSum; - internal int UsingHistogram = 0; + internal int IsCriticalSectionOccupied = 0; internal HistogramBuckets(double[] explicitBounds) { diff --git a/src/OpenTelemetry/Metrics/MetricPoint.cs b/src/OpenTelemetry/Metrics/MetricPoint.cs index f80bcaa6cac..f27219af55c 100644 --- a/src/OpenTelemetry/Metrics/MetricPoint.cs +++ b/src/OpenTelemetry/Metrics/MetricPoint.cs @@ -326,7 +326,7 @@ internal void Update(double number) var sw = default(SpinWait); while (true) { - if (Interlocked.Exchange(ref this.histogramBuckets.UsingHistogram, 1) == 0) + if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0) { unchecked { @@ -335,7 +335,7 @@ internal void Update(double number) this.histogramBuckets.RunningBucketCounts[i]++; } - this.histogramBuckets.UsingHistogram = 0; + this.histogramBuckets.IsCriticalSectionOccupied = 0; break; } From 8467fa10941b1e671790c6bc15e21c652b4bc5aa Mon Sep 17 00:00:00 2001 From: Cijo Thomas Date: Tue, 1 Mar 2022 18:13:47 -0800 Subject: [PATCH 4/5] Update src/OpenTelemetry/CHANGELOG.md --- src/OpenTelemetry/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index fa7cbb3f99a..d48b6f7883d 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -6,7 +6,7 @@ detectors. ([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897)) -* Implement lock-free updates for Histogram +* Perf improvement for Histogram updates, by implementing lock-free updates. ([2951](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2951)) ## 1.2.0-rc2 From 5e9a98df22c007f1db2c80f2e1914917766d1385 Mon Sep 17 00:00:00 2001 From: Utkarsh Umesan Pillai Date: Tue, 1 Mar 2022 18:41:30 -0800 Subject: [PATCH 5/5] Update src/OpenTelemetry/CHANGELOG.md Co-authored-by: Cijo Thomas --- src/OpenTelemetry/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index d48b6f7883d..ef6dcfff080 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -6,7 +6,7 @@ detectors. ([2897](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2897)) -* Perf improvement for Histogram updates, by implementing lock-free updates. +* Perf improvement for Histogram, by implementing lock-free updates. ([2951](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2951)) ## 1.2.0-rc2