BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / frame_log.rs

frame_log.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
frame_log.rs 3.6 KiB Download Raw
1use std::io::Write;
2use std::sync::Mutex;
3use std::sync::OnceLock;
4use std::time::{Duration, Instant};
5
6use gpui_kit::*;
7
8const ENVIRONMENT_VARIABLE: &str = "RUSTY_TEAMS_FRAME_LOG";
9const REPORT_INTERVAL: Duration = Duration::from_secs(1);
10
11struct FrameStatistics {
12 path: String,
13 window_start: Instant,
14 last_draw: Option<Instant>,
15 prepaint_at: Option<Instant>,
16 draws: u32,
17 longest_interval: Duration,
18 total_span: Duration,
19 longest_span: Duration,
20 gpu_reported: bool,
21}
22
23fn statistics() -> Option<&'static Mutex<FrameStatistics>> {
24 static STATISTICS: OnceLock<Option<Mutex<FrameStatistics>>> = OnceLock::new();
25 STATISTICS
26 .get_or_init(|| {
27 let path = std::env::var(ENVIRONMENT_VARIABLE).ok()?;
28 Some(Mutex::new(FrameStatistics {
29 path,
30 window_start: Instant::now(),
31 last_draw: None,
32 prepaint_at: None,
33 draws: 0,
34 longest_interval: Duration::ZERO,
35 total_span: Duration::ZERO,
36 longest_span: Duration::ZERO,
37 gpu_reported: false,
38 }))
39 })
40 .as_ref()
41}
42
43fn append(path: &str, line: &str) {
44 if let Ok(mut file) = std::fs::OpenOptions::new()
45 .create(true)
46 .append(true)
47 .open(path)
48 {
49 let _ = writeln!(file, "{line}");
50 }
51}
52
53pub fn probe(position: &'static str) -> impl IntoElement {
54 canvas(
55 move |_, _, _| {
56 if let Some(statistics) = statistics()
57 && position == "first"
58 {
59 statistics.lock().unwrap().prepaint_at = Some(Instant::now());
60 }
61 },
62 move |_, _, window, _| {
63 let Some(statistics) = statistics() else {
64 return;
65 };
66 if position != "last" {
67 return;
68 }
69 let mut statistics = statistics.lock().unwrap();
70 let now = Instant::now();
71 if !statistics.gpu_reported {
72 statistics.gpu_reported = true;
73 let line = format!("gpu {:?}", window.gpu_specs());
74 append(&statistics.path, &line);
75 }
76 if let Some(last) = statistics.last_draw {
77 statistics.longest_interval = statistics.longest_interval.max(now - last);
78 }
79 statistics.last_draw = Some(now);
80 if let Some(prepaint_at) = statistics.prepaint_at.take() {
81 let span = now - prepaint_at;
82 statistics.total_span += span;
83 statistics.longest_span = statistics.longest_span.max(span);
84 }
85 statistics.draws += 1;
86 let elapsed = now - statistics.window_start;
87 if elapsed >= REPORT_INTERVAL {
88 let line = format!(
89 "draws {} in {:.2}s | longest gap {:.1} ms | prepaint-to-paint mean {:.2} ms max {:.2} ms",
90 statistics.draws,
91 elapsed.as_secs_f64(),
92 statistics.longest_interval.as_secs_f64() * 1000.,
93 statistics.total_span.as_secs_f64() * 1000. / statistics.draws as f64,
94 statistics.longest_span.as_secs_f64() * 1000.,
95 );
96 append(&statistics.path, &line);
97 statistics.window_start = now;
98 statistics.draws = 0;
99 statistics.longest_interval = Duration::ZERO;
100 statistics.total_span = Duration::ZERO;
101 statistics.longest_span = Duration::ZERO;
102 }
103 },
104 )
105 .absolute()
106 .size_0()
107}