From 4dbc64e93ddafbddba8a2be193006ef9afed7f9a Mon Sep 17 00:00:00 2001 From: Neel Date: Tue, 17 Nov 2026 23:03:23 +0000 Subject: [PATCH] Experiment only: log window draws and their causes Not for landing. When GPUI_DRAW_LOG names a file, every draw appends the test name, window and the entity types that notified since the last draw. --- crates/gpui/src/app.rs | 1 + crates/gpui/src/app/context.rs | 1 + crates/gpui/src/draw_log.rs | 56 ++++++++++++++++++++++++++++++++++ crates/gpui/src/gpui.rs | 1 + crates/gpui/src/window.rs | 2 ++ 5 files changed, 61 insertions(+) create mode 100644 crates/gpui/src/draw_log.rs diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 67609d7214e..60cef0950c4 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -2420,6 +2420,7 @@ impl App { } fn apply_refresh_effect(&mut self) { + crate::draw_log::record_cause(""); for window in self.windows.values_mut() { if let Some(window) = window.as_deref_mut() { window.refreshing = true; diff --git a/crates/gpui/src/app/context.rs b/crates/gpui/src/app/context.rs index aba73617e38..4ebbe2e1465 100644 --- a/crates/gpui/src/app/context.rs +++ b/crates/gpui/src/app/context.rs @@ -214,6 +214,7 @@ impl<'a, T: 'static> Context<'a, T> { /// Tell GPUI that this entity has changed and observers of it should be notified. pub fn notify(&mut self) { + crate::draw_log::record_cause(std::any::type_name::()); self.app.notify(self.entity_state.entity_id); } diff --git a/crates/gpui/src/draw_log.rs b/crates/gpui/src/draw_log.rs new file mode 100644 index 00000000000..0d6572b1a49 --- /dev/null +++ b/crates/gpui/src/draw_log.rs @@ -0,0 +1,56 @@ +//! Experiment-only: logs every window draw and what invalidated it. +//! +//! When `GPUI_DRAW_LOG` names a file, each draw appends one line: +//! `\t\t=,...`, where the causes +//! are the entity types that notified, and refreshes, since the previous draw +//! on this thread. + +use collections::FxHashMap; +use parking_lot::Mutex; +use std::{cell::RefCell, fs::File, io::Write as _, sync::OnceLock}; + +thread_local! { + static CAUSES: RefCell> = const { RefCell::new(Vec::new()) }; +} + +fn log_file() -> Option<&'static Mutex> { + static FILE: OnceLock>> = OnceLock::new(); + FILE.get_or_init(|| { + let path = std::env::var_os("GPUI_DRAW_LOG")?; + let file = std::fs::OpenOptions::new() + .create(true) + .append(true) + .open(path) + .ok()?; + Some(Mutex::new(file)) + }) + .as_ref() +} + +pub(crate) fn record_cause(cause: &'static str) { + if log_file().is_some() { + CAUSES.with(|causes| causes.borrow_mut().push(cause)); + } +} + +pub(crate) fn record_draw(window_id: u64) { + let Some(file) = log_file() else { + return; + }; + let causes = CAUSES.with(|causes| std::mem::take(&mut *causes.borrow_mut())); + let mut counts = FxHashMap::<&'static str, usize>::default(); + for cause in causes { + *counts.entry(cause).or_default() += 1; + } + let mut counts = counts.into_iter().collect::>(); + counts.sort_by(|left, right| right.1.cmp(&left.1)); + let causes = counts + .iter() + .map(|(cause, count)| format!("{cause}={count}")) + .collect::>() + .join(","); + let test = std::env::var("NEXTEST_TEST_NAME").unwrap_or_default(); + let mut line = format!("{test}\t{window_id}\t{causes}\n"); + line.retain(|character| character != '\r'); + file.lock().write_all(line.as_bytes()).ok(); +} diff --git a/crates/gpui/src/gpui.rs b/crates/gpui/src/gpui.rs index abd966d750a..b768da62ed1 100644 --- a/crates/gpui/src/gpui.rs +++ b/crates/gpui/src/gpui.rs @@ -32,6 +32,7 @@ mod asset_cache; mod assets; mod bounds_tree; mod color; +mod draw_log; mod element; mod elements; mod executor; diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs index 78d81bea1ea..7df4b86f7a5 100644 --- a/crates/gpui/src/window.rs +++ b/crates/gpui/src/window.rs @@ -2699,6 +2699,7 @@ impl Window { /// Mark the window as dirty, scheduling it to be redrawn on the next frame. pub fn refresh(&mut self) { + crate::draw_log::record_cause(""); if self.invalidator.not_drawing() { self.refreshing = true; self.invalidator.set_dirty(true); @@ -3804,6 +3805,7 @@ impl Window { /// the contents of the new [`Scene`], use [`Self::present`]. #[profiling::function] pub fn draw(&mut self, cx: &mut App) -> ArenaClearNeeded { + crate::draw_log::record_draw(self.handle.window_id().as_u64()); // Drain every draw in profiler builds so a previous frame's // first-invalidation timestamp can't be attributed to this one. #[cfg(feature = "profiler")]