< prev index next >

src/hotspot/os/linux/os_linux.cpp

Print this page




 783     while (osthread->get_state() == INITIALIZED) {
 784       sync->wait_without_safepoint_check();
 785     }
 786   }
 787 
 788   assert(osthread->pthread_id() != 0, "pthread_id was not set as expected");
 789 
 790   // call one more level start routine
 791   thread->call_run();
 792 
 793   // Note: at this point the thread object may already have deleted itself.
 794   // Prevent dereferencing it from here on out.
 795   thread = NULL;
 796 
 797   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
 798     os::current_thread_id(), (uintx) pthread_self());
 799 
 800   return 0;
 801 }
 802 
























































 803 bool os::create_thread(Thread* thread, ThreadType thr_type,
 804                        size_t req_stack_size) {
 805   assert(thread->osthread() == NULL, "caller responsible");
 806 
 807   // Allocate the OSThread object
 808   OSThread* osthread = new OSThread(NULL, NULL);
 809   if (osthread == NULL) {
 810     return false;
 811   }
 812 
 813   // set the correct thread state
 814   osthread->set_thread_type(thr_type);
 815 
 816   // Initial state is ALLOCATED but not INITIALIZED
 817   osthread->set_state(ALLOCATED);
 818 
 819   thread->set_osthread(osthread);
 820 
 821   // init thread attributes
 822   pthread_attr_t attr;
 823   pthread_attr_init(&attr);
 824   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 825 
 826   // Calculate stack size if it's not specified by caller.
 827   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
 828   // In the Linux NPTL pthread implementation the guard size mechanism
 829   // is not implemented properly. The posix standard requires adding
 830   // the size of the guard pages to the stack size, instead Linux
 831   // takes the space out of 'stacksize'. Thus we adapt the requested
 832   // stack_size by the size of the guard pages to mimick proper
 833   // behaviour. However, be careful not to end up with a size
 834   // of zero due to overflow. Don't add the guard page in that case.
 835   size_t guard_size = os::Linux::default_guard_size(thr_type);
 836   if (stack_size <= SIZE_MAX - guard_size) {
 837     stack_size += guard_size;















 838   }
 839   assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");
 840 
 841   int status = pthread_attr_setstacksize(&attr, stack_size);
 842   assert_status(status == 0, status, "pthread_attr_setstacksize");
 843 
 844   // Configure glibc guard page.
 845   pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
 846 
 847   ThreadState state;
 848 
 849   {
 850     pthread_t tid;
 851     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
 852 
 853     char buf[64];
 854     if (ret == 0) {
 855       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 856         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 857     } else {
 858       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 859         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 860       // Log some OS information which might explain why creating the thread failed.
 861       log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
 862       LogStream st(Log(os, thread)::info());
 863       os::Posix::print_rlimit_info(&st);
 864       os::print_memory_info(&st);
 865       os::Linux::print_proc_sys_info(&st);
 866       os::Linux::print_container_info(&st);


5104   // This could be set after os::Posix::init() but all platforms
5105   // have to set it the same so we have to mirror Solaris.
5106   DEBUG_ONLY(os::set_mutex_init_done();)
5107 
5108   os::Posix::init_2();
5109 
5110   Linux::fast_thread_clock_init();
5111 
5112   // initialize suspend/resume support - must do this before signal_sets_init()
5113   if (SR_initialize() != 0) {
5114     perror("SR_initialize failed");
5115     return JNI_ERR;
5116   }
5117 
5118   Linux::signal_sets_init();
5119   Linux::install_signal_handlers();
5120   // Initialize data for jdk.internal.misc.Signal
5121   if (!ReduceSignalUsage) {
5122     jdk_misc_signal_init();
5123   }


5124 
5125   // Check and sets minimum stack sizes against command line options
5126   if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
5127     return JNI_ERR;
5128   }
5129 
5130 #if defined(IA32)
5131   // Need to ensure we've determined the process's initial stack to
5132   // perform the workaround
5133   Linux::capture_initial_stack(JavaThread::stack_size_at_create());
5134   workaround_expand_exec_shield_cs_limit();
5135 #else
5136   suppress_primordial_thread_resolution = Arguments::created_by_java_launcher();
5137   if (!suppress_primordial_thread_resolution) {
5138     Linux::capture_initial_stack(JavaThread::stack_size_at_create());
5139   }
5140 #endif
5141 
5142   Linux::libpthread_init();
5143   Linux::sched_getcpu_init();




 783     while (osthread->get_state() == INITIALIZED) {
 784       sync->wait_without_safepoint_check();
 785     }
 786   }
 787 
 788   assert(osthread->pthread_id() != 0, "pthread_id was not set as expected");
 789 
 790   // call one more level start routine
 791   thread->call_run();
 792 
 793   // Note: at this point the thread object may already have deleted itself.
 794   // Prevent dereferencing it from here on out.
 795   thread = NULL;
 796 
 797   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
 798     os::current_thread_id(), (uintx) pthread_self());
 799 
 800   return 0;
 801 }
 802 
 803 // On Linux, glibc places static TLS blocks (for __thread variables) on
 804 // the thread stack. This decreases the stack size actually available
 805 // to threads.
 806 //
 807 // For large static TLS sizes, this may cause threads to malfunction due
 808 // to insufficient stack space. This is a well-known issue in glibc:
 809 // http://sourceware.org/bugzilla/show_bug.cgi?id=11787.
 810 //
 811 // As a workaround, we call a private but assumed-stable glibc function,
 812 // __pthread_get_minstack() to obtain the minstack size and derive the
 813 // static TLS size from it. We then increase the user requested stack
 814 // size by this TLS size.
 815 //
 816 // Due to compatibility concerns, this size adjustment is opt-in and
 817 // controlled via AdjustStackSizeForTLS.
 818 typedef size_t (*GetMinStack)(const pthread_attr_t *attr);
 819 
 820 GetMinStack _get_minstack_func = NULL;
 821 
 822 static void get_minstack_init() {
 823   _get_minstack_func =
 824         (GetMinStack)dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
 825 }
 826 
 827 // Returns the size of the static TLS area glibc puts on thread stacks.
 828 static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
 829   size_t tls_size = 0;
 830   if (_get_minstack_func != NULL) {
 831     // Obtain the pthread minstack size by calling __pthread_get_minstack.
 832     size_t minstack_size = _get_minstack_func(attr);
 833 
 834     // Remove non-TLS area size included in minstack size returned
 835     // by __pthread_get_minstack() to get the static TLS size.
 836     // In glibc before 2.27, minstack size includes guard_size.
 837     // In glibc 2.27 and later, guard_size is automatically added
 838     // to the stack size by pthread_create and is no longer included
 839     // in minstack size. In both cases, the guard_size is taken into
 840     // account, so there is no need to adjust the result for that.
 841     //
 842     // Although __pthread_get_minstack() is a private glibc function,
 843     // it is expected to have a stable behavior across future glibc
 844     // versions while glibc still allocats the static TLS blocks off
 845     // the stack. Following is glibc 2.28 __pthread_get_minstack(): 
 846     //
 847     // size_t
 848     // __pthread_get_minstack (const pthread_attr_t *attr)
 849     // {
 850     //   return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
 851     // }
 852     //
 853     tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN;
 854     assert(tls_size > 0, "unexpected size");
 855   }
 856   return tls_size;
 857 }
 858 
 859 bool os::create_thread(Thread* thread, ThreadType thr_type,
 860                        size_t req_stack_size) {
 861   assert(thread->osthread() == NULL, "caller responsible");
 862 
 863   // Allocate the OSThread object
 864   OSThread* osthread = new OSThread(NULL, NULL);
 865   if (osthread == NULL) {
 866     return false;
 867   }
 868 
 869   // set the correct thread state
 870   osthread->set_thread_type(thr_type);
 871 
 872   // Initial state is ALLOCATED but not INITIALIZED
 873   osthread->set_state(ALLOCATED);
 874 
 875   thread->set_osthread(osthread);
 876 
 877   // init thread attributes
 878   pthread_attr_t attr;
 879   pthread_attr_init(&attr);
 880   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 881 
 882   // Calculate stack size if it's not specified by caller.
 883   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
 884   // In the Linux NPTL pthread implementation the guard size mechanism
 885   // is not implemented properly. The posix standard requires adding
 886   // the size of the guard pages to the stack size, instead Linux
 887   // takes the space out of 'stacksize'. Thus we adapt the requested
 888   // stack_size by the size of the guard pages to mimick proper
 889   // behaviour. However, be careful not to end up with a size
 890   // of zero due to overflow. Don't add the guard page in that case.
 891   size_t guard_size = os::Linux::default_guard_size(thr_type);
 892   // Configure glibc guard page.
 893   pthread_attr_setguardsize(&attr, guard_size);
 894  
 895   size_t stack_adjust_size = 0;
 896   if (AdjustStackSizeForTLS) {
 897     // Adjust the stack_size by adding the on-stack TLS size if
 898     // AdjustStackSizeForTLS is true. The guard size is already
 899     // accounted in this case, please see comments in
 900     // get_static_tls_area_size().
 901     stack_adjust_size += get_static_tls_area_size(&attr);
 902   } else {
 903     stack_adjust_size += guard_size;
 904   }
 905 
 906   stack_adjust_size = align_up(stack_adjust_size, os::vm_page_size()); 
 907   if (stack_size <= SIZE_MAX - stack_adjust_size) {
 908     stack_size += stack_adjust_size;
 909   }
 910   assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");
 911 
 912   int status = pthread_attr_setstacksize(&attr, stack_size);
 913   assert_status(status == 0, status, "pthread_attr_setstacksize");
 914 



 915   ThreadState state;
 916 
 917   {
 918     pthread_t tid;
 919     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
 920 
 921     char buf[64];
 922     if (ret == 0) {
 923       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 924         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 925     } else {
 926       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 927         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 928       // Log some OS information which might explain why creating the thread failed.
 929       log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
 930       LogStream st(Log(os, thread)::info());
 931       os::Posix::print_rlimit_info(&st);
 932       os::print_memory_info(&st);
 933       os::Linux::print_proc_sys_info(&st);
 934       os::Linux::print_container_info(&st);


5172   // This could be set after os::Posix::init() but all platforms
5173   // have to set it the same so we have to mirror Solaris.
5174   DEBUG_ONLY(os::set_mutex_init_done();)
5175 
5176   os::Posix::init_2();
5177 
5178   Linux::fast_thread_clock_init();
5179 
5180   // initialize suspend/resume support - must do this before signal_sets_init()
5181   if (SR_initialize() != 0) {
5182     perror("SR_initialize failed");
5183     return JNI_ERR;
5184   }
5185 
5186   Linux::signal_sets_init();
5187   Linux::install_signal_handlers();
5188   // Initialize data for jdk.internal.misc.Signal
5189   if (!ReduceSignalUsage) {
5190     jdk_misc_signal_init();
5191   }
5192 
5193   get_minstack_init();
5194 
5195   // Check and sets minimum stack sizes against command line options
5196   if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
5197     return JNI_ERR;
5198   }
5199 
5200 #if defined(IA32)
5201   // Need to ensure we've determined the process's initial stack to
5202   // perform the workaround
5203   Linux::capture_initial_stack(JavaThread::stack_size_at_create());
5204   workaround_expand_exec_shield_cs_limit();
5205 #else
5206   suppress_primordial_thread_resolution = Arguments::created_by_java_launcher();
5207   if (!suppress_primordial_thread_resolution) {
5208     Linux::capture_initial_stack(JavaThread::stack_size_at_create());
5209   }
5210 #endif
5211 
5212   Linux::libpthread_init();
5213   Linux::sched_getcpu_init();


< prev index next >