mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-29 11:52:50 +01:00
feat(animation): add a notification throttle and animation stats
An app that moves its own windows, or is moved faster than it can report, can send a notification for every frame, and the window manager's event channel is small. throttle lets one through per window per interval. stats keeps just enough of how an animation burst went to answer why it felt slow from the log: how regular the frames were, and how long the apps took to apply them.
This commit is contained in:
1 parent
877c4e0e28
commit
190f9b44ce
3 files changed
+268
No files matched your search
@@ -16,8 +16,10 @@ pub mod lerp;
|
||||
pub mod prefix;
|
||||
pub mod render_dispatcher;
|
||||
pub use render_dispatcher::RenderDispatcher;
|
||||
pub mod stats;
|
||||
pub mod step;
|
||||
pub mod style;
|
||||
pub mod throttle;
|
||||
pub mod tracks;
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
//! A record of how an animation burst went, logged once the animator goes idle
|
||||
//!
|
||||
//! Smoothness is decided by two things: how regularly frames are produced, and how long the apps take
|
||||
//! to apply them. This keeps just enough of both to answer "why did that feel slow" from the log.
|
||||
|
||||
use parking_lot::Mutex;
|
||||
use std::sync::LazyLock;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
/// How many write durations are kept for one burst
|
||||
const MAX_SAMPLES: usize = 8192;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
struct Burst {
|
||||
started: Option<Instant>,
|
||||
ticks: u64,
|
||||
overruns: u64,
|
||||
windows_moved: u64,
|
||||
writes: u64,
|
||||
failed_writes: u64,
|
||||
write_micros: Vec<u64>,
|
||||
}
|
||||
|
||||
static BURST: LazyLock<Mutex<Burst>> = LazyLock::new(|| Mutex::new(Burst::default()));
|
||||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Summary {
|
||||
pub wall: Duration,
|
||||
pub ticks: u64,
|
||||
pub overruns: u64,
|
||||
pub windows_moved: u64,
|
||||
pub writes: u64,
|
||||
pub failed_writes: u64,
|
||||
pub write_p50: Duration,
|
||||
pub write_p95: Duration,
|
||||
pub write_max: Duration,
|
||||
}
|
||||
|
||||
impl Summary {
|
||||
/// How many frames per second the animator actually produced
|
||||
pub fn effective_fps(&self) -> f64 {
|
||||
let seconds = self.wall.as_secs_f64();
|
||||
|
||||
if seconds <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
|
||||
self.ticks as f64 / seconds
|
||||
}
|
||||
}
|
||||
|
||||
/// A frame was produced
|
||||
pub fn record_tick(now: Instant, windows_moved: usize, overran: bool) {
|
||||
let mut burst = BURST.lock();
|
||||
burst.started.get_or_insert(now);
|
||||
burst.ticks += 1;
|
||||
burst.windows_moved += windows_moved as u64;
|
||||
|
||||
if overran {
|
||||
burst.overruns += 1;
|
||||
}
|
||||
}
|
||||
|
||||
/// A window was written to
|
||||
pub fn record_write(took: Duration, succeeded: bool) {
|
||||
let mut burst = BURST.lock();
|
||||
burst.writes += 1;
|
||||
|
||||
if !succeeded {
|
||||
burst.failed_writes += 1;
|
||||
}
|
||||
|
||||
if burst.write_micros.len() < MAX_SAMPLES {
|
||||
burst.write_micros.push(took.as_micros() as u64);
|
||||
}
|
||||
}
|
||||
|
||||
/// Close the current burst and return what happened in it, if anything did
|
||||
pub fn take_summary(now: Instant) -> Option<Summary> {
|
||||
let mut burst = BURST.lock();
|
||||
let finished = std::mem::take(&mut *burst);
|
||||
drop(burst);
|
||||
|
||||
let started = finished.started?;
|
||||
|
||||
let mut micros = finished.write_micros;
|
||||
micros.sort_unstable();
|
||||
|
||||
Some(Summary {
|
||||
wall: now.saturating_duration_since(started),
|
||||
ticks: finished.ticks,
|
||||
overruns: finished.overruns,
|
||||
windows_moved: finished.windows_moved,
|
||||
writes: finished.writes,
|
||||
failed_writes: finished.failed_writes,
|
||||
write_p50: percentile(µs, 50),
|
||||
write_p95: percentile(µs, 95),
|
||||
write_max: micros
|
||||
.last()
|
||||
.copied()
|
||||
.map(Duration::from_micros)
|
||||
.unwrap_or_default(),
|
||||
})
|
||||
}
|
||||
|
||||
/// The value at `percent` of the way through already sorted samples
|
||||
fn percentile(sorted: &[u64], percent: usize) -> Duration {
|
||||
if sorted.is_empty() {
|
||||
return Duration::ZERO;
|
||||
}
|
||||
|
||||
let rank = (sorted.len() * percent).div_ceil(100).max(1);
|
||||
|
||||
Duration::from_micros(sorted[(rank - 1).min(sorted.len() - 1)])
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn percentiles_of_nothing_are_zero() {
|
||||
assert_eq!(percentile(&[], 50), Duration::ZERO);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn percentiles_pick_the_expected_samples() {
|
||||
let samples = (1..=100).collect::<Vec<u64>>();
|
||||
|
||||
assert_eq!(percentile(&samples, 50), Duration::from_micros(50));
|
||||
assert_eq!(percentile(&samples, 95), Duration::from_micros(95));
|
||||
assert_eq!(percentile(&samples, 100), Duration::from_micros(100));
|
||||
assert_eq!(percentile(&[7], 95), Duration::from_micros(7));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn effective_fps_is_ticks_over_wall_time() {
|
||||
let summary = Summary {
|
||||
wall: Duration::from_millis(500),
|
||||
ticks: 30,
|
||||
overruns: 0,
|
||||
windows_moved: 90,
|
||||
writes: 90,
|
||||
failed_writes: 0,
|
||||
write_p50: Duration::ZERO,
|
||||
write_p95: Duration::ZERO,
|
||||
write_max: Duration::ZERO,
|
||||
};
|
||||
|
||||
assert!((summary.effective_fps() - 60.0).abs() < 1e-9);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
//! A guard against floods of move and resize notifications
|
||||
//!
|
||||
//! An app that is moving its own windows, or is being moved faster than it can report, can send a
|
||||
//! notification for every frame, and the window manager's event channel is small. The notifications
|
||||
//! that matter while the mouse button is up say nothing a second one a moment later would not, so
|
||||
//! only the first in any short interval is let through.
|
||||
|
||||
use super::tracks::WindowId;
|
||||
use std::collections::HashMap;
|
||||
use std::time::Duration;
|
||||
use std::time::Instant;
|
||||
|
||||
/// The shortest interval between two of the same notification for the same window
|
||||
pub const MIN_INTERVAL: Duration = Duration::from_millis(100);
|
||||
|
||||
/// Entries older than this are forgotten
|
||||
const TTL: Duration = Duration::from_secs(1);
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Kind {
|
||||
Moved,
|
||||
Resized,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct Throttle {
|
||||
last: HashMap<(WindowId, Kind), Instant>,
|
||||
last_prune: Option<Instant>,
|
||||
}
|
||||
|
||||
impl Throttle {
|
||||
/// Whether a notification may be let through, recording that it was if so
|
||||
pub fn admit(&mut self, window_id: WindowId, kind: Kind, now: Instant) -> bool {
|
||||
self.prune(now);
|
||||
|
||||
match self.last.get(&(window_id, kind)) {
|
||||
Some(last) if now.saturating_duration_since(*last) < MIN_INTERVAL => false,
|
||||
_ => {
|
||||
self.last.insert((window_id, kind), now);
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn prune(&mut self, now: Instant) {
|
||||
if self
|
||||
.last_prune
|
||||
.is_some_and(|pruned| now.saturating_duration_since(pruned) < TTL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
self.last_prune = Some(now);
|
||||
self.last
|
||||
.retain(|_, last| now.saturating_duration_since(*last) < TTL);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_first_notification_is_let_through_and_a_burst_after_it_is_not() {
|
||||
let now = Instant::now();
|
||||
let mut throttle = Throttle::default();
|
||||
|
||||
assert!(throttle.admit(1, Kind::Moved, now));
|
||||
assert!(!throttle.admit(1, Kind::Moved, now + Duration::from_millis(10)));
|
||||
assert!(!throttle.admit(1, Kind::Moved, now + Duration::from_millis(99)));
|
||||
assert!(throttle.admit(1, Kind::Moved, now + MIN_INTERVAL));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn dropped_notifications_do_not_extend_the_interval() {
|
||||
let now = Instant::now();
|
||||
let mut throttle = Throttle::default();
|
||||
|
||||
assert!(throttle.admit(1, Kind::Moved, now));
|
||||
|
||||
// a steady stream still gets one through every interval
|
||||
let mut admitted = 0;
|
||||
for millis in 1..=200 {
|
||||
if throttle.admit(1, Kind::Moved, now + Duration::from_millis(millis)) {
|
||||
admitted += 1;
|
||||
}
|
||||
}
|
||||
|
||||
assert_eq!(admitted, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn windows_and_kinds_are_throttled_separately() {
|
||||
let now = Instant::now();
|
||||
let mut throttle = Throttle::default();
|
||||
|
||||
assert!(throttle.admit(1, Kind::Moved, now));
|
||||
assert!(throttle.admit(2, Kind::Moved, now));
|
||||
assert!(throttle.admit(1, Kind::Resized, now));
|
||||
assert!(!throttle.admit(1, Kind::Moved, now));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn old_entries_are_forgotten() {
|
||||
let now = Instant::now();
|
||||
let mut throttle = Throttle::default();
|
||||
|
||||
throttle.admit(1, Kind::Moved, now);
|
||||
throttle.admit(2, Kind::Moved, now + Duration::from_secs(5));
|
||||
|
||||
assert_eq!(throttle.last.len(), 1);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user