@@ -10,7 +10,6 @@ use std::time::Instant;
10
10
use log:: trace;
11
11
use rand:: rngs:: StdRng ;
12
12
use rand:: SeedableRng ;
13
- use std:: collections:: hash_map:: Entry ;
14
13
use measureme:: { Profiler , StringId , EventId , DetachedTiming } ;
15
14
16
15
use rustc_data_structures:: fx:: FxHashMap ;
@@ -45,6 +44,9 @@ pub struct FrameData<'tcx> {
45
44
/// we stop unwinding, use the `CatchUnwindData` to handle catching.
46
45
pub catch_unwind : Option < CatchUnwindData < ' tcx > > ,
47
46
47
+ /// If `measureme` profiling is enabled, holds timing information
48
+ /// for the start of this frame. When we finish executing this frame,
49
+ /// we use this to register a completed event with `measureme`.
48
50
pub timing : Option < DetachedTiming > ,
49
51
}
50
52
@@ -274,7 +276,11 @@ pub struct Evaluator<'mir, 'tcx> {
274
276
/// Allocations that are considered roots of static memory (that may leak).
275
277
pub ( crate ) static_roots : Vec < AllocId > ,
276
278
279
+ /// The `measureme` profiler used to record timing information about
280
+ /// the emulated program.
277
281
profiler : Option < Profiler > ,
282
+ /// Used with `profiler` to cache the `StringId`s for event names
283
+ /// uesd with `measureme`.
278
284
string_cache : FxHashMap < String , StringId > ,
279
285
}
280
286
@@ -607,29 +613,28 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
607
613
ecx : & mut InterpCx < ' mir , ' tcx , Self > ,
608
614
frame : Frame < ' mir , ' tcx , Tag > ,
609
615
) -> InterpResult < ' tcx , Frame < ' mir , ' tcx , Tag , FrameData < ' tcx > > > {
610
- let stacked_borrows = ecx. memory . extra . stacked_borrows . as_ref ( ) ;
611
- let call_id = stacked_borrows. map_or ( NonZeroU64 :: new ( 1 ) . unwrap ( ) , |stacked_borrows| {
612
- stacked_borrows. borrow_mut ( ) . new_call ( )
613
- } ) ;
616
+ // Start recording our event before doing anything else
614
617
let timing = if let Some ( profiler) = ecx. machine . profiler . as_ref ( ) {
615
618
let fn_name = frame. instance . to_string ( ) ;
616
619
let entry = ecx. machine . string_cache . entry ( fn_name. clone ( ) ) ;
617
- let name = match entry {
618
- Entry :: Occupied ( e) => * e. get ( ) ,
619
- Entry :: Vacant ( e) => {
620
- * e. insert ( profiler. alloc_string ( & * fn_name) )
621
- }
622
- } ;
620
+ let name = entry. or_insert_with ( || {
621
+ profiler. alloc_string ( & * fn_name)
622
+ } ) ;
623
623
624
624
Some ( profiler. start_recording_interval_event_detached (
625
- name,
626
- EventId :: from_label ( name) ,
627
- ecx. get_active_thread ( ) . to_u32 ( )
625
+ * name,
626
+ EventId :: from_label ( * name) ,
627
+ ecx. get_active_thread ( ) . to_u32 ( ) ,
628
628
) )
629
629
} else {
630
630
None
631
631
} ;
632
632
633
+ let stacked_borrows = ecx. memory . extra . stacked_borrows . as_ref ( ) ;
634
+ let call_id = stacked_borrows. map_or ( NonZeroU64 :: new ( 1 ) . unwrap ( ) , |stacked_borrows| {
635
+ stacked_borrows. borrow_mut ( ) . new_call ( )
636
+ } ) ;
637
+
633
638
let extra = FrameData { call_id, catch_unwind : None , timing } ;
634
639
Ok ( frame. with_extra ( extra) )
635
640
}
0 commit comments