use std::collections::BTreeMap;
use crate::admission::Key;
use crate::sizing::Ran;
pub const STEP: u64 = 2_000;
const FRESH: u64 = 3 * STEP;
pub const THROTTLES_UNDER: u64 = 20 << 30;
pub const OOMD_UNDER: u64 = 6_657_199_309;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct RunId {
pub run_id: u64,
pub run_attempt: u32,
pub name: String,
}
impl RunId {
pub fn of(run: &Ran) -> RunId {
RunId {
run_id: run.run_id,
run_attempt: run.run_attempt,
name: run.name.clone(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Point {
pub at: u64,
pub memory: u64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Reading {
NotYet,
Stale,
Used(u64),
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct RunTrace {
pub samples: Vec<Point>,
pub named_after: u64,
}
impl RunTrace {
pub fn at(&self, ran: u64) -> Reading {
let next = self.samples.partition_point(|point| point.at <= ran);
let Some(last) = next.checked_sub(1).map(|at| self.samples[at]) else {
return Reading::NotYet;
};
if next < self.samples.len() && ran.saturating_sub(last.at) > FRESH {
return Reading::Stale;
}
Reading::Used(last.memory)
}
fn peak(&self) -> u64 {
self.samples
.iter()
.map(|point| point.memory)
.max()
.unwrap_or(0)
}
fn heaviest(&self, lasting: u64) -> Vec<Point> {
let peak = self.peak();
let at = self.samples.iter().find(|point| point.memory == peak);
let (Some(at), Some(last)) = (at.map(|point| point.at), self.samples.last()) else {
return Vec::new();
};
if at < lasting {
return self.samples.clone();
}
let from = at
.saturating_sub(lasting / 2)
.min(last.at.saturating_sub(lasting));
self.samples
.iter()
.filter(|point| (from..=from + lasting).contains(&point.at))
.map(|point| Point {
at: point.at - from,
..*point
})
.collect()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Outside {
pub at: u64,
pub memory: u64,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Trace {
pub runs: BTreeMap<RunId, RunTrace>,
pub outside: Vec<Outside>,
pub total: u64,
pub overhead: u64,
pub guard_overhead: u64,
pub host_overhead: u64,
}
impl Trace {
pub fn outside_at(&self, at: u64) -> Option<u64> {
let next = self.outside.partition_point(|point| point.at <= at);
let point = self.outside.get(next.saturating_sub(1))?;
Some(point.memory)
}
pub fn ends(&self) -> u64 {
self.outside.last().map_or(0, |point| point.at)
}
#[must_use]
pub fn shifted(&self, by: u64) -> Trace {
let mut shifted = self.clone();
if let (Some(first), Some(last)) = (self.outside.first(), self.outside.last()) {
let span = last.at - first.at + 1;
for point in &mut shifted.outside {
point.at = first.at + (point.at - first.at + by) % span;
}
shifted.outside.sort_by_key(|point| point.at);
}
shifted
}
#[must_use]
pub fn cold(&self, jobs: &[Ran]) -> Trace {
let mut heaviest: BTreeMap<Key, &RunTrace> = BTreeMap::new();
for run in jobs {
let Some(trace) = self.runs.get(&RunId::of(run)) else {
continue;
};
let known = heaviest.entry(Key::of(run)).or_insert(trace);
if trace.peak() > known.peak() {
*known = trace;
}
}
let mut cold = self.clone();
for run in jobs {
if let (Some(trace), Some(heavy)) = (
cold.runs.get_mut(&RunId::of(run)),
heaviest.get(&Key::of(run)),
) {
let lasting = run
.finished_at
.zip(run.started_at)
.map_or(u64::MAX, |(finished, started)| {
finished.saturating_sub(started)
});
trace.samples = heavy.heaviest(lasting);
}
}
cold
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Episodes {
pub count: u64,
pub time: u64,
pub worst: u64,
open: bool,
}
impl Episodes {
pub(super) fn note(&mut self, past: Option<u64>, span: u64) {
let Some(past) = past else {
self.open = false;
return;
};
self.count += u64::from(!self.open);
self.open = true;
self.time += span;
self.worst = self.worst.max(past);
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Hazard {
pub slice_peak: u64,
pub over_mark: Episodes,
pub over_max: Episodes,
pub unfunded: Episodes,
pub under_throttling: u64,
pub under_oomd: u64,
pub in_band: u64,
pub active: u64,
pub lowest_available: Option<i64>,
pub credit_falls: u64,
pub overdrawn: u64,
pub host_credit: u128,
pub credit: u128,
pub most_credit: u64,
pub braked: u64,
pub guarded: u64,
}