< 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);


5103 
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();




 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   log_info(os, thread)("Lookup of __pthread_get_minstack %s",
 826                        _get_minstack_func == NULL ? "failed" : "succeeded");
 827 }
 828 
 829 // Returns the size of the static TLS area glibc puts on thread stacks.
 830 static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
 831   size_t tls_size = 0;
 832   if (_get_minstack_func != NULL) {
 833     // Obtain the pthread minstack size by calling __pthread_get_minstack.
 834     size_t minstack_size = _get_minstack_func(attr);
 835 
 836     // Remove non-TLS area size included in minstack size returned
 837     // by __pthread_get_minstack() to get the static TLS size.
 838     // In glibc before 2.27, minstack size includes guard_size.
 839     // In glibc 2.27 and later, guard_size is automatically added
 840     // to the stack size by pthread_create and is no longer included
 841     // in minstack size. In both cases, the guard_size is taken into
 842     // account, so there is no need to adjust the result for that.
 843     //
 844     // Although __pthread_get_minstack() is a private glibc function,
 845     // it is expected to have a stable behavior across future glibc
 846     // versions while glibc still allocats the static TLS blocks off
 847     // the stack. Following is glibc 2.28 __pthread_get_minstack(): 
 848     //
 849     // size_t
 850     // __pthread_get_minstack (const pthread_attr_t *attr)
 851     // {
 852     //   return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
 853     // }
 854     //
 855     //
 856     // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN'
 857     // if check is done for precaution.
 858     if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) {  
 859       tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN;
 860     }
 861   }
 862 
 863   log_info(os, thread)("Stack size adjustment for TLS is " SIZE_FORMAT,
 864                        tls_size);
 865   return tls_size;
 866 }
 867 
 868 bool os::create_thread(Thread* thread, ThreadType thr_type,
 869                        size_t req_stack_size) {
 870   assert(thread->osthread() == NULL, "caller responsible");
 871 
 872   // Allocate the OSThread object
 873   OSThread* osthread = new OSThread(NULL, NULL);
 874   if (osthread == NULL) {
 875     return false;
 876   }
 877 
 878   // set the correct thread state
 879   osthread->set_thread_type(thr_type);
 880 
 881   // Initial state is ALLOCATED but not INITIALIZED
 882   osthread->set_state(ALLOCATED);
 883 
 884   thread->set_osthread(osthread);
 885 
 886   // init thread attributes
 887   pthread_attr_t attr;
 888   pthread_attr_init(&attr);
 889   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 890 
 891   // Calculate stack size if it's not specified by caller.
 892   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
 893   // In glibc versions prior to 2.7 the guard size mechanism
 894   // is not implemented properly. The posix standard requires adding
 895   // the size of the guard pages to the stack size, instead Linux
 896   // takes the space out of 'stacksize'. Thus we adapt the requested
 897   // stack_size by the size of the guard pages to mimick proper
 898   // behaviour. However, be careful not to end up with a size
 899   // of zero due to overflow. Don't add the guard page in that case.
 900   size_t guard_size = os::Linux::default_guard_size(thr_type);
 901   // Configure glibc guard page. Must happen before calling
 902   // get_static_tls_area_size(), which uses the guard_size.
 903   pthread_attr_setguardsize(&attr, guard_size);
 904  
 905   size_t stack_adjust_size = 0;
 906   if (AdjustStackSizeForTLS) {
 907     // Adjust the stack_size for on-stack TLS - see get_static_tls_area_size().
 908     stack_adjust_size += get_static_tls_area_size(&attr);
 909   } else {
 910     stack_adjust_size += guard_size;
 911   }
 912 
 913   stack_adjust_size = align_up(stack_adjust_size, os::vm_page_size()); 
 914   if (stack_size <= SIZE_MAX - stack_adjust_size) {
 915     stack_size += stack_adjust_size;
 916   }
 917   assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");
 918 
 919   int status = pthread_attr_setstacksize(&attr, stack_size);
 920   assert_status(status == 0, status, "pthread_attr_setstacksize");
 921 



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


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


< prev index next >