2663 2664 if (!recoverable_mmap_error(err)) { 2665 warn_fail_commit_memory(addr, size, exec, err); 2666 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory."); 2667 } 2668 2669 return err; 2670 } 2671 2672 bool os::pd_commit_memory(char* addr, size_t size, bool exec) { 2673 return os::Linux::commit_memory_impl(addr, size, exec) == 0; 2674 } 2675 2676 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec, 2677 const char* mesg) { 2678 assert(mesg != NULL, "mesg must be specified"); 2679 int err = os::Linux::commit_memory_impl(addr, size, exec); 2680 if (err != 0) { 2681 // the caller wants all commit errors to exit with the specified mesg: 2682 warn_fail_commit_memory(addr, size, exec, err); 2683 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg); 2684 } 2685 } 2686 2687 // Define MAP_HUGETLB here so we can build HotSpot on old systems. 2688 #ifndef MAP_HUGETLB 2689 #define MAP_HUGETLB 0x40000 2690 #endif 2691 2692 // Define MADV_HUGEPAGE here so we can build HotSpot on old systems. 2693 #ifndef MADV_HUGEPAGE 2694 #define MADV_HUGEPAGE 14 2695 #endif 2696 2697 int os::Linux::commit_memory_impl(char* addr, size_t size, 2698 size_t alignment_hint, bool exec) { 2699 int err = os::Linux::commit_memory_impl(addr, size, exec); 2700 if (err == 0) { 2701 realign_memory(addr, size, alignment_hint); 2702 } 2703 return err; 2704 } 2705 2706 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 2707 bool exec) { 2708 return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0; 2709 } 2710 2711 void os::pd_commit_memory_or_exit(char* addr, size_t size, 2712 size_t alignment_hint, bool exec, 2713 const char* mesg) { 2714 assert(mesg != NULL, "mesg must be specified"); 2715 int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec); 2716 if (err != 0) { 2717 // the caller wants all commit errors to exit with the specified mesg: 2718 warn_fail_commit_memory(addr, size, alignment_hint, exec, err); 2719 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg); 2720 } 2721 } 2722 2723 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { 2724 if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) { 2725 // We don't check the return value: madvise(MADV_HUGEPAGE) may not 2726 // be supported or the memory may already be backed by huge pages. 2727 ::madvise(addr, bytes, MADV_HUGEPAGE); 2728 } 2729 } 2730 2731 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { 2732 // This method works by doing an mmap over an existing mmaping and effectively discarding 2733 // the existing pages. However it won't work for SHM-based large pages that cannot be 2734 // uncommitted at all. We don't do anything in this case to avoid creating a segment with 2735 // small pages on top of the SHM segment. This method always works for small pages, so we 2736 // allow that in any case. 2737 if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) { 2738 commit_memory(addr, bytes, alignment_hint, !ExecMem); 2739 } 4261 void os::Linux::set_signal_handler(int sig, bool set_installed) { 4262 // Check for overwrite. 4263 struct sigaction oldAct; 4264 sigaction(sig, (struct sigaction*)NULL, &oldAct); 4265 4266 void* oldhand = oldAct.sa_sigaction 4267 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 4268 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 4269 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) && 4270 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) && 4271 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) { 4272 if (AllowUserSignalHandlers || !set_installed) { 4273 // Do not overwrite; user takes responsibility to forward to us. 4274 return; 4275 } else if (UseSignalChaining) { 4276 // save the old handler in jvm 4277 save_preinstalled_handler(sig, oldAct); 4278 // libjsig also interposes the sigaction() call below and saves the 4279 // old sigaction on it own. 4280 } else { 4281 fatal(err_msg("Encountered unexpected pre-existing sigaction handler " 4282 "%#lx for signal %d.", (long)oldhand, sig)); 4283 } 4284 } 4285 4286 struct sigaction sigAct; 4287 sigfillset(&(sigAct.sa_mask)); 4288 sigAct.sa_handler = SIG_DFL; 4289 if (!set_installed) { 4290 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4291 } else { 4292 sigAct.sa_sigaction = signalHandler; 4293 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4294 } 4295 // Save flags, which are set by ours 4296 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 4297 sigflags[sig] = sigAct.sa_flags; 4298 4299 int ret = sigaction(sig, &sigAct, &oldAct); 4300 assert(ret == 0, "check"); 4301 4302 void* oldhand2 = oldAct.sa_sigaction 4594 } 4595 return buf; 4596 } else { 4597 return NULL; 4598 } 4599 } 4600 4601 // this is called _before_ the most of global arguments have been parsed 4602 void os::init(void) { 4603 char dummy; // used to get a guess on initial stack address 4604 // first_hrtime = gethrtime(); 4605 4606 clock_tics_per_sec = sysconf(_SC_CLK_TCK); 4607 4608 init_random(1234567); 4609 4610 ThreadCritical::initialize(); 4611 4612 Linux::set_page_size(sysconf(_SC_PAGESIZE)); 4613 if (Linux::page_size() == -1) { 4614 fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)", 4615 strerror(errno))); 4616 } 4617 init_page_sizes((size_t) Linux::page_size()); 4618 4619 Linux::initialize_system_info(); 4620 4621 // main_thread points to the aboriginal thread 4622 Linux::_main_thread = pthread_self(); 4623 4624 Linux::clock_init(); 4625 initial_time_count = javaTimeNanos(); 4626 4627 // pthread_condattr initialization for monotonic clock 4628 int status; 4629 pthread_condattr_t* _condattr = os::Linux::condAttr(); 4630 if ((status = pthread_condattr_init(_condattr)) != 0) { 4631 fatal(err_msg("pthread_condattr_init: %s", strerror(status))); 4632 } 4633 // Only set the clock if CLOCK_MONOTONIC is available 4634 if (os::supports_monotonic_clock()) { 4635 if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) { 4636 if (status == EINVAL) { 4637 warning("Unable to use monotonic clock with relative timed-waits" \ 4638 " - changes to the time-of-day clock may have adverse affects"); 4639 } else { 4640 fatal(err_msg("pthread_condattr_setclock: %s", strerror(status))); 4641 } 4642 } 4643 } 4644 // else it defaults to CLOCK_REALTIME 4645 4646 // If the pagesize of the VM is greater than 8K determine the appropriate 4647 // number of initial guard pages. The user can change this with the 4648 // command line arguments, if needed. 4649 if (vm_page_size() > (int)Linux::vm_default_page_size()) { 4650 StackYellowPages = 1; 4651 StackRedPages = 1; 4652 StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size(); 4653 } 4654 4655 // retrieve entry point for pthread_setname_np 4656 Linux::_pthread_setname_np = 4657 (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np"); 4658 4659 } 4660 | 2663 2664 if (!recoverable_mmap_error(err)) { 2665 warn_fail_commit_memory(addr, size, exec, err); 2666 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory."); 2667 } 2668 2669 return err; 2670 } 2671 2672 bool os::pd_commit_memory(char* addr, size_t size, bool exec) { 2673 return os::Linux::commit_memory_impl(addr, size, exec) == 0; 2674 } 2675 2676 void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec, 2677 const char* mesg) { 2678 assert(mesg != NULL, "mesg must be specified"); 2679 int err = os::Linux::commit_memory_impl(addr, size, exec); 2680 if (err != 0) { 2681 // the caller wants all commit errors to exit with the specified mesg: 2682 warn_fail_commit_memory(addr, size, exec, err); 2683 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg); 2684 } 2685 } 2686 2687 // Define MAP_HUGETLB here so we can build HotSpot on old systems. 2688 #ifndef MAP_HUGETLB 2689 #define MAP_HUGETLB 0x40000 2690 #endif 2691 2692 // Define MADV_HUGEPAGE here so we can build HotSpot on old systems. 2693 #ifndef MADV_HUGEPAGE 2694 #define MADV_HUGEPAGE 14 2695 #endif 2696 2697 int os::Linux::commit_memory_impl(char* addr, size_t size, 2698 size_t alignment_hint, bool exec) { 2699 int err = os::Linux::commit_memory_impl(addr, size, exec); 2700 if (err == 0) { 2701 realign_memory(addr, size, alignment_hint); 2702 } 2703 return err; 2704 } 2705 2706 bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint, 2707 bool exec) { 2708 return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0; 2709 } 2710 2711 void os::pd_commit_memory_or_exit(char* addr, size_t size, 2712 size_t alignment_hint, bool exec, 2713 const char* mesg) { 2714 assert(mesg != NULL, "mesg must be specified"); 2715 int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec); 2716 if (err != 0) { 2717 // the caller wants all commit errors to exit with the specified mesg: 2718 warn_fail_commit_memory(addr, size, alignment_hint, exec, err); 2719 vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "%s", mesg); 2720 } 2721 } 2722 2723 void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) { 2724 if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) { 2725 // We don't check the return value: madvise(MADV_HUGEPAGE) may not 2726 // be supported or the memory may already be backed by huge pages. 2727 ::madvise(addr, bytes, MADV_HUGEPAGE); 2728 } 2729 } 2730 2731 void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) { 2732 // This method works by doing an mmap over an existing mmaping and effectively discarding 2733 // the existing pages. However it won't work for SHM-based large pages that cannot be 2734 // uncommitted at all. We don't do anything in this case to avoid creating a segment with 2735 // small pages on top of the SHM segment. This method always works for small pages, so we 2736 // allow that in any case. 2737 if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) { 2738 commit_memory(addr, bytes, alignment_hint, !ExecMem); 2739 } 4261 void os::Linux::set_signal_handler(int sig, bool set_installed) { 4262 // Check for overwrite. 4263 struct sigaction oldAct; 4264 sigaction(sig, (struct sigaction*)NULL, &oldAct); 4265 4266 void* oldhand = oldAct.sa_sigaction 4267 ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction) 4268 : CAST_FROM_FN_PTR(void*, oldAct.sa_handler); 4269 if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) && 4270 oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) && 4271 oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) { 4272 if (AllowUserSignalHandlers || !set_installed) { 4273 // Do not overwrite; user takes responsibility to forward to us. 4274 return; 4275 } else if (UseSignalChaining) { 4276 // save the old handler in jvm 4277 save_preinstalled_handler(sig, oldAct); 4278 // libjsig also interposes the sigaction() call below and saves the 4279 // old sigaction on it own. 4280 } else { 4281 fatal("Encountered unexpected pre-existing sigaction handler " 4282 "%#lx for signal %d.", (long)oldhand, sig); 4283 } 4284 } 4285 4286 struct sigaction sigAct; 4287 sigfillset(&(sigAct.sa_mask)); 4288 sigAct.sa_handler = SIG_DFL; 4289 if (!set_installed) { 4290 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4291 } else { 4292 sigAct.sa_sigaction = signalHandler; 4293 sigAct.sa_flags = SA_SIGINFO|SA_RESTART; 4294 } 4295 // Save flags, which are set by ours 4296 assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range"); 4297 sigflags[sig] = sigAct.sa_flags; 4298 4299 int ret = sigaction(sig, &sigAct, &oldAct); 4300 assert(ret == 0, "check"); 4301 4302 void* oldhand2 = oldAct.sa_sigaction 4594 } 4595 return buf; 4596 } else { 4597 return NULL; 4598 } 4599 } 4600 4601 // this is called _before_ the most of global arguments have been parsed 4602 void os::init(void) { 4603 char dummy; // used to get a guess on initial stack address 4604 // first_hrtime = gethrtime(); 4605 4606 clock_tics_per_sec = sysconf(_SC_CLK_TCK); 4607 4608 init_random(1234567); 4609 4610 ThreadCritical::initialize(); 4611 4612 Linux::set_page_size(sysconf(_SC_PAGESIZE)); 4613 if (Linux::page_size() == -1) { 4614 fatal("os_linux.cpp: os::init: sysconf failed (%s)", 4615 strerror(errno)); 4616 } 4617 init_page_sizes((size_t) Linux::page_size()); 4618 4619 Linux::initialize_system_info(); 4620 4621 // main_thread points to the aboriginal thread 4622 Linux::_main_thread = pthread_self(); 4623 4624 Linux::clock_init(); 4625 initial_time_count = javaTimeNanos(); 4626 4627 // pthread_condattr initialization for monotonic clock 4628 int status; 4629 pthread_condattr_t* _condattr = os::Linux::condAttr(); 4630 if ((status = pthread_condattr_init(_condattr)) != 0) { 4631 fatal("pthread_condattr_init: %s", strerror(status)); 4632 } 4633 // Only set the clock if CLOCK_MONOTONIC is available 4634 if (os::supports_monotonic_clock()) { 4635 if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) { 4636 if (status == EINVAL) { 4637 warning("Unable to use monotonic clock with relative timed-waits" \ 4638 " - changes to the time-of-day clock may have adverse affects"); 4639 } else { 4640 fatal("pthread_condattr_setclock: %s", strerror(status)); 4641 } 4642 } 4643 } 4644 // else it defaults to CLOCK_REALTIME 4645 4646 // If the pagesize of the VM is greater than 8K determine the appropriate 4647 // number of initial guard pages. The user can change this with the 4648 // command line arguments, if needed. 4649 if (vm_page_size() > (int)Linux::vm_default_page_size()) { 4650 StackYellowPages = 1; 4651 StackRedPages = 1; 4652 StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size(); 4653 } 4654 4655 // retrieve entry point for pthread_setname_np 4656 Linux::_pthread_setname_np = 4657 (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np"); 4658 4659 } 4660 |