mirror of
https://codeberg.org/unspeaker/tengri.git
synced 2026-04-03 21:40:44 +02:00
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
use crate::*;
|
|
|
|
/// Performance counter
|
|
#[derive(Debug)]
|
|
pub struct PerfModel {
|
|
pub clock: quanta::Clock,
|
|
/// Measurement has a small cost. Disable it here.
|
|
pub enabled: bool,
|
|
// In nanoseconds. Time used by last iteration.
|
|
pub used: AtomicF64,
|
|
// In microseconds. Max prescribed time for iteration (frame, chunk...).
|
|
pub window: AtomicF64,
|
|
}
|
|
|
|
impl_default!(PerfModel: Self {
|
|
enabled: true,
|
|
clock: quanta::Clock::new(),
|
|
used: Default::default(),
|
|
window: Default::default(),
|
|
});
|
|
|
|
impl PerfModel {
|
|
pub fn get_t0 (&self) -> Option<u64> {
|
|
if self.enabled {
|
|
Some(self.clock.raw())
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
pub fn get_t1 (&self, t0: Option<u64>) -> Option<std::time::Duration> {
|
|
if let Some(t0) = t0 {
|
|
if self.enabled {
|
|
Some(self.clock.delta(t0, self.clock.raw()))
|
|
} else {
|
|
None
|
|
}
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
pub fn update (&self, t0: Option<u64>, microseconds: f64) {
|
|
if let Some(t0) = t0 {
|
|
let t1 = self.clock.raw();
|
|
self.used.store(self.clock.delta_as_nanos(t0, t1) as f64, Relaxed);
|
|
self.window.store(microseconds, Relaxed,);
|
|
}
|
|
}
|
|
pub fn percentage (&self) -> Option<f64> {
|
|
let window = self.window.load(Relaxed) * 1000.0;
|
|
if window > 0.0 {
|
|
let used = self.used.load(Relaxed);
|
|
Some(100.0 * used / window)
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
pub fn cycle <F: FnMut(&Self)->T, T> (&self, call: &mut F) -> T {
|
|
let t0 = self.get_t0();
|
|
let result = call(self);
|
|
let _t1 = self.get_t1(t0).unwrap();
|
|
result
|
|
}
|
|
}
|