@Override void setIntegerToDoubleValueConversionRatio(final double integerToDoubleValueConversionRatio) { try { wrp.readerLock(); inactiveCounts.doubleToIntegerValueConversionRatio = 1.0 / integerToDoubleValueConversionRatio; // switch active and inactive: AtomicLongArrayWithNormalizingOffset tmp = activeCounts; activeCounts = inactiveCounts; inactiveCounts = tmp; wrp.flipPhase(); inactiveCounts.doubleToIntegerValueConversionRatio = 1.0 / integerToDoubleValueConversionRatio; // switch active and inactive again: tmp = activeCounts; activeCounts = inactiveCounts; inactiveCounts = tmp; wrp.flipPhase(); // At this point, both active and inactive have normalizingIndexOffset safely set, // and the switch in each was done without any writers using the wrong value in flight. } finally { wrp.readerUnlock(); } super.setIntegerToDoubleValueConversionRatio(integerToDoubleValueConversionRatio); }
/** * Record a value * @param value the value to record * @throws ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue */ public void recordValue(final double value) { long criticalValueAtEnter = recordingPhaser.writerCriticalSectionEnter(); try { activeHistogram.recordValue(value); } finally { recordingPhaser.writerCriticalSectionExit(criticalValueAtEnter); } }
private void readObject(final ObjectInputStream o) throws IOException, ClassNotFoundException { o.defaultReadObject(); wrp = new WriterReaderPhaser(); }
@Override synchronized void fillBufferFromCountsArray(final ByteBuffer buffer) { try { wrp.readerLock(); super.fillBufferFromCountsArray(buffer); } finally { wrp.readerUnlock(); } }
/** * Flip a phase in the {@link WriterReaderPhaser} instance, {@link WriterReaderPhaser#flipPhase()} * can only be called while holding the {@link WriterReaderPhaser#readerLock() readerLock}. * {@link WriterReaderPhaser#flipPhase()} will return only after all writer critical sections (protected by * {@link WriterReaderPhaser#writerCriticalSectionEnter() writerCriticalSectionEnter} and * {@link WriterReaderPhaser#writerCriticalSectionExit writerCriticalSectionEnter}) that may have been * in flight when the {@link WriterReaderPhaser#flipPhase()} call were made had completed. * <p> * No actual writer critical section activity is required for {@link WriterReaderPhaser#flipPhase()} to * succeed. * <p> * However, {@link WriterReaderPhaser#flipPhase()} is lock-free with respect to calls to * {@link WriterReaderPhaser#writerCriticalSectionEnter()} and * {@link WriterReaderPhaser#writerCriticalSectionExit writerCriticalSectionExit()}. It may spin-wait * or for active writer critical section code to complete. */ public void flipPhase() { flipPhase(0); } }
@Override void setCountAtNormalizedIndex(final int index, final long value) { try { wrp.readerLock(); assert (countsArrayLength == activeCounts.length()); assert (countsArrayLength == inactiveCounts.length()); inactiveCounts.lazySet(index, value); activeCounts.lazySet(index, 0); } finally { wrp.readerUnlock(); } }
/** * Flip a phase in the {@link WriterReaderPhaser} instance, {@link WriterReaderPhaser#flipPhase()} * can only be called while holding the readerLock(). * {@link WriterReaderPhaser#flipPhase()} will return only after all writer critical sections (protected by * {@link WriterReaderPhaser#writerCriticalSectionEnter()} ()} and * {@link WriterReaderPhaser#writerCriticalSectionExit(long)} ()}) that may have been in flight when the * {@link WriterReaderPhaser#flipPhase()} call were made had completed. * <p> * No actual writer critical section activity is required for {@link WriterReaderPhaser#flipPhase()} to * succeed. * <p> * However, {@link WriterReaderPhaser#flipPhase()} is lock-free with respect to calls to * {@link WriterReaderPhaser#writerCriticalSectionEnter()} and * {@link WriterReaderPhaser#writerCriticalSectionExit(long)}. It may spin-wait for for active * writer critical section code to complete. */ public void flipPhase() { flipPhase(0); } }
private void performIntervalSample() { try { recordingPhaser.readerLock(); // Make sure we have an inactive version to flip in: if (inactiveHistogram == null) { inactiveHistogram = new InternalHistogram(activeHistogram); } inactiveHistogram.reset(); // Swap active and inactive histograms: final InternalHistogram tempHistogram = inactiveHistogram; inactiveHistogram = activeHistogram; activeHistogram = tempHistogram; // Mark end time of previous interval and start time of new one: long now = System.currentTimeMillis(); activeHistogram.setStartTimeStamp(now); inactiveHistogram.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: recordingPhaser.flipPhase(500000L /* yield in 0.5 msec units if needed */); } finally { recordingPhaser.readerUnlock(); } }
/** * Record a value in the histogram (adding to the value's current count) * * @param value The value to be recorded * @param count The number of occurrences of this value to record * @throws ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue */ public void recordValueWithCount(final double value, final long count) throws ArrayIndexOutOfBoundsException { long criticalValueAtEnter = recordingPhaser.writerCriticalSectionEnter(); try { activeHistogram.recordValueWithCount(value, count); } finally { recordingPhaser.writerCriticalSectionExit(criticalValueAtEnter); } }
@Override long getCountAtNormalizedIndex(final int index) { try { wrp.readerLock(); assert (countsArrayLength == activeCounts.length()); assert (countsArrayLength == inactiveCounts.length()); long activeCount = activeCounts.get(index); long inactiveCount = inactiveCounts.get(index); return activeCount + inactiveCount; } finally { wrp.readerUnlock(); } }
private void readObject(final ObjectInputStream o) throws IOException, ClassNotFoundException { o.defaultReadObject(); wrp = new WriterReaderPhaser(); }
private void performIntervalSample() { try { recordingPhaser.readerLock(); // Make sure we have an inactive version to flip in: if (inactiveHistogram == null) { inactiveHistogram = new InternalDoubleHistogram(activeHistogram); } inactiveHistogram.reset(); // Swap active and inactive histograms: final InternalDoubleHistogram tempHistogram = inactiveHistogram; inactiveHistogram = activeHistogram; activeHistogram = tempHistogram; // Mark end time of previous interval and start time of new one: long now = System.currentTimeMillis(); activeHistogram.setStartTimeStamp(now); inactiveHistogram.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: recordingPhaser.flipPhase(500000L /* yield in 0.5 msec units if needed */); } finally { recordingPhaser.readerUnlock(); } }
/** * Record a value * @param value the value to record * @throws ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue */ @Override public void recordValue(final long value) { long criticalValueAtEnter = recordingPhaser.writerCriticalSectionEnter(); try { activeHistogram.recordValue(value); } finally { recordingPhaser.writerCriticalSectionExit(criticalValueAtEnter); } }
@Override void shiftNormalizingIndexByOffset(final int offsetToAdd, final boolean lowestHalfBucketPopulated, final double newIntegerToDoubleValueConversionRatio) { try { wrp.readerLock(); assert (countsArrayLength == activeCounts.length()); assert (countsArrayLength == inactiveCounts.length()); int newNormalizingIndexOffset = getNormalizingIndexOffset() + offsetToAdd; setNormalizingIndexOffset(newNormalizingIndexOffset, offsetToAdd, lowestHalfBucketPopulated, newIntegerToDoubleValueConversionRatio ); } finally { wrp.readerUnlock(); } }
private void performIntervalSample() { try { recordingPhaser.readerLock(); // Make sure we have an inactive version to flip in: if (inactiveHistogram == null) { inactiveHistogram = new InternalConcurrentDoubleHistogram(activeHistogram); } inactiveHistogram.reset(); // Swap active and inactive histograms: final InternalConcurrentDoubleHistogram tempHistogram = inactiveHistogram; inactiveHistogram = activeHistogram; activeHistogram = tempHistogram; // Mark end time of previous interval and start time of new one: long now = System.currentTimeMillis(); activeHistogram.setStartTimeStamp(now); inactiveHistogram.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: recordingPhaser.flipPhase(500000L /* yield in 0.5 msec units if needed */); } finally { recordingPhaser.readerUnlock(); } }
/** * Record a value * @param value the value to record * @throws ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue */ @Override public void recordValue(final long value) throws ArrayIndexOutOfBoundsException { long criticalValueAtEnter = recordingPhaser.writerCriticalSectionEnter(); try { activeHistogram.recordValue(value); } finally { recordingPhaser.writerCriticalSectionExit(criticalValueAtEnter); } }
@Override void clearCounts() { try { wrp.readerLock(); assert (countsArrayLength == activeCounts.length()); assert (countsArrayLength == inactiveCounts.length()); for (int i = 0; i < activeCounts.length(); i++) { activeCounts.lazySet(i, 0); inactiveCounts.lazySet(i, 0); } totalCountUpdater.set(this, 0); } finally { wrp.readerUnlock(); } }
private void performIntervalSample() { try { recordingPhaser.readerLock(); recordingPhaser.flipPhase(500000L /* yield in 0.5 msec units if needed */); } finally { recordingPhaser.readerUnlock();
/** * Record a value * @param value the value to record * @throws ArrayIndexOutOfBoundsException (may throw) if value is exceeds highestTrackableValue */ @Override public void recordValue(final double value) { long criticalValueAtEnter = recordingPhaser.writerCriticalSectionEnter(); try { activeHistogram.recordValue(value); } finally { recordingPhaser.writerCriticalSectionExit(criticalValueAtEnter); } }
@Override void setCountAtIndex(final int index, final long value) { try { wrp.readerLock(); assert (countsArrayLength == activeCounts.length()); assert (countsArrayLength == inactiveCounts.length()); activeCounts.lazySet( normalizeIndex(index, activeCounts.getNormalizingIndexOffset(), activeCounts.length()), value); inactiveCounts.lazySet( normalizeIndex(index, inactiveCounts.getNormalizingIndexOffset(), inactiveCounts.length()), 0); } finally { wrp.readerUnlock(); } }