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.
This commit is contained in:
Neel
2026-11-17 23:03:23 +00:00
parent 9925feb84c
commit 60910fb5ec
5 changed files with 61 additions and 0 deletions
+1
View File
@@ -2389,6 +2389,7 @@ impl App {
}
fn apply_refresh_effect(&mut self) {
crate::draw_log::record_cause("<refresh windows>");
for window in self.windows.values_mut() {
if let Some(window) = window.as_deref_mut() {
window.refreshing = true;
+1
View File
@@ -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::<T>());
self.app.notify(self.entity_state.entity_id);
}
+56
View File
@@ -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:
//! `<NEXTEST_TEST_NAME>\t<window id>\t<cause>=<count>,...`, 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<Vec<&'static str>> = const { RefCell::new(Vec::new()) };
}
fn log_file() -> Option<&'static Mutex<File>> {
static FILE: OnceLock<Option<Mutex<File>>> = 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::<Vec<_>>();
counts.sort_by(|left, right| right.1.cmp(&left.1));
let causes = counts
.iter()
.map(|(cause, count)| format!("{cause}={count}"))
.collect::<Vec<_>>()
.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();
}
+1
View File
@@ -32,6 +32,7 @@ mod asset_cache;
mod assets;
mod bounds_tree;
mod color;
mod draw_log;
mod element;
mod elements;
mod executor;
+2
View File
@@ -2691,6 +2691,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("<refresh window>");
if self.invalidator.not_drawing() {
self.refreshing = true;
self.invalidator.set_dirty(true);
@@ -3781,6 +3782,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")]