crcbl-ui 0.2.0

Crucible immediate-mode GUI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! The budget row compares CPU active time with the measured GPU elapsed span.
//! GPU elapsed includes scheduling gaps and contention, not just active work;
//! the larger-span classification is a diagnostic, not a throughput guarantee.
//!
//! Topic 40's first debug-panel row, and the reason it is
//! first: "'GPU-bound' is the first question and nothing answers it today". The
//! two numbers come from opposite ends of the engine — the CPU one from the
//! frame span `crcbl_core::trace` records, the GPU one from
//! `crcbl_render::timing`'s per-pass timestamps — and neither of those knows
//! about the other, so this is where they meet. It knows about neither in turn:
//! it takes nanoseconds.
//!
//! # The two numbers are about different frames, and the row does not pretend
//! otherwise
//!
//! The GPU report is **frames latent by design** — a ring of query sets resolved
//! only when a slot comes back round, so the newest GPU number is about a frame a
//! few frames ago, and topic 40 is explicit that no consumer
//! "fixes" that by stalling. So a row pairing *this* frame's CPU cost with *this*
//! frame's GPU cost would be stating something it cannot know.
//!
//! What it shows instead is two **distributions**: p50 and p95 over a rolling
//! window of [`DEFAULT_FRAME_WINDOW`] frames each. A window that long is not
//! moved by the two or three frames the ring lags — the samples in it are the
//! same frames give or take the offset — while a single-frame pairing would be
//! wrong by exactly that offset. The lag is shown rather than hidden: the row
//! carries the frame number the newest GPU sample came from, so a reader can
//! watch it trail the frame counter.
//!
//! # It reports nothing until it can report a percentile
//!
//! A p95 over five samples is the largest of the five, which is a maximum wearing
//! a percentile's name. [`MIN_PERCENTILE_SAMPLES`] is the point below which that
//! is true, so below it the row says how full the window is and nothing else.

use core::fmt;
use core::time::Duration;

use crate::debug::{DEFAULT_FRAME_WINDOW, DebugModule, DebugSection};

/// Samples a window needs before [`BudgetStats`] reports a percentile.
///
/// Re-exported rather than defined here: `crcbl bench` reports the same
/// percentiles over its own samples, and the rank arithmetic and the count it is
/// honest at are one piece of knowledge. [`crcbl_core::stats`] is where it
/// lives, and where its derivation is tested across every count below it.
pub use crcbl_core::stats::MIN_PERCENTILE_SAMPLES;

/// Which of the CPU and the GPU is the frame's budget.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Bound {
    /// Neither window holds [`MIN_PERCENTILE_SAMPLES`] yet.
    Unknown,
    /// The CPU p50 is the larger.
    Cpu,
    /// The GPU p50 is the larger.
    Gpu,
    /// The two p50s are equal.
    Tied,
}

impl Bound {
    /// How the row spells it.
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Unknown => "unknown",
            Self::Cpu => "cpu",
            Self::Gpu => "gpu",
            Self::Tied => "tied",
        }
    }
}

impl fmt::Display for Bound {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// One rolling window of per-frame nanosecond costs.
///
/// The window itself is [`crcbl_core::stats::Window`], which
/// `crcbl_render::pass_stats` also keeps one of per pass: the eviction, the
/// sort and the count below which a p95 is the maximum are one piece of
/// knowledge and this row is not the only reader of it.
type Window = crcbl_core::stats::Window<DEFAULT_FRAME_WINDOW>;

/// CPU active time against measured GPU elapsed time, over rolling windows.
///
/// Fed once a frame from both ends — [`BudgetStats::record_cpu`] with the frame
/// span's active duration, [`BudgetStats::record_gpu`] with the outer interval
/// the timers resolved — and read by the debug panel. See the
/// [module docs](self) for why the two are shown as distributions rather than
/// paired frame by frame.
#[derive(Clone, Debug, Default)]
pub struct BudgetStats {
    cpu: Window,
    gpu: Window,
    /// Which frame the newest GPU sample was about. Also the guard against
    /// counting one latent report several times: the ring hands the same
    /// `FrameTimings` back until a new slot resolves.
    gpu_frame: Option<u64>,
}

impl BudgetStats {
    /// Empty windows.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Records what this frame cost on the CPU.
    pub fn record_cpu(&mut self, cpu: Duration) {
        self.cpu.record(nanos_of(cpu));
    }

