mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 19:56:42 +01:00
58 lines
1.3 KiB
Rust
58 lines
1.3 KiB
Rust
use crate::*;
|
|
|
|
/// Performance counter
|
|
pub struct PerfModel {
|
|
pub enabled: bool,
|
|
clock: quanta::Clock,
|
|
// In nanoseconds
|
|
used: AtomicF64,
|
|
// In microseconds
|
|
period: AtomicF64,
|
|
}
|
|
|
|
pub trait HasPerf {
|
|
fn perf (&self) -> &PerfModel;
|
|
}
|
|
|
|
impl Default for PerfModel {
|
|
fn default () -> Self {
|
|
Self {
|
|
enabled: true,
|
|
clock: quanta::Clock::new(),
|
|
used: Default::default(),
|
|
period: Default::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl PerfModel {
|
|
pub fn get_t0 (&self) -> Option<u64> {
|
|
if self.enabled {
|
|
Some(self.clock.raw())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
pub fn update (&self, t0: Option<u64>, scope: &ProcessScope) {
|
|
if let Some(t0) = t0 {
|
|
let t1 = self.clock.raw();
|
|
self.used.store(
|
|
self.clock.delta_as_nanos(t0, t1) as f64,
|
|
Relaxed,
|
|
);
|
|
self.period.store(
|
|
scope.cycle_times().unwrap().period_usecs as f64,
|
|
Relaxed,
|
|
);
|
|
}
|
|
}
|
|
pub fn percentage (&self) -> Option<f64> {
|
|
let period = self.period.load(Relaxed) * 1000.0;
|
|
if period > 0.0 {
|
|
let used = self.used.load(Relaxed);
|
|
Some(100.0 * used / period)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
}
|