use super::pooled;
use crate::api::{HostBudget, HostCredit, HostMemory, host_margin};
pub const STALL_ENTER: u32 = 500;
pub const STALL_LEAVE: u32 = 100;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum HostBand {
#[default]
Plain,
Larger,
Stalled,
}
impl HostBand {
pub fn after_plan(
memory: &HostMemory,
reserved: u64,
stalled: bool,
needs: impl IntoIterator<Item = u64>,
) -> Self {
if stalled {
HostBand::Stalled
} else if short(memory, reserved, stalled, needs) {
HostBand::Larger
} else {
HostBand::Plain
}
}
pub fn is_short(self) -> bool {
!matches!(self, HostBand::Plain)
}
pub fn short(self, occupancy: Occupancy) -> bool {
self.is_short() && occupancy == Occupancy::Occupied
}
pub fn was_stalled(self) -> bool {
self == HostBand::Stalled
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum Occupancy {
#[default]
Empty,
Occupied,
}
impl Occupancy {
pub fn of(any_runner: bool) -> Self {
if any_runner {
Occupancy::Occupied
} else {
Occupancy::Empty
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct Remembered {
band: HostBand,
occupancy: Occupancy,
}
impl Remembered {
pub fn new(band: HostBand, occupancy: Occupancy) -> Self {
Self { band, occupancy }
}
pub fn short(self) -> bool {
self.band.short(self.occupancy)
}
pub fn was_stalled(self) -> bool {
self.band.was_stalled()
}
}
#[cfg(test)]
impl Remembered {
pub(crate) fn testing(short: bool, stalled: bool) -> Self {
let occupancy = Occupancy::of(short);
let band = match (short, stalled) {
(_, true) => HostBand::Stalled,
(true, false) => HostBand::Larger,
(false, false) => HostBand::Plain,
};
Self { band, occupancy }
}
}
pub fn margin(total: u64, short: bool) -> u64 {
let margin = host_margin(total);
if short {
margin.saturating_add(margin / 2)
} else {
margin
}
}
pub fn short(
memory: &HostMemory,
reserved: u64,
stalled: bool,
needs: impl IntoIterator<Item = u64>,
) -> bool {
if stalled {
return true;
}
let room = left(limit(memory, false), reserved, false);
let mut needs = needs.into_iter().peekable();
needs.peek().is_some()
&& needs.all(|needs| i64::try_from(needs).ok().is_none_or(|needs| needs > room))
}
const CREDIT_ALLOWANCE_SHARE: u64 = 4;
pub fn pooling(memory: &HostMemory, remembered: Remembered) -> pooled::Host {
let stalled = stalls(memory, remembered.was_stalled());
let margin = margin(memory.grown(), remembered.short() || stalled);
pooled::Host {
available: memory.available.saturating_add(memory.growable()),
keeps: margin.saturating_add(memory.grown() / CREDIT_ALLOWANCE_SHARE),
stalled,
}
}
pub fn stalls(memory: &HostMemory, before: bool) -> bool {
let full = memory.full.map_or(0, |full| full.avg10);
let stalling = full >= STALL_ENTER || (before && full >= STALL_LEAVE);
stalling && memory.available < margin(memory.total, false).saturating_mul(2)
}
pub fn limit(memory: &HostMemory, short: bool) -> u64 {
memory
.slice
.saturating_add(memory.available)
.saturating_add(memory.growable())
.saturating_sub(margin(memory.grown(), short))
}
pub fn budget(memory: HostMemory, count: pooled::Count, remembered: Remembered) -> HostBudget {
let stalled = stalls(&memory, remembered.was_stalled());
let limit = limit(&memory, remembered.short() || stalled);
HostBudget {
memory,
limit,
spare: u64::try_from(left(limit, count.hosted(), stalled)).unwrap_or(0),
stalled,
credit: (count.host_credit > 0).then_some(HostCredit {
bytes: count.host_credit,
used: count.used,
}),
}
}
pub fn left(limit: u64, reserved: u64, stalled: bool) -> i64 {
if stalled {
return 0;
}
let bytes = |bytes: u64| i64::try_from(bytes).unwrap_or(i64::MAX);
bytes(limit).saturating_sub(bytes(reserved))
}
pub fn first_runner(memory: &HostMemory, reserved: u64) -> u64 {
u64::try_from(left(limit(memory, false), reserved, false)).unwrap_or(0)
}
#[cfg(test)]
mod tests;