-
Notifications
You must be signed in to change notification settings - Fork 13k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Thread native name setting #21678
Thread native name setting #21678
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
pthread_set_name_np(pthread_self(), cname.as_ptr()); | ||
} | ||
|
||
#[cfg(target_os = "macos")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think iOS is (somewhat) supported too; does it use pthread_setname_np
too?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@huonw It would make sense since the two OSes share the same posix base, but I can't find a direct confirmation... There is a target_os
for iOS?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found it: over here it says pthread_setname_np()
is available since OS X 10.6 and iOS 3.2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vojtechkral @huonw yep, iOS is similar to OS X here, please change it to #[cfg(any(target_os = "macos", target_os = "ios"))]
// because pthread_setname_np() wasn't added until glibc 2.12 | ||
// PR_SET_NAME since Linux 2.6.9 | ||
let cname = CString::from_slice(name.as_bytes()); | ||
prctl(15i32 /* = PR_SET_NAME */, cname.as_ptr() as u64, 0u64, 0u64, 0u64); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this use the linkage = "extern_weak"
trick instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alexcrichton Sure, which function though? prctl()
or pthread_setname_np()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For pthread_setname_np
, but largely only because it looks like it's only defined on newer glibc implementations. This is a bit of a nicety, however, so we probably won't lose too much if it's not available.
@alexcrichton Updated as requested. I'll try to find someone to test this on BSD / OS X ... |
@vhbit I just noticed that too :) Note that the "ios" target should probably accompany most of the other |
Have no BSD but will test it on OS X |
@vojtechkral usually it is although I'm not fast enough to track all the changes :-) |
Works fine on OS X |
@vhbit thanks! |
@bors try 7e67 |
Fixes #10302 I really am not sure I'm doing this right, so here goes nothing... Also testing this isn't easy. I don't have any other *nix boxes besides a Linux one. Test code: ```rust use std::thread; use std::io::timer::sleep; use std::time::duration::Duration; fn make_thread<'a>(i: i64) -> thread::JoinGuard<'a, ()> { thread::Builder::new().name(format!("MyThread{}", i).to_string()).scoped(move || { println!("Start: {}", i); sleep(Duration::seconds(i)); println!("End: {}", i); }) } fn main() { let mut guards = vec![make_thread(3)]; for i in 4i64..16 { guards.push(make_thread(i)); } } ``` GDB output on my machine: ``` (gdb) info threads Id Target Id Frame 15 Thread 0x7fdfbb35f700 (LWP 23575) "MyThread3" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 14 Thread 0x7fdfba7ff700 (LWP 23576) "MyThread4" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 13 Thread 0x7fdfba5fe700 (LWP 23577) "MyThread5" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 12 Thread 0x7fdfba3fd700 (LWP 23578) "MyThread6" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 11 Thread 0x7fdfb8dfe700 (LWP 23580) "MyThread4" 0x00007fdfbb746193 in select () from /usr/lib/libc.so.6 10 Thread 0x7fdfb8fff700 (LWP 23579) "MyThread7" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 9 Thread 0x7fdfb8bfd700 (LWP 23581) "MyThread8" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 8 Thread 0x7fdfb3fff700 (LWP 23582) "MyThread9" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 7 Thread 0x7fdfb3dfe700 (LWP 23583) "MyThread10" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 6 Thread 0x7fdfb3bfd700 (LWP 23584) "MyThread11" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 5 Thread 0x7fdfb2bff700 (LWP 23585) "MyThread12" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 4 Thread 0x7fdfb29fe700 (LWP 23586) "MyThread13" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 3 Thread 0x7fdfb27fd700 (LWP 23587) "MyThread14" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 2 Thread 0x7fdfb1bff700 (LWP 23588) "MyThread15" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 * 1 Thread 0x7fdfbc411800 (LWP 23574) "threads" 0x00007fdfbbe2e505 in pthread_join () from /usr/lib/libpthread.so.0 ``` (I'm not sure why one of the threads is duplicated, but it does that without my patch too...)
⌛ Testing commit 7e67eba with merge 90ed244... |
💔 Test failed - auto-linux-64-x-android-t |
Seems to break 64-bit android |
There was a missing "android" target on the extern, which is weird... I don't understand how it compiled on Android before... edit: It's because the other android cfg test are basically redundant since |
@bors try 9ee9 |
⌛ Testing commit 9ee972c with merge f01bc73... |
⌛ Testing commit 9ee972c with merge badca40... |
💔 Test failed - auto-linux-64-x-android-t |
@bors: retry |
Fixes rust-lang#10302 I really am not sure I'm doing this right, so here goes nothing... Also testing this isn't easy. I don't have any other *nix boxes besides a Linux one. Test code: ```rust use std::thread; use std::io::timer::sleep; use std::time::duration::Duration; fn make_thread<'a>(i: i64) -> thread::JoinGuard<'a, ()> { thread::Builder::new().name(format!("MyThread{}", i).to_string()).scoped(move || { println!("Start: {}", i); sleep(Duration::seconds(i)); println!("End: {}", i); }) } fn main() { let mut guards = vec![make_thread(3)]; for i in 4i64..16 { guards.push(make_thread(i)); } } ``` GDB output on my machine: ``` (gdb) info threads Id Target Id Frame 15 Thread 0x7fdfbb35f700 (LWP 23575) "MyThread3" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 14 Thread 0x7fdfba7ff700 (LWP 23576) "MyThread4" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 13 Thread 0x7fdfba5fe700 (LWP 23577) "MyThread5" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 12 Thread 0x7fdfba3fd700 (LWP 23578) "MyThread6" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 11 Thread 0x7fdfb8dfe700 (LWP 23580) "MyThread4" 0x00007fdfbb746193 in select () from /usr/lib/libc.so.6 10 Thread 0x7fdfb8fff700 (LWP 23579) "MyThread7" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 9 Thread 0x7fdfb8bfd700 (LWP 23581) "MyThread8" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 8 Thread 0x7fdfb3fff700 (LWP 23582) "MyThread9" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 7 Thread 0x7fdfb3dfe700 (LWP 23583) "MyThread10" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 6 Thread 0x7fdfb3bfd700 (LWP 23584) "MyThread11" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 5 Thread 0x7fdfb2bff700 (LWP 23585) "MyThread12" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 4 Thread 0x7fdfb29fe700 (LWP 23586) "MyThread13" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 3 Thread 0x7fdfb27fd700 (LWP 23587) "MyThread14" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 2 Thread 0x7fdfb1bff700 (LWP 23588) "MyThread15" 0x00007fdfbbe35a8d in nanosleep () from /usr/lib/libpthread.so.0 * 1 Thread 0x7fdfbc411800 (LWP 23574) "threads" 0x00007fdfbbe2e505 in pthread_join () from /usr/lib/libpthread.so.0 ``` (I'm not sure why one of the threads is duplicated, but it does that without my patch too...)
- incoporate changes introduced by rust-lang#21678
Fixes #10302
I really am not sure I'm doing this right, so here goes nothing...
Also testing this isn't easy. I don't have any other *nix boxes besides a Linux one.
Test code:
GDB output on my machine:
(I'm not sure why one of the threads is duplicated, but it does that without my patch too...)