Skip to content

Commit 0d7f5a0

Browse files
Rollup merge of rust-lang#82535 - wesleywiser:wip_codegen_thread_names, r=nagisa
Set codegen thread names Set names on threads spawned during codegen. Various debugging and profiling tools can take advantage of this to show a more useful identifier for threads. For example, gdb will show thread names in `info threads`: ``` (gdb) info threads Id Target Id Frame 1 Thread 0x7fffefa7ec40 (LWP 2905) "rustc" __pthread_clockjoin_ex (threadid=140737214134016, thread_return=0x0, clockid=<optimized out>, abstime=<optimized out>, block=<optimized out>) at pthread_join_common.c:145 2 Thread 0x7fffefa7b700 (LWP 2957) "rustc" 0x00007ffff125eaa8 in llvm::X86_MC::initLLVMToSEHAndCVRegMapping(llvm::MCRegisterInfo*) () from /home/wesley/.rustup/toolchains/stage1/lib/librustc_driver-f866439e29074957.so 3 Thread 0x7fffeef0f700 (LWP 3116) "rustc" futex_wait_cancelable (private=0, expected=0, futex_word=0x7fffe8602ac8) at ../sysdeps/nptl/futex-internal.h:183 * 4 Thread 0x7fffeed0e700 (LWP 3123) "rustc" rustc_codegen_ssa::back::write::spawn_work (cgcx=..., work=...) at /home/wesley/code/rust/rust/compiler/rustc_codegen_ssa/src/back/write.rs:1573 6 Thread 0x7fffe113b700 (LWP 3150) "opt foof.7rcbfp" 0x00007ffff2940e62 in llvm::CallGraph::populateCallGraphNode(llvm::CallGraphNode*) () from /home/wesley/.rustup/toolchains/stage1/lib/librustc_driver-f866439e29074957.so 8 Thread 0x7fffe0d39700 (LWP 3158) "opt foof.7rcbfp" 0x00007fffefe8998e in malloc_consolidate (av=av@entry=0x7ffe2c000020) at malloc.c:4492 9 Thread 0x7fffe0f3a700 (LWP 3162) "opt foof.7rcbfp" 0x00007fffefef27c4 in __libc_open64 (file=0x7fffe0f38608 "foof.foof.7rcbfp3g-cgu.6.rcgu.o", oflag=524865) at ../sysdeps/unix/sysv/linux/open64.c:48 (gdb) ``` and Windows Performance Analyzer will also show this information when profiling: ![image](https://user-images.githubusercontent.com/831192/109231017-d311f780-7793-11eb-8072-ab836a830e90.png)
2 parents c21341b + 9d3739d commit 0d7f5a0

File tree

1 file changed

+77
-47
lines changed
  • compiler/rustc_codegen_ssa/src/back

1 file changed

+77
-47
lines changed

compiler/rustc_codegen_ssa/src/back/write.rs

+77-47
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,33 @@ impl<B: WriteBackendMethods> WorkItem<B> {
712712
}
713713
}
714714
}
715+
716+
/// Generate a short description of this work item suitable for use as a thread name.
717+
fn short_description(&self) -> String {
718+
// `pthread_setname()` on *nix is limited to 15 characters and longer names are ignored.
719+
// Use very short descriptions in this case to maximize the space available for the module name.
720+
// Windows does not have that limitation so use slightly more descriptive names there.
721+
match self {
722+
WorkItem::Optimize(m) => {
723+
#[cfg(windows)]
724+
return format!("optimize module {}", m.name);
725+
#[cfg(not(windows))]
726+
return format!("opt {}", m.name);
727+
}
728+
WorkItem::CopyPostLtoArtifacts(m) => {
729+
#[cfg(windows)]
730+
return format!("copy LTO artifacts for {}", m.name);
731+
#[cfg(not(windows))]
732+
return format!("copy {}", m.name);
733+
}
734+
WorkItem::LTO(m) => {
735+
#[cfg(windows)]
736+
return format!("LTO module {}", m.name());
737+
#[cfg(not(windows))]
738+
return format!("LTO {}", m.name());
739+
}
740+
}
741+
}
715742
}
716743

