use std::collections::VecDeque;
use crate::Profile;
#[derive(Clone, Debug)]
pub struct Ring {
capacity: u64,
slices: VecDeque<(u64, Profile)>,
}
impl Ring {
#[must_use]
pub fn new(capacity: u64) -> Self {
Self {
capacity: capacity.max(1),
slices: VecDeque::new(),
}
}
#[must_use]
pub fn capacity(&self) -> u64 {
self.capacity
}
#[must_use]
pub fn len(&self) -> usize {
self.slices.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.slices.is_empty()
}
#[must_use]
pub fn newest(&self) -> Option<u64> {
self.slices.back().map(|(second, _)| *second)
}
pub fn add(&mut self, second: u64, thread: &str, on_cpu: bool, leaf_pc: u64, frames: &[u64]) {
let newest = self.newest();
if newest.is_some_and(|newest| second.saturating_add(self.capacity) <= newest) {
return;
}
let at = match self.slices.binary_search_by_key(&second, |(s, _)| *s) {
Ok(at) => at,
Err(at) => {
self.slices.insert(at, (second, Profile::default()));
at
}
};
if newest.is_none_or(|newest| second > newest) {
while self.slices.front().is_some_and(|(old, _)| {
*old < second && old.saturating_add(self.capacity) <= second
}) {
self.slices.pop_front();
}
}
let at = at.min(self.slices.len().saturating_sub(1));
if let Some((_, slice)) = self.slices.get_mut(at) {
let id = slice.thread_id(thread);
slice.add(id, on_cpu, leaf_pc, frames, 1);
}
}
#[must_use]
pub fn window(&self, from: u64, spec: &str, hz: u32) -> Profile {
self.between(from, u64::MAX, spec, hz)
}
fn between(&self, from: u64, to: u64, spec: &str, hz: u32) -> Profile {
let mut out = Profile::new(spec, hz);
for (second, slice) in &self.slices {
if (from..=to).contains(second) {
out.merge(slice);
}
}
out
}
#[must_use]
pub fn recent(&self, now: u64, seconds: f64, spec: &str, hz: u32) -> Profile {
let back = if seconds.is_finite() && seconds > 0.0 {
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let whole = seconds.ceil().min(self.capacity as f64) as u64;
whole.min(self.capacity - 1)
} else {
0
};
self.between(now.saturating_sub(back), now, spec, hz)
}
}
impl Default for Ring {
fn default() -> Self {
Self::new(crate::config::DEFAULT_RING_SECONDS)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn filled(capacity: u64, seconds: std::ops::Range<u64>) -> Ring {
let mut ring = Ring::new(capacity);
for second in seconds {
ring.add(second, "worker", true, 0x1000 + second, &[0x1000]);
}
ring
}
#[test]
fn keeps_the_last_capacity_seconds() {
let ring = filled(40, 0..45);
assert_eq!(ring.len(), 40);
assert_eq!(ring.newest(), Some(44));
assert_eq!(
ring.window(0, "x", 1).total(),
40,
"older seconds were dropped"
);
assert_eq!(ring.window(42, "x", 1).total(), 3);
assert_eq!(ring.window(45, "x", 1).total(), 0);
}
#[test]
fn capacity_boundaries() {
let ring = filled(1, 0..5);
assert_eq!(ring.len(), 1);
assert_eq!(ring.window(0, "x", 1).total(), 1);
assert_eq!(Ring::new(0).capacity(), 1);
let exact = filled(3, 0..3);
assert_eq!(exact.len(), 3, "exactly capacity seconds fit");
}
#[test]
fn gaps_evict_by_second_not_by_count() {
let mut ring = Ring::new(10);
ring.add(0, "t", true, 1, &[1]);
ring.add(5, "t", true, 2, &[2]);
assert_eq!(ring.len(), 2);
ring.add(15, "t", true, 3, &[3]);
assert_eq!(ring.len(), 1);
ring.add(16, "t", true, 4, &[4]);
assert_eq!(ring.window(0, "x", 1).total(), 2);
}
#[test]
fn late_samples_go_to_their_second_or_are_dropped() {
let mut ring = Ring::new(10);
ring.add(20, "t", true, 1, &[1]);
ring.add(18, "t", true, 2, &[2]);
ring.add(20, "t", true, 3, &[3]);
ring.add(10, "t", true, 4, &[4]);
assert_eq!(ring.len(), 2);
assert_eq!(ring.window(19, "x", 1).total(), 2);
assert_eq!(ring.window(18, "x", 1).total(), 3);
ring.add(11, "t", true, 5, &[5]);
assert_eq!(ring.window(0, "x", 1).total(), 4);
ring.add(u64::MAX, "t", true, 6, &[6]);
assert_eq!(ring.len(), 1);
}
#[test]
fn threads_are_kept_apart_by_name() {
let mut ring = Ring::new(4);
ring.add(0, "a", true, 1, &[1]);
ring.add(1, "b", false, 1, &[1]);
ring.add(1, "a", true, 1, &[1]);
let profile = ring.window(0, "spec", 100);
assert_eq!(profile.spec, "spec");
assert_eq!(profile.hz, 100);
assert_eq!(profile.threads(), ["a", "b"]);
assert_eq!(profile.total(), 3);
assert!(profile.folded().contains("b#wait;jit+0x1 1"));
}
#[test]
fn recent_counts_back_whole_seconds() {
let ring = filled(40, 0..45);
let total = |seconds| ring.recent(44, seconds, "x", 1).total();
assert_eq!(total(0.0), 1, "the current second only");
assert_eq!(total(0.1), 2);
assert_eq!(total(1.0), 2);
assert_eq!(total(2.5), 4);
assert_eq!(total(1e9), 40, "capped to the ring");
assert_eq!(total(f64::NAN), 1);
assert_eq!(total(-3.0), 1);
assert_eq!(total(f64::INFINITY), 1);
}
#[test]
fn recent_leaves_out_later_seconds() {
let ring = filled(40, 0..45);
let profile = ring.recent(40, 1.0, "x", 1);
assert_eq!(profile.total(), 2);
let leaves: Vec<u64> = profile.stacks().map(|stack| stack.leaf_pc).collect();
assert!(
leaves.iter().all(|&leaf| leaf <= 0x1000 + 40),
"{leaves:x?}"
);
assert_eq!(ring.recent(10, 3.0, "x", 1).total(), 4, "seconds 7 to 10");
assert_eq!(
ring.recent(2, 0.0, "x", 1).total(),
0,
"second 2 was dropped"
);
}
}