aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsla <unknown>2014-10-13 22:11:39 +0200
committerbell-sw <liberica@bell-sw.com>2019-07-22 19:21:54 +0300
commitf4aee60664626b7c6d8c740d1bb0ba5041407ede (patch)
treeafe926743cc52e1c479822d44f178ba7dcc00e27
parent4b5eeb501d7f1308b5f21dd8c5707b377f3b99e6 (diff)
downloadjdk8u_hotspot-f4aee60664626b7c6d8c740d1bb0ba5041407ede.tar.gz
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
-rw-r--r--src/os/linux/vm/os_linux.cpp16
-rw-r--r--src/os/linux/vm/os_linux.hpp1
-rw-r--r--src/os/windows/vm/os_windows.cpp25
-rw-r--r--src/share/vm/runtime/thread.cpp1
-rw-r--r--src/share/vm/runtime/vmThread.cpp1
5 files changed, 40 insertions, 4 deletions
diff --git a/src/os/linux/vm/os_linux.cpp b/src/os/linux/vm/os_linux.cpp
index a7b12483c..a499e499e 100644
--- a/src/os/linux/vm/os_linux.cpp
+++ b/src/os/linux/vm/os_linux.cpp
@@ -136,6 +136,7 @@ uintptr_t os::Linux::_initial_thread_stack_size = 0;
int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;
int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
+int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL;
Mutex* os::Linux::_createThread_lock = NULL;
pthread_t os::Linux::_main_thread;
int os::Linux::_page_size = -1;
@@ -5054,6 +5055,11 @@ void os::init(void) {
StackRedPages = 1;
StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size();
}
+
+ // retrieve entry point for pthread_setname_np
+ Linux::_pthread_setname_np =
+ (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
+
}
// To install functions for atexit system call
@@ -5308,8 +5314,14 @@ int os::active_processor_count() {
}
void os::set_native_thread_name(const char *name) {
- // Not yet implemented.
- return;
+ if (Linux::_pthread_setname_np) {
+ char buf [16]; // according to glibc manpage, 16 chars incl. '/0'
+ snprintf(buf, sizeof(buf), "%s", name);
+ buf[sizeof(buf) - 1] = '\0';
+ const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
+ // ERANGE should not happen; all other errors should just be ignored.
+ assert(rc != ERANGE, "pthread_setname_np failed");
+ }
}
bool os::distribute_processes(uint length, uint* distribution) {
diff --git a/src/os/linux/vm/os_linux.hpp b/src/os/linux/vm/os_linux.hpp
index e207dccd7..79a9f39ab 100644
--- a/src/os/linux/vm/os_linux.hpp
+++ b/src/os/linux/vm/os_linux.hpp
@@ -56,6 +56,7 @@ class Linux {
static int (*_clock_gettime)(clockid_t, struct timespec *);
static int (*_pthread_getcpuclockid)(pthread_t, clockid_t *);
+ static int (*_pthread_setname_np)(pthread_t, const char*);
static address _initial_thread_stack_bottom;
static uintptr_t _initial_thread_stack_size;
diff --git a/src/os/windows/vm/os_windows.cpp b/src/os/windows/vm/os_windows.cpp
index 358692f9e..2aca06d56 100644
--- a/src/os/windows/vm/os_windows.cpp
+++ b/src/os/windows/vm/os_windows.cpp
@@ -744,8 +744,29 @@ int os::active_processor_count() {
}
void os::set_native_thread_name(const char *name) {
- // Not yet implemented.
- return;
+
+ // See: http://msdn.microsoft.com/en-us/library/xcb2z8hs.aspx
+ //
+ // Note that unfortunately this only works if the process
+ // is already attached to a debugger; debugger must observe
+ // the exception below to show the correct name.
+
+ const DWORD MS_VC_EXCEPTION = 0x406D1388;
+ struct {
+ DWORD dwType; // must be 0x1000
+ LPCSTR szName; // pointer to name (in user addr space)
+ DWORD dwThreadID; // thread ID (-1=caller thread)
+ DWORD dwFlags; // reserved for future use, must be zero
+ } info;
+
+ info.dwType = 0x1000;
+ info.szName = name;
+ info.dwThreadID = -1;
+ info.dwFlags = 0;
+
+ __try {
+ RaiseException (MS_VC_EXCEPTION, 0, sizeof(info)/sizeof(DWORD), (const ULONG_PTR*)&info );
+ } __except(EXCEPTION_CONTINUE_EXECUTION) {}
}
bool os::distribute_processes(uint length, uint* distribution) {
diff --git a/src/share/vm/runtime/thread.cpp b/src/share/vm/runtime/thread.cpp
index 48bba789f..5d56cfea3 100644
--- a/src/share/vm/runtime/thread.cpp
+++ b/src/share/vm/runtime/thread.cpp
@@ -1310,6 +1310,7 @@ void WatcherThread::run() {
this->record_stack_base_and_size();
this->initialize_thread_local_storage();
+ this->set_native_thread_name(this->name());
this->set_active_handles(JNIHandleBlock::allocate_block());
while(!_should_terminate) {
assert(watcher_thread() == Thread::current(), "thread consistency check");
diff --git a/src/share/vm/runtime/vmThread.cpp b/src/share/vm/runtime/vmThread.cpp
index 0ee5982a9..d08c5db81 100644
--- a/src/share/vm/runtime/vmThread.cpp
+++ b/src/share/vm/runtime/vmThread.cpp
@@ -252,6 +252,7 @@ void VMThread::run() {
assert(this == vm_thread(), "check");
this->initialize_thread_local_storage();
+ this->set_native_thread_name(this->name());
this->record_stack_base_and_size();
// Notify_lock wait checks on active_handles() to rewait in
// case of spurious wakeup, it should wait on the last