use indexmap::indexmap;
use rapl_energy::Rapl;
pub trait EnergyAccumulator {
fn elapsed(&self) -> indexmap::IndexMap<String, f32>;
fn reset(&mut self);
}
pub struct DefaultEnergyAccumulator {
rapl: Option<Rapl>,
}
impl DefaultEnergyAccumulator {
pub fn new() -> Self {
let rapl = Rapl::now(true);
if rapl.as_ref().is_none_or(|r| r.packages.is_empty()) {
log::warn!("No RAPL packages were found. Please ensure you have read access to the files in `/sys/class/powercap/intel-rapl`.");
}
Self { rapl }
}
}
impl EnergyAccumulator for DefaultEnergyAccumulator {
fn elapsed(&self) -> indexmap::IndexMap<String, f32> {
self.rapl.as_ref().map_or(indexmap! {}, |x| x.elapsed())
}
fn reset(&mut self) {
if let Some(rapl) = &mut self.rapl {
rapl.reset();
}
}
}