    /// Records what frame `frame` cost on the GPU, ignoring a report already
    /// recorded.
    ///
    /// Reports whether it was a new frame. The caller reads a *latent* report
    /// that answers with the same frame until the ring comes round, so counting
    /// every read would fill the window with duplicates of whatever the GPU
    /// happened to be slow at.
    pub fn record_gpu(&mut self, frame: u64, gpu: Duration) -> bool {
        if self.gpu_frame == Some(frame) {
            return false;
        }
        self.gpu_frame = Some(frame);
        self.gpu.record(nanos_of(gpu));
        true
    }

    /// Whether either window holds anything.
    ///
    /// What the overlay asks before adding the section: a run that never turned
    /// the trace on and has no GPU timers has nothing to say here, and three rows
    /// of `none` in every sample is clutter rather than information.
    #[must_use]
    pub fn has_samples(&self) -> bool {
        !self.cpu.is_empty() || !self.gpu.is_empty()
    }

    /// The CPU window's p50 and p95, or `None` below
    /// [`MIN_PERCENTILE_SAMPLES`].
    #[must_use]
    pub fn cpu(&self) -> Option<(Duration, Duration)> {
        self.cpu.percentiles().map(durations_of)
    }

    /// The GPU window's p50 and p95, or `None` below
    /// [`MIN_PERCENTILE_SAMPLES`].
    #[must_use]
    pub fn gpu(&self) -> Option<(Duration, Duration)> {
        self.gpu.percentiles().map(durations_of)
    }

    /// The frame the newest GPU sample came from, which trails the frame being
    /// drawn by the timers' ring latency.
    #[must_use]
    pub const fn gpu_frame(&self) -> Option<u64> {
        self.gpu_frame
    }

    /// Which of the two the frame is costing.
    ///
    /// **The rule, in full**: the budget is whichever side has the larger p50,
    /// and it is [`Bound::Unknown`] until *both* windows hold
    /// [`MIN_PERCENTILE_SAMPLES`]. p50 rather than p95 because the question is
    /// what a frame normally costs — a p95 comparison answers "which side stalls
    /// worse", which is a different and rarer question, and the p95s are on the
    /// row for whoever is asking it.
    #[must_use]
    pub fn bound(&self) -> Bound {
        let (Some((cpu, _)), Some((gpu, _))) = (self.cpu.percentiles(), self.gpu.percentiles())
        else {
            return Bound::Unknown;
        };
        match gpu.cmp(&cpu) {
            core::cmp::Ordering::Greater => Bound::Gpu,
            core::cmp::Ordering::Less => Bound::Cpu,
            core::cmp::Ordering::Equal => Bound::Tied,
        }
    }
}

/// A window's contribution to the row: its two percentiles, or why it has none.
fn write_window(out: &mut DebugSection, label: &str, window: &Window) {
    match window.percentiles() {
        Some((p50, p95)) => out.row(
            label,
            format_args!("{:.2} / {:.2} ms", millis(p50), millis(p95)),
        ),
        None if window.is_empty() => out.row_str(label, "none"),
        // Deliberately not a number: a p95 over this many samples is the
        // maximum, and printing it as a percentile is the lie this avoids.
        None => out.row(
            label,
            format_args!("filling {}/{}", window.len(), MIN_PERCENTILE_SAMPLES),
        ),
    }
}

fn millis(nanos: u64) -> f64 {
    nanos as f64 / 1.0e6
}

fn nanos_of(duration: Duration) -> u64 {
    u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX)
}

fn durations_of((p50, p95): (u64, u64)) -> (Duration, Duration) {
    (Duration::from_nanos(p50), Duration::from_nanos(p95))
}

