Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Written by Gil Tene of Azul Systems, and released to the public domain, as explained at http://creativecommons.org/publicdomain/zero/1.0/

Author(s):
Gil Tene
  
  
  package org.HdrHistogram;
  
Records floting point (double) values, and provides stable interval DoubleHistogram samples from live recorded data without interrupting or stalling active recording of values. Each interval histogram provided contains all value counts accumulated since the previous interval histogram was taken.

This pattern is commonly used in logging interval histogram information while recoding is ongoing.

DoubleRecorder supports concurrent recordValue(double) or recordValueWithExpectedInterval(double,double) calls. Recording calls are wait-free on architectures that support atomic increment operations, and are lock-free on architectures that do no.

 
 
 public class DoubleRecorder {
     private static AtomicLong instanceIdSequencer = new AtomicLong(1);
     private final long instanceId = .getAndIncrement();
 
     private final WriterReaderPhaser recordingPhaser = new WriterReaderPhaser();
 
Construct an auto-resizing DoubleRecorder using a precision stated as a number of significant decimal digits.

Parameters:
numberOfSignificantValueDigits Specifies the precision to use. This is the number of significant decimal digits to which the histogram will maintain value resolution and separation. Must be a non-negative integer between 0 and 5.
 
     public DoubleRecorder(final int numberOfSignificantValueDigits) {
          = new InternalConcurrentDoubleHistogram(numberOfSignificantValueDigits);
          = new InternalConcurrentDoubleHistogram(numberOfSignificantValueDigits);
     }

    
Construct a DoubleRecorder dynamic range of values to cover and a number of significant decimal digits.

Parameters:
highestToLowestValueRatio specifies the dynamic range to use (as a ratio)
numberOfSignificantValueDigits Specifies the precision to use. This is the number of significant decimal digits to which the histogram will maintain value resolution and separation. Must be a non-negative integer between 0 and 5.
 
     public DoubleRecorder(final long highestToLowestValueRatio,
                           final int numberOfSignificantValueDigits) {
                 highestToLowestValueRationumberOfSignificantValueDigits);
                 highestToLowestValueRationumberOfSignificantValueDigits);
     }

    
Record a value

Parameters:
value the value to record
Throws:
java.lang.ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue
 
     public void recordValue(double value) {
         long criticalValueAtEnter = .writerCriticalSectionEnter();
         try {
             .recordValue(value);
         } finally {
             .writerCriticalSectionExit(criticalValueAtEnter);
         }
     }

    
Record a value

To compensate for the loss of sampled values when a recorded value is larger than the expected interval between value samples, Histogram will auto-generate an additional series of decreasingly-smaller (down to the expectedIntervalBetweenValueSamples) value records.

See related notes DoubleHistogram.recordValueWithExpectedInterval(double,double) for more explanations about coordinated opmissionand expetced interval correction. *

Parameters:
value The value to record
expectedIntervalBetweenValueSamples If expectedIntervalBetweenValueSamples is larger than 0, add auto-generated value records as appropriate if value is larger than expectedIntervalBetweenValueSamples
Throws:
java.lang.ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue
 
     public void recordValueWithExpectedInterval(final double valuefinal double expectedIntervalBetweenValueSamples)
             throws ArrayIndexOutOfBoundsException {
         long criticalValueAtEnter = .writerCriticalSectionEnter();
        try {
            .recordValueWithExpectedInterval(valueexpectedIntervalBetweenValueSamples);
        } finally {
            .writerCriticalSectionExit(criticalValueAtEnter);
        }
    }

    
Get a new instance of an interval histogram, which will include a stable, consistent view of all value counts accumulated since the last interval histogram was taken.

Calling getIntervalHistogram() will reset the value counts, and start accumulating value counts for the next interval.

Returns:
a histogram containing the value counts accumulated since the last interval histogram was taken.
    public synchronized DoubleHistogram getIntervalHistogram() {
        return getIntervalHistogram(null);
    }

    
