use portable_atomic::{AtomicU64, Ordering};
#[derive(Clone, Copy)]
struct IntHistMetric<'a> {
buckets: &'a [AtomicU64],
sum: &'a AtomicU64,
count: &'a AtomicU64,
}
#[derive(Clone, Copy)]
pub struct IntHistSeries<'a> {
bounds: &'static [u64],
metric: Option<IntHistMetric<'a>>,
}
impl<'a> IntHistSeries<'a> {
pub(crate) fn bound(
bounds: &'static [u64],
buckets: &'a [AtomicU64],
sum: &'a AtomicU64,
count: &'a AtomicU64,
) -> Self {
Self {
bounds,
metric: Some(IntHistMetric {
buckets,
sum,
count,
}),
}
}
pub(crate) const fn sink(bounds: &'static [u64]) -> Self {
Self {
bounds,
metric: None,
}
}
#[inline]
pub fn observe(&self, v: u64) {
let Some(metric) = self.metric else {
return;
};
#[cfg(feature = "consistent-histograms")]
critical_section::with(|_cs| Self::observe_inner(self.bounds, metric, v));
#[cfg(not(feature = "consistent-histograms"))]
Self::observe_inner(self.bounds, metric, v);
}
#[inline]
fn observe_inner(bounds: &[u64], metric: IntHistMetric<'_>, v: u64) {
if let Some(i) = bounds.iter().position(|bound| v <= *bound) {
metric.buckets[i].fetch_add(1, Ordering::Relaxed);
}
metric.sum.fetch_add(v, Ordering::Relaxed);
metric.count.fetch_add(1, Ordering::Relaxed);
}
pub fn bounds(&self) -> &'static [u64] {
self.bounds
}
pub fn bucket(&self, i: usize) -> u64 {
assert!(
i < self.bounds.len(),
"embeprom: histogram bucket index out of range"
);
self.metric
.map_or(0, |metric| metric.buckets[i].load(Ordering::Relaxed))
}
pub fn count(&self) -> u64 {
self.metric
.map_or(0, |metric| metric.count.load(Ordering::Relaxed))
}
pub fn sum(&self) -> u64 {
self.metric
.map_or(0, |metric| metric.sum.load(Ordering::Relaxed))
}
}
#[cfg(feature = "float")]
#[derive(Clone, Copy)]
struct HistMetric<'a> {
buckets: &'a [AtomicU64],
sum: &'a portable_atomic::AtomicF64,
count: &'a AtomicU64,
}
#[cfg(feature = "float")]
#[derive(Clone, Copy)]
pub struct HistSeries<'a> {
bounds: &'static [f64],
metric: Option<HistMetric<'a>>,
}
#[cfg(feature = "float")]
impl<'a> HistSeries<'a> {
pub(crate) fn bound(
bounds: &'static [f64],
buckets: &'a [AtomicU64],
sum: &'a portable_atomic::AtomicF64,
count: &'a AtomicU64,
) -> Self {
Self {
bounds,
metric: Some(HistMetric {
buckets,
sum,
count,
}),
}
}
pub(crate) const fn sink(bounds: &'static [f64]) -> Self {
Self {
bounds,
metric: None,
}
}
#[inline]
pub fn observe(&self, v: f64) {
let Some(metric) = self.metric else {
return;
};
#[cfg(feature = "consistent-histograms")]
critical_section::with(|_cs| Self::observe_inner(self.bounds, metric, v));
#[cfg(not(feature = "consistent-histograms"))]
Self::observe_inner(self.bounds, metric, v);
}
#[inline]
fn observe_inner(bounds: &[f64], metric: HistMetric<'_>, v: f64) {
if let Some(i) = bounds.iter().position(|bound| v <= *bound) {
metric.buckets[i].fetch_add(1, Ordering::Relaxed);
}
metric.sum.fetch_add(v, Ordering::Relaxed);
metric.count.fetch_add(1, Ordering::Relaxed);
}
pub fn bounds(&self) -> &'static [f64] {
self.bounds
}
pub fn bucket(&self, i: usize) -> u64 {
assert!(
i < self.bounds.len(),
"embeprom: histogram bucket index out of range"
);
self.metric
.map_or(0, |metric| metric.buckets[i].load(Ordering::Relaxed))
}
pub fn count(&self) -> u64 {
self.metric
.map_or(0, |metric| metric.count.load(Ordering::Relaxed))
}
pub fn sum(&self) -> f64 {
self.metric
.map_or(0.0, |metric| metric.sum.load(Ordering::Relaxed))
}
}