Skip to content

Commit 3d95330

Browse files
authored
Rollup merge of #87404 - rylev:artifact-size-profiling, r=wesleywiser
Add support for artifact size profiling This adds support for profiling artifact file sizes (incremental compilation artifacts and query cache to begin with). Eventually we want to track this in perf.rlo so we can ensure that file sizes do not change dramatically on each pull request. This relies on support in measureme: rust-lang/measureme#169. Once that lands we can update this PR to not point to a git dependency. This was worked on together with `@michaelwoerister.` r? `@wesleywiser`
2 parents ca6798a + 757f76e commit 3d95330

File tree

8 files changed

+76
-15
lines changed

8 files changed

+76
-15
lines changed

Cargo.lock

+19-5
Original file line numberDiff line numberDiff line change
@@ -2143,6 +2143,20 @@ dependencies = [
21432143
"smallvec",
21442144
]
21452145

2146+
[[package]]
2147+
name = "measureme"
2148+
version = "10.0.0"
2149+
source = "registry+https://github.com/rust-lang/crates.io-index"
2150+
checksum = "bd460fad6e55ca82fa0cd9dab0d315294188fd9ec6efbf4105e5635d4872ef9c"
2151+
dependencies = [
2152+
"log",
2153+
"memmap2",
2154+
"parking_lot",
2155+
"perf-event-open-sys",
2156+
"rustc-hash",
2157+
"smallvec",
2158+
]
2159+
21462160
[[package]]
21472161
name = "memchr"
21482162
version = "2.4.1"
@@ -2247,7 +2261,7 @@ dependencies = [
22472261
"hex 0.4.2",
22482262
"libc",
22492263
"log",
2250-
"measureme",
2264+
"measureme 9.1.2",
22512265
"rand 0.8.4",
22522266
"rustc-workspace-hack",
22532267
"rustc_version 0.4.0",
@@ -3235,7 +3249,7 @@ dependencies = [
32353249
"indexmap",
32363250
"jobserver",
32373251
"libc",
3238-
"measureme",
3252+
"measureme 9.1.2",
32393253
"memmap2",
32403254
"parking_lot",
32413255
"rustc-ap-rustc_graphviz",
@@ -3674,7 +3688,7 @@ dependencies = [
36743688
"bitflags",
36753689
"cstr",
36763690
"libc",
3677-
"measureme",
3691+
"measureme 10.0.0",
36783692
"rustc-demangle",
36793693
"rustc_arena",
36803694
"rustc_ast",
@@ -3767,7 +3781,7 @@ dependencies = [
37673781
"indexmap",
37683782
"jobserver",
37693783
"libc",
3770-
"measureme",
3784+
"measureme 10.0.0",
37713785
"memmap2",
37723786
"parking_lot",
37733787
"rustc-hash",
@@ -4292,7 +4306,7 @@ dependencies = [
42924306
name = "rustc_query_impl"
42934307
version = "0.0.0"
42944308
dependencies = [
4295-
"measureme",
4309+
"measureme 10.0.0",
42964310
"rustc-rayon-core",
42974311
"rustc_ast",
42984312
"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 = "10.0.0"
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 = "10.0.0"
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 = "10.0.0"
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
@@ -1283,7 +1283,7 @@ options! {
12831283
"specify the events recorded by the self profiler;
12841284
for example: `-Z self-profile-events=default,query-keys`
12851285
all options: none, all, default, generic-activity, query-provider, query-cache-hit
1286-
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm"),
1286+
query-blocked, incr-cache-load, incr-result-hashing, query-keys, function-args, args, llvm, artifact-sizes"),
12871287
share_generics: Option<bool> = (None, parse_opt_bool, [TRACKED],
12881288
"make the current crate share its generic instantiations"),
12891289
show_span: Option<String> = (None, parse_opt_string, [TRACKED],

0 commit comments

Comments
 (0)