900
901 // Helper function to trace thread attributes, similar to os::Posix::describe_pthread_attr()
902 static char* describe_thr_create_attributes(char* buf, size_t buflen,
903 size_t stacksize, long flags) {
904 stringStream ss(buf, buflen);
905 ss.print("stacksize: " SIZE_FORMAT "k, ", stacksize / 1024);
906 ss.print("flags: ");
907 #define PRINT_FLAG(f) if (flags & f) ss.print( #f " ");
908 #define ALL(X) \
909 X(THR_SUSPENDED) \
910 X(THR_DETACHED) \
911 X(THR_BOUND) \
912 X(THR_NEW_LWP) \
913 X(THR_DAEMON)
914 ALL(PRINT_FLAG)
915 #undef ALL
916 #undef PRINT_FLAG
917 return buf;
918 }
919
920 bool os::create_thread(Thread* thread, ThreadType thr_type,
921 size_t stack_size) {
922 // Allocate the OSThread object
923 OSThread* osthread = new OSThread(NULL, NULL);
924 if (osthread == NULL) {
925 return false;
926 }
927
928 if (ThreadPriorityVerbose) {
929 char *thrtyp;
930 switch (thr_type) {
931 case vm_thread:
932 thrtyp = (char *)"vm";
933 break;
934 case cgc_thread:
935 thrtyp = (char *)"cgc";
936 break;
937 case pgc_thread:
938 thrtyp = (char *)"pgc";
939 break;
940 case java_thread:
941 thrtyp = (char *)"java";
942 break;
943 case compiler_thread:
944 thrtyp = (char *)"compiler";
945 break;
946 case watcher_thread:
947 thrtyp = (char *)"watcher";
948 break;
949 default:
950 thrtyp = (char *)"unknown";
951 break;
952 }
953 tty->print_cr("In create_thread, creating a %s thread\n", thrtyp);
954 }
955
956 // Calculate stack size if it's not specified by caller.
957 if (stack_size == 0) {
958 // The default stack size 1M (2M for LP64).
959 stack_size = (BytesPerWord >> 2) * K * K;
960
961 switch (thr_type) {
962 case os::java_thread:
963 // Java threads use ThreadStackSize which default value can be changed with the flag -Xss
964 if (JavaThread::stack_size_at_create() > 0) stack_size = JavaThread::stack_size_at_create();
965 break;
966 case os::compiler_thread:
967 if (CompilerThreadStackSize > 0) {
968 stack_size = (size_t)(CompilerThreadStackSize * K);
969 break;
970 } // else fall through:
971 // use VMThreadStackSize if CompilerThreadStackSize is not defined
972 case os::vm_thread:
973 case os::pgc_thread:
974 case os::cgc_thread:
975 case os::watcher_thread:
976 if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
977 break;
978 }
979 }
980 stack_size = MAX2(stack_size, os::Solaris::min_stack_allowed);
981
982 // Initial state is ALLOCATED but not INITIALIZED
983 osthread->set_state(ALLOCATED);
984
985 if (os::Solaris::_os_thread_count > os::Solaris::_os_thread_limit) {
986 // We got lots of threads. Check if we still have some address space left.
987 // Need to be at least 5Mb of unreserved address space. We do check by
988 // trying to reserve some.
989 const size_t VirtualMemoryBangSize = 20*K*K;
990 char* mem = os::reserve_memory(VirtualMemoryBangSize);
991 if (mem == NULL) {
992 delete osthread;
993 return false;
994 } else {
995 // Release the memory again
996 os::release_memory(mem, VirtualMemoryBangSize);
997 }
998 }
999
1000 // Setup osthread because the child thread may need it.
1001 thread->set_osthread(osthread);
4383
4384 clock_tics_per_sec = CLK_TCK;
4385
4386 // check if dladdr1() exists; dladdr1 can provide more information than
4387 // dladdr for os::dll_address_to_function_name. It comes with SunOS 5.9
4388 // and is available on linker patches for 5.7 and 5.8.
4389 // libdl.so must have been loaded, this call is just an entry lookup
4390 void * hdl = dlopen("libdl.so", RTLD_NOW);
4391 if (hdl) {
4392 dladdr1_func = CAST_TO_FN_PTR(dladdr1_func_type, dlsym(hdl, "dladdr1"));
4393 }
4394
4395 // (Solaris only) this switches to calls that actually do locking.
4396 ThreadCritical::initialize();
4397
4398 main_thread = thr_self();
4399
4400 // Constant minimum stack size allowed. It must be at least
4401 // the minimum of what the OS supports (thr_min_stack()), and
4402 // enough to allow the thread to get to user bytecode execution.
4403 Solaris::min_stack_allowed = MAX2(thr_min_stack(), Solaris::min_stack_allowed);
4404
4405 // dynamic lookup of functions that may not be available in our lowest
4406 // supported Solaris release
4407 void * handle = dlopen("libc.so.1", RTLD_LAZY);
4408 if (handle != NULL) {
4409 Solaris::_pthread_setname_np = // from 11.3
4410 (Solaris::pthread_setname_np_func_t)dlsym(handle, "pthread_setname_np");
4411 }
4412 }
4413
4414 // To install functions for atexit system call
4415 extern "C" {
4416 static void perfMemory_exit_helper() {
4417 perfMemory_exit();
4418 }
4419 }
4420
4421 // this is called _after_ the global arguments have been parsed
4422 jint os::init_2(void) {
4423 // try to enable extended file IO ASAP, see 6431278
4428 address polling_page = (address)Solaris::mmap_chunk((char*)page_size,
4429 page_size,
4430 MAP_PRIVATE | MAP_ALIGN,
4431 PROT_READ);
4432 if (polling_page == NULL) {
4433 has_map_align = false;
4434 polling_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE,
4435 PROT_READ);
4436 }
4437
4438 os::set_polling_page(polling_page);
4439 log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4440
4441 if (!UseMembar) {
4442 address mem_serialize_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE, PROT_READ | PROT_WRITE);
4443 guarantee(mem_serialize_page != NULL, "mmap Failed for memory serialize page");
4444 os::set_memory_serialize_page(mem_serialize_page);
4445 log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4446 }
4447
4448 // Check minimum allowable stack size for thread creation and to initialize
4449 // the java system classes, including StackOverflowError - depends on page
4450 // size. Add two 4K pages for compiler2 recursion in main thread.
4451 // Add in 4*BytesPerWord 4K pages to account for VM stack during
4452 // class initialization depending on 32 or 64 bit VM.
4453 os::Solaris::min_stack_allowed = MAX2(os::Solaris::min_stack_allowed,
4454 JavaThread::stack_guard_zone_size() +
4455 JavaThread::stack_shadow_zone_size() +
4456 (4*BytesPerWord COMPILER2_PRESENT(+2)) * 4 * K);
4457
4458 os::Solaris::min_stack_allowed = align_size_up(os::Solaris::min_stack_allowed, os::vm_page_size());
4459
4460 size_t threadStackSizeInBytes = ThreadStackSize * K;
4461 if (threadStackSizeInBytes != 0 &&
4462 threadStackSizeInBytes < os::Solaris::min_stack_allowed) {
4463 tty->print_cr("\nThe stack size specified is too small, Specify at least %dk",
4464 os::Solaris::min_stack_allowed/K);
4465 return JNI_ERR;
4466 }
4467
4468 // For 64kbps there will be a 64kb page size, which makes
4469 // the usable default stack size quite a bit less. Increase the
4470 // stack for 64kb (or any > than 8kb) pages, this increases
4471 // virtual memory fragmentation (since we're not creating the
4472 // stack on a power of 2 boundary. The real fix for this
4473 // should be to fix the guard page mechanism.
4474
4475 if (vm_page_size() > 8*K) {
4476 threadStackSizeInBytes = (threadStackSizeInBytes != 0)
4477 ? threadStackSizeInBytes +
4478 JavaThread::stack_red_zone_size() +
4479 JavaThread::stack_yellow_zone_size()
4480 : 0;
4481 ThreadStackSize = threadStackSizeInBytes/K;
4482 }
4483
4484 // Make the stack size a multiple of the page size so that
4485 // the yellow/red zones can be guarded.
4486 JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
4487 vm_page_size()));
4488
4489 Solaris::libthread_init();
4490
4491 if (UseNUMA) {
4492 if (!Solaris::liblgrp_init()) {
4493 UseNUMA = false;
4494 } else {
4495 size_t lgrp_limit = os::numa_get_groups_num();
4496 int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtInternal);
4497 size_t lgrp_num = os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
4498 FREE_C_HEAP_ARRAY(int, lgrp_ids);
4499 if (lgrp_num < 2) {
4500 // There's only one locality group, disable NUMA.
4501 UseNUMA = false;
4502 }
4503 }
4504 if (!UseNUMA && ForceNUMA) {
4505 UseNUMA = true;
4506 }
4507 }
4508
|
900
901 // Helper function to trace thread attributes, similar to os::Posix::describe_pthread_attr()
902 static char* describe_thr_create_attributes(char* buf, size_t buflen,
903 size_t stacksize, long flags) {
904 stringStream ss(buf, buflen);
905 ss.print("stacksize: " SIZE_FORMAT "k, ", stacksize / 1024);
906 ss.print("flags: ");
907 #define PRINT_FLAG(f) if (flags & f) ss.print( #f " ");
908 #define ALL(X) \
909 X(THR_SUSPENDED) \
910 X(THR_DETACHED) \
911 X(THR_BOUND) \
912 X(THR_NEW_LWP) \
913 X(THR_DAEMON)
914 ALL(PRINT_FLAG)
915 #undef ALL
916 #undef PRINT_FLAG
917 return buf;
918 }
919
920 // return default stack size for thr_type
921 size_t os::Posix::default_stack_size(os::ThreadType thr_type) {
922 // default stack size when not specified by caller is 1M (2M for LP64)
923 size_t s = (BytesPerWord >> 2) * K * K;
924 return s;
925 }
926
927 bool os::create_thread(Thread* thread, ThreadType thr_type,
928 size_t req_stack_size) {
929 // Allocate the OSThread object
930 OSThread* osthread = new OSThread(NULL, NULL);
931 if (osthread == NULL) {
932 return false;
933 }
934
935 if (ThreadPriorityVerbose) {
936 char *thrtyp;
937 switch (thr_type) {
938 case vm_thread:
939 thrtyp = (char *)"vm";
940 break;
941 case cgc_thread:
942 thrtyp = (char *)"cgc";
943 break;
944 case pgc_thread:
945 thrtyp = (char *)"pgc";
946 break;
947 case java_thread:
948 thrtyp = (char *)"java";
949 break;
950 case compiler_thread:
951 thrtyp = (char *)"compiler";
952 break;
953 case watcher_thread:
954 thrtyp = (char *)"watcher";
955 break;
956 default:
957 thrtyp = (char *)"unknown";
958 break;
959 }
960 tty->print_cr("In create_thread, creating a %s thread\n", thrtyp);
961 }
962
963 // calculate stack size if it's not specified by caller
964 size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
965
966 // Initial state is ALLOCATED but not INITIALIZED
967 osthread->set_state(ALLOCATED);
968
969 if (os::Solaris::_os_thread_count > os::Solaris::_os_thread_limit) {
970 // We got lots of threads. Check if we still have some address space left.
971 // Need to be at least 5Mb of unreserved address space. We do check by
972 // trying to reserve some.
973 const size_t VirtualMemoryBangSize = 20*K*K;
974 char* mem = os::reserve_memory(VirtualMemoryBangSize);
975 if (mem == NULL) {
976 delete osthread;
977 return false;
978 } else {
979 // Release the memory again
980 os::release_memory(mem, VirtualMemoryBangSize);
981 }
982 }
983
984 // Setup osthread because the child thread may need it.
985 thread->set_osthread(osthread);
4367
4368 clock_tics_per_sec = CLK_TCK;
4369
4370 // check if dladdr1() exists; dladdr1 can provide more information than
4371 // dladdr for os::dll_address_to_function_name. It comes with SunOS 5.9
4372 // and is available on linker patches for 5.7 and 5.8.
4373 // libdl.so must have been loaded, this call is just an entry lookup
4374 void * hdl = dlopen("libdl.so", RTLD_NOW);
4375 if (hdl) {
4376 dladdr1_func = CAST_TO_FN_PTR(dladdr1_func_type, dlsym(hdl, "dladdr1"));
4377 }
4378
4379 // (Solaris only) this switches to calls that actually do locking.
4380 ThreadCritical::initialize();
4381
4382 main_thread = thr_self();
4383
4384 // Constant minimum stack size allowed. It must be at least
4385 // the minimum of what the OS supports (thr_min_stack()), and
4386 // enough to allow the thread to get to user bytecode execution.
4387 Posix::_compiler_thread_min_stack_allowed = MAX2(thr_min_stack(),
4388 Posix::_compiler_thread_min_stack_allowed);
4389 Posix::_java_thread_min_stack_allowed = MAX2(thr_min_stack(),
4390 Posix::_java_thread_min_stack_allowed);
4391 Posix::_vm_internal_thread_min_stack_allowed = MAX2(thr_min_stack(),
4392 Posix::_vm_internal_thread_min_stack_allowed);
4393
4394 // dynamic lookup of functions that may not be available in our lowest
4395 // supported Solaris release
4396 void * handle = dlopen("libc.so.1", RTLD_LAZY);
4397 if (handle != NULL) {
4398 Solaris::_pthread_setname_np = // from 11.3
4399 (Solaris::pthread_setname_np_func_t)dlsym(handle, "pthread_setname_np");
4400 }
4401 }
4402
4403 // To install functions for atexit system call
4404 extern "C" {
4405 static void perfMemory_exit_helper() {
4406 perfMemory_exit();
4407 }
4408 }
4409
4410 // this is called _after_ the global arguments have been parsed
4411 jint os::init_2(void) {
4412 // try to enable extended file IO ASAP, see 6431278
4417 address polling_page = (address)Solaris::mmap_chunk((char*)page_size,
4418 page_size,
4419 MAP_PRIVATE | MAP_ALIGN,
4420 PROT_READ);
4421 if (polling_page == NULL) {
4422 has_map_align = false;
4423 polling_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE,
4424 PROT_READ);
4425 }
4426
4427 os::set_polling_page(polling_page);
4428 log_info(os)("SafePoint Polling address: " INTPTR_FORMAT, p2i(polling_page));
4429
4430 if (!UseMembar) {
4431 address mem_serialize_page = (address)Solaris::mmap_chunk(NULL, page_size, MAP_PRIVATE, PROT_READ | PROT_WRITE);
4432 guarantee(mem_serialize_page != NULL, "mmap Failed for memory serialize page");
4433 os::set_memory_serialize_page(mem_serialize_page);
4434 log_info(os)("Memory Serialize Page address: " INTPTR_FORMAT, p2i(mem_serialize_page));
4435 }
4436
4437 // Check and sets minimum stack sizes against command line options
4438 if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
4439 return JNI_ERR;
4440 }
4441
4442 Solaris::libthread_init();
4443
4444 if (UseNUMA) {
4445 if (!Solaris::liblgrp_init()) {
4446 UseNUMA = false;
4447 } else {
4448 size_t lgrp_limit = os::numa_get_groups_num();
4449 int *lgrp_ids = NEW_C_HEAP_ARRAY(int, lgrp_limit, mtInternal);
4450 size_t lgrp_num = os::numa_get_leaf_groups(lgrp_ids, lgrp_limit);
4451 FREE_C_HEAP_ARRAY(int, lgrp_ids);
4452 if (lgrp_num < 2) {
4453 // There's only one locality group, disable NUMA.
4454 UseNUMA = false;
4455 }
4456 }
4457 if (!UseNUMA && ForceNUMA) {
4458 UseNUMA = true;
4459 }
4460 }
4461
|