Skip to content

armgit / zed

Public Imported from GitHub
0

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.

gpui: Measure frame pacing against each window's refresh rate (#65351)

Anthony Eid4f6d97b9de7d
diff --git a/Cargo.lock b/Cargo.lock
index 9f390074738..c6b0a9b1793 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -7864,8 +7864,8 @@ name = "hang_telemetry"
version = "0.1.0"
dependencies = [
"gpui",
- "hdrhistogram",
"log",
+ "serde",
"serde_json",
"telemetry_events",
]
@@ -8799,6 +8799,8 @@ dependencies = [
"collections",
"gpui",
"hdrhistogram",
+ "serde_json",
+ "sysinfo 0.39.6",
"telemetry",
]
diff --git a/crates/gpui/src/profiler.rs b/crates/gpui/src/profiler.rs
index e8108c106b8..ea1cd5ae1df 100644
--- a/crates/gpui/src/profiler.rs
+++ b/crates/gpui/src/profiler.rs
@@ -797,10 +797,139 @@ pub struct FrameTiming {
pub draw_start: Instant,
/// When `Window::draw` finished.
pub draw_end: Instant,
+ /// The refresh interval of the window's display when the frame was drawn,
+ /// or `None` when the platform doesn't report it.
+ pub refresh_interval: Option<Duration>,
+ /// When the platform's frame signal for this frame fired, or `None` when
+ /// the platform doesn't report it. Coalesced signals report the first.
+ pub signal_at: Option<Instant>,
+ /// How long each phase of the draw took.
+ pub phases: DrawPhases,
}
+/// How long each phase of a window draw took.
+#[cfg(feature = "profiler")]
+#[derive(Debug, Copy, Clone, Default, PartialEq, Eq)]
+pub struct DrawPhases {
+ /// Rendering views into elements and requesting their layout.
+ pub request_layout: Duration,
+ /// Computing layout and prepainting elements, including deferred draws,
+ /// prompts, drags, and tooltips.
+ pub prepaint: Duration,
+ /// Painting elements into the scene.
+ pub paint: Duration,
+}
+
+/// A phase of drawing a view.
+#[cfg(feature = "profiler")]
+#[derive(Debug, Copy, Clone, PartialEq, Eq)]
+pub enum ViewPhase {
+ /// Rendering the view and requesting its layout.
+ RequestLayout,
+ /// Prepainting the view's elements. Views whose cached elements are
+ /// stale also render and lay out here.
+ Prepaint,
+ /// Painting the view's elements.
+ Paint,
+}
+
+#[cfg(feature = "profiler")]
+impl ViewPhase {
+ /// The phase's name, e.g. `"prepaint"`.
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::RequestLayout => "request_layout",
+ Self::Prepaint => "prepaint",
+ Self::Paint => "paint",
+ }
+ }
+}
+
+/// Views whose own work in one draw phase takes less than this aren't
+/// recorded, keeping the journal's per-frame cost bounded while still naming
+/// the views that make a frame late.
+#[cfg(feature = "profiler")]
+pub const VIEW_TIMING_FLOOR: Duration = Duration::from_micros(500);
+
+/// One phase of drawing one view, recorded when its own work took at least
+/// [`VIEW_TIMING_FLOOR`].
+#[cfg(feature = "profiler")]
+#[derive(Debug, Copy, Clone)]
+pub struct ViewTiming {
+ /// The view's type name.
+ pub name: &'static str,
+ /// The draw phase.
+ pub phase: ViewPhase,
+ /// When the view's phase began.
+ pub start: Instant,
+ /// When the view's phase ended.
+ pub end: Instant,
+ /// The phase's duration excluding views nested inside it, so a parent
+ /// view isn't blamed for its children's work.
+ pub self_duration: Duration,
+}
+
+/// The least work a frame can take before it counts as late: one refresh
+/// at 120 Hz. Frames rarely fit a single refresh on faster displays yet, so
+/// judging them by it would count nearly every frame as late and hide
+/// regressions; missed refreshes still measure against the real interval.
+#[cfg(feature = "profiler")]
+pub const FRAME_BUDGET_FLOOR: Duration = Duration::from_nanos(8_333_333);
+
+/// The work a frame can take before it counts as late when its display's
+/// refresh interval is unknown: one refresh at 60 Hz, the most common rate,
+/// so frames on faster displays may be judged leniently but none too strictly.
+#[cfg(feature = "profiler")]
+pub const UNKNOWN_REFRESH_FRAME_BUDGET: Duration = Duration::from_nanos(16_666_667);
+
#[cfg(feature = "profiler")]
impl FrameTiming {
+ /// When the frame's work began: the later of its signal and its first
+ /// invalidation, since waiting for the platform's frame signal isn't work,
+ /// however late it fires. `None` without a frame signal.
+ pub fn work_start(&self) -> Option<Instant> {
+ let signal_at = self.signal_at?;
+ // A frame signal can fire while the main thread is busy and only be
+ // handled after the window became dirty; time before then isn't the
+ // frame's.
+ Some(
+ self.dirty_at
+ .map_or(signal_at, |dirty_at| dirty_at.max(signal_at)),
+ )
+ }
+
+ /// The main-thread time from [`FrameTiming::work_start`] until the frame
+ /// was submitted at `submitted_at`: the start of its submission, which may
+ /// consist mostly of waiting for the display to free a buffer.
+ pub fn work(&self, submitted_at: Instant) -> Option<Duration> {
+ Some(submitted_at.saturating_duration_since(self.work_start()?))
+ }
+
+ /// Whether the frame's work, submitted at `submitted_at`, exceeded
+ /// [`FrameTiming::budget`], when its work is known.
+ pub fn is_late(&self, submitted_at: Instant) -> Option<bool> {
+ Some(self.work(submitted_at)? > self.budget())
+ }
+
+ /// The work a frame can take before it counts as late: one refresh
+ /// interval, but no less than [`FRAME_BUDGET_FLOOR`], or
+ /// [`UNKNOWN_REFRESH_FRAME_BUDGET`] when the interval is unknown.
+ pub fn budget(&self) -> Duration {
+ self.refresh_interval
+ .map_or(UNKNOWN_REFRESH_FRAME_BUDGET, |interval| {
+ interval.max(FRAME_BUDGET_FLOOR)
+ })
+ }
+
+ /// How many refreshes the frame missed when submitted at `submitted_at`:
+ /// zero when its work fit within one refresh interval.
+ pub fn missed_refreshes(&self, submitted_at: Instant) -> Option<u64> {
+ let refresh_interval = self.refresh_interval?;
+ let work = self.work(submitted_at)?;
+ let refreshes = work.as_nanos().div_ceil(refresh_interval.as_nanos()).max(1);
+ Some(u64::try_from(refreshes - 1).unwrap_or(u64::MAX))
+ }
+
/// Time spent inside `Window::draw`.
pub fn draw_duration(&self) -> Duration {
self.draw_end.duration_since(self.draw_start)
@@ -859,6 +988,173 @@ pub struct FrameDurationSnapshot {
/// Histogram of intervals between consecutively presented frames while the
/// window was animating, in nanoseconds.
pub present_interval_histogram: Histogram<u64>,
+ /// The same frames, split by the refresh interval of the window's display
+ /// when they were drawn.
+ pub by_refresh_interval: Vec<RefreshIntervalFrames>,
+}
+
+/// Upper bounds, in milliseconds, of the duration buckets in
+/// [`RefreshIntervalFrames`]. Each bucket counts values up to and including its
+/// bound; a final bucket counts everything longer.
+#[cfg(feature = "profiler")]
+pub const FRAME_DURATION_BUCKETS_MS: [u64; 9] = [4, 8, 16, 33, 50, 100, 250, 500, 1000];
+
+/// Upper bounds, in percent of the display's real refresh interval, of the
+/// work-per-frame buckets in [`RefreshIntervalFrames`]. Each bucket counts
+/// values up to and including its bound; a final bucket counts everything
+/// more. Whether a frame was on time is judged by [`FrameTiming::budget`]
+/// instead, which never drops below [`FRAME_BUDGET_FLOOR`], so on displays
+/// faster than 120 Hz frames above 100% can still be on time.
+#[cfg(feature = "profiler")]
+pub const FRAME_WORK_BUCKETS_PERCENT: [u64; 7] = [25, 50, 75, 100, 150, 200, 400];
+
+/// Counts `histogram`'s values above the previous bound and up to and
+/// including each of `upper_bounds`, plus a final count of the values above
+/// the last bound.
+#[cfg(feature = "profiler")]
+pub fn histogram_bucket_counts(
+ histogram: &Histogram<u64>,
+ upper_bounds: impl IntoIterator<Item = u64>,
+) -> Vec<u64> {
+ let mut counted = 0;
+ let mut counts: Vec<u64> = upper_bounds
+ .into_iter()
+ .map(|bound| {
+ // Cumulative counts, since adjacent bounds can share a histogram
+ // bucket.
+ let cumulative = histogram.count_between(0, bound);
+ let count = cumulative.saturating_sub(counted);
+ counted = counted.max(cumulative);
+ count
+ })
+ .collect();
+ counts.push(histogram.len().saturating_sub(counted));
+ counts
+}
+
+/// Statistics for the frames a window drew and presented while its display had
+/// one refresh interval.
+///
+/// A frame is on time when its [`FrameTiming::work`] fit within
+/// [`FrameTiming::budget`].
+#[cfg(feature = "profiler")]
+#[derive(Clone, Debug)]
+pub struct RefreshIntervalFrames {
+ /// The display's refresh interval, or `None` when the platform doesn't
+ /// report it. Frame pacing is only measured for known intervals.
+ pub refresh_interval: Option<Duration>,
+ /// Frames drawn, whether or not they were presented.
+ pub frames_drawn: u64,
+ /// Draw durations, in nanoseconds.
+ pub draw_duration: Histogram<u64>,
+ /// Presented frames whose work fit within [`FrameTiming::budget`].
+ pub frames_on_time: u64,
+ /// Presented frames whose work exceeded [`FrameTiming::budget`].
+ pub frames_late: u64,
+ /// Refreshes that on-time and late frames missed in total, when the
+ /// refresh interval is known.
+ pub missed_refreshes: u64,
+ /// Presented frames without a frame signal to measure their work from.
+ pub frames_unmeasured: u64,
+ /// Work of on-time and late frames, in nanoseconds.
+ pub work: Histogram<u64>,
+ /// Of `frames_on_time`, the frames that responded to input.
+ pub input_frames_on_time: u64,
+ /// Of `frames_late`, the frames that responded to input.
+ pub input_frames_late: u64,
+ /// Work of on-time and late frames that responded to input, in
+ /// nanoseconds.
+ pub input_work: Histogram<u64>,
+}
+
+#[cfg(feature = "profiler")]
+impl RefreshIntervalFrames {
+ fn new(refresh_interval: Option<Duration>, empty_histogram: &Histogram<u64>) -> Self {
+ Self {
+ refresh_interval,
+ frames_drawn: 0,
+ draw_duration: empty_histogram.clone(),
+ frames_on_time: 0,
+ frames_late: 0,
+ missed_refreshes: 0,
+ frames_unmeasured: 0,
+ work: empty_histogram.clone(),
+ input_frames_on_time: 0,
+ input_frames_late: 0,
+ input_work: empty_histogram.clone(),
+ }
+ }
+
+ /// The frames recorded since `earlier`, a previous snapshot of the same
+ /// statistics.
+ pub fn since(&self, earlier: &Self) -> Self {
+ let subtract = |current: &Histogram<u64>, earlier: &Histogram<u64>| {
+ let mut delta = current.clone();
+ match delta.subtract(earlier) {
+ Ok(()) => delta,
+ Err(error) => {
+ log::error!("failed to subtract frame histograms: {error}");
+ current.clone()
+ }
+ }
+ };
+ Self {
+ refresh_interval: self.refresh_interval,
+ frames_drawn: self.frames_drawn.saturating_sub(earlier.frames_drawn),
+ draw_duration: subtract(&self.draw_duration, &earlier.draw_duration),
+ frames_on_time: self.frames_on_time.saturating_sub(earlier.frames_on_time),
+ frames_late: self.frames_late.saturating_sub(earlier.frames_late),
+ missed_refreshes: self
+ .missed_refreshes
+ .saturating_sub(earlier.missed_refreshes),
+ frames_unmeasured: self
+ .frames_unmeasured
+ .saturating_sub(earlier.frames_unmeasured),
+ work: subtract(&self.work, &earlier.work),
+ input_frames_on_time: self
+ .input_frames_on_time
+ .saturating_sub(earlier.input_frames_on_time),
+ input_frames_late: self
+ .input_frames_late
+ .saturating_sub(earlier.input_frames_late),
+ input_work: subtract(&self.input_work, &earlier.input_work),
+ }
+ }
+
+ fn record_presented_frame(
+ &mut self,
+ frame: &FrameTiming,
+ presentation: &PresentTiming,
+ responded_to_input: bool,
+ ) {
+ let submitted_at = presentation.present_start;
+ let (Some(work), Some(is_late)) = (frame.work(submitted_at), frame.is_late(submitted_at))
+ else {
+ self.frames_unmeasured += 1;
+ return;
+ };
+ record_duration(&mut self.work, work);
+ if responded_to_input {
+ record_duration(&mut self.input_work, work);
+ }
+ self.missed_refreshes += frame.missed_refreshes(submitted_at).unwrap_or(0);
+ if is_late {
+ self.frames_late += 1;
+ self.input_frames_late += u64::from(responded_to_input);
+ } else {
+ self.frames_on_time += 1;
+ self.input_frames_on_time += u64::from(responded_to_input);
+ }
+ }
+}
+
+/// Records `duration` in nanoseconds, growing the histogram as needed.
+#[cfg(feature = "profiler")]
+fn record_duration(histogram: &mut Histogram<u64>, duration: Duration) {
+ let nanos = u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX);
+ if let Err(error) = histogram.record(nanos) {
+ log::error!("failed to record frame duration: {error}");
+ }
}
/// A point-in-time snapshot of the input-latency histograms for a window,
@@ -907,6 +1203,16 @@ pub struct WindowProfiler {
last_present_at: Option<Instant>,
animating_at_last_present: bool,
pending_frame: Option<FrameTiming>,
+ refresh_interval: Option<Duration>,
+ frame_signal_at: Option<Instant>,
+ by_refresh_interval: Vec<RefreshIntervalFrames>,
+ /// Cloned for each new refresh interval's histograms, since creating one
+ /// can fail.
+ empty_histogram: Histogram<u64>,
+ draw_phases: DrawPhases,
+ /// For each view phase in progress, the time spent in views nested
+ /// inside it so far.
+ nested_view_time: SmallVec<[Duration; 16]>,
}
#[cfg(feature = "profiler")]
@@ -938,6 +1244,13 @@ impl WindowProfiler {
last_present_at: None,
animating_at_last_present: false,
pending_frame: None,
+ refresh_interval: None,
+ frame_signal_at: None,
+ by_refresh_interval: Vec::new(),
+ empty_histogram: Histogram::new(3)
+ .map_err(|error| anyhow::anyhow!("Failed to create frame histogram: {error}"))?,
+ draw_phases: DrawPhases::default(),
+ nested_view_time: SmallVec::new(),
};
journal::record_frame_pending(window_id, Instant::now());
Ok(profiler)
@@ -1022,6 +1335,18 @@ impl WindowProfiler {
journal::end_foreground_turn();
}
+ /// Records when the frame signal for the frame the next draw will produce
+ /// fired. Coalesced signals report the first.
+ pub fn record_frame_signal(&mut self, signal_at: Option<Instant>) {
+ self.frame_signal_at = signal_at;
+ }
+
+ /// Sets the refresh interval of the window's display, which frames drawn
+ /// from now on are attributed to.
+ pub fn set_refresh_interval(&mut self, refresh_interval: Option<Duration>) {
+ self.refresh_interval = refresh_interval;
+ }
+
/// Records the beginning of a window draw.
pub fn begin_draw(&mut self) {
journal::begin_foreground_turn();
@@ -1031,6 +1356,39 @@ impl WindowProfiler {
.push(WindowActivity::Draw { started_at });
}
+ /// Records the phases of the window draw in progress.
+ pub fn record_draw_phases(&mut self, phases: DrawPhases) {
+ self.draw_phases = phases;
+ }
+
+ /// Records the start of one phase of drawing a view, returning its start
+ /// time to pass to [`WindowProfiler::end_view`].
+ pub fn begin_view(&mut self) -> Instant {
+ self.nested_view_time.push(Duration::ZERO);
+ Instant::now()
+ }
+
+ /// Records the end of a view phase begun at `start`, journaling it when
+ /// its own work reached [`VIEW_TIMING_FLOOR`].
+ pub fn end_view(&mut self, start: Instant, name: &'static str, phase: ViewPhase) {
+ let end = Instant::now();
+ let duration = end.saturating_duration_since(start);
+ let nested = self.nested_view_time.pop().unwrap_or_default();
+ if let Some(parent_nested) = self.nested_view_time.last_mut() {
+ *parent_nested += duration;
+ }
+ let self_duration = duration.saturating_sub(nested);
+ if self_duration >= VIEW_TIMING_FLOOR {
+ journal::record_view(ViewTiming {
+ name,
+ phase,
+ start,
+ end,
+ self_duration,
+ });
+ }
+ }
+
/// Records the end of a window draw and returns the draw duration.
pub fn end_draw(&mut self, dirty_at: Option<Instant>, invalidations: u64) -> Duration {
let Some(WindowActivity::Draw {
@@ -1043,12 +1401,18 @@ impl WindowProfiler {
};
let draw_end = Instant::now();
+ // Taken even when the draw isn't recorded, so a later draw can't claim it.
+ let signal_at = self.frame_signal_at.take();
+ let dirty_at = dirty_at.filter(|at| journal::frame_sample_is_valid(self.window_id, *at));
let frame_timing = FrameTiming {
window_id: self.window_id,
- dirty_at: dirty_at.filter(|at| journal::frame_sample_is_valid(self.window_id, *at)),
+ dirty_at,
invalidations,
draw_start,
draw_end,
+ refresh_interval: self.refresh_interval,
+ signal_at,
+ phases: std::mem::take(&mut self.draw_phases),
};
let draw_duration = frame_timing.draw_duration();
if !journal::power_interrupted_since(draw_start) {
@@ -1060,8 +1424,8 @@ impl WindowProfiler {
/// Records that a frame was presented.
///
- /// `next_frame_scheduled` marks the animation state for the interval ending
- /// at the next newly drawn frame's presentation.
+ /// `next_frame_scheduled` marks the animation state for the interval
+ /// ending at the next newly drawn frame's presentation.
pub fn record_present(
&mut self,
present_start: Instant,
@@ -1092,9 +1456,28 @@ impl WindowProfiler {
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(),
+ by_refresh_interval: self.by_refresh_interval.clone(),
}
}
+ fn frames_for(&mut self, refresh_interval: Option<Duration>) -> &mut RefreshIntervalFrames {
+ let index = match self
+ .by_refresh_interval
+ .iter()
+ .position(|frames| frames.refresh_interval == refresh_interval)
+ {
+ Some(index) => index,
+ None => {
+ self.by_refresh_interval.push(RefreshIntervalFrames::new(
+ refresh_interval,
+ &self.empty_histogram,
+ ));
+ self.by_refresh_interval.len() - 1
+ }
+ };
+ &mut self.by_refresh_interval[index]
+ }
+
fn record_present_at(
&mut self,
present_start: Instant,
@@ -1102,9 +1485,11 @@ impl WindowProfiler {
window_active: bool,
next_frame_scheduled: bool,
) {
+ let mut responded_to_input = false;
if let Some(first_input_at) = self.first_input_at.take()
&& journal::frame_sample_is_valid(self.window_id, first_input_at)
{
+ responded_to_input = true;
let latency_nanos = present_end.duration_since(first_input_at).as_nanos() as u64;
self.input_latency_histogram.record(latency_nanos).ok();
if self.pending_input_count > 0 {
@@ -1144,6 +1529,8 @@ impl WindowProfiler {
let Some(frame) = frame else {
return;
};
+ self.frames_for(frame.refresh_interval)
+ .record_presented_frame(&frame, &present_timing, responded_to_input);
if let Some(dirty_at) = frame.dirty_at
&& let Err(error) = self
@@ -1166,6 +1553,9 @@ impl WindowProfiler {
fn record_draw_timing(&mut self, timing: FrameTiming) {
self.record_draw_duration(timing.draw_duration());
+ let frames = self.frames_for(timing.refresh_interval);
+ frames.frames_drawn += 1;
+ record_duration(&mut frames.draw_duration, timing.draw_duration());
self.pending_frame = Some(timing);
record_frame_event(FrameEvent::Draw(timing));
journal::record_draw(timing);
@@ -1303,6 +1693,194 @@ mod tests {
}
}
+ #[test]
+ fn frame_work_starts_at_the_later_of_signal_and_invalidation() {
+ let refresh = Duration::from_millis(10);
+ let start = Instant::now();
+ let at = |ms| start + Duration::from_millis(ms);
+ let frame_with = |dirty: Option<u64>, signal: Option<u64>| FrameTiming {
+ window_id: WindowId::from(1),
+ dirty_at: dirty.map(at),
+ invalidations: 1,
+ draw_start: at(1),
+ draw_end: at(2),
+ refresh_interval: Some(refresh),
+ signal_at: signal.map(at),
+ phases: Default::default(),
+ };
+ let work = |dirty, signal| frame_with(dirty, signal).work(at(50));
+
+ // Waiting for the frame signal isn't work, however late it fires.
+ assert_eq!(work(Some(0), Some(8)), Some(Duration::from_millis(42)));
+ assert_eq!(work(Some(0), Some(40)), Some(Duration::from_millis(10)));
+ // A frame needed after its signal fired is measured from when it was
+ // needed.
+ assert_eq!(work(Some(5), Some(0)), Some(Duration::from_millis(45)));
+ // Animation frames have no invalidation to wait for.
+ assert_eq!(work(None, Some(0)), Some(Duration::from_millis(50)));
+ assert_eq!(work(Some(0), None), None);
+
+ let frame = frame_with(Some(0), Some(0));
+ for (submitted_ms, missed) in [(7, 0), (10, 0), (20, 1), (25, 2)] {
+ assert_eq!(frame.missed_refreshes(at(submitted_ms)), Some(missed));
+ }
+ }
+
+ #[test]
+ fn frames_are_split_by_refresh_interval_and_classified() {
+ let (_journal, _guard) = journal::install_test_foreground_journal(64, 4);
+ let mut profiler = WindowProfiler::new(WindowId::from(1)).expect("valid histograms");
+ let sixty_hertz = Duration::from_secs(1) / 60;
+ let one_hundred_twenty_hertz = Duration::from_secs(1) / 120;
+ let three_hundred_sixty_hertz = Duration::from_secs(1) / 360;
+ let base = Instant::now();
+ let at = |ms| base + Duration::from_millis(ms);
+
+ struct Frame {
+ refresh_interval: Option<Duration>,
+ signal_ms: Option<u64>,
+ present_start_ms: u64,
+ responded_to_input: bool,
+ }
+ let frames = [
+ // On time, responding to input.
+ Frame {
+ refresh_interval: Some(sixty_hertz),
+ signal_ms: Some(1),
+ present_start_ms: 5,
+ responded_to_input: true,
+ },
+ Frame {
+ refresh_interval: Some(sixty_hertz),
+ signal_ms: Some(0),
+ present_start_ms: 40,
+ responded_to_input: true,
+ },
+ Frame {
+ refresh_interval: Some(sixty_hertz),
+ signal_ms: None,
+ present_start_ms: 5,
+ responded_to_input: false,
+ },
+ // A late frame signal: only the 2 ms after it count.
+ Frame {
+ refresh_interval: Some(sixty_hertz),
+ signal_ms: Some(40),
+ present_start_ms: 42,
+ responded_to_input: false,
+ },
+ Frame {
+ refresh_interval: Some(one_hundred_twenty_hertz),
+ signal_ms: Some(0),
+ present_start_ms: 12,
+ responded_to_input: false,
+ },
+ // Misses refreshes but fits the budget floor.
+ Frame {
+ refresh_interval: Some(three_hundred_sixty_hertz),
+ signal_ms: Some(0),
+ present_start_ms: 6,
+ responded_to_input: false,
+ },
+ Frame {
+ refresh_interval: Some(three_hundred_sixty_hertz),
+ signal_ms: Some(0),
+ present_start_ms: 9,
+ responded_to_input: false,
+ },
+ Frame {
+ refresh_interval: None,
+ signal_ms: Some(0),
+ present_start_ms: 2,
+ responded_to_input: false,
+ },
+ ];
+ for frame in frames {
+ profiler.set_refresh_interval(frame.refresh_interval);
+ profiler.record_draw_timing(FrameTiming {
+ window_id: profiler.window_id,
+ dirty_at: Some(at(0)),
+ invalidations: 1,
+ draw_start: at(0),
+ draw_end: at(1),
+ refresh_interval: frame.refresh_interval,
+ signal_at: frame.signal_ms.map(at),
+ phases: Default::default(),
+ });
+ if frame.responded_to_input {
+ profiler.first_input_at = Some(at(0));
+ }
+ profiler.record_present_at(
+ at(frame.present_start_ms),
+ at(frame.present_start_ms + 1),
+ true,
+ false,
+ );
+ }
+
+ let snapshot = profiler.frame_duration_snapshot();
+ let frames_for = |refresh_interval| {
+ snapshot
+ .by_refresh_interval
+ .iter()
+ .find(|frames| frames.refresh_interval == refresh_interval)
+ .cloned()
+ .expect("frames for the refresh interval")
+ };
+ let work_buckets = |work: &Histogram<u64>, refresh_interval: Duration| {
+ histogram_bucket_counts(
+ work,
+ FRAME_WORK_BUCKETS_PERCENT
+ .map(|percent| (refresh_interval * percent as u32 / 100).as_nanos() as u64),
+ )
+ };
+ let sixty = frames_for(Some(sixty_hertz));
+ assert_eq!(sixty.frames_drawn, 4);
+ assert_eq!(sixty.frames_on_time, 2);
+ assert_eq!(sixty.frames_late, 1);
+ // 40 ms of work at 16.7 ms per refresh.
+ assert_eq!(sixty.missed_refreshes, 2);
+ assert_eq!(sixty.frames_unmeasured, 1);
+ assert_eq!(
+ work_buckets(&sixty.work, sixty_hertz),
+ [2, 0, 0, 0, 0, 0, 1, 0]
+ );
+ assert_eq!(sixty.input_frames_on_time, 1);
+ assert_eq!(sixty.input_frames_late, 1);
+ assert_eq!(
+ work_buckets(&sixty.input_work, sixty_hertz),
+ [1, 0, 0, 0, 0, 0, 1, 0]
+ );
+
+ let one_hundred_twenty = frames_for(Some(one_hundred_twenty_hertz));
+ assert_eq!(one_hundred_twenty.frames_late, 1);
+ assert_eq!(one_hundred_twenty.missed_refreshes, 1);
+ assert_eq!(one_hundred_twenty.input_frames_late, 0);
+
+ let three_hundred_sixty = frames_for(Some(three_hundred_sixty_hertz));
+ assert_eq!(three_hundred_sixty.frames_on_time, 1);
+ assert_eq!(three_hundred_sixty.frames_late, 1);
+ // 6 ms of work misses 2 refreshes at 2.78 ms each, and 9 ms misses 3.
+ assert_eq!(three_hundred_sixty.missed_refreshes, 5);
+
+ // Judged against the 60 Hz budget, without missed refreshes.
+ let unknown = frames_for(None);
+ assert_eq!(unknown.frames_drawn, 1);
+ assert_eq!(unknown.frames_on_time, 1);
+ assert_eq!(unknown.missed_refreshes, 0);
+
+ let later = profiler.frame_duration_snapshot();
+ let delta = later.by_refresh_interval[0].since(&snapshot.by_refresh_interval[0]);
+ assert_eq!(
+ delta.frames_drawn + delta.frames_on_time + delta.frames_late,
+ 0
+ );
+ assert_eq!(
+ delta.draw_duration.len() + delta.work.len() + delta.input_work.len(),
+ 0
+ );
+ }
+
#[test]
fn records_draw_events_only_while_tracing() {
let _trace_test_guard = TraceTestGuard::new();
@@ -1639,6 +2217,9 @@ mod tests {
invalidations: 1,
draw_start: draw_end - Duration::from_millis(2),
draw_end,
+ refresh_interval: None,
+ signal_at: None,
+ phases: Default::default(),
});
}
}
diff --git a/crates/gpui/src/profiler/hang.rs b/crates/gpui/src/profiler/hang.rs
index fbd52341e30..827682c50fc 100644
--- a/crates/gpui/src/profiler/hang.rs
+++ b/crates/gpui/src/profiler/hang.rs
@@ -23,7 +23,7 @@ pub(crate) use monitor::HangMonitor;
pub use monitor::{HangMonitorConfig, HangMonitorError, HangMonitorPoll, HangMonitorPollReason};
/// Version of the power/visibility-aware measurement rules.
-pub const MEASUREMENT_VERSION: u32 = 2;
+pub const MEASUREMENT_VERSION: u32 = 3;
use super::SerializedLocation;
use super::journal::{
@@ -42,6 +42,91 @@ pub struct HangDetector {
threshold: Duration,
frame_budget: Duration,
first_present_at: Option<Instant>,
+ activity: ActiveUse,
+}
+
+/// The user counts as actively using the app for this long after deliberate
+/// input.
+pub const ACTIVE_USE_WINDOW: Duration = Duration::from_secs(60);
+
+/// Input that doesn't show the user is using the app: platforms deliver
+/// pointer movement to background windows the pointer merely passes over.
+const PASSIVE_INPUT_KINDS: [&str; 3] = ["mouse_move", "mouse_exited", "mouse_pressure"];
+
+/// Accumulates the time the user was actively using the app: within
+/// [`ACTIVE_USE_WINDOW`] of deliberate input, excluding stretches interrupted
+/// by a system suspend.
+#[derive(Default)]
+struct ActiveUse {
+ /// End times of recent deliberate input, oldest first, for
+ /// [`Self::was_active_during`].
+ recent_inputs: std::collections::VecDeque<Instant>,
+ accounted_until: Option<Instant>,
+ active_until: Option<Instant>,
+ first_input_since_accounted: Option<Instant>,
+ last_input_since_accounted: Option<Instant>,
+ suspended: bool,
+}
+
+impl ActiveUse {
+ fn observe(&mut self, entry: &ForegroundJournalEntry) {
+ match entry {
+ ForegroundJournalEntry::Event(ForegroundEvent::Input(input))
+ if !PASSIVE_INPUT_KINDS.contains(&input.kind) =>
+ {
+ self.recent_inputs.push_back(input.end);
+ self.first_input_since_accounted.get_or_insert(input.start);
+ self.last_input_since_accounted = Some(
+ self.last_input_since_accounted
+ .map_or(input.end, |last| last.max(input.end)),
+ );
+ }
+ ForegroundJournalEntry::Boundary(IntervalBoundary::PowerTransition { .. }) => {
+ self.suspended = true;
+ }
+ _ => {}
+ }
+ }
+
+ /// Whether the user was actively using the app at some point from
+ /// `start` to `end`: deliberate input during that span or within
+ /// [`ACTIVE_USE_WINDOW`] before it.
+ fn was_active_during(&self, start: Instant, end: Instant) -> bool {
+ let since = start.checked_sub(ACTIVE_USE_WINDOW).unwrap_or(start);
+ self.recent_inputs
+ .iter()
+ .any(|input| *input >= since && *input <= end)
+ }
+
+ fn take(&mut self, now: Instant, max_elapsed: Duration) -> Duration {
+ // Incidents are judged soon after they end; inputs older than this
+ // can't be within the active window of anything still being sealed.
+ let retained_since = now.checked_sub(ACTIVE_USE_WINDOW * 10);
+ while self
+ .recent_inputs
+ .front()
+ .is_some_and(|input| retained_since.is_some_and(|since| *input < since))
+ {
+ self.recent_inputs.pop_front();
+ }
+ let since = self.accounted_until.replace(now);
+ let previous_until = self.active_until;
+ let first_input = self.first_input_since_accounted.take();
+ if let Some(last_input) = self.last_input_since_accounted.take() {
+ let until = last_input + ACTIVE_USE_WINDOW;
+ self.active_until = Some(previous_until.map_or(until, |previous| previous.max(until)));
+ }
+ let (Some(since), false) = (since, std::mem::take(&mut self.suspended)) else {
+ return Duration::ZERO;
+ };
+ let continued_until = previous_until.map_or(since, |until| until.min(now).max(since));
+ let mut active = continued_until.duration_since(since);
+ if let (Some(first_input), Some(active_until)) = (first_input, self.active_until) {
+ let start = first_input.max(continued_until);
+ active += active_until.min(now).saturating_duration_since(start);
+ }
+ active.min(max_elapsed)
+ }
}
/// One sealed interval that contained at least one hang.
@@ -52,11 +137,15 @@ pub struct HangIncident {
pub snapshot: FrameSnapshot,
/// Which detection rule qualified the interval.
pub trigger: HangTrigger,
- /// For [`HangTrigger::Threshold`], the events that blocked the foreground
- /// for at least the detector's threshold, longest first. For
- /// [`HangTrigger::Budget`], no event crossed the threshold and this instead
- /// holds every event in the interval, longest first.
+ /// The events that caused the incident, longest first: those at least as
+ /// long as the threshold or the frame budget for [`HangTrigger::Threshold`]
+ /// and [`HangTrigger::BlockingWork`], and those overlapping the late frame's
+ /// work for [`HangTrigger::LateFrame`].
pub contributors: Vec<ForegroundEvent>,
+ /// Whether the user was actively using the app during the incident (see
+ /// [`HangDetector::take_active_time`]), so rates per active hour can count
+ /// only incidents that happened during it.
+ pub during_active_use: bool,
}
/// The detection rule that qualified an interval as a [`HangIncident`].
@@ -64,19 +153,38 @@ pub struct HangIncident {
/// Recorded explicitly so consumers can separate the two classes without
/// re-deriving them from `stall_ms`, which stops working whenever the
/// detector's thresholds change.
-#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum HangTrigger {
/// A single event blocked the foreground for at least the hang threshold.
Threshold,
- /// No single event crossed the threshold, but the interval's total
- /// foreground spend reached the frame budget.
- Budget,
+ /// The interval presented a frame whose work exceeded its budget (see
+ /// [`FrameTiming::is_late`](crate::profiler::FrameTiming::is_late)).
+ LateFrame,
+ /// The interval presented no frame whose work could be measured, but a
+ /// single event blocked the foreground for at least the frame budget,
+ /// long enough to delay a frame.
+ BlockingWork,
+}
+
+impl HangTrigger {
+ /// Every trigger.
+ pub const ALL: [Self; 3] = [Self::Threshold, Self::LateFrame, Self::BlockingWork];
+
+ /// The trigger's serialized name, e.g. `"late_frame"`.
+ pub fn as_str(self) -> &'static str {
+ match self {
+ Self::Threshold => "threshold",
+ Self::LateFrame => "late_frame",
+ Self::BlockingWork => "blocking_work",
+ }
+ }
}
impl HangDetector {
- /// Creates a detector reporting single events at or above `threshold`
- /// and intervals whose total foreground spend reached `frame_budget`.
+ /// Creates a detector reporting single events at or above `threshold`,
+ /// late frames, and blocking work of at least `frame_budget` (see
+ /// [`HangIncident::detect`]).
/// Only events recorded from this point on are observed.
pub fn new(journal: ForegroundJournal, threshold: Duration, frame_budget: Duration) -> Self {
Self {
@@ -85,9 +193,20 @@ impl HangDetector {
threshold,
frame_budget,
first_present_at: None,
+ activity: ActiveUse::default(),
}
}
+ /// The time since the previous call that the user was actively using
+ /// the app, as of the journal entries polled so far: within
+ /// [`ACTIVE_USE_WINDOW`] of deliberate input, such as key presses, clicks,
+ /// and scrolling, excluding polls that observed a system
+ /// suspend. Capped at `max_elapsed`, so time this thread spent suspended
+ /// isn't counted.
+ pub fn take_active_time(&mut self, now: Instant, max_elapsed: Duration) -> Duration {
+ self.activity.take(now, max_elapsed)
+ }
+
/// When the first newly drawn frame observed by this detector finished
/// platform submission. `None` until a presentation boundary is observed.
pub fn first_present_at(&self) -> Option<Instant> {
@@ -98,6 +217,9 @@ impl HangDetector {
/// the previous poll.
pub fn poll(&mut self) -> Vec<HangIncident> {
let drained = self.collector.collect_unseen();
+ for entry in &drained.entries {
+ self.activity.observe(entry);
+ }
if self.first_present_at.is_none() {
self.first_present_at = drained.entries.iter().find_map(|entry| match entry {
ForegroundJournalEntry::Boundary(IntervalBoundary::Presented(presented)) => {
@@ -110,7 +232,11 @@ impl HangDetector {
.push_entries(drained.entries)
.into_iter()
.filter_map(|snapshot| {
- HangIncident::detect(snapshot, self.threshold, self.frame_budget)
+ let mut incident =
+ HangIncident::detect(snapshot, self.threshold, self.frame_budget)?;
+ let (start, end) = incident.active_window();
+ incident.during_active_use = self.activity.was_active_during(start, end);
+ Some(incident)
})
.collect()
}
@@ -127,8 +253,11 @@ pub struct SerializedHangIncident {
/// newly drawn frame finished platform submission (see
/// [`HangDetector::first_present_at`]), otherwise `"steady"`.
pub phase: &'static str,
- /// `"threshold"` or `"budget"` (see [`HangTrigger`]).
+ /// `"threshold"`, `"late_frame"`, or `"blocking_work"` (see
+ /// [`HangTrigger`]).
pub trigger: HangTrigger,
+ /// See [`HangIncident::during_active_use`].
+ pub during_active_use: bool,
/// When the incident's active window started, in milliseconds since app
/// startup: the sealing frame's first invalidation, or the earliest
/// contributor's start when nothing was pending a repaint. Foreground
@@ -231,6 +360,28 @@ pub enum SerializedHangContributor {
dirty_to_draw_ms: Option<f64>,
/// Invalidations coalesced into the frame.
invalidations: u64,
+ /// Time spent rendering views and requesting layout, in milliseconds.
+ request_layout_ms: f64,
+ /// Time spent computing layout and prepainting, in milliseconds.
+ prepaint_ms: f64,
+ /// Time spent painting, in milliseconds.
+ paint_ms: f64,
+ /// How many other events in the interval contain this one.
+ depth: usize,
+ },
+ /// One phase of drawing a view whose own work took at least
+ /// [`crate::profiler::VIEW_TIMING_FLOOR`].
+ View {
+ /// The view's type name.
+ name: &'static str,
+ /// The draw phase: `"request_layout"`, `"prepaint"`, or `"paint"`.
+ phase: &'static str,
+ /// When the phase started, in milliseconds since app startup.
+ start_ms: f64,
+ /// How long the phase took, including nested views, in milliseconds.
+ duration_ms: f64,
+ /// How long the phase took excluding nested views, in milliseconds.
+ self_ms: f64,
/// How many other events in the interval contain this one.
depth: usize,
},
@@ -284,6 +435,7 @@ impl SerializedHangIncident {
},
measurement_version: MEASUREMENT_VERSION,
trigger: incident.trigger,
+ during_active_use: incident.during_active_use,
start_ms: since_startup(active_start),
active_ms: as_millis(active),
stall_ms: incident
@@ -378,6 +530,17 @@ impl SerializedHangContributor {
duration_ms,
dirty_to_draw_ms: timing.dirty_to_draw_duration().map(as_millis),
invalidations: timing.invalidations,
+ request_layout_ms: as_millis(timing.phases.request_layout),
+ prepaint_ms: as_millis(timing.phases.prepaint),
+ paint_ms: as_millis(timing.phases.paint),
+ depth,
+ },
+ ForegroundEvent::View(timing) => Self::View {
+ name: timing.name,
+ phase: timing.phase.as_str(),
+ start_ms: since_startup(timing.start),
+ duration_ms,
+ self_ms: as_millis(timing.self_duration),
depth,
},
ForegroundEvent::Present(timing) => Self::Present {
@@ -428,41 +591,61 @@ impl HangIncident {
(start, snapshot.interval_end())
}
- /// Returns an incident when the snapshot contains at least one event
- /// that blocked the foreground for `threshold` or longer, or when the
- /// interval's total foreground spend — event time plus folded
- /// small-poll time — reached `frame_budget` even though no single event
- /// did. In the latter case every event in the interval becomes a
- /// contributor, since no single stall explains the busy interval.
+ /// Returns an incident when the snapshot contains an event that blocked
+ /// the foreground for `threshold` or longer; otherwise when the interval
+ /// presented a late frame, unless the journal lost events; otherwise, when
+ /// no presented frame's work could be measured, when an event blocked the
+ /// foreground for `frame_budget` or longer. See [`HangTrigger`].
pub fn detect(
snapshot: FrameSnapshot,
threshold: Duration,
frame_budget: Duration,
) -> Option<Self> {
- let mut contributors: Vec<ForegroundEvent> = snapshot
- .events
- .iter()
- .filter(|event| event.duration() >= threshold)
- .copied()
- .collect();
- let trigger = if contributors.is_empty() {
- if snapshot.journal_discontinuous {
+ let events_at_least = |minimum: Duration| -> Vec<ForegroundEvent> {
+ snapshot
+ .events
+ .iter()
+ .filter(|event| event.duration() >= minimum)
+ .copied()
+ .collect()
+ };
+ let mut contributors = events_at_least(threshold);
+ let measured_frame = match &snapshot.boundary {
+ IntervalBoundary::Presented(presented) => presented
+ .frame
+ .work_start()
+ .map(|work_start| (presented, work_start)),
+ _ => None,
+ };
+ let trigger = if !contributors.is_empty() {
+ HangTrigger::Threshold
+ } else if let Some((presented, work_start)) = measured_frame {
+ let submitted_at = presented.presentation.present_start;
+ if snapshot.journal_discontinuous
+ || !presented.frame.is_late(submitted_at).unwrap_or(false)
+ {
return None;
}
- let spend = snapshot.occupancy_within(snapshot.interval_start, snapshot.interval_end());
- if spend < frame_budget {
+ contributors = snapshot
+ .events
+ .iter()
+ .filter(|event| event.end_time() > work_start && event.start_time() < submitted_at)
+ .copied()
+ .collect();
+ HangTrigger::LateFrame
+ } else {
+ contributors = events_at_least(frame_budget);
+ if contributors.is_empty() {
return None;
}
- contributors = snapshot.events.clone();
- HangTrigger::Budget
- } else {
- HangTrigger::Threshold
+ HangTrigger::BlockingWork
};
contributors.sort_by_key(|event| std::cmp::Reverse(event.duration()));
Some(Self {
snapshot,
trigger,
contributors,
+ during_active_use: false,
})
}
}
@@ -479,12 +662,14 @@ mod tests {
use scheduler::SpawnTime;
use crate::{
- self as gpui, Context, FocusHandle, InteractiveElement, IntoElement, Modifiers,
- MouseButton, Render, Styled, TestAppContext, VisualTestContext, Window, WindowId, div,
- point, px,
+ self as gpui, AnyView, AppContext as _, Context, FocusHandle, InteractiveElement,
+ IntoElement, Modifiers, MouseButton, ParentElement as _, Render, Styled, TestAppContext,
+ VisualTestContext, Window, WindowId, div, point, px,
};
- use crate::profiler::{ActionTiming, FrameTiming, PresentTiming, TaskTiming, YieldTime};
+ use crate::profiler::{
+ ActionTiming, FrameTiming, PresentTiming, TaskTiming, ViewPhase, YieldTime,
+ };
use super::super::journal::{
FRAME_DEADLINE, ForegroundEvent, ForegroundJournalEntry, FrameSkipReason, FrameSkipped,
@@ -511,14 +696,14 @@ mod tests {
let at = |ms: u64| startup + Duration::from_millis(ms);
let window_id = WindowId::from(1);
for discontinuous in [false, true] {
- for trigger in [HangTrigger::Threshold, HangTrigger::Budget] {
+ for trigger in [HangTrigger::Threshold, HangTrigger::BlockingWork] {
let mut sealer = IntervalSealer::new(startup);
let mut entries = Vec::new();
match trigger {
HangTrigger::Threshold => entries.push(ForegroundJournalEntry::Event(
task_poll_event(at(10), at(30)),
)),
- HangTrigger::Budget => entries.extend([
+ _ => entries.extend([
ForegroundJournalEntry::Event(task_poll_event(at(10), at(16))),
ForegroundJournalEntry::Event(task_poll_event(at(20), at(26))),
]),
@@ -538,8 +723,8 @@ mod tests {
panic!("expected one snapshot, got {snapshots:?}");
};
let incident = HangIncident::detect(snapshot.clone(), HANG_THRESHOLD, FRAME_BUDGET);
- if discontinuous && trigger == HangTrigger::Budget {
- assert!(incident.is_none(), "skips must not exempt journal gaps");
+ if trigger == HangTrigger::BlockingWork {
+ assert!(incident.is_none(), "short work isn't blocking");
continue;
}
let incident = incident.expect("real work still qualifies across a skip");
@@ -645,11 +830,14 @@ mod tests {
animation_interval: None,
};
let frame = FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(at(100)),
invalidations: 3,
draw_start: at(350),
draw_end: at(380),
+ phases: Default::default(),
};
let snapshot = FrameSnapshot {
interval_start: at(150),
@@ -957,11 +1145,14 @@ mod tests {
interval_start: at(0),
boundary: IntervalBoundary::Presented(PresentedFrame {
frame: FrameTiming {
+ refresh_interval: None,
+ signal_at: Some(at(0)),
window_id,
dirty_at: Some(at(0)),
invalidations: 1,
draw_start: at(140),
draw_end: at(145),
+ phases: Default::default(),
},
presentation: PresentTiming {
window_id,
@@ -981,15 +1172,15 @@ mod tests {
};
let incident = HangIncident::detect(snapshot, HANG_THRESHOLD, FRAME_BUDGET)
- .expect("foreground spend exceeded the frame budget");
- assert_eq!(incident.trigger, HangTrigger::Budget);
+ .expect("the frame missed the 60 Hz budget used for unknown displays");
+ assert_eq!(incident.trigger, HangTrigger::LateFrame);
assert_eq!(incident.contributors.len(), 3);
assert_eq!(
incident.contributors[0].duration(),
Duration::from_millis(8)
);
let serialized = SerializedHangIncident::convert(startup, &incident, 8, Some(startup));
- assert_eq!(serialized.trigger, HangTrigger::Budget);
+ assert_eq!(serialized.trigger, HangTrigger::LateFrame);
assert_eq!(serialized.stall_ms, 8.0);
assert_eq!(serialized.dirty_to_present_ms, Some(150.0));
assert_eq!(serialized.sealed_by, "present");
@@ -1043,11 +1234,14 @@ mod tests {
interval_start: at(0),
boundary: IntervalBoundary::Presented(PresentedFrame {
frame: FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(at(0)),
invalidations: 1,
draw_start: at(145),
draw_end: at(147),
+ phases: Default::default(),
},
presentation: PresentTiming {
window_id,
@@ -1065,32 +1259,136 @@ mod tests {
assert!(HangIncident::detect(snapshot, HANG_THRESHOLD, FRAME_BUDGET).is_none());
}
- /// The budget applies to idle-sealed intervals too: a headless app (or
- /// a stretch with no repaint pending) can still starve the foreground
- /// with accumulated sub-threshold work.
#[test]
- fn an_idle_sealed_interval_over_budget_is_an_incident() {
+ fn active_use_counts_time_within_a_minute_of_deliberate_input() {
+ let start = scheduler::Instant::now();
+ let at = |seconds: u64| start + Duration::from_secs(seconds);
+ let input_of_kind = |kind: &'static str, seconds: u64| {
+ ForegroundJournalEntry::Event(ForegroundEvent::Input(InputTiming {
+ kind,
+ start: at(seconds),
+ end: at(seconds),
+ caused_invalidation: true,
+ }))
+ };
+ let input = |seconds: u64| input_of_kind("key_down", seconds);
+ let max = Duration::from_secs(10);
+ let mut activity = super::ActiveUse::default();
+ assert_eq!(activity.take(at(0), max), Duration::ZERO);
+ // The pointer passing over a window isn't use.
+ activity.observe(&input_of_kind("mouse_move", 1));
+ activity.observe(&input_of_kind("mouse_exited", 2));
+ assert_eq!(activity.take(at(4), max), Duration::ZERO);
+ // Idle until input at 5 s.
+ activity.observe(&input(5));
+ assert_eq!(activity.take(at(8), max), Duration::from_secs(3));
+ // Active until 65 s, a minute after the input.
+ assert_eq!(activity.take(at(16), max), Duration::from_secs(8));
+ for second in (24..=64).step_by(8) {
+ activity.take(at(second), max);
+ }
+ assert_eq!(activity.take(at(70), max), Duration::from_secs(1));
+ assert_eq!(activity.take(at(78), max), Duration::ZERO);
+ // A suspend discards the poll it was observed in.
+ activity.observe(&input(80));
+ activity.observe(&ForegroundJournalEntry::Boundary(
+ IntervalBoundary::PowerTransition { ended_at: at(81) },
+ ));
+ assert_eq!(activity.take(at(84), max), Duration::ZERO);
+ assert_eq!(activity.take(at(88), max), Duration::from_secs(4));
+ // Time the monitor itself was suspended isn't counted.
+ assert_eq!(activity.take(at(130), max), max);
+
+ // Incidents spanning input or within a minute after it are during
+ // active use; ones later than that aren't.
+ assert!(activity.was_active_during(at(79), at(80)));
+ assert!(activity.was_active_during(at(100), at(101)));
+ assert!(!activity.was_active_during(at(141), at(142)));
+ assert!(!activity.was_active_during(at(66), at(70)));
+ }
+
+ /// Without a measured frame, only a single event as long as the frame
+ /// budget is blocking work, however much short work the interval holds.
+ #[test]
+ fn only_single_events_over_the_frame_budget_are_blocking_work() {
let startup = scheduler::Instant::now();
let at = |ms: u64| startup + Duration::from_millis(ms);
- let snapshot = FrameSnapshot {
+ let snapshot = |longest_ms: u64| FrameSnapshot {
interval_start: at(0),
boundary: IntervalBoundary::Idle { ended_at: at(200) },
events: (0..10)
.map(|i| task_poll_event(at(i * 20), at(i * 20 + 5)))
+ .chain([task_poll_event(at(200), at(200 + longest_ms))])
.collect(),
small_polls: Vec::new(),
dropped_events: 0,
journal_discontinuous: false,
};
+ let frame_budget = Duration::from_millis(7);
+ assert!(HangIncident::detect(snapshot(6), HANG_THRESHOLD, frame_budget).is_none());
+ let incident = HangIncident::detect(snapshot(7), HANG_THRESHOLD, frame_budget)
+ .expect("one event as long as the frame budget");
+ assert_eq!(incident.trigger, HangTrigger::BlockingWork);
+ assert_eq!(incident.contributors.len(), 1);
+ }
- let incident = HangIncident::detect(snapshot, HANG_THRESHOLD, FRAME_BUDGET)
- .expect("foreground spend exceeded the frame budget");
- assert_eq!(incident.contributors.len(), 10);
- let serialized = SerializedHangIncident::convert(startup, &incident, 8, Some(startup));
- assert_eq!(serialized.sealed_by, "idle");
- assert_eq!(serialized.dirty_to_present_ms, None);
- assert_eq!(serialized.stall_ms, 5.0);
- assert_eq!(serialized.contributors_elided, 2);
+ /// With a known refresh interval, a frame is a budget hang when its work,
+ /// from its frame signal to its submission, exceeded one refresh interval or
+ /// the 120 Hz floor, whichever is longer.
+ #[test]
+ fn late_frames_are_budget_hangs() {
+ let startup = scheduler::Instant::now();
+ let at = |ms: u64| startup + Duration::from_millis(ms);
+ let window_id = WindowId::from(0xB0D6E7);
+ let detect = |refresh_interval: Duration,
+ signal_at: Option<scheduler::Instant>,
+ submitted_ms: u64| {
+ let snapshot = FrameSnapshot {
+ interval_start: at(0),
+ boundary: IntervalBoundary::Presented(PresentedFrame {
+ frame: FrameTiming {
+ window_id,
+ dirty_at: Some(at(0)),
+ invalidations: 1,
+ draw_start: at(1),
+ draw_end: at(submitted_ms - 1),
+ refresh_interval: Some(refresh_interval),
+ signal_at,
+ phases: Default::default(),
+ },
+ presentation: PresentTiming {
+ window_id,
+ present_start: at(submitted_ms),
+ // Time spent inside the submission doesn't make
+ // the frame late.
+ present_end: at(submitted_ms + 50),
+ animation_interval: None,
+ },
+ }),
+ events: vec![
+ task_poll_event(at(1), at(6)),
+ task_poll_event(at(10), at(15)),
+ ],
+ small_polls: Vec::new(),
+ dropped_events: 0,
+ journal_discontinuous: false,
+ };
+ HangIncident::detect(snapshot, HANG_THRESHOLD, FRAME_BUDGET)
+ };
+
+ let sixty_hertz = Duration::from_secs(1) / 60;
+ let incident = detect(sixty_hertz, Some(at(0)), 25).expect("work exceeded one refresh");
+ assert_eq!(incident.trigger, HangTrigger::LateFrame);
+ assert_eq!(incident.contributors.len(), 2);
+ // Work from before the frame signal didn't delay the frame.
+ let incident = detect(sixty_hertz, Some(at(8)), 25).expect("17 ms of work at 60 Hz");
+ assert_eq!(incident.contributors.len(), 1);
+ assert!(detect(sixty_hertz, Some(at(0)), 16).is_none());
+ assert!(detect(sixty_hertz, None, 25).is_none());
+
+ let three_hundred_sixty_hertz = Duration::from_secs(1) / 360;
+ assert!(detect(three_hundred_sixty_hertz, Some(at(0)), 8).is_none());
+ assert!(detect(three_hundred_sixty_hertz, Some(at(0)), 9).is_some());
}
/// Contributors serialize in start order with their nesting depth: an
@@ -1114,11 +1412,14 @@ mod tests {
caused_invalidation: true,
}),
ForegroundEvent::Draw(FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(at(0)),
invalidations: 1,
draw_start: at(1),
draw_end: at(39),
+ phases: Default::default(),
}),
],
small_polls: Vec::new(),
@@ -1145,55 +1446,6 @@ mod tests {
));
}
- /// Folded small polls count toward foreground spend, so an interval can
- /// reach the budget with no retained events at all. The incident then
- /// has no contributors and the small-poll summary carries the story.
- #[test]
- fn small_poll_spend_alone_can_reach_the_budget() {
- let startup = scheduler::Instant::now();
- let at = |ms: u64| startup + Duration::from_millis(ms);
- let window_id = WindowId::from(0xDE1A7);
- let snapshot = FrameSnapshot {
- interval_start: at(0),
- boundary: IntervalBoundary::Presented(PresentedFrame {
- frame: FrameTiming {
- window_id,
- dirty_at: Some(at(0)),
- invalidations: 1,
- draw_start: at(148),
- draw_end: at(149),
- },
- presentation: PresentTiming {
- window_id,
- present_start: at(149),
- present_end: at(150),
- animation_interval: None,
- },
- }),
- events: Vec::new(),
- small_polls: vec![SmallPollFlush {
- summary: PollSummary {
- count: 40,
- total: Duration::from_millis(12),
- },
- since: at(0),
- until: at(150),
- }],
- dropped_events: 0,
- journal_discontinuous: false,
- };
-
- let incident = HangIncident::detect(snapshot, HANG_THRESHOLD, FRAME_BUDGET)
- .expect("small-poll spend exceeded the frame budget");
- assert!(incident.contributors.is_empty());
- let serialized = SerializedHangIncident::convert(startup, &incident, 8, Some(startup));
- assert_eq!(serialized.stall_ms, 0.0);
- assert_eq!(serialized.event_count, 0);
- assert_eq!(serialized.small_poll_count, 40);
- assert_eq!(serialized.dirty_to_present_ms, Some(150.0));
- assert_eq!(serialized.busy_fraction, 0.08);
- }
-
proptest! {
#![proptest_config(ProptestConfig {
failure_persistence: None,
@@ -1201,10 +1453,10 @@ mod tests {
})]
#[test]
- fn detection_matches_threshold_and_budget_model_at_boundaries(
+ fn detection_matches_threshold_and_blocking_work_model_at_boundaries(
durations_ms in prop::collection::vec(0u16..=50, 0..=16),
threshold_ms in 0u16..=60,
- frame_budget_ms in 0u16..=500,
+ frame_budget_ms in 0u16..=60,
small_poll_total_ms in 0u16..=100,
) {
let origin = scheduler::Instant::now();
@@ -1225,11 +1477,29 @@ mod tests {
})
.collect::<Vec<_>>();
let interval_end_ms = cursor_ms.max(1);
+ let interval_end = origin + Duration::from_millis(interval_end_ms);
+ // Without a frame signal, the frame's work can't be measured.
+ let window_id = WindowId::from(1);
let snapshot = FrameSnapshot {
interval_start: origin,
- boundary: IntervalBoundary::Idle {
- ended_at: origin + Duration::from_millis(interval_end_ms),
- },
+ boundary: IntervalBoundary::Presented(PresentedFrame {
+ frame: FrameTiming {
+ window_id,
+ dirty_at: Some(origin),
+ invalidations: 1,
+ draw_start: interval_end,
+ draw_end: interval_end,
+ refresh_interval: None,
+ signal_at: None,
+ phases: Default::default(),
+ },
+ presentation: PresentTiming {
+ window_id,
+ present_start: interval_end,
+ present_end: interval_end,
+ animation_interval: None,
+ },
+ }),
events,
small_polls: vec![SmallPollFlush {
summary: PollSummary {
@@ -1247,13 +1517,13 @@ mod tests {
.copied()
.filter(|duration| *duration >= threshold_ms)
.collect::<Vec<_>>();
- let occupancy_ms = durations_ms
+ let blocking_durations = durations_ms
.iter()
- .map(|duration| u64::from(*duration))
- .sum::<u64>()
- + u64::from(small_poll_total_ms);
+ .copied()
+ .filter(|duration| *duration >= frame_budget_ms)
+ .collect::<Vec<_>>();
let expected_incident =
- !qualifying_durations.is_empty() || occupancy_ms >= u64::from(frame_budget_ms);
+ !qualifying_durations.is_empty() || !blocking_durations.is_empty();
let incident = HangIncident::detect(
snapshot,
@@ -1264,7 +1534,7 @@ mod tests {
if let Some(incident) = incident {
let mut expected_contributors = if qualifying_durations.is_empty() {
- durations_ms
+ blocking_durations
} else {
qualifying_durations
};
@@ -1400,6 +1670,92 @@ mod tests {
}
}
+ /// A slow view's draw is journaled under its type name with its own time,
+ /// excluding a slow view nested inside it, even when it's embedded through
+ /// an `AnyView`; fast phases aren't journaled, and the draw records how
+ /// long each of its phases took.
+ #[gpui::test]
+ fn slow_views_are_journaled_with_their_own_time(cx: &mut TestAppContext) {
+ let (journal, _journal_guard) = install_test_foreground_journal(1024, 16);
+ let mut collector = journal.collector();
+ let (_view, cx) = cx.add_window_view(|_, cx| SlowParent {
+ child: cx.new(|_| SlowChild).into(),
+ });
+ draw_window(cx);
+
+ let events: Vec<ForegroundEvent> = collector
+ .collect_unseen()
+ .entries
+ .into_iter()
+ .filter_map(|entry| match entry {
+ ForegroundJournalEntry::Event(event) => Some(event),
+ _ => None,
+ })
+ .collect();
+ let view_timing = |suffix: &str, phase: ViewPhase| {
+ events
+ .iter()
+ .rev()
+ .find_map(|event| match event {
+ ForegroundEvent::View(timing)
+ if timing.name.ends_with(suffix) && timing.phase == phase =>
+ {
+ Some(*timing)
+ }
+ _ => None,
+ })
+ .unwrap_or_else(|| panic!("expected a {phase:?} timing for {suffix}"))
+ };
+ let parent = view_timing("::SlowParent", ViewPhase::RequestLayout);
+ let child = view_timing("::SlowChild", ViewPhase::RequestLayout);
+ let parent_duration = parent.end.duration_since(parent.start);
+ let child_duration = child.end.duration_since(child.start);
+ assert!(child.self_duration >= SLOW_CHILD_RENDER);
+ assert!(parent.self_duration >= SLOW_PARENT_RENDER);
+ assert!(parent_duration >= child_duration + parent.self_duration);
+ assert!(!events.iter().any(|event| matches!(
+ event,
+ ForegroundEvent::View(timing) if timing.phase == ViewPhase::Paint
+ )));
+
+ let draw = events
+ .iter()
+ .rev()
+ .find_map(|event| match event {
+ ForegroundEvent::Draw(timing) => Some(*timing),
+ _ => None,
+ })
+ .expect("a draw");
+ assert!(draw.phases.request_layout >= SLOW_PARENT_RENDER + SLOW_CHILD_RENDER);
+ assert!(
+ draw.phases.request_layout + draw.phases.prepaint + draw.phases.paint
+ <= draw.draw_duration()
+ );
+ }
+
+ const SLOW_PARENT_RENDER: Duration = Duration::from_millis(3);
+ const SLOW_CHILD_RENDER: Duration = Duration::from_millis(2);
+
+ struct SlowParent {
+ child: AnyView,
+ }
+
+ impl Render for SlowParent {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ thread::sleep(SLOW_PARENT_RENDER);
+ div().size_full().child(self.child.clone())
+ }
+ }
+
+ struct SlowChild;
+
+ impl Render for SlowChild {
+ fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
+ thread::sleep(SLOW_CHILD_RENDER);
+ div().size_full()
+ }
+ }
+
struct Injection {
kind: HangKind,
duration: Duration,
@@ -1496,11 +1852,14 @@ mod tests {
fn frame(window_id: WindowId, draw_end: scheduler::Instant) -> FrameTiming {
FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(draw_end - Duration::from_millis(2)),
invalidations: 1,
draw_start: draw_end - Duration::from_millis(1),
draw_end,
+ phases: Default::default(),
}
}
}
diff --git a/crates/gpui/src/profiler/hang/monitor.rs b/crates/gpui/src/profiler/hang/monitor.rs
index 566b32d3dc0..c8cb491af19 100644
--- a/crates/gpui/src/profiler/hang/monitor.rs
+++ b/crates/gpui/src/profiler/hang/monitor.rs
@@ -12,7 +12,9 @@ use super::{HangDetector, HangIncident};
pub struct HangMonitorConfig {
/// Duration at which a single piece of foreground work counts as a hang.
pub threshold: Duration,
- /// Total foreground spend within one interval that counts as a hang.
+ /// Duration at which a single piece of foreground work counts as
+ /// blocking work in an interval without a measured frame (see
+ /// [`super::HangTrigger::BlockingWork`]).
pub frame_budget: Duration,
/// How often the monitor thread drains the journal.
pub interval: Duration,
@@ -45,6 +47,9 @@ pub struct HangMonitorPoll {
pub incidents: Vec<HangIncident>,
/// See [`HangDetector::first_present_at`].
pub first_present_at: Option<Instant>,
+ /// Time since the previous poll that the user was actively using the
+ /// app; see [`HangDetector::take_active_time`].
+ pub active_time: Duration,
/// Why this poll happened.
pub reason: HangMonitorPollReason,
}
@@ -89,9 +94,11 @@ impl HangMonitor {
Err(RecvTimeoutError::Disconnected) => break,
};
let incidents = detector.poll();
+ let active_time = detector.take_active_time(Instant::now(), interval * 2);
on_poll(HangMonitorPoll {
incidents,
first_present_at: detector.first_present_at(),
+ active_time,
reason,
});
if let Some(done) = done {
diff --git a/crates/gpui/src/profiler/journal.rs b/crates/gpui/src/profiler/journal.rs
index fba75ae77ca..97d88852d7b 100644
--- a/crates/gpui/src/profiler/journal.rs
+++ b/crates/gpui/src/profiler/journal.rs
@@ -24,7 +24,7 @@ use std::time::Duration;
use gpui_util::ResultExt;
use scheduler::Instant;
-use super::{ActionTiming, FrameTiming, PresentTiming, TaskTiming};
+use super::{ActionTiming, FrameTiming, PresentTiming, TaskTiming, ViewTiming};
use crate::{App, WindowId, WindowVisibility};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
@@ -84,6 +84,8 @@ pub enum ForegroundEvent {
Input(InputTiming),
/// A completed window draw.
Draw(FrameTiming),
+ /// One phase of drawing a view, recorded during the draw that contains it.
+ View(ViewTiming),
/// Work spent submitting a frame to the platform.
Present(PresentTiming),
/// Aggregate of task polls below [`TASK_POLL_FLOOR`], flushed before the
@@ -100,6 +102,7 @@ impl ForegroundEvent {
Self::Action(timing) => timing.start,
Self::Input(timing) => timing.start,
Self::Draw(timing) => timing.draw_start,
+ Self::View(timing) => timing.start,
Self::Present(timing) => timing.present_start,
Self::SmallPolls(flush) => flush.since,
}
@@ -112,6 +115,7 @@ impl ForegroundEvent {
Self::Action(timing) => timing.end,
Self::Input(timing) => timing.end,
Self::Draw(timing) => timing.draw_end,
+ Self::View(timing) => timing.end,
Self::Present(timing) => timing.present_end,
Self::SmallPolls(flush) => flush.until,
}
@@ -1038,6 +1042,10 @@ pub(crate) fn record_input(timing: InputTiming) {
with_journal(|journal| journal.record_event(ForegroundEvent::Input(timing)));
}
+pub(crate) fn record_view(timing: ViewTiming) {
+ with_journal(|journal| journal.record_event(ForegroundEvent::View(timing)));
+}
+
pub(crate) fn record_draw(timing: FrameTiming) {
with_journal(|journal| journal.record_event(ForegroundEvent::Draw(timing)));
}
@@ -1672,11 +1680,14 @@ mod tests {
assert!(sealer.push_entries(entries).is_empty());
let frame = FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(start),
invalidations: 1,
draw_start: start,
draw_end: start + Duration::from_millis(20),
+ phases: Default::default(),
};
record_draw(frame);
record_present(presentation_timing(window_id, frame.draw_end), Some(frame));
@@ -1851,11 +1862,14 @@ mod tests {
caused_invalidation: true,
};
let frame = FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id: WindowId::from(1),
dirty_at: Some(input.start),
invalidations: 1,
draw_start: start + Duration::from_millis(3),
draw_end: start + Duration::from_millis(4),
+ phases: Default::default(),
};
let presentation = PresentTiming {
window_id: frame.window_id,
@@ -2698,6 +2712,7 @@ mod tests {
ForegroundEvent::Draw(_) => 3,
ForegroundEvent::Present(_) => 4,
ForegroundEvent::SmallPolls(_) => 5,
+ ForegroundEvent::View(_) => 6,
};
NormalizedEvent {
kind,
@@ -3483,11 +3498,14 @@ mod tests {
fn frame_timing(window_id: WindowId, dirty_at: Instant, draw_end: Instant) -> FrameTiming {
FrameTiming {
+ refresh_interval: None,
+ signal_at: None,
window_id,
dirty_at: Some(dirty_at),
invalidations: 1,
draw_start: draw_end,
draw_end,
+ phases: Default::default(),
}
}
diff --git a/crates/gpui/src/view.rs b/crates/gpui/src/view.rs
index f24a4da9b79..081edd9a06b 100644
--- a/crates/gpui/src/view.rs
+++ b/crates/gpui/src/view.rs
@@ -24,6 +24,7 @@ use std::{
pub struct AnyView {
entity: AnyEntity,
render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
+ view_type_name: &'static str,
}
impl<V: Render> From<Entity<V>> for AnyView {
@@ -31,6 +32,7 @@ impl<V: Render> From<Entity<V>> for AnyView {
AnyView {
entity: value.into_any(),
render: any_view::render::<V>,
+ view_type_name: type_name::<V>(),
}
}
}
@@ -42,7 +44,7 @@ impl AnyView {
/// [Context::notify] was called on the backing entity since it was rendered
/// (or [Window::refresh] is called, which ignores caching).
pub fn cached(self, style: StyleRefinement) -> ViewElement<AnyView> {
- ViewElement::new(self).cached(style)
+ self.into_element().cached(style)
}
/// Convert this to a weak handle.
@@ -50,6 +52,7 @@ impl AnyView {
AnyWeakView {
entity: self.entity.downgrade(),
render: self.render,
+ view_type_name: self.view_type_name,
}
}
@@ -61,6 +64,7 @@ impl AnyView {
Err(entity) => Err(Self {
entity,
render: self.render,
+ view_type_name: self.view_type_name,
}),
}
}
@@ -101,7 +105,7 @@ impl<V: 'static + Render> IntoElement for Entity<V> {
type Element = ViewElement<Entity<V>>;
fn into_element(self) -> Self::Element {
- ViewElement::new(self)
+ ViewElement::with_type_name(self, type_name::<V>())
}
#[inline(never)]
@@ -114,7 +118,8 @@ impl IntoElement for AnyView {
type Element = ViewElement<AnyView>;
fn into_element(self) -> Self::Element {
- ViewElement::new(self)
+ let view_type_name = self.view_type_name;
+ ViewElement::with_type_name(self, view_type_name)
}
}
@@ -122,6 +127,7 @@ impl IntoElement for AnyView {
pub struct AnyWeakView {
entity: AnyWeakEntity,
render: fn(&AnyView, &mut Window, &mut App) -> AnyElement,
+ view_type_name: &'static str,
}
impl AnyWeakView {
@@ -131,6 +137,7 @@ impl AnyWeakView {
Some(AnyView {
entity,
render: self.render,
+ view_type_name: self.view_type_name,
})
}
}
@@ -140,6 +147,7 @@ impl<V: 'static + Render> From<WeakEntity<V>> for AnyWeakView {
AnyWeakView {
entity: view.into(),
render: any_view::render::<V>,
+ view_type_name: type_name::<V>(),
}
}
}
@@ -240,7 +248,7 @@ impl<T: Render> Entity<T> {
/// uncached case.
#[track_caller]
pub fn cached(self, style: StyleRefinement) -> ViewElement<Entity<T>> {
- ViewElement::new(self).cached(style)
+ ViewElement::with_type_name(self, type_name::<T>()).cached(style)
}
}
@@ -251,6 +259,8 @@ pub struct ViewElement<V: View> {
view: Option<V>,
entity_id: Option<EntityId>,
cached_style: Option<StyleRefinement>,
+ #[cfg(feature = "profiler")]
+ view_type_name: &'static str,
#[cfg(debug_assertions)]
source: &'static core::panic::Location<'static>,
}
@@ -259,10 +269,20 @@ impl<V: View> ViewElement<V> {
/// Wrap a [`View`] as an element.
#[track_caller]
pub fn new(view: V) -> Self {
+ Self::with_type_name(view, type_name::<V>())
+ }
+
+ /// Wraps a view whose draw time profiling attributes to `view_type_name`,
+ /// such as an entity's type rather than [`Entity`]'s.
+ #[track_caller]
+ #[cfg_attr(not(feature = "profiler"), expect(unused_variables))]
+ pub(crate) fn with_type_name(view: V, view_type_name: &'static str) -> Self {
let entity_id = view.entity_id();
ViewElement {
entity_id,
cached_style: None,
+ #[cfg(feature = "profiler")]
+ view_type_name,
view: Some(view),
#[cfg(debug_assertions)]
source: core::panic::Location::caller(),
@@ -330,14 +350,23 @@ impl<V: View> Element for ViewElement<V> {
) -> (LayoutId, Self::RequestLayoutState) {
if let Some(entity_id) = self.entity_id {
// Stateful path: create a reactive boundary.
+ #[cfg(feature = "profiler")]
+ let start = window.window_profiler.begin_view();
let view = &mut self.view;
- request_layout_view(
+ let layout = request_layout_view(
entity_id,
self.cached_style.as_ref(),
window,
cx,
&mut |window, cx| view.take().unwrap().render(window, cx).into_any_element(),
- )
+ );
+ #[cfg(feature = "profiler")]
+ window.window_profiler.end_view(
+ start,
+ self.view_type_name,
+ crate::profiler::ViewPhase::RequestLayout,
+ );
+ layout
} else {
// Stateless path: isolate subtree via type name (no entity identity).
request_layout_component(type_name::<V>(), window, cx, &mut |window, cx| {
@@ -361,7 +390,9 @@ impl<V: View> Element for ViewElement<V> {
) -> Option<AnyElement> {
if let Some(entity_id) = self.entity_id {
// Stateful path.
- prepaint_view(
+ #[cfg(feature = "profiler")]
+ let start = window.window_profiler.begin_view();
+ let prepainted = prepaint_view(
entity_id,
global_id,
bounds,
@@ -375,7 +406,14 @@ impl<V: View> Element for ViewElement<V> {
.render(window, cx)
.into_any_element()
},
- )
+ );
+ #[cfg(feature = "profiler")]
+ window.window_profiler.end_view(
+ start,
+ self.view_type_name,
+ crate::profiler::ViewPhase::Prepaint,
+ );
+ prepainted
} else {
// Stateless path: just prepaint the element.
prepaint_component(type_name::<V>(), element, window, cx)
@@ -394,6 +432,8 @@ impl<V: View> Element for ViewElement<V> {
) {
if let Some(entity_id) = self.entity_id {
// Stateful path.
+ #[cfg(feature = "profiler")]
+ let start = window.window_profiler.begin_view();
paint_view(
entity_id,
self.cached_style.is_some(),
@@ -402,6 +442,12 @@ impl<V: View> Element for ViewElement<V> {
window,
cx,
);
+ #[cfg(feature = "profiler")]
+ window.window_profiler.end_view(
+ start,
+ self.view_type_name,
+ crate::profiler::ViewPhase::Paint,
+ );
} else {
// Stateless path: just paint the element.
paint_component(std::any::type_name::<V>(), element, window, cx);
diff --git a/crates/gpui/src/window.rs b/crates/gpui/src/window.rs
index 567da8f39e6..1214b0351a2 100644
--- a/crates/gpui/src/window.rs
+++ b/crates/gpui/src/window.rs
@@ -1277,7 +1277,7 @@ pub struct Window {
/// Used to selectively enable VRR optimization only when input rate exceeds 60fps.
pub(crate) input_rate_tracker: Rc<RefCell<InputRateTracker>>,
#[cfg(feature = "profiler")]
- window_profiler: profiler::WindowProfiler,
+ pub(crate) window_profiler: profiler::WindowProfiler,
last_input_modality: InputModality,
touch_gestures: TouchGestureRecognizer,
touch_prediction_enabled: bool,
@@ -1886,6 +1886,10 @@ impl Window {
measure("frame duration", || {
handle
.update(&mut cx, |_, window, cx| {
+ #[cfg(feature = "profiler")]
+ window
+ .window_profiler
+ .record_frame_signal(request_frame_options.signal_at);
if force_render {
// Bypass cached view reuse so we don't replay stale
// atlas tile references after a GPU device recovery.
@@ -3561,6 +3565,8 @@ impl Window {
#[cfg(feature = "profiler")]
{
+ self.window_profiler
+ .set_refresh_interval(self.refresh_interval(cx));
let draw_duration = self
.window_profiler
.end_draw(frame_dirty.dirty_at, frame_dirty.invalidations);
@@ -3691,8 +3697,12 @@ impl Window {
// stretches to fill the viewport unless explicitly sized, window roots
// fill the window when their size is `auto`.
let scale_factor = self.scale_factor();
+ #[cfg(feature = "profiler")]
+ let request_layout_start = Instant::now();
let mut root_element = self.root.as_ref().unwrap().clone().into_any_element();
let root_layout_id = root_element.request_layout(self, cx);
+ #[cfg(feature = "profiler")]
+ let prepaint_start = Instant::now();
self.layout_engine
.as_mut()
.unwrap()
@@ -3730,6 +3740,8 @@ impl Window {
self.mouse_hit_test = self.next_frame.hit_test(self.mouse_position);
// Now actually paint the elements.
+ #[cfg(feature = "profiler")]
+ let paint_start = Instant::now();
self.invalidator.set_phase(DrawPhase::Paint);
root_element.paint(self, cx);
@@ -3749,6 +3761,14 @@ impl Window {
#[cfg(any(feature = "inspector", debug_assertions))]
self.paint_inspector_hitbox(cx);
+ #[cfg(feature = "profiler")]
+ self.window_profiler
+ .record_draw_phases(profiler::DrawPhases {
+ request_layout: prepaint_start.duration_since(request_layout_start),
+ prepaint: paint_start.duration_since(prepaint_start),
+ paint: Instant::now().duration_since(paint_start),
+ });
+
// a11y may have been activated/deactivated halfway through the frame
let a11y_active_start_of_frame = self.a11y.is_active();
self.a11y.sync_active_flag();
diff --git a/crates/hang_telemetry/Cargo.toml b/crates/hang_telemetry/Cargo.toml
index 8555fcf66aa..1cebb2ba63a 100644
--- a/crates/hang_telemetry/Cargo.toml
+++ b/crates/hang_telemetry/Cargo.toml
@@ -14,7 +14,7 @@ doctest = false
[dependencies]
gpui = { workspace = true, features = ["profiler"] }
-hdrhistogram.workspace = true
log.workspace = true
+serde.workspace = true
serde_json.workspace = true
telemetry_events.workspace = true
diff --git a/crates/hang_telemetry/src/hang_telemetry.rs b/crates/hang_telemetry/src/hang_telemetry.rs
index 97ed89f2614..f659580f694 100644
--- a/crates/hang_telemetry/src/hang_telemetry.rs
+++ b/crates/hang_telemetry/src/hang_telemetry.rs
@@ -12,10 +12,11 @@ use std::time::{Duration, Instant};
use gpui::App;
use gpui::profiler::hang::{
- HangMonitorConfig, HangMonitorError, HangMonitorPoll, HangMonitorPollReason, HangTrigger,
- MEASUREMENT_VERSION, SerializedHangIncident,
+ HangIncident, HangMonitorConfig, HangMonitorError, HangMonitorPoll, HangMonitorPollReason,
+ HangTrigger, MEASUREMENT_VERSION, SerializedHangIncident,
};
-use hdrhistogram::Histogram;
+use gpui::profiler::journal::ForegroundEvent;
+use serde::Serialize;
use serde_json::Value;
use telemetry_events::FlexibleEvent;
@@ -28,12 +29,29 @@ const MAX_SERIALIZED_CONTRIBUTORS: usize = 8;
/// them all.
const MAX_REPORTED_INCIDENTS: usize = 10;
+/// Cap on the contributors summed across every incident in an event, kept by
+/// total duration, so frequent causes show up even when no single incident
+/// they're in is among the largest.
+const MAX_REPORTED_CONTRIBUTOR_TOTALS: usize = 20;
+
+/// Cap on the distinct contributors tracked between sends, bounding memory
+/// for sessions with many distinct slow tasks.
+const MAX_TRACKED_CONTRIBUTORS: usize = 512;
+
// A long interval keeps hang telemetry a small fraction of event volume even
// for pathologically hang-prone sessions; the on-quit flush covers short ones.
const SEND_INTERVAL: Duration = Duration::from_mins(30);
const MONITOR_INTERVAL: Duration = Duration::from_secs(1);
+/// Upper bounds, in milliseconds, of the stall buckets each event reports.
+/// Each bucket counts stalls up to and including its bound; a final bucket
+/// counts longer ones.
+const STALL_BUCKETS_MS: [u64; 8] = [50, 100, 250, 500, 1000, 2000, 5000, 10000];
+
+/// Counts per [`STALL_BUCKETS_MS`] bucket, plus the final unbounded one.
+type StallBuckets = [u64; STALL_BUCKETS_MS.len() + 1];
+
/// Duration at which a single piece of foreground work counts as a hang.
pub fn hang_threshold() -> Duration {
if cfg!(debug_assertions) {
@@ -49,16 +67,16 @@ pub fn hang_threshold() -> Duration {
}
}
-/// Total foreground spend within one interval that counts as a hang.
+/// Duration at which a single piece of foreground work counts as blocking
+/// work when no frame's work was measured (see [`HangTrigger::BlockingWork`]).
pub fn frame_budget() -> Duration {
if cfg!(debug_assertions) {
// Unoptimized builds routinely spend more than a release frame budget
- // on ordinary frames; keep dev builds from reporting constantly.
+ // on ordinary work; keep dev builds from reporting constantly.
Duration::from_millis(100)
} else {
- // At least one dropped frame on any display. Generous while budget
- // incidents are plentiful; lower it as they get fixed.
- Duration::from_millis(24)
+ // Long enough to delay a frame even at 60 Hz.
+ gpui::profiler::UNKNOWN_REFRESH_FRAME_BUDGET
}
}
@@ -118,6 +136,10 @@ impl HangTelemetry {
fn handle_poll(&mut self, poll: HangMonitorPoll) {
let flush = poll.reason == HangMonitorPollReason::Flush;
+ let active_time = poll.active_time;
+ for incident in &poll.incidents {
+ self.reporter.add_contributors(incident);
+ }
let incidents = serialize_incidents(self.startup, poll);
if !incidents.is_empty()
&& let Some(observe_incidents) = self.observe_incidents.as_mut()
@@ -127,6 +149,7 @@ impl HangTelemetry {
for incident in incidents {
self.reporter.add(incident);
}
+ self.reporter.active_time += active_time;
if flush || self.reporter.last_send.elapsed() > SEND_INTERVAL {
(self.send_event)(self.reporter.take_event().into_flexible_event());
}
@@ -150,10 +173,47 @@ fn serialize_incidents(startup: Instant, poll: HangMonitorPoll) -> Vec<Serialize
struct Reporter {
last_send: Instant,
pending: Vec<SerializedHangIncident>,
- threshold_incidents: u64,
- budget_incidents: u64,
- /// Every incident's stall, in milliseconds; `pending` keeps only the largest.
- stalls: Histogram<u64>,
+ incident_counts: IncidentCounts,
+ /// Every incident's stall, bucketed by [`STALL_BUCKETS_MS`]; `pending`
+ /// keeps only the largest incidents.
+ stall_buckets: StallBuckets,
+ stall_max_ms: u64,
+ /// See [`gpui::profiler::hang::HangDetector::take_active_time`].
+ active_time: Duration,
+ contributor_totals: HashMap<String, ContributorTotal>,
+ contributors_untracked: u64,
+}
+
+/// Incidents and those during active use, by trigger.
+type IncidentCounts = HashMap<HangTrigger, (u64, u64)>;
+
+/// One contributor's share of every incident since the last send.
+#[derive(Clone, Debug, Default, PartialEq, Serialize)]
+struct ContributorTotal {
+ /// See [`contributor_name`].
+ name: String,
+ /// Incidents it contributed to.
+ incidents: u64,
+ /// Its total duration across those incidents, in milliseconds. Nested
+ /// work, such as a draw inside an input dispatch, counts toward both,
+ /// except that views count only their own work, excluding nested views.
+ total_ms: f64,
+}
+
+/// What a contributor was, without timing: the action's name, where the task
+/// was spawned, or the kind of input or frame work.
+fn contributor_name(event: &ForegroundEvent) -> String {
+ match event {
+ ForegroundEvent::TaskPoll(timing) => {
+ format!("task:{}:{}", timing.location.file(), timing.location.line())
+ }
+ ForegroundEvent::Action(timing) => format!("action:{}", timing.name),
+ ForegroundEvent::Input(timing) => format!("input:{}", timing.kind),
+ ForegroundEvent::Draw(_) => "draw".to_string(),
+ ForegroundEvent::View(timing) => format!("view:{}:{}", timing.name, timing.phase.as_str()),
+ ForegroundEvent::Present(_) => "present".to_string(),
+ ForegroundEvent::SmallPolls(_) => "small_polls".to_string(),
+ }
}
impl Reporter {
@@ -161,18 +221,55 @@ impl Reporter {
Self {
last_send: Instant::now(),
pending: Vec::new(),
- threshold_incidents: 0,
- budget_incidents: 0,
- stalls: Histogram::new(3).expect("3 significant figures is a valid histogram"),
+ incident_counts: IncidentCounts::new(),
+ stall_buckets: StallBuckets::default(),
+ stall_max_ms: 0,
+ active_time: Duration::ZERO,
+ contributor_totals: HashMap::new(),
+ contributors_untracked: 0,
}
}
- fn add(&mut self, incident: SerializedHangIncident) {
- match incident.trigger {
- HangTrigger::Threshold => self.threshold_incidents += 1,
- HangTrigger::Budget => self.budget_incidents += 1,
+ fn add_contributors(&mut self, incident: &HangIncident) {
+ let mut counted = Vec::new();
+ for event in &incident.contributors {
+ let name = contributor_name(event);
+ if !self.contributor_totals.contains_key(&name)
+ && self.contributor_totals.len() >= MAX_TRACKED_CONTRIBUTORS
+ {
+ self.contributors_untracked += 1;
+ continue;
+ }
+ let total = self
+ .contributor_totals
+ .entry(name.clone())
+ .or_insert_with(|| ContributorTotal {
+ name: name.clone(),
+ ..ContributorTotal::default()
+ });
+ let duration = match event {
+ ForegroundEvent::View(timing) => timing.self_duration,
+ _ => event.duration(),
+ };
+ total.total_ms += duration.as_secs_f64() * 1000.0;
+ if !counted.contains(&name) {
+ total.incidents += 1;
+ counted.push(name);
+ }
}
- self.stalls.record(incident.stall_ms as u64).ok();
+ }
+
+ fn add(&mut self, incident: SerializedHangIncident) {
+ let (count, active_count) = self.incident_counts.entry(incident.trigger).or_default();
+ *count += 1;
+ *active_count += u64::from(incident.during_active_use);
+ let stall_ms = incident.stall_ms.max(0.0).ceil() as u64;
+ let bucket = STALL_BUCKETS_MS
+ .iter()
+ .position(|bound| stall_ms <= *bound)
+ .unwrap_or(STALL_BUCKETS_MS.len());
+ self.stall_buckets[bucket] += 1;
+ self.stall_max_ms = self.stall_max_ms.max(stall_ms);
self.pending.push(incident);
if self.pending.len() > MAX_REPORTED_INCIDENTS {
self.pending
@@ -187,59 +284,105 @@ impl Reporter {
self.last_send = now;
let mut incidents = std::mem::take(&mut self.pending);
incidents.sort_by(|first, second| second.stall_ms.total_cmp(&first.stall_ms));
- let threshold_incidents = std::mem::take(&mut self.threshold_incidents);
- let budget_incidents = std::mem::take(&mut self.budget_incidents);
+ let mut contributor_totals = std::mem::take(&mut self.contributor_totals)
+ .into_values()
+ .collect::<Vec<_>>();
+ contributor_totals.sort_by(|first, second| second.total_ms.total_cmp(&first.total_ms));
+ let contributor_totals_elided = contributor_totals
+ .len()
+ .saturating_sub(MAX_REPORTED_CONTRIBUTOR_TOTALS)
+ as u64
+ + std::mem::take(&mut self.contributors_untracked);
+ contributor_totals.truncate(MAX_REPORTED_CONTRIBUTOR_TOTALS);
+ for total in &mut contributor_totals {
+ total.total_ms = (total.total_ms * 1000.0).round() / 1000.0;
+ }
let event = HangIncidentsEvent {
incidents,
- total_incidents: threshold_incidents + budget_incidents,
- threshold_incidents,
- budget_incidents,
- stall_p50_ms: self.stalls.value_at_quantile(0.5),
- stall_p95_ms: self.stalls.value_at_quantile(0.95),
- stall_max_ms: self.stalls.max(),
+ incident_counts: std::mem::take(&mut self.incident_counts),
+ stall_buckets: std::mem::take(&mut self.stall_buckets),
+ stall_max_ms: std::mem::take(&mut self.stall_max_ms),
report_window_seconds,
+ active_seconds: std::mem::take(&mut self.active_time).as_secs(),
+ contributor_totals,
+ contributor_totals_elided,
};
- self.stalls.reset();
event
}
}
struct HangIncidentsEvent {
incidents: Vec<SerializedHangIncident>,
- /// Predates the threshold/budget split; existing queries key on it.
- total_incidents: u64,
- threshold_incidents: u64,
- budget_incidents: u64,
- stall_p50_ms: u64,
- stall_p95_ms: u64,
+ /// Reported as `total_incidents`, `{trigger}_incidents`, and
+ /// `active_{trigger}_incidents` (those during active use).
+ incident_counts: IncidentCounts,
+ stall_buckets: StallBuckets,
stall_max_ms: u64,
report_window_seconds: u64,
+ /// Of `report_window_seconds`, how long the user was actively using the
+ /// app (within a minute of a key press, click, or scroll), for rates such
+ /// as hangs per active hour.
+ active_seconds: u64,
+ /// Contributors summed across every incident, not only the reported ones,
+ /// longest total first.
+ contributor_totals: Vec<ContributorTotal>,
+ /// Contributors left out of `contributor_totals` by its cap, or untracked.
+ contributor_totals_elided: u64,
}
impl HangIncidentsEvent {
fn into_flexible_event(self) -> FlexibleEvent {
+ let mut event_properties = HashMap::from([
+ ("incidents".to_string(), to_value(&self.incidents)),
+ ("stall_max_ms".to_string(), self.stall_max_ms.into()),
+ (
+ "report_window_seconds".to_string(),
+ self.report_window_seconds.into(),
+ ),
+ ("active_seconds".to_string(), self.active_seconds.into()),
+ (
+ "contributor_totals".to_string(),
+ serde_json::to_value(&self.contributor_totals).unwrap_or_else(|error| {
+ log::error!("failed to serialize contributor totals: {error}");
+ Value::Null
+ }),
+ ),
+ (
+ "contributor_totals_elided".to_string(),
+ self.contributor_totals_elided.into(),
+ ),
+ (
+ "measurement_version".to_string(),
+ MEASUREMENT_VERSION.into(),
+ ),
+ ]);
+ let mut total_incidents = 0;
+ for trigger in HangTrigger::ALL {
+ let (count, active_count) = self
+ .incident_counts
+ .get(&trigger)
+ .copied()
+ .unwrap_or_default();
+ total_incidents += count;
+ let name = trigger.as_str();
+ event_properties.insert(format!("{name}_incidents"), count.into());
+ event_properties.insert(format!("active_{name}_incidents"), active_count.into());
+ }
+ // Predates the split by trigger; existing queries key on it.
+ event_properties.insert("total_incidents".to_string(), total_incidents.into());
+ for (index, count) in self.stall_buckets.iter().enumerate() {
+ let name = match STALL_BUCKETS_MS.get(index) {
+ Some(bound) => format!("stall_ms_le_{bound}"),
+ None => format!(
+ "stall_ms_gt_{}",
+ STALL_BUCKETS_MS.last().copied().unwrap_or_default()
+ ),
+ };
+ event_properties.insert(name, (*count).into());
+ }
FlexibleEvent {
event_type: EVENT_TYPE.to_string(),
- event_properties: HashMap::from([
- ("incidents".to_string(), to_value(&self.incidents)),
- ("total_incidents".to_string(), self.total_incidents.into()),
- (
- "threshold_incidents".to_string(),
- self.threshold_incidents.into(),
- ),
- ("budget_incidents".to_string(), self.budget_incidents.into()),
- ("stall_p50_ms".to_string(), self.stall_p50_ms.into()),
- ("stall_p95_ms".to_string(), self.stall_p95_ms.into()),
- ("stall_max_ms".to_string(), self.stall_max_ms.into()),
- (
- "report_window_seconds".to_string(),
- self.report_window_seconds.into(),
- ),
- (
- "measurement_version".to_string(),
- MEASUREMENT_VERSION.into(),
- ),
- ]),
+ event_properties,
}
}
}
@@ -259,13 +402,115 @@ mod tests {
use super::*;
use serde_json::json;
+ #[test]
+ fn reporter_sums_contributors_across_all_incidents() {
+ use gpui::profiler::{ActionTiming, journal::InputTiming};
+
+ let start = Instant::now();
+ let at = |ms: u64| start + Duration::from_millis(ms);
+ let action = |name: &'static str, start_ms: u64, end_ms: u64| {
+ ForegroundEvent::Action(ActionTiming {
+ name,
+ start: at(start_ms),
+ end: at(end_ms),
+ })
+ };
+ let mut reporter = Reporter::new();
+ for index in 0..30u64 {
+ let mut contributors = vec![
+ action("editor::Paste", index * 100, index * 100 + 40),
+ ForegroundEvent::Input(InputTiming {
+ kind: "key_down",
+ start: at(index * 100),
+ end: at(index * 100 + 41),
+ caused_invalidation: true,
+ }),
+ ];
+ if index == 0 {
+ // A name repeated within one incident counts as one incident.
+ contributors.push(action("editor::Paste", 50, 60));
+ }
+ reporter.add_contributors(&incident_with_contributors(contributors));
+ }
+ let event = reporter.take_event();
+
+ assert_eq!(
+ event.contributor_totals,
+ [
+ ContributorTotal {
+ name: "input:key_down".to_string(),
+ incidents: 30,
+ total_ms: 1230.0,
+ },
+ ContributorTotal {
+ name: "action:editor::Paste".to_string(),
+ incidents: 30,
+ total_ms: 1210.0,
+ },
+ ]
+ );
+ assert_eq!(event.contributor_totals_elided, 0);
+ assert!(reporter.take_event().contributor_totals.is_empty());
+ }
+
+ #[test]
+ fn reporter_totals_views_by_name_and_phase_using_their_own_time() {
+ use gpui::profiler::{ViewPhase, ViewTiming};
+
+ let start = Instant::now();
+ let mut reporter = Reporter::new();
+ reporter.add_contributors(&incident_with_contributors(vec![ForegroundEvent::View(
+ ViewTiming {
+ name: "markdown_preview::MarkdownPreviewView",
+ phase: ViewPhase::Prepaint,
+ start,
+ end: start + Duration::from_millis(12),
+ self_duration: Duration::from_millis(9),
+ },
+ )]));
+
+ assert_eq!(
+ reporter.take_event().contributor_totals,
+ [ContributorTotal {
+ name: "view:markdown_preview::MarkdownPreviewView:prepaint".to_string(),
+ incidents: 1,
+ total_ms: 9.0,
+ }]
+ );
+ }
+
+ #[test]
+ fn reporter_counts_incidents_during_active_use() {
+ let mut reporter = Reporter::new();
+ for (trigger, during_active_use) in [
+ (HangTrigger::Threshold, true),
+ (HangTrigger::Threshold, false),
+ (HangTrigger::LateFrame, true),
+ (HangTrigger::BlockingWork, false),
+ ] {
+ let mut incident = serialized_incident(120.0);
+ incident.trigger = trigger;
+ incident.during_active_use = during_active_use;
+ reporter.add(incident);
+ }
+ let event = reporter.take_event();
+ assert_eq!(
+ event.incident_counts,
+ HashMap::from([
+ (HangTrigger::Threshold, (2, 1)),
+ (HangTrigger::LateFrame, (1, 1)),
+ (HangTrigger::BlockingWork, (1, 0)),
+ ])
+ );
+ }
+
#[test]
fn reporter_keeps_largest_incidents_and_counts_all() {
let mut reporter = Reporter::new();
for stall_ms in 0..12 {
let mut incident = serialized_incident(f64::from(stall_ms));
if stall_ms % 2 == 0 {
- incident.trigger = HangTrigger::Budget;
+ incident.trigger = HangTrigger::LateFrame;
}
reporter.add(incident);
}
@@ -277,11 +522,14 @@ mod tests {
.map(|incident| incident.stall_ms)
.collect::<Vec<_>>();
- assert_eq!(event.total_incidents, 12);
- assert_eq!(event.threshold_incidents, 6);
- assert_eq!(event.budget_incidents, 6);
- assert_eq!(event.stall_p50_ms, 5);
- assert_eq!(event.stall_p95_ms, 11);
+ assert_eq!(
+ event.incident_counts,
+ HashMap::from([
+ (HangTrigger::Threshold, (6, 0)),
+ (HangTrigger::LateFrame, (6, 0)),
+ ])
+ );
+ assert_eq!(event.stall_buckets, [12, 0, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(event.stall_max_ms, 11);
assert_eq!(
stalls,
@@ -296,25 +544,22 @@ mod tests {
reporter.take_event();
let mut incident = serialized_incident(20.9);
- incident.trigger = HangTrigger::Budget;
+ incident.trigger = HangTrigger::LateFrame;
reporter.add(incident);
let event = reporter.take_event();
- assert_eq!(event.total_incidents, 1);
- assert_eq!(event.threshold_incidents, 0);
- assert_eq!(event.budget_incidents, 1);
+ assert_eq!(
+ event.incident_counts,
+ HashMap::from([(HangTrigger::LateFrame, (1, 0))])
+ );
assert_eq!(event.incidents[0].stall_ms, 20.9);
- assert_eq!(event.stall_p50_ms, 20);
- assert_eq!(event.stall_p95_ms, 20);
- assert_eq!(event.stall_max_ms, 20);
+ assert_eq!(event.stall_buckets, [1, 0, 0, 0, 0, 0, 0, 0, 0]);
+ assert_eq!(event.stall_max_ms, 21);
let empty = reporter.take_event();
assert!(empty.incidents.is_empty());
- assert_eq!(empty.total_incidents, 0);
- assert_eq!(empty.threshold_incidents, 0);
- assert_eq!(empty.budget_incidents, 0);
- assert_eq!(empty.stall_p50_ms, 0);
- assert_eq!(empty.stall_p95_ms, 0);
+ assert!(empty.incident_counts.is_empty());
+ assert_eq!(empty.stall_buckets, StallBuckets::default());
assert_eq!(empty.stall_max_ms, 0);
}
@@ -334,12 +579,26 @@ mod tests {
"incidents": [],
"total_incidents": 0,
"threshold_incidents": 0,
- "budget_incidents": 0,
- "stall_p50_ms": 0,
- "stall_p95_ms": 0,
+ "late_frame_incidents": 0,
+ "blocking_work_incidents": 0,
"stall_max_ms": 0,
+ "stall_ms_le_50": 0,
+ "stall_ms_le_100": 0,
+ "stall_ms_le_250": 0,
+ "stall_ms_le_500": 0,
+ "stall_ms_le_1000": 0,
+ "stall_ms_le_2000": 0,
+ "stall_ms_le_5000": 0,
+ "stall_ms_le_10000": 0,
+ "stall_ms_gt_10000": 0,
"report_window_seconds": report_window_seconds,
- "measurement_version": 2
+ "active_seconds": 0,
+ "active_threshold_incidents": 0,
+ "active_late_frame_incidents": 0,
+ "active_blocking_work_incidents": 0,
+ "contributor_totals": [],
+ "contributor_totals_elided": 0,
+ "measurement_version": 3
})
);
}
@@ -348,13 +607,21 @@ mod tests {
fn hang_incidents_event_uses_expected_wire_shape() {
let event = HangIncidentsEvent {
incidents: vec![serialized_incident(125.0)],
- total_incidents: 3,
- threshold_incidents: 2,
- budget_incidents: 1,
- stall_p50_ms: 25,
- stall_p95_ms: 125,
+ incident_counts: HashMap::from([
+ (HangTrigger::Threshold, (2, 1)),
+ (HangTrigger::LateFrame, (1, 1)),
+ (HangTrigger::BlockingWork, (1, 0)),
+ ]),
+ stall_buckets: [1, 0, 2, 0, 0, 0, 0, 0, 0],
stall_max_ms: 125,
report_window_seconds: 1800,
+ active_seconds: 900,
+ contributor_totals: vec![ContributorTotal {
+ name: "action:editor::Paste".to_string(),
+ incidents: 2,
+ total_ms: 250.5,
+ }],
+ contributor_totals_elided: 4,
}
.into_flexible_event();
@@ -363,9 +630,10 @@ mod tests {
serde_json::to_value(event.event_properties).unwrap(),
json!({
"incidents": [{
- "measurement_version": 2,
+ "measurement_version": 3,
"phase": "steady",
"trigger": "threshold",
+ "during_active_use": false,
"start_ms": 10.0,
"active_ms": 125.0,
"stall_ms": 125.0,
@@ -380,23 +648,59 @@ mod tests {
"contributors": [],
"contributors_elided": 0
}],
- "total_incidents": 3,
+ "total_incidents": 4,
"threshold_incidents": 2,
- "budget_incidents": 1,
- "stall_p50_ms": 25,
- "stall_p95_ms": 125,
+ "late_frame_incidents": 1,
+ "blocking_work_incidents": 1,
"stall_max_ms": 125,
+ "stall_ms_le_50": 1,
+ "stall_ms_le_100": 0,
+ "stall_ms_le_250": 2,
+ "stall_ms_le_500": 0,
+ "stall_ms_le_1000": 0,
+ "stall_ms_le_2000": 0,
+ "stall_ms_le_5000": 0,
+ "stall_ms_le_10000": 0,
+ "stall_ms_gt_10000": 0,
"report_window_seconds": 1800,
- "measurement_version": 2
+ "active_seconds": 900,
+ "active_threshold_incidents": 1,
+ "active_late_frame_incidents": 1,
+ "active_blocking_work_incidents": 0,
+ "contributor_totals": [{
+ "name": "action:editor::Paste",
+ "incidents": 2,
+ "total_ms": 250.5
+ }],
+ "contributor_totals_elided": 4,
+ "measurement_version": 3
})
);
}
+ fn incident_with_contributors(contributors: Vec<ForegroundEvent>) -> HangIncident {
+ let start = Instant::now();
+ HangIncident {
+ snapshot: gpui::profiler::journal::FrameSnapshot {
+ interval_start: start,
+ boundary: gpui::profiler::journal::IntervalBoundary::Idle { ended_at: start },
+ events: contributors.clone(),
+ small_polls: Vec::new(),
+ dropped_events: 0,
+ journal_discontinuous: false,
+ },
+ trigger: HangTrigger::Threshold,
+ contributors,
+ during_active_use: false,
+ }
+ }
+
fn serialized_incident(stall_ms: f64) -> SerializedHangIncident {
SerializedHangIncident {
measurement_version: MEASUREMENT_VERSION,
phase: "steady",
trigger: HangTrigger::Threshold,
+ during_active_use: false,
start_ms: 10.0,
active_ms: stall_ms,
stall_ms,
diff --git a/crates/input_latency_ui/Cargo.toml b/crates/input_latency_ui/Cargo.toml
index 792f8fe107c..b2bfe9b81bc 100644
--- a/crates/input_latency_ui/Cargo.toml
+++ b/crates/input_latency_ui/Cargo.toml
@@ -18,4 +18,6 @@ chrono.workspace = true
collections.workspace = true
gpui = { workspace = true, features = ["profiler"] }
hdrhistogram.workspace = true
+serde_json.workspace = true
+sysinfo.workspace = true
telemetry.workspace = true
diff --git a/crates/input_latency_ui/src/input_latency_ui.rs b/crates/input_latency_ui/src/input_latency_ui.rs
index dced1380392..1ef104d621b 100644
--- a/crates/input_latency_ui/src/input_latency_ui.rs
+++ b/crates/input_latency_ui/src/input_latency_ui.rs
@@ -1,7 +1,13 @@
use collections::{HashMap, HashSet};
-use gpui::{App, FrameDurationSnapshot, Global, InputLatencySnapshot, Window, WindowId, actions};
+use gpui::{
+ App, FRAME_DURATION_BUCKETS_MS, FRAME_WORK_BUCKETS_PERCENT, Global, InputLatencySnapshot,
+ RefreshIntervalFrames, Window, WindowId, actions, histogram_bucket_counts,
+};
use hdrhistogram::Histogram;
-use std::time::Instant;
+use std::{
+ sync::OnceLock,
+ time::{Duration, Instant},
+};
actions!(
dev,
@@ -140,158 +146,213 @@ pub fn report_input_latency_telemetry(window: &Window, cx: &mut App) {
state.previous.insert(window_id, (now, current));
- let frames_sub4 = count_frames_in_range(&delta_latency, 0, MS4_NS);
- let frames_4to8 = count_frames_in_range(&delta_latency, MS4_NS, MS8_NS);
- let frames_8to16 = count_frames_in_range(&delta_latency, MS8_NS, MS16_NS);
- let frames_16to33 = count_frames_in_range(&delta_latency, MS16_NS, MS33_NS);
- let frames_33to100 = count_frames_in_range(&delta_latency, MS33_NS, MS100_NS);
- // frames > 100 ms are implicitly total_frames - (sub4 + 4to8 + 8to16 + 16to33 + 33to100)
-
- let frames_with_1_event = count_frames_in_range(&delta_coalesce, 1, 2);
- let frames_with_2_events = count_frames_in_range(&delta_coalesce, 2, 3);
- let frames_with_3_events = count_frames_in_range(&delta_coalesce, 3, 4);
- // frames with 4+ events are implicitly total_frames - (1 + 2 + 3)
+ // Frames over 100 ms, or with 4+ events, are implied by total_frames.
+ let latency_counts =
+ histogram_bucket_counts(&delta_latency, [MS4_NS, MS8_NS, MS16_NS, MS33_NS, MS100_NS]);
+ let coalesce_counts = histogram_bucket_counts(&delta_coalesce, [1, 2, 3]);
+ let latency = |index: usize| latency_counts.get(index).copied().unwrap_or_default();
+ let coalesce = |index: usize| coalesce_counts.get(index).copied().unwrap_or_default();
telemetry::event!(
"Latency Report",
- frames_sub4 = frames_sub4,
- frames_4to8 = frames_4to8,
- frames_8to16 = frames_8to16,
- frames_16to33 = frames_16to33,
- frames_33to100 = frames_33to100,
+ frames_sub4 = latency(0),
+ frames_4to8 = latency(1),
+ frames_8to16 = latency(2),
+ frames_16to33 = latency(3),
+ frames_33to100 = latency(4),
total_frames = total_frames,
- frames_with_1_event = frames_with_1_event,
- frames_with_2_events = frames_with_2_events,
- frames_with_3_events = frames_with_3_events,
+ frames_with_1_event = coalesce(0),
+ frames_with_2_events = coalesce(1),
+ frames_with_3_events = coalesce(2),
report_window_seconds = report_window_seconds,
measurement_version = gpui::profiler::hang::MEASUREMENT_VERSION,
);
}
-/// Per-window baselines for frame-duration telemetry, keyed by window id. Kept
-/// separate from the input-latency baselines so the two reports never share
-/// state.
+/// Per-window, per-refresh-interval baselines for frame-duration telemetry.
+/// Kept separate from the input-latency baselines so the two reports never
+/// share state.
#[derive(Default)]
struct FrameDurationTelemetryState {
- previous: HashMap<WindowId, (Instant, FrameDurationSnapshot)>,
+ previous: HashMap<(WindowId, Option<Duration>), (Instant, RefreshIntervalFrames)>,
}
impl Global for FrameDurationTelemetryState {}
-/// Nanosecond boundaries for the present-interval buckets used in telemetry:
-/// roughly the 120Hz, 60Hz, and 30Hz frame budgets, with headroom for jitter.
-const MS9_NS: u64 = 9_000_000;
-const MS18_NS: u64 = 18_000_000;
-const MS36_NS: u64 = 36_000_000;
-
-/// Minimum number of draws that must be present in the delta window for the
-/// frame-duration report to be sent. Avoids sending noise for windows that are
-/// mostly idle.
+/// Minimum number of draws at one refresh interval since its last report for
+/// a frame-duration report to be sent. Avoids sending noise for windows that
+/// are mostly idle.
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.
+/// Sends a `Frame Duration Report` telemetry event for each refresh interval
+/// the given window drew enough frames at since its last report, so every
+/// report describes frames on displays with one refresh rate.
///
-/// 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.
+/// Each report contains frame pacing (frames on time and late, missed
+/// refreshes, and work per frame as a percentage of the refresh interval), the
+/// same for frames that responded to input, and bucketed draw durations.
+/// Buckets rather than percentiles, so reports can be summed.
///
/// 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 snapshot = window.frame_duration_snapshot();
let window_handle = window.window_handle();
let window_id = window_handle.window_id();
+ let gpu = window.gpu_specs();
let open_window_ids = open_window_ids(cx);
let state = cx.default_global::<FrameDurationTelemetryState>();
state
.previous
- .retain(|window_id, _| open_window_ids.contains(window_id));
+ .retain(|(window_id, _), _| open_window_ids.contains(window_id));
let now = Instant::now();
- 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
- .subtract(&prev_snapshot.draw_duration_histogram)
- .ok();
- let mut delta_intervals = current.present_interval_histogram.clone();
- 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,
- 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
- // the window has been open, so record 0 to signal that this is the
- // initial accumulation period rather than a fixed-width window.
- (
- current.draw_duration_histogram.clone(),
- current.present_interval_histogram.clone(),
- current.dirty_to_present_histogram.clone(),
- 0u64,
- )
+ for current in snapshot.by_refresh_interval {
+ let key = (window_id, current.refresh_interval);
+ let (frames, report_window_seconds) = match state.previous.get(&key) {
+ Some((previous_instant, previous)) => (
+ current.since(previous),
+ now.duration_since(*previous_instant).as_secs(),
+ ),
+ // First report for this window and refresh interval: we don't know
+ // how long it has been accumulating, so record 0 to signal that
+ // this isn't a fixed-width window.
+ None => (current.clone(), 0),
};
+ if frames.frames_drawn < MIN_DRAWS_TO_REPORT {
+ continue;
+ }
+ state.previous.insert(key, (now, current));
- let total_draws = delta_draws.len();
- if total_draws < MIN_DRAWS_TO_REPORT {
- return;
+ let mut properties = std::collections::HashMap::from_iter([
+ (
+ "refresh_interval_ms".to_string(),
+ serde_json::json!(
+ frames
+ .refresh_interval
+ .map(|interval| interval.as_secs_f64() * 1000.0)
+ ),
+ ),
+ (
+ "refresh_rate_hz".to_string(),
+ serde_json::json!(
+ frames
+ .refresh_interval
+ .map(|interval| (1.0 / interval.as_secs_f64()).round() as u64)
+ ),
+ ),
+ ("frames_drawn".to_string(), frames.frames_drawn.into()),
+ (
+ "input_frames_on_time".to_string(),
+ frames.input_frames_on_time.into(),
+ ),
+ (
+ "input_frames_late".to_string(),
+ frames.input_frames_late.into(),
+ ),
+ ("frames_on_time".to_string(), frames.frames_on_time.into()),
+ ("frames_late".to_string(), frames.frames_late.into()),
+ (
+ "missed_refreshes".to_string(),
+ frames.missed_refreshes.into(),
+ ),
+ (
+ "frames_unmeasured".to_string(),
+ frames.frames_unmeasured.into(),
+ ),
+ (
+ "gpu_name".to_string(),
+ serde_json::json!(gpu.as_ref().map(|gpu| gpu.device_name.clone())),
+ ),
+ (
+ "gpu_is_software_emulated".to_string(),
+ serde_json::json!(gpu.as_ref().map(|gpu| gpu.is_software_emulated)),
+ ),
+ ("cpu_name".to_string(), serde_json::json!(cpu_name())),
+ (
+ "root_entity_type_name".to_string(),
+ serde_json::json!(window_handle.root_entity_type_name()),
+ ),
+ (
+ "report_window_seconds".to_string(),
+ report_window_seconds.into(),
+ ),
+ (
+ "measurement_version".to_string(),
+ gpui::profiler::hang::MEASUREMENT_VERSION.into(),
+ ),
+ ]);
+ // Work buckets are relative to the refresh interval, so frames on
+ // displays with an unknown interval have none.
+ if let Some(refresh_interval) = frames.refresh_interval {
+ let work_bounds = FRAME_WORK_BUCKETS_PERCENT.map(|percent| {
+ u64::try_from(refresh_interval.as_nanos() * u128::from(percent) / 100)
+ .unwrap_or(u64::MAX)
+ });
+ for (prefix, work) in [
+ ("work_pct", &frames.work),
+ ("input_work_pct", &frames.input_work),
+ ] {
+ let counts = histogram_bucket_counts(work, work_bounds);
+ insert_buckets(
+ &mut properties,
+ prefix,
+ &FRAME_WORK_BUCKETS_PERCENT,
+ &counts,
+ );
+ }
+ }
+ let draw_counts = histogram_bucket_counts(
+ &frames.draw_duration,
+ FRAME_DURATION_BUCKETS_MS.map(|milliseconds| milliseconds * 1_000_000),
+ );
+ insert_buckets(
+ &mut properties,
+ "draw_ms",
+ &FRAME_DURATION_BUCKETS_MS,
+ &draw_counts,
+ );
+ telemetry::send_event(telemetry::Event {
+ event_type: "Frame Duration Report".to_string(),
+ event_properties: properties,
+ });
}
+}
- state.previous.insert(window_id, (now, current));
-
- let draws_sub4 = count_frames_in_range(&delta_draws, 0, MS4_NS);
- let draws_4to8 = count_frames_in_range(&delta_draws, MS4_NS, MS8_NS);
- let draws_8to16 = count_frames_in_range(&delta_draws, MS8_NS, MS16_NS);
- let draws_16to33 = count_frames_in_range(&delta_draws, MS16_NS, MS33_NS);
- // draws > 33ms are implicitly total_draws - (sub4 + 4to8 + 8to16 + 16to33)
-
- let total_intervals = delta_intervals.len();
- let intervals_sub9 = count_frames_in_range(&delta_intervals, 0, MS9_NS);
- let intervals_9to18 = count_frames_in_range(&delta_intervals, MS9_NS, MS18_NS);
- 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_present_interval_ms = delta_intervals.mean() / 1_000_000.0;
- let average_dirty_to_present_ms = delta_dirty_to_present.mean() / 1_000_000.0;
+/// Inserts bucket counts as `{prefix}_le_{bound}` properties, plus
+/// `{prefix}_gt_{last bound}` for the final, unbounded bucket.
+fn insert_buckets(
+ properties: &mut std::collections::HashMap<String, serde_json::Value>,
+ prefix: &str,
+ upper_bounds: &[u64],
+ counts: &[u64],
+) {
+ for (index, count) in counts.iter().enumerate() {
+ let name = match upper_bounds.get(index) {
+ Some(bound) => format!("{prefix}_le_{bound}"),
+ None => format!(
+ "{prefix}_gt_{}",
+ upper_bounds.last().copied().unwrap_or_default()
+ ),
+ };
+ properties.insert(name, (*count).into());
+ }
+}
- telemetry::event!(
- "Frame Duration Report",
- draws_sub4 = draws_sub4,
- draws_4to8 = draws_4to8,
- draws_8to16 = draws_8to16,
- draws_16to33 = draws_16to33,
- total_draws = total_draws,
- intervals_sub9 = intervals_sub9,
- intervals_9to18 = intervals_9to18,
- intervals_18to36 = intervals_18to36,
- intervals_36to100 = intervals_36to100,
- total_intervals = total_intervals,
- average_present_interval_ms = average_present_interval_ms,
- average_dirty_to_present_ms = average_dirty_to_present_ms,
- dirty_to_present_samples = delta_dirty_to_present.len(),
- dirty_to_present_p50_ms =
- delta_dirty_to_present.value_at_quantile(0.5) as f64 / 1_000_000.0,
- dirty_to_present_p95_ms =
- delta_dirty_to_present.value_at_quantile(0.95) as f64 / 1_000_000.0,
- dirty_to_present_max_ms = delta_dirty_to_present.max() as f64 / 1_000_000.0,
- root_entity_type_name = window_handle.root_entity_type_name(),
- report_window_seconds = report_window_seconds,
- measurement_version = gpui::profiler::hang::MEASUREMENT_VERSION,
- );
+fn cpu_name() -> Option<&'static str> {
+ static CPU_NAME: OnceLock<Option<String>> = OnceLock::new();
+ CPU_NAME
+ .get_or_init(|| {
+ let system = sysinfo::System::new_with_specifics(
+ sysinfo::RefreshKind::nothing().with_cpu(sysinfo::CpuRefreshKind::nothing()),
+ );
+ system
+ .cpus()
+ .first()
+ .map(|cpu| cpu.brand().trim().to_string())
+ .filter(|brand| !brand.is_empty())
+ })
+ .as_deref()
}
fn open_window_ids(cx: &App) -> HashSet<WindowId> {
@@ -301,14 +362,6 @@ fn open_window_ids(cx: &App) -> HashSet<WindowId> {
.collect()
}
-fn count_frames_in_range(histogram: &Histogram<u64>, low_ns: u64, high_ns: u64) -> u64 {
- histogram
- .iter_recorded()
- .filter(|v| v.value_iterated_to() >= low_ns && v.value_iterated_to() < high_ns)
- .map(|v| v.count_at_value())
- .sum()
-}
-
fn format_report(data: &InputLatencyReportData) -> String {
let snapshot = &data.snapshot;
let histogram = &snapshot.latency_histogram;