@@ -43,7 +43,9 @@ use std::fmt;
4343use std:: io:: Write ;
4444use std:: path:: PathBuf ;
4545use std:: time:: Duration ;
46- use std:: sync:: mpsc;
46+ use std:: sync:: { Arc , mpsc} ;
47+
48+ use parking_lot:: Mutex as PlMutex ;
4749
4850mod code_stats;
4951pub mod config;
@@ -126,11 +128,8 @@ pub struct Session {
126128 /// Used by `-Z profile-queries` in `util::common`.
127129 pub profile_channel : Lock < Option < mpsc:: Sender < ProfileQueriesMsg > > > ,
128130
129- /// Used by `-Z self-profile`.
130- pub self_profiling_active : bool ,
131-
132- /// Used by `-Z self-profile`.
133- pub self_profiling : Lock < SelfProfiler > ,
131+ /// Used by -Z self-profile
132+ pub self_profiling : Option < Arc < PlMutex < SelfProfiler > > > ,
134133
135134 /// Some measurements that are being gathered during compilation.
136135 pub perf_stats : PerfStats ,
@@ -833,27 +832,23 @@ impl Session {
833832 #[ inline( never) ]
834833 #[ cold]
835834 fn profiler_active < F : FnOnce ( & mut SelfProfiler ) -> ( ) > ( & self , f : F ) {
836- let mut profiler = self . self_profiling . borrow_mut ( ) ;
837- f ( & mut profiler) ;
835+ match & self . self_profiling {
836+ None => bug ! ( "profiler_active() called but there was no profiler active" ) ,
837+ Some ( profiler) => {
838+ let mut p = profiler. lock ( ) ;
839+
840+ f ( & mut p) ;
841+ }
842+ }
838843 }
839844
840845 #[ inline( always) ]
841846 pub fn profiler < F : FnOnce ( & mut SelfProfiler ) -> ( ) > ( & self , f : F ) {
842- if unlikely ! ( self . self_profiling_active ) {
847+ if unlikely ! ( self . self_profiling . is_some ( ) ) {
843848 self . profiler_active ( f)
844849 }
845850 }
846851
847- pub fn print_profiler_results ( & self ) {
848- let mut profiler = self . self_profiling . borrow_mut ( ) ;
849- profiler. print_results ( & self . opts ) ;
850- }
851-
852- pub fn save_json_results ( & self ) {
853- let profiler = self . self_profiling . borrow ( ) ;
854- profiler. save_results ( & self . opts ) ;
855- }
856-
857852 pub fn print_perf_stats ( & self ) {
858853 println ! (
859854 "Total time spent computing symbol hashes: {}" ,
@@ -1135,6 +1130,13 @@ pub fn build_session_(
11351130 source_map : Lrc < source_map:: SourceMap > ,
11361131 driver_lint_caps : FxHashMap < lint:: LintId , lint:: Level > ,
11371132) -> Session {
1133+ let self_profiling_active = sopts. debugging_opts . self_profile ||
1134+ sopts. debugging_opts . profile_json ;
1135+
1136+ let self_profiler =
1137+ if self_profiling_active { Some ( Arc :: new ( PlMutex :: new ( SelfProfiler :: new ( ) ) ) ) }
1138+ else { None } ;
1139+
11381140 let host_triple = TargetTriple :: from_triple ( config:: host_triple ( ) ) ;
11391141 let host = Target :: search ( & host_triple) . unwrap_or_else ( |e|
11401142 span_diagnostic
@@ -1184,9 +1186,6 @@ pub fn build_session_(
11841186 CguReuseTracker :: new_disabled ( )
11851187 } ;
11861188
1187- let self_profiling_active = sopts. debugging_opts . self_profile ||
1188- sopts. debugging_opts . profile_json ;
1189-
11901189 let sess = Session {
11911190 target : target_cfg,
11921191 host,
@@ -1215,8 +1214,7 @@ pub fn build_session_(
12151214 imported_macro_spans : OneThread :: new ( RefCell :: new ( FxHashMap :: default ( ) ) ) ,
12161215 incr_comp_session : OneThread :: new ( RefCell :: new ( IncrCompSession :: NotInitialized ) ) ,
12171216 cgu_reuse_tracker,
1218- self_profiling_active,
1219- self_profiling : Lock :: new ( SelfProfiler :: new ( ) ) ,
1217+ self_profiling : self_profiler,
12201218 profile_channel : Lock :: new ( None ) ,
12211219 perf_stats : PerfStats {
12221220 symbol_hash_time : Lock :: new ( Duration :: from_secs ( 0 ) ) ,
0 commit comments