From 7aa7198b4b5116ace4d00fb38650c6e3a67c2776 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sun, 1 Apr 2018 08:17:25 +0200 Subject: [PATCH] Make PerfStats thread-safe and remove unused fields --- src/librustc/infer/canonical.rs | 4 +- src/librustc/session/mod.rs | 74 ++++--------------- src/librustc/util/common.rs | 17 ++--- .../normalize_erasing_regions.rs | 7 +- .../normalize_projection_ty.rs | 4 +- 5 files changed, 27 insertions(+), 79 deletions(-) diff --git a/src/librustc/infer/canonical.rs b/src/librustc/infer/canonical.rs index 4357c9a5a77..8ea6eb005a1 100644 --- a/src/librustc/infer/canonical.rs +++ b/src/librustc/infer/canonical.rs @@ -36,13 +36,13 @@ use serialize::UseSpecializedDecodable; use std::fmt::Debug; use std::ops::Index; +use std::sync::atomic::Ordering; use syntax::codemap::Span; use traits::{Obligation, ObligationCause, PredicateObligation}; use ty::{self, CanonicalVar, Lift, Region, Slice, Ty, TyCtxt, TypeFlags}; use ty::subst::{Kind, UnpackedKind}; use ty::fold::{TypeFoldable, TypeFolder}; use util::captures::Captures; -use util::common::CellUsizeExt; use rustc_data_structures::indexed_vec::IndexVec; use rustc_data_structures::fx::FxHashMap; @@ -473,7 +473,7 @@ pub fn canonicalize_query(&self, value: &V) -> (V::Canonicalized, CanonicalVa where V: Canonicalize<'gcx, 'tcx>, { - self.tcx.sess.perf_stats.queries_canonicalized.increment(); + self.tcx.sess.perf_stats.queries_canonicalized.fetch_add(1, Ordering::Relaxed); Canonicalizer::canonicalize( value, diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index afb62aca582..c084c868481 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -55,6 +55,7 @@ use std::path::{Path, PathBuf}; use std::time::Duration; use std::sync::mpsc; +use std::sync::atomic::{AtomicUsize, Ordering}; mod code_stats; pub mod config; @@ -165,27 +166,16 @@ pub struct Session { } pub struct PerfStats { - /// The accumulated time needed for computing the SVH of the crate - pub svh_time: Cell, - /// The accumulated time spent on computing incr. comp. hashes - pub incr_comp_hashes_time: Cell, - /// The number of incr. comp. hash computations performed - pub incr_comp_hashes_count: Cell, - /// The number of bytes hashed when computing ICH values - pub incr_comp_bytes_hashed: Cell, /// The accumulated time spent on computing symbol hashes - pub symbol_hash_time: Cell, + pub symbol_hash_time: Lock, /// The accumulated time spent decoding def path tables from metadata - pub decode_def_path_tables_time: Cell, + pub decode_def_path_tables_time: Lock, /// Total number of values canonicalized queries constructed. - pub queries_canonicalized: Cell, - /// Number of times we canonicalized a value and found that the - /// result had already been canonicalized. - pub canonicalized_values_allocated: Cell, + pub queries_canonicalized: AtomicUsize, /// Number of times this query is invoked. - pub normalize_ty_after_erasing_regions: Cell, + pub normalize_ty_after_erasing_regions: AtomicUsize, /// Number of times this query is invoked. - pub normalize_projection_ty: Cell, + pub normalize_projection_ty: AtomicUsize, } /// Enum to support dispatch of one-time diagnostics (in Session.diag_once) @@ -838,47 +828,20 @@ pub fn incr_comp_session_dir_opt(&self) -> Option> { } pub fn print_perf_stats(&self) { - println!( - "Total time spent computing SVHs: {}", - duration_to_secs_str(self.perf_stats.svh_time.get()) - ); - println!( - "Total time spent computing incr. comp. hashes: {}", - duration_to_secs_str(self.perf_stats.incr_comp_hashes_time.get()) - ); - println!( - "Total number of incr. comp. hashes computed: {}", - self.perf_stats.incr_comp_hashes_count.get() - ); - println!( - "Total number of bytes hashed for incr. comp.: {}", - self.perf_stats.incr_comp_bytes_hashed.get() - ); - if self.perf_stats.incr_comp_hashes_count.get() != 0 { - println!( - "Average bytes hashed per incr. comp. HIR node: {}", - self.perf_stats.incr_comp_bytes_hashed.get() - / self.perf_stats.incr_comp_hashes_count.get() - ); - } else { - println!("Average bytes hashed per incr. comp. HIR node: N/A"); - } println!( "Total time spent computing symbol hashes: {}", - duration_to_secs_str(self.perf_stats.symbol_hash_time.get()) + duration_to_secs_str(*self.perf_stats.symbol_hash_time.lock()) ); println!( "Total time spent decoding DefPath tables: {}", - duration_to_secs_str(self.perf_stats.decode_def_path_tables_time.get()) + duration_to_secs_str(*self.perf_stats.decode_def_path_tables_time.lock()) ); println!("Total queries canonicalized: {}", - self.perf_stats.queries_canonicalized.get()); - println!("Total canonical values interned: {}", - self.perf_stats.canonicalized_values_allocated.get()); + self.perf_stats.queries_canonicalized.load(Ordering::Relaxed)); println!("normalize_ty_after_erasing_regions: {}", - self.perf_stats.normalize_ty_after_erasing_regions.get()); + self.perf_stats.normalize_ty_after_erasing_regions.load(Ordering::Relaxed)); println!("normalize_projection_ty: {}", - self.perf_stats.normalize_projection_ty.get()); + self.perf_stats.normalize_projection_ty.load(Ordering::Relaxed)); } /// We want to know if we're allowed to do an optimization for crate foo from -z fuel=foo=n. @@ -1160,16 +1123,11 @@ pub fn build_session_( ignored_attr_names: ich::compute_ignored_attr_names(), profile_channel: Lock::new(None), perf_stats: PerfStats { - svh_time: Cell::new(Duration::from_secs(0)), - incr_comp_hashes_time: Cell::new(Duration::from_secs(0)), - incr_comp_hashes_count: Cell::new(0), - incr_comp_bytes_hashed: Cell::new(0), - symbol_hash_time: Cell::new(Duration::from_secs(0)), - decode_def_path_tables_time: Cell::new(Duration::from_secs(0)), - queries_canonicalized: Cell::new(0), - canonicalized_values_allocated: Cell::new(0), - normalize_ty_after_erasing_regions: Cell::new(0), - normalize_projection_ty: Cell::new(0), + symbol_hash_time: Lock::new(Duration::from_secs(0)), + decode_def_path_tables_time: Lock::new(Duration::from_secs(0)), + queries_canonicalized: AtomicUsize::new(0), + normalize_ty_after_erasing_regions: AtomicUsize::new(0), + normalize_projection_ty: AtomicUsize::new(0), }, code_stats: RefCell::new(CodeStats::new()), optimization_fuel_crate, diff --git a/src/librustc/util/common.rs b/src/librustc/util/common.rs index 32ec837f031..bb6aa654c29 100644 --- a/src/librustc/util/common.rs +++ b/src/librustc/util/common.rs @@ -10,6 +10,8 @@ #![allow(non_camel_case_types)] +use rustc_data_structures::sync::Lock; + use std::cell::{RefCell, Cell}; use std::collections::HashMap; use std::ffi::CString; @@ -236,13 +238,14 @@ pub fn to_readable_str(mut val: usize) -> String { groups.join("_") } -pub fn record_time(accu: &Cell, f: F) -> T where +pub fn record_time(accu: &Lock, f: F) -> T where F: FnOnce() -> T, { let start = Instant::now(); let rv = f(); let duration = start.elapsed(); - accu.set(duration + accu.get()); + let mut accu = accu.lock(); + *accu = *accu + duration; rv } @@ -382,13 +385,3 @@ fn test_to_readable_str() { assert_eq!("1_000_000", to_readable_str(1_000_000)); assert_eq!("1_234_567", to_readable_str(1_234_567)); } - -pub trait CellUsizeExt { - fn increment(&self); -} - -impl CellUsizeExt for Cell { - fn increment(&self) { - self.set(self.get() + 1); - } -} diff --git a/src/librustc_traits/normalize_erasing_regions.rs b/src/librustc_traits/normalize_erasing_regions.rs index 14f8694dbf7..1857df5717b 100644 --- a/src/librustc_traits/normalize_erasing_regions.rs +++ b/src/librustc_traits/normalize_erasing_regions.rs @@ -11,17 +11,14 @@ use rustc::traits::{Normalized, ObligationCause}; use rustc::traits::query::NoSolution; use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt}; -use rustc::util::common::CellUsizeExt; +use std::sync::atomic::Ordering; crate fn normalize_ty_after_erasing_regions<'tcx>( tcx: TyCtxt<'_, 'tcx, 'tcx>, goal: ParamEnvAnd<'tcx, Ty<'tcx>>, ) -> Ty<'tcx> { let ParamEnvAnd { param_env, value } = goal; - tcx.sess - .perf_stats - .normalize_ty_after_erasing_regions - .increment(); + tcx.sess.perf_stats.normalize_ty_after_erasing_regions.fetch_add(1, Ordering::Relaxed); tcx.infer_ctxt().enter(|infcx| { let cause = ObligationCause::dummy(); match infcx.at(&cause, param_env).normalize(&value) { diff --git a/src/librustc_traits/normalize_projection_ty.rs b/src/librustc_traits/normalize_projection_ty.rs index 62d5ef11551..8fc00c937e6 100644 --- a/src/librustc_traits/normalize_projection_ty.rs +++ b/src/librustc_traits/normalize_projection_ty.rs @@ -13,11 +13,11 @@ SelectionContext}; use rustc::traits::query::{CanonicalProjectionGoal, NoSolution, normalize::NormalizationResult}; use rustc::ty::{ParamEnvAnd, TyCtxt}; -use rustc::util::common::CellUsizeExt; use rustc_data_structures::sync::Lrc; use syntax::ast::DUMMY_NODE_ID; use syntax_pos::DUMMY_SP; use util; +use std::sync::atomic::Ordering; crate fn normalize_projection_ty<'tcx>( tcx: TyCtxt<'_, 'tcx, 'tcx>, @@ -25,7 +25,7 @@ ) -> Result>>>, NoSolution> { debug!("normalize_provider(goal={:#?})", goal); - tcx.sess.perf_stats.normalize_projection_ty.increment(); + tcx.sess.perf_stats.normalize_projection_ty.fetch_add(1, Ordering::Relaxed); tcx.infer_ctxt().enter(|ref infcx| { let ( ParamEnvAnd { -- GitLab