@@ -10,7 +10,6 @@ use std::time::Instant;
1010use log:: trace;
1111use rand:: rngs:: StdRng ;
1212use rand:: SeedableRng ;
13- use std:: collections:: hash_map:: Entry ;
1413use measureme:: { Profiler , StringId , EventId , DetachedTiming } ;
1514
1615use rustc_data_structures:: fx:: FxHashMap ;
@@ -45,6 +44,9 @@ pub struct FrameData<'tcx> {
4544 /// we stop unwinding, use the `CatchUnwindData` to handle catching.
4645 pub catch_unwind : Option < CatchUnwindData < ' tcx > > ,
4746
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`.
4850 pub timing : Option < DetachedTiming > ,
4951}
5052
@@ -274,7 +276,11 @@ pub struct Evaluator<'mir, 'tcx> {
274276 /// Allocations that are considered roots of static memory (that may leak).
275277 pub ( crate ) static_roots : Vec < AllocId > ,
276278
279+ /// The `measureme` profiler used to record timing information about
280+ /// the emulated program.
277281 profiler : Option < Profiler > ,
282+ /// Used with `profiler` to cache the `StringId`s for event names
283+ /// uesd with `measureme`.
278284 string_cache : FxHashMap < String , StringId > ,
279285}
280286
@@ -607,29 +613,28 @@ impl<'mir, 'tcx> Machine<'mir, 'tcx> for Evaluator<'mir, 'tcx> {
607613 ecx : & mut InterpCx < ' mir , ' tcx , Self > ,
608614 frame : Frame < ' mir , ' tcx , Tag > ,
609615 ) -> 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
614617 let timing = if let Some ( profiler) = ecx. machine . profiler . as_ref ( ) {
615618 let fn_name = frame. instance . to_string ( ) ;
616619 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+ } ) ;
623623
624624 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 ( ) ,
628628 ) )
629629 } else {
630630 None
631631 } ;
632632
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+
633638 let extra = FrameData { call_id, catch_unwind : None , timing } ;
634639 Ok ( frame. with_extra ( extra) )
635640 }
0 commit comments