Skip to content

Commit f795e8a

Browse files
committed
Auto merge of #67397 - michaelwoerister:query-keys-in-self-profiling, r=wesleywiser
self-profiling: Support recording query keys This PR makes self-profiling able to record query keys. The implementation is not as efficient as it could be yet (all query keys except for `DefId`s cause string data to be duplicated) and the rendered strings could be nicer too. But the implementation is functional and introduces the basic framework for emitting per-query-invocation event data. I tried to add proper documentation on how everything works. Let me know if more documentation is needed. r? @wesleywiser @Mark-Simulacrum, heads up: This updates `measureme` to 0.7.0 which means that `summarize` on perf.rlo needs to be update accordingly once this is merged.
2 parents 2d8d559 + ad65e3e commit f795e8a

File tree

11 files changed

+550
-125
lines changed

11 files changed

+550
-125
lines changed

Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -1995,9 +1995,9 @@ dependencies = [
19951995

19961996
[[package]]
19971997
name = "measureme"
1998-
version = "0.5.0"
1998+
version = "0.7.1"
19991999
source = "registry+https://github.com/rust-lang/crates.io-index"
2000-
checksum = "c420bbc064623934620b5ab2dc0cf96451b34163329e82f95e7fa1b7b99a6ac8"
2000+
checksum = "fef709d3257013bba7cff14fc504e07e80631d3fe0f6d38ce63b8f6510ccb932"
20012001
dependencies = [
20022002
"byteorder",
20032003
"memmap",
@@ -3079,6 +3079,7 @@ dependencies = [
30793079
"graphviz",
30803080
"jobserver",
30813081
"log",
3082+
"measureme",
30823083
"parking_lot",
30833084
"polonius-engine",
30843085
"rustc-rayon",

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ parking_lot = "0.9"
3636
byteorder = { version = "1.3" }
3737
chalk-engine = { version = "0.9.0", default-features=false }
3838
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
39+
measureme = "0.7.1"
3940
rustc_error_codes = { path = "../librustc_error_codes" }
4041
rustc_session = { path = "../librustc_session" }

src/librustc/dep_graph/graph.rs

+28-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::ty::{self, TyCtxt};
22
use errors::Diagnostic;
33
use parking_lot::{Condvar, Mutex};
44
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
5+
use rustc_data_structures::profiling::QueryInvocationId;
56
use rustc_data_structures::sharded::{self, Sharded};
67
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
78
use rustc_data_structures::sync::{AtomicU32, AtomicU64, Lock, Lrc, Ordering};
@@ -11,7 +12,7 @@ use std::collections::hash_map::Entry;
1112
use std::env;
1213
use std::hash::Hash;
1314
use std::mem;
14-
use std::sync::atomic::Ordering::SeqCst;
15+
use std::sync::atomic::Ordering::Relaxed;
1516

1617
use crate::ich::{Fingerprint, StableHashingContext, StableHashingContextProvider};
1718

@@ -25,6 +26,12 @@ use super::serialized::{SerializedDepGraph, SerializedDepNodeIndex};
2526
#[derive(Clone)]
2627
pub struct DepGraph {
2728
data: Option<Lrc<DepGraphData>>,
29+
30+
/// This field is used for assigning DepNodeIndices when running in
31+
/// non-incremental mode. Even in non-incremental mode we make sure that
32+
/// each task has a `DepNodeIndex` that uniquely identifies it. This unique
33+
/// ID is used for self-profiling.
34+
virtual_dep_node_index: Lrc<AtomicU32>,
2835
}
2936

3037
rustc_index::newtype_index! {
@@ -35,6 +42,13 @@ impl DepNodeIndex {
3542
pub const INVALID: DepNodeIndex = DepNodeIndex::MAX;
3643
}
3744

45+
impl std::convert::From<DepNodeIndex> for QueryInvocationId {
46+
#[inline]
47+
fn from(dep_node_index: DepNodeIndex) -> Self {
48+
QueryInvocationId(dep_node_index.as_u32())
49+
}
50+
}
51+
3852
#[derive(PartialEq)]
3953
pub enum DepNodeColor {
4054
Red,
@@ -105,11 +119,12 @@ impl DepGraph {
105119
previous: prev_graph,
106120
colors: DepNodeColorMap::new(prev_graph_node_count),
107121
})),
122+
virtual_dep_node_index: Lrc::new(AtomicU32::new(0)),
108123
}
109124
}
110125

111126
pub fn new_disabled() -> DepGraph {
112-
DepGraph { data: None }
127+
DepGraph { data: None, virtual_dep_node_index: Lrc::new(AtomicU32::new(0)) }
113128
}
114129

115130
/// Returns `true` if we are actually building the full dep-graph, and `false` otherwise.
@@ -322,7 +337,7 @@ impl DepGraph {
322337

323338
(result, dep_node_index)
324339
} else {
325-
(task(cx, arg), DepNodeIndex::INVALID)
340+
(task(cx, arg), self.next_virtual_depnode_index())
326341
}
327342
}
328343