717744
enum WorkItemResult<B: WriteBackendMethods> {
@@ -1609,56 +1636,59 @@ fn start_executing_work<B: ExtraBackendMethods>(
16091636
pub struct WorkerFatalError;
16101637

16111638
fn spawn_work<B: ExtraBackendMethods>(cgcx: CodegenContext<B>, work: WorkItem<B>) {
1612-
thread::spawn(move || {
1613-
// Set up a destructor which will fire off a message that we're done as
1614-
// we exit.
1615-
struct Bomb<B: ExtraBackendMethods> {
1616-
coordinator_send: Sender<Box<dyn Any + Send>>,
1617-
result: Option<Result<WorkItemResult<B>, FatalError>>,
1618-
worker_id: usize,
1619-
}
1620-
impl<B: ExtraBackendMethods> Drop for Bomb<B> {
1621-
fn drop(&mut self) {
1622-
let worker_id = self.worker_id;
1623-
let msg = match self.result.take() {
1624-
Some(Ok(WorkItemResult::Compiled(m))) => {
1625-
Message::Done::<B> { result: Ok(m), worker_id }
1626-
}
1627-
Some(Ok(WorkItemResult::NeedsLink(m))) => {
1628-
Message::NeedsLink::<B> { module: m, worker_id }
1629-
}
1630-
Some(Ok(WorkItemResult::NeedsFatLTO(m))) => {
1631-
Message::NeedsFatLTO::<B> { result: m, worker_id }
1632-
}
1633-
Some(Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer))) => {
1634-
Message::NeedsThinLTO::<B> { name, thin_buffer, worker_id }
1635-
}
1636-
Some(Err(FatalError)) => {
1637-
Message::Done::<B> { result: Err(Some(WorkerFatalError)), worker_id }
1638-
}
1639-
None => Message::Done::<B> { result: Err(None), worker_id },
1640-
};
1641-
drop(self.coordinator_send.send(Box::new(msg)));
1639+
let builder = thread::Builder::new().name(work.short_description());
1640+
builder
1641+
.spawn(move || {
1642+
// Set up a destructor which will fire off a message that we're done as
1643+
// we exit.
1644+
struct Bomb<B: ExtraBackendMethods> {
1645+
coordinator_send: Sender<Box<dyn Any + Send>>,
1646+
result: Option<Result<WorkItemResult<B>, FatalError>>,
1647+
worker_id: usize,
1648+
}
1649+
impl<B: ExtraBackendMethods> Drop for Bomb<B> {
1650+
fn drop(&mut self) {
1651+
let worker_id = self.worker_id;
1652+
let msg = match self.result.take() {
1653+
Some(Ok(WorkItemResult::Compiled(m))) => {
1654+
Message::Done::<B> { result: Ok(m), worker_id }
1655+
}
1656+
Some(Ok(WorkItemResult::NeedsLink(m))) => {
1657+
Message::NeedsLink::<B> { module: m, worker_id }
1658+
}
1659+
Some(Ok(WorkItemResult::NeedsFatLTO(m))) => {
1660+
Message::NeedsFatLTO::<B> { result: m, worker_id }
1661+
}
1662+
Some(Ok(WorkItemResult::NeedsThinLTO(name, thin_buffer))) => {
1663+
Message::NeedsThinLTO::<B> { name, thin_buffer, worker_id }
1664+
}
1665+
Some(Err(FatalError)) => {
1666+
Message::Done::<B> { result: Err(Some(WorkerFatalError)), worker_id }
1667+
}
1668+
None => Message::Done::<B> { result: Err(None), worker_id },
1669+
};
1670+
drop(self.coordinator_send.send(Box::new(msg)));
1671+
}
16421672
}
1643-
}
16441673

1645-
let mut bomb = Bomb::<B> {
1646-
coordinator_send: cgcx.coordinator_send.clone(),
1647-
result: None,
1648-
worker_id: cgcx.worker,
1649-
};
1674+
let mut bomb = Bomb::<B> {
1675+
coordinator_send: cgcx.coordinator_send.clone(),
1676+
result: None,
1677+
worker_id: cgcx.worker,
1678+
};
16501679

1651-
// Execute the work itself, and if it finishes successfully then flag
1652-
// ourselves as a success as well.
1653-
//
1654-
// Note that we ignore any `FatalError` coming out of `execute_work_item`,
1655-
// as a diagnostic was already sent off to the main thread - just
1656-
// surface that there was an error in this worker.
1657-
bomb.result = {
1658-
let _prof_timer = work.start_profiling(&cgcx);
1659-
Some(execute_work_item(&cgcx, work))
1660-
};
1661-
});
1680+
// Execute the work itself, and if it finishes successfully then flag
1681+
// ourselves as a success as well.
1682+
//
1683+
// Note that we ignore any `FatalError` coming out of `execute_work_item`,
1684+
// as a diagnostic was already sent off to the main thread - just
1685+
// surface that there was an error in this worker.
1686+
bomb.result = {
1687+
let _prof_timer = work.start_profiling(&cgcx);
1688+
Some(execute_work_item(&cgcx, work))
1689+
};
1690+
})
1691+
.expect("failed to spawn thread");
16621692
}
16631693

16641694
enum SharedEmitterMessage {

0 commit comments

Comments
 (0)