Skip to content

Commit 666b1b6

Browse files
committed
Tweak thread names for CGU processing.
For non-incremental builds on Unix, currently all the thread names look like `opt regex.f10ba03eb5ec7975-cgu.0`. But they are truncated by `pthread_setname` to `opt regex.f10ba`, hiding the numeric suffix that distinguishes them. This is really annoying when using a profiler like Samply. This commit changes these thread names to a form like `opt cgu.0`, which is much better.
1 parent 487bdeb commit 666b1b6

File tree

2 files changed

+45
-21
lines changed

2 files changed

+45
-21
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+42-21
Original file line numberDiff line numberDiff line change
@@ -698,28 +698,49 @@ impl<B: WriteBackendMethods> WorkItem<B> {
698698

699699
/// Generate a short description of this work item suitable for use as a thread name.
700700
fn short_description(&self) -> String {
701-
// `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
702-
// Use very short descriptions in this case to maximize the space available for the module name.
703-
// Windows does not have that limitation so use slightly more descriptive names there.
701+
// `pthread_setname()` on *nix ignores anything beyond the first 15
702+
// bytes. Use short descriptions to maximize the space available for
703+
// the module name.
704+
#[cfg(not(windows))]
705+
fn desc(short: &str, _long: &str, name: &str) -> String {
706+
// The short label is three bytes, and is followed by a space. That
707+
// leaves 11 bytes for the CGU name. How we obtain those 11 bytes
708+
// depends on the the CGU name form.
709+
//
710+
// - Non-incremental, e.g. `regex.f10ba03eb5ec7975-cgu.0`: the part
711+
// before the `-cgu.0` is the same for every CGU, so use the
712+
// `cgu.0` part. The number suffix will be different for each
713+
// CGU.
714+
//
715+
// - Incremental (normal), e.g. `2i52vvl2hco29us0`: use the whole
716+
// name because each CGU will have a unique ASCII hash, and the
717+
// first 11 bytes will be enough to identify it.
718+
//
719+
// - Incremental (with `-Zhuman-readable-cgu-names`), e.g.
720+
// `regex.f10ba03eb5ec7975-re_builder.volatile`: use the whole
721+
// name. The first 11 bytes won't be enough to uniquely identify
722+
// it, but no obvious substring will, and this is a rarely used
723+
// option so it doesn't matter much.
724+
//
725+
assert_eq!(short.len(), 3);
726+
let name = if let Some(index) = name.find("-cgu.") {
727+
&name[index + 1..] // +1 skips the leading '-'.
728+
} else {
729+
name
730+
};
731+
format!("{short} {name}")
732+
}
733+
734+
// Windows has no thread name length limit, so use more descriptive names.
735+
#[cfg(windows)]
736+
fn desc(_short: &str, long: &str, name: &str) -> String {
737+
format!("{long} {name}")
738+
}
739+
704740
match self {
705-
WorkItem::Optimize(m) => {
706-
#[cfg(windows)]
707-
return format!("optimize module {}", m.name);
708-
#[cfg(not(windows))]
709-
return format!("opt {}", m.name);
710-
}
711-
WorkItem::CopyPostLtoArtifacts(m) => {
712-
#[cfg(windows)]
713-
return format!("copy LTO artifacts for {}", m.name);
714-
#[cfg(not(windows))]
715-
return format!("copy {}", m.name);
716-
}
717-
WorkItem::LTO(m) => {
718-
#[cfg(windows)]
719-
return format!("LTO module {}", m.name());
720-
#[cfg(not(windows))]
721-
return format!("LTO {}", m.name());
722-
}
741+
WorkItem::Optimize(m) => desc("opt", "optimize module {}", &m.name),
742+
WorkItem::CopyPostLtoArtifacts(m) => desc("cpy", "copy LTO artifacts for {}", &m.name),
743+
WorkItem::LTO(m) => desc("lto", "LTO module {}", m.name()),
723744
}
724745
}
725746
}

compiler/rustc_monomorphize/src/partitioning.rs

+3
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,9 @@ fn merge_codegen_units<'tcx>(
431431
codegen_units.sort_by_key(|cgu| cmp::Reverse(cgu.size_estimate()));
432432
let num_digits = codegen_units.len().ilog10() as usize + 1;
433433
for (index, cgu) in codegen_units.iter_mut().enumerate() {
434+
// Note: `WorkItem::short_description` depends on this name ending
435+
// with `-cgu.` followed by a numeric suffix. Please keep it in
436+
// sync with this code.
434437
let suffix = format!("{index:0num_digits$}");
435438
let numbered_codegen_unit_name =
436439
cgu_name_builder.build_cgu_name_no_mangle(LOCAL_CRATE, &["cgu"], Some(suffix));

0 commit comments

Comments
 (0)