@@ -352,7 +367,7 @@ impl DepGraph {
352367
let dep_node_index = data.current.complete_anon_task(dep_kind, task_deps);
353368
(result, dep_node_index)
354369
} else {
355-
(op(), DepNodeIndex::INVALID)
370+
(op(), self.next_virtual_depnode_index())
356371
}
357372
}
358373

@@ -478,8 +493,8 @@ impl DepGraph {
478493
let current_dep_graph = &self.data.as_ref().unwrap().current;
479494

480495
Some((
481-
current_dep_graph.total_read_count.load(SeqCst),
482-
current_dep_graph.total_duplicate_read_count.load(SeqCst),
496+
current_dep_graph.total_read_count.load(Relaxed),
497+
current_dep_graph.total_duplicate_read_count.load(Relaxed),
483498
))
484499
} else {
485500
None
@@ -877,6 +892,11 @@ impl DepGraph {
877892
}
878893
}
879894
}
895+
896+
fn next_virtual_depnode_index(&self) -> DepNodeIndex {
897+
let index = self.virtual_dep_node_index.fetch_add(1, Relaxed);
898+
DepNodeIndex::from_u32(index)
899+
}
880900
}
881901

882902
/// A "work product" is an intermediate result that we save into the
@@ -1087,7 +1107,7 @@ impl DepGraphData {
10871107
if let Some(task_deps) = icx.task_deps {
10881108
let mut task_deps = task_deps.lock();
10891109
if cfg!(debug_assertions) {
1090-
self.current.total_read_count.fetch_add(1, SeqCst);
1110+
self.current.total_read_count.fetch_add(1, Relaxed);
10911111
}
10921112
if task_deps.read_set.insert(source) {
10931113
task_deps.reads.push(source);
@@ -1105,7 +1125,7 @@ impl DepGraphData {
11051125
}
11061126
}
11071127
} else if cfg!(debug_assertions) {
1108-
self.current.total_duplicate_read_count.fetch_add(1, SeqCst);
1128+
self.current.total_duplicate_read_count.fetch_add(1, Relaxed);
11091129
}
11101130
}
11111131
})

src/librustc/ty/query/config.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ use crate::dep_graph::SerializedDepNodeIndex;
22
use crate::dep_graph::{DepKind, DepNode};
33
use crate::ty::query::plumbing::CycleError;
44
use crate::ty::query::queries;
5-
use crate::ty::query::QueryCache;
6-
use crate::ty::query::{Query, QueryName};
5+
use crate::ty::query::{Query, QueryCache};
76
use crate::ty::TyCtxt;
87
use rustc_data_structures::profiling::ProfileCategory;
98
use rustc_hir::def_id::{CrateNum, DefId};
@@ -20,7 +19,7 @@ use std::hash::Hash;
2019
// FIXME(eddyb) false positive, the lifetime parameter is used for `Key`/`Value`.
2120
#[allow(unused_lifetimes)]
2221
pub trait QueryConfig<'tcx> {
23-
const NAME: QueryName;
22+
const NAME: &'static str;
2423
const CATEGORY: ProfileCategory;
2524

2625
type Key: Eq + Hash + Clone + Debug;

src/librustc/ty/query/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ pub(crate) use self::config::QueryDescription;
8181
mod on_disk_cache;
8282
pub use self::on_disk_cache::OnDiskCache;
8383

84+
mod profiling_support;
85+
pub use self::profiling_support::{IntoSelfProfilingString, QueryKeyStringBuilder};
86+
8487
// Each of these queries corresponds to a function pointer field in the
8588
// `Providers` struct for requesting a value of that type, and a method
8689
// on `tcx: TyCtxt` (and `tcx.at(span)`) for doing that request in a way

0 commit comments

Comments
 (0)