Skip to main content

euv_performance/observability/
impl.rs

1use super::*;
2
3/// Inherent implementation of [`ObservabilityCoverage`].
4impl ObservabilityCoverage {
5    /// Returns the optional diagnostic reason explaining missing/unsupported coverage.
6    ///
7    /// # Arguments
8    ///
9    /// - `&Self` - Coverage item reference.
10    ///
11    /// # Returns
12    ///
13    /// - `Option<&str>` - Reason text when present.
14    #[must_use]
15    pub fn reason_value(&self) -> Option<&str> {
16        let Self { reason, .. } = self;
17        reason.as_deref()
18    }
19}
20
21/// Inherent implementation of [`ObservabilityMetric`].
22impl ObservabilityMetric {
23    /// Creates a measured common scalar metric conforming to `schemas/report-v1.schema.json`.
24    ///
25    /// # Arguments
26    ///
27    /// - `&str` - Canonical catalog metric ID.
28    /// - `&str` - Coverage scope name.
29    /// - `ObservabilityUnit` - Canonical metric unit.
30    /// - `&str` - Sampling window label.
31    /// - `ObservabilityStatistic` - Summary statistic tag.
32    /// - `f64` - Measured scalar value.
33    ///
34    /// # Returns
35    ///
36    /// - `Self` - Measured common metric entry.
37    #[must_use]
38    pub fn measured_common(
39        id: &str,
40        scope: &str,
41        unit: ObservabilityUnit,
42        window: &str,
43        statistic: ObservabilityStatistic,
44        value: f64,
45    ) -> Self {
46        Self {
47            id: id.to_string(),
48            scope: scope.to_string(),
49            unit,
50            status: ObservabilityMetricStatus::Measured,
51            timing_source: None,
52            upload_strategy: None,
53            window: window.to_string(),
54            statistic,
55            value: Some(value),
56            baseline: None,
57            after_drain: None,
58            slope_per_cycle: None,
59            samples: None,
60            source: Some(OBS_SOURCE_EUV_PERF.to_string()),
61            clock: Some(OBS_CLOCK_MONOTONIC.to_string()),
62            sample_rate_hz: None,
63            reason: None,
64            evidence_refs: Vec::new(),
65            threshold: None,
66        }
67    }
68
69    /// Creates a measured leak/lifecycle metric carrying explicit `baseline`, `after_drain`, and `slope_per_cycle`.
70    ///
71    /// # Arguments
72    ///
73    /// - `&str` - Canonical catalog leak metric ID.
74    /// - `&str` - Coverage scope name.
75    /// - `ObservabilityUnit` - Canonical metric unit.
76    /// - `&str` - Sampling window label.
77    /// - `f64` - Post-warmup baseline value.
78    /// - `f64` - Post-drain value.
79    /// - `f64` - Linear slope per lifecycle cycle.
80    ///
81    /// # Returns
82    ///
83    /// - `Self` - Measured leak metric entry.
84    #[must_use]
85    pub fn measured_leak(
86        id: &str,
87        scope: &str,
88        unit: ObservabilityUnit,
89        window: &str,
90        baseline: f64,
91        after_drain: f64,
92        slope_per_cycle: f64,
93    ) -> Self {
94        Self {
95            id: id.to_string(),
96            scope: scope.to_string(),
97            unit,
98            status: ObservabilityMetricStatus::Measured,
99            timing_source: None,
100            upload_strategy: None,
101            window: window.to_string(),
102            statistic: ObservabilityStatistic::LiveCount,
103            value: None,
104            baseline: Some(baseline),
105            after_drain: Some(after_drain),
106            slope_per_cycle: Some(slope_per_cycle),
107            samples: None,
108            source: Some(OBS_SOURCE_REACTIVE_ARENA.to_string()),
109            clock: Some(OBS_CLOCK_MONOTONIC.to_string()),
110            sample_rate_hz: None,
111            reason: None,
112            evidence_refs: Vec::new(),
113            threshold: None,
114        }
115    }
116
117    /// Creates a proxy common scalar metric explicitly marked `status = "proxy"` and `timing_source = "submission_proxy"`.
118    ///
119    /// # Arguments
120    ///
121    /// - `&str` - Canonical catalog metric ID.
122    /// - `&str` - Coverage scope name.
123    /// - `ObservabilityUnit` - Canonical metric unit.
124    /// - `&str` - Sampling window label.
125    /// - `ObservabilityStatistic` - Summary statistic tag.
126    /// - `f64` - Proxy scalar value.
127    ///
128    /// # Returns
129    ///
130    /// - `Self` - Proxy common metric entry.
131    #[must_use]
132    pub fn proxy_common(
133        id: &str,
134        scope: &str,
135        unit: ObservabilityUnit,
136        window: &str,
137        statistic: ObservabilityStatistic,
138        value: f64,
139    ) -> Self {
140        Self {
141            id: id.to_string(),
142            scope: scope.to_string(),
143            unit,
144            status: ObservabilityMetricStatus::Proxy,
145            timing_source: Some(OBS_TIMING_SOURCE_SUBMISSION_PROXY.to_string()),
146            upload_strategy: None,
147            window: window.to_string(),
148            statistic,
149            value: Some(value),
150            baseline: None,
151            after_drain: None,
152            slope_per_cycle: None,
153            samples: None,
154            source: Some(OBS_SOURCE_EUV_PERF.to_string()),
155            clock: Some(OBS_CLOCK_MONOTONIC.to_string()),
156            sample_rate_hz: None,
157            reason: None,
158            evidence_refs: Vec::new(),
159            threshold: None,
160        }
161    }
162
163    /// Creates an explicitly unobservable metric with `value = None` (`null` in JSON) and a required diagnostic reason (never faking zero).
164    ///
165    /// # Arguments
166    ///
167    /// - `&str` - Canonical catalog metric ID.
168    /// - `&str` - Coverage scope name.
169    /// - `ObservabilityUnit` - Canonical metric unit.
170    /// - `&str` - Sampling window label.
171    /// - `ObservabilityStatistic` - Summary statistic tag.
172    /// - `&str` - Non-empty reason explaining why the metric is unobservable.
173    ///
174    /// # Returns
175    ///
176    /// - `Self` - Truthful unobservable metric entry.
177    #[must_use]
178    pub fn unobservable(
179        id: &str,
180        scope: &str,
181        unit: ObservabilityUnit,
182        window: &str,
183        statistic: ObservabilityStatistic,
184        reason: &str,
185    ) -> Self {
186        Self {
187            id: id.to_string(),
188            scope: scope.to_string(),
189            unit,
190            status: ObservabilityMetricStatus::Unobservable,
191            timing_source: Some(OBS_TIMING_SOURCE_UNOBSERVABLE.to_string()),
192            upload_strategy: None,
193            window: window.to_string(),
194            statistic,
195            value: None,
196            baseline: None,
197            after_drain: None,
198            slope_per_cycle: None,
199            samples: None,
200            source: Some(OBS_SOURCE_EUV_PERF.to_string()),
201            clock: None,
202            sample_rate_hz: None,
203            reason: Some(reason.to_string()),
204            evidence_refs: Vec::new(),
205            threshold: None,
206        }
207    }
208
209    /// Attaches an explicit presentation timing source tag to this metric entry.
210    ///
211    /// # Arguments
212    ///
213    /// - `Self` - Metric builder.
214    /// - `&str` - Timing source label (`compositor_measured`, `submission_proxy`, or `unobservable`).
215    ///
216    /// # Returns
217    ///
218    /// - `Self` - Updated metric entry.
219    #[must_use]
220    pub fn with_timing_source(mut self, source: &str) -> Self {
221        let Self {
222            timing_source: slot,
223            ..
224        } = &mut self;
225        *slot = Some(source.to_string());
226        self
227    }
228
229    /// Attaches an explicit upload strategy tag to this metric entry.
230    ///
231    /// # Arguments
232    ///
233    /// - `Self` - Metric builder.
234    /// - `&str` - Upload strategy label (`direct_gpu`, `multi_rect`, `merged_bbox`, `full_frame`, or `none`).
235    ///
236    /// # Returns
237    ///
238    /// - `Self` - Updated metric entry.
239    #[must_use]
240    pub fn with_upload_strategy(mut self, strategy: &str) -> Self {
241        let Self {
242            upload_strategy: slot,
243            ..
244        } = &mut self;
245        *slot = Some(strategy.to_string());
246        self
247    }
248
249    /// Returns the optional timing source provenance label.
250    ///
251    /// # Arguments
252    ///
253    /// - `&Self` - Metric reference.
254    ///
255    /// # Returns
256    ///
257    /// - `Option<&str>` - Timing source label when set.
258    #[must_use]
259    pub fn timing_source_value(&self) -> Option<&str> {
260        let Self { timing_source, .. } = self;
261        timing_source.as_deref()
262    }
263
264    /// Returns the optional diagnostic reason explaining why this metric is unobservable or unmeasured.
265    ///
266    /// # Arguments
267    ///
268    /// - `&Self` - Metric reference.
269    ///
270    /// # Returns
271    ///
272    /// - `Option<&str>` - Reason text when present.
273    #[must_use]
274    pub fn reason_value(&self) -> Option<&str> {
275        let Self { reason, .. } = self;
276        reason.as_deref()
277    }
278
279    /// Attaches a sample count to this metric entry.
280    ///
281    /// # Arguments
282    ///
283    /// - `Self` - Metric builder.
284    /// - `u64` - Sample count collected during the window.
285    ///
286    /// # Returns
287    ///
288    /// - `Self` - Updated metric entry.
289    #[must_use]
290    pub fn with_samples(mut self, samples: u64) -> Self {
291        let Self { samples: slot, .. } = &mut self;
292        *slot = Some(samples);
293        self
294    }
295
296    /// Attaches a budget threshold to this metric entry.
297    ///
298    /// # Arguments
299    ///
300    /// - `Self` - Metric builder.
301    /// - `ObservabilityThreshold` - Budget threshold contract.
302    ///
303    /// # Returns
304    ///
305    /// - `Self` - Updated metric entry.
306    #[must_use]
307    pub fn with_threshold(mut self, threshold: ObservabilityThreshold) -> Self {
308        let Self {
309            threshold: slot, ..
310        } = &mut self;
311        *slot = Some(threshold);
312        self
313    }
314
315    /// Attaches linked evidence identifiers to this metric entry.
316    ///
317    /// # Arguments
318    ///
319    /// - `Self` - Metric builder.
320    /// - `Vec<String>` - Evidence reference IDs.
321    ///
322    /// # Returns
323    ///
324    /// - `Self` - Updated metric entry.
325    #[must_use]
326    pub fn with_evidence_refs(mut self, refs: Vec<String>) -> Self {
327        let Self { evidence_refs, .. } = &mut self;
328        *evidence_refs = refs;
329        self
330    }
331
332    /// Returns the measured scalar value when present.
333    ///
334    /// # Arguments
335    ///
336    /// - `&Self` - Metric reference.
337    ///
338    /// # Returns
339    ///
340    /// - `Option<f64>` - Scalar value.
341    #[must_use]
342    pub fn scalar_value(&self) -> Option<f64> {
343        let Self { value, .. } = *self;
344        value
345    }
346
347    /// Returns the baseline leak measurement when present.
348    ///
349    /// # Arguments
350    ///
351    /// - `&Self` - Metric reference.
352    ///
353    /// # Returns
354    ///
355    /// - `Option<f64>` - Baseline value.
356    #[must_use]
357    pub fn baseline_value(&self) -> Option<f64> {
358        let Self { baseline, .. } = *self;
359        baseline
360    }
361
362    /// Returns the post-drain leak measurement when present.
363    ///
364    /// # Arguments
365    ///
366    /// - `&Self` - Metric reference.
367    ///
368    /// # Returns
369    ///
370    /// - `Option<f64>` - Post-drain value.
371    #[must_use]
372    pub fn after_drain_value(&self) -> Option<f64> {
373        let Self { after_drain, .. } = *self;
374        after_drain
375    }
376
377    /// Returns the per-cycle leak slope when present.
378    ///
379    /// # Arguments
380    ///
381    /// - `&Self` - Metric reference.
382    ///
383    /// # Returns
384    ///
385    /// - `Option<f64>` - Slope per cycle.
386    #[must_use]
387    pub fn slope_per_cycle_value(&self) -> Option<f64> {
388        let Self {
389            slope_per_cycle, ..
390        } = *self;
391        slope_per_cycle
392    }
393
394    /// Returns `true` if any numeric sample field (`value`, `baseline`, `after_drain`, `slope_per_cycle`) is populated.
395    ///
396    /// # Arguments
397    ///
398    /// - `&Self` - Metric reference.
399    ///
400    /// # Returns
401    ///
402    /// - `bool` - Whether numeric measurements are present.
403    #[must_use]
404    pub fn has_any_numeric_value(&self) -> bool {
405        let Self {
406            value,
407            baseline,
408            after_drain,
409            slope_per_cycle,
410            ..
411        } = *self;
412        value.is_some() || baseline.is_some() || after_drain.is_some() || slope_per_cycle.is_some()
413    }
414}
415
416/// Inherent implementation of [`ObservabilityReportV1`].
417impl ObservabilityReportV1 {
418    /// Returns the optional adaptive render router snapshot attached to this report.
419    ///
420    /// # Arguments
421    ///
422    /// - `&Self` - Report reference.
423    ///
424    /// # Returns
425    ///
426    /// - `Option<AdaptiveRouterSnapshot>` - Attached adaptive router snapshot if present.
427    #[must_use]
428    pub fn adaptive_router_snapshot(&self) -> Option<AdaptiveRouterSnapshot> {
429        let Self {
430            adaptive_router, ..
431        } = *self;
432        adaptive_router
433    }
434}