Skip to content

Commit 947a33b

Browse files
committed
Add support for artifact size profiling
1 parent ca8078d commit 947a33b

File tree

8 files changed

+76
-16
lines changed

8 files changed

+76
-16
lines changed

Cargo.lock

+19-6
Original file line numberDiff line numberDiff line change
@@ -2138,6 +2138,19 @@ dependencies = [
21382138
"smallvec",
21392139
]
21402140

2141+
[[package]]
2142+
name = "measureme"
2143+
version = "9.1.2"
2144+
source = "git+https://github.com/rylev/measureme#b9cccd7ad4c859a5e0e3dd6bff3daac7a190bdd7"
2145+
dependencies = [
2146+
"log",
2147+
"memmap2",
2148+
"parking_lot",
2149+
"perf-event-open-sys",
2150+
"rustc-hash",
2151+
"smallvec",
2152+
]
2153+
21412154
[[package]]
21422155
name = "memchr"
21432156
version = "2.4.1"
@@ -2242,8 +2255,8 @@ dependencies = [
22422255
"hex 0.4.2",
22432256
"libc",
22442257
"log",
2245-
"measureme",
2246-
"rand 0.8.4",
2258+
"measureme 9.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
2259+
"rand 0.8.3",
22472260
"rustc-workspace-hack",
22482261
"rustc_version 0.4.0",
22492262
"shell-escape",
@@ -3219,7 +3232,7 @@ dependencies = [
32193232
"indexmap",
32203233
"jobserver",
32213234
"libc",
3222-
"measureme",
3235+
"measureme 9.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
32233236
"memmap2",
32243237
"parking_lot",
32253238
"rustc-ap-rustc_graphviz",
@@ -3657,7 +3670,7 @@ dependencies = [
36573670
"bitflags",
36583671
"cstr",
36593672
"libc",
3660-
"measureme",
3673+
"measureme 9.1.2 (git+https://github.com/rylev/measureme)",
36613674
"rustc-demangle",
36623675
"rustc_arena",
36633676
"rustc_ast",
@@ -3752,7 +3765,7 @@ dependencies = [
37523765
"indexmap",
37533766
"jobserver",
37543767
"libc",
3755-
"measureme",
3768+
"measureme 9.1.2 (git+https://github.com/rylev/measureme)",
37563769
"memmap2",
37573770
"parking_lot",
37583771
"rustc-hash",
@@ -4276,7 +4289,7 @@ dependencies = [
42764289
name = "rustc_query_impl"
42774290
version = "0.0.0"
42784291
dependencies = [
4279-
"measureme",
4292+
"measureme 9.1.2 (git+https://github.com/rylev/measureme)",
42804293
"rustc-rayon-core",
42814294
"rustc_ast",
42824295
"rustc_data_structures",

compiler/rustc_codegen_llvm/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ doctest = false
1111
bitflags = "1.0"
1212
cstr = "0.2"
1313
libc = "0.2"
14-
measureme = "9.1.0"
14+
measureme = { git = "https://github.com/rylev/measureme" }
1515
snap = "1"
1616
tracing = "0.1"
1717
rustc_middle = { path = "../rustc_middle" }

compiler/rustc_data_structures/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ rustc-hash = "1.1.0"
2323
smallvec = { version = "1.6.1", features = ["union", "may_dangle"] }
2424
rustc_index = { path = "../rustc_index", package = "rustc_index" }
2525
bitflags = "1.2.1"
26-
measureme = "9.1.0"
26+
measureme = { git = "https://github.com/rylev/measureme" }
2727
libc = "0.2"
2828
stacker = "0.1.14"
2929
tempfile = "3.2"

compiler/rustc_data_structures/src/profiling.rs

+40-3
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,14 @@ bitflags::bitflags! {
110110
const FUNCTION_ARGS = 1 << 6;
111111
const LLVM = 1 << 7;
112112
const INCR_RESULT_HASHING = 1 << 8;
113+
const ARTIFACT_SIZES = 1 << 9;
113114

114115
const DEFAULT = Self::GENERIC_ACTIVITIES.bits |
115116
Self::QUERY_PROVIDERS.bits |
116117
Self::QUERY_BLOCKED.bits |
117118
Self::INCR_CACHE_LOADS.bits |
118-
Self::INCR_RESULT_HASHING.bits;
119+
Self::INCR_RESULT_HASHING.bits |
120+
Self::ARTIFACT_SIZES.bits;
119121

120122
const ARGS = Self::QUERY_KEYS.bits | Self::FUNCTION_ARGS.bits;
121123
}
@@ -136,6 +138,7 @@ const EVENT_FILTERS_BY_NAME: &[(&str, EventFilter)] = &[
136138
("args", EventFilter::ARGS),
137139
("llvm", EventFilter::LLVM),
138140
("incr-result-hashing", EventFilter::INCR_RESULT_HASHING),
141+
("artifact-sizes", EventFilter::ARTIFACT_SIZES),
139142
];
140143

141144
/// Something that uniquely identifies a query invocation.
@@ -285,6 +288,33 @@ impl SelfProfilerRef {
285288
})
286289
}
287290

291+
/// Record the size of an artifact that the compiler produces
292+
///
293+
/// `artifact_kind` is the class of artifact (e.g., query_cache, object_file, etc.)
294+
/// `artifact_name` is an identifier to the specific artifact being stored (usually a filename)
295+
#[inline(always)]
296+
pub fn artifact_size<A>(&self, artifact_kind: &str, artifact_name: A, size: u64)
297+
where
298+
A: Borrow<str> + Into<String>,
299+
{
300+
drop(self.exec(EventFilter::ARTIFACT_SIZES, |profiler| {
301+
let builder = EventIdBuilder::new(&profiler.profiler);
302+
let event_label = profiler.get_or_alloc_cached_string(artifact_kind);
303+
let event_arg = profiler.get_or_alloc_cached_string(artifact_name);
304+
let event_id = builder.from_label_and_arg(event_label, event_arg);
305+
let thread_id = get_thread_id();
306+
307+
profiler.profiler.record_integer_event(
308+
profiler.artifact_size_event_kind,
309+
event_id,
310+
thread_id,
311+
size,
312+
);
313+
314+
TimingGuard::none()
315+
}))
316+
}
317+
288318
#[inline(always)]
289319
pub fn generic_activity_with_args(
290320
&self,
@@ -372,7 +402,7 @@ impl SelfProfilerRef {
372402
) {
373403
drop(self.exec(event_filter, |profiler| {
374404
let event_id = StringId::new_virtual(query_invocation_id.0);
375-
let thread_id = std::thread::current().id().as_u64().get() as u32;
405+
let thread_id = get_thread_id();
376406

377407
profiler.profiler.record_instant_event(
378408
event_kind(profiler),
@@ -425,6 +455,7 @@ pub struct SelfProfiler {
425455
incremental_result_hashing_event_kind: StringId,
426456
query_blocked_event_kind: StringId,
427457
query_cache_hit_event_kind: StringId,
458+
artifact_size_event_kind: StringId,
428459
}
429460

430461
impl SelfProfiler {
@@ -447,6 +478,7 @@ impl SelfProfiler {
447478
profiler.alloc_string("IncrementalResultHashing");
448479
let query_blocked_event_kind = profiler.alloc_string("QueryBlocked");
449480
let query_cache_hit_event_kind = profiler.alloc_string("QueryCacheHit");
481+
let artifact_size_event_kind = profiler.alloc_string("ArtifactSize");
450482

451483
let mut event_filter_mask = EventFilter::empty();
452484

@@ -491,6 +523,7 @@ impl SelfProfiler {
491523
incremental_result_hashing_event_kind,
492524
query_blocked_event_kind,
493525
query_cache_hit_event_kind,
526+
artifact_size_event_kind,
494527
})
495528
}
496529

@@ -561,7 +594,7 @@ impl<'a> TimingGuard<'a> {
561594
event_kind: StringId,
562595
event_id: EventId,
563596
) -> TimingGuard<'a> {
564-
let thread_id = std::thread::current().id().as_u64().get() as u32;
597+
let thread_id = get_thread_id();
565598
let raw_profiler = &profiler.profiler;
566599
let timing_guard =
567600
raw_profiler.start_recording_interval_event(event_kind, event_id, thread_id);
@@ -655,6 +688,10 @@ pub fn duration_to_secs_str(dur: std::time::Duration) -> String {
655688
format!("{:.3}", dur.as_secs_f64())
656689
}
657690

691+
fn get_thread_id() -> u32 {
692+
std::thread::current().id().as_u64().get() as u32
693+
}
694+
658695
// Memory reporting
659696
cfg_if! {
660697
if #[cfg(windows)] {

compiler/rustc_incremental/src/persist/file_format.rs

+6
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ where
9595
return;
9696
}
9797

98+
sess.prof.artifact_size(
99+
&name.replace(' ', "_"),
100+
path_buf.file_name().unwrap().to_string_lossy(),
101+
encoder.position() as u64,
102+
);
103+
98104
debug!("save: data written to disk successfully");
99105
}
100106

compiler/rustc_query_impl/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
doctest = false
88

99
[dependencies]
10-
measureme = "9.0.0"
10+
measureme = { git = "https://github.com/rylev/measureme" }
1111
rustc-rayon-core = "0.3.1"
1212
tracing = "0.1"
1313
rustc_ast = { path = "../rustc_ast" }

compiler/rustc_query_system/src/dep_graph/serialized.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ impl<K: DepKind> EncoderState<K> {
222222
index
223223
}
224224

225-
fn finish(self) -> FileEncodeResult {
225+
fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
226226
let Self { mut encoder, total_node_count, total_edge_count, result, stats: _ } = self;
227227
let () = result?;
228228

@@ -235,7 +235,11 @@ impl<K: DepKind> EncoderState<K> {
235235
IntEncodedWithFixedSize(edge_count).encode(&mut encoder)?;
236236
debug!("position: {:?}", encoder.position());
237237
// Drop the encoder so that nothing is written after the counts.
238-
encoder.flush()
238+
let result = encoder.flush();
239+
// FIXME(rylev): we hardcode the dep graph file name so we don't need a dependency on
240+
// rustc_incremental just for that.
241+
profiler.artifact_size("dep_graph", "dep-graph.bin", encoder.position() as u64);
242+
result
239243
}
240244
}
241245

@@ -332,6 +336,6 @@ impl<K: DepKind + Encodable<FileEncoder>> GraphEncoder<K> {
332336

333337
pub fn finish(self, profiler: &SelfProfilerRef) -> FileEncodeResult {
334338
let _prof_timer = profiler.generic_activity("incr_comp_encode_dep_graph");
335-
self.status.into_inner().finish()
339+
self.status.into_inner().finish(profiler)
336340
}
337341
}

compiler/rustc_session/src/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ options! {
12791279
"specify the events recorded by the self profiler;
12801280
for example: `-Z self-profile-events=default,query-keys`
12811281
all options: none, all, default, generic-activity, query-provider, query-cache-hit
1282-
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm"),
1282+
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm, artifact-sizes"),
12831283
share_generics: Option<bool> = (None, parse_opt_bool, [TRACKED],
12841284
"make the current crate share its generic instantiations"),
12851285
show_span: Option<String> = (None, parse_opt_string, [TRACKED],

0 commit comments

Comments
 (0)