Get an interval histogram, which will include a stable, consistent view of all value counts accumulated since the last interval histogram was taken.

getIntervalHistogram(histogramToRecycle) accepts a previously returned interval histogram that can be recycled internally to avoid allocation and content copying operations, and is therefore siginificantly more efficient for repeated use than getIntervalHistogram() and getIntervalHistogramInto(). The provided histogramToRecycle must be either be null or an interval histogram returned by a previous call to getIntervalHistogram(histogramToRecycle) or getIntervalHistogram().

NOTE: The caller is responsible for not recycling the same returned interval histogram more than once. If the same interval histogram instance is recycled more than once, behavior is undefined.

Calling getIntervalHistogram(histogramToRecycle) will reset the value counts, and start accumulating value counts for the next interval

Parameters:
histogramToRecycle a previously returned interval histogram that may be recycled to avoid allocation and copy operations.
Returns:
a histogram containing the value counts accumulated since the last interval histogram was taken.
    public synchronized DoubleHistogram getIntervalHistogram(DoubleHistogram histogramToRecycle) {
        if (histogramToRecycle == null) {
            histogramToRecycle = new InternalConcurrentDoubleHistogram();
        }
        // Verify that replacement histogram can validly be used as an inactiuve histogram replacement:
        validateFitAsReplacementHistogram(histogramToRecycle);
        try {
            .readerLock();
             = (InternalConcurrentDoubleHistogramhistogramToRecycle;
            performIntervalSample();
            return ;
        } finally {
            .readerUnlock();
        }
    }

    
Place a copy of the value counts accumulated since accumulated (since the last interval histogram was taken) into targetHistogram. Calling getIntervalHistogramInto(org.HdrHistogram.DoubleHistogram)() will reset the value counts, and start accumulating value counts for the next interval.

Parameters:
targetHistogram the histogram into which the interval histogram's data should be copied
    public synchronized void getIntervalHistogramInto(DoubleHistogram targetHistogram) {
        performIntervalSample();
        .copyInto(targetHistogram);
    }

    
Reset any value counts accumulated thus far.
    public synchronized void reset() {
        // the currently inactive histogram is reset each time we flip. So flipping twice resets both:
        performIntervalSample();
        performIntervalSample();
    }
    private void performIntervalSample() {
        .reset();
        try {
            .readerLock();
            // Swap active and inactive histograms:
            final InternalConcurrentDoubleHistogram tempHistogram = ;
             = ;
             = tempHistogram;
            // Mark end time of previous interval and start time of new one:
            long now = System.currentTimeMillis();
            .setStartTimeStamp(now);
            .setEndTimeStamp(now);
            // Make sure we are not in the middle of recording a value on the previously active histogram:
            // Flip phase to make sure no recordings that were in flight pre-flip are still active:
            .flipPhase(500000L /* yield in 0.5 msec units if needed */);
        } finally {
            .readerUnlock();
        }
    }
        private final long containingInstanceId;
        private InternalConcurrentDoubleHistogram(long idint numberOfSignificantValueDigits) {
            super(numberOfSignificantValueDigits);
            this. = id;
        }
        private InternalConcurrentDoubleHistogram(long id,
                                                  long highestToLowestValueRatio,
                                                  int numberOfSignificantValueDigits) {
            super(highestToLowestValueRationumberOfSignificantValueDigits);
            this. = id;
        }
            super(source);
            this. = source.containingInstanceId;
        }
    }
    void validateFitAsReplacementHistogram(DoubleHistogram replacementHistogram) {
        boolean bad = true;
        if ((replacementHistogram instanceof InternalConcurrentDoubleHistogram)
                &&
                (((InternalConcurrentDoubleHistogramreplacementHistogram). ==
                        .)
                ) {
            bad = false;
        }
        if (bad) {
            throw new IllegalArgumentException("replacement histogram must have been obtained via a previous" +
                    "getIntervalHistogram() call from this " + this.getClass().getName() +" instance");
        }
    }
New to GrepCode? Check out our FAQ X