< prev index next >

hotspot/src/os/linux/vm/os_linux.cpp

Print this page




 684 
 685   // call one more level start routine
 686   thread->run();
 687 
 688   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
 689     os::current_thread_id(), (uintx) pthread_self());
 690 
 691   // If a thread has not deleted itself ("delete this") as part of its
 692   // termination sequence, we have to ensure thread-local-storage is
 693   // cleared before we actually terminate. No threads should ever be
 694   // deleted asynchronously with respect to their termination.
 695   if (Thread::current_or_null_safe() != NULL) {
 696     assert(Thread::current_or_null_safe() == thread, "current thread is wrong");
 697     thread->clear_thread_current();
 698   }
 699 
 700   return 0;
 701 }
 702 
 703 bool os::create_thread(Thread* thread, ThreadType thr_type,
 704                        size_t stack_size) {
 705   assert(thread->osthread() == NULL, "caller responsible");
 706 
 707   // Allocate the OSThread object
 708   OSThread* osthread = new OSThread(NULL, NULL);
 709   if (osthread == NULL) {
 710     return false;
 711   }
 712 
 713   // set the correct thread state
 714   osthread->set_thread_type(thr_type);
 715 
 716   // Initial state is ALLOCATED but not INITIALIZED
 717   osthread->set_state(ALLOCATED);
 718 
 719   thread->set_osthread(osthread);
 720 
 721   // init thread attributes
 722   pthread_attr_t attr;
 723   pthread_attr_init(&attr);
 724   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 725 
 726   // stack size
 727   // calculate stack size if it's not specified by caller
 728   if (stack_size == 0) {
 729     stack_size = os::Linux::default_stack_size(thr_type);
 730 
 731     switch (thr_type) {
 732     case os::java_thread:
 733       // Java threads use ThreadStackSize which default value can be
 734       // changed with the flag -Xss
 735       assert(JavaThread::stack_size_at_create() > 0, "this should be set");
 736       stack_size = JavaThread::stack_size_at_create();
 737       break;
 738     case os::compiler_thread:
 739       if (CompilerThreadStackSize > 0) {
 740         stack_size = (size_t)(CompilerThreadStackSize * K);
 741         break;
 742       } // else fall through:
 743         // use VMThreadStackSize if CompilerThreadStackSize is not defined
 744     case os::vm_thread:
 745     case os::pgc_thread:
 746     case os::cgc_thread:
 747     case os::watcher_thread:
 748       if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
 749       break;
 750     }
 751   }
 752 
 753   stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
 754   pthread_attr_setstacksize(&attr, stack_size);
 755 
 756   // glibc guard page
 757   pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
 758 
 759   ThreadState state;
 760 
 761   {
 762     pthread_t tid;
 763     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
 764 
 765     char buf[64];
 766     if (ret == 0) {
 767       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 768         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 769     } else {
 770       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 771         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 772     }
 773 


 939           if (vma_high) *vma_high = high;
 940           fclose(fp);
 941           return true;
 942         }
 943       }
 944       for (;;) {
 945         int ch = fgetc(fp);
 946         if (ch == EOF || ch == (int)'\n') break;
 947       }
 948     }
 949     fclose(fp);
 950   }
 951   return false;
 952 }
 953 
 954 // Locate initial thread stack. This special handling of initial thread stack
 955 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
 956 // bogus value for initial thread.
 957 void os::Linux::capture_initial_stack(size_t max_size) {
 958   // stack size is the easy part, get it from RLIMIT_STACK
 959   size_t stack_size;
 960   struct rlimit rlim;
 961   getrlimit(RLIMIT_STACK, &rlim);
 962   stack_size = rlim.rlim_cur;
 963 
 964   // 6308388: a bug in ld.so will relocate its own .data section to the
 965   //   lower end of primordial stack; reduce ulimit -s value a little bit
 966   //   so we won't install guard page on ld.so's data section.
 967   stack_size -= 2 * page_size();
 968 
 969   // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
 970   //   7.1, in both cases we will get 2G in return value.
 971   // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
 972   //   SuSE 7.2, Debian) can not handle alternate signal stack correctly
 973   //   for initial thread if its stack size exceeds 6M. Cap it at 2M,
 974   //   in case other parts in glibc still assumes 2M max stack size.
 975   // FIXME: alt signal stack is gone, maybe we can relax this constraint?
 976   // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
 977   if (stack_size > 2 * K * K IA64_ONLY(*2)) {
 978     stack_size = 2 * K * K IA64_ONLY(*2);
 979   }
 980   // Try to figure out where the stack base (top) is. This is harder.
 981   //
 982   // When an application is started, glibc saves the initial stack pointer in


4776 
4777   os::set_polling_page(polling_page);
4778   log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4779 
4780   if (!UseMembar) {
4781     address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4782     guarantee(mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");
4783     os::set_memory_serialize_page(mem_serialize_page);
4784     log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4785   }
4786 
4787   // initialize suspend/resume support - must do this before signal_sets_init()
4788   if (SR_initialize() != 0) {
4789     perror("SR_initialize failed");
4790     return JNI_ERR;
4791   }
4792 
4793   Linux::signal_sets_init();
4794   Linux::install_signal_handlers();
4795 
4796   // Check minimum allowable stack size for thread creation and to initialize
4797   // the java system classes, including StackOverflowError - depends on page
4798   // size.  Add two 4K pages for compiler2 recursion in main thread.
4799   // Add in 4*BytesPerWord 4K pages to account for VM stack during
4800   // class initialization depending on 32 or 64 bit VM.
4801   os::Linux::min_stack_allowed = MAX2(os::Linux::min_stack_allowed,
4802                                       JavaThread::stack_guard_zone_size() +
4803                                       JavaThread::stack_shadow_zone_size() +
4804                                       (4*BytesPerWord COMPILER2_PRESENT(+2)) * 4 * K);
4805 
4806   os::Linux::min_stack_allowed = align_size_up(os::Linux::min_stack_allowed, os::vm_page_size());
4807 
4808   size_t threadStackSizeInBytes = ThreadStackSize * K;
4809   if (threadStackSizeInBytes != 0 &&
4810       threadStackSizeInBytes < os::Linux::min_stack_allowed) {
4811     tty->print_cr("\nThe stack size specified is too small, "
4812                   "Specify at least " SIZE_FORMAT "k",
4813                   os::Linux::min_stack_allowed/ K);
4814     return JNI_ERR;
4815   }
4816 
4817   // Make the stack size a multiple of the page size so that
4818   // the yellow/red zones can be guarded.
4819   JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
4820                                                 vm_page_size()));
4821 
4822   Linux::capture_initial_stack(JavaThread::stack_size_at_create());
4823 
4824 #if defined(IA32)
4825   workaround_expand_exec_shield_cs_limit();
4826 #endif
4827 
4828   Linux::libpthread_init();
4829   log_info(os)("HotSpot is running with %s, %s",
4830                Linux::glibc_version(), Linux::libpthread_version());
4831 
4832   if (UseNUMA) {
4833     if (!Linux::libnuma_init()) {
4834       UseNUMA = false;
4835     } else {
4836       if ((Linux::numa_max_node() < 1)) {
4837         // There's only one node(they start from 0), disable NUMA.
4838         UseNUMA = false;
4839       }
4840     }
4841     // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way




 684 
 685   // call one more level start routine
 686   thread->run();
 687 
 688   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
 689     os::current_thread_id(), (uintx) pthread_self());
 690 
 691   // If a thread has not deleted itself ("delete this") as part of its
 692   // termination sequence, we have to ensure thread-local-storage is
 693   // cleared before we actually terminate. No threads should ever be
 694   // deleted asynchronously with respect to their termination.
 695   if (Thread::current_or_null_safe() != NULL) {
 696     assert(Thread::current_or_null_safe() == thread, "current thread is wrong");
 697     thread->clear_thread_current();
 698   }
 699 
 700   return 0;
 701 }
 702 
 703 bool os::create_thread(Thread* thread, ThreadType thr_type,
 704                        size_t req_stack_size) {
 705   assert(thread->osthread() == NULL, "caller responsible");
 706 
 707   // Allocate the OSThread object
 708   OSThread* osthread = new OSThread(NULL, NULL);
 709   if (osthread == NULL) {
 710     return false;
 711   }
 712 
 713   // set the correct thread state
 714   osthread->set_thread_type(thr_type);
 715 
 716   // Initial state is ALLOCATED but not INITIALIZED
 717   osthread->set_state(ALLOCATED);
 718 
 719   thread->set_osthread(osthread);
 720 
 721   // init thread attributes
 722   pthread_attr_t attr;
 723   pthread_attr_init(&attr);
 724   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
 725 

 726   // calculate stack size if it's not specified by caller
 727   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);

























 728   pthread_attr_setstacksize(&attr, stack_size);
 729 
 730   // glibc guard page
 731   pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
 732 
 733   ThreadState state;
 734 
 735   {
 736     pthread_t tid;
 737     int ret = pthread_create(&tid, &attr, (void* (*)(void*)) thread_native_entry, thread);
 738 
 739     char buf[64];
 740     if (ret == 0) {
 741       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
 742         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 743     } else {
 744       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
 745         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
 746     }
 747 


 913           if (vma_high) *vma_high = high;
 914           fclose(fp);
 915           return true;
 916         }
 917       }
 918       for (;;) {
 919         int ch = fgetc(fp);
 920         if (ch == EOF || ch == (int)'\n') break;
 921       }
 922     }
 923     fclose(fp);
 924   }
 925   return false;
 926 }
 927 
 928 // Locate initial thread stack. This special handling of initial thread stack
 929 // is needed because pthread_getattr_np() on most (all?) Linux distros returns
 930 // bogus value for initial thread.
 931 void os::Linux::capture_initial_stack(size_t max_size) {
 932   // stack size is the easy part, get it from RLIMIT_STACK

 933   struct rlimit rlim;
 934   getrlimit(RLIMIT_STACK, &rlim);
 935   size_t stack_size = rlim.rlim_cur;
 936 
 937   // 6308388: a bug in ld.so will relocate its own .data section to the
 938   //   lower end of primordial stack; reduce ulimit -s value a little bit
 939   //   so we won't install guard page on ld.so's data section.
 940   stack_size -= 2 * page_size();
 941 
 942   // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
 943   //   7.1, in both cases we will get 2G in return value.
 944   // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
 945   //   SuSE 7.2, Debian) can not handle alternate signal stack correctly
 946   //   for initial thread if its stack size exceeds 6M. Cap it at 2M,
 947   //   in case other parts in glibc still assumes 2M max stack size.
 948   // FIXME: alt signal stack is gone, maybe we can relax this constraint?
 949   // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
 950   if (stack_size > 2 * K * K IA64_ONLY(*2)) {
 951     stack_size = 2 * K * K IA64_ONLY(*2);
 952   }
 953   // Try to figure out where the stack base (top) is. This is harder.
 954   //
 955   // When an application is started, glibc saves the initial stack pointer in


4749 
4750   os::set_polling_page(polling_page);
4751   log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4752 
4753   if (!UseMembar) {
4754     address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
4755     guarantee(mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");
4756     os::set_memory_serialize_page(mem_serialize_page);
4757     log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4758   }
4759 
4760   // initialize suspend/resume support - must do this before signal_sets_init()
4761   if (SR_initialize() != 0) {
4762     perror("SR_initialize failed");
4763     return JNI_ERR;
4764   }
4765 
4766   Linux::signal_sets_init();
4767   Linux::install_signal_handlers();
4768 
4769   // Check and sets minimum stack sizes against command line options
4770   if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
















4771     return JNI_ERR;
4772   }






4773   Linux::capture_initial_stack(JavaThread::stack_size_at_create());
4774 
4775 #if defined(IA32)
4776   workaround_expand_exec_shield_cs_limit();
4777 #endif
4778 
4779   Linux::libpthread_init();
4780   log_info(os)("HotSpot is running with %s, %s",
4781                Linux::glibc_version(), Linux::libpthread_version());
4782 
4783   if (UseNUMA) {
4784     if (!Linux::libnuma_init()) {
4785       UseNUMA = false;
4786     } else {
4787       if ((Linux::numa_max_node() < 1)) {
4788         // There's only one node(they start from 0), disable NUMA.
4789         UseNUMA = false;
4790       }
4791     }
4792     // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way


< prev index next >