-
Notifications
You must be signed in to change notification settings - Fork 54
/
profiler.rs
1891 lines (1686 loc) · 59.9 KB
/
profiler.rs
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use api::{ColorF, ColorU};
use crate::debug_render::DebugRenderer;
use crate::device::query::{GpuSampler, GpuTimer, NamedTag};
use euclid::{Point2D, Rect, Size2D, vec2, default};
use crate::internal_types::FastHashMap;
use crate::renderer::{MAX_VERTEX_TEXTURE_WIDTH, wr_has_been_initialized};
use std::collections::vec_deque::VecDeque;
use std::{f32, mem};
use std::ffi::CStr;
use std::ops::Range;
use std::time::Duration;
use time::precise_time_ns;
pub mod expected {
use std::ops::Range;
pub const AVG_BACKEND_CPU_TIME: Range<f64> = 0.0..3.0;
pub const MAX_BACKEND_CPU_TIME: Range<f64> = 0.0..6.0;
pub const AVG_RENDERER_CPU_TIME: Range<f64> = 0.0..5.0;
pub const MAX_RENDERER_CPU_TIME: Range<f64> = 0.0..10.0;
pub const AVG_IPC_TIME: Range<f64> = 0.0..2.0;
pub const MAX_IPC_TIME: Range<f64> = 0.0..4.0;
pub const AVG_GPU_TIME: Range<f64> = 0.0..8.0;
pub const MAX_GPU_TIME: Range<f64> = 0.0..15.0;
pub const DRAW_CALLS: Range<u64> = 1..100;
pub const VERTICES: Range<u64> = 10..25_000;
pub const TOTAL_PRIMITIVES: Range<u64> = 1..5000;
pub const VISIBLE_PRIMITIVES: Range<u64> = 1..5000;
pub const USED_TARGETS: Range<u64> = 1..4;
pub const COLOR_PASSES: Range<u64> = 1..4;
pub const ALPHA_PASSES: Range<u64> = 0..3;
pub const RENDERED_PICTURE_CACHE_TILES: Range<u64> = 0..5;
pub const TOTAL_PICTURE_CACHE_TILES: Range<u64> = 0..15;
pub const CREATED_TARGETS: Range<u64> = 0..3;
pub const CHANGED_TARGETS: Range<u64> = 0..3;
pub const TEXTURE_DATA_UPLOADED: Range<u64> = 0..10;
pub const GPU_CACHE_ROWS_TOTAL: Range<u64> = 1..50;
pub const GPU_CACHE_ROWS_UPDATED: Range<u64> = 0..25;
pub const GPU_CACHE_BLOCKS_TOTAL: Range<u64> = 1..65_000;
pub const GPU_CACHE_BLOCKS_UPDATED: Range<u64> = 0..1000;
pub const GPU_CACHE_BLOCKS_SAVED: Range<u64> = 0..50_000;
pub const DISPLAY_LIST_BUILD_TIME: Range<f64> = 0.0..3.0;
pub const MAX_SCENE_BUILD_TIME: Range<f64> = 0.0..3.0;
pub const DISPLAY_LIST_SEND_TIME: Range<f64> = 0.0..1.0;
pub const DISPLAY_LIST_TOTAL_TIME: Range<f64> = 0.0..4.0;
pub const NUM_FONT_TEMPLATES: Range<usize> = 0..50;
pub const FONT_TEMPLATES_MB: Range<f32> = 0.0..40.0;
pub const NUM_IMAGE_TEMPLATES: Range<usize> = 0..20;
pub const IMAGE_TEMPLATES_MB: Range<f32> = 0.0..10.0;
pub const DISPLAY_LIST_MB: Range<f32> = 0.0..0.2;
pub const NUM_RASTERIZED_BLOBS: Range<usize> = 0..25; // in tiles
pub const RASTERIZED_BLOBS_MB: Range<f32> = 0.0..4.0;
}
const GRAPH_WIDTH: f32 = 1024.0;
const GRAPH_HEIGHT: f32 = 320.0;
const GRAPH_PADDING: f32 = 8.0;
const GRAPH_FRAME_HEIGHT: f32 = 16.0;
const PROFILE_PADDING: f32 = 8.0;
const ONE_SECOND_NS: u64 = 1000000000;
const AVERAGE_OVER_NS: u64 = ONE_SECOND_NS / 2;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ProfileStyle {
Full,
Compact,
Smart,
}
/// Defines the interface for hooking up an external profiler to WR.
pub trait ProfilerHooks : Send + Sync {
/// Called at the beginning of a profile scope. The label must
/// be a C string (null terminated).
fn begin_marker(&self, label: &CStr);
/// Called at the end of a profile scope. The label must
/// be a C string (null terminated).
fn end_marker(&self, label: &CStr);
/// Called to mark an event happening. The label must
/// be a C string (null terminated).
fn event_marker(&self, label: &CStr);
/// Called with a duration to indicate a text marker that just ended. Text
/// markers allow different types of entries to be recorded on the same row
/// in the timeline, by adding labels to the entry.
///
/// This variant is also useful when the caller only wants to record events
/// longer than a certain threshold, and thus they don't know in advance
/// whether the event will qualify.
fn add_text_marker(&self, label: &CStr, text: &str, duration: Duration);
/// Returns true if the current thread is being profiled.
fn thread_is_being_profiled(&self) -> bool;
}
/// The current global profiler callbacks, if set by embedder.
pub static mut PROFILER_HOOKS: Option<&'static dyn ProfilerHooks> = None;
/// Set the profiler callbacks, or None to disable the profiler.
/// This function must only ever be called before any WR instances
/// have been created, or the hooks will not be set.
pub fn set_profiler_hooks(hooks: Option<&'static dyn ProfilerHooks>) {
if !wr_has_been_initialized() {
unsafe {
PROFILER_HOOKS = hooks;
}
}
}
/// A simple RAII style struct to manage a profile scope.
pub struct ProfileScope {
name: &'static CStr,
}
/// Records a marker of the given duration that just ended.
pub fn add_text_marker(label: &CStr, text: &str, duration: Duration) {
unsafe {
if let Some(ref hooks) = PROFILER_HOOKS {
hooks.add_text_marker(label, text, duration);
}
}
}
/// Records a marker of the given duration that just ended.
pub fn add_event_marker(label: &CStr) {
unsafe {
if let Some(ref hooks) = PROFILER_HOOKS {
hooks.event_marker(label);
}
}
}
/// Returns true if the current thread is being profiled.
pub fn thread_is_being_profiled() -> bool {
unsafe {
PROFILER_HOOKS.map_or(false, |h| h.thread_is_being_profiled())
}
}
impl ProfileScope {
/// Begin a new profile scope
pub fn new(name: &'static CStr) -> Self {
unsafe {
if let Some(ref hooks) = PROFILER_HOOKS {
hooks.begin_marker(name);
}
}
ProfileScope {
name,
}
}
}
impl Drop for ProfileScope {
fn drop(&mut self) {
unsafe {
if let Some(ref hooks) = PROFILER_HOOKS {
hooks.end_marker(self.name);
}
}
}
}
/// A helper macro to define profile scopes.
macro_rules! profile_marker {
($string:expr) => {
let _scope = $crate::profiler::ProfileScope::new(cstr!($string));
};
}
#[derive(Debug, Clone)]
pub struct GpuProfileTag {
pub label: &'static str,
pub color: ColorF,
}
impl NamedTag for GpuProfileTag {
fn get_label(&self) -> &str {
self.label
}
}
trait ProfileCounter {
fn description(&self) -> &'static str;
fn value(&self) -> String;
fn is_expected(&self) -> bool;
}
#[derive(Clone)]
pub struct IntProfileCounter {
description: &'static str,
value: usize,
expect: Option<Range<u64>>,
}
impl IntProfileCounter {
fn new(description: &'static str, expect: Option<Range<u64>>) -> Self {
IntProfileCounter {
description,
value: 0,
expect,
}
}
#[inline(always)]
pub fn inc(&mut self) {
self.value += 1;
}
pub fn set(&mut self, value: usize) {
self.value = value;
}
}
impl ProfileCounter for IntProfileCounter {
fn description(&self) -> &'static str {
self.description
}
fn value(&self) -> String {
format!("{}", self.value)
}
fn is_expected(&self) -> bool {
self.expect.as_ref().map(|range| range.contains(&(self.value as u64))).unwrap_or(true)
}
}
/// A profile counter recording average and maximum integer values over time slices
/// of half a second.
#[derive(Clone)]
pub struct AverageIntProfileCounter {
description: &'static str,
/// Start of the current time slice.
start_ns: u64,
/// Sum of the values recorded during the current time slice.
sum: u64,
/// Number of samples in the current time slice.
num_samples: u64,
/// The max value in in-progress time slice.
next_max: u64,
/// The max value of the previous time slice (displayed).
max: u64,
/// The average value of the previous time slice (displayed).
avg: u64,
/// Intermediate accumulator for `add` and `inc`.
accum: u64,
/// Expected average range of values, if any.
expect_avg: Option<Range<u64>>,
/// Expected maximum range of values, if any.
expect_max: Option<Range<u64>>,
}
impl AverageIntProfileCounter {
pub fn new(
description: &'static str,
expect_avg: Option<Range<u64>>,
expect_max: Option<Range<u64>>,
) -> Self {
AverageIntProfileCounter {
description,
start_ns: precise_time_ns(),
sum: 0,
num_samples: 0,
next_max: 0,
max: 0,
avg: 0,
accum: 0,
expect_avg,
expect_max,
}
}
pub fn reset(&mut self) {
if self.accum > 0 {
self.set_u64(self.accum);
self.accum = 0;
}
}
pub fn set(&mut self, val: usize) {
self.set_u64(val as u64);
}
pub fn set_u64(&mut self, val: u64) {
let now = precise_time_ns();
if (now - self.start_ns) > AVERAGE_OVER_NS && self.num_samples > 0 {
self.avg = self.sum / self.num_samples;
self.max = self.next_max;
self.start_ns = now;
self.sum = 0;
self.num_samples = 0;
self.next_max = 0;
}
self.next_max = self.next_max.max(val);
self.sum += val;
self.num_samples += 1;
self.accum = 0;
}
pub fn add(&mut self, val: usize) {
self.accum += val as u64;
}
pub fn inc(&mut self) {
self.accum += 1;
}
/// Returns either the most up to date value if the counter is updated
/// with add add inc, or the average over the previous time slice.
pub fn get(&self) -> usize {
let result = if self.accum != 0 {
self.accum
} else {
self.avg
};
result as usize
}
}
impl ProfileCounter for AverageIntProfileCounter {
fn description(&self) -> &'static str {
self.description
}
fn value(&self) -> String {
format!("{:.2} (max {:.2})", self.avg, self.max)
}
fn is_expected(&self) -> bool {
self.expect_avg.as_ref().map(|range| range.contains(&self.avg)).unwrap_or(true)
&& self.expect_max.as_ref().map(|range| range.contains(&self.max)).unwrap_or(true)
}
}
pub struct PercentageProfileCounter {
description: &'static str,
value: f32,
}
impl ProfileCounter for PercentageProfileCounter {
fn description(&self) -> &'static str {
self.description
}
fn value(&self) -> String {
format!("{:.2}%", self.value * 100.0)
}
fn is_expected(&self) -> bool { true }
}
#[derive(Clone)]
pub struct ResourceProfileCounter {
description: &'static str,
value: usize,
// in bytes.
size: usize,
expected_count: Option<Range<usize>>,
// in MB
expected_size: Option<Range<f32>>,
}
impl ResourceProfileCounter {
fn new(
description: &'static str,
expected_count: Option<Range<usize>>,
expected_size: Option<Range<f32>>
) -> Self {
ResourceProfileCounter {
description,
value: 0,
size: 0,
expected_count,
expected_size,
}
}
#[allow(dead_code)]
fn reset(&mut self) {
self.value = 0;
self.size = 0;
}
#[inline(always)]
pub fn inc(&mut self, size: usize) {
self.value += 1;
self.size += size;
}
pub fn set(&mut self, count: usize, size: usize) {
self.value = count;
self.size = size;
}
pub fn size_mb(&self) -> f32 {
self.size as f32 / (1024.0 * 1024.0)
}
}
impl ProfileCounter for ResourceProfileCounter {
fn description(&self) -> &'static str {
self.description
}
fn value(&self) -> String {
format!("{} ({:.2} MB)", self.value, self.size_mb())
}
fn is_expected(&self) -> bool {
self.expected_count.as_ref().map(|range| range.contains(&self.value)).unwrap_or(true)
&& self.expected_size.as_ref().map(|range| range.contains(&self.size_mb())).unwrap_or(true)
}
}
#[derive(Clone)]
pub struct TimeProfileCounter {
description: &'static str,
nanoseconds: u64,
invert: bool,
expect_ms: Option<Range<f64>>,
}
pub struct Timer<'a> {
start: u64,
result: &'a mut u64,
}
impl<'a> Drop for Timer<'a> {
fn drop(&mut self) {
let end = precise_time_ns();
*self.result += end - self.start;
}
}
impl TimeProfileCounter {
pub fn new(description: &'static str, invert: bool, expect_ms: Option<Range<f64>>) -> Self {
TimeProfileCounter {
description,
nanoseconds: 0,
invert,
expect_ms,
}
}
fn reset(&mut self) {
self.nanoseconds = 0;
}
#[allow(dead_code)]
pub fn set(&mut self, ns: u64) {
self.nanoseconds = ns;
}
pub fn profile<T, F>(&mut self, callback: F) -> T
where
F: FnOnce() -> T,
{
let t0 = precise_time_ns();
let val = callback();
let t1 = precise_time_ns();
let ns = t1 - t0;
self.nanoseconds += ns;
val
}
pub fn timer(&mut self) -> Timer {
Timer {
start: precise_time_ns(),
result: &mut self.nanoseconds,
}
}
pub fn inc(&mut self, ns: u64) {
self.nanoseconds += ns;
}
pub fn get(&self) -> u64 {
self.nanoseconds
}
pub fn get_ms(&self) -> f64 {
self.nanoseconds as f64 / 1000000.0
}
}
impl ProfileCounter for TimeProfileCounter {
fn description(&self) -> &'static str {
self.description
}
fn value(&self) -> String {
if self.invert {
format!("{:.2} fps", 1000000000.0 / self.nanoseconds as f64)
} else {
format!("{:.2} ms", self.get_ms())
}
}
fn is_expected(&self) -> bool {
self.expect_ms.as_ref()
.map(|range| range.contains(&(self.nanoseconds as f64 / 1000000.0)))
.unwrap_or(true)
}
}
#[derive(Clone)]
pub struct AverageTimeProfileCounter {
counter: AverageIntProfileCounter,
invert: bool,
}
impl AverageTimeProfileCounter {
pub fn new(
description: &'static str,
invert: bool,
expect_avg: Option<Range<f64>>,
expect_max: Option<Range<f64>>,
) -> Self {
let expect_avg_ns = expect_avg.map(
|range| (range.start * 1000000.0) as u64 .. (range.end * 1000000.0) as u64
);
let expect_max_ns = expect_max.map(
|range| (range.start * 1000000.0) as u64 .. (range.end * 1000000.0) as u64
);
AverageTimeProfileCounter {
counter: AverageIntProfileCounter::new(
description,
expect_avg_ns,
expect_max_ns,
),
invert,
}
}
pub fn set(&mut self, ns: u64) {
self.counter.set_u64(ns);
}
#[allow(dead_code)]
pub fn profile<T, F>(&mut self, callback: F) -> T
where
F: FnOnce() -> T,
{
let t0 = precise_time_ns();
let val = callback();
let t1 = precise_time_ns();
self.counter.set_u64(t1 - t0);
val
}
pub fn avg_ms(&self) -> f64 { self.counter.avg as f64 / 1000000.0 }
pub fn max_ms(&self) -> f64 { self.counter.max as f64 / 1000000.0 }
}
impl ProfileCounter for AverageTimeProfileCounter {
fn description(&self) -> &'static str {
self.counter.description
}
fn value(&self) -> String {
if self.invert {
format!("{:.2} fps", 1000000000.0 / self.counter.avg as f64)
} else {
format!("{:.2} ms (max {:.2} ms)", self.avg_ms(), self.max_ms())
}
}
fn is_expected(&self) -> bool {
self.counter.is_expected()
}
}
#[derive(Clone)]
pub struct FrameProfileCounters {
pub total_primitives: AverageIntProfileCounter,
pub visible_primitives: AverageIntProfileCounter,
pub targets_used: AverageIntProfileCounter,
pub targets_changed: AverageIntProfileCounter,
pub targets_created: AverageIntProfileCounter,
}
impl FrameProfileCounters {
pub fn new() -> Self {
FrameProfileCounters {
total_primitives: AverageIntProfileCounter::new(
"Total Primitives",
None, Some(expected::TOTAL_PRIMITIVES),
),
visible_primitives: AverageIntProfileCounter::new(
"Visible Primitives",
None, Some(expected::VISIBLE_PRIMITIVES),
),
targets_used: AverageIntProfileCounter::new(
"Used targets",
None, Some(expected::USED_TARGETS),
),
targets_changed: AverageIntProfileCounter::new(
"Changed targets",
None, Some(expected::CHANGED_TARGETS),
),
targets_created: AverageIntProfileCounter::new(
"Created targets",
None, Some(expected::CREATED_TARGETS),
),
}
}
pub fn reset_targets(&mut self) {
self.targets_used.reset();
self.targets_changed.reset();
self.targets_created.reset();
}
}
#[derive(Clone)]
pub struct TextureCacheProfileCounters {
pub pages_alpha8_linear: ResourceProfileCounter,
pub pages_alpha16_linear: ResourceProfileCounter,
pub pages_color8_linear: ResourceProfileCounter,
pub pages_color8_nearest: ResourceProfileCounter,
pub pages_picture: ResourceProfileCounter,
pub rasterized_blob_pixels: ResourceProfileCounter,
}
impl TextureCacheProfileCounters {
pub fn new() -> Self {
TextureCacheProfileCounters {
pages_alpha8_linear: ResourceProfileCounter::new("Texture A8 cached pages", None, None),
pages_alpha16_linear: ResourceProfileCounter::new("Texture A16 cached pages", None, None),
pages_color8_linear: ResourceProfileCounter::new("Texture RGBA8 cached pages (L)", None, None),
pages_color8_nearest: ResourceProfileCounter::new("Texture RGBA8 cached pages (N)", None, None),
pages_picture: ResourceProfileCounter::new("Picture cached pages", None, None),
rasterized_blob_pixels: ResourceProfileCounter::new(
"Rasterized Blob Pixels",
Some(expected::NUM_RASTERIZED_BLOBS),
Some(expected::RASTERIZED_BLOBS_MB),
),
}
}
}
#[derive(Clone)]
pub struct GpuCacheProfileCounters {
pub allocated_rows: AverageIntProfileCounter,
pub allocated_blocks: AverageIntProfileCounter,
pub updated_rows: AverageIntProfileCounter,
pub updated_blocks: AverageIntProfileCounter,
pub saved_blocks: AverageIntProfileCounter,
}
impl GpuCacheProfileCounters {
pub fn new() -> Self {
GpuCacheProfileCounters {
allocated_rows: AverageIntProfileCounter::new(
"GPU cache rows: total",
None, Some(expected::GPU_CACHE_ROWS_TOTAL),
),
updated_rows: AverageIntProfileCounter::new(
"GPU cache rows: updated",
None, Some(expected::GPU_CACHE_ROWS_UPDATED),
),
allocated_blocks: AverageIntProfileCounter::new(
"GPU cache blocks: total",
None, Some(expected::GPU_CACHE_BLOCKS_TOTAL),
),
updated_blocks: AverageIntProfileCounter::new(
"GPU cache blocks: updated",
None, Some(expected::GPU_CACHE_BLOCKS_UPDATED),
),
saved_blocks: AverageIntProfileCounter::new(
"GPU cache blocks: saved",
None, Some(expected::GPU_CACHE_BLOCKS_SAVED),
),
}
}
}
#[derive(Clone)]
pub struct BackendProfileCounters {
pub total_time: TimeProfileCounter,
pub resources: ResourceProfileCounters,
pub txn: TransactionProfileCounters,
pub intern: InternProfileCounters,
pub scene_changed: bool,
}
#[derive(Clone)]
pub struct ResourceProfileCounters {
pub font_templates: ResourceProfileCounter,
pub image_templates: ResourceProfileCounter,
pub texture_cache: TextureCacheProfileCounters,
pub gpu_cache: GpuCacheProfileCounters,
pub content_slices: IntProfileCounter,
}
#[derive(Clone)]
pub struct TransactionProfileCounters {
pub display_list_build_time: TimeProfileCounter,
pub scene_build_time: TimeProfileCounter,
/// Time between when the display list is built and when it is sent by the API.
pub content_send_time: TimeProfileCounter,
/// Time between sending the SetDisplayList from the API and picking it up on
/// the render scene builder thread.
pub api_send_time: TimeProfileCounter,
/// Sum of content_send_time and api_send_time.
pub total_send_time: TimeProfileCounter,
pub display_lists: ResourceProfileCounter,
}
macro_rules! declare_intern_profile_counters {
( $( $name:ident : $ty:ty, )+ ) => {
#[derive(Clone)]
pub struct InternProfileCounters {
$(
pub $name: ResourceProfileCounter,
)+
}
impl InternProfileCounters {
fn draw(
&self,
debug_renderer: &mut DebugRenderer,
draw_state: &mut DrawState,
) {
Profiler::draw_counters(
&[
$(
&self.$name,
)+
],
None,
debug_renderer,
false,
draw_state,
);
}
}
}
}
enumerate_interners!(declare_intern_profile_counters);
impl TransactionProfileCounters {
pub fn set(
&mut self,
dl_build_start: u64,
dl_build_end: u64,
send_start: u64,
scene_build_start: u64,
scene_build_end: u64,
display_len: usize,
) {
self.display_list_build_time.reset();
self.content_send_time.reset();
self.api_send_time.reset();
self.total_send_time.reset();
self.scene_build_time.reset();
self.display_lists.reset();
let dl_build_time = dl_build_end - dl_build_start;
let scene_build_time = scene_build_end - scene_build_start;
let content_send_time = send_start - dl_build_end;
let api_send_time = scene_build_start - send_start;
self.display_list_build_time.inc(dl_build_time);
self.scene_build_time.inc(scene_build_time);
self.content_send_time.inc(content_send_time);
self.api_send_time.inc(api_send_time);
self.total_send_time.inc(content_send_time + api_send_time);
self.display_lists.inc(display_len);
}
}
impl BackendProfileCounters {
pub fn new() -> Self {
BackendProfileCounters {
total_time: TimeProfileCounter::new(
"Backend CPU Time", false,
Some(expected::MAX_BACKEND_CPU_TIME),
),
resources: ResourceProfileCounters {
font_templates: ResourceProfileCounter::new(
"Font Templates",
Some(expected::NUM_FONT_TEMPLATES),
Some(expected::FONT_TEMPLATES_MB),
),
image_templates: ResourceProfileCounter::new(
"Image Templates",
Some(expected::NUM_IMAGE_TEMPLATES),
Some(expected::IMAGE_TEMPLATES_MB),
),
content_slices: IntProfileCounter::new(
"Content Slices",
None,
),
texture_cache: TextureCacheProfileCounters::new(),
gpu_cache: GpuCacheProfileCounters::new(),
},
txn: TransactionProfileCounters {
display_list_build_time: TimeProfileCounter::new(
"DisplayList Build Time", false,
Some(expected::DISPLAY_LIST_BUILD_TIME)
),
scene_build_time: TimeProfileCounter::new(
"Scene build time", false,
Some(expected::MAX_SCENE_BUILD_TIME),
),
content_send_time: TimeProfileCounter::new(
"Content Send Time", false,
Some(expected::DISPLAY_LIST_SEND_TIME),
),
api_send_time: TimeProfileCounter::new(
"API Send Time", false,
Some(expected::DISPLAY_LIST_SEND_TIME),
),
total_send_time: TimeProfileCounter::new(
"Total IPC Time", false,
Some(expected::DISPLAY_LIST_TOTAL_TIME),
),
display_lists: ResourceProfileCounter::new(
"DisplayLists Sent",
None, Some(expected::DISPLAY_LIST_MB),
),
},
//TODO: generate this by a macro
intern: InternProfileCounters {
prim: ResourceProfileCounter::new("Interned primitives", None, None),
conic_grad: ResourceProfileCounter::new("Interned conic gradients", None, None),
image: ResourceProfileCounter::new("Interned images", None, None),
image_border: ResourceProfileCounter::new("Interned image borders", None, None),
line_decoration: ResourceProfileCounter::new("Interned line decorations", None, None),
linear_grad: ResourceProfileCounter::new("Interned linear gradients", None, None),
normal_border: ResourceProfileCounter::new("Interned normal borders", None, None),
picture: ResourceProfileCounter::new("Interned pictures", None, None),
radial_grad: ResourceProfileCounter::new("Interned radial gradients", None, None),
text_run: ResourceProfileCounter::new("Interned text runs", None, None),
yuv_image: ResourceProfileCounter::new("Interned YUV images", None, None),
clip: ResourceProfileCounter::new("Interned clips", None, None),
filter_data: ResourceProfileCounter::new("Interned filter data", None, None),
backdrop: ResourceProfileCounter::new("Interned backdrops", None, None),
},
scene_changed: false,
}
}
pub fn reset(&mut self) {
self.total_time.reset();
self.resources.texture_cache.rasterized_blob_pixels.reset();
self.scene_changed = false;
}
}
pub struct RendererProfileCounters {
pub frame_counter: IntProfileCounter,
pub frame_time: AverageTimeProfileCounter,
pub draw_calls: AverageIntProfileCounter,
pub vertices: AverageIntProfileCounter,
pub vao_count_and_size: ResourceProfileCounter,
pub color_passes: AverageIntProfileCounter,
pub alpha_passes: AverageIntProfileCounter,
pub texture_data_uploaded: AverageIntProfileCounter,
pub rendered_picture_cache_tiles: AverageIntProfileCounter,
pub total_picture_cache_tiles: AverageIntProfileCounter,
}
pub struct RendererProfileTimers {
pub cpu_time: TimeProfileCounter,
pub gpu_graph: TimeProfileCounter,
pub gpu_samples: Vec<GpuTimer<GpuProfileTag>>,
}
impl RendererProfileCounters {
pub fn new() -> Self {
RendererProfileCounters {
frame_counter: IntProfileCounter::new("Frame", None),
frame_time: AverageTimeProfileCounter::new(
"FPS", true, None, None,
),
draw_calls: AverageIntProfileCounter::new(
"Draw Calls",
None, Some(expected::DRAW_CALLS),
),
vertices: AverageIntProfileCounter::new(
"Vertices",
None, Some(expected::VERTICES),
),
vao_count_and_size: ResourceProfileCounter::new("VAO", None, None),
color_passes: AverageIntProfileCounter::new(
"Color passes",
None, Some(expected::COLOR_PASSES),
),
alpha_passes: AverageIntProfileCounter::new(
"Alpha passes",
None, Some(expected::ALPHA_PASSES),
),
texture_data_uploaded: AverageIntProfileCounter::new(
"Texture data, kb",
None, Some(expected::TEXTURE_DATA_UPLOADED),
),
rendered_picture_cache_tiles: AverageIntProfileCounter::new(
"Rendered tiles",
None, Some(expected::RENDERED_PICTURE_CACHE_TILES),
),
total_picture_cache_tiles: AverageIntProfileCounter::new(
"Total tiles",
None, Some(expected::TOTAL_PICTURE_CACHE_TILES),
),
}
}
pub fn reset(&mut self) {
self.draw_calls.reset();
self.vertices.reset();
self.color_passes.reset();
self.alpha_passes.reset();
self.texture_data_uploaded.reset();
self.rendered_picture_cache_tiles.reset();
self.total_picture_cache_tiles.reset();
}
}
impl RendererProfileTimers {
pub fn new() -> Self {
RendererProfileTimers {
cpu_time: TimeProfileCounter::new("Renderer CPU Time", false, None),
gpu_samples: Vec::new(),
gpu_graph: TimeProfileCounter::new("GPU Time", false, None),
}
}
}
struct GraphStats {
min_value: f32,
mean_value: f32,
max_value: f32,
}
struct ProfileGraph {
max_samples: usize,
scale: f32,
values: VecDeque<f32>,
short_description: &'static str,
unit_description: &'static str,
}
impl ProfileGraph {
fn new(
max_samples: usize,
scale: f32,
short_description: &'static str,
unit_description: &'static str,
) -> Self {
ProfileGraph {
max_samples,
scale,
values: VecDeque::new(),
short_description,
unit_description,
}
}
fn push(&mut self, ns: u64) {
let val = ns as f64 * self.scale as f64;
if self.values.len() == self.max_samples {
self.values.pop_back();
}
self.values.push_front(val as f32);
}
fn stats(&self) -> GraphStats {
let mut stats = GraphStats {
min_value: f32::MAX,
mean_value: 0.0,
max_value: -f32::MAX,
};
for value in &self.values {
stats.min_value = stats.min_value.min(*value);
stats.mean_value += *value;
stats.max_value = stats.max_value.max(*value);
}
if !self.values.is_empty() {
stats.mean_value /= self.values.len() as f32;
}
stats
}
fn draw_graph(