From db5d0293dac1715bf585af42c0b86e668eba6803 Mon Sep 17 00:00:00 2001 From: Diego Date: Mon, 30 Nov 2015 23:50:21 -0300 Subject: ! core: Avoid updating the totalCount on our histograms and close #293 --- .../instrument/AtomicHistogramFieldsAccessor.scala | 35 ---------------------- .../scala/kamon/metric/instrument/Histogram.scala | 29 ++++-------------- .../instrument/ModifiedAtomicHistogram.scala | 31 +++++++++++++++++++ 3 files changed, 37 insertions(+), 58 deletions(-) delete mode 100644 kamon-core/src/main/scala/kamon/metric/instrument/AtomicHistogramFieldsAccessor.scala create mode 100644 kamon-core/src/main/scala/kamon/metric/instrument/ModifiedAtomicHistogram.scala diff --git a/kamon-core/src/main/scala/kamon/metric/instrument/AtomicHistogramFieldsAccessor.scala b/kamon-core/src/main/scala/kamon/metric/instrument/AtomicHistogramFieldsAccessor.scala deleted file mode 100644 index e79090a8..00000000 --- a/kamon-core/src/main/scala/kamon/metric/instrument/AtomicHistogramFieldsAccessor.scala +++ /dev/null @@ -1,35 +0,0 @@ -/* - * ========================================================================================= - * Copyright © 2013-2014 the kamon project - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - -package org.HdrHistogram - -import java.util.concurrent.atomic.{ AtomicLongArray, AtomicLongFieldUpdater } - -trait AtomicHistogramFieldsAccessor { - self: AtomicHistogram ⇒ - - def countsArray(): AtomicLongArray = self.counts - - def unitMagnitude(): Int = self.unitMagnitude - - def subBucketHalfCount(): Int = self.subBucketHalfCount - - def subBucketHalfCountMagnitude(): Int = self.subBucketHalfCountMagnitude -} - -object AtomicHistogramFieldsAccessor { - def totalCountUpdater(): AtomicLongFieldUpdater[AtomicHistogram] = AtomicHistogram.totalCountUpdater -} diff --git a/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala b/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala index 5c4c7f71..d2fb88a2 100644 --- a/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala +++ b/kamon-core/src/main/scala/kamon/metric/instrument/Histogram.scala @@ -17,9 +17,9 @@ package kamon.metric.instrument import java.nio.LongBuffer -import org.HdrHistogram.AtomicHistogramFieldsAccessor -import kamon.metric.instrument.Histogram.{ Snapshot, DynamicRange } -import org.HdrHistogram.AtomicHistogram + +import kamon.metric.instrument.Histogram.{ DynamicRange, Snapshot } +import org.HdrHistogram.ModifiedAtomicHistogram trait Histogram extends Instrument { type SnapshotType = Histogram.Snapshot @@ -54,11 +54,9 @@ object Histogram { * @param lowestDiscernibleValue * The lowest value that can be discerned (distinguished from 0) by the histogram.Must be a positive integer that * is >= 1. May be internally rounded down to nearest power of 2. - * * @param highestTrackableValue * The highest value to be tracked by the histogram. Must be a positive integer that is >= (2 * lowestDiscernibleValue). * Must not be larger than (Long.MAX_VALUE/2). - * * @param precision * The number of significant decimal digits to which the histogram will maintain value resolution and separation. * Must be a non-negative integer between 1 and 3. @@ -108,9 +106,8 @@ object Histogram { * The collect(..) operation extracts all the recorded values from the histogram and resets the counts, but still * leave it in a consistent state even in the case of concurrent modification while the snapshot is being taken. */ -class HdrHistogram(dynamicRange: DynamicRange) extends AtomicHistogram(dynamicRange.lowestDiscernibleValue, - dynamicRange.highestTrackableValue, dynamicRange.precision) with Histogram with AtomicHistogramFieldsAccessor { - import AtomicHistogramFieldsAccessor.totalCountUpdater +class HdrHistogram(dynamicRange: DynamicRange) extends ModifiedAtomicHistogram(dynamicRange.lowestDiscernibleValue, + dynamicRange.highestTrackableValue, dynamicRange.precision) with Histogram { def record(value: Long): Unit = recordValue(value) @@ -125,7 +122,7 @@ class HdrHistogram(dynamicRange: DynamicRange) extends AtomicHistogram(dynamicRa val measurementsArray = Array.ofDim[Long](buffer.limit()) buffer.get(measurementsArray, 0, measurementsArray.length) - new CompactHdrSnapshot(nrOfMeasurements, measurementsArray, unitMagnitude(), subBucketHalfCount(), subBucketHalfCountMagnitude()) + new CompactHdrSnapshot(nrOfMeasurements, measurementsArray, protectedUnitMagnitude(), protectedSubBucketHalfCount(), protectedSubBucketHalfCountMagnitude()) } def getCounts = countsArray().length() @@ -148,22 +145,8 @@ class HdrHistogram(dynamicRange: DynamicRange) extends AtomicHistogram(dynamicRa index += 1 } - - reestablishTotalCount(nrOfMeasurements) nrOfMeasurements } - - private def reestablishTotalCount(diff: Long): Unit = { - def tryUpdateTotalCount: Boolean = { - val previousTotalCount = totalCountUpdater.get(this) - val newTotalCount = previousTotalCount - diff - - totalCountUpdater.compareAndSet(this, previousTotalCount, newTotalCount) - } - - while (!tryUpdateTotalCount) {} - } - } case class CompactHdrSnapshot(val numberOfMeasurements: Long, compactRecords: Array[Long], unitMagnitude: Int, diff --git a/kamon-core/src/main/scala/kamon/metric/instrument/ModifiedAtomicHistogram.scala b/kamon-core/src/main/scala/kamon/metric/instrument/ModifiedAtomicHistogram.scala new file mode 100644 index 00000000..eb01d114 --- /dev/null +++ b/kamon-core/src/main/scala/kamon/metric/instrument/ModifiedAtomicHistogram.scala @@ -0,0 +1,31 @@ +/* + * ========================================================================================= + * Copyright © 2013-2014 the kamon project + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + * ========================================================================================= + */ + +package org.HdrHistogram + +import java.util.concurrent.atomic.AtomicLongArray + +abstract class ModifiedAtomicHistogram(low: Long, high: Long, precision: Int) + extends AtomicHistogram(low, high, precision) { self ⇒ + + override def incrementTotalCount(): Unit = {} + override def addToTotalCount(value: Long): Unit = {} + + def countsArray(): AtomicLongArray = counts + def protectedUnitMagnitude(): Int = unitMagnitude + def protectedSubBucketHalfCount(): Int = subBucketHalfCount + def protectedSubBucketHalfCountMagnitude(): Int = subBucketHalfCountMagnitude +} \ No newline at end of file -- cgit v1.2.3