/// The row itself.
///
/// Labels are deliberately long enough to be unique across the whole panel:
/// `DebugModule` labels share one namespace and nothing detects a collision, so
/// a bare `cpu` beside the GPU section's per-pass labels would be a row a reader
/// — or a test searching the draw list — could pick up as the wrong one.
impl DebugModule for BudgetStats {
    fn debug_section(&self, out: &mut DebugSection) {
        out.set_title("budget");
        write_window(out, "cpu p50/p95", &self.cpu);
        write_window(out, "gpu elapsed p50/p95", &self.gpu);
        out.row_str("bound", self.bound().as_str());
        match self.gpu_frame {
            Some(frame) => out.row("gpu frame", format_args!("{frame}")),
            None => out.row_str("gpu frame", "pending"),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn ms(millis: u64) -> Duration {
        Duration::from_millis(millis)
    }

    /// Fills a window with `n` samples of `millis` each.
    fn fill(stats: &mut BudgetStats, n: usize, cpu: u64, gpu: u64) {
        for frame in 0..n {
            stats.record_cpu(ms(cpu));
            stats.record_gpu(frame as u64, ms(gpu));
        }
    }

    /// **Below the minimum there is no percentile**, and the row says which of
    /// the two reasons it has none.
    #[test]
    fn a_short_window_reports_how_full_it_is_rather_than_a_percentile() {
        let mut stats = BudgetStats::new();
        assert!(!stats.has_samples());
        assert_eq!(stats.cpu(), None);
        assert_eq!(stats.bound(), Bound::Unknown);

        assert_eq!(rendered(&stats, "cpu p50/p95"), "none");
        assert_eq!(rendered(&stats, "gpu frame"), "pending");

        fill(&mut stats, MIN_PERCENTILE_SAMPLES - 1, 4, 8);
        assert!(stats.has_samples());
        assert_eq!(stats.cpu(), None, "one sample short is still short");
        assert_eq!(stats.bound(), Bound::Unknown);
        assert_eq!(rendered(&stats, "cpu p50/p95"), "filling 19/20");
        assert_eq!(rendered(&stats, "bound"), "unknown");

        // The sample that completes the window is the one that produces a
        // number: the boundary, not somewhere near it.
        fill(&mut stats, 1, 4, 8);
        assert_eq!(stats.cpu(), Some((ms(4), ms(4))));
        assert_eq!(stats.bound(), Bound::Gpu);
    }

    /// **The budget rule, both ways round**, so an inverted comparison cannot
    /// pass by agreeing with itself.
    #[test]
    fn the_budget_is_whichever_side_has_the_larger_p50() {
        let mut gpu_bound = BudgetStats::new();
        fill(&mut gpu_bound, MIN_PERCENTILE_SAMPLES, 4, 9);
        assert_eq!(gpu_bound.cpu(), Some((ms(4), ms(4))));
        assert_eq!(gpu_bound.gpu(), Some((ms(9), ms(9))));
        assert_eq!(gpu_bound.bound(), Bound::Gpu);
        assert_eq!(gpu_bound.bound().to_string(), "gpu");

        let mut cpu_bound = BudgetStats::new();
        fill(&mut cpu_bound, MIN_PERCENTILE_SAMPLES, 9, 4);
        assert_eq!(cpu_bound.bound(), Bound::Cpu);

        let mut tied = BudgetStats::new();
        fill(&mut tied, MIN_PERCENTILE_SAMPLES, 5, 5);
        assert_eq!(tied.bound(), Bound::Tied);

        // One side alone is not an answer, however many samples it has.
        let mut half = BudgetStats::new();
        for _ in 0..MIN_PERCENTILE_SAMPLES {
            half.record_cpu(ms(4));
        }
        assert!(half.cpu().is_some());
        assert_eq!(half.gpu(), None);
        assert_eq!(half.bound(), Bound::Unknown);
    }

    /// **The latent GPU report is counted once**, not once per frame it is read.
    ///
    /// The ring hands back the same `FrameTimings` until a slot comes round, so a
    /// window that took every read would be a window of duplicates — and a
    /// stalled ring would look like a steady frame rate.
    #[test]
    fn re_reading_the_same_gpu_frame_does_not_fill_the_window() {
        let mut stats = BudgetStats::new();
        assert!(stats.record_gpu(7, ms(3)), "a frame not seen before");
        assert!(!stats.record_gpu(7, ms(3)), "the same frame again");
        assert!(!stats.record_gpu(7, ms(99)), "even at a different cost");
        assert_eq!(stats.gpu.len(), 1);
        assert_eq!(stats.gpu_frame(), Some(7));

        assert!(stats.record_gpu(8, ms(3)));
        assert_eq!(stats.gpu.len(), 2);
        assert_eq!(stats.gpu_frame(), Some(8));
    }

    /// The window evicts at its bound, so a stall that has scrolled off stops
    /// counting — and the bound is the frame row's, so the two rows cover the
    /// same frames.
    #[test]
    fn the_window_holds_the_frame_rows_worth_of_frames_and_then_evicts() {
        let mut stats = BudgetStats::new();
        // One enormous frame, then a full window of small ones on top of it.
        stats.record_cpu(ms(500));
        for _ in 0..DEFAULT_FRAME_WINDOW {
            stats.record_cpu(ms(4));
        }
        assert_eq!(stats.cpu.len(), DEFAULT_FRAME_WINDOW);
        assert_eq!(
            stats.cpu(),
            Some((ms(4), ms(4))),
            "the 500 ms frame has scrolled out of the window"
        );
    }

    /// **The row's text changes when the numbers do.** A row that rendered a
    /// constant would pass a test that only checked the row existed.
    #[test]
    fn the_row_renders_the_numbers_it_was_given() {
        let mut stats = BudgetStats::new();
        fill(&mut stats, MIN_PERCENTILE_SAMPLES, 4, 9);
        let mut section = DebugSection::default();
        stats.debug_section(&mut section);
        assert_eq!(section.title(), "budget");
        assert_eq!(rendered(&stats, "cpu p50/p95"), "4.00 / 4.00 ms");
        assert_eq!(rendered(&stats, "gpu elapsed p50/p95"), "9.00 / 9.00 ms");
        assert_eq!(rendered(&stats, "bound"), "gpu");
        assert_eq!(
            rendered(&stats, "gpu frame"),
            (MIN_PERCENTILE_SAMPLES - 1).to_string(),
        );

        // Different numbers in, different strings out — including the bound.
        let mut other = BudgetStats::new();
        for frame in 0..MIN_PERCENTILE_SAMPLES {
            other.record_cpu(ms(20));
            other.record_gpu(frame as u64, ms(1));
        }
        assert_eq!(rendered(&other, "cpu p50/p95"), "20.00 / 20.00 ms");
        assert_eq!(rendered(&other, "gpu elapsed p50/p95"), "1.00 / 1.00 ms");
        assert_eq!(rendered(&other, "bound"), "cpu");
    }

    /// A spread window, so the two percentiles are actually different numbers
    /// and the row is not just printing one value twice.
    #[test]
    fn the_two_percentiles_are_read_off_the_same_window_separately() {
        let mut stats = BudgetStats::new();
        // 1..=20 milliseconds: p50 is the 10th, p95 the 19th.
        for value in 1..=MIN_PERCENTILE_SAMPLES as u64 {
            stats.record_cpu(ms(value));
        }
        assert_eq!(stats.cpu(), Some((ms(10), ms(19))));
        assert_eq!(rendered(&stats, "cpu p50/p95"), "10.00 / 19.00 ms");
    }

    /// Every label this section writes, so a rename is visible and a collision
    /// with another module's row is something a reader can check against this
    /// list.
    #[test]
    fn the_sections_labels_are_its_own() {
        let mut section = DebugSection::default();
        BudgetStats::new().debug_section(&mut section);
        let labels: Vec<&str> = section
            .rows()
            .iter()
            .map(|row| row.label.as_str())
            .collect();
        assert_eq!(
            labels,
            ["cpu p50/p95", "gpu elapsed p50/p95", "bound", "gpu frame"],
        );
    }

    /// One row's value, by label, off a section written from scratch — the way
    /// the panel writes one, which clears before every `debug_section`.
    fn rendered(stats: &BudgetStats, label: &str) -> String {
        let mut section = DebugSection::default();
        stats.debug_section(&mut section);
        section
            .rows()
            .iter()
            .find(|row| row.label == label)
            .unwrap_or_else(|| panic!("no {label:?} row in {:?}", section.rows()))
            .value
            .clone()
    }
}