use crate::features::concurrent_writes::{ConcurrentHdrHistogram, Snapshot};
use std::sync::atomic::{AtomicUsize, Ordering};
pub struct DualRecorder {
histograms: [ConcurrentHdrHistogram; 2],
active: AtomicUsize,
}
impl DualRecorder {
pub fn new(significant_digits: u32) -> Self {
Self::with_majors(significant_digits, 32)
}
pub fn with_majors(significant_digits: u32, majors: u32) -> Self {
Self {
histograms: [
ConcurrentHdrHistogram::with_majors(significant_digits, majors),
ConcurrentHdrHistogram::with_majors(significant_digits, majors),
],
active: AtomicUsize::new(0),
}
}
pub fn record(&self, value: u64) {
let idx = self.active.load(Ordering::Acquire);
self.histograms[idx].record(value);
}
pub fn get_interval_histogram(&self) -> Snapshot {
let prev = self.active.load(Ordering::Acquire);
let next = 1 - prev;
self.active.store(next, Ordering::Release);
self.histograms[prev].drain_snapshot()
}
pub fn active_index(&self) -> usize {
self.active.load(Ordering::Acquire)
}
}
#[cfg(test)]
#[path = "dual_recorder_tests.rs"]
mod tests;