Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
gpui: Report average dirty-to-present frame timings (#63221)
Anthony Eid55007f518bc1
diff --git a/crates/gpui/src/profiler.rs b/crates/gpui/src/profiler.rsindex 1c4f09adc34..f3dc0ce5685 100644--- a/crates/gpui/src/profiler.rs+++ b/crates/gpui/src/profiler.rs@@ -852,6 +852,8 @@ pub enum FrameEvent {#[cfg(feature = "profiler")]#[derive(Clone)]pub struct FrameDurationSnapshot {+ /// Histogram of durations from the first invalidation through presentation, in nanoseconds.+ pub dirty_to_present_histogram: Histogram<u64>,/// Histogram of `Window::draw` durations, in nanoseconds.pub draw_duration_histogram: Histogram<u64>,/// Histogram of intervals between consecutively presented frames while the@@ -894,6 +896,7 @@ pub struct WindowProfiler {window_id: WindowId,active_activities: SmallVec<[WindowActivity; 4]>,active_actions: SmallVec<[(&'static str, Instant); 2]>,+ dirty_to_present_histogram: Histogram<u64>,draw_duration_histogram: Histogram<u64>,present_interval_histogram: Histogram<u64>,first_input_at: Option<Instant>,@@ -914,6 +917,9 @@ impl WindowProfiler {window_id,active_activities: SmallVec::new(),active_actions: SmallVec::new(),+ dirty_to_present_histogram: Histogram::new(3).map_err(|error| {+ anyhow::anyhow!("Failed to create dirty-to-present histogram: {error}")+ })?,draw_duration_histogram: Histogram::new(3).map_err(|error| {anyhow::anyhow!("Failed to create draw duration histogram: {error}")})?,@@ -1070,6 +1076,7 @@ impl WindowProfiler {/// Returns a snapshot of the current frame-duration histograms.pub fn frame_duration_snapshot(&self) -> FrameDurationSnapshot {FrameDurationSnapshot {+ dirty_to_present_histogram: self.dirty_to_present_histogram.clone(),draw_duration_histogram: self.draw_duration_histogram.clone(),present_interval_histogram: self.present_interval_histogram.clone(),}@@ -1109,8 +1116,16 @@ impl WindowProfiler {};journal::record_present(present_timing, frame);- if frame.is_none() {+ let Some(frame) = frame else {return;+ };++ if let Some(dirty_at) = frame.dirty_at+ && let Err(error) = self+ .dirty_to_present_histogram+ .record(present_end.duration_since(dirty_at).as_nanos() as u64)+ {+ log::error!("failed to record dirty-to-present frame timing: {error}");}if let Some(animation_interval) = animation_interval {@@ -1408,6 +1423,22 @@ mod tests {assert_eq!(window_profiler.present_interval_histogram.len(), 1);}+ #[test]+ fn records_dirty_to_present_durations() {+ let mut window_profiler =+ WindowProfiler::new(WindowId::from(8)).expect("window profiler should initialize");+ let draw_end = Instant::now();+ let present_end = draw_end + Duration::from_millis(6);++ record_test_draw(&mut window_profiler, draw_end);+ window_profiler.record_present_at(present_end, present_end, true, false);++ let snapshot = window_profiler.frame_duration_snapshot();+ let histogram = snapshot.dirty_to_present_histogram;+ assert_eq!(histogram.len(), 1);+ assert!(histogram.max() >= Duration::from_millis(10).as_nanos() as u64);+ }+#[cfg(feature = "profiler")]#[test]fn records_every_draw_duration() {diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rsindex 50d3d915107..a871936dc42 100644--- a/crates/gpui/src/window.rs+++ b/crates/gpui/src/window.rs@@ -6422,6 +6422,7 @@ impl<V: 'static + Render> WindowHandle<V> {any_handle: AnyWindowHandle {id,state_type: TypeId::of::<V>(),+ root_entity_type_name: std::any::type_name::<V>(),},state_type: PhantomData,}@@ -6544,6 +6545,7 @@ impl<V: 'static> From<WindowHandle<V>> for AnyWindowHandle {pub struct AnyWindowHandle {pub(crate) id: WindowId,state_type: TypeId,+ root_entity_type_name: &'static str,}impl AnyWindowHandle {@@ -6552,6 +6554,11 @@ impl AnyWindowHandle {self.id}+ /// Returns the name of the window's declared root entity type.+ pub fn root_entity_type_name(&self) -> &'static str {+ self.root_entity_type_name+ }+/// Attempt to convert this handle to a window handle with a specific root view type./// If the types do not match, this will return `None`.pub fn downcast<T: 'static>(&self) -> Option<WindowHandle<T>> {diff --git a/crates/input_latency_ui/src/input_latency_ui.rs b/crates/input_latency_ui/src/input_latency_ui.rsindex e63afd288a6..a1961a2254c 100644--- a/crates/input_latency_ui/src/input_latency_ui.rs+++ b/crates/input_latency_ui/src/input_latency_ui.rs@@ -191,15 +191,15 @@ const MIN_DRAWS_TO_REPORT: u64 = 1_000;/// Computes and sends a `Frame Duration Report` telemetry event for the given/// window if enough frames were drawn since the last report.///-/// The report contains bucketed draw durations (how long `Window::draw` took)-/// and bucketed present intervals (the achieved frame-to-frame cadence while-/// the window was animating), so missed frames are visible in the fleet even-/// when no input was involved.+/// The report contains bucketed draw durations (how long `Window::draw` took),+/// bucketed present intervals (the achieved frame-to-frame cadence while the+/// window was animating), and the average dirty-to-present duration.///-/// Call this periodically (e.g. every five minutes) from a spawned task.+/// Call this periodically from a spawned task.pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {let current = window.frame_duration_snapshot();- let window_id = window.window_handle().window_id();+ let window_handle = window.window_handle();+ let window_id = window_handle.window_id();let open_window_ids = open_window_ids(cx);let state = cx.default_global::<FrameDurationTelemetryState>();@@ -208,7 +208,7 @@ pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {.retain(|window_id, _| open_window_ids.contains(window_id));let now = Instant::now();- let (delta_draws, delta_intervals, report_window_seconds) =+ let (delta_draws, delta_intervals, delta_dirty_to_present, report_window_seconds) =if let Some((prev_instant, prev_snapshot)) = state.previous.get(&window_id) {let mut delta_draws = current.draw_duration_histogram.clone();delta_draws@@ -218,8 +218,20 @@ pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {delta_intervals.subtract(&prev_snapshot.present_interval_histogram).ok();+ let mut delta_dirty_to_present = current.dirty_to_present_histogram.clone();+ if delta_dirty_to_present+ .subtract(&prev_snapshot.dirty_to_present_histogram)+ .is_err()+ {+ delta_dirty_to_present = current.dirty_to_present_histogram.clone();+ }let elapsed = now.duration_since(*prev_instant).as_secs();- (delta_draws, delta_intervals, elapsed)+ (+ delta_draws,+ delta_intervals,+ delta_dirty_to_present,+ elapsed,+ )} else {// First report for this window: the full cumulative histograms are// the delta from the empty starting state. We don't know how long@@ -228,6 +240,7 @@ pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {(current.draw_duration_histogram.clone(),current.present_interval_histogram.clone(),+ current.dirty_to_present_histogram.clone(),0u64,)};@@ -251,6 +264,7 @@ pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {let intervals_18to36 = count_frames_in_range(&delta_intervals, MS18_NS, MS36_NS);let intervals_36to100 = count_frames_in_range(&delta_intervals, MS36_NS, MS100_NS);// intervals > 100ms are implicitly total_intervals - (the buckets above)+ let average_dirty_to_present_ms = delta_dirty_to_present.mean() / 1_000_000.0;telemetry::event!("Frame Duration Report",@@ -264,6 +278,8 @@ pub fn report_frame_duration_telemetry(window: &Window, cx: &mut App) {intervals_18to36 = intervals_18to36,intervals_36to100 = intervals_36to100,total_intervals = total_intervals,+ average_dirty_to_present_ms = average_dirty_to_present_ms,+ root_entity_type_name = window_handle.root_entity_type_name(),report_window_seconds = report_window_seconds,);}diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rsindex f4afcc6930e..b460196dd43 100644--- a/crates/zed/src/zed.rs+++ b/crates/zed/src/zed.rs@@ -474,7 +474,7 @@ pub fn initialize_workspace(app_state: Arc<AppState>, cx: &mut App) {}cx.spawn_in(window, async move |_this, cx| {- const TELEMETRY_INTERVAL: std::time::Duration = std::time::Duration::from_secs(5 * 60);+ const TELEMETRY_INTERVAL: std::time::Duration = std::time::Duration::from_mins(5);loop {cx.background_executor().timer(TELEMETRY_INTERVAL).await;if cx