@@ -512,6 +512,12 @@ impl Ord for OrderedData {
512
512
}
513
513
}
514
514
515
+ /// Add these to [`GlobalProfiler`] with [`GlobalProfiler::add_sink`].
516
+ pub type FrameSink = Box < dyn Fn ( Arc < FrameData > ) + Send > ;
517
+
518
+ #[ derive( Clone , Copy , Debug , Hash , PartialEq , Eq ) ]
519
+ pub struct FrameSinkId ( u64 ) ;
520
+
515
521
/// Singleton. Collects profiling data from multiple threads.
516
522
pub struct GlobalProfiler {
517
523
current_frame_index : FrameIndex ,
@@ -523,6 +529,9 @@ pub struct GlobalProfiler {
523
529
524
530
slowest_frames : std:: collections:: BinaryHeap < OrderedData > ,
525
531
max_slow : usize ,
532
+
533
+ next_sink_id : FrameSinkId ,
534
+ sinks : std:: collections:: HashMap < FrameSinkId , FrameSink > ,
526
535
}
527
536
528
537
impl Default for GlobalProfiler {
@@ -537,6 +546,8 @@ impl Default for GlobalProfiler {
537
546
max_recent,
538
547
slowest_frames : std:: collections:: BinaryHeap :: with_capacity ( max_slow) ,
539
548
max_slow,
549
+ next_sink_id : FrameSinkId ( 1 ) ,
550
+ sinks : Default :: default ( ) ,
540
551
}
541
552
}
542
553
}
@@ -573,6 +584,10 @@ impl GlobalProfiler {
573
584
574
585
/// Manually add frame data.
575
586
pub fn add_frame ( & mut self , new_frame : Arc < FrameData > ) {
587
+ for sink in self . sinks . values ( ) {
588
+ sink ( new_frame. clone ( ) ) ;
589
+ }
590
+
576
591
let add_to_slowest = if self . slowest_frames . len ( ) < self . max_slow {
577
592
true
578
593
} else if let Some ( fastest_of_the_slow) = self . slowest_frames . peek ( ) {
@@ -703,6 +718,19 @@ impl GlobalProfiler {
703
718
704
719
Ok ( slf)
705
720
}
721
+
722
+ /// Call this function with each new finished frame.
723
+ /// The returned [`FrameSinkId`] can be used to remove the sink with [`Self::remove_sink`].
724
+ pub fn add_sink ( & mut self , sink : FrameSink ) -> FrameSinkId {
725
+ let id = self . next_sink_id ;
726
+ self . next_sink_id . 0 += 1 ;
727
+ self . sinks . insert ( id, sink) ;
728
+ id
729
+ }
730
+
731
+ pub fn remove_sink ( & mut self , id : FrameSinkId ) -> Option < FrameSink > {
732
+ self . sinks . remove ( & id)
733
+ }
706
734
}
707
735
708
736
// ----------------------------------------------------------------------------
0 commit comments