BlueGit

ict rusty-teams

Files

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

frame_log.rs

Initial commit
metis Daniel Däschle · pushed by metis · 7 weeks ago
06e0ae51 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().create(true).append(true).open(path) {
45 let _ = writeln!(file, "{line}");
46 }
47}
48
49pub fn probe(position: &'static str) -> impl IntoElement {
50 canvas(
51 move |_, _, _| {
52 if let Some(statistics) = statistics()
53 && position == "first"
54 {
55 statistics.lock().unwrap().prepaint_at = Some(Instant::now());
56 }
57 },
58 move |_, _, window, _| {
59 let Some(statistics) = statistics() else {
60 return;
61 };
62 if position != "last" {
63 return;
64 }
65 let mut statistics = statistics.lock().unwrap();
66 let now = Instant::now();
67 if !statistics.gpu_reported {
68 statistics.gpu_reported = true;
69 let line = format!("gpu {:?}", window.gpu_specs());
70 append(&statistics.path, &line);
71 }
72 if let Some(last) = statistics.last_draw {
73 statistics.longest_interval = statistics.longest_interval.max(now - last);
74 }
75 statistics.last_draw = Some(now);
76 if let Some(prepaint_at) = statistics.prepaint_at.take() {
77 let span = now - prepaint_at;
78 statistics.total_span += span;
79 statistics.longest_span = statistics.longest_span.max(span);
80 }
81 statistics.draws += 1;
82 let elapsed = now - statistics.window_start;
83 if elapsed >= REPORT_INTERVAL {
84 let line = format!(
85 "draws {} in {:.2}s | longest gap {:.1} ms | prepaint-to-paint mean {:.2} ms max {:.2} ms",
86 statistics.draws,
87 elapsed.as_secs_f64(),
88 statistics.longest_interval.as_secs_f64() * 1000.,
89 statistics.total_span.as_secs_f64() * 1000. / statistics.draws as f64,
90 statistics.longest_span.as_secs_f64() * 1000.,
91 );
92 append(&statistics.path, &line);
93 statistics.window_start = now;
94 statistics.draws = 0;
95 statistics.longest_interval = Duration::ZERO;
96 statistics.total_span = Duration::ZERO;
97 statistics.longest_span = Duration::ZERO;
98 }
99 },
100 )
101 .absolute()
102 .size_0()
103}