mod analysis;
mod calls;
pub(crate) mod capture;
mod flame;
mod sidebar;
mod timeline;
mod toolbar;
use std::{sync::Arc, time::Duration};
use self::{
analysis::{AnalysisProps, AnalysisState, ProfilerAnalysis},
capture::{FRAME_BUDGET_MS, ProfilerCapture, TimeRange, format_span},
sidebar::DetailTab,
};
use super::{
changes,
kit::{FILL, line},
};
use crate::{
app::{
ctx::{Ctx, Interval},
profiler::{
ProfileReport, ProfilingHandle, RecordingOptions, ScreenshotOptions, SessionId, SessionStatus, SpanTrack,
},
theme::{PaletteColor, RadiusSize},
},
components::{Column, Row},
core::Signal,
layout::{Alignment, layout_kind::Justify},
node::Element,
ui::{EmptyState, Length, ScrollArea, icons},
};
pub(crate) const STATUS_INTERVAL: Duration = Duration::from_millis(250);
const OVERVIEW_BUCKETS: usize = 120;
const FRAMES_LEAST_HEIGHT: f32 = 120.0;
const ANALYSIS_LEAST_HEIGHT: f32 = 220.0;
const EMPTY_WIDTH: f32 = 440.0;
#[cfg_attr(target_os = "ios", allow(dead_code))]
pub(crate) const LOAD_WHILE_RECORDING: &str = "Load skipped: a recording is running; Stop it, then Load again";
#[derive(Clone, Default)]
pub(crate) struct ProfilerState {
recording: Option<Recording>,
include_devtools: bool,
detailed: bool,
screenshots: bool,
report: Option<Arc<ProfileReport>>,
capture: Option<Arc<ProfilerCapture>>,
range: Option<TimeRange>,
range_anchor: Option<usize>,
range_dragging: bool,
selected: Option<u64>,
hovered_screenshot: Option<usize>,
error: Option<String>,
}
#[derive(Clone)]
struct Recording {
handle: ProfilingHandle,
id: SessionId,
status: Option<SessionStatus>,
}
impl crate::app::component::DevtoolsInspectable for ProfilerState {
fn write_info(&self, buffer: &mut Vec<crate::app::component::ComponentInfo>) {
buffer.push(crate::app::component::ComponentInfo::with_value(
"recording",
std::any::type_name::<bool>(),
self.recording.is_some().to_string(),
));
buffer.push(crate::app::component::ComponentInfo::with_value(
"passes",
std::any::type_name::<usize>(),
self
.capture
.as_ref()
.map_or(0, |capture| capture.passes.len())
.to_string(),
));
}
}
impl ProfilerState {
pub(crate) fn recording(&self) -> bool {
self.recording.is_some()
}
pub(crate) fn start(&mut self, handle: &ProfilingHandle) -> bool {
if self.recording.is_some() {
return true;
}
let options = RecordingOptions {
include_devtools: self.include_devtools,
detailed: self.detailed,
screenshots: self.screenshots.then(ScreenshotOptions::default),
..RecordingOptions::default()
};
match handle.start_recording(options) {
Ok(started) => {
*self = Self {
recording: Some(Recording {
handle: handle.clone(),
id: started.id,
status: handle.status(started.id).ok(),
}),
include_devtools: self.include_devtools,
detailed: self.detailed,
screenshots: self.screenshots,
..Self::default()
};
true
}
Err(error) => {
self.error = Some(error.to_string());
false
}
}
}
pub(crate) fn stop(&mut self) {
let Some(recording) = self.recording.take() else {
return;
};
match recording.handle.end(recording.id) {
Ok(report) => {
self.error = None;
self.show(report);
}
Err(error) => self.error = Some(error.to_string()),
}
}
#[cfg_attr(target_os = "ios", allow(dead_code))]
pub(crate) fn load(&mut self, file: Result<ProfileReport, String>) {
if self.recording.is_some() {
self.error = Some(LOAD_WHILE_RECORDING.to_owned());
return;
}
match file {
Ok(report) => {
*self = Self {
include_devtools: self.include_devtools,
detailed: self.detailed,
screenshots: self.screenshots,
..Self::default()
};
self.show(report);
}
Err(error) => self.error = Some(error),
}
}
fn show(&mut self, report: ProfileReport) {
let capture = ProfilerCapture::from_report(&report);
let range = capture.default_range();
self.selected = capture.slowest_in(range);
self.range = Some(range);
self.capture = Some(Arc::new(capture));
self.report = Some(Arc::new(report));
}
pub(crate) fn refresh_status(&mut self) {
if let Some(recording) = &mut self.recording {
recording.status = recording.handle.status(recording.id).ok();
}
}
pub(crate) fn clear(&mut self) {
if let Some(recording) = self.recording.take() {
let _ = recording.handle.end(recording.id);
}
*self = Self {
include_devtools: self.include_devtools,
detailed: self.detailed,
screenshots: self.screenshots,
..Self::default()
};
}
fn press_bucket(&mut self, bucket: usize, extend: bool) {
let anchor = match (extend, self.range_anchor) {
(true, Some(anchor)) => anchor,
_ => bucket,
};
self.range_anchor = Some(anchor);
self.range_dragging = true;
self.select_buckets(anchor, bucket);
}
fn drag_bucket(&mut self, bucket: usize) {
if let (true, Some(anchor)) = (self.range_dragging, self.range_anchor) {
self.select_buckets(anchor, bucket);
}
}
fn drag_changes_range(&self, bucket: usize) -> bool {
match (self.range_dragging, self.range_anchor, &self.capture) {
(true, Some(anchor), Some(capture)) => self.range != Some(capture.bucket_range(anchor, bucket, OVERVIEW_BUCKETS)),
_ => false,
}
}
fn select_buckets(&mut self, first: usize, last: usize) {
if let Some(capture) = &self.capture {
let range = capture.bucket_range(first, last, OVERVIEW_BUCKETS);
self.set_range(range);
}
}
fn set_range(&mut self, range: TimeRange) {
let Some(capture) = &self.capture else {
return;
};
let selected_in_range = self.selected.is_some_and(|sequence| {
capture
.passes_in(range)
.iter()
.any(|sample| sample.sequence == sequence)
});
if !selected_in_range {
self.selected = capture.slowest_in(range);
}
self.range = Some(range);
}
fn show_whole(&mut self) {
if let Some(whole) = self.capture.as_ref().map(|capture| capture.whole()) {
self.range_anchor = None;
self.set_range(whole);
}
}
fn select(&mut self, sequence: u64) {
self.selected = Some(sequence);
}
}
#[derive(Clone)]
pub(crate) struct ProfilerTab {
pub(crate) state: Signal<ProfilerState>,
ticker: Interval,
detail: Signal<DetailTab>,
analysis: Signal<AnalysisState>,
picked: Signal<Option<&'static str>>,
}
impl ProfilerTab {
pub(crate) fn new(ctx: &mut Ctx) -> Self {
let state = ctx.signal(ProfilerState::default());
let ticker = ctx.create_interval(STATUS_INTERVAL, {
let state = state.clone();
move || state.update(ProfilerState::refresh_status)
});
Self {
state,
ticker,
detail: ctx.signal(DetailTab::default()),
analysis: ctx.signal(AnalysisState::default()),
picked: ctx.signal(None),
}
}
pub(crate) fn close(&self) {
self.ticker.stop();
self.state.update(ProfilerState::clear);
}
fn select_pass(state: &Signal<ProfilerState>, sequence: u64) {
if state.with_untracked(|state| state.selected != Some(sequence)) {
state.update(|state| state.select(sequence));
}
}
}
pub(super) fn view(ctx: &mut Ctx, tab: &ProfilerTab, handle: Option<ProfilingHandle>) -> Element {
let state = tab.state.get();
let body = body(ctx, tab, &state);
Column::new()
.size(FILL, FILL)
.child(toolbar::toolbar(&state, tab, handle))
.child(Column::new().width(FILL).flex(1.0).border_top(line()).child(body))
.into()
}
fn body(ctx: &mut Ctx, tab: &ProfilerTab, state: &ProfilerState) -> Element {
if state.recording() {
return changes::centred(
EmptyState::new("Recording")
.description("Use the inspected app, then Stop recording to see every pass it made.")
.media(recording_mark())
.bordered(false)
.width(EMPTY_WIDTH)
.id("devtools-profiler-recording")
.into(),
);
}
let Some(capture) = state.capture.clone() else {
return changes::centred(
EmptyState::new("No recording")
.description(
"Start recording, use the inspected app, then Stop: the timeline shows every pass of its windows in between.",
)
.icon(icons::PULSE)
.bordered(false)
.width(EMPTY_WIDTH)
.id("devtools-profiler-empty")
.into(),
);
};
let range = state.range.unwrap_or_else(|| capture.default_range());
let analysis = ctx.mount::<ProfilerAnalysis>(AnalysisProps {
capture: capture.clone(),
range,
selected: state.selected,
state: tab.analysis.clone(),
picked: tab.picked.clone(),
on_select_pass: Some(Arc::new({
let state = tab.state.clone();
move |sequence| ProfilerTab::select_pass(&state, sequence)
})),
});
let main = Column::new()
.height(FILL)
.flex(1.0)
.min_width(0.0)
.border_right(line())
.child(timeline::recording(&capture, range, &tab.state))
.child(
Column::new()
.width(FILL)
.flex_shrink(1.0)
.min_height(FRAMES_LEAST_HEIGHT)
.child(
ScrollArea::new()
.label("Frames")
.height(Length::Fill)
.child(timeline::frames(&capture, range, state, &tab.state)),
),
)
.child(
Column::new()
.width(FILL)
.flex_full(1.0, 0.0, Some(ANALYSIS_LEAST_HEIGHT))
.child(analysis),
);
Row::new()
.size(FILL, FILL)
.child(main)
.child(sidebar::sidebar(&capture, state, tab))
.into()
}
fn recording_mark() -> Element {
Row::new()
.size(40.0, 40.0)
.align_items(Alignment::Center)
.justify(Justify::Center)
.rounded(RadiusSize::Lg)
.background(PaletteColor::Muted)
.child(
Row::new()
.size(10.0, 10.0)
.rounded(RadiusSize::Full)
.background(PaletteColor::Destructive),
)
.into()
}
fn passes(count: usize) -> String {
match count {
1 => "1 pass".to_owned(),
count => format!("{count} passes"),
}
}
fn format_offset(capture: &ProfilerCapture, at: f64) -> String {
format!("+{}", format_span(at - capture.started_ms))
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum Budget {
Within,
Over,
OverTwice,
}
impl Budget {
pub(crate) fn of(ms: f64) -> Self {
if ms > FRAME_BUDGET_MS * 2.0 {
Self::OverTwice
} else if ms > FRAME_BUDGET_MS {
Self::Over
} else {
Self::Within
}
}
pub(crate) fn of_latency(ms: f64) -> Self {
if ms > FRAME_BUDGET_MS * 6.0 {
Self::OverTwice
} else if ms > FRAME_BUDGET_MS * 3.0 {
Self::Over
} else {
Self::Within
}
}
fn color(self, within: PaletteColor) -> PaletteColor {
match self {
Self::Within => within,
Self::Over => PaletteColor::Warning,
Self::OverTwice => PaletteColor::Destructive,
}
}
}
fn phase_color(name: &str) -> Option<PaletteColor> {
Some(match name {
crate::app::profiler::PASS_SPAN_NAME => PaletteColor::MutedForeground,
"pass_setup" | "pass_notifications" | "ui_rebuild" | "ui_commit" | "input_dispatch" => PaletteColor::Chart1,
"layout_update" | "layout_compute" | "component_after_layout" => PaletteColor::Chart2,
"quad_resolve" | "glyph_rasterize" | "canvas_recording" | "canvas_preparation" => PaletteColor::Chart3,
name if name.starts_with("render") || name.starts_with("canvas_") => PaletteColor::Chart4,
_ => return None,
})
}
fn span_color(name: &str, track: SpanTrack) -> PaletteColor {
match track {
SpanTrack::Frames => phase_color(name).unwrap_or(PaletteColor::Chart1),
SpanTrack::Application(_) => PaletteColor::Chart5,
}
}
#[cfg(test)]
mod tests;