Skip to content

Commit 7aa7198

Browse files
committed
Make PerfStats thread-safe and remove unused fields
1 parent 05c4ea4 commit 7aa7198

File tree

5 files changed

+27
-79
lines changed

5 files changed

+27
-79
lines changed

src/librustc/infer/canonical.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ use rustc_data_structures::indexed_vec::Idx;
3636
use serialize::UseSpecializedDecodable;
3737
use std::fmt::Debug;
3838
use std::ops::Index;
39+
use std::sync::atomic::Ordering;
3940
use syntax::codemap::Span;
4041
use traits::{Obligation, ObligationCause, PredicateObligation};
4142
use ty::{self, CanonicalVar, Lift, Region, Slice, Ty, TyCtxt, TypeFlags};
4243
use ty::subst::{Kind, UnpackedKind};
4344
use ty::fold::{TypeFoldable, TypeFolder};
4445
use util::captures::Captures;
45-
use util::common::CellUsizeExt;
4646

4747
use rustc_data_structures::indexed_vec::IndexVec;
4848
use rustc_data_structures::fx::FxHashMap;
@@ -473,7 +473,7 @@ impl<'cx, 'gcx, 'tcx> InferCtxt<'cx, 'gcx, 'tcx> {
473473
where
474474
V: Canonicalize<'gcx, 'tcx>,
475475
{
476-
self.tcx.sess.perf_stats.queries_canonicalized.increment();
476+
self.tcx.sess.perf_stats.queries_canonicalized.fetch_add(1, Ordering::Relaxed);
477477

478478
Canonicalizer::canonicalize(
479479
value,

src/librustc/session/mod.rs

Lines changed: 16 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use std::io::Write;
5555
use std::path::{Path, PathBuf};
5656
use std::time::Duration;
5757
use std::sync::mpsc;
58+
use std::sync::atomic::{AtomicUsize, Ordering};
5859

5960
mod code_stats;
6061
pub mod config;
@@ -165,27 +166,16 @@ pub struct Session {
165166
}
166167

167168
pub struct PerfStats {
168-
/// The accumulated time needed for computing the SVH of the crate
169-
pub svh_time: Cell<Duration>,
170-
/// The accumulated time spent on computing incr. comp. hashes
171-
pub incr_comp_hashes_time: Cell<Duration>,
172-
/// The number of incr. comp. hash computations performed
173-
pub incr_comp_hashes_count: Cell<u64>,
174-
/// The number of bytes hashed when computing ICH values
175-
pub incr_comp_bytes_hashed: Cell<u64>,
176169
/// The accumulated time spent on computing symbol hashes
177-
pub symbol_hash_time: Cell<Duration>,
170+
pub symbol_hash_time: Lock<Duration>,
178171
/// The accumulated time spent decoding def path tables from metadata
179-
pub decode_def_path_tables_time: Cell<Duration>,
172+
pub decode_def_path_tables_time: Lock<Duration>,
180173
/// Total number of values canonicalized queries constructed.
181-
pub queries_canonicalized: Cell<usize>,
182-
/// Number of times we canonicalized a value and found that the
183-
/// result had already been canonicalized.
184-
pub canonicalized_values_allocated: Cell<usize>,
174+
pub queries_canonicalized: AtomicUsize,
185175
/// Number of times this query is invoked.
186-
pub normalize_ty_after_erasing_regions: Cell<usize>,
176+
pub normalize_ty_after_erasing_regions: AtomicUsize,
187177
/// Number of times this query is invoked.
188-
pub normalize_projection_ty: Cell<usize>,
178+
pub normalize_projection_ty: AtomicUsize,
189179
}
190180

191181
/// Enum to support dispatch of one-time diagnostics (in Session.diag_once)
@@ -838,47 +828,20 @@ impl Session {
838828
}
839829

840830
pub fn print_perf_stats(&self) {
841-
println!(
842-
"Total time spent computing SVHs: {}",
843-
duration_to_secs_str(self.perf_stats.svh_time.get())
844-
);
845-
println!(
846-
"Total time spent computing incr. comp. hashes: {}",
847-
duration_to_secs_str(self.perf_stats.incr_comp_hashes_time.get())
848-
);
849-
println!(
850-
"Total number of incr. comp. hashes computed: {}",
851-
self.perf_stats.incr_comp_hashes_count.get()
852-
);
853-
println!(
854-
"Total number of bytes hashed for incr. comp.: {}",
855-
self.perf_stats.incr_comp_bytes_hashed.get()
856-
);
857-
if self.perf_stats.incr_comp_hashes_count.get() != 0 {
858-
println!(
859-
"Average bytes hashed per incr. comp. HIR node: {}",
860-
self.perf_stats.incr_comp_bytes_hashed.get()
861-
/ self.perf_stats.incr_comp_hashes_count.get()
862-
);
863-
} else {
864-
println!("Average bytes hashed per incr. comp. HIR node: N/A");
865-
}
866831
println!(
867832
"Total time spent computing symbol hashes: {}",
868-
duration_to_secs_str(self.perf_stats.symbol_hash_time.get())
833+
duration_to_secs_str(*self.perf_stats.symbol_hash_time.lock())
869834
);
870835
println!(
871836
"Total time spent decoding DefPath tables: {}",
872-
duration_to_secs_str(self.perf_stats.decode_def_path_tables_time.get())
837+
duration_to_secs_str(*self.perf_stats.decode_def_path_tables_time.lock())
873838
);
874839
println!("Total queries canonicalized: {}",
875-
self.perf_stats.queries_canonicalized.get());
876-
println!("Total canonical values interned: {}",
877-
self.perf_stats.canonicalized_values_allocated.get());
840+
self.perf_stats.queries_canonicalized.load(Ordering::Relaxed));
878841
println!("normalize_ty_after_erasing_regions: {}",
879-
self.perf_stats.normalize_ty_after_erasing_regions.get());
842+
self.perf_stats.normalize_ty_after_erasing_regions.load(Ordering::Relaxed));
880843
println!("normalize_projection_ty: {}",
881-
self.perf_stats.normalize_projection_ty.get());
844+
self.perf_stats.normalize_projection_ty.load(Ordering::Relaxed));
882845
}
883846

884847
/// 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_(
11601123
ignored_attr_names: ich::compute_ignored_attr_names(),
11611124
profile_channel: Lock::new(None),
11621125
perf_stats: PerfStats {
1163-
svh_time: Cell::new(Duration::from_secs(0)),
1164-
incr_comp_hashes_time: Cell::new(Duration::from_secs(0)),
1165-
incr_comp_hashes_count: Cell::new(0),
1166-
incr_comp_bytes_hashed: Cell::new(0),
1167-
symbol_hash_time: Cell::new(Duration::from_secs(0)),
1168-
decode_def_path_tables_time: Cell::new(Duration::from_secs(0)),
1169-
queries_canonicalized: Cell::new(0),
1170-
canonicalized_values_allocated: Cell::new(0),
1171-
normalize_ty_after_erasing_regions: Cell::new(0),
1172-
normalize_projection_ty: Cell::new(0),
1126+
symbol_hash_time: Lock::new(Duration::from_secs(0)),
1127+
decode_def_path_tables_time: Lock::new(Duration::from_secs(0)),
1128+
queries_canonicalized: AtomicUsize::new(0),
1129+
normalize_ty_after_erasing_regions: AtomicUsize::new(0),
1130+
normalize_projection_ty: AtomicUsize::new(0),
11731131
},
11741132
code_stats: RefCell::new(CodeStats::new()),
11751133
optimization_fuel_crate,

src/librustc/util/common.rs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
#![allow(non_camel_case_types)]
1212

13+
use rustc_data_structures::sync::Lock;
14+
1315
use std::cell::{RefCell, Cell};
1416
use std::collections::HashMap;
1517
use std::ffi::CString;
@@ -236,13 +238,14 @@ pub fn to_readable_str(mut val: usize) -> String {
236238
groups.join("_")
237239
}
238240

239-
pub fn record_time<T, F>(accu: &Cell<Duration>, f: F) -> T where
241+
pub fn record_time<T, F>(accu: &Lock<Duration>, f: F) -> T where
240242
F: FnOnce() -> T,
241243
{
242244
let start = Instant::now();
243245
let rv = f();
244246
let duration = start.elapsed();
245-
accu.set(duration + accu.get());
247+
let mut accu = accu.lock();
248+
*accu = *accu + duration;
246249
rv
247250
}
248251

@@ -382,13 +385,3 @@ fn test_to_readable_str() {
382385
assert_eq!("1_000_000", to_readable_str(1_000_000));
383386
assert_eq!("1_234_567", to_readable_str(1_234_567));
384387
}
385-
386-
pub trait CellUsizeExt {
387-
fn increment(&self);
388-
}
389-
390-
impl CellUsizeExt for Cell<usize> {
391-
fn increment(&self) {
392-
self.set(self.get() + 1);
393-
}
394-
}

src/librustc_traits/normalize_erasing_regions.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,14 @@
1111
use rustc::traits::{Normalized, ObligationCause};
1212
use rustc::traits::query::NoSolution;
1313
use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt};
14-
use rustc::util::common::CellUsizeExt;
14+
use std::sync::atomic::Ordering;
1515

1616
crate fn normalize_ty_after_erasing_regions<'tcx>(
1717
tcx: TyCtxt<'_, 'tcx, 'tcx>,
1818
goal: ParamEnvAnd<'tcx, Ty<'tcx>>,
1919
) -> Ty<'tcx> {
2020
let ParamEnvAnd { param_env, value } = goal;
21-
tcx.sess
22-
.perf_stats
23-
.normalize_ty_after_erasing_regions
24-
.increment();
21+
tcx.sess.perf_stats.normalize_ty_after_erasing_regions.fetch_add(1, Ordering::Relaxed);
2522
tcx.infer_ctxt().enter(|infcx| {
2623
let cause = ObligationCause::dummy();
2724
match infcx.at(&cause, param_env).normalize(&value) {

src/librustc_traits/normalize_projection_ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@ use rustc::traits::{self, FulfillmentContext, Normalized, ObligationCause,
1313
SelectionContext};
1414
use rustc::traits::query::{CanonicalProjectionGoal, NoSolution, normalize::NormalizationResult};
1515
use rustc::ty::{ParamEnvAnd, TyCtxt};
16-
use rustc::util::common::CellUsizeExt;
1716
use rustc_data_structures::sync::Lrc;
1817
use syntax::ast::DUMMY_NODE_ID;
1918
use syntax_pos::DUMMY_SP;
2019
use util;
20+
use std::sync::atomic::Ordering;
2121

2222
crate fn normalize_projection_ty<'tcx>(
2323
tcx: TyCtxt<'_, 'tcx, 'tcx>,
2424
goal: CanonicalProjectionGoal<'tcx>,
2525
) -> Result<Lrc<Canonical<'tcx, QueryResult<'tcx, NormalizationResult<'tcx>>>>, NoSolution> {
2626
debug!("normalize_provider(goal={:#?})", goal);
2727

28-
tcx.sess.perf_stats.normalize_projection_ty.increment();
28+
tcx.sess.perf_stats.normalize_projection_ty.fetch_add(1, Ordering::Relaxed);
2929
tcx.infer_ctxt().enter(|ref infcx| {
3030
let (
3131
ParamEnvAnd {

0 commit comments

Comments
 (0)