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