Repository navigation
Expand file tree
/
Copy pathrex3_gpu_telemetry.rs
More file actions
1279 lines (1206 loc) · 42.1 KB
/
Copy pathrex3_gpu_telemetry.rs
File metadata and controls
1279 lines (1206 loc) · 42.1 KB
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! GPU report accounting, independent of adapter initialization.
use std::sync::atomic::{AtomicU64, Ordering};
const BATCH_HISTOGRAM_BUCKETS: usize = 17;
static NEXT_COUNTER_EPOCH: AtomicU64 = AtomicU64::new(1);
macro_rules! subtract_counters {
($after:ident, $before:ident, $($field:ident),+ $(,)?) => {
$(
$after.$field = $after.$field.checked_sub($before.$field).ok_or_else(|| {
format!("{} counter decreased between snapshots", stringify!($field))
})?;
)+
};
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum TimestampSupport {
#[default]
Disabled,
Unsupported(&'static str),
Unavailable(String),
Enabled {
readback_transfer: bool,
},
}
#[derive(Clone, Debug, Default)]
pub struct ReplayReport {
/// Identifies one uninterrupted series of cumulative counters.
pub counter_epoch: u64,
pub handled_draws: u64,
pub fallback_draws: u64,
pub fallback_reasons: std::collections::BTreeMap<String, u64>,
pub submissions: u64,
pub fences: u64,
pub bytes_uploaded: u64,
pub bytes_read_back: u64,
pub pending_draws: usize,
pub error: Option<String>,
pub timestamp_error: Option<String>,
pub timestamp_support: TimestampSupport,
pub telemetry: Option<StageTelemetry>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum FlushReason {
BatchFull,
Idle,
WaitIdle,
DisplayFence,
Exit,
CpuFallback,
Explicit,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct FlushCounts {
pub batch_full: u64,
pub idle: u64,
pub wait_idle: u64,
pub display_fence: u64,
pub exit: u64,
pub cpu_fallback: u64,
pub explicit: u64,
}
impl FlushCounts {
pub(crate) fn record(&mut self, reason: FlushReason) {
let count = match reason {
FlushReason::BatchFull => &mut self.batch_full,
FlushReason::Idle => &mut self.idle,
FlushReason::WaitIdle => &mut self.wait_idle,
FlushReason::DisplayFence => &mut self.display_fence,
FlushReason::Exit => &mut self.exit,
FlushReason::CpuFallback => &mut self.cpu_fallback,
FlushReason::Explicit => &mut self.explicit,
};
*count += 1;
}
fn delta_since(&self, before: &Self) -> Result<Self, String> {
let mut delta = self.clone();
subtract_counters!(
delta,
before,
batch_full,
idle,
wait_idle,
display_fence,
exit,
cpu_fallback,
explicit,
);
Ok(delta)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct TimestampTotals {
pub count: u64,
pub nanoseconds: u64,
/// Samples discarded because the device counter wrapped or went backwards.
pub discarded: u64,
}
impl TimestampTotals {
fn delta_since(&self, before: &Self) -> Result<Self, String> {
let mut delta = self.clone();
subtract_counters!(delta, before, count, nanoseconds, discarded);
Ok(delta)
}
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct StageTelemetry {
pub match_attempts: u64,
pub sampled_draws: u64,
pub sampled_match_ns: u64,
pub sampled_enqueue_ns: u64,
pub upload_enqueue_ns: u64,
pub resource_creation_ns: u64,
pub command_encoding_ns: u64,
pub queue_submit_ns: u64,
pub device_poll_ns: u64,
pub device_wait_ns: u64,
pub map_request_ns: u64,
pub map_callback_wait_ns: u64,
pub cpu_readback_copy_ns: u64,
pub timestamp_readback_ns: u64,
pub timestamp_readback_bytes: u64,
pub parameter_bytes: u64,
pub batch_count: u64,
pub batch_draws: u64,
/// Bucket i contains batches of 2^i through 2^(i+1)-1 draws.
pub batch_histogram: [u64; BATCH_HISTOGRAM_BUCKETS],
pub flush_reasons: FlushCounts,
pub gpu_compute: Option<TimestampTotals>,
pub gpu_readback_transfer: Option<TimestampTotals>,
pub timestamp_submissions_skipped: u64,
}
impl StageTelemetry {
pub(crate) fn record_batch(&mut self, draws: usize, bytes_per_draw: usize) {
if draws == 0 {
return;
}
self.batch_count += 1;
self.batch_draws += draws as u64;
self.parameter_bytes += (draws * bytes_per_draw) as u64;
let bucket = (usize::BITS - 1 - draws.leading_zeros()) as usize;
self.batch_histogram[bucket.min(BATCH_HISTOGRAM_BUCKETS - 1)] += 1;
}
fn delta_since(&self, before: &Self) -> Result<Self, String> {
let mut delta = self.clone();
subtract_counters!(
delta,
before,
match_attempts,
sampled_draws,
sampled_match_ns,
sampled_enqueue_ns,
upload_enqueue_ns,
resource_creation_ns,
command_encoding_ns,
queue_submit_ns,
device_poll_ns,
device_wait_ns,
map_request_ns,
map_callback_wait_ns,
cpu_readback_copy_ns,
timestamp_readback_ns,
timestamp_readback_bytes,
parameter_bytes,
batch_count,
batch_draws,
timestamp_submissions_skipped,
);
for (bucket, previous) in delta.batch_histogram.iter_mut().zip(before.batch_histogram) {
*bucket = bucket
.checked_sub(previous)
.ok_or_else(|| "batch histogram decreased between snapshots".to_owned())?;
}
delta.flush_reasons = self.flush_reasons.delta_since(&before.flush_reasons)?;
delta.gpu_compute = timestamp_delta(&self.gpu_compute, &before.gpu_compute)?;
delta.gpu_readback_transfer =
timestamp_delta(&self.gpu_readback_transfer, &before.gpu_readback_transfer)?;
Ok(delta)
}
}
fn timestamp_delta(
after: &Option<TimestampTotals>,
before: &Option<TimestampTotals>,
) -> Result<Option<TimestampTotals>, String> {
match (after, before) {
(Some(after), Some(before)) => Ok(Some(after.delta_since(before)?)),
(None, None) => Ok(None),
_ => Err("timestamp availability changed between snapshots".into()),
}
}
impl ReplayReport {
pub(crate) fn fresh() -> Self {
Self {
counter_epoch: NEXT_COUNTER_EPOCH.fetch_add(1, Ordering::Relaxed),
..Self::default()
}
}
pub(crate) fn timestamp_collection_failed(&mut self, error: String) {
self.timestamp_error = Some(error.clone());
self.timestamp_support = TimestampSupport::Unavailable(error);
if let Some(telemetry) = &mut self.telemetry {
telemetry.gpu_compute = None;
telemetry.gpu_readback_transfer = None;
}
}
#[cfg(test)]
pub(crate) fn advance_counter_epoch(&mut self) {
self.counter_epoch = NEXT_COUNTER_EPOCH.fetch_add(1, Ordering::Relaxed);
}
pub fn delta_since(&self, before: &Self) -> Result<Self, String> {
if self.counter_epoch != before.counter_epoch {
return Err(
"report counters were reset, toggled, or belong to different engines".into(),
);
}
if self.timestamp_support != before.timestamp_support
|| self.timestamp_error != before.timestamp_error
{
return Err("timestamp support or error changed between snapshots".into());
}
let mut delta = self.clone();
subtract_counters!(
delta,
before,
handled_draws,
fallback_draws,
submissions,
fences,
bytes_uploaded,
bytes_read_back,
);
for (reason, previous) in &before.fallback_reasons {
let count = delta.fallback_reasons.get_mut(reason).ok_or_else(|| {
format!("fallback reason {reason:?} disappeared between snapshots")
})?;
*count = count
.checked_sub(*previous)
.ok_or_else(|| format!("fallback reason {reason:?} decreased between snapshots"))?;
}
delta.fallback_reasons.retain(|_, count| *count != 0);
delta.telemetry = match (&self.telemetry, &before.telemetry) {
(Some(after), Some(before)) => Some(after.delta_since(before)?),
(None, None) => None,
_ => return Err("telemetry was reset or toggled between snapshots".into()),
};
Ok(delta)
}
}
pub(crate) fn timestamp_duration_ns(
bytes: &[u8],
index: u32,
period: f32,
) -> Result<Option<u64>, String> {
if !period.is_finite() || period <= 0.0 {
return Err(format!("invalid GPU timestamp period {period}"));
}
let offset = index as usize * std::mem::size_of::<u64>();
let read = |offset: usize| -> Result<u64, String> {
let bytes = bytes
.get(offset..offset + 8)
.ok_or_else(|| "incomplete timestamp readback".to_owned())?;
Ok(u64::from_ne_bytes(bytes.try_into().unwrap()))
};
let start = read(offset)?;
let end = read(offset + 8)?;
let Some(ticks) = end.checked_sub(start) else {
return Ok(None);
};
let nanos = ticks as f64 * f64::from(period);
if !nanos.is_finite() || nanos >= u64::MAX as f64 {
return Err(format!("invalid GPU timestamp duration {nanos} ns"));
}
Ok(Some(nanos as u64))
}
pub(crate) fn record_timestamp(
total: &mut TimestampTotals,
duration: Option<u64>,
) -> Result<(), String> {
let Some(duration) = duration else {
total.discarded = total
.discarded
.checked_add(1)
.ok_or("discarded timestamp count overflow")?;
return Ok(());
};
let count = total
.count
.checked_add(1)
.ok_or("timestamp count overflow")?;
let nanoseconds = total
.nanoseconds
.checked_add(duration)
.ok_or("timestamp duration overflow")?;
total.count = count;
total.nanoseconds = nanoseconds;
Ok(())
}
#[cfg(test)]
pub(crate) fn require_usable_timestamps(
stage: &str,
total: &TimestampTotals,
) -> Result<(), String> {
if total.count != 0 {
return Ok(());
}
if total.discarded != 0 {
return Err(format!(
"INCONCLUSIVE: all {} {stage} timestamp samples were discarded",
total.discarded
));
}
Err(format!(
"INCONCLUSIVE: {stage} timestamps produced no usable samples"
))
}
#[cfg(test)]
const LIVE_AA_SAMPLE_MS: &str = "REX3_GPU_AA_SAMPLE_MS";
#[cfg(test)]
const LIVE_AA_MAX_ROUNDS: &str = "REX3_GPU_AA_MAX_ROUNDS";
#[cfg(test)]
const LIVE_AA_MIN_ROUNDS: usize = 16;
#[cfg(test)]
const LIVE_AA_ROUND_BATCH: usize = 8;
#[cfg(test)]
const LIVE_AA_SIGN_ALPHA: f64 = 1394.0 / 65536.0;
#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct LiveAaOptions {
pub(crate) sample_ms: u64,
pub(crate) max_rounds: usize,
}
#[cfg(test)]
impl LiveAaOptions {
pub(crate) fn from_env() -> Result<Self, String> {
fn read(name: &str) -> Result<Option<String>, String> {
match std::env::var(name) {
Ok(value) => Ok(Some(value)),
Err(std::env::VarError::NotPresent) => Ok(None),
Err(std::env::VarError::NotUnicode(_)) => {
Err(format!("{name}: expected a Unicode integer"))
}
}
}
let sample_ms = read(LIVE_AA_SAMPLE_MS)?;
let max_rounds = read(LIVE_AA_MAX_ROUNDS)?;
Self::parse(sample_ms.as_deref(), max_rounds.as_deref())
}
fn parse(sample_ms: Option<&str>, max_rounds: Option<&str>) -> Result<Self, String> {
fn integer(
name: &str,
value: Option<&str>,
default: u64,
min: u64,
max: u64,
) -> Result<u64, String> {
let Some(value) = value else {
return Ok(default);
};
let invalid = || format!("{name}={value:?}: expected an integer in {min}..={max}");
if value.is_empty() || !value.bytes().all(|byte| byte.is_ascii_digit()) {
return Err(invalid());
}
let parsed = value.parse::<u64>().map_err(|_| invalid())?;
if !(min..=max).contains(&parsed) {
return Err(invalid());
}
Ok(parsed)
}
let sample_ms = integer(LIVE_AA_SAMPLE_MS, sample_ms, 600, 100, 2000)?;
let max_rounds = integer(LIVE_AA_MAX_ROUNDS, max_rounds, 48, 16, 128)? as usize;
if max_rounds % LIVE_AA_ROUND_BATCH != 0 {
return Err(format!(
"{LIVE_AA_MAX_ROUNDS}={max_rounds}: expected a multiple of \
{LIVE_AA_ROUND_BATCH} in 16..=128"
));
}
Ok(Self {
sample_ms,
max_rounds,
})
}
pub(crate) fn is_look(&self, completed: usize) -> bool {
(LIVE_AA_MIN_ROUNDS..=self.max_rounds).contains(&completed)
&& (completed - LIVE_AA_MIN_ROUNDS) % LIVE_AA_ROUND_BATCH == 0
}
pub(crate) fn look_count(&self) -> usize {
(self.max_rounds - LIVE_AA_MIN_ROUNDS) / LIVE_AA_ROUND_BATCH + 1
}
pub(crate) fn look_number(&self, completed: usize) -> usize {
assert!(self.is_look(completed));
(completed - LIVE_AA_MIN_ROUNDS) / LIVE_AA_ROUND_BATCH + 1
}
pub(crate) fn per_look_alpha(&self) -> f64 {
LIVE_AA_SIGN_ALPHA / self.look_count() as f64
}
pub(crate) fn simultaneous_confidence(&self) -> f64 {
1.0 - LIVE_AA_SIGN_ALPHA
}
}
#[cfg(test)]
#[derive(Debug)]
pub(crate) struct LiveGateStats {
pub(crate) median: f64,
pub(crate) coefficient_of_variation: f64,
pub(crate) range_over_median: f64,
}
#[cfg(test)]
fn sorted_finite(values: &[f64]) -> Vec<f64> {
assert!(!values.is_empty() && values.iter().all(|value| value.is_finite()));
let mut sorted = values.to_vec();
sorted.sort_by(f64::total_cmp);
sorted
}
#[cfg(test)]
fn median_sorted(sorted: &[f64]) -> f64 {
let middle = sorted.len() / 2;
if sorted.len() % 2 == 0 {
(sorted[middle - 1] + sorted[middle]) / 2.0
} else {
sorted[middle]
}
}
#[cfg(test)]
pub(crate) fn live_gate_stats(values: &[f64]) -> LiveGateStats {
assert!(values.iter().all(|value| *value > 0.0));
let sorted = sorted_finite(values);
let median = median_sorted(&sorted);
let mean = values.iter().sum::<f64>() / values.len() as f64;
let variance = values
.iter()
.map(|value| (value - mean).powi(2))
.sum::<f64>()
/ values.len() as f64;
LiveGateStats {
median,
coefficient_of_variation: variance.sqrt() / mean,
range_over_median: (sorted[sorted.len() - 1] - sorted[0]) / median,
}
}
#[cfg(test)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct PairedRateStats {
/// Signed throughput change; negative means the second condition is slower.
pub(crate) median: f64,
pub(crate) lower: f64,
pub(crate) upper: f64,
pub(crate) median_absolute: f64,
pub(crate) half_shift: f64,
pub(crate) confidence: f64,
}
#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum OverheadDecision {
Pass,
Fail,
Inconclusive,
}
#[cfg(test)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum LiveAaStop {
Continue,
Pass,
FailSlowdown,
MaxRounds,
}
#[cfg(test)]
pub(crate) fn log_rate_ratio(first: f64, second: f64) -> f64 {
assert!(first.is_finite() && first > 0.0 && second.is_finite() && second > 0.0);
(second / first).ln()
}
#[cfg(test)]
pub(crate) fn abba_log_rate_ratio(rates: [f64; 4], times: [f64; 4], on_first: bool) -> f64 {
assert!(rates.iter().all(|rate| rate.is_finite() && *rate > 0.0));
assert!(times.iter().all(|time| time.is_finite()));
assert!(times.windows(2).all(|pair| pair[0] < pair[1]));
let inner_time = (times[1] + times[2]) / 2.0;
let weight = (inner_time - times[0]) / (times[3] - times[0]);
let outer = (1.0 - weight) * rates[0].ln() + weight * rates[3].ln();
let inner = (rates[1].ln() + rates[2].ln()) / 2.0;
if on_first {
outer - inner
} else {
inner - outer
}
}
#[cfg(test)]
pub(crate) fn paired_order_shift(log_ratios: &[f64], on_first: &[bool]) -> f64 {
assert_eq!(log_ratios.len(), on_first.len());
let mut first_on = Vec::new();
let mut first_off = Vec::new();
for (&ratio, &on) in log_ratios.iter().zip(on_first) {
if on {
first_on.push(ratio);
} else {
first_off.push(ratio);
}
}
let on = median_sorted(&sorted_finite(&first_on));
let off = median_sorted(&sorted_finite(&first_off));
(on - off).abs().exp_m1()
}
#[cfg(test)]
pub(crate) fn paired_rate_stats(log_ratios: &[f64]) -> PairedRateStats {
paired_rate_stats_with_alpha(log_ratios, 0.05)
}
#[cfg(test)]
pub(crate) fn paired_rate_stats_with_alpha(log_ratios: &[f64], alpha: f64) -> PairedRateStats {
assert!(alpha.is_finite() && alpha > 0.0 && alpha < 1.0);
let sorted = sorted_finite(log_ratios);
let count = sorted.len();
let mut probability = 2.0_f64.powf(-(count as f64));
let mut tail = 0.0;
let mut rank = None;
for index in 0..count / 2 {
let next = tail + probability;
if next > alpha / 2.0 {
break;
}
tail = next;
rank = Some(index);
probability *= (count - index) as f64 / (index + 1) as f64;
}
let rank = rank.expect("too few pairs for the requested median interval confidence");
let absolute: Vec<_> = log_ratios.iter().map(|ratio| ratio.abs()).collect();
let midpoint = count / 2;
let first = median_sorted(&sorted_finite(&log_ratios[..midpoint]));
let second = median_sorted(&sorted_finite(&log_ratios[midpoint..]));
PairedRateStats {
median: median_sorted(&sorted).exp_m1(),
lower: sorted[rank].exp_m1(),
upper: sorted[count - rank - 1].exp_m1(),
median_absolute: median_sorted(&sorted_finite(&absolute)).exp_m1(),
half_shift: (first - second).abs().exp_m1(),
confidence: 1.0 - 2.0 * tail,
}
}
#[cfg(test)]
pub(crate) fn overhead_decision(
gpu_aa: PairedRateStats,
telemetry: PairedRateStats,
order_shift: f64,
limit: f64,
) -> OverheadDecision {
assert!(limit.is_finite() && limit > 0.0);
assert!(order_shift.is_finite() && order_shift >= 0.0);
let usable_gpu_control = gpu_aa.lower <= 0.0
&& gpu_aa.upper >= 0.0
&& gpu_aa.lower > -limit
&& gpu_aa.upper < limit
&& gpu_aa.median_absolute < limit
&& gpu_aa.half_shift < limit;
if !usable_gpu_control || order_shift >= limit {
return OverheadDecision::Inconclusive;
}
if telemetry.lower > -limit && telemetry.upper < limit {
OverheadDecision::Pass
} else if telemetry.upper <= -limit {
OverheadDecision::Fail
} else {
OverheadDecision::Inconclusive
}
}
#[cfg(test)]
pub(crate) fn live_aa_stop(
options: &LiveAaOptions,
completed: usize,
gpu_aa: PairedRateStats,
telemetry: PairedRateStats,
order_shift: f64,
limit: f64,
) -> LiveAaStop {
assert!(options.is_look(completed));
match overhead_decision(gpu_aa, telemetry, order_shift, limit) {
OverheadDecision::Pass => LiveAaStop::Pass,
OverheadDecision::Fail => LiveAaStop::FailSlowdown,
OverheadDecision::Inconclusive if completed == options.max_rounds => LiveAaStop::MaxRounds,
OverheadDecision::Inconclusive => LiveAaStop::Continue,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn telemetry_deltas_account_for_batches_reasons_and_timestamps() {
let mut before = ReplayReport {
handled_draws: 8,
fallback_draws: 1,
submissions: 1,
telemetry: Some(StageTelemetry {
gpu_compute: Some(TimestampTotals::default()),
..StageTelemetry::default()
}),
..ReplayReport::default()
};
before.fallback_reasons.insert("opcode".into(), 1);
{
let telemetry = before.telemetry.as_mut().unwrap();
telemetry.match_attempts = 9;
telemetry.record_batch(8, 32);
telemetry.flush_reasons.record(FlushReason::BatchFull);
}
let mut after = before.clone();
after.handled_draws += 4;
after.fallback_draws += 1;
after.submissions += 2;
after.pending_draws = 2;
*after.fallback_reasons.get_mut("opcode").unwrap() += 1;
{
let telemetry = after.telemetry.as_mut().unwrap();
telemetry.match_attempts += 5;
telemetry.sampled_draws += 1;
telemetry.sampled_match_ns += 13;
telemetry.timestamp_readback_bytes += 16;
telemetry.record_batch(1, 32);
telemetry.record_batch(3, 32);
telemetry.flush_reasons.record(FlushReason::Idle);
telemetry.flush_reasons.record(FlushReason::CpuFallback);
record_timestamp(telemetry.gpu_compute.as_mut().unwrap(), Some(90)).unwrap();
}
let delta = after.delta_since(&before).unwrap();
assert_eq!(
(delta.handled_draws, delta.fallback_draws, delta.submissions),
(4, 1, 2)
);
assert_eq!(delta.pending_draws, 2);
assert_eq!(delta.fallback_reasons.get("opcode"), Some(&1));
let telemetry = delta.telemetry.unwrap();
assert_eq!(
(
telemetry.match_attempts,
telemetry.sampled_draws,
telemetry.sampled_match_ns
),
(5, 1, 13)
);
assert_eq!((telemetry.batch_count, telemetry.batch_draws), (2, 4));
assert_eq!(telemetry.parameter_bytes, 4 * 32);
assert_eq!(telemetry.timestamp_readback_bytes, 16);
assert_eq!(telemetry.batch_histogram[0], 1);
assert_eq!(telemetry.batch_histogram[1], 1);
assert_eq!(telemetry.batch_histogram[3], 0);
assert_eq!(
(
telemetry.flush_reasons.batch_full,
telemetry.flush_reasons.idle
),
(0, 1)
);
assert_eq!(telemetry.flush_reasons.cpu_fallback, 1);
assert_eq!(
telemetry.gpu_compute,
Some(TimestampTotals {
count: 1,
nanoseconds: 90,
discarded: 0,
})
);
assert_eq!(telemetry.gpu_readback_transfer, None);
}
#[test]
fn telemetry_detects_reset_or_changed_availability() {
let before = ReplayReport {
handled_draws: 3,
telemetry: Some(StageTelemetry {
match_attempts: 3,
gpu_compute: Some(TimestampTotals::default()),
..StageTelemetry::default()
}),
..ReplayReport::default()
};
let mut after = before.clone();
after.telemetry = None;
assert!(after.delta_since(&before).unwrap_err().contains("toggled"));
after.telemetry = Some(StageTelemetry::default());
assert!(after
.delta_since(&before)
.unwrap_err()
.contains("match_attempts"));
after.telemetry.as_mut().unwrap().match_attempts = 3;
assert!(after
.delta_since(&before)
.unwrap_err()
.contains("timestamp availability"));
after = before.clone();
after.handled_draws = 0;
assert!(after
.delta_since(&before)
.unwrap_err()
.contains("handled_draws"));
}
#[test]
fn counter_epochs_reject_resets_and_different_engines() {
let before = ReplayReport::fresh();
let mut after = before.clone();
after.advance_counter_epoch();
after.handled_draws = 100;
assert!(after.delta_since(&before).unwrap_err().contains("reset"));
assert!(ReplayReport::fresh()
.delta_since(&before)
.unwrap_err()
.contains("different engines"));
}
#[test]
fn batch_histogram_covers_configured_limits() {
let mut telemetry = StageTelemetry::default();
for draws in [0, 1, 2, 3, 1023, 1024, 65536] {
telemetry.record_batch(draws, 32);
}
assert_eq!(telemetry.batch_count, 6);
assert_eq!(telemetry.batch_draws, 1 + 2 + 3 + 1023 + 1024 + 65536);
assert_eq!(telemetry.batch_histogram[0], 1);
assert_eq!(telemetry.batch_histogram[1], 2);
assert_eq!(telemetry.batch_histogram[9], 1);
assert_eq!(telemetry.batch_histogram[10], 1);
assert_eq!(telemetry.batch_histogram[16], 1);
assert_eq!(
telemetry.batch_histogram.iter().sum::<u64>(),
telemetry.batch_count
);
}
#[test]
fn timestamp_readback_uses_period_and_rejects_bad_data() {
let bytes: Vec<u8> = [100_u64, 140, 200, 202]
.into_iter()
.flat_map(u64::to_ne_bytes)
.collect();
assert_eq!(timestamp_duration_ns(&bytes, 0, 2.5), Ok(Some(100)));
assert_eq!(timestamp_duration_ns(&bytes, 2, 2.5), Ok(Some(5)));
assert!(timestamp_duration_ns(&bytes, 4, 2.5).is_err());
assert!(timestamp_duration_ns(&bytes, 0, 0.0).is_err());
let backwards: Vec<u8> = [2_u64, 1].into_iter().flat_map(u64::to_ne_bytes).collect();
assert_eq!(timestamp_duration_ns(&backwards, 0, 1.0), Ok(None));
}
#[test]
fn timestamp_failure_invalidates_totals_and_rejects_transition_deltas() {
let before = ReplayReport {
timestamp_support: TimestampSupport::Enabled {
readback_transfer: true,
},
telemetry: Some(StageTelemetry {
gpu_compute: Some(TimestampTotals {
count: 1,
nanoseconds: 20,
discarded: 0,
}),
gpu_readback_transfer: Some(TimestampTotals::default()),
..StageTelemetry::default()
}),
..ReplayReport::default()
};
let mut after = before.clone();
after.timestamp_collection_failed("readback failed".into());
let telemetry = after.telemetry.as_ref().unwrap();
assert_eq!(telemetry.gpu_compute, None);
assert_eq!(telemetry.gpu_readback_transfer, None);
assert_eq!(after.timestamp_error.as_deref(), Some("readback failed"));
assert!(after
.delta_since(&before)
.unwrap_err()
.contains("timestamp support"));
let mut inconsistent = before.clone();
inconsistent.timestamp_support = TimestampSupport::Unavailable("failed".into());
assert!(inconsistent
.delta_since(&before)
.unwrap_err()
.contains("timestamp support"));
let mut error_only = before.clone();
error_only.timestamp_error = Some("failed".into());
assert!(error_only
.delta_since(&before)
.unwrap_err()
.contains("timestamp support"));
assert!(after.delta_since(&after).is_ok());
}
#[test]
fn timestamp_wrap_discards_only_affected_sample_and_keeps_profiling() {
let bytes: Vec<u8> = [u64::MAX - 2, 1, 10, 15]
.into_iter()
.flat_map(u64::to_ne_bytes)
.collect();
let mut total = TimestampTotals::default();
let before = total.clone();
record_timestamp(&mut total, timestamp_duration_ns(&bytes, 0, 2.0).unwrap()).unwrap();
record_timestamp(&mut total, timestamp_duration_ns(&bytes, 2, 2.0).unwrap()).unwrap();
assert_eq!(
total.delta_since(&before).unwrap(),
TimestampTotals {
count: 1,
nanoseconds: 10,
discarded: 1,
}
);
}
#[test]
fn timestamp_validation_requires_a_usable_sample() {
assert!(require_usable_timestamps(
"compute",
&TimestampTotals {
count: 1,
nanoseconds: 5,
discarded: 3,
},
)
.is_ok());
let all_discarded = require_usable_timestamps(
"compute",
&TimestampTotals {
discarded: 3,
..TimestampTotals::default()
},
)
.unwrap_err();
assert!(all_discarded.contains("INCONCLUSIVE"));
assert!(all_discarded.contains("all 3 compute"));
assert!(
require_usable_timestamps("transfer", &TimestampTotals::default())
.unwrap_err()
.contains("no usable samples")
);
}
#[test]
fn live_aa_options_validate_bounds_and_balanced_rounds() {
assert_eq!(
LiveAaOptions::parse(None, None).unwrap(),
LiveAaOptions {
sample_ms: 600,
max_rounds: 48,
}
);
assert_eq!(
LiveAaOptions::parse(Some("100"), Some("16")).unwrap(),
LiveAaOptions {
sample_ms: 100,
max_rounds: 16,
}
);
assert_eq!(
LiveAaOptions::parse(Some("2000"), Some("128")).unwrap(),
LiveAaOptions {
sample_ms: 2000,
max_rounds: 128,
}
);
for value in [
"",
"99",
"2001",
"-1",
"+600",
" 600",
"99999999999999999999",
] {
assert!(LiveAaOptions::parse(Some(value), None)
.unwrap_err()
.contains(LIVE_AA_SAMPLE_MS));
}
for value in ["", "15", "17", "127", "129", "NaN", "99999999999999999999"] {
assert!(LiveAaOptions::parse(None, Some(value))
.unwrap_err()
.contains(LIVE_AA_MAX_ROUNDS));
}
}
#[test]
fn live_aa_looks_are_balanced_and_spend_confidence_across_looks() {
for max_rounds in [16, 24, 48, 64, 128] {
let options = LiveAaOptions::parse(None, Some(&max_rounds.to_string())).unwrap();
let looks: Vec<_> = (0..=max_rounds)
.filter(|&round| options.is_look(round))
.collect();
assert_eq!(looks.first(), Some(&16));
assert_eq!(looks.last(), Some(&max_rounds));
assert_eq!(looks.len(), options.look_count());
assert!(looks.windows(2).all(|pair| pair[1] - pair[0] == 8));
let mut spent = 0.0;
for (index, &rounds) in looks.iter().enumerate() {
assert_eq!(options.look_number(rounds), index + 1);
let stats =
paired_rate_stats_with_alpha(&vec![0.0; rounds], options.per_look_alpha());
assert!(1.0 - stats.confidence <= options.per_look_alpha() + 1e-12);
spent += 1.0 - stats.confidence;
}
assert!(spent <= LIVE_AA_SIGN_ALPHA + 1e-12);
}
let single_look = LiveAaOptions::parse(None, Some("16")).unwrap();
let values: Vec<_> = (0..16).map(|value| (value as f64 + 1.0).ln()).collect();
let old = paired_rate_stats(&values);
let sequential = paired_rate_stats_with_alpha(&values, single_look.per_look_alpha());
assert_eq!(sequential.lower, old.lower);
assert_eq!(sequential.upper, old.upper);
assert_eq!(sequential.confidence, old.confidence);
}
#[test]
fn live_gate_statistics_handle_odd_even_and_noise() {
let stats = live_gate_stats(&[5.0, 1.0, 3.0, 2.0, 4.0]);
assert_eq!(stats.median, 3.0);
assert!((stats.coefficient_of_variation - 2.0_f64.sqrt() / 3.0).abs() < 1e-12);
assert!((stats.range_over_median - 4.0 / 3.0).abs() < 1e-12);
assert_eq!(live_gate_stats(&[1.0, 3.0]).median, 2.0);
}
#[test]
fn abba_rate_ratios_cancel_linear_log_drift_in_both_orders() {
let times = [0.0, 0.28, 0.72, 1.43];
for on_first in [false, true] {
let rates = std::array::from_fn(|index| {
let on = if index == 0 || index == 3 {
on_first
} else {
!on_first
};
100.0 * (times[index] * 0.04_f64).exp() * if on { 0.98 } else { 1.0 }
});
assert!((abba_log_rate_ratio(rates, times, on_first) - 0.98_f64.ln()).abs() < 1e-12);
}
assert!((log_rate_ratio(100.0, 98.0) - 0.98_f64.ln()).abs() < 1e-12);
}
#[test]
fn paired_statistics_use_sign_interval_and_resist_outliers() {
let mut ratios = [0.99_f64.ln(); 16];
ratios[..3].fill(0.5_f64.ln());
ratios[13..].fill(2.0_f64.ln());
let stats = paired_rate_stats(&ratios);
assert!((stats.median + 0.01).abs() < 1e-12);
assert!((stats.lower + 0.01).abs() < 1e-12);
assert!((stats.upper + 0.01).abs() < 1e-12);
assert!((stats.confidence - (1.0 - 2.0 * 697.0 / 65536.0)).abs() < 1e-12);
}
#[test]
fn overhead_decision_distinguishes_effect_from_noise() {
let control = paired_rate_stats(&[0.0; 16]);
let change = |rate: f64| paired_rate_stats(&[rate.ln(); 16]);
assert_eq!(
overhead_decision(control, change(0.98), 0.0, 0.03),
OverheadDecision::Pass
);
assert_eq!(
overhead_decision(control, change(0.96), 0.0, 0.03),