Mon, 13 Oct 2014 22:11:39 +0200
7102541: RFE: os::set_native_thread_name() cleanups
Summary: implement os::set_native_thread_name() on windows, linux
Reviewed-by: sla, ctornqvi, simonis
Contributed-by: thomas.stuefe@sap.com
1.1 --- a/src/os/linux/vm/os_linux.cpp Fri May 17 07:44:31 2019 +0100 1.2 +++ b/src/os/linux/vm/os_linux.cpp Mon Oct 13 22:11:39 2014 +0200 1.3 @@ -136,6 +136,7 @@ 1.4 1.5 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL; 1.6 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL; 1.7 +int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL; 1.8 Mutex* os::Linux::_createThread_lock = NULL; 1.9 pthread_t os::Linux::_main_thread; 1.10 int os::Linux::_page_size = -1; 1.11 @@ -5054,6 +5055,11 @@ 1.12 StackRedPages = 1; 1.13 StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size(); 1.14 } 1.15 + 1.16 + // retrieve entry point for pthread_setname_np 1.17 + Linux::_pthread_setname_np = 1.18 + (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np"); 1.19 + 1.20 } 1.21 1.22 // To install functions for atexit system call 1.23 @@ -5308,8 +5314,14 @@ 1.24 } 1.25 1.26 void os::set_native_thread_name(const char *name) { 1.27 - // Not yet implemented. 1.28 - return; 1.29 + if (Linux::_pthread_setname_np) { 1.30 + char buf [16]; // according to glibc manpage, 16 chars incl. '/0' 1.31 + snprintf(buf, sizeof(buf), "%s", name); 1.32 + buf[sizeof(buf) - 1] = '\0'; 1.33 + const int rc = Linux::_pthread_setname_np(pthread_self(), buf); 1.34 + // ERANGE should not happen; all other errors should just be ignored. 1.35 + assert(rc != ERANGE, "pthread_setname_np failed"); 1.36 + } 1.37 } 1.38 1.39 bool os::distribute_processes(uint length, uint* distribution) {
2.1 --- a/src/os/linux/vm/os_linux.hpp Fri May 17 07:44:31 2019 +0100 2.2 +++ b/src/os/linux/vm/os_linux.hpp Mon Oct 13 22:11:39 2014 +0200 2.3 @@ -56,6 +56,7 @@ 2.4 2.5 static int (*_clock_gettime)(clockid_t, struct timespec *); 2.6 static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *); 2.7 + static int (*_pthread_setname_np)(pthread_t, const char*); 2.8 2.9 static address _initial_thread_stack_bottom; 2.10 static uintptr_t _initial_thread_stack_size;
3.1 --- a/src/os/windows/vm/os_windows.cpp Fri May 17 07:44:31 2019 +0100 3.2 +++ b/src/os/windows/vm/os_windows.cpp Mon Oct 13 22:11:39 2014 +0200 3.3 @@ -744,8 +744,29 @@ 3.4 } 3.5 3.6 void os::set_native_thread_name(const char *name) { 3.7 - // Not yet implemented. 3.8 - return; 3.9 + 3.10 + // See: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx 3.11 + // 3.12 + // Note that unfortunately this only works if the process 3.13 + // is already attached to a debugger; debugger must observe 3.14 + // the exception below to show the correct name. 3.15 + 3.16 + const DWORD MS_VC_EXCEPTION = 0x406D1388; 3.17 + struct { 3.18 + DWORD dwType; // must be 0x1000 3.19 + LPCSTR szName; // pointer to name (in user addr space) 3.20 + DWORD dwThreadID; // thread ID (-1=caller thread) 3.21 + DWORD dwFlags; // reserved for future use, must be zero 3.22 + } info; 3.23 + 3.24 + info.dwType = 0x1000; 3.25 + info.szName = name; 3.26 + info.dwThreadID = -1; 3.27 + info.dwFlags = 0; 3.28 + 3.29 + __try { 3.30 + RaiseException (MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info ); 3.31 + } __except(EXCEPTION_CONTINUE_EXECUTION) {} 3.32 } 3.33 3.34 bool os::distribute_processes(uint length, uint* distribution) {
4.1 --- a/src/share/vm/runtime/thread.cpp Fri May 17 07:44:31 2019 +0100 4.2 +++ b/src/share/vm/runtime/thread.cpp Mon Oct 13 22:11:39 2014 +0200 4.3 @@ -1310,6 +1310,7 @@ 4.4 4.5 this->record_stack_base_and_size(); 4.6 this->initialize_thread_local_storage(); 4.7 + this->set_native_thread_name(this->name()); 4.8 this->set_active_handles(JNIHandleBlock::allocate_block()); 4.9 while(!_should_terminate) { 4.10 assert(watcher_thread() == Thread::current(), "thread consistency check");
5.1 --- a/src/share/vm/runtime/vmThread.cpp Fri May 17 07:44:31 2019 +0100 5.2 +++ b/src/share/vm/runtime/vmThread.cpp Mon Oct 13 22:11:39 2014 +0200 5.3 @@ -252,6 +252,7 @@ 5.4 assert(this == vm_thread(), "check"); 5.5 5.6 this->initialize_thread_local_storage(); 5.7 + this->set_native_thread_name(this->name()); 5.8 this->record_stack_base_and_size(); 5.9 // Notify_lock wait checks on active_handles() to rewait in 5.10 // case of spurious wakeup, it should wait on the last