< prev index next >

src/os/solaris/vm/os_solaris.cpp

Print this page
rev 10257 : 8149036: Add tracing for thread related events at os level
Reviewed-by: coleenp, mlarsson, dholmes


  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_solaris.h"

  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/filemap.hpp"
  37 #include "mutex_solaris.inline.hpp"
  38 #include "oops/oop.inline.hpp"
  39 #include "os_share_solaris.hpp"
  40 #include "os_solaris.inline.hpp"
  41 #include "prims/jniFastGetField.hpp"
  42 #include "prims/jvm.h"
  43 #include "prims/jvm_misc.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/atomic.inline.hpp"
  46 #include "runtime/extendedPC.hpp"
  47 #include "runtime/globals.hpp"
  48 #include "runtime/interfaceSupport.hpp"
  49 #include "runtime/java.hpp"
  50 #include "runtime/javaCalls.hpp"
  51 #include "runtime/mutexLocker.hpp"
  52 #include "runtime/objectMonitor.hpp"
  53 #include "runtime/orderAccess.inline.hpp"
  54 #include "runtime/osThread.hpp"
  55 #include "runtime/perfMemory.hpp"
  56 #include "runtime/sharedRuntime.hpp"
  57 #include "runtime/statSampler.hpp"
  58 #include "runtime/stubRoutines.hpp"
  59 #include "runtime/thread.inline.hpp"
  60 #include "runtime/threadCritical.hpp"
  61 #include "runtime/timer.hpp"
  62 #include "runtime/vm_version.hpp"
  63 #include "semaphore_posix.hpp"
  64 #include "services/attachListener.hpp"
  65 #include "services/memTracker.hpp"
  66 #include "services/runtimeService.hpp"
  67 #include "utilities/decoder.hpp"
  68 #include "utilities/defaultStream.hpp"
  69 #include "utilities/events.hpp"
  70 #include "utilities/growableArray.hpp"

  71 #include "utilities/vmError.hpp"
  72 
  73 // put OS-includes here
  74 # include <dlfcn.h>
  75 # include <errno.h>
  76 # include <exception>
  77 # include <link.h>
  78 # include <poll.h>
  79 # include <pthread.h>
  80 # include <pwd.h>
  81 # include <schedctl.h>
  82 # include <setjmp.h>
  83 # include <signal.h>
  84 # include <stdio.h>
  85 # include <alloca.h>
  86 # include <sys/filio.h>
  87 # include <sys/ipc.h>
  88 # include <sys/lwp.h>
  89 # include <sys/machelf.h>     // for elf Sym structure used by dladdr1
  90 # include <sys/mman.h>


 719 extern "C" void* java_start(void* thread_addr) {
 720   // Try to randomize the cache line index of hot stack frames.
 721   // This helps when threads of the same stack traces evict each other's
 722   // cache lines. The threads can be either from the same JVM instance, or
 723   // from different JVM instances. The benefit is especially true for
 724   // processors with hyperthreading technology.
 725   static int counter = 0;
 726   int pid = os::current_process_id();
 727   alloca(((pid ^ counter++) & 7) * 128);
 728 
 729   int prio;
 730   Thread* thread = (Thread*)thread_addr;
 731 
 732   thread->initialize_thread_current();
 733 
 734   OSThread* osthr = thread->osthread();
 735 
 736   osthr->set_lwp_id(_lwp_self());  // Store lwp in case we are bound
 737   thread->_schedctl = (void *) schedctl_init();
 738 



 739   if (UseNUMA) {
 740     int lgrp_id = os::numa_get_group_id();
 741     if (lgrp_id != -1) {
 742       thread->set_lgrp_id(lgrp_id);
 743     }
 744   }
 745 
 746   // If the creator called set priority before we started,
 747   // we need to call set_native_priority now that we have an lwp.
 748   // We used to get the priority from thr_getprio (we called
 749   // thr_setprio way back in create_thread) and pass it to
 750   // set_native_priority, but Solaris scales the priority
 751   // in java_to_os_priority, so when we read it back here,
 752   // we pass trash to set_native_priority instead of what's
 753   // in java_to_os_priority. So we save the native priority
 754   // in the osThread and recall it here.
 755 
 756   if (osthr->thread_id() != -1) {
 757     if (UseThreadPriorities) {
 758       int prio = osthr->native_priority();


 764       os::set_native_priority(thread, prio);
 765     }
 766   } else if (ThreadPriorityVerbose) {
 767     warning("Can't set priority in _start routine, thread id hasn't been set\n");
 768   }
 769 
 770   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 771 
 772   // initialize signal mask for this thread
 773   os::Solaris::hotspot_sigmask(thread);
 774 
 775   thread->run();
 776 
 777   // One less thread is executing
 778   // When the VMThread gets here, the main thread may have already exited
 779   // which frees the CodeHeap containing the Atomic::dec code
 780   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 781     Atomic::dec(&os::Solaris::_os_thread_count);
 782   }
 783 


 784   if (UseDetachedThreads) {
 785     thr_exit(NULL);
 786     ShouldNotReachHere();
 787   }
 788   return NULL;
 789 }
 790 
 791 static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
 792   // Allocate the OSThread object
 793   OSThread* osthread = new OSThread(NULL, NULL);
 794   if (osthread == NULL) return NULL;
 795 
 796   // Store info on the Solaris thread into the OSThread
 797   osthread->set_thread_id(thread_id);
 798   osthread->set_lwp_id(_lwp_self());
 799   thread->_schedctl = (void *) schedctl_init();
 800 
 801   if (UseNUMA) {
 802     int lgrp_id = os::numa_get_group_id();
 803     if (lgrp_id != -1) {


 836   }
 837 }
 838 
 839 bool os::create_attached_thread(JavaThread* thread) {
 840 #ifdef ASSERT
 841   thread->verify_not_published();
 842 #endif
 843   OSThread* osthread = create_os_thread(thread, thr_self());
 844   if (osthread == NULL) {
 845     return false;
 846   }
 847 
 848   // Initial thread state is RUNNABLE
 849   osthread->set_state(RUNNABLE);
 850   thread->set_osthread(osthread);
 851 
 852   // initialize signal mask for this thread
 853   // and save the caller's signal mask
 854   os::Solaris::hotspot_sigmask(thread);
 855 



 856   return true;
 857 }
 858 
 859 bool os::create_main_thread(JavaThread* thread) {
 860 #ifdef ASSERT
 861   thread->verify_not_published();
 862 #endif
 863   if (_starting_thread == NULL) {
 864     _starting_thread = create_os_thread(thread, main_thread);
 865     if (_starting_thread == NULL) {
 866       return false;
 867     }
 868   }
 869 
 870   // The primodial thread is runnable from the start
 871   _starting_thread->set_state(RUNNABLE);
 872 
 873   thread->set_osthread(_starting_thread);
 874 
 875   // initialize signal mask for this thread
 876   // and save the caller's signal mask
 877   os::Solaris::hotspot_sigmask(thread);
 878 
 879   return true;
 880 }
 881 



















 882 
 883 bool os::create_thread(Thread* thread, ThreadType thr_type,
 884                        size_t stack_size) {
 885   // Allocate the OSThread object
 886   OSThread* osthread = new OSThread(NULL, NULL);
 887   if (osthread == NULL) {
 888     return false;
 889   }
 890 
 891   if (ThreadPriorityVerbose) {
 892     char *thrtyp;
 893     switch (thr_type) {
 894     case vm_thread:
 895       thrtyp = (char *)"vm";
 896       break;
 897     case cgc_thread:
 898       thrtyp = (char *)"cgc";
 899       break;
 900     case pgc_thread:
 901       thrtyp = (char *)"pgc";


 957     } else {
 958       // Release the memory again
 959       os::release_memory(mem, VirtualMemoryBangSize);
 960     }
 961   }
 962 
 963   // Setup osthread because the child thread may need it.
 964   thread->set_osthread(osthread);
 965 
 966   // Create the Solaris thread
 967   thread_t tid = 0;
 968   long     flags = (UseDetachedThreads ? THR_DETACHED : 0) | THR_SUSPENDED;
 969   int      status;
 970 
 971   // Mark that we don't have an lwp or thread id yet.
 972   // In case we attempt to set the priority before the thread starts.
 973   osthread->set_lwp_id(-1);
 974   osthread->set_thread_id(-1);
 975 
 976   status = thr_create(NULL, stack_size, java_start, thread, flags, &tid);
 977   if (status != 0) {
 978     if (PrintMiscellaneous && (Verbose || WizardMode)) {
 979       perror("os::create_thread");





 980     }


 981     thread->set_osthread(NULL);
 982     // Need to clean up stuff we've allocated so far
 983     delete osthread;
 984     return false;
 985   }
 986 
 987   Atomic::inc(&os::Solaris::_os_thread_count);
 988 
 989   // Store info on the Solaris thread into the OSThread
 990   osthread->set_thread_id(tid);
 991 
 992   // Remember that we created this thread so we can set priority on it
 993   osthread->set_vm_created();
 994 
 995   // Initial thread state is INITIALIZED, not SUSPENDED
 996   osthread->set_state(INITIALIZED);
 997 
 998   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
 999   return true;
1000 }




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 // no precompiled headers
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "classfile/vmSymbols.hpp"
  29 #include "code/icBuffer.hpp"
  30 #include "code/vtableStubs.hpp"
  31 #include "compiler/compileBroker.hpp"
  32 #include "compiler/disassembler.hpp"
  33 #include "interpreter/interpreter.hpp"
  34 #include "jvm_solaris.h"
  35 #include "logging/log.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/filemap.hpp"
  38 #include "mutex_solaris.inline.hpp"
  39 #include "oops/oop.inline.hpp"
  40 #include "os_share_solaris.hpp"
  41 #include "os_solaris.inline.hpp"
  42 #include "prims/jniFastGetField.hpp"
  43 #include "prims/jvm.h"
  44 #include "prims/jvm_misc.hpp"
  45 #include "runtime/arguments.hpp"
  46 #include "runtime/atomic.inline.hpp"
  47 #include "runtime/extendedPC.hpp"
  48 #include "runtime/globals.hpp"
  49 #include "runtime/interfaceSupport.hpp"
  50 #include "runtime/java.hpp"
  51 #include "runtime/javaCalls.hpp"
  52 #include "runtime/mutexLocker.hpp"
  53 #include "runtime/objectMonitor.hpp"
  54 #include "runtime/orderAccess.inline.hpp"
  55 #include "runtime/osThread.hpp"
  56 #include "runtime/perfMemory.hpp"
  57 #include "runtime/sharedRuntime.hpp"
  58 #include "runtime/statSampler.hpp"
  59 #include "runtime/stubRoutines.hpp"
  60 #include "runtime/thread.inline.hpp"
  61 #include "runtime/threadCritical.hpp"
  62 #include "runtime/timer.hpp"
  63 #include "runtime/vm_version.hpp"
  64 #include "semaphore_posix.hpp"
  65 #include "services/attachListener.hpp"
  66 #include "services/memTracker.hpp"
  67 #include "services/runtimeService.hpp"
  68 #include "utilities/decoder.hpp"
  69 #include "utilities/defaultStream.hpp"
  70 #include "utilities/events.hpp"
  71 #include "utilities/growableArray.hpp"
  72 #include "utilities/macros.hpp"
  73 #include "utilities/vmError.hpp"
  74 
  75 // put OS-includes here
  76 # include <dlfcn.h>
  77 # include <errno.h>
  78 # include <exception>
  79 # include <link.h>
  80 # include <poll.h>
  81 # include <pthread.h>
  82 # include <pwd.h>
  83 # include <schedctl.h>
  84 # include <setjmp.h>
  85 # include <signal.h>
  86 # include <stdio.h>
  87 # include <alloca.h>
  88 # include <sys/filio.h>
  89 # include <sys/ipc.h>
  90 # include <sys/lwp.h>
  91 # include <sys/machelf.h>     // for elf Sym structure used by dladdr1
  92 # include <sys/mman.h>


 721 extern "C" void* java_start(void* thread_addr) {
 722   // Try to randomize the cache line index of hot stack frames.
 723   // This helps when threads of the same stack traces evict each other's
 724   // cache lines. The threads can be either from the same JVM instance, or
 725   // from different JVM instances. The benefit is especially true for
 726   // processors with hyperthreading technology.
 727   static int counter = 0;
 728   int pid = os::current_process_id();
 729   alloca(((pid ^ counter++) & 7) * 128);
 730 
 731   int prio;
 732   Thread* thread = (Thread*)thread_addr;
 733 
 734   thread->initialize_thread_current();
 735 
 736   OSThread* osthr = thread->osthread();
 737 
 738   osthr->set_lwp_id(_lwp_self());  // Store lwp in case we are bound
 739   thread->_schedctl = (void *) schedctl_init();
 740 
 741   log_info(os, thread)("Thread is alive (tid: " UINTX_FORMAT ").",
 742     os::current_thread_id());
 743 
 744   if (UseNUMA) {
 745     int lgrp_id = os::numa_get_group_id();
 746     if (lgrp_id != -1) {
 747       thread->set_lgrp_id(lgrp_id);
 748     }
 749   }
 750 
 751   // If the creator called set priority before we started,
 752   // we need to call set_native_priority now that we have an lwp.
 753   // We used to get the priority from thr_getprio (we called
 754   // thr_setprio way back in create_thread) and pass it to
 755   // set_native_priority, but Solaris scales the priority
 756   // in java_to_os_priority, so when we read it back here,
 757   // we pass trash to set_native_priority instead of what's
 758   // in java_to_os_priority. So we save the native priority
 759   // in the osThread and recall it here.
 760 
 761   if (osthr->thread_id() != -1) {
 762     if (UseThreadPriorities) {
 763       int prio = osthr->native_priority();


 769       os::set_native_priority(thread, prio);
 770     }
 771   } else if (ThreadPriorityVerbose) {
 772     warning("Can't set priority in _start routine, thread id hasn't been set\n");
 773   }
 774 
 775   assert(osthr->get_state() == RUNNABLE, "invalid os thread state");
 776 
 777   // initialize signal mask for this thread
 778   os::Solaris::hotspot_sigmask(thread);
 779 
 780   thread->run();
 781 
 782   // One less thread is executing
 783   // When the VMThread gets here, the main thread may have already exited
 784   // which frees the CodeHeap containing the Atomic::dec code
 785   if (thread != VMThread::vm_thread() && VMThread::vm_thread() != NULL) {
 786     Atomic::dec(&os::Solaris::_os_thread_count);
 787   }
 788 
 789   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ").", os::current_thread_id());
 790 
 791   if (UseDetachedThreads) {
 792     thr_exit(NULL);
 793     ShouldNotReachHere();
 794   }
 795   return NULL;
 796 }
 797 
 798 static OSThread* create_os_thread(Thread* thread, thread_t thread_id) {
 799   // Allocate the OSThread object
 800   OSThread* osthread = new OSThread(NULL, NULL);
 801   if (osthread == NULL) return NULL;
 802 
 803   // Store info on the Solaris thread into the OSThread
 804   osthread->set_thread_id(thread_id);
 805   osthread->set_lwp_id(_lwp_self());
 806   thread->_schedctl = (void *) schedctl_init();
 807 
 808   if (UseNUMA) {
 809     int lgrp_id = os::numa_get_group_id();
 810     if (lgrp_id != -1) {


 843   }
 844 }
 845 
 846 bool os::create_attached_thread(JavaThread* thread) {
 847 #ifdef ASSERT
 848   thread->verify_not_published();
 849 #endif
 850   OSThread* osthread = create_os_thread(thread, thr_self());
 851   if (osthread == NULL) {
 852     return false;
 853   }
 854 
 855   // Initial thread state is RUNNABLE
 856   osthread->set_state(RUNNABLE);
 857   thread->set_osthread(osthread);
 858 
 859   // initialize signal mask for this thread
 860   // and save the caller's signal mask
 861   os::Solaris::hotspot_sigmask(thread);
 862 
 863   log_info(os, thread)("Thread attached (tid: " UINTX_FORMAT ").",
 864     os::current_thread_id());
 865 
 866   return true;
 867 }
 868 
 869 bool os::create_main_thread(JavaThread* thread) {
 870 #ifdef ASSERT
 871   thread->verify_not_published();
 872 #endif
 873   if (_starting_thread == NULL) {
 874     _starting_thread = create_os_thread(thread, main_thread);
 875     if (_starting_thread == NULL) {
 876       return false;
 877     }
 878   }
 879 
 880   // The primodial thread is runnable from the start
 881   _starting_thread->set_state(RUNNABLE);
 882 
 883   thread->set_osthread(_starting_thread);
 884 
 885   // initialize signal mask for this thread
 886   // and save the caller's signal mask
 887   os::Solaris::hotspot_sigmask(thread);
 888 
 889   return true;
 890 }
 891 
 892 // Helper function to trace thread attributes, similar to os::Posix::describe_pthread_attr()
 893 static char* describe_thr_create_attributes(char* buf, size_t buflen,
 894   size_t stacksize, long flags)
 895 {
 896   stringStream ss(buf, buflen);
 897   ss.print("stacksize: " SIZE_FORMAT "k, ", stacksize / 1024);
 898   ss.print("flags: ");
 899   #define PRINT_FLAG(f) if (flags & f) ss.print( XSTR(f) " ");
 900   #define ALL(X) \
 901     X(THR_SUSPENDED) \
 902     X(THR_DETACHED) \
 903     X(THR_BOUND) \
 904     X(THR_NEW_LWP) \
 905     X(THR_DAEMON)
 906   ALL(PRINT_FLAG)
 907   #undef ALL
 908   #undef PRINT_FLAG
 909   return buf;
 910 }
 911 
 912 bool os::create_thread(Thread* thread, ThreadType thr_type,
 913                        size_t stack_size) {
 914   // Allocate the OSThread object
 915   OSThread* osthread = new OSThread(NULL, NULL);
 916   if (osthread == NULL) {
 917     return false;
 918   }
 919 
 920   if (ThreadPriorityVerbose) {
 921     char *thrtyp;
 922     switch (thr_type) {
 923     case vm_thread:
 924       thrtyp = (char *)"vm";
 925       break;
 926     case cgc_thread:
 927       thrtyp = (char *)"cgc";
 928       break;
 929     case pgc_thread:
 930       thrtyp = (char *)"pgc";


 986     } else {
 987       // Release the memory again
 988       os::release_memory(mem, VirtualMemoryBangSize);
 989     }
 990   }
 991 
 992   // Setup osthread because the child thread may need it.
 993   thread->set_osthread(osthread);
 994 
 995   // Create the Solaris thread
 996   thread_t tid = 0;
 997   long     flags = (UseDetachedThreads ? THR_DETACHED : 0) | THR_SUSPENDED;
 998   int      status;
 999 
1000   // Mark that we don't have an lwp or thread id yet.
1001   // In case we attempt to set the priority before the thread starts.
1002   osthread->set_lwp_id(-1);
1003   osthread->set_thread_id(-1);
1004 
1005   status = thr_create(NULL, stack_size, java_start, thread, flags, &tid);
1006 
1007   char buf[64];
1008   if (status == 0) {
1009     log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
1010       (uintx) tid, describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
1011   } else {
1012     log_warning(os, thread)("Failed to start thread - thr_create failed (%s) for attributes: %s.",
1013       strerror(status), describe_thr_create_attributes(buf, sizeof(buf), stack_size, flags));
1014   }
1015 
1016   if (status != 0) {
1017     thread->set_osthread(NULL);
1018     // Need to clean up stuff we've allocated so far
1019     delete osthread;
1020     return false;
1021   }
1022 
1023   Atomic::inc(&os::Solaris::_os_thread_count);
1024 
1025   // Store info on the Solaris thread into the OSThread
1026   osthread->set_thread_id(tid);
1027 
1028   // Remember that we created this thread so we can set priority on it
1029   osthread->set_vm_created();
1030 
1031   // Initial thread state is INITIALIZED, not SUSPENDED
1032   osthread->set_state(INITIALIZED);
1033 
1034   // The thread is returned suspended (in state INITIALIZED), and is started higher up in the call chain
1035   return true;
1036 }


< prev index next >