Files
EonaCat.XSBox/crates/graphics/src/hot.rs
T
EonaCat 53e6e612c9
Rust validation / Formatting (push) Failing after 26s
EonaCat/ci-workflows: SonarQube / sonar (push) Successful in 47s
Rust validation / Windows x64 build and tests (push) Canceled after 0s
Initial
2026-12-04 21:27:30 +01:00

165 lines
4.6 KiB
Rust

use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use std::sync::{Mutex, OnceLock};
use std::time::{Duration, Instant, SystemTime};
static ACTIVE: AtomicBool = AtomicBool::new(false);
static TABLE: Mutex<Vec<(String, String)>> = Mutex::new(Vec::new());
fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(|e| e.into_inner())
}
pub(crate) fn over(name: &str) -> Option<String> {
if !ACTIVE.load(Relaxed) {
return None;
}
lock(&TABLE).iter().find(|(n, _)| n == name).map(|(_, v)| v.clone())
}
#[allow(dead_code)]
pub(crate) fn var(name: &str) -> Option<String> {
over(name).or_else(|| std::env::var(name).ok())
}
fn parse(text: &str) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
for line in text.lines() {
let line = line.trim();
if line.is_empty() || line.starts_with('#') {
continue;
}
let Some((name, value)) = line.split_once('=') else { continue };
let (name, value) = (name.trim(), value.trim());
if !name.starts_with("EONACAT_XS_") || name.len() == "EONACAT_XS_".len() {
continue;
}
match out.iter_mut().find(|(n, _)| n == name) {
Some(entry) => entry.1 = value.to_string(),
None => out.push((name.to_string(), value.to_string())),
}
}
out
}
fn describe(table: &[(String, String)]) -> String {
if table.is_empty() {
return "defaults".to_string();
}
table.iter().map(|(n, v)| format!("{n}={v}")).collect::<Vec<_>>().join(" ")
}
fn summary(frames: &[f32]) -> (f32, f32) {
if frames.is_empty() {
return (0.0, 0.0);
}
let mut v = frames.to_vec();
v.sort_unstable_by(f32::total_cmp);
(v[v.len() / 2], v[(v.len() - 1) * 9 / 10])
}
const SETTLE: Duration = Duration::from_secs(1);
const CHECK_EVERY: Duration = Duration::from_secs(1);
const LOG_EVERY: Duration = Duration::from_secs(5);
struct Run {
path: PathBuf,
seen: Option<SystemTime>,
checked: Instant,
frame: u64,
segment: u64,
settings: String,
began: Instant,
logged: Instant,
frames: Vec<f32>,
}
impl Run {
fn line(&self) {
let (median, p90) = summary(&self.frames);
tracing::info!(
"hot-ab: segment {} ({}) frames {} median {median:.1} ms p90 {p90:.1} ms",
self.segment,
self.settings,
self.frames.len()
);
}
fn reload(&mut self, now: Instant) -> bool {
let Ok(modified) = std::fs::metadata(&self.path).and_then(|m| m.modified()) else { return false };
if self.seen == Some(modified) {
return false;
}
let Ok(text) = std::fs::read_to_string(&self.path) else { return false };
self.seen = Some(modified);
let table = parse(&text);
if self.segment > 0 {
self.line();
}
self.segment += 1;
self.settings = describe(&table);
*lock(&TABLE) = table;
tracing::info!("hot-ab: segment {} starts: {} (frame {})", self.segment, self.settings, self.frame);
(self.began, self.logged) = (now, now);
self.frames.clear();
true
}
}
fn run() -> &'static Mutex<Option<Run>> {
static RUN: OnceLock<Mutex<Option<Run>>> = OnceLock::new();
RUN.get_or_init(|| {
let run = std::env::var_os("EONACAT_XS_HOT_AB").filter(|p| !p.is_empty()).map(|path| {
let now = Instant::now();
ACTIVE.store(true, Relaxed);
tracing::info!("hot-ab: following {}", PathBuf::from(&path).display());
Run {
path: path.into(),
seen: None,
checked: now - CHECK_EVERY,
frame: 0,
segment: 0,
settings: String::new(),
began: now,
logged: now,
frames: Vec::new(),
}
});
Mutex::new(run)
})
}
pub(crate) fn presented(interval_ms: f32) {
static OFF: AtomicBool = AtomicBool::new(false);
if OFF.load(Relaxed) {
return;
}
let mut guard = lock(run());
let Some(r) = guard.as_mut() else {
OFF.store(true, Relaxed);
return;
};
let now = Instant::now();
r.frame += 1;
if now.duration_since(r.checked) >= CHECK_EVERY {
r.checked = now;
r.reload(now);
}
if r.segment == 0 {
return;
}
if now.duration_since(r.began) >= SETTLE {
r.frames.push(interval_ms);
}
if now.duration_since(r.logged) >= LOG_EVERY {
r.logged = now;
r.line();
}
}