Skip to main content

euv_core/app/telemetry/
impl.rs

1use super::*;
2
3/// Query and conversion helpers for `PresentOutcome`.
4impl PresentOutcome {
5    /// Returns `true` when this outcome represents a verified frame presentation.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Present outcome reference.
10    ///
11    /// # Returns
12    ///
13    /// - `bool` - `true` only for `PresentOutcome::Presented`.
14    #[must_use]
15    pub const fn is_presented(&self) -> bool {
16        matches!(*self, Self::Presented)
17    }
18
19    /// Returns `true` when the pending frame must be retried on the active surface generation.
20    ///
21    /// # Arguments
22    ///
23    /// - `&Self` - Present outcome reference.
24    ///
25    /// # Returns
26    ///
27    /// - `bool` - `true` only for `PresentOutcome::PresentFailedRetry`.
28    #[must_use]
29    pub const fn should_retry(&self) -> bool {
30        matches!(*self, Self::PresentFailedRetry)
31    }
32
33    /// Returns `true` when no frame was presented because of zero damage or bounded in-flight backpressure.
34    ///
35    /// # Arguments
36    ///
37    /// - `&Self` - Present outcome reference.
38    ///
39    /// # Returns
40    ///
41    /// - `bool` - `true` for `SkippedNoDamage` or `WaitInFlight`.
42    #[must_use]
43    pub const fn is_idle_or_waiting(&self) -> bool {
44        matches!(*self, Self::SkippedNoDamage | Self::WaitInFlight)
45    }
46
47    /// Maps this `PresentOutcome` to the legacy `FrameSubmitOutcome` classification.
48    ///
49    /// # Arguments
50    ///
51    /// - `&Self` - Present outcome reference.
52    ///
53    /// # Returns
54    ///
55    /// - `FrameSubmitOutcome` - Equivalent submission outcome.
56    #[must_use]
57    pub const fn to_submit_outcome(&self) -> FrameSubmitOutcome {
58        match *self {
59            Self::Presented => FrameSubmitOutcome::Submitted,
60            Self::SkippedNoDamage | Self::WaitInFlight => FrameSubmitOutcome::SkippedNoDamage,
61            Self::PresentFailedRetry => FrameSubmitOutcome::SubmitFailed,
62        }
63    }
64
65    /// Constructs a `PresentOutcome` from a legacy `FrameSubmitOutcome`.
66    ///
67    /// # Arguments
68    ///
69    /// - `FrameSubmitOutcome` - Submission outcome to convert.
70    ///
71    /// # Returns
72    ///
73    /// - `Self` - Corresponding `PresentOutcome`.
74    #[must_use]
75    pub const fn from_submit_outcome(outcome: FrameSubmitOutcome) -> Self {
76        match outcome {
77            FrameSubmitOutcome::Submitted => Self::Presented,
78            FrameSubmitOutcome::SkippedNoDamage => Self::SkippedNoDamage,
79            FrameSubmitOutcome::SubmitFailed => Self::PresentFailedRetry,
80        }
81    }
82}
83
84/// Lossless conversion from `FrameSubmitOutcome` into `PresentOutcome`.
85impl From<FrameSubmitOutcome> for PresentOutcome {
86    /// Converts `outcome` into its corresponding `PresentOutcome` variant.
87    ///
88    /// # Arguments
89    ///
90    /// - `FrameSubmitOutcome` - Legacy frame submission outcome.
91    ///
92    /// # Returns
93    ///
94    /// - `Self` - Equivalent `PresentOutcome`.
95    fn from(outcome: FrameSubmitOutcome) -> Self {
96        Self::from_submit_outcome(outcome)
97    }
98}
99
100/// Lossy conversion from `PresentOutcome` into `FrameSubmitOutcome`.
101impl From<PresentOutcome> for FrameSubmitOutcome {
102    /// Converts `outcome` into its corresponding `FrameSubmitOutcome` variant.
103    ///
104    /// # Arguments
105    ///
106    /// - `PresentOutcome` - Detailed present outcome.
107    ///
108    /// # Returns
109    ///
110    /// - `Self` - Equivalent `FrameSubmitOutcome`.
111    fn from(outcome: PresentOutcome) -> Self {
112        outcome.to_submit_outcome()
113    }
114}
115
116/// Stage stamping and elapsed-millisecond breakdown helpers for `StartupTelemetry`.
117impl StartupTelemetry {
118    /// Creates a new `StartupTelemetry` anchored at process boot timestamp `t0_process_boot_ns`.
119    ///
120    /// # Arguments
121    ///
122    /// - `u64` - Monotonic process boot timestamp in nanoseconds.
123    ///
124    /// # Returns
125    ///
126    /// - `Self` - Initialized telemetry record with `t0_process_boot_ns` stamped.
127    #[must_use]
128    pub const fn with_boot_ns(t0_process_boot_ns: u64) -> Self {
129        Self {
130            t0_process_boot_ns,
131            t1_runtime_ready_ns: STARTUP_TIMESTAMP_UNSET,
132            t2_font_ready_ns: STARTUP_TIMESTAMP_UNSET,
133            t3_mount_done_ns: STARTUP_TIMESTAMP_UNSET,
134            t4_layout_done_ns: STARTUP_TIMESTAMP_UNSET,
135            t5_paint_done_ns: STARTUP_TIMESTAMP_UNSET,
136            t6_gpu_submit_ns: STARTUP_TIMESTAMP_UNSET,
137            t7_first_present_ns: STARTUP_TIMESTAMP_UNSET,
138        }
139    }
140
141    /// Computes a non-negative elapsed duration in milliseconds between two monotonic nanosecond timestamps.
142    ///
143    /// # Arguments
144    ///
145    /// - `u64` - Earlier monotonic timestamp in nanoseconds.
146    /// - `u64` - Later monotonic timestamp in nanoseconds.
147    ///
148    /// # Returns
149    ///
150    /// - `Option<f64>` - Elapsed milliseconds when both timestamps are recorded (`!= 0`) and `end_ns >= start_ns`.
151    #[must_use]
152    pub fn elapsed_ms_between(start_ns: u64, end_ns: u64) -> Option<f64> {
153        if start_ns == STARTUP_TIMESTAMP_UNSET
154            || end_ns == STARTUP_TIMESTAMP_UNSET
155            || end_ns < start_ns
156        {
157            return None;
158        }
159        Some(((end_ns - start_ns) as f64) / TELEMETRY_NS_PER_MS)
160    }
161
162    /// Records the first-observed monotonic nanosecond timestamp for `stage` without overwriting an existing stamp.
163    ///
164    /// # Arguments
165    ///
166    /// - `&Self` - Mutable startup telemetry record.
167    /// - `StartupStage` - Milestone stage being stamped.
168    /// - `u64` - Monotonic timestamp in nanoseconds.
169    ///
170    /// # Returns
171    ///
172    /// - `bool` - `true` when the timestamp was recorded; `false` if `timestamp_ns == 0` or the stage was already stamped.
173    pub fn record_stage_once(&mut self, stage: StartupStage, timestamp_ns: u64) -> bool {
174        if timestamp_ns == STARTUP_TIMESTAMP_UNSET {
175            return false;
176        }
177        match stage {
178            StartupStage::ProcessBoot => {
179                if self.get_t0_process_boot_ns() != STARTUP_TIMESTAMP_UNSET {
180                    return false;
181                }
182                self.set_t0_process_boot_ns(timestamp_ns);
183            }
184            StartupStage::RuntimeReady => {
185                if self.get_t1_runtime_ready_ns() != STARTUP_TIMESTAMP_UNSET {
186                    return false;
187                }
188                self.set_t1_runtime_ready_ns(timestamp_ns);
189            }
190            StartupStage::FontReady => {
191                if self.get_t2_font_ready_ns() != STARTUP_TIMESTAMP_UNSET {
192                    return false;
193                }
194                self.set_t2_font_ready_ns(timestamp_ns);
195            }
196            StartupStage::MountDone => {
197                if self.get_t3_mount_done_ns() != STARTUP_TIMESTAMP_UNSET {
198                    return false;
199                }
200                self.set_t3_mount_done_ns(timestamp_ns);
201            }
202            StartupStage::LayoutDone => {
203                if self.get_t4_layout_done_ns() != STARTUP_TIMESTAMP_UNSET {
204                    return false;
205                }
206                self.set_t4_layout_done_ns(timestamp_ns);
207            }
208            StartupStage::PaintDone => {
209                if self.get_t5_paint_done_ns() != STARTUP_TIMESTAMP_UNSET {
210                    return false;
211                }
212                self.set_t5_paint_done_ns(timestamp_ns);
213            }
214            StartupStage::GpuSubmit => {
215                if self.get_t6_gpu_submit_ns() != STARTUP_TIMESTAMP_UNSET {
216                    return false;
217                }
218                self.set_t6_gpu_submit_ns(timestamp_ns);
219            }
220            StartupStage::FirstPresent => {
221                if self.get_t7_first_present_ns() != STARTUP_TIMESTAMP_UNSET {
222                    return false;
223                }
224                self.set_t7_first_present_ns(timestamp_ns);
225            }
226        }
227        true
228    }
229
230    /// Returns the recorded nanosecond timestamp for `stage`, if set.
231    ///
232    /// # Arguments
233    ///
234    /// - `&Self` - Startup telemetry reference.
235    /// - `StartupStage` - Milestone stage to read.
236    ///
237    /// # Returns
238    ///
239    /// - `Option<u64>` - Recorded timestamp in nanoseconds, or `None` when unset.
240    #[must_use]
241    pub fn stage_timestamp_ns(&self, stage: StartupStage) -> Option<u64> {
242        let raw: u64 = match stage {
243            StartupStage::ProcessBoot => self.get_t0_process_boot_ns(),
244            StartupStage::RuntimeReady => self.get_t1_runtime_ready_ns(),
245            StartupStage::FontReady => self.get_t2_font_ready_ns(),
246            StartupStage::MountDone => self.get_t3_mount_done_ns(),
247            StartupStage::LayoutDone => self.get_t4_layout_done_ns(),
248            StartupStage::PaintDone => self.get_t5_paint_done_ns(),
249            StartupStage::GpuSubmit => self.get_t6_gpu_submit_ns(),
250            StartupStage::FirstPresent => self.get_t7_first_present_ns(),
251        };
252        if raw == STARTUP_TIMESTAMP_UNSET {
253            None
254        } else {
255            Some(raw)
256        }
257    }
258
259    /// Returns `true` when both process boot (`t0`) and verified first present (`t7`) timestamps are recorded.
260    ///
261    /// # Arguments
262    ///
263    /// - `&Self` - Startup telemetry reference.
264    ///
265    /// # Returns
266    ///
267    /// - `bool` - `true` when full TTFF is available.
268    #[must_use]
269    pub fn is_complete(&self) -> bool {
270        self.get_t0_process_boot_ns() != STARTUP_TIMESTAMP_UNSET
271            && self.get_t7_first_present_ns() != STARTUP_TIMESTAMP_UNSET
272            && self.get_t7_first_present_ns() >= self.get_t0_process_boot_ns()
273    }
274
275    /// Returns total cold-start time-to-first-frame (`t7 - t0`) in milliseconds.
276    ///
277    /// # Arguments
278    ///
279    /// - `&Self` - Startup telemetry reference.
280    ///
281    /// # Returns
282    ///
283    /// - `Option<f64>` - Total TTFF in milliseconds.
284    #[must_use]
285    pub fn total_ttff_ms(&self) -> Option<f64> {
286        Self::elapsed_ms_between(
287            self.get_t0_process_boot_ns(),
288            self.get_t7_first_present_ns(),
289        )
290    }
291
292    /// Returns runtime and renderer initialization duration (`t1 - t0`) in milliseconds.
293    ///
294    /// # Arguments
295    ///
296    /// - `&Self` - Startup telemetry reference.
297    ///
298    /// # Returns
299    ///
300    /// - `Option<f64>` - Runtime ready duration in milliseconds.
301    #[must_use]
302    pub fn runtime_ready_ms(&self) -> Option<f64> {
303        Self::elapsed_ms_between(
304            self.get_t0_process_boot_ns(),
305            self.get_t1_runtime_ready_ns(),
306        )
307    }
308
309    /// Returns default font initialization duration (`t2 - t1`) in milliseconds.
310    ///
311    /// # Arguments
312    ///
313    /// - `&Self` - Startup telemetry reference.
314    ///
315    /// # Returns
316    ///
317    /// - `Option<f64>` - Font ready stage duration in milliseconds.
318    #[must_use]
319    pub fn font_ready_ms(&self) -> Option<f64> {
320        Self::elapsed_ms_between(self.get_t1_runtime_ready_ns(), self.get_t2_font_ready_ns())
321    }
322
323    /// Returns initial route scene mount duration (`t3 - t2`) in milliseconds.
324    ///
325    /// # Arguments
326    ///
327    /// - `&Self` - Startup telemetry reference.
328    ///
329    /// # Returns
330    ///
331    /// - `Option<f64>` - Initial mount duration in milliseconds.
332    #[must_use]
333    pub fn mount_ms(&self) -> Option<f64> {
334        Self::elapsed_ms_between(self.get_t2_font_ready_ns(), self.get_t3_mount_done_ns())
335    }
336
337    /// Returns initial layout pass duration (`t4 - t3`) in milliseconds.
338    ///
339    /// # Arguments
340    ///
341    /// - `&Self` - Startup telemetry reference.
342    ///
343    /// # Returns
344    ///
345    /// - `Option<f64>` - First layout pass duration in milliseconds.
346    #[must_use]
347    pub fn layout_ms(&self) -> Option<f64> {
348        Self::elapsed_ms_between(self.get_t3_mount_done_ns(), self.get_t4_layout_done_ns())
349    }
350
351    /// Returns initial display list paint and visible glyph upload duration (`t5 - t4`) in milliseconds.
352    ///
353    /// # Arguments
354    ///
355    /// - `&Self` - Startup telemetry reference.
356    ///
357    /// # Returns
358    ///
359    /// - `Option<f64>` - First paint stage duration in milliseconds.
360    #[must_use]
361    pub fn paint_ms(&self) -> Option<f64> {
362        Self::elapsed_ms_between(self.get_t4_layout_done_ns(), self.get_t5_paint_done_ns())
363    }
364
365    /// Returns first GPU/surface draw submission duration (`t6 - t5`) in milliseconds.
366    ///
367    /// # Arguments
368    ///
369    /// - `&Self` - Startup telemetry reference.
370    ///
371    /// # Returns
372    ///
373    /// - `Option<f64>` - First submit stage duration in milliseconds.
374    #[must_use]
375    pub fn gpu_submit_ms(&self) -> Option<f64> {
376        Self::elapsed_ms_between(self.get_t5_paint_done_ns(), self.get_t6_gpu_submit_ns())
377    }
378
379    /// Returns verified first presentation latency (`t7 - t6`) in milliseconds.
380    ///
381    /// # Arguments
382    ///
383    /// - `&Self` - Startup telemetry reference.
384    ///
385    /// # Returns
386    ///
387    /// - `Option<f64>` - First present stage duration in milliseconds.
388    #[must_use]
389    pub fn first_present_ms(&self) -> Option<f64> {
390        Self::elapsed_ms_between(self.get_t6_gpu_submit_ns(), self.get_t7_first_present_ns())
391    }
392
393    /// Returns cumulative elapsed milliseconds from `t0` to `stage`.
394    ///
395    /// # Arguments
396    ///
397    /// - `&Self` - Startup telemetry reference.
398    /// - `StartupStage` - Target milestone stage.
399    ///
400    /// # Returns
401    ///
402    /// - `Option<f64>` - Elapsed milliseconds since `t0_process_boot_ns`.
403    #[must_use]
404    pub fn elapsed_since_boot_ms(&self, stage: StartupStage) -> Option<f64> {
405        let stage_ns: u64 = self.stage_timestamp_ns(stage)?;
406        Self::elapsed_ms_between(self.get_t0_process_boot_ns(), stage_ns)
407    }
408}