use super::{ProfileGroup, ProfileInspector, ProfileSnapshot, Subsystem};
use std::collections::BTreeMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{Duration, Instant};
#[derive(Default, Debug)]
pub struct CacheStats {
hits: AtomicU64,
misses: AtomicU64,
}
impl CacheStats {
pub fn hits(&self) -> u64 {
self.hits.load(Ordering::Relaxed)
}
pub fn misses(&self) -> u64 {
self.misses.load(Ordering::Relaxed)
}
pub fn reset(&self) {
self.hits.store(0, Ordering::Relaxed);
self.misses.store(0, Ordering::Relaxed);
}
}
pub static CACHE_STATS: CacheStats = CacheStats {
hits: AtomicU64::new(0),
misses: AtomicU64::new(0),
};
struct CacheEntry {
taken_at: Instant,
groups: Vec<ProfileGroup>,
}
pub struct CachedProfileInspector {
inner: ProfileInspector,
entries: BTreeMap<Subsystem, CacheEntry>,
ttls: BTreeMap<Subsystem, Duration>,
default_ttl: Duration,
}
impl CachedProfileInspector {
pub fn new() -> Self {
let mut ttls = BTreeMap::new();
ttls.insert(Subsystem::Nvme, Duration::from_secs(30));
ttls.insert(Subsystem::Gpu, Duration::from_secs(15));
ttls.insert(Subsystem::Memory, Duration::from_secs(60));
ttls.insert(Subsystem::Display, Duration::from_secs(5));
ttls.insert(Subsystem::Cpu, Duration::from_secs(2));
Self {
inner: ProfileInspector::new(),
entries: BTreeMap::new(),
ttls,
default_ttl: Duration::from_secs(5),
}
}
pub fn set_ttl(&mut self, sub: Subsystem, ttl: Duration) {
self.ttls.insert(sub, ttl);
}
pub fn invalidate(&mut self, sub: Option<Subsystem>) {
match sub {
Some(s) => {
self.entries.remove(&s);
}
None => self.entries.clear(),
}
}
fn ttl_for(&self, sub: Subsystem) -> Duration {
*self.ttls.get(&sub).unwrap_or(&self.default_ttl)
}
pub fn snapshot(&mut self, sub: Subsystem) -> Result<Vec<ProfileGroup>, String> {
if let Some(entry) = self.entries.get(&sub) {
if entry.taken_at.elapsed() < self.ttl_for(sub) {
CACHE_STATS.hits.fetch_add(1, Ordering::Relaxed);
return Ok(entry.groups.clone());
}
}
CACHE_STATS.misses.fetch_add(1, Ordering::Relaxed);
let groups = self.inner.snapshot(sub)?;
self.entries.insert(
sub,
CacheEntry {
taken_at: Instant::now(),
groups: groups.clone(),
},
);
Ok(groups)
}
pub fn snapshot_all(&mut self) -> ProfileSnapshot {
let mut providers = BTreeMap::new();
let mut errors = BTreeMap::new();
for sub in Subsystem::ALL {
match self.snapshot(*sub) {
Ok(groups) => {
providers.insert(*sub, groups);
}
Err(reason) => {
errors.insert(*sub, reason);
}
}
}
ProfileSnapshot {
timestamp: std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0),
providers,
errors,
}
}
}
impl Default for CachedProfileInspector {
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
static STATS_GUARD: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn lock_stats() -> std::sync::MutexGuard<'static, ()> {
STATS_GUARD.lock().unwrap_or_else(|e| e.into_inner())
}
#[test]
fn cold_call_misses_warm_call_hits() {
let _guard = lock_stats();
CACHE_STATS.reset();
let mut c = CachedProfileInspector::new();
c.set_ttl(Subsystem::Cpu, Duration::from_secs(60));
let cold = c.snapshot(Subsystem::Cpu);
let misses_after_cold = CACHE_STATS.misses();
if let Err(reason) = cold {
eprintln!("skipping: the CPU profile was not read: {reason}");
return;
}
let _ = c.snapshot(Subsystem::Cpu);
let hits_after_warm = CACHE_STATS.hits();
assert!(misses_after_cold >= 1);
assert!(hits_after_warm >= 1);
}
#[test]
fn invalidate_forces_refresh() {
let _guard = lock_stats();
CACHE_STATS.reset();
let mut c = CachedProfileInspector::new();
c.set_ttl(Subsystem::Cpu, Duration::from_secs(60));
let _ = c.snapshot(Subsystem::Cpu);
c.invalidate(Some(Subsystem::Cpu));
let miss_before = CACHE_STATS.misses();
let _ = c.snapshot(Subsystem::Cpu);
assert!(CACHE_STATS.misses() > miss_before);
}
#[test]
fn snapshot_all_populates_all_subsystems() {
let _guard = lock_stats();
let mut c = CachedProfileInspector::new();
let snap = c.snapshot_all();
for sub in Subsystem::ALL {
assert!(snap.providers.contains_key(sub) ^ snap.errors.contains_key(sub));
}
}
}