use crate::headroom::is_reclaim_candidate;
use crate::history::History;
use crate::model::{ForecastTarget, GroupKind, OomKiller, PressureLevel, Snapshot, ThermalPressure};
use crate::throttle::{is_busy, speed_label, unit_factor, BUSY_PCT, THROTTLED_BELOW};
use crate::units::{format_bytes_short, format_duration};
use serde::{Deserialize, Serialize};
pub const SWAP_STORM_BYTES_PER_MIN: u64 = 100 * 1024 * 1024;
pub const PSI_SOME_PCT: f64 = 20.0;
pub const LOW_BATTERY_PCT: f64 = 30.0;
pub const CPU_SATURATED_PCT: f64 = 90.0;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, PartialOrd, Ord)]
#[serde(rename_all = "snake_case")]
pub enum CauseKind {
OomImminent,
SwapStorm,
MemoryPressure,
ThermalThrottle,
LowPowerMode,
LowBattery,
GpuThrottle,
CpuSaturated,
}
impl CauseKind {
pub fn is_throttle_related(self) -> bool {
matches!(
self,
CauseKind::ThermalThrottle
| CauseKind::LowPowerMode
| CauseKind::LowBattery
| CauseKind::GpuThrottle
)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Cause {
pub kind: CauseKind,
pub score: f64,
pub title: String,
pub evidence: Vec<String>,
pub fix: Option<String>,
}
fn thermal_label(p: ThermalPressure) -> &'static str {
match p {
ThermalPressure::Nominal => "nominal",
ThermalPressure::Moderate => "moderate",
ThermalPressure::Heavy => "heavy",
ThermalPressure::Trapping => "trapping",
ThermalPressure::Sleeping => "sleeping",
}
}
fn killer_label(k: OomKiller) -> &'static str {
match k {
OomKiller::Kernel => "the kernel OOM killer",
OomKiller::SystemdOomd => "systemd-oomd",
OomKiller::Earlyoom => "earlyoom",
OomKiller::Jetsam => "jetsam",
}
}
fn plural(n: usize, one: &str, many: &str) -> String {
if n == 1 {
format!("1 {one}")
} else {
format!("{n} {many}")
}
}
pub fn under_load(s: &Snapshot) -> bool {
s.thermal.throttle_factor.value.is_some()
|| s.thermal.clusters.iter().any(is_busy)
|| s.accelerators
.iter()
.any(|a| a.util_pct.value.map(|u| u >= BUSY_PCT).unwrap_or(false))
|| s.cpu.total_pct.value.map(|c| c >= BUSY_PCT).unwrap_or(false)
}
pub fn reclaim_fix(s: &Snapshot) -> Option<String> {
let cands: Vec<_> = s
.groups
.iter()
.filter(|g| is_reclaim_candidate(g))
.filter_map(|g| Some((g, g.reclaim_gain.value.filter(|v| *v > 0)?)))
.collect();
if cands.is_empty() {
return None;
}
let total: u64 = cands.iter().map(|(_, v)| *v).sum();
let orphans = cands.iter().filter(|(g, _)| g.orphan).count();
let daemons = cands
.iter()
.filter(|(g, _)| !g.orphan && g.kind == GroupKind::BuildDaemon)
.count();
let idle_daemons = cands
.iter()
.filter(|(g, _)| !g.orphan && g.kind == GroupKind::BuildDaemon && g.idle)
.count();
let models = cands
.iter()
.filter(|(g, _)| !g.orphan && g.kind == GroupKind::ModelServer)
.count();
let mut parts = Vec::new();
if daemons > 0 {
parts.push(if idle_daemons == daemons {
plural(daemons, "idle build daemon", "idle build daemons")
} else {
plural(daemons, "build daemon", "build daemons")
});
}
if models > 0 {
parts.push(plural(models, "idle model server", "idle model servers"));
}
if orphans > 0 {
parts.push(plural(orphans, "orphan", "orphans"));
}
Some(format!(
"stop {} (≈{})",
parts.join(" + "),
format_bytes_short(total)
))
}
fn largest_line(s: &Snapshot) -> Option<String> {
let mut gs: Vec<_> = s
.groups
.iter()
.filter_map(|g| Some((g, g.totals.footprint.value?)))
.filter(|(_, v)| *v > 0)
.collect();
gs.sort_by(|a, b| b.1.cmp(&a.1).then_with(|| a.0.id.cmp(&b.0.id)));
let top: Vec<String> = gs
.iter()
.take(3)
.map(|(g, v)| {
format!(
"{} {}{}",
g.label,
format_bytes_short(*v),
if g.lower_bound { "+" } else { "" }
)
})
.collect();
(!top.is_empty()).then(|| format!("largest: {}", top.join(", ")))
}
fn swap_growth_s(history: &History) -> Option<(u64, u64)> {
let pts: Vec<(u64, u64)> = history
.iter()
.filter_map(|p| Some((p.t_ms, p.swap_used?)))
.collect();
let (&(t_last, v_last), rest) = pts.split_last()?;
let mut run = vec![(t_last, v_last)];
let mut next_v = v_last;
for &(t, v) in rest.iter().rev() {
if v > next_v.saturating_add(next_v / 100) {
break;
}
run.push((t, v));
next_v = v;
}
let floor = run.iter().map(|p| p.1).min().unwrap_or(v_last);
let noise = v_last.saturating_sub(floor) / 100;
let start = run
.iter()
.find(|p| p.1 <= floor.saturating_add(noise))
.copied()
.unwrap_or((t_last, v_last));
let dur = t_last.saturating_sub(start.0) / 1000;
let grown = v_last.saturating_sub(start.1);
(dur >= 60 && grown > 0).then_some((dur, grown))
}
fn title_repeats(title: &str, evidence: &str) -> bool {
let norm = |x: &str| {
x.to_lowercase()
.split_whitespace()
.filter(|w| *w != "is")
.collect::<Vec<_>>()
.join(" ")
};
let (t, e) = (norm(title), norm(evidence));
!e.is_empty() && t.contains(&e)
}
pub fn explain(s: &Snapshot, history: &History) -> Vec<Cause> {
let mut out = Vec::new();
let mem = &s.memory;
let reclaim = reclaim_fix(s);
let loaded = under_load(s);
if let Some(f) = &s.oom.forecast {
let eta = format_duration(f.eta_s);
let title = match f.target {
ForecastTarget::SwapExhaustion => format!("Swap full in ~{eta} at this rate"),
ForecastTarget::AvailableExhaustion => format!("Out of memory in ~{eta} at this rate"),
ForecastTarget::KillerThreshold => format!(
"{} acts in ~{eta} at this rate",
f.killer.map(killer_label).unwrap_or("the OOM killer")
),
};
let has_rate = f.rate_per_min.is_finite() && f.rate_per_min.abs() >= 1.0;
let rate = format_bytes_short(f.rate_per_min.abs() as u64);
let trend = match f.target {
ForecastTarget::SwapExhaustion if has_rate => format!("swap used +{rate}/min"),
ForecastTarget::SwapExhaustion => "swap used rising".to_string(),
ForecastTarget::AvailableExhaustion if has_rate => format!("available −{rate}/min"),
ForecastTarget::AvailableExhaustion => "available falling".to_string(),
ForecastTarget::KillerThreshold if has_rate => format!("{rate}/min toward the threshold"),
ForecastTarget::KillerThreshold => "rising steadily toward the threshold".to_string(),
};
let mut evidence = vec![format!(
"{trend} over {} (R² {:.2})",
format_duration(f.window_s),
f.confidence
)];
if let Some(v) = &s.oom.likely_victim {
evidence.push(format!("likely victim: {} ({})", v.name, v.reason));
}
out.push(Cause {
kind: CauseKind::OomImminent,
score: 1.0,
title,
evidence,
fix: reclaim.clone(),
});
}
let swap_in = mem.swap_in_per_min.value.unwrap_or(0);
let swap_out = mem.swap_out_per_min.value.unwrap_or(0);
let traffic = swap_in.saturating_add(swap_out);
let rates_known = mem.swap_in_per_min.value.is_some() || mem.swap_out_per_min.value.is_some();
let growth = swap_growth_s(history);
let growth_rate = growth
.map(|(d, grown)| grown.saturating_mul(60) / d.max(1))
.unwrap_or(0);
if !rates_known && growth_rate >= SWAP_STORM_BYTES_PER_MIN {
let (d, grown) = growth.unwrap_or_default();
let mut ev = vec![format!(
"swap used +{} in {} (≈{}/min; swap in/out rates unavailable)",
format_bytes_short(grown),
format_duration(d),
format_bytes_short(growth_rate)
)];
if let (Some(u), Some(t)) = (mem.swap_used.value, mem.swap_total.value) {
ev.push(format!(
"swap {} / {}",
format_bytes_short(u),
format_bytes_short(t)
));
}
ev.extend(largest_line(s));
let gib = growth_rate as f64 / (1024.0 * 1024.0 * 1024.0);
out.push(Cause {
kind: CauseKind::SwapStorm,
score: (0.45 + gib * 0.4 + (d as f64 / 600.0).min(0.1)).clamp(0.45, 0.95),
title: "Swapping heavily".into(),
evidence: ev,
fix: reclaim.clone(),
});
}
if traffic >= SWAP_STORM_BYTES_PER_MIN {
let dur = growth
.map(|(d, _)| format!(" for {}", format_duration(d)))
.unwrap_or_default();
let mut ev = vec![format!(
"swap in +{}/min, out +{}/min{dur}",
format_bytes_short(swap_in),
format_bytes_short(swap_out)
)];
if let Some((d, grown)) = growth {
ev.push(format!(
"swap used +{} in {}",
format_bytes_short(grown),
format_duration(d)
));
}
if let (Some(u), Some(t)) = (mem.swap_used.value, mem.swap_total.value) {
ev.push(format!(
"swap {} / {}",
format_bytes_short(u),
format_bytes_short(t)
));
}
ev.extend(largest_line(s));
let gib = traffic as f64 / (1024.0 * 1024.0 * 1024.0);
let sustained = growth.map(|(d, _)| (d as f64 / 600.0).min(0.1)).unwrap_or(0.0);
out.push(Cause {
kind: CauseKind::SwapStorm,
score: (0.45 + gib * 0.4 + sustained).clamp(0.45, 0.95),
title: "Swapping heavily".into(),
evidence: ev,
fix: reclaim.clone(),
});
}
let pressure = mem.pressure.value.unwrap_or_default();
let psi = mem.psi.value.map(|p| p.some_avg10).unwrap_or(0.0);
let psi_full = mem.psi.value.map(|p| p.full_avg10).unwrap_or(0.0);
if pressure >= PressureLevel::Warn || psi > PSI_SOME_PCT {
let mut ev = Vec::new();
if pressure >= PressureLevel::Warn {
ev.push(format!(
"memory pressure {}",
if pressure == PressureLevel::Critical {
"critical"
} else {
"warn"
}
));
}
if let Some(level) = mem.memorystatus_level.value {
ev.push(format!("memorystatus level {level}% available"));
}
if mem.psi.value.is_some() && psi > 0.0 {
ev.push(format!("memory PSI some {psi:.0}%, full {psi_full:.0}% (10 s)"));
}
if let Some(a) = mem.available.value {
ev.push(format!("available {}", format_bytes_short(a)));
}
if let (Some(u), Some(t)) = (mem.swap_used.value, mem.swap_total.value) {
ev.push(format!(
"swap {} / {}",
format_bytes_short(u),
format_bytes_short(t)
));
}
ev.extend(largest_line(s));
out.push(Cause {
kind: CauseKind::MemoryPressure,
score: if pressure == PressureLevel::Critical {
0.9
} else {
(0.6 + (psi / 250.0).min(0.25) + (psi_full / 500.0).min(0.05)).min(0.9)
},
title: if pressure == PressureLevel::Critical {
"Critical memory pressure".into()
} else {
"Memory pressure".into()
},
evidence: ev,
fix: reclaim.clone(),
});
}
let t = &s.thermal;
let on_battery = t.on_battery.value == Some(true);
let battery = t.battery_pct.value;
if loaded {
let factor = t.throttle_factor.value;
let tp = t.pressure.value;
let trip = t.trip_point_hit.value == Some(true);
let hot = tp.map(|p| p >= ThermalPressure::Heavy).unwrap_or(false) || trip;
let slow = factor.map(|f| f < THROTTLED_BELOW).unwrap_or(false);
let lpm = t.low_power_mode.value == Some(true);
let warm = tp.map(|p| p >= ThermalPressure::Moderate).unwrap_or(false) || trip;
if hot || (slow && (warm || !lpm)) {
let mut ev = Vec::new();
if let Some(f) = factor {
ev.push(speed_label(f));
}
let with_pressure = tp
.filter(|p| *p >= ThermalPressure::Moderate)
.map(|p| format!(" with thermal pressure {}", thermal_label(p)))
.unwrap_or_default();
for u in &t.clusters {
if let Some(uf) = unit_factor(u).filter(|f| *f < THROTTLED_BELOW) {
ev.push(format!(
"{} at {:.0}% of max clock ({:.0}% busy){with_pressure}",
u.name,
uf * 100.0,
u.active_pct
));
}
}
if let Some(p) = tp {
ev.push(format!("thermal pressure {}", thermal_label(p)));
}
if trip {
ev.push("thermal trip point hit".into());
}
if let Some(hottest) = t
.temps
.iter()
.filter(|x| x.celsius.is_finite())
.max_by(|a, b| a.celsius.total_cmp(&b.celsius))
{
ev.push(format!(
"hottest sensor {} {:.0}°C",
hottest.name, hottest.celsius
));
}
if let Some(w) = t.package_power_w.value {
ev.push(format!("package power {w:.1} W"));
}
if on_battery {
ev.push(match battery {
Some(b) => format!("on battery at {b:.0}%"),
None => "on battery".into(),
});
}
let base = factor
.map(|f| (0.35 + (1.0 - f) * 0.6).clamp(0.3, 0.85))
.unwrap_or(0.5);
let bump = if hot { 0.05 } else { 0.0 };
out.push(Cause {
kind: CauseKind::ThermalThrottle,
score: (base + bump).min(0.9),
title: match (factor, warm) {
(Some(f), true) if slow => format!("Thermal throttling — {}", speed_label(f)),
(_, true) => "Thermal throttling".into(),
(Some(f), false) => format!("Clocks reduced — {}", speed_label(f)),
(None, false) => "Clocks reduced".into(),
},
evidence: ev,
fix: Some(match (warm, on_battery) {
(true, true) => "plug in and pause heavy work until it cools".into(),
(true, false) => "improve airflow or pause heavy work until it cools".into(),
(false, true) => "plug in or pause heavy work".into(),
(false, false) => "pause heavy work or check the power profile".into(),
}),
});
}
if lpm {
let mut ev = vec!["Low Power Mode on".to_string()];
if let Some(f) = factor {
ev.push(speed_label(f));
}
ev.extend(t.clusters.iter().filter_map(|u| {
let uf = unit_factor(u).filter(|f| *f < THROTTLED_BELOW)?;
Some(format!(
"{} at {:.0}% of max clock ({:.0}% busy)",
u.name,
uf * 100.0,
u.active_pct
))
}));
let score = match (slow, warm) {
(true, false) => factor
.map(|f| (0.4 + (1.0 - f) * 0.6).clamp(0.6, 0.85))
.unwrap_or(0.6),
(true, true) => 0.6,
_ => 0.5,
};
out.push(Cause {
kind: CauseKind::LowPowerMode,
score,
title: "Low Power Mode is on".into(),
evidence: ev,
fix: Some("turn off Low Power Mode (System Settings → Battery)".into()),
});
}
if on_battery {
if let Some(pct) = battery.filter(|p| *p < LOW_BATTERY_PCT) {
let mut ev = vec![format!("on battery at {pct:.0}%")];
if let Some(w) = t.adapter_watts.value.filter(|w| *w > 0.0) {
ev.push(format!("adapter {w:.0} W"));
}
out.push(Cause {
kind: CauseKind::LowBattery,
score: if slow { 0.45 } else { 0.35 },
title: "On battery, low charge".into(),
evidence: ev,
fix: Some("plug in".into()),
});
}
}
for a in &s.accelerators {
let busy = a.util_pct.value.map(|u| u >= BUSY_PCT).unwrap_or(false);
if !busy {
continue;
}
if let (Some(c), Some(m)) = (a.clock_mhz.value, a.max_clock_mhz.value) {
if m > 0.0 && c / m < THROTTLED_BELOW {
let ratio = (c / m).clamp(0.0, 1.0);
let with_pressure = tp
.filter(|p| *p >= ThermalPressure::Moderate)
.map(|p| format!(" with thermal pressure {}", thermal_label(p)))
.unwrap_or_default();
let mut ev = vec![format!(
"{} at {:.0}% of max clock{with_pressure}",
a.name,
ratio * 100.0
)];
if let Some(temp) = a.temp_c.value {
ev.push(format!("{} at {temp:.0}°C", a.name));
}
if let Some(w) = a.power_w.value {
ev.push(format!("{} drawing {w:.0} W", a.name));
}
ev.extend(a.throttle_reasons.iter().map(|r| format!("throttle reason: {r}")));
out.push(Cause {
kind: CauseKind::GpuThrottle,
score: (1.0 - ratio).clamp(0.3, 0.85),
title: format!("{} is throttled", if a.name.is_empty() { "GPU" } else { &a.name }),
evidence: ev,
fix: None,
});
}
}
}
}
if let Some(cpu) = s.cpu.total_pct.value.filter(|c| *c > CPU_SATURATED_PCT) {
let mut ev = vec![format!("CPU {cpu:.0}% of all cores")];
let mut top: Vec<_> = s
.groups
.iter()
.filter_map(|g| Some((g, g.totals.cpu_pct.value?)))
.filter(|(_, c)| *c >= 10.0)
.collect();
top.sort_by(|a, b| b.1.total_cmp(&a.1).then_with(|| a.0.id.cmp(&b.0.id)));
let names: Vec<String> = top
.iter()
.take(3)
.map(|(g, c)| format!("{} {c:.0}%", g.label))
.collect();
if !names.is_empty() {
ev.push(format!("busiest: {}", names.join(", ")));
}
if let Some(l) = s.cpu.load_avg_1.value {
ev.push(format!("load average {l:.1}"));
}
out.push(Cause {
kind: CauseKind::CpuSaturated,
score: 0.4,
title: "CPU saturated".into(),
evidence: ev,
fix: None,
});
}
for c in &mut out {
c.score = (c.score.clamp(0.0, 1.0) * 1000.0).round() / 1000.0;
let title = c.title.clone();
c.evidence.retain(|e| !title_repeats(&title, e));
}
out.sort_by(|a, b| b.score.total_cmp(&a.score).then_with(|| a.kind.cmp(&b.kind)));
out
}
#[cfg(test)]
mod tests {
use super::*;
use crate::model::{ClusterFreq, Measured};
fn swap_history(points: &[(u64, u64)]) -> History {
let mut h = History::default();
for &(t_s, used) in points {
h.push(crate::history::HistoryPoint {
t_ms: t_s * 1000,
swap_used: Some(used),
swap_total: Some(16 << 30),
..Default::default()
});
}
h
}
#[test]
fn flat_lead_in_is_not_swap_growth() {
const MIB: u64 = 1 << 20;
let pts: Vec<(u64, u64)> = (0..=120u64)
.map(|i| {
let t = i * 5;
(t, 2048 * MIB + t.saturating_sub(480) * 5 * MIB)
})
.collect();
let h = swap_history(&pts);
let (dur, grown) = swap_growth_s(&h).expect("growth");
assert_eq!(dur, 120, "the 8 flat minutes are not growth");
assert_eq!(grown, 600 * MIB);
let s = Snapshot::default();
let causes = explain(&s, &h);
let storm = causes
.iter()
.find(|c| c.kind == CauseKind::SwapStorm)
.expect("swap storm");
assert!(storm.evidence[0].contains("in 2m"), "{:?}", storm.evidence);
let mut s = Snapshot::default();
s.memory.swap_in_per_min = Measured::exact(200 * MIB, "t");
s.memory.swap_out_per_min = Measured::exact(300 * MIB, "t");
let causes = explain(&s, &h);
let storm = causes.iter().find(|c| c.kind == CauseKind::SwapStorm).unwrap();
assert!(storm.evidence[0].ends_with("for 2m"), "{:?}", storm.evidence);
let flat: Vec<(u64, u64)> = (0..=60u64).map(|i| (i * 5, 2048 * MIB)).collect();
assert_eq!(swap_growth_s(&swap_history(&flat)), None);
let steady: Vec<(u64, u64)> = (0..=60u64).map(|i| (i * 5, 2048 * MIB + i * MIB)).collect();
assert_eq!(swap_growth_s(&swap_history(&steady)), Some((300, 60 * MIB)));
}
#[test]
fn forecast_without_a_byte_rate_never_prints_zero_per_minute() {
use crate::model::Forecast;
let mut s = Snapshot::default();
s.oom.forecast = Some(Forecast {
target: ForecastTarget::KillerThreshold,
killer: Some(OomKiller::SystemdOomd),
eta_s: 400,
confidence: 0.93,
rate_per_min: 0.0,
window_s: 180,
});
let causes = explain(&s, &History::default());
let c = causes.iter().find(|c| c.kind == CauseKind::OomImminent).unwrap();
assert_eq!(
c.evidence[0],
"rising steadily toward the threshold over 3m (R² 0.93)"
);
assert!(c.evidence.iter().all(|e| !e.contains("0B")), "{:?}", c.evidence);
s.oom.forecast.as_mut().unwrap().rate_per_min = 250.0 * 1024.0 * 1024.0;
let causes = explain(&s, &History::default());
assert_eq!(
causes[0].evidence[0],
"250M/min toward the threshold over 3m (R² 0.93)"
);
}
#[test]
fn idle_machine_reports_nothing_throttle_related() {
let mut s = Snapshot::default();
s.thermal.clusters = vec![ClusterFreq {
name: "p".into(),
cur_mhz: 600.0,
max_mhz: 4000.0,
active_pct: 2.0,
}];
s.thermal.throttle_factor = crate::throttle::throttle_factor(&s.thermal.clusters);
s.thermal.low_power_mode = Measured::exact(true, "t");
s.thermal.pressure = Measured::exact(ThermalPressure::Heavy, "t");
s.thermal.on_battery = Measured::exact(true, "t");
s.thermal.battery_pct = Measured::exact(12.0, "t");
assert!(explain(&s, &History::default()).is_empty());
}
#[test]
fn low_power_mode_is_not_called_heat() {
let mut s = Snapshot::default();
s.thermal.clusters = vec![ClusterFreq {
name: "P-cluster".into(),
cur_mhz: 1800.0,
max_mhz: 4000.0,
active_pct: 90.0,
}];
s.thermal.throttle_factor = crate::throttle::throttle_factor(&s.thermal.clusters);
s.thermal.low_power_mode = Measured::exact(true, "t");
s.thermal.pressure = Measured::exact(ThermalPressure::Nominal, "t");
let c = explain(&s, &History::default());
assert_eq!(c[0].kind, CauseKind::LowPowerMode, "{c:#?}");
assert!(c[0].evidence.iter().any(|e| e.starts_with("running at 45%")));
assert!(c.iter().all(|c| c.kind != CauseKind::ThermalThrottle));
s.thermal.pressure = Measured::exact(ThermalPressure::Heavy, "t");
let c = explain(&s, &History::default());
let th = c.iter().find(|c| c.kind == CauseKind::ThermalThrottle).unwrap();
assert!(th.title.starts_with("Thermal throttling"));
assert!(c.iter().any(|c| c.kind == CauseKind::LowPowerMode));
s.thermal.low_power_mode = Measured::exact(false, "t");
s.thermal.pressure = Measured::exact(ThermalPressure::Moderate, "t");
let c = explain(&s, &History::default());
assert_eq!(c[0].title, "Thermal throttling — running at 45% speed");
s.thermal.pressure = Measured::exact(ThermalPressure::Nominal, "t");
s.thermal.low_power_mode = Measured::exact(false, "t");
let c = explain(&s, &History::default());
assert_eq!(c[0].title, "Clocks reduced — running at 45% speed");
assert_eq!(
c[0].evidence,
[
"P-cluster at 45% of max clock (90% busy)",
"thermal pressure nominal"
],
"the headline is not repeated as the first bullet"
);
}
#[test]
fn evidence_never_repeats_the_title() {
assert!(title_repeats(
"Clocks reduced — running at 35% speed",
"running at 35% speed"
));
assert!(title_repeats("Low Power Mode is on", "Low Power Mode on"));
assert!(!title_repeats("Low Power Mode is on", "running at 35% speed"));
assert!(!title_repeats("Swapping heavily", ""));
let mut s = Snapshot::default();
s.thermal.clusters = vec![ClusterFreq {
name: "P-cluster".into(),
cur_mhz: 1800.0,
max_mhz: 4000.0,
active_pct: 90.0,
}];
s.thermal.throttle_factor = crate::throttle::throttle_factor(&s.thermal.clusters);
s.thermal.low_power_mode = Measured::exact(true, "t");
for c in explain(&s, &History::default()) {
for e in &c.evidence {
assert!(!title_repeats(&c.title, e), "{c:?}");
}
}
}
#[test]
fn swap_storm_from_history_when_rates_are_unavailable() {
use crate::history::HistoryPoint;
let mut h = History::default();
let t0 = 1_000_000u64;
for k in 0..11u64 {
h.push(HistoryPoint {
t_ms: t0 + k * 30_000,
swap_used: Some((1024 + k * 150) * 1024 * 1024),
..Default::default()
});
}
let mut s = Snapshot {
taken_at_ms: t0 + 300_000,
..Default::default()
};
s.memory.swap_in_per_min = Measured::unavailable("vm_stat", "n/a");
let c = explain(&s, &h);
let sw = c
.iter()
.find(|c| c.kind == CauseKind::SwapStorm)
.expect("swap storm");
assert!(sw.evidence[0].contains("in 5m"), "{:?}", sw.evidence);
assert!(sw.evidence[0].contains("rates unavailable"));
s.memory.swap_in_per_min = Measured::exact(0, "t");
s.memory.swap_out_per_min = Measured::exact(0, "t");
assert!(explain(&s, &h).iter().all(|c| c.kind != CauseKind::SwapStorm));
}
#[test]
fn throttled_run_and_swap() {
let mut s = Snapshot::default();
s.thermal.throttle_factor = Measured::estimate(0.45, "t");
s.thermal.pressure = Measured::exact(ThermalPressure::Heavy, "t");
s.memory.swap_in_per_min = Measured::exact(600 * 1024 * 1024, "t");
let c = explain(&s, &History::default());
let kinds: Vec<CauseKind> = c.iter().map(|c| c.kind).collect();
assert!(kinds.contains(&CauseKind::ThermalThrottle));
assert!(kinds.contains(&CauseKind::SwapStorm));
let th = c.iter().find(|c| c.kind == CauseKind::ThermalThrottle).unwrap();
assert_eq!(th.title, "Thermal throttling — running at 45% speed");
assert!(!th.evidence.iter().any(|e| e == "running at 45% speed"), "{th:?}");
assert!(th.evidence.contains(&"thermal pressure heavy".to_string()));
assert!(c.windows(2).all(|w| w[0].score >= w[1].score));
}
}