Path: blob/jdk8u272-b10-aarch32-20201026/hotspot/src/os/linux/vm/os_linux.cpp
48785 views
/*1* Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324// no precompiled headers25#include "classfile/classLoader.hpp"26#include "classfile/systemDictionary.hpp"27#include "classfile/vmSymbols.hpp"28#include "code/icBuffer.hpp"29#include "code/vtableStubs.hpp"30#include "compiler/compileBroker.hpp"31#include "compiler/disassembler.hpp"32#include "interpreter/interpreter.hpp"33#include "jvm_linux.h"34#include "memory/allocation.inline.hpp"35#include "memory/filemap.hpp"36#include "mutex_linux.inline.hpp"37#include "oops/oop.inline.hpp"38#include "os_share_linux.hpp"39#include "osContainer_linux.hpp"40#include "prims/jniFastGetField.hpp"41#include "prims/jvm.h"42#include "prims/jvm_misc.hpp"43#include "runtime/arguments.hpp"44#include "runtime/extendedPC.hpp"45#include "runtime/globals.hpp"46#include "runtime/interfaceSupport.hpp"47#include "runtime/init.hpp"48#include "runtime/java.hpp"49#include "runtime/javaCalls.hpp"50#include "runtime/mutexLocker.hpp"51#include "runtime/objectMonitor.hpp"52#include "runtime/orderAccess.inline.hpp"53#include "runtime/osThread.hpp"54#include "runtime/perfMemory.hpp"55#include "runtime/sharedRuntime.hpp"56#include "runtime/statSampler.hpp"57#include "runtime/stubRoutines.hpp"58#include "runtime/thread.inline.hpp"59#include "runtime/threadCritical.hpp"60#include "runtime/timer.hpp"61#include "services/attachListener.hpp"62#include "services/memTracker.hpp"63#include "services/runtimeService.hpp"64#include "utilities/decoder.hpp"65#include "utilities/defaultStream.hpp"66#include "utilities/events.hpp"67#include "utilities/elfFile.hpp"68#include "utilities/growableArray.hpp"69#include "utilities/vmError.hpp"7071// put OS-includes here72# include <sys/types.h>73# include <sys/mman.h>74# include <sys/stat.h>75# include <sys/select.h>76# include <pthread.h>77# include <signal.h>78# include <errno.h>79# include <dlfcn.h>80# include <stdio.h>81# include <unistd.h>82# include <sys/resource.h>83# include <pthread.h>84# include <sys/stat.h>85# include <sys/time.h>86# include <sys/times.h>87# include <sys/utsname.h>88# include <sys/socket.h>89# include <sys/wait.h>90# include <pwd.h>91# include <poll.h>92# include <semaphore.h>93# include <fcntl.h>94# include <string.h>95# include <sys/sysinfo.h>96#if !defined(__UCLIBC__) && !defined(__ANDROID__)97# include <gnu/libc-version.h>98#endif99# include <sys/ipc.h>100#if !defined(__ANDROID__)101# include <syscall.h>102# include <sys/shm.h>103#else104# include <sys/syscall.h>105#include <libgen.h>106#endif107# include <link.h>108# include <stdint.h>109# include <inttypes.h>110# include <sys/ioctl.h>111112PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC113114#ifdef __ANDROID__115# define DISABLE_SHM116#endif117/*118#ifdef __ANDROID__119# define lseek lseek64120# define open open64121# define off_t off64_t122#endif123*/124#ifndef _GNU_SOURCE125#define _GNU_SOURCE126#include <sched.h>127#undef _GNU_SOURCE128#else129#include <sched.h>130#endif131132// if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling133// getrusage() is prepared to handle the associated failure.134#ifndef RUSAGE_THREAD135#define RUSAGE_THREAD (1) /* only the calling thread */136#endif137138#define MAX_PATH (2 * K)139140#define MAX_SECS 100000000141142// for timer info max values which include all bits143#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)144145#define LARGEPAGES_BIT (1 << 6)146////////////////////////////////////////////////////////////////////////////////147// global variables148julong os::Linux::_physical_memory = 0;149150address os::Linux::_initial_thread_stack_bottom = NULL;151uintptr_t os::Linux::_initial_thread_stack_size = 0;152153int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;154int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;155int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL;156Mutex* os::Linux::_createThread_lock = NULL;157pthread_t os::Linux::_main_thread;158int os::Linux::_page_size = -1;159const int os::Linux::_vm_default_page_size = (8 * K);160bool os::Linux::_is_floating_stack = false;161bool os::Linux::_is_NPTL = false;162bool os::Linux::_supports_fast_thread_cpu_time = false;163const char * os::Linux::_glibc_version = NULL;164const char * os::Linux::_libpthread_version = NULL;165pthread_condattr_t os::Linux::_condattr[1];166167static jlong initial_time_count=0;168169static int clock_tics_per_sec = 100;170171// For diagnostics to print a message once. see run_periodic_checks172static sigset_t check_signal_done;173static bool check_signals = true;174175static pid_t _initial_pid = 0;176177/* Signal number used to suspend/resume a thread */178179/* do not use any signal number less than SIGSEGV, see 4355769 */180static int SR_signum = SIGUSR2;181sigset_t SR_sigset;182183/* Used to protect dlsym() calls */184static pthread_mutex_t dl_mutex;185186// Declarations187static bool read_so_path_from_maps(const char* so_name, char* buf, int buflen);188static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);189190// utility functions191192static int SR_initialize();193194julong os::available_memory() {195return Linux::available_memory();196}197198julong os::Linux::available_memory() {199// values in struct sysinfo are "unsigned long"200struct sysinfo si;201julong avail_mem;202203if (OSContainer::is_containerized()) {204jlong mem_limit, mem_usage;205if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {206if (PrintContainerInfo) {207tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",208mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);209}210}211212if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {213if (PrintContainerInfo) {214tty->print_cr("container memory usage failed: " JLONG_FORMAT ", using host value", mem_usage);215}216}217218if (mem_limit > 0 && mem_usage > 0 ) {219avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;220if (PrintContainerInfo) {221tty->print_cr("available container memory: " JULONG_FORMAT, avail_mem);222}223return avail_mem;224}225}226227sysinfo(&si);228avail_mem = (julong)si.freeram * si.mem_unit;229if (Verbose) {230tty->print_cr("available memory: " JULONG_FORMAT, avail_mem);231}232return avail_mem;233}234235julong os::physical_memory() {236jlong phys_mem = 0;237if (OSContainer::is_containerized()) {238jlong mem_limit;239if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {240if (PrintContainerInfo) {241tty->print_cr("total container memory: " JLONG_FORMAT, mem_limit);242}243return mem_limit;244}245246if (PrintContainerInfo) {247tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",248mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);249}250}251252phys_mem = Linux::physical_memory();253if (Verbose) {254tty->print_cr("total system memory: " JLONG_FORMAT, phys_mem);255}256return phys_mem;257}258259////////////////////////////////////////////////////////////////////////////////260// environment support261262bool os::getenv(const char* name, char* buf, int len) {263const char* val = ::getenv(name);264if (val != NULL && strlen(val) < (size_t)len) {265strcpy(buf, val);266return true;267}268if (len > 0) buf[0] = 0; // return a null string269return false;270}271272273// Return true if user is running as root.274275bool os::have_special_privileges() {276static bool init = false;277static bool privileges = false;278if (!init) {279privileges = (getuid() != geteuid()) || (getgid() != getegid());280init = true;281}282return privileges;283}284285286#ifndef SYS_gettid287// i386: 224, ia64: 1105, amd64: 186, sparc 143288#ifdef __ia64__289#define SYS_gettid 1105290#else291#if defined(__i386__) || defined(__arm__)292#define SYS_gettid 224293#else294#ifdef __amd64__295#define SYS_gettid 186296#else297#ifdef __sparc__298#define SYS_gettid 143299#else300#error define gettid for the arch301#endif302#endif303#endif304#endif305#endif306307// Cpu architecture string308static char cpu_arch[] = HOTSPOT_LIB_ARCH;309310// pid_t gettid()311//312// Returns the kernel thread id of the currently running thread. Kernel313// thread id is used to access /proc.314//315// (Note that getpid() on LinuxThreads returns kernel thread id too; but316// on NPTL, it returns the same pid for all threads, as required by POSIX.)317//318pid_t os::Linux::gettid() {319int rslt = syscall(SYS_gettid);320if (rslt == -1) {321// old kernel, no NPTL support322return getpid();323} else {324return (pid_t)rslt;325}326}327328// Most versions of linux have a bug where the number of processors are329// determined by looking at the /proc file system. In a chroot environment,330// the system call returns 1. This causes the VM to act as if it is331// a single processor and elide locking (see is_MP() call).332static bool unsafe_chroot_detected = false;333static const char *unstable_chroot_error = "/proc file system not found.\n"334"Java may be unstable running multithreaded in a chroot "335"environment on Linux when /proc filesystem is not mounted.";336337void os::Linux::initialize_system_info() {338set_processor_count(sysconf(_SC_NPROCESSORS_CONF));339if (processor_count() == 1) {340pid_t pid = os::Linux::gettid();341char fname[32];342jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);343FILE *fp = fopen(fname, "r");344if (fp == NULL) {345unsafe_chroot_detected = true;346} else {347fclose(fp);348}349}350_physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);351assert(processor_count() > 0, "linux error");352}353354void os::init_system_properties_values() {355// The next steps are taken in the product version:356//357// Obtain the JAVA_HOME value from the location of libjvm.so.358// This library should be located at:359// <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm.so.360//361// If "/jre/lib/" appears at the right place in the path, then we362// assume libjvm.so is installed in a JDK and we use this path.363//364// Otherwise exit with message: "Could not create the Java virtual machine."365//366// The following extra steps are taken in the debugging version:367//368// If "/jre/lib/" does NOT appear at the right place in the path369// instead of exit check for $JAVA_HOME environment variable.370//371// If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,372// then we append a fake suffix "hotspot/libjvm.so" to this path so373// it looks like libjvm.so is installed there374// <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm.so.375//376// Otherwise exit.377//378// Important note: if the location of libjvm.so changes this379// code needs to be changed accordingly.380381// See ld(1):382// The linker uses the following search paths to locate required383// shared libraries:384// 1: ...385// ...386// 7: The default directories, normally /lib and /usr/lib.387#if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))388#define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"389#else390#define DEFAULT_LIBPATH "/lib:/usr/lib"391#endif392393// Base path of extensions installed on the system.394#define SYS_EXT_DIR "/usr/java/packages"395#define EXTENSIONS_DIR "/lib/ext"396#define ENDORSED_DIR "/lib/endorsed"397398// Buffer that fits several sprintfs.399// Note that the space for the colon and the trailing null are provided400// by the nulls included by the sizeof operator.401const size_t bufsize =402MAX3((size_t)MAXPATHLEN, // For dll_dir & friends.403(size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR), // extensions dir404(size_t)MAXPATHLEN + sizeof(ENDORSED_DIR)); // endorsed dir405char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);406407// sysclasspath, java_home, dll_dir408{409char *pslash;410os::jvm_path(buf, bufsize);411412// Found the full path to libjvm.so.413// Now cut the path to <java_home>/jre if we can.414*(strrchr(buf, '/')) = '\0'; // Get rid of /libjvm.so.415pslash = strrchr(buf, '/');416if (pslash != NULL) {417*pslash = '\0'; // Get rid of /{client|server|hotspot}.418}419Arguments::set_dll_dir(buf);420421if (pslash != NULL) {422pslash = strrchr(buf, '/');423if (pslash != NULL) {424*pslash = '\0'; // Get rid of /<arch>.425pslash = strrchr(buf, '/');426if (pslash != NULL) {427*pslash = '\0'; // Get rid of /lib.428}429}430}431Arguments::set_java_home(buf);432set_boot_path('/', ':');433}434435// Where to look for native libraries.436//437// Note: Due to a legacy implementation, most of the library path438// is set in the launcher. This was to accomodate linking restrictions439// on legacy Linux implementations (which are no longer supported).440// Eventually, all the library path setting will be done here.441//442// However, to prevent the proliferation of improperly built native443// libraries, the new path component /usr/java/packages is added here.444// Eventually, all the library path setting will be done here.445{446// Get the user setting of LD_LIBRARY_PATH, and prepended it. It447// should always exist (until the legacy problem cited above is448// addressed).449const char *v = ::getenv("LD_LIBRARY_PATH");450const char *v_colon = ":";451if (v == NULL) { v = ""; v_colon = ""; }452// That's +1 for the colon and +1 for the trailing '\0'.453char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,454strlen(v) + 1 +455sizeof(SYS_EXT_DIR) + sizeof("/lib/") + strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH) + 1,456mtInternal);457sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib/%s:" DEFAULT_LIBPATH, v, v_colon, cpu_arch);458Arguments::set_library_path(ld_library_path);459FREE_C_HEAP_ARRAY(char, ld_library_path, mtInternal);460}461462// Extensions directories.463sprintf(buf, "%s" EXTENSIONS_DIR ":" SYS_EXT_DIR EXTENSIONS_DIR, Arguments::get_java_home());464Arguments::set_ext_dirs(buf);465466// Endorsed standards default directory.467sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());468Arguments::set_endorsed_dirs(buf);469470FREE_C_HEAP_ARRAY(char, buf, mtInternal);471472#undef DEFAULT_LIBPATH473#undef SYS_EXT_DIR474#undef EXTENSIONS_DIR475#undef ENDORSED_DIR476}477478////////////////////////////////////////////////////////////////////////////////479// breakpoint support480481void os::breakpoint() {482BREAKPOINT;483}484485extern "C" void breakpoint() {486// use debugger to set breakpoint here487}488489////////////////////////////////////////////////////////////////////////////////490// signal support491492debug_only(static bool signal_sets_initialized = false);493static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;494495bool os::Linux::is_sig_ignored(int sig) {496struct sigaction oact;497sigaction(sig, (struct sigaction*)NULL, &oact);498void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction)499: CAST_FROM_FN_PTR(void*, oact.sa_handler);500if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN))501return true;502else503return false;504}505506void os::Linux::signal_sets_init() {507// Should also have an assertion stating we are still single-threaded.508assert(!signal_sets_initialized, "Already initialized");509// Fill in signals that are necessarily unblocked for all threads in510// the VM. Currently, we unblock the following signals:511// SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden512// by -Xrs (=ReduceSignalUsage));513// BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all514// other threads. The "ReduceSignalUsage" boolean tells us not to alter515// the dispositions or masks wrt these signals.516// Programs embedding the VM that want to use the above signals for their517// own purposes must, at this time, use the "-Xrs" option to prevent518// interference with shutdown hooks and BREAK_SIGNAL thread dumping.519// (See bug 4345157, and other related bugs).520// In reality, though, unblocking these signals is really a nop, since521// these signals are not blocked by default.522sigemptyset(&unblocked_sigs);523sigemptyset(&allowdebug_blocked_sigs);524sigaddset(&unblocked_sigs, SIGILL);525sigaddset(&unblocked_sigs, SIGSEGV);526sigaddset(&unblocked_sigs, SIGBUS);527sigaddset(&unblocked_sigs, SIGFPE);528#if defined(PPC64)529sigaddset(&unblocked_sigs, SIGTRAP);530#endif531sigaddset(&unblocked_sigs, SR_signum);532533if (!ReduceSignalUsage) {534if (!os::Linux::is_sig_ignored(SHUTDOWN1_SIGNAL)) {535sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);536sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);537}538if (!os::Linux::is_sig_ignored(SHUTDOWN2_SIGNAL)) {539sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);540sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);541}542if (!os::Linux::is_sig_ignored(SHUTDOWN3_SIGNAL)) {543sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);544sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);545}546}547// Fill in signals that are blocked by all but the VM thread.548sigemptyset(&vm_sigs);549if (!ReduceSignalUsage)550sigaddset(&vm_sigs, BREAK_SIGNAL);551debug_only(signal_sets_initialized = true);552553}554555// These are signals that are unblocked while a thread is running Java.556// (For some reason, they get blocked by default.)557sigset_t* os::Linux::unblocked_signals() {558assert(signal_sets_initialized, "Not initialized");559return &unblocked_sigs;560}561562// These are the signals that are blocked while a (non-VM) thread is563// running Java. Only the VM thread handles these signals.564sigset_t* os::Linux::vm_signals() {565assert(signal_sets_initialized, "Not initialized");566return &vm_sigs;567}568569// These are signals that are blocked during cond_wait to allow debugger in570sigset_t* os::Linux::allowdebug_blocked_signals() {571assert(signal_sets_initialized, "Not initialized");572return &allowdebug_blocked_sigs;573}574575void os::Linux::hotspot_sigmask(Thread* thread) {576577//Save caller's signal mask before setting VM signal mask578sigset_t caller_sigmask;579pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask);580581OSThread* osthread = thread->osthread();582osthread->set_caller_sigmask(caller_sigmask);583584pthread_sigmask(SIG_UNBLOCK, os::Linux::unblocked_signals(), NULL);585586if (!ReduceSignalUsage) {587if (thread->is_VM_thread()) {588// Only the VM thread handles BREAK_SIGNAL ...589pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);590} else {591// ... all other threads block BREAK_SIGNAL592pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);593}594}595}596597//////////////////////////////////////////////////////////////////////////////598// detecting pthread library599600void os::Linux::libpthread_init() {601// Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION602// and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a603// generic name for earlier versions.604// Define macros here so we can build HotSpot on old systems.605# ifndef _CS_GNU_LIBC_VERSION606# define _CS_GNU_LIBC_VERSION 2607# endif608# ifndef _CS_GNU_LIBPTHREAD_VERSION609# define _CS_GNU_LIBPTHREAD_VERSION 3610# endif611612#ifdef __ANDROID__613os::Linux::set_glibc_version("android bionic libc api-21");614os::Linux::set_libpthread_version("android bionic libc api-21 NPTL");615#else616size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);617if (n > 0) {618char *str = (char *)malloc(n, mtInternal);619confstr(_CS_GNU_LIBC_VERSION, str, n);620os::Linux::set_glibc_version(str);621} else {622#ifndef __UCLIBC__623// _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()624static char _gnu_libc_version[32];625jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),626"glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());627os::Linux::set_glibc_version(_gnu_libc_version);628#else629#define STRFY(s) #s630os::Linux::set_glibc_version("uclibc " STRFY(__UCLIB_MAJOR__) "." STRFY(__UCLIBC_MINOR__) " stable");631#undef STRFY632#endif633}634635n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);636if (n > 0) {637char *str = (char *)malloc(n, mtInternal);638confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);639// Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells640// us "NPTL-0.29" even we are running with LinuxThreads. Check if this641// is the case. LinuxThreads has a hard limit on max number of threads.642// So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.643// On the other hand, NPTL does not have such a limit, sysconf()644// will return -1 and errno is not changed. Check if it is really NPTL.645if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&646strstr(str, "NPTL") &&647sysconf(_SC_THREAD_THREADS_MAX) > 0) {648free(str);649os::Linux::set_libpthread_version("linuxthreads");650} else {651os::Linux::set_libpthread_version(str);652}653} else {654// glibc before 2.3.2 only has LinuxThreads.655os::Linux::set_libpthread_version("linuxthreads");656}657#endif // !__ANDROID__658659if (strstr(libpthread_version(), "NPTL")) {660os::Linux::set_is_NPTL();661} else {662os::Linux::set_is_LinuxThreads();663}664665// LinuxThreads have two flavors: floating-stack mode, which allows variable666// stack size; and fixed-stack mode. NPTL is always floating-stack.667if (os::Linux::is_NPTL() || os::Linux::supports_variable_stack_size()) {668os::Linux::set_is_floating_stack();669}670}671672/////////////////////////////////////////////////////////////////////////////673// thread stack674675// Force Linux kernel to expand current thread stack. If "bottom" is close676// to the stack guard, caller should block all signals.677//678// MAP_GROWSDOWN:679// A special mmap() flag that is used to implement thread stacks. It tells680// kernel that the memory region should extend downwards when needed. This681// allows early versions of LinuxThreads to only mmap the first few pages682// when creating a new thread. Linux kernel will automatically expand thread683// stack as needed (on page faults).684//685// However, because the memory region of a MAP_GROWSDOWN stack can grow on686// demand, if a page fault happens outside an already mapped MAP_GROWSDOWN687// region, it's hard to tell if the fault is due to a legitimate stack688// access or because of reading/writing non-exist memory (e.g. buffer689// overrun). As a rule, if the fault happens below current stack pointer,690// Linux kernel does not expand stack, instead a SIGSEGV is sent to the691// application (see Linux kernel fault.c).692//693// This Linux feature can cause SIGSEGV when VM bangs thread stack for694// stack overflow detection.695//696// Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do697// not use this flag. However, the stack of initial thread is not created698// by pthread, it is still MAP_GROWSDOWN. Also it's possible (though699// unlikely) that user code can create a thread with MAP_GROWSDOWN stack700// and then attach the thread to JVM.701//702// To get around the problem and allow stack banging on Linux, we need to703// manually expand thread stack after receiving the SIGSEGV.704//705// There are two ways to expand thread stack to address "bottom", we used706// both of them in JVM before 1.5:707// 1. adjust stack pointer first so that it is below "bottom", and then708// touch "bottom"709// 2. mmap() the page in question710//711// Now alternate signal stack is gone, it's harder to use 2. For instance,712// if current sp is already near the lower end of page 101, and we need to713// call mmap() to map page 100, it is possible that part of the mmap() frame714// will be placed in page 100. When page 100 is mapped, it is zero-filled.715// That will destroy the mmap() frame and cause VM to crash.716//717// The following code works by adjusting sp first, then accessing the "bottom"718// page to force a page fault. Linux kernel will then automatically expand the719// stack mapping.720//721// _expand_stack_to() assumes its frame size is less than page size, which722// should always be true if the function is not inlined.723724#if __GNUC__ < 3 // gcc 2.x does not support noinline attribute725#define NOINLINE726#else727#define NOINLINE __attribute__ ((noinline))728#endif729730static void _expand_stack_to(address bottom) NOINLINE;731732static void _expand_stack_to(address bottom) {733address sp;734size_t size;735volatile char *p;736737// Adjust bottom to point to the largest address within the same page, it738// gives us a one-page buffer if alloca() allocates slightly more memory.739bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size());740bottom += os::Linux::page_size() - 1;741742// sp might be slightly above current stack pointer; if that's the case, we743// will alloca() a little more space than necessary, which is OK. Don't use744// os::current_stack_pointer(), as its result can be slightly below current745// stack pointer, causing us to not alloca enough to reach "bottom".746sp = (address)&sp;747748if (sp > bottom) {749size = sp - bottom;750p = (volatile char *)alloca(size);751assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");752p[0] = '\0';753}754}755756void os::Linux::expand_stack_to(address bottom) {757_expand_stack_to(bottom);758}759760bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {761assert(t!=NULL, "just checking");762assert(t->osthread()->expanding_stack(), "expand should be set");763assert(t->stack_base() != NULL, "stack_base was not initialized");764765if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {766sigset_t mask_all, old_sigset;767sigfillset(&mask_all);768pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);769_expand_stack_to(addr);770pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);771return true;772}773return false;774}775776//////////////////////////////////////////////////////////////////////////////777// create new thread778779static address highest_vm_reserved_address();780781// check if it's safe to start a new thread782static bool _thread_safety_check(Thread* thread) {783if (os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack()) {784// Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat)785// Heap is mmap'ed at lower end of memory space. Thread stacks are786// allocated (MAP_FIXED) from high address space. Every thread stack787// occupies a fixed size slot (usually 2Mbytes, but user can change788// it to other values if they rebuild LinuxThreads).789//790// Problem with MAP_FIXED is that mmap() can still succeed even part of791// the memory region has already been mmap'ed. That means if we have too792// many threads and/or very large heap, eventually thread stack will793// collide with heap.794//795// Here we try to prevent heap/stack collision by comparing current796// stack bottom with the highest address that has been mmap'ed by JVM797// plus a safety margin for memory maps created by native code.798//799// This feature can be disabled by setting ThreadSafetyMargin to 0800//801if (ThreadSafetyMargin > 0) {802address stack_bottom = os::current_stack_base() - os::current_stack_size();803804// not safe if our stack extends below the safety margin805return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();806} else {807return true;808}809} else {810// Floating stack LinuxThreads or NPTL:811// Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When812// there's not enough space left, pthread_create() will fail. If we come813// here, that means enough space has been reserved for stack.814return true;815}816}817818// Thread start routine for all newly created threads819static void *java_start(Thread *thread) {820// Try to randomize the cache line index of hot stack frames.821// This helps when threads of the same stack traces evict each other's822// cache lines. The threads can be either from the same JVM instance, or823// from different JVM instances. The benefit is especially true for824// processors with hyperthreading technology.825static int counter = 0;826int pid = os::current_process_id();827alloca(((pid ^ counter++) & 7) * 128);828829ThreadLocalStorage::set_thread(thread);830831OSThread* osthread = thread->osthread();832Monitor* sync = osthread->startThread_lock();833834// non floating stack LinuxThreads needs extra check, see above835if (!_thread_safety_check(thread)) {836// notify parent thread837MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);838osthread->set_state(ZOMBIE);839sync->notify_all();840return NULL;841}842843// thread_id is kernel thread id (similar to Solaris LWP id)844osthread->set_thread_id(os::Linux::gettid());845846if (UseNUMA) {847int lgrp_id = os::numa_get_group_id();848if (lgrp_id != -1) {849thread->set_lgrp_id(lgrp_id);850}851}852// initialize signal mask for this thread853os::Linux::hotspot_sigmask(thread);854855// initialize floating point control register856os::Linux::init_thread_fpu_state();857858// handshaking with parent thread859{860MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);861862// notify parent thread863osthread->set_state(INITIALIZED);864sync->notify_all();865866// wait until os::start_thread()867while (osthread->get_state() == INITIALIZED) {868sync->wait(Mutex::_no_safepoint_check_flag);869}870}871872// call one more level start routine873thread->run();874875return 0;876}877878bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {879assert(thread->osthread() == NULL, "caller responsible");880881// Allocate the OSThread object882OSThread* osthread = new OSThread(NULL, NULL);883if (osthread == NULL) {884return false;885}886887// set the correct thread state888osthread->set_thread_type(thr_type);889890// Initial state is ALLOCATED but not INITIALIZED891osthread->set_state(ALLOCATED);892893thread->set_osthread(osthread);894895// init thread attributes896pthread_attr_t attr;897pthread_attr_init(&attr);898pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);899900// stack size901if (os::Linux::supports_variable_stack_size()) {902// calculate stack size if it's not specified by caller903if (stack_size == 0) {904stack_size = os::Linux::default_stack_size(thr_type);905906switch (thr_type) {907case os::java_thread:908// Java threads use ThreadStackSize which default value can be909// changed with the flag -Xss910assert (JavaThread::stack_size_at_create() > 0, "this should be set");911stack_size = JavaThread::stack_size_at_create();912break;913case os::compiler_thread:914if (CompilerThreadStackSize > 0) {915stack_size = (size_t)(CompilerThreadStackSize * K);916break;917} // else fall through:918// use VMThreadStackSize if CompilerThreadStackSize is not defined919case os::vm_thread:920case os::pgc_thread:921case os::cgc_thread:922case os::watcher_thread:923if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);924break;925}926}927928stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);929pthread_attr_setstacksize(&attr, stack_size);930} else {931// let pthread_create() pick the default value.932}933934// glibc guard page935pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));936937ThreadState state;938939{940// Serialize thread creation if we are running with fixed stack LinuxThreads941bool lock = os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack();942if (lock) {943os::Linux::createThread_lock()->lock_without_safepoint_check();944}945946pthread_t tid;947int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);948949pthread_attr_destroy(&attr);950951if (ret != 0) {952if (PrintMiscellaneous && (Verbose || WizardMode)) {953perror("pthread_create()");954}955// Need to clean up stuff we've allocated so far956thread->set_osthread(NULL);957delete osthread;958if (lock) os::Linux::createThread_lock()->unlock();959return false;960}961962// Store pthread info into the OSThread963osthread->set_pthread_id(tid);964965// Wait until child thread is either initialized or aborted966{967Monitor* sync_with_child = osthread->startThread_lock();968MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);969while ((state = osthread->get_state()) == ALLOCATED) {970sync_with_child->wait(Mutex::_no_safepoint_check_flag);971}972}973974if (lock) {975os::Linux::createThread_lock()->unlock();976}977}978979// Aborted due to thread limit being reached980if (state == ZOMBIE) {981thread->set_osthread(NULL);982delete osthread;983return false;984}985986// The thread is returned suspended (in state INITIALIZED),987// and is started higher up in the call chain988assert(state == INITIALIZED, "race condition");989return true;990}991992/////////////////////////////////////////////////////////////////////////////993// attach existing thread994995// bootstrap the main thread996bool os::create_main_thread(JavaThread* thread) {997assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread");998return create_attached_thread(thread);999}10001001bool os::create_attached_thread(JavaThread* thread) {1002#ifdef ASSERT1003thread->verify_not_published();1004#endif10051006// Allocate the OSThread object1007OSThread* osthread = new OSThread(NULL, NULL);10081009if (osthread == NULL) {1010return false;1011}10121013// Store pthread info into the OSThread1014osthread->set_thread_id(os::Linux::gettid());1015osthread->set_pthread_id(::pthread_self());10161017// initialize floating point control register1018os::Linux::init_thread_fpu_state();10191020// Initial thread state is RUNNABLE1021osthread->set_state(RUNNABLE);10221023thread->set_osthread(osthread);10241025if (UseNUMA) {1026int lgrp_id = os::numa_get_group_id();1027if (lgrp_id != -1) {1028thread->set_lgrp_id(lgrp_id);1029}1030}10311032if (os::is_primordial_thread()) {1033// If current thread is primordial thread, its stack is mapped on demand,1034// see notes about MAP_GROWSDOWN. Here we try to force kernel to map1035// the entire stack region to avoid SEGV in stack banging.1036// It is also useful to get around the heap-stack-gap problem on SuSE1037// kernel (see 4821821 for details). We first expand stack to the top1038// of yellow zone, then enable stack yellow zone (order is significant,1039// enabling yellow zone first will crash JVM on SuSE Linux), so there1040// is no gap between the last two virtual memory regions.10411042JavaThread *jt = (JavaThread *)thread;1043address addr = jt->stack_yellow_zone_base();1044assert(addr != NULL, "initialization problem?");1045assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");10461047osthread->set_expanding_stack();1048os::Linux::manually_expand_stack(jt, addr);1049osthread->clear_expanding_stack();1050}10511052// initialize signal mask for this thread1053// and save the caller's signal mask1054os::Linux::hotspot_sigmask(thread);10551056return true;1057}10581059void os::pd_start_thread(Thread* thread) {1060OSThread * osthread = thread->osthread();1061assert(osthread->get_state() != INITIALIZED, "just checking");1062Monitor* sync_with_child = osthread->startThread_lock();1063MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);1064sync_with_child->notify();1065}10661067// Free Linux resources related to the OSThread1068void os::free_thread(OSThread* osthread) {1069assert(osthread != NULL, "osthread not set");10701071if (Thread::current()->osthread() == osthread) {1072// Restore caller's signal mask1073sigset_t sigmask = osthread->caller_sigmask();1074pthread_sigmask(SIG_SETMASK, &sigmask, NULL);1075}10761077delete osthread;1078}10791080//////////////////////////////////////////////////////////////////////////////1081// thread local storage10821083// Restore the thread pointer if the destructor is called. This is in case1084// someone from JNI code sets up a destructor with pthread_key_create to run1085// detachCurrentThread on thread death. Unless we restore the thread pointer we1086// will hang or crash. When detachCurrentThread is called the key will be set1087// to null and we will not be called again. If detachCurrentThread is never1088// called we could loop forever depending on the pthread implementation.1089static void restore_thread_pointer(void* p) {1090Thread* thread = (Thread*) p;1091os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);1092}10931094int os::allocate_thread_local_storage() {1095pthread_key_t key;1096int rslt = pthread_key_create(&key, restore_thread_pointer);1097assert(rslt == 0, "cannot allocate thread local storage");1098return (int)key;1099}11001101// Note: This is currently not used by VM, as we don't destroy TLS key1102// on VM exit.1103void os::free_thread_local_storage(int index) {1104int rslt = pthread_key_delete((pthread_key_t)index);1105assert(rslt == 0, "invalid index");1106}11071108void os::thread_local_storage_at_put(int index, void* value) {1109int rslt = pthread_setspecific((pthread_key_t)index, value);1110assert(rslt == 0, "pthread_setspecific failed");1111}11121113extern "C" Thread* get_thread() {1114return ThreadLocalStorage::thread();1115}11161117//////////////////////////////////////////////////////////////////////////////1118// primordial thread11191120// Check if current thread is the primordial thread, similar to Solaris thr_main.1121bool os::is_primordial_thread(void) {1122char dummy;1123// If called before init complete, thread stack bottom will be null.1124// Can be called if fatal error occurs before initialization.1125if (os::Linux::initial_thread_stack_bottom() == NULL) return false;1126assert(os::Linux::initial_thread_stack_bottom() != NULL &&1127os::Linux::initial_thread_stack_size() != 0,1128"os::init did not locate primordial thread's stack region");1129if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&1130(address)&dummy < os::Linux::initial_thread_stack_bottom() +1131os::Linux::initial_thread_stack_size()) {1132return true;1133} else {1134return false;1135}1136}11371138// Find the virtual memory area that contains addr1139static bool find_vma(address addr, address* vma_low, address* vma_high) {1140FILE *fp = fopen("/proc/self/maps", "r");1141if (fp) {1142address low, high;1143while (!feof(fp)) {1144if (fscanf(fp, "%p-%p", &low, &high) == 2) {1145if (low <= addr && addr < high) {1146if (vma_low) *vma_low = low;1147if (vma_high) *vma_high = high;1148fclose (fp);1149return true;1150}1151}1152for (;;) {1153int ch = fgetc(fp);1154if (ch == EOF || ch == (int)'\n') break;1155}1156}1157fclose(fp);1158}1159return false;1160}11611162// Locate primordial thread stack. This special handling of primordial thread stack1163// is needed because pthread_getattr_np() on most (all?) Linux distros returns1164// bogus value for the primordial process thread. While the launcher has created1165// the VM in a new thread since JDK 6, we still have to allow for the use of the1166// JNI invocation API from a primordial thread.1167void os::Linux::capture_initial_stack(size_t max_size) {11681169// max_size is either 0 (which means accept OS default for thread stacks) or1170// a user-specified value known to be at least the minimum needed. If we1171// are actually on the primordial thread we can make it appear that we have a1172// smaller max_size stack by inserting the guard pages at that location. But we1173// cannot do anything to emulate a larger stack than what has been provided by1174// the OS or threading library. In fact if we try to use a stack greater than1175// what is set by rlimit then we will crash the hosting process.11761177// Maximum stack size is the easy part, get it from RLIMIT_STACK.1178// If this is "unlimited" then it will be a huge value.1179struct rlimit rlim;1180getrlimit(RLIMIT_STACK, &rlim);1181size_t stack_size = rlim.rlim_cur;11821183// 6308388: a bug in ld.so will relocate its own .data section to the1184// lower end of primordial stack; reduce ulimit -s value a little bit1185// so we won't install guard page on ld.so's data section.1186// But ensure we don't underflow the stack size - allow 1 page spare1187if (stack_size >= (size_t)(3 * page_size())) {1188stack_size -= 2 * page_size();1189}11901191// Try to figure out where the stack base (top) is. This is harder.1192//1193// When an application is started, glibc saves the initial stack pointer in1194// a global variable "__libc_stack_end", which is then used by system1195// libraries. __libc_stack_end should be pretty close to stack top. The1196// variable is available since the very early days. However, because it is1197// a private interface, it could disappear in the future.1198//1199// Linux kernel saves start_stack information in /proc/<pid>/stat. Similar1200// to __libc_stack_end, it is very close to stack top, but isn't the real1201// stack top. Note that /proc may not exist if VM is running as a chroot1202// program, so reading /proc/<pid>/stat could fail. Also the contents of1203// /proc/<pid>/stat could change in the future (though unlikely).1204//1205// We try __libc_stack_end first. If that doesn't work, look for1206// /proc/<pid>/stat. If neither of them works, we use current stack pointer1207// as a hint, which should work well in most cases.12081209uintptr_t stack_start;12101211// try __libc_stack_end first1212uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");1213if (p && *p) {1214stack_start = *p;1215} else {1216// see if we can get the start_stack field from /proc/self/stat1217FILE *fp;1218int pid;1219char state;1220int ppid;1221int pgrp;1222int session;1223int nr;1224int tpgrp;1225unsigned long flags;1226unsigned long minflt;1227unsigned long cminflt;1228unsigned long majflt;1229unsigned long cmajflt;1230unsigned long utime;1231unsigned long stime;1232long cutime;1233long cstime;1234long prio;1235long nice;1236long junk;1237long it_real;1238uintptr_t start;1239uintptr_t vsize;1240intptr_t rss;1241uintptr_t rsslim;1242uintptr_t scodes;1243uintptr_t ecode;1244int i;12451246// Figure what the primordial thread stack base is. Code is inspired1247// by email from Hans Boehm. /proc/self/stat begins with current pid,1248// followed by command name surrounded by parentheses, state, etc.1249char stat[2048];1250int statlen;12511252fp = fopen("/proc/self/stat", "r");1253if (fp) {1254statlen = fread(stat, 1, 2047, fp);1255stat[statlen] = '\0';1256fclose(fp);12571258// Skip pid and the command string. Note that we could be dealing with1259// weird command names, e.g. user could decide to rename java launcher1260// to "java 1.4.2 :)", then the stat file would look like1261// 1234 (java 1.4.2 :)) R ... ...1262// We don't really need to know the command string, just find the last1263// occurrence of ")" and then start parsing from there. See bug 4726580.1264char * s = strrchr(stat, ')');12651266i = 0;1267if (s) {1268// Skip blank chars1269do s++; while (isspace(*s));12701271#define _UFM UINTX_FORMAT1272#define _DFM INTX_FORMAT12731274/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */1275/* 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 */1276i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " _UFM _UFM _DFM _UFM _UFM _UFM _UFM,1277&state, /* 3 %c */1278&ppid, /* 4 %d */1279&pgrp, /* 5 %d */1280&session, /* 6 %d */1281&nr, /* 7 %d */1282&tpgrp, /* 8 %d */1283&flags, /* 9 %lu */1284&minflt, /* 10 %lu */1285&cminflt, /* 11 %lu */1286&majflt, /* 12 %lu */1287&cmajflt, /* 13 %lu */1288&utime, /* 14 %lu */1289&stime, /* 15 %lu */1290&cutime, /* 16 %ld */1291&cstime, /* 17 %ld */1292&prio, /* 18 %ld */1293&nice, /* 19 %ld */1294&junk, /* 20 %ld */1295&it_real, /* 21 %ld */1296&start, /* 22 UINTX_FORMAT */1297&vsize, /* 23 UINTX_FORMAT */1298&rss, /* 24 INTX_FORMAT */1299&rsslim, /* 25 UINTX_FORMAT */1300&scodes, /* 26 UINTX_FORMAT */1301&ecode, /* 27 UINTX_FORMAT */1302&stack_start); /* 28 UINTX_FORMAT */1303}13041305#undef _UFM1306#undef _DFM13071308if (i != 28 - 2) {1309assert(false, "Bad conversion from /proc/self/stat");1310// product mode - assume we are the primordial thread, good luck in the1311// embedded case.1312warning("Can't detect primordial thread stack location - bad conversion");1313stack_start = (uintptr_t) &rlim;1314}1315} else {1316// For some reason we can't open /proc/self/stat (for example, running on1317// FreeBSD with a Linux emulator, or inside chroot), this should work for1318// most cases, so don't abort:1319warning("Can't detect primordial thread stack location - no /proc/self/stat");1320stack_start = (uintptr_t) &rlim;1321}1322}13231324// Now we have a pointer (stack_start) very close to the stack top, the1325// next thing to do is to figure out the exact location of stack top. We1326// can find out the virtual memory area that contains stack_start by1327// reading /proc/self/maps, it should be the last vma in /proc/self/maps,1328// and its upper limit is the real stack top. (again, this would fail if1329// running inside chroot, because /proc may not exist.)13301331uintptr_t stack_top;1332address low, high;1333if (find_vma((address)stack_start, &low, &high)) {1334// success, "high" is the true stack top. (ignore "low", because initial1335// thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)1336stack_top = (uintptr_t)high;1337} else {1338// failed, likely because /proc/self/maps does not exist1339warning("Can't detect primordial thread stack location - find_vma failed");1340// best effort: stack_start is normally within a few pages below the real1341// stack top, use it as stack top, and reduce stack size so we won't put1342// guard page outside stack.1343stack_top = stack_start;1344stack_size -= 16 * page_size();1345}13461347// stack_top could be partially down the page so align it1348stack_top = align_size_up(stack_top, page_size());13491350// Allowed stack value is minimum of max_size and what we derived from rlimit1351if (max_size > 0) {1352_initial_thread_stack_size = MIN2(max_size, stack_size);1353} else {1354// Accept the rlimit max, but if stack is unlimited then it will be huge, so1355// clamp it at 8MB as we do on Solaris1356_initial_thread_stack_size = MIN2(stack_size, 8*M);1357}13581359_initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());1360_initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;1361assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!");1362}13631364////////////////////////////////////////////////////////////////////////////////1365// time support13661367// Time since start-up in seconds to a fine granularity.1368// Used by VMSelfDestructTimer and the MemProfiler.1369double os::elapsedTime() {13701371return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution1372}13731374jlong os::elapsed_counter() {1375return javaTimeNanos() - initial_time_count;1376}13771378jlong os::elapsed_frequency() {1379return NANOSECS_PER_SEC; // nanosecond resolution1380}13811382bool os::supports_vtime() { return true; }1383bool os::enable_vtime() { return false; }1384bool os::vtime_enabled() { return false; }13851386double os::elapsedVTime() {1387struct rusage usage;1388int retval = getrusage(RUSAGE_THREAD, &usage);1389if (retval == 0) {1390return (double) (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) + (double) (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000 * 1000);1391} else {1392// better than nothing, but not much1393return elapsedTime();1394}1395}13961397jlong os::javaTimeMillis() {1398timeval time;1399int status = gettimeofday(&time, NULL);1400assert(status != -1, "linux error");1401return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);1402}14031404#ifndef CLOCK_MONOTONIC1405#define CLOCK_MONOTONIC (1)1406#endif14071408void os::Linux::clock_init() {1409// we do dlopen's in this particular order due to bug in linux1410// dynamical loader (see 6348968) leading to crash on exit1411void* handle = dlopen("librt.so.1", RTLD_LAZY);1412if (handle == NULL) {1413handle = dlopen("librt.so", RTLD_LAZY);1414}1415#ifdef __ANDROID__1416if (handle == NULL) {1417// libc has clock_getres and clock_gettime1418handle = RTLD_DEFAULT;1419}1420#endif14211422if (handle) {1423int (*clock_getres_func)(clockid_t, struct timespec*) =1424(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");1425int (*clock_gettime_func)(clockid_t, struct timespec*) =1426(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");1427if (clock_getres_func && clock_gettime_func) {1428// See if monotonic clock is supported by the kernel. Note that some1429// early implementations simply return kernel jiffies (updated every1430// 1/100 or 1/1000 second). It would be bad to use such a low res clock1431// for nano time (though the monotonic property is still nice to have).1432// It's fixed in newer kernels, however clock_getres() still returns1433// 1/HZ. We check if clock_getres() works, but will ignore its reported1434// resolution for now. Hopefully as people move to new kernels, this1435// won't be a problem.1436struct timespec res;1437struct timespec tp;1438if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&1439clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {1440// yes, monotonic clock is supported1441_clock_gettime = clock_gettime_func;1442return;1443} else {1444// close librt if there is no monotonic clock1445#ifndef __ANDROID__ // we should not close RTLD_DEFAULT :)1446dlclose(handle);1447#endif1448}1449}1450}1451warning("No monotonic clock was available - timed services may " \1452"be adversely affected if the time-of-day clock changes");1453}14541455#ifndef SYS_clock_getres14561457#if defined(IA32) || defined(AMD64) || defined(AARCH64)1458#define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229) AARCH64_ONLY(114)1459#define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)1460#else1461#warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"1462#define sys_clock_getres(x,y) -11463#endif14641465#else1466#define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)1467#endif14681469void os::Linux::fast_thread_clock_init() {1470if (!UseLinuxPosixThreadCPUClocks) {1471return;1472}1473clockid_t clockid;1474struct timespec tp;1475int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =1476(int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");14771478// Switch to using fast clocks for thread cpu time if1479// the sys_clock_getres() returns 0 error code.1480// Note, that some kernels may support the current thread1481// clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks1482// returned by the pthread_getcpuclockid().1483// If the fast Posix clocks are supported then the sys_clock_getres()1484// must return at least tp.tv_sec == 0 which means a resolution1485// better than 1 sec. This is extra check for reliability.14861487if(pthread_getcpuclockid_func &&1488pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&1489sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {14901491_supports_fast_thread_cpu_time = true;1492_pthread_getcpuclockid = pthread_getcpuclockid_func;1493}1494}14951496jlong os::javaTimeNanos() {1497if (Linux::supports_monotonic_clock()) {1498struct timespec tp;1499int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);1500assert(status == 0, "gettime error");1501jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);1502return result;1503} else {1504timeval time;1505int status = gettimeofday(&time, NULL);1506assert(status != -1, "linux error");1507jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);1508return 1000 * usecs;1509}1510}15111512void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {1513if (Linux::supports_monotonic_clock()) {1514info_ptr->max_value = ALL_64_BITS;15151516// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past1517info_ptr->may_skip_backward = false; // not subject to resetting or drifting1518info_ptr->may_skip_forward = false; // not subject to resetting or drifting1519} else {1520// gettimeofday - based on time in seconds since the Epoch thus does not wrap1521info_ptr->max_value = ALL_64_BITS;15221523// gettimeofday is a real time clock so it skips1524info_ptr->may_skip_backward = true;1525info_ptr->may_skip_forward = true;1526}15271528info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time1529}15301531// Return the real, user, and system times in seconds from an1532// arbitrary fixed point in the past.1533bool os::getTimesSecs(double* process_real_time,1534double* process_user_time,1535double* process_system_time) {1536struct tms ticks;1537clock_t real_ticks = times(&ticks);15381539if (real_ticks == (clock_t) (-1)) {1540return false;1541} else {1542double ticks_per_second = (double) clock_tics_per_sec;1543*process_user_time = ((double) ticks.tms_utime) / ticks_per_second;1544*process_system_time = ((double) ticks.tms_stime) / ticks_per_second;1545*process_real_time = ((double) real_ticks) / ticks_per_second;15461547return true;1548}1549}155015511552char * os::local_time_string(char *buf, size_t buflen) {1553struct tm t;1554time_t long_time;1555time(&long_time);1556localtime_r(&long_time, &t);1557jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",1558t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,1559t.tm_hour, t.tm_min, t.tm_sec);1560return buf;1561}15621563struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {1564return localtime_r(clock, res);1565}15661567////////////////////////////////////////////////////////////////////////////////1568// runtime exit support15691570// Note: os::shutdown() might be called very early during initialization, or1571// called from signal handler. Before adding something to os::shutdown(), make1572// sure it is async-safe and can handle partially initialized VM.1573void os::shutdown() {15741575// allow PerfMemory to attempt cleanup of any persistent resources1576perfMemory_exit();15771578// needs to remove object in file system1579AttachListener::abort();15801581// flush buffered output, finish log files1582ostream_abort();15831584// Check for abort hook1585abort_hook_t abort_hook = Arguments::abort_hook();1586if (abort_hook != NULL) {1587abort_hook();1588}15891590}15911592// Note: os::abort() might be called very early during initialization, or1593// called from signal handler. Before adding something to os::abort(), make1594// sure it is async-safe and can handle partially initialized VM.1595void os::abort(bool dump_core) {1596os::shutdown();1597if (dump_core) {1598#ifndef PRODUCT1599fdStream out(defaultStream::output_fd());1600out.print_raw("Current thread is ");1601char buf[16];1602jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());1603out.print_raw_cr(buf);1604out.print_raw_cr("Dumping core ...");1605#endif1606::abort(); // dump core1607}16081609::exit(1);1610}16111612// Die immediately, no exit hook, no abort hook, no cleanup.1613void os::die() {1614// _exit() on LinuxThreads only kills current thread1615::abort();1616}161716181619// This method is a copy of JDK's sysGetLastErrorString1620// from src/solaris/hpi/src/system_md.c16211622size_t os::lasterror(char *buf, size_t len) {16231624if (errno == 0) return 0;16251626const char *s = ::strerror(errno);1627size_t n = ::strlen(s);1628if (n >= len) {1629n = len - 1;1630}1631::strncpy(buf, s, n);1632buf[n] = '\0';1633return n;1634}16351636intx os::current_thread_id() { return (intx)pthread_self(); }1637int os::current_process_id() {16381639// Under the old linux thread library, linux gives each thread1640// its own process id. Because of this each thread will return1641// a different pid if this method were to return the result1642// of getpid(2). Linux provides no api that returns the pid1643// of the launcher thread for the vm. This implementation1644// returns a unique pid, the pid of the launcher thread1645// that starts the vm 'process'.16461647// Under the NPTL, getpid() returns the same pid as the1648// launcher thread rather than a unique pid per thread.1649// Use gettid() if you want the old pre NPTL behaviour.16501651// if you are looking for the result of a call to getpid() that1652// returns a unique pid for the calling thread, then look at the1653// OSThread::thread_id() method in osThread_linux.hpp file16541655return (int)(_initial_pid ? _initial_pid : getpid());1656}16571658// DLL functions16591660const char* os::dll_file_extension() { return ".so"; }16611662// This must be hard coded because it's the system's temporary1663// directory not the java application's temp directory, ala java.io.tmpdir.1664const char* os::get_temp_directory() { return "/tmp"; }16651666static bool file_exists(const char* filename) {1667struct stat statbuf;1668if (filename == NULL || strlen(filename) == 0) {1669return false;1670}1671return os::stat(filename, &statbuf) == 0;1672}16731674bool os::dll_build_name(char* buffer, size_t buflen,1675const char* pname, const char* fname) {1676bool retval = false;1677// Copied from libhpi1678const size_t pnamelen = pname ? strlen(pname) : 0;16791680// Return error on buffer overflow.1681if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {1682return retval;1683}16841685if (pnamelen == 0) {1686snprintf(buffer, buflen, "lib%s.so", fname);1687retval = true;1688} else if (strchr(pname, *os::path_separator()) != NULL) {1689int n;1690char** pelements = split_path(pname, &n);1691if (pelements == NULL) {1692return false;1693}1694for (int i = 0 ; i < n ; i++) {1695// Really shouldn't be NULL, but check can't hurt1696if (pelements[i] == NULL || strlen(pelements[i]) == 0) {1697continue; // skip the empty path values1698}1699snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);1700if (file_exists(buffer)) {1701retval = true;1702break;1703}1704}1705// release the storage1706for (int i = 0 ; i < n ; i++) {1707if (pelements[i] != NULL) {1708FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);1709}1710}1711if (pelements != NULL) {1712FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);1713}1714} else {1715snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);1716retval = true;1717}1718return retval;1719}17201721// check if addr is inside libjvm.so1722bool os::address_is_in_vm(address addr) {1723static address libjvm_base_addr;1724Dl_info dlinfo;17251726if (libjvm_base_addr == NULL) {1727if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {1728libjvm_base_addr = (address)dlinfo.dli_fbase;1729}1730assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");1731}17321733if (dladdr((void *)addr, &dlinfo) != 0) {1734if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;1735}17361737return false;1738}17391740bool os::dll_address_to_function_name(address addr, char *buf,1741int buflen, int *offset) {1742// buf is not optional, but offset is optional1743assert(buf != NULL, "sanity check");17441745Dl_info dlinfo;17461747if (dladdr((void*)addr, &dlinfo) != 0) {1748// see if we have a matching symbol1749if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {1750if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {1751jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);1752}1753if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;1754return true;1755}1756// no matching symbol so try for just file info1757if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {1758if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),1759buf, buflen, offset, dlinfo.dli_fname)) {1760return true;1761}1762}1763}17641765buf[0] = '\0';1766if (offset != NULL) *offset = -1;1767return false;1768}17691770struct _address_to_library_name {1771address addr; // input : memory address1772size_t buflen; // size of fname1773char* fname; // output: library name1774address base; // library base addr1775};17761777static int address_to_library_name_callback(struct dl_phdr_info *info,1778size_t size, void *data) {1779int i;1780bool found = false;1781address libbase = NULL;1782struct _address_to_library_name * d = (struct _address_to_library_name *)data;17831784// iterate through all loadable segments1785for (i = 0; i < info->dlpi_phnum; i++) {1786address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);1787if (info->dlpi_phdr[i].p_type == PT_LOAD) {1788// base address of a library is the lowest address of its loaded1789// segments.1790if (libbase == NULL || libbase > segbase) {1791libbase = segbase;1792}1793// see if 'addr' is within current segment1794if (segbase <= d->addr &&1795d->addr < segbase + info->dlpi_phdr[i].p_memsz) {1796found = true;1797}1798}1799}18001801// dlpi_name is NULL or empty if the ELF file is executable, return 01802// so dll_address_to_library_name() can fall through to use dladdr() which1803// can figure out executable name from argv[0].1804if (found && info->dlpi_name && info->dlpi_name[0]) {1805d->base = libbase;1806if (d->fname) {1807jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);1808}1809return 1;1810}1811return 0;1812}1813bool os::dll_address_to_library_name(address addr, char* buf,1814int buflen, int* offset) {1815// buf is not optional, but offset is optional1816assert(buf != NULL, "sanity check");18171818Dl_info dlinfo;18191820// Android bionic libc does not have the bug below (?)1821#ifndef __ANDROID__1822struct _address_to_library_name data;18231824// There is a bug in old glibc dladdr() implementation that it could resolve1825// to wrong library name if the .so file has a base address != NULL. Here1826// we iterate through the program headers of all loaded libraries to find1827// out which library 'addr' really belongs to. This workaround can be1828// removed once the minimum requirement for glibc is moved to 2.3.x.1829data.addr = addr;1830data.fname = buf;1831data.buflen = buflen;1832data.base = NULL;1833int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);18341835if (rslt) {1836// buf already contains library name1837if (offset) *offset = addr - data.base;1838return true;1839}1840#endif // !__ANDROID__1841if (dladdr((void*)addr, &dlinfo) != 0) {1842if (dlinfo.dli_fname != NULL) {1843jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);1844}1845if (dlinfo.dli_fbase != NULL && offset != NULL) {1846*offset = addr - (address)dlinfo.dli_fbase;1847}1848return true;1849}18501851buf[0] = '\0';1852if (offset) *offset = -1;1853return false;1854}18551856static bool read_so_path_from_maps(const char* so_name, char* buf, int buflen) {1857FILE *fp = fopen("/proc/self/maps", "r");1858assert(fp, "Failed to open /proc/self/maps");1859if (!fp) {1860return false;1861}18621863char maps_buffer[2048];1864while (fgets(maps_buffer, 2048, fp) != NULL) {1865if (strstr(maps_buffer, so_name) == NULL) {1866continue;1867}18681869char *so_path = strchr(maps_buffer, '/');1870so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n1871jio_snprintf(buf, buflen, "%s", so_path);1872fclose(fp);1873return true;1874}18751876fclose(fp);1877return false;1878}18791880// Loads .dll/.so and1881// in case of error it checks if .dll/.so was built for the1882// same architecture as Hotspot is running on188318841885// Remember the stack's state. The Linux dynamic linker will change1886// the stack to 'executable' at most once, so we must safepoint only once.1887bool os::Linux::_stack_is_executable = false;18881889// VM operation that loads a library. This is necessary if stack protection1890// of the Java stacks can be lost during loading the library. If we1891// do not stop the Java threads, they can stack overflow before the stacks1892// are protected again.1893class VM_LinuxDllLoad: public VM_Operation {1894private:1895const char *_filename;1896char *_ebuf;1897int _ebuflen;1898void *_lib;1899public:1900VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) :1901_filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL) {}1902VMOp_Type type() const { return VMOp_LinuxDllLoad; }1903void doit() {1904_lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen);1905os::Linux::_stack_is_executable = true;1906}1907void* loaded_library() { return _lib; }1908};19091910void * os::dll_load(const char *filename, char *ebuf, int ebuflen)1911{1912void * result = NULL;1913bool load_attempted = false;19141915// Check whether the library to load might change execution rights1916// of the stack. If they are changed, the protection of the stack1917// guard pages will be lost. We need a safepoint to fix this.1918//1919// See Linux man page execstack(8) for more info.1920if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) {1921ElfFile ef(filename);1922if (!ef.specifies_noexecstack()) {1923if (!is_init_completed()) {1924os::Linux::_stack_is_executable = true;1925// This is OK - No Java threads have been created yet, and hence no1926// stack guard pages to fix.1927//1928// This should happen only when you are building JDK7 using a very1929// old version of JDK6 (e.g., with JPRT) and running test_gamma.1930//1931// Dynamic loader will make all stacks executable after1932// this function returns, and will not do that again.1933assert(Threads::first() == NULL, "no Java threads should exist yet.");1934} else {1935warning("You have loaded library %s which might have disabled stack guard. "1936"The VM will try to fix the stack guard now.\n"1937"It's highly recommended that you fix the library with "1938"'execstack -c <libfile>', or link it with '-z noexecstack'.",1939filename);19401941assert(Thread::current()->is_Java_thread(), "must be Java thread");1942JavaThread *jt = JavaThread::current();1943if (jt->thread_state() != _thread_in_native) {1944// This happens when a compiler thread tries to load a hsdis-<arch>.so file1945// that requires ExecStack. Cannot enter safe point. Let's give up.1946warning("Unable to fix stack guard. Giving up.");1947} else {1948if (!LoadExecStackDllInVMThread) {1949// This is for the case where the DLL has an static1950// constructor function that executes JNI code. We cannot1951// load such DLLs in the VMThread.1952result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);1953}19541955ThreadInVMfromNative tiv(jt);1956debug_only(VMNativeEntryWrapper vew;)19571958VM_LinuxDllLoad op(filename, ebuf, ebuflen);1959VMThread::execute(&op);1960if (LoadExecStackDllInVMThread) {1961result = op.loaded_library();1962}1963load_attempted = true;1964}1965}1966}1967}19681969if (!load_attempted) {1970result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);1971}19721973if (result != NULL) {1974// Successful loading1975return result;1976}19771978Elf32_Ehdr elf_head;1979int diag_msg_max_length=ebuflen-strlen(ebuf);1980char* diag_msg_buf=ebuf+strlen(ebuf);19811982if (diag_msg_max_length==0) {1983// No more space in ebuf for additional diagnostics message1984return NULL;1985}198619871988int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);19891990if (file_descriptor < 0) {1991// Can't open library, report dlerror() message1992return NULL;1993}19941995bool failed_to_read_elf_head=1996(sizeof(elf_head)!=1997(::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;19981999::close(file_descriptor);2000if (failed_to_read_elf_head) {2001// file i/o error - report dlerror() msg2002return NULL;2003}20042005typedef struct {2006Elf32_Half code; // Actual value as defined in elf.h2007Elf32_Half compat_class; // Compatibility of archs at VM's sense2008char elf_class; // 32 or 64 bit2009char endianess; // MSB or LSB2010char* name; // String representation2011} arch_t;20122013#ifndef EM_4862014#define EM_486 6 /* Intel 80486 */2015#endif2016#ifndef EM_AARCH642017#define EM_AARCH64 183 /* ARM AARCH64 */2018#endif20192020static const arch_t arch_array[]={2021{EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},2022{EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},2023{EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},2024{EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},2025{EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},2026{EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},2027{EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},2028{EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},2029#if defined(VM_LITTLE_ENDIAN)2030{EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64 LE"},2031#else2032{EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},2033#endif2034{EM_ARM, EM_ARM, ELFCLASS32, ELFDATA2LSB, (char*)"ARM"},2035{EM_S390, EM_S390, ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},2036{EM_ALPHA, EM_ALPHA, ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},2037{EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},2038{EM_MIPS, EM_MIPS, ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},2039{EM_PARISC, EM_PARISC, ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},2040{EM_68K, EM_68K, ELFCLASS32, ELFDATA2MSB, (char*)"M68k"},2041{EM_AARCH64, EM_AARCH64, ELFCLASS64, ELFDATA2LSB, (char*)"AARCH64"},2042};20432044#if (defined IA32)2045static Elf32_Half running_arch_code=EM_386;2046#elif (defined AMD64)2047static Elf32_Half running_arch_code=EM_X86_64;2048#elif (defined IA64)2049static Elf32_Half running_arch_code=EM_IA_64;2050#elif (defined __sparc) && (defined _LP64)2051static Elf32_Half running_arch_code=EM_SPARCV9;2052#elif (defined __sparc) && (!defined _LP64)2053static Elf32_Half running_arch_code=EM_SPARC;2054#elif (defined __powerpc64__)2055static Elf32_Half running_arch_code=EM_PPC64;2056#elif (defined __powerpc__)2057static Elf32_Half running_arch_code=EM_PPC;2058#elif (defined ARM)2059static Elf32_Half running_arch_code=EM_ARM;2060#elif (defined S390)2061static Elf32_Half running_arch_code=EM_S390;2062#elif (defined ALPHA)2063static Elf32_Half running_arch_code=EM_ALPHA;2064#elif (defined MIPSEL)2065static Elf32_Half running_arch_code=EM_MIPS_RS3_LE;2066#elif (defined PARISC)2067static Elf32_Half running_arch_code=EM_PARISC;2068#elif (defined MIPS)2069static Elf32_Half running_arch_code=EM_MIPS;2070#elif (defined M68K)2071static Elf32_Half running_arch_code=EM_68K;2072#elif (defined AARCH64)2073static Elf32_Half running_arch_code=EM_AARCH64;2074#else2075#error Method os::dll_load requires that one of following is defined:\2076IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K, AARCH642077#endif20782079// Identify compatability class for VM's architecture and library's architecture2080// Obtain string descriptions for architectures20812082arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};2083int running_arch_index=-1;20842085for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {2086if (running_arch_code == arch_array[i].code) {2087running_arch_index = i;2088}2089if (lib_arch.code == arch_array[i].code) {2090lib_arch.compat_class = arch_array[i].compat_class;2091lib_arch.name = arch_array[i].name;2092}2093}20942095assert(running_arch_index != -1,2096"Didn't find running architecture code (running_arch_code) in arch_array");2097if (running_arch_index == -1) {2098// Even though running architecture detection failed2099// we may still continue with reporting dlerror() message2100return NULL;2101}21022103if (lib_arch.endianess != arch_array[running_arch_index].endianess) {2104::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");2105return NULL;2106}21072108#ifndef S3902109if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {2110::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");2111return NULL;2112}2113#endif // !S39021142115if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {2116if ( lib_arch.name!=NULL ) {2117::snprintf(diag_msg_buf, diag_msg_max_length-1,2118" (Possible cause: can't load %s-bit .so on a %s-bit platform)",2119lib_arch.name, arch_array[running_arch_index].name);2120} else {2121::snprintf(diag_msg_buf, diag_msg_max_length-1,2122" (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",2123lib_arch.code,2124arch_array[running_arch_index].name);2125}2126}21272128return NULL;2129}21302131void * os::Linux::dlopen_helper(const char *filename, char *ebuf, int ebuflen) {2132void * result = ::dlopen(filename, RTLD_LAZY);2133if (result == NULL) {2134::strncpy(ebuf, ::dlerror(), ebuflen - 1);2135ebuf[ebuflen-1] = '\0';2136}2137return result;2138}21392140void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf, int ebuflen) {2141void * result = NULL;2142if (LoadExecStackDllInVMThread) {2143result = dlopen_helper(filename, ebuf, ebuflen);2144}21452146// Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a2147// library that requires an executable stack, or which does not have this2148// stack attribute set, dlopen changes the stack attribute to executable. The2149// read protection of the guard pages gets lost.2150//2151// Need to check _stack_is_executable again as multiple VM_LinuxDllLoad2152// may have been queued at the same time.21532154if (!_stack_is_executable) {2155JavaThread *jt = Threads::first();21562157while (jt) {2158if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized2159jt->stack_yellow_zone_enabled()) { // No pending stack overflow exceptions2160if (!os::guard_memory((char *) jt->stack_red_zone_base() - jt->stack_red_zone_size(),2161jt->stack_yellow_zone_size() + jt->stack_red_zone_size())) {2162warning("Attempt to reguard stack yellow zone failed.");2163}2164}2165jt = jt->next();2166}2167}21682169return result;2170}21712172/*2173* glibc-2.0 libdl is not MT safe. If you are building with any glibc,2174* chances are you might want to run the generated bits against glibc-2.02175* libdl.so, so always use locking for any version of glibc.2176*/2177void* os::dll_lookup(void* handle, const char* name) {2178pthread_mutex_lock(&dl_mutex);2179void* res = dlsym(handle, name);2180pthread_mutex_unlock(&dl_mutex);2181return res;2182}21832184void* os::get_default_process_handle() {2185return (void*)::dlopen(NULL, RTLD_LAZY);2186}21872188static bool _print_ascii_file(const char* filename, outputStream* st) {2189int fd = ::open(filename, O_RDONLY);2190if (fd == -1) {2191return false;2192}21932194char buf[32];2195int bytes;2196while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {2197st->print_raw(buf, bytes);2198}21992200::close(fd);22012202return true;2203}22042205void os::print_dll_info(outputStream *st) {2206st->print_cr("Dynamic libraries:");22072208char fname[32];2209pid_t pid = os::Linux::gettid();22102211jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);22122213if (!_print_ascii_file(fname, st)) {2214st->print("Can not get library information for pid = %d\n", pid);2215}2216}22172218int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {2219FILE *procmapsFile = NULL;22202221// Open the procfs maps file for the current process2222if ((procmapsFile = fopen("/proc/self/maps", "r")) != NULL) {2223// Allocate PATH_MAX for file name plus a reasonable size for other fields.2224char line[PATH_MAX + 100];22252226// Read line by line from 'file'2227while (fgets(line, sizeof(line), procmapsFile) != NULL) {2228u8 base, top, offset, inode;2229char permissions[5];2230char device[6];2231char name[PATH_MAX + 1];22322233// Parse fields from line2234sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %4s " UINT64_FORMAT_X " %7s " INT64_FORMAT " %s",2235&base, &top, permissions, &offset, device, &inode, name);22362237// Filter by device id '00:00' so that we only get file system mapped files.2238if (strcmp(device, "00:00") != 0) {22392240// Call callback with the fields of interest2241if(callback(name, (address)base, (address)top, param)) {2242// Oops abort, callback aborted2243fclose(procmapsFile);2244return 1;2245}2246}2247}2248fclose(procmapsFile);2249}2250return 0;2251}22522253void os::print_os_info_brief(outputStream* st) {2254os::Linux::print_distro_info(st);22552256os::Posix::print_uname_info(st);22572258os::Linux::print_libversion_info(st);22592260}22612262void os::print_os_info(outputStream* st) {2263st->print("OS:");22642265os::Linux::print_distro_info(st);22662267os::Posix::print_uname_info(st);22682269// Print warning if unsafe chroot environment detected2270if (unsafe_chroot_detected) {2271st->print("WARNING!! ");2272st->print_cr("%s", unstable_chroot_error);2273}22742275os::Linux::print_libversion_info(st);22762277os::Posix::print_rlimit_info(st);22782279os::Posix::print_load_average(st);22802281os::Linux::print_full_memory_info(st);22822283os::Linux::print_container_info(st);2284}22852286// Try to identify popular distros.2287// Most Linux distributions have a /etc/XXX-release file, which contains2288// the OS version string. Newer Linux distributions have a /etc/lsb-release2289// file that also contains the OS version string. Some have more than one2290// /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and2291// /etc/redhat-release.), so the order is important.2292// Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have2293// their own specific XXX-release file as well as a redhat-release file.2294// Because of this the XXX-release file needs to be searched for before the2295// redhat-release file.2296// Since Red Hat has a lsb-release file that is not very descriptive the2297// search for redhat-release needs to be before lsb-release.2298// Since the lsb-release file is the new standard it needs to be searched2299// before the older style release files.2300// Searching system-release (Red Hat) and os-release (other Linuxes) are a2301// next to last resort. The os-release file is a new standard that contains2302// distribution information and the system-release file seems to be an old2303// standard that has been replaced by the lsb-release and os-release files.2304// Searching for the debian_version file is the last resort. It contains2305// an informative string like "6.0.6" or "wheezy/sid". Because of this2306// "Debian " is printed before the contents of the debian_version file.2307void os::Linux::print_distro_info(outputStream* st) {2308if (!_print_ascii_file("/etc/oracle-release", st) &&2309!_print_ascii_file("/etc/mandriva-release", st) &&2310!_print_ascii_file("/etc/mandrake-release", st) &&2311!_print_ascii_file("/etc/sun-release", st) &&2312!_print_ascii_file("/etc/redhat-release", st) &&2313!_print_ascii_file("/etc/lsb-release", st) &&2314!_print_ascii_file("/etc/SuSE-release", st) &&2315!_print_ascii_file("/etc/turbolinux-release", st) &&2316!_print_ascii_file("/etc/gentoo-release", st) &&2317!_print_ascii_file("/etc/ltib-release", st) &&2318!_print_ascii_file("/etc/angstrom-version", st) &&2319!_print_ascii_file("/etc/system-release", st) &&2320!_print_ascii_file("/etc/os-release", st)) {23212322if (file_exists("/etc/debian_version")) {2323st->print("Debian ");2324_print_ascii_file("/etc/debian_version", st);2325} else {2326st->print("Linux");2327}2328}2329st->cr();2330}23312332void os::Linux::print_libversion_info(outputStream* st) {2333// libc, pthread2334st->print("libc:");2335st->print("%s ", os::Linux::glibc_version());2336st->print("%s ", os::Linux::libpthread_version());2337if (os::Linux::is_LinuxThreads()) {2338st->print("(%s stack)", os::Linux::is_floating_stack() ? "floating" : "fixed");2339}2340st->cr();2341}23422343void os::Linux::print_full_memory_info(outputStream* st) {2344st->print("\n/proc/meminfo:\n");2345_print_ascii_file("/proc/meminfo", st);2346st->cr();2347}23482349void os::Linux::print_container_info(outputStream* st) {2350if (!OSContainer::is_containerized()) {2351return;2352}23532354st->print("container (cgroup) information:\n");23552356const char *p_ct = OSContainer::container_type();2357st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");23582359char *p = OSContainer::cpu_cpuset_cpus();2360st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");2361free(p);23622363p = OSContainer::cpu_cpuset_memory_nodes();2364st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");2365free(p);23662367int i = OSContainer::active_processor_count();2368if (i > 0) {2369st->print("active_processor_count: %d\n", i);2370} else {2371st->print("active_processor_count: failed\n");2372}23732374i = OSContainer::cpu_quota();2375st->print("cpu_quota: %d\n", i);23762377i = OSContainer::cpu_period();2378st->print("cpu_period: %d\n", i);23792380i = OSContainer::cpu_shares();2381st->print("cpu_shares: %d\n", i);23822383jlong j = OSContainer::memory_limit_in_bytes();2384st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);23852386j = OSContainer::memory_and_swap_limit_in_bytes();2387st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);23882389j = OSContainer::memory_soft_limit_in_bytes();2390st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);23912392j = OSContainer::OSContainer::memory_usage_in_bytes();2393st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);23942395j = OSContainer::OSContainer::memory_max_usage_in_bytes();2396st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);2397st->cr();2398}23992400void os::print_memory_info(outputStream* st) {24012402st->print("Memory:");2403st->print(" %dk page", os::vm_page_size()>>10);24042405// values in struct sysinfo are "unsigned long"2406struct sysinfo si;2407sysinfo(&si);24082409st->print(", physical " UINT64_FORMAT "k",2410os::physical_memory() >> 10);2411st->print("(" UINT64_FORMAT "k free)",2412os::available_memory() >> 10);2413st->print(", swap " UINT64_FORMAT "k",2414((jlong)si.totalswap * si.mem_unit) >> 10);2415st->print("(" UINT64_FORMAT "k free)",2416((jlong)si.freeswap * si.mem_unit) >> 10);2417st->cr();2418}24192420void os::pd_print_cpu_info(outputStream* st) {2421st->print("\n/proc/cpuinfo:\n");2422if (!_print_ascii_file("/proc/cpuinfo", st)) {2423st->print(" <Not Available>");2424}2425st->cr();2426}24272428void os::print_siginfo(outputStream* st, void* siginfo) {2429const siginfo_t* si = (const siginfo_t*)siginfo;24302431os::Posix::print_siginfo_brief(st, si);2432#if INCLUDE_CDS2433if (si && (si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&2434UseSharedSpaces) {2435FileMapInfo* mapinfo = FileMapInfo::current_info();2436if (mapinfo->is_in_shared_space(si->si_addr)) {2437st->print("\n\nError accessing class data sharing archive." \2438" Mapped file inaccessible during execution, " \2439" possible disk/network problem.");2440}2441}2442#endif2443st->cr();2444}244524462447static void print_signal_handler(outputStream* st, int sig,2448char* buf, size_t buflen);24492450void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {2451st->print_cr("Signal Handlers:");2452print_signal_handler(st, SIGSEGV, buf, buflen);2453print_signal_handler(st, SIGBUS , buf, buflen);2454print_signal_handler(st, SIGFPE , buf, buflen);2455print_signal_handler(st, SIGPIPE, buf, buflen);2456print_signal_handler(st, SIGXFSZ, buf, buflen);2457print_signal_handler(st, SIGILL , buf, buflen);2458print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);2459print_signal_handler(st, SR_signum, buf, buflen);2460print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen);2461print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);2462print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen);2463print_signal_handler(st, BREAK_SIGNAL, buf, buflen);2464#if defined(PPC64)2465print_signal_handler(st, SIGTRAP, buf, buflen);2466#endif2467}24682469static char saved_jvm_path[MAXPATHLEN] = {0};24702471// Find the full path to the current module, libjvm.so2472void os::jvm_path(char *buf, jint buflen) {2473// Error checking.2474if (buflen < MAXPATHLEN) {2475assert(false, "must use a large-enough buffer");2476buf[0] = '\0';2477return;2478}2479// Lazy resolve the path to current module.2480if (saved_jvm_path[0] != 0) {2481strcpy(buf, saved_jvm_path);2482return;2483}24842485char dli_fname[MAXPATHLEN];2486bool ret = dll_address_to_library_name(2487CAST_FROM_FN_PTR(address, os::jvm_path),2488dli_fname, sizeof(dli_fname), NULL);2489assert(ret, "cannot locate libjvm");2490#ifdef __ANDROID__2491if (dli_fname[0] == '\0') {2492return;2493}2494if (strchr(dli_fname, '/') == NULL) {2495bool ok = read_so_path_from_maps(dli_fname, buf, buflen);2496assert(ok, "unable to turn relative libjvm.so path into absolute");2497return;2498}2499snprintf(buf, buflen, /* "%s/lib/%s/server/%s", java_home_var, cpu_arch, */ "%s", dli_fname);2500#else // !__ANDROID__2501char *rp = NULL;2502if (ret && dli_fname[0] != '\0') {2503rp = realpath(dli_fname, buf);2504}2505if (rp == NULL)2506return;25072508if (Arguments::created_by_gamma_launcher()) {2509// Support for the gamma launcher. Typical value for buf is2510// "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so". If "/jre/lib/" appears at2511// the right place in the string, then assume we are installed in a JDK and2512// we're done. Otherwise, check for a JAVA_HOME environment variable and fix2513// up the path so it looks like libjvm.so is installed there (append a2514// fake suffix hotspot/libjvm.so).2515const char *p = buf + strlen(buf) - 1;2516for (int count = 0; p > buf && count < 5; ++count) {2517for (--p; p > buf && *p != '/'; --p)2518/* empty */ ;2519}25202521if (strncmp(p, "/jre/lib/", 9) != 0) {2522// Look for JAVA_HOME in the environment.2523char* java_home_var = ::getenv("JAVA_HOME");2524if (java_home_var != NULL && java_home_var[0] != 0) {2525char* jrelib_p;2526int len;25272528// Check the current module name "libjvm.so".2529p = strrchr(buf, '/');2530assert(strstr(p, "/libjvm") == p, "invalid library name");25312532rp = realpath(java_home_var, buf);2533if (rp == NULL)2534return;25352536// determine if this is a legacy image or modules image2537// modules image doesn't have "jre" subdirectory2538len = strlen(buf);2539assert(len < buflen, "Ran out of buffer room");2540jrelib_p = buf + len;2541snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);2542if (0 != access(buf, F_OK)) {2543snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);2544}25452546if (0 == access(buf, F_OK)) {2547// Use current module name "libjvm.so"2548len = strlen(buf);2549snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");2550} else {2551// Go back to path of .so2552rp = realpath(dli_fname, buf);2553if (rp == NULL)2554return;2555}2556}2557}2558}2559#endif // !__ANDROID__25602561strncpy(saved_jvm_path, buf, MAXPATHLEN);2562}25632564void os::print_jni_name_prefix_on(outputStream* st, int args_size) {2565// no prefix required, not even "_"2566}25672568void os::print_jni_name_suffix_on(outputStream* st, int args_size) {2569// no suffix required2570}25712572////////////////////////////////////////////////////////////////////////////////2573// sun.misc.Signal support25742575static volatile jint sigint_count = 0;25762577static void2578UserHandler(int sig, void *siginfo, void *context) {2579// 4511530 - sem_post is serialized and handled by the manager thread. When2580// the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We2581// don't want to flood the manager thread with sem_post requests.2582if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)2583return;25842585// Ctrl-C is pressed during error reporting, likely because the error2586// handler fails to abort. Let VM die immediately.2587if (sig == SIGINT && is_error_reported()) {2588os::die();2589}25902591os::signal_notify(sig);2592}25932594void* os::user_handler() {2595return CAST_FROM_FN_PTR(void*, UserHandler);2596}25972598class Semaphore : public StackObj {2599public:2600Semaphore();2601~Semaphore();2602void signal();2603void wait();2604bool trywait();2605bool timedwait(unsigned int sec, int nsec);2606private:2607sem_t _semaphore;2608};26092610Semaphore::Semaphore() {2611sem_init(&_semaphore, 0, 0);2612}26132614Semaphore::~Semaphore() {2615sem_destroy(&_semaphore);2616}26172618void Semaphore::signal() {2619sem_post(&_semaphore);2620}26212622void Semaphore::wait() {2623sem_wait(&_semaphore);2624}26252626bool Semaphore::trywait() {2627return sem_trywait(&_semaphore) == 0;2628}26292630bool Semaphore::timedwait(unsigned int sec, int nsec) {26312632struct timespec ts;2633// Semaphore's are always associated with CLOCK_REALTIME2634os::Linux::clock_gettime(CLOCK_REALTIME, &ts);2635// see unpackTime for discussion on overflow checking2636if (sec >= MAX_SECS) {2637ts.tv_sec += MAX_SECS;2638ts.tv_nsec = 0;2639} else {2640ts.tv_sec += sec;2641ts.tv_nsec += nsec;2642if (ts.tv_nsec >= NANOSECS_PER_SEC) {2643ts.tv_nsec -= NANOSECS_PER_SEC;2644++ts.tv_sec; // note: this must be <= max_secs2645}2646}26472648while (1) {2649int result = sem_timedwait(&_semaphore, &ts);2650if (result == 0) {2651return true;2652} else if (errno == EINTR) {2653continue;2654} else if (errno == ETIMEDOUT) {2655return false;2656} else {2657return false;2658}2659}2660}26612662extern "C" {2663typedef void (*sa_handler_t)(int);2664typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);2665}26662667void* os::signal(int signal_number, void* handler) {2668struct sigaction sigAct, oldSigAct;26692670sigfillset(&(sigAct.sa_mask));2671sigAct.sa_flags = SA_RESTART|SA_SIGINFO;2672sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);26732674if (sigaction(signal_number, &sigAct, &oldSigAct)) {2675// -1 means registration failed2676return (void *)-1;2677}26782679return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);2680}26812682void os::signal_raise(int signal_number) {2683::raise(signal_number);2684}26852686/*2687* The following code is moved from os.cpp for making this2688* code platform specific, which it is by its very nature.2689*/26902691// Will be modified when max signal is changed to be dynamic2692int os::sigexitnum_pd() {2693return NSIG;2694}26952696// a counter for each possible signal value2697static volatile jint pending_signals[NSIG+1] = { 0 };26982699// Linux(POSIX) specific hand shaking semaphore.2700static sem_t sig_sem;2701static Semaphore sr_semaphore;27022703void os::signal_init_pd() {2704// Initialize signal structures2705::memset((void*)pending_signals, 0, sizeof(pending_signals));27062707// Initialize signal semaphore2708::sem_init(&sig_sem, 0, 0);2709}27102711void os::signal_notify(int sig) {2712Atomic::inc(&pending_signals[sig]);2713::sem_post(&sig_sem);2714}27152716static int check_pending_signals(bool wait) {2717Atomic::store(0, &sigint_count);2718for (;;) {2719for (int i = 0; i < NSIG + 1; i++) {2720jint n = pending_signals[i];2721if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {2722return i;2723}2724}2725if (!wait) {2726return -1;2727}2728JavaThread *thread = JavaThread::current();2729ThreadBlockInVM tbivm(thread);27302731bool threadIsSuspended;2732do {2733thread->set_suspend_equivalent();2734// cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()2735::sem_wait(&sig_sem);27362737// were we externally suspended while we were waiting?2738threadIsSuspended = thread->handle_special_suspend_equivalent_condition();2739if (threadIsSuspended) {2740//2741// The semaphore has been incremented, but while we were waiting2742// another thread suspended us. We don't want to continue running2743// while suspended because that would surprise the thread that2744// suspended us.2745//2746::sem_post(&sig_sem);27472748thread->java_suspend_self();2749}2750} while (threadIsSuspended);2751}2752}27532754int os::signal_lookup() {2755return check_pending_signals(false);2756}27572758int os::signal_wait() {2759return check_pending_signals(true);2760}27612762////////////////////////////////////////////////////////////////////////////////2763// Virtual Memory27642765int os::vm_page_size() {2766// Seems redundant as all get out2767assert(os::Linux::page_size() != -1, "must call os::init");2768return os::Linux::page_size();2769}27702771// Solaris allocates memory by pages.2772int os::vm_allocation_granularity() {2773assert(os::Linux::page_size() != -1, "must call os::init");2774return os::Linux::page_size();2775}27762777// Rationale behind this function:2778// current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable2779// mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get2780// samples for JITted code. Here we create private executable mapping over the code cache2781// and then we can use standard (well, almost, as mapping can change) way to provide2782// info for the reporting script by storing timestamp and location of symbol2783void linux_wrap_code(char* base, size_t size) {2784static volatile jint cnt = 0;27852786if (!UseOprofile) {2787return;2788}27892790char buf[PATH_MAX+1];2791int num = Atomic::add(1, &cnt);27922793snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",2794os::get_temp_directory(), os::current_process_id(), num);2795unlink(buf);27962797int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU);27982799if (fd != -1) {2800off_t rv = ::lseek(fd, size-2, SEEK_SET);2801if (rv != (off_t)-1) {2802if (::write(fd, "", 1) == 1) {2803mmap(base, size,2804PROT_READ|PROT_WRITE|PROT_EXEC,2805MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);2806}2807}2808::close(fd);2809unlink(buf);2810}2811}28122813static bool recoverable_mmap_error(int err) {2814// See if the error is one we can let the caller handle. This2815// list of errno values comes from JBS-6843484. I can't find a2816// Linux man page that documents this specific set of errno2817// values so while this list currently matches Solaris, it may2818// change as we gain experience with this failure mode.2819switch (err) {2820case EBADF:2821case EINVAL:2822case ENOTSUP:2823// let the caller deal with these errors2824return true;28252826default:2827// Any remaining errors on this OS can cause our reserved mapping2828// to be lost. That can cause confusion where different data2829// structures think they have the same memory mapped. The worst2830// scenario is if both the VM and a library think they have the2831// same memory mapped.2832return false;2833}2834}28352836static void warn_fail_commit_memory(char* addr, size_t size, bool exec,2837int err) {2838warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT2839", %d) failed; error='%s' (errno=%d)", addr, size, exec,2840strerror(err), err);2841}28422843static void warn_fail_commit_memory(char* addr, size_t size,2844size_t alignment_hint, bool exec,2845int err) {2846warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT2847", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", addr, size,2848alignment_hint, exec, strerror(err), err);2849}28502851// NOTE: Linux kernel does not really reserve the pages for us.2852// All it does is to check if there are enough free pages2853// left at the time of mmap(). This could be a potential2854// problem.2855int os::Linux::commit_memory_impl(char* addr, size_t size, bool exec) {2856int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;2857uintptr_t res = (uintptr_t) ::mmap(addr, size, prot,2858MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);2859if (res != (uintptr_t) MAP_FAILED) {2860if (UseNUMAInterleaving) {2861numa_make_global(addr, size);2862}2863return 0;2864}28652866int err = errno; // save errno from mmap() call above28672868if (!recoverable_mmap_error(err)) {2869warn_fail_commit_memory(addr, size, exec, err);2870vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory.");2871}28722873return err;2874}28752876bool os::pd_commit_memory(char* addr, size_t size, bool exec) {2877return os::Linux::commit_memory_impl(addr, size, exec) == 0;2878}28792880void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,2881const char* mesg) {2882assert(mesg != NULL, "mesg must be specified");2883int err = os::Linux::commit_memory_impl(addr, size, exec);2884if (err != 0) {2885// the caller wants all commit errors to exit with the specified mesg:2886warn_fail_commit_memory(addr, size, exec, err);2887vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);2888}2889}28902891// Define MAP_HUGETLB here so we can build HotSpot on old systems.2892#ifndef MAP_HUGETLB2893#define MAP_HUGETLB 0x400002894#endif28952896// Define MADV_HUGEPAGE here so we can build HotSpot on old systems.2897#ifndef MADV_HUGEPAGE2898#define MADV_HUGEPAGE 142899#endif29002901int os::Linux::commit_memory_impl(char* addr, size_t size,2902size_t alignment_hint, bool exec) {2903int err = os::Linux::commit_memory_impl(addr, size, exec);2904if (err == 0) {2905realign_memory(addr, size, alignment_hint);2906}2907return err;2908}29092910bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,2911bool exec) {2912return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0;2913}29142915void os::pd_commit_memory_or_exit(char* addr, size_t size,2916size_t alignment_hint, bool exec,2917const char* mesg) {2918assert(mesg != NULL, "mesg must be specified");2919int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec);2920if (err != 0) {2921// the caller wants all commit errors to exit with the specified mesg:2922warn_fail_commit_memory(addr, size, alignment_hint, exec, err);2923vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);2924}2925}29262927void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {2928if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) {2929// We don't check the return value: madvise(MADV_HUGEPAGE) may not2930// be supported or the memory may already be backed by huge pages.2931::madvise(addr, bytes, MADV_HUGEPAGE);2932}2933}29342935void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {2936// This method works by doing an mmap over an existing mmaping and effectively discarding2937// the existing pages. However it won't work for SHM-based large pages that cannot be2938// uncommitted at all. We don't do anything in this case to avoid creating a segment with2939// small pages on top of the SHM segment. This method always works for small pages, so we2940// allow that in any case.2941if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) {2942commit_memory(addr, bytes, alignment_hint, !ExecMem);2943}2944}29452946void os::numa_make_global(char *addr, size_t bytes) {2947Linux::numa_interleave_memory(addr, bytes);2948}29492950// Define for numa_set_bind_policy(int). Setting the argument to 0 will set the2951// bind policy to MPOL_PREFERRED for the current thread.2952#define USE_MPOL_PREFERRED 029532954void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {2955// To make NUMA and large pages more robust when both enabled, we need to ease2956// the requirements on where the memory should be allocated. MPOL_BIND is the2957// default policy and it will force memory to be allocated on the specified2958// node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on2959// the specified node, but will not force it. Using this policy will prevent2960// getting SIGBUS when trying to allocate large pages on NUMA nodes with no2961// free large pages.2962Linux::numa_set_bind_policy(USE_MPOL_PREFERRED);2963Linux::numa_tonode_memory(addr, bytes, lgrp_hint);2964}29652966bool os::numa_topology_changed() { return false; }29672968size_t os::numa_get_groups_num() {2969// Return just the number of nodes in which it's possible to allocate memory2970// (in numa terminology, configured nodes).2971return Linux::numa_num_configured_nodes();2972}29732974int os::numa_get_group_id() {2975int cpu_id = Linux::sched_getcpu();2976if (cpu_id != -1) {2977int lgrp_id = Linux::get_node_by_cpu(cpu_id);2978if (lgrp_id != -1) {2979return lgrp_id;2980}2981}2982return 0;2983}29842985int os::Linux::get_existing_num_nodes() {2986size_t node;2987size_t highest_node_number = Linux::numa_max_node();2988int num_nodes = 0;29892990// Get the total number of nodes in the system including nodes without memory.2991for (node = 0; node <= highest_node_number; node++) {2992if (isnode_in_existing_nodes(node)) {2993num_nodes++;2994}2995}2996return num_nodes;2997}29982999size_t os::numa_get_leaf_groups(int *ids, size_t size) {3000size_t highest_node_number = Linux::numa_max_node();3001size_t i = 0;30023003// Map all node ids in which is possible to allocate memory. Also nodes are3004// not always consecutively available, i.e. available from 0 to the highest3005// node number.3006for (size_t node = 0; node <= highest_node_number; node++) {3007if (Linux::isnode_in_configured_nodes(node)) {3008ids[i++] = node;3009}3010}3011return i;3012}30133014bool os::get_page_info(char *start, page_info* info) {3015return false;3016}30173018char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {3019return end;3020}302130223023int os::Linux::sched_getcpu_syscall(void) {3024unsigned int cpu = 0;3025int retval = -1;30263027#if defined(AMD64)3028// Unfortunately we have to bring all these macros here from vsyscall.h3029// to be able to compile on old linuxes.3030# define __NR_vgetcpu 23031# define VSYSCALL_START (-10UL << 20)3032# define VSYSCALL_SIZE 10243033# define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr))3034typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);3035vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu);3036retval = vgetcpu(&cpu, NULL, NULL);3037#elif defined(IA32) || defined(AARCH64)3038# ifndef SYS_getcpu3039# define SYS_getcpu AARCH64_ONLY(168) IA32_ONLY(318)3040# endif3041retval = syscall(SYS_getcpu, &cpu, NULL, NULL);3042#endif30433044return (retval == -1) ? retval : cpu;3045}30463047// Something to do with the numa-aware allocator needs these symbols3048extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }3049extern "C" JNIEXPORT void numa_error(char *where) { }3050extern "C" JNIEXPORT int fork1() { return fork(); }30513052// Handle request to load libnuma symbol version 1.1 (API v1). If it fails3053// load symbol from base version instead.3054void* os::Linux::libnuma_dlsym(void* handle, const char *name) {3055#if !defined(__UCLIBC__) && !defined(__ANDROID__)3056void *f = dlvsym(handle, name, "libnuma_1.1");3057if (f == NULL) {3058f = dlsym(handle, name);3059}3060return f;3061#else3062return dlsym(handle, name);3063#endif3064}30653066// Handle request to load libnuma symbol version 1.2 (API v2) only.3067// Return NULL if the symbol is not defined in this particular version.3068void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {3069#ifndef __ANDROID__3070return dlvsym(handle, name, "libnuma_1.2");3071#else // __ANDROID__3072return NULL;3073#endif // !__ANDROID__3074}30753076bool os::Linux::libnuma_init() {3077// sched_getcpu() should be in libc.3078set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,3079dlsym(RTLD_DEFAULT, "sched_getcpu")));30803081// If it's not, try a direct syscall.3082if (sched_getcpu() == -1)3083set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, (void*)&sched_getcpu_syscall));30843085if (sched_getcpu() != -1) { // Does it work?3086void *handle = dlopen("libnuma.so.1", RTLD_LAZY);3087if (handle != NULL) {3088set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,3089libnuma_dlsym(handle, "numa_node_to_cpus")));3090set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,3091libnuma_dlsym(handle, "numa_max_node")));3092set_numa_num_configured_nodes(CAST_TO_FN_PTR(numa_num_configured_nodes_func_t,3093libnuma_dlsym(handle, "numa_num_configured_nodes")));3094set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,3095libnuma_dlsym(handle, "numa_available")));3096set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,3097libnuma_dlsym(handle, "numa_tonode_memory")));3098set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,3099libnuma_dlsym(handle, "numa_interleave_memory")));3100set_numa_interleave_memory_v2(CAST_TO_FN_PTR(numa_interleave_memory_v2_func_t,3101libnuma_v2_dlsym(handle, "numa_interleave_memory")));3102set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,3103libnuma_dlsym(handle, "numa_set_bind_policy")));3104set_numa_bitmask_isbitset(CAST_TO_FN_PTR(numa_bitmask_isbitset_func_t,3105libnuma_dlsym(handle, "numa_bitmask_isbitset")));3106set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,3107libnuma_dlsym(handle, "numa_distance")));31083109if (numa_available() != -1) {3110set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));3111set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));3112set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));3113// Create an index -> node mapping, since nodes are not always consecutive3114_nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);3115rebuild_nindex_to_node_map();3116// Create a cpu -> node mapping3117_cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);3118rebuild_cpu_to_node_map();3119return true;3120}3121}3122}3123return false;3124}31253126void os::Linux::rebuild_nindex_to_node_map() {3127int highest_node_number = Linux::numa_max_node();31283129nindex_to_node()->clear();3130for (int node = 0; node <= highest_node_number; node++) {3131if (Linux::isnode_in_existing_nodes(node)) {3132nindex_to_node()->append(node);3133}3134}3135}31363137// rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.3138// The table is later used in get_node_by_cpu().3139void os::Linux::rebuild_cpu_to_node_map() {3140const size_t NCPUS = 32768; // Since the buffer size computation is very obscure3141// in libnuma (possible values are starting from 16,3142// and continuing up with every other power of 2, but less3143// than the maximum number of CPUs supported by kernel), and3144// is a subject to change (in libnuma version 2 the requirements3145// are more reasonable) we'll just hardcode the number they use3146// in the library.3147const size_t BitsPerCLong = sizeof(long) * CHAR_BIT;31483149size_t cpu_num = processor_count();3150size_t cpu_map_size = NCPUS / BitsPerCLong;3151size_t cpu_map_valid_size =3152MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);31533154cpu_to_node()->clear();3155cpu_to_node()->at_grow(cpu_num - 1);31563157size_t node_num = get_existing_num_nodes();31583159int distance = 0;3160int closest_distance = INT_MAX;3161int closest_node = 0;3162unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal);3163for (size_t i = 0; i < node_num; i++) {3164// Check if node is configured (not a memory-less node). If it is not, find3165// the closest configured node.3166if (!isnode_in_configured_nodes(nindex_to_node()->at(i))) {3167closest_distance = INT_MAX;3168// Check distance from all remaining nodes in the system. Ignore distance3169// from itself and from another non-configured node.3170for (size_t m = 0; m < node_num; m++) {3171if (m != i && isnode_in_configured_nodes(nindex_to_node()->at(m))) {3172distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));3173// If a closest node is found, update. There is always at least one3174// configured node in the system so there is always at least one node3175// close.3176if (distance != 0 && distance < closest_distance) {3177closest_distance = distance;3178closest_node = nindex_to_node()->at(m);3179}3180}3181}3182} else {3183// Current node is already a configured node.3184closest_node = nindex_to_node()->at(i);3185}31863187// Get cpus from the original node and map them to the closest node. If node3188// is a configured node (not a memory-less node), then original node and3189// closest node are the same.3190if (numa_node_to_cpus(nindex_to_node()->at(i), cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {3191for (size_t j = 0; j < cpu_map_valid_size; j++) {3192if (cpu_map[j] != 0) {3193for (size_t k = 0; k < BitsPerCLong; k++) {3194if (cpu_map[j] & (1UL << k)) {3195cpu_to_node()->at_put(j * BitsPerCLong + k, closest_node);3196}3197}3198}3199}3200}3201}3202FREE_C_HEAP_ARRAY(unsigned long, cpu_map, mtInternal);3203}32043205int os::Linux::get_node_by_cpu(int cpu_id) {3206if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {3207return cpu_to_node()->at(cpu_id);3208}3209return -1;3210}32113212GrowableArray<int>* os::Linux::_cpu_to_node;3213GrowableArray<int>* os::Linux::_nindex_to_node;3214os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;3215os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;3216os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;3217os::Linux::numa_num_configured_nodes_func_t os::Linux::_numa_num_configured_nodes;3218os::Linux::numa_available_func_t os::Linux::_numa_available;3219os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;3220os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;3221os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;3222os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;3223os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;3224os::Linux::numa_distance_func_t os::Linux::_numa_distance;3225unsigned long* os::Linux::_numa_all_nodes;3226struct bitmask* os::Linux::_numa_all_nodes_ptr;3227struct bitmask* os::Linux::_numa_nodes_ptr;32283229bool os::pd_uncommit_memory(char* addr, size_t size) {3230uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,3231MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);3232return res != (uintptr_t) MAP_FAILED;3233}32343235static3236address get_stack_commited_bottom(address bottom, size_t size) {3237address nbot = bottom;3238address ntop = bottom + size;32393240size_t page_sz = os::vm_page_size();3241unsigned pages = size / page_sz;32423243unsigned char vec[1];3244unsigned imin = 1, imax = pages + 1, imid;3245int mincore_return_value = 0;32463247assert(imin <= imax, "Unexpected page size");32483249while (imin < imax) {3250imid = (imax + imin) / 2;3251nbot = ntop - (imid * page_sz);32523253// Use a trick with mincore to check whether the page is mapped or not.3254// mincore sets vec to 1 if page resides in memory and to 0 if page3255// is swapped output but if page we are asking for is unmapped3256// it returns -1,ENOMEM3257mincore_return_value = mincore(nbot, page_sz, vec);32583259if (mincore_return_value == -1) {3260// Page is not mapped go up3261// to find first mapped page3262if (errno != EAGAIN) {3263assert(errno == ENOMEM, "Unexpected mincore errno");3264imax = imid;3265}3266} else {3267// Page is mapped go down3268// to find first not mapped page3269imin = imid + 1;3270}3271}32723273nbot = nbot + page_sz;32743275// Adjust stack bottom one page up if last checked page is not mapped3276if (mincore_return_value == -1) {3277nbot = nbot + page_sz;3278}32793280return nbot;3281}328232833284// Linux uses a growable mapping for the stack, and if the mapping for3285// the stack guard pages is not removed when we detach a thread the3286// stack cannot grow beyond the pages where the stack guard was3287// mapped. If at some point later in the process the stack expands to3288// that point, the Linux kernel cannot expand the stack any further3289// because the guard pages are in the way, and a segfault occurs.3290//3291// However, it's essential not to split the stack region by unmapping3292// a region (leaving a hole) that's already part of the stack mapping,3293// so if the stack mapping has already grown beyond the guard pages at3294// the time we create them, we have to truncate the stack mapping.3295// So, we need to know the extent of the stack mapping when3296// create_stack_guard_pages() is called.32973298// We only need this for stacks that are growable: at the time of3299// writing thread stacks don't use growable mappings (i.e. those3300// creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this3301// only applies to the main thread.33023303// If the (growable) stack mapping already extends beyond the point3304// where we're going to put our guard pages, truncate the mapping at3305// that point by munmap()ping it. This ensures that when we later3306// munmap() the guard pages we don't leave a hole in the stack3307// mapping. This only affects the main/primordial thread33083309bool os::pd_create_stack_guard_pages(char* addr, size_t size) {33103311if (os::is_primordial_thread()) {3312// As we manually grow stack up to bottom inside create_attached_thread(),3313// it's likely that os::Linux::initial_thread_stack_bottom is mapped and3314// we don't need to do anything special.3315// Check it first, before calling heavy function.3316uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();3317unsigned char vec[1];33183319if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {3320// Fallback to slow path on all errors, including EAGAIN3321stack_extent = (uintptr_t) get_stack_commited_bottom(3322os::Linux::initial_thread_stack_bottom(),3323(size_t)addr - stack_extent);3324}33253326if (stack_extent < (uintptr_t)addr) {3327::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));3328}3329}33303331return os::commit_memory(addr, size, !ExecMem);3332}33333334// If this is a growable mapping, remove the guard pages entirely by3335// munmap()ping them. If not, just call uncommit_memory(). This only3336// affects the main/primordial thread, but guard against future OS changes.3337// It's safe to always unmap guard pages for primordial thread because we3338// always place it right after end of the mapped region.33393340bool os::remove_stack_guard_pages(char* addr, size_t size) {3341uintptr_t stack_extent, stack_base;33423343if (os::is_primordial_thread()) {3344return ::munmap(addr, size) == 0;3345}33463347return os::uncommit_memory(addr, size);3348}33493350static address _highest_vm_reserved_address = NULL;33513352// If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory3353// at 'requested_addr'. If there are existing memory mappings at the same3354// location, however, they will be overwritten. If 'fixed' is false,3355// 'requested_addr' is only treated as a hint, the return value may or3356// may not start from the requested address. Unlike Linux mmap(), this3357// function returns NULL to indicate failure.3358static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {3359char * addr;3360int flags;33613362flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;3363if (fixed) {3364assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address");3365flags |= MAP_FIXED;3366}33673368// Map reserved/uncommitted pages PROT_NONE so we fail early if we3369// touch an uncommitted page. Otherwise, the read/write might3370// succeed if we have enough swap space to back the physical page.3371addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,3372flags, -1, 0);33733374if (addr != MAP_FAILED) {3375// anon_mmap() should only get called during VM initialization,3376// don't need lock (actually we can skip locking even it can be called3377// from multiple threads, because _highest_vm_reserved_address is just a3378// hint about the upper limit of non-stack memory regions.)3379if ((address)addr + bytes > _highest_vm_reserved_address) {3380_highest_vm_reserved_address = (address)addr + bytes;3381}3382}33833384return addr == MAP_FAILED ? NULL : addr;3385}33863387// Allocate (using mmap, NO_RESERVE, with small pages) at either a given request address3388// (req_addr != NULL) or with a given alignment.3389// - bytes shall be a multiple of alignment.3390// - req_addr can be NULL. If not NULL, it must be a multiple of alignment.3391// - alignment sets the alignment at which memory shall be allocated.3392// It must be a multiple of allocation granularity.3393// Returns address of memory or NULL. If req_addr was not NULL, will only return3394// req_addr or NULL.3395static char* anon_mmap_aligned(size_t bytes, size_t alignment, char* req_addr) {33963397size_t extra_size = bytes;3398if (req_addr == NULL && alignment > 0) {3399extra_size += alignment;3400}34013402char* start = (char*) ::mmap(req_addr, extra_size, PROT_NONE,3403MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,3404-1, 0);3405if (start == MAP_FAILED) {3406start = NULL;3407} else {3408if (req_addr != NULL) {3409if (start != req_addr) {3410::munmap(start, extra_size);3411start = NULL;3412}3413} else {3414char* const start_aligned = (char*) align_ptr_up(start, alignment);3415char* const end_aligned = start_aligned + bytes;3416char* const end = start + extra_size;3417if (start_aligned > start) {3418::munmap(start, start_aligned - start);3419}3420if (end_aligned < end) {3421::munmap(end_aligned, end - end_aligned);3422}3423start = start_aligned;3424}3425}3426return start;3427}34283429// Don't update _highest_vm_reserved_address, because there might be memory3430// regions above addr + size. If so, releasing a memory region only creates3431// a hole in the address space, it doesn't help prevent heap-stack collision.3432//3433static int anon_munmap(char * addr, size_t size) {3434return ::munmap(addr, size) == 0;3435}34363437char* os::pd_reserve_memory(size_t bytes, char* requested_addr,3438size_t alignment_hint) {3439return anon_mmap(requested_addr, bytes, (requested_addr != NULL));3440}34413442bool os::pd_release_memory(char* addr, size_t size) {3443return anon_munmap(addr, size);3444}34453446static address highest_vm_reserved_address() {3447return _highest_vm_reserved_address;3448}34493450static bool linux_mprotect(char* addr, size_t size, int prot) {3451// Linux wants the mprotect address argument to be page aligned.3452char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size());34533454// According to SUSv3, mprotect() should only be used with mappings3455// established by mmap(), and mmap() always maps whole pages. Unaligned3456// 'addr' likely indicates problem in the VM (e.g. trying to change3457// protection of malloc'ed or statically allocated memory). Check the3458// caller if you hit this assert.3459assert(addr == bottom, "sanity check");34603461size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());3462return ::mprotect(bottom, size, prot) == 0;3463}34643465// Set protections specified3466bool os::protect_memory(char* addr, size_t bytes, ProtType prot,3467bool is_committed) {3468unsigned int p = 0;3469switch (prot) {3470case MEM_PROT_NONE: p = PROT_NONE; break;3471case MEM_PROT_READ: p = PROT_READ; break;3472case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break;3473case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break;3474default:3475ShouldNotReachHere();3476}3477// is_committed is unused.3478return linux_mprotect(addr, bytes, p);3479}34803481bool os::guard_memory(char* addr, size_t size) {3482return linux_mprotect(addr, size, PROT_NONE);3483}34843485bool os::unguard_memory(char* addr, size_t size) {3486return linux_mprotect(addr, size, PROT_READ|PROT_WRITE);3487}34883489bool os::Linux::transparent_huge_pages_sanity_check(bool warn, size_t page_size) {3490bool result = false;3491void *p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,3492MAP_ANONYMOUS|MAP_PRIVATE,3493-1, 0);3494if (p != MAP_FAILED) {3495void *aligned_p = align_ptr_up(p, page_size);34963497result = madvise(aligned_p, page_size, MADV_HUGEPAGE) == 0;34983499munmap(p, page_size * 2);3500}35013502if (warn && !result) {3503warning("TransparentHugePages is not supported by the operating system.");3504}35053506return result;3507}35083509bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) {3510bool result = false;3511void *p = mmap(NULL, page_size, PROT_READ|PROT_WRITE,3512MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB,3513-1, 0);35143515if (p != MAP_FAILED) {3516// We don't know if this really is a huge page or not.3517FILE *fp = fopen("/proc/self/maps", "r");3518if (fp) {3519while (!feof(fp)) {3520char chars[257];3521long x = 0;3522if (fgets(chars, sizeof(chars), fp)) {3523if (sscanf(chars, "%lx-%*x", &x) == 13524&& x == (long)p) {3525if (strstr (chars, "hugepage")) {3526result = true;3527break;3528}3529}3530}3531}3532fclose(fp);3533}3534munmap(p, page_size);3535}35363537if (warn && !result) {3538warning("HugeTLBFS is not supported by the operating system.");3539}35403541return result;3542}35433544/*3545* Set the coredump_filter bits to include largepages in core dump (bit 6)3546*3547* From the coredump_filter documentation:3548*3549* - (bit 0) anonymous private memory3550* - (bit 1) anonymous shared memory3551* - (bit 2) file-backed private memory3552* - (bit 3) file-backed shared memory3553* - (bit 4) ELF header pages in file-backed private memory areas (it is3554* effective only if the bit 2 is cleared)3555* - (bit 5) hugetlb private memory3556* - (bit 6) hugetlb shared memory3557*/3558static void set_coredump_filter(void) {3559FILE *f;3560long cdm;35613562if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) {3563return;3564}35653566if (fscanf(f, "%lx", &cdm) != 1) {3567fclose(f);3568return;3569}35703571rewind(f);35723573if ((cdm & LARGEPAGES_BIT) == 0) {3574cdm |= LARGEPAGES_BIT;3575fprintf(f, "%#lx", cdm);3576}35773578fclose(f);3579}35803581// Large page support35823583static size_t _large_page_size = 0;35843585size_t os::Linux::find_large_page_size() {3586size_t large_page_size = 0;35873588// large_page_size on Linux is used to round up heap size. x86 uses either3589// 2M or 4M page, depending on whether PAE (Physical Address Extensions)3590// mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use3591// page as large as 256M.3592//3593// Here we try to figure out page size by parsing /proc/meminfo and looking3594// for a line with the following format:3595// Hugepagesize: 2048 kB3596//3597// If we can't determine the value (e.g. /proc is not mounted, or the text3598// format has been changed), we'll use the largest page size supported by3599// the processor.36003601#ifndef ZERO3602large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)3603ARM_ONLY(2 * M) PPC_ONLY(4 * M) AARCH64_ONLY(2 * M);3604#endif // ZERO36053606FILE *fp = fopen("/proc/meminfo", "r");3607if (fp) {3608while (!feof(fp)) {3609int x = 0;3610char buf[16];3611if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {3612if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {3613large_page_size = x * K;3614break;3615}3616} else {3617// skip to next line3618for (;;) {3619int ch = fgetc(fp);3620if (ch == EOF || ch == (int)'\n') break;3621}3622}3623}3624fclose(fp);3625}36263627if (!FLAG_IS_DEFAULT(LargePageSizeInBytes) && LargePageSizeInBytes != large_page_size) {3628warning("Setting LargePageSizeInBytes has no effect on this OS. Large page size is "3629SIZE_FORMAT "%s.", byte_size_in_proper_unit(large_page_size),3630proper_unit_for_byte_size(large_page_size));3631}36323633return large_page_size;3634}36353636size_t os::Linux::setup_large_page_size() {3637_large_page_size = Linux::find_large_page_size();3638const size_t default_page_size = (size_t)Linux::page_size();3639if (_large_page_size > default_page_size) {3640_page_sizes[0] = _large_page_size;3641_page_sizes[1] = default_page_size;3642_page_sizes[2] = 0;3643}36443645return _large_page_size;3646}36473648bool os::Linux::setup_large_page_type(size_t page_size) {3649if (FLAG_IS_DEFAULT(UseHugeTLBFS) &&3650FLAG_IS_DEFAULT(UseSHM) &&3651FLAG_IS_DEFAULT(UseTransparentHugePages)) {36523653// The type of large pages has not been specified by the user.36543655// Try UseHugeTLBFS and then UseSHM.3656UseHugeTLBFS = UseSHM = true;36573658// Don't try UseTransparentHugePages since there are known3659// performance issues with it turned on. This might change in the future.3660UseTransparentHugePages = false;3661}36623663if (UseTransparentHugePages) {3664bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages);3665if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) {3666UseHugeTLBFS = false;3667UseSHM = false;3668return true;3669}3670UseTransparentHugePages = false;3671}36723673if (UseHugeTLBFS) {3674bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS);3675if (hugetlbfs_sanity_check(warn_on_failure, page_size)) {3676UseSHM = false;3677return true;3678}3679UseHugeTLBFS = false;3680}36813682#ifdef DISABLE_SHM3683if (UseSHM) {3684warning("UseSHM is disabled");3685UseSHM = false;3686}3687#endif //DISABLE_SHM36883689return UseSHM;3690}36913692void os::large_page_init() {3693if (!UseLargePages &&3694!UseTransparentHugePages &&3695!UseHugeTLBFS &&3696!UseSHM) {3697// Not using large pages.3698return;3699}37003701if (!FLAG_IS_DEFAULT(UseLargePages) && !UseLargePages) {3702// The user explicitly turned off large pages.3703// Ignore the rest of the large pages flags.3704UseTransparentHugePages = false;3705UseHugeTLBFS = false;3706UseSHM = false;3707return;3708}37093710size_t large_page_size = Linux::setup_large_page_size();3711UseLargePages = Linux::setup_large_page_type(large_page_size);37123713set_coredump_filter();3714}37153716#ifndef SHM_HUGETLB3717#define SHM_HUGETLB 040003718#endif37193720#ifndef DISABLE_SHM3721#define shm_warning_format(format, ...) \3722do { \3723if (UseLargePages && \3724(!FLAG_IS_DEFAULT(UseLargePages) || \3725!FLAG_IS_DEFAULT(UseSHM) || \3726!FLAG_IS_DEFAULT(LargePageSizeInBytes))) { \3727warning(format, __VA_ARGS__); \3728} \3729} while (0)37303731#define shm_warning(str) shm_warning_format("%s", str)37323733#define shm_warning_with_errno(str) \3734do { \3735int err = errno; \3736shm_warning_format(str " (error = %d)", err); \3737} while (0)37383739static char* shmat_with_alignment(int shmid, size_t bytes, size_t alignment) {3740assert(is_size_aligned(bytes, alignment), "Must be divisible by the alignment");37413742if (!is_size_aligned(alignment, SHMLBA)) {3743assert(false, "Code below assumes that alignment is at least SHMLBA aligned");3744return NULL;3745}37463747// To ensure that we get 'alignment' aligned memory from shmat,3748// we pre-reserve aligned virtual memory and then attach to that.37493750char* pre_reserved_addr = anon_mmap_aligned(bytes, alignment, NULL);3751if (pre_reserved_addr == NULL) {3752// Couldn't pre-reserve aligned memory.3753shm_warning("Failed to pre-reserve aligned memory for shmat.");3754return NULL;3755}37563757// SHM_REMAP is needed to allow shmat to map over an existing mapping.3758char* addr = (char*)shmat(shmid, pre_reserved_addr, SHM_REMAP);37593760if ((intptr_t)addr == -1) {3761int err = errno;3762shm_warning_with_errno("Failed to attach shared memory.");37633764assert(err != EACCES, "Unexpected error");3765assert(err != EIDRM, "Unexpected error");3766assert(err != EINVAL, "Unexpected error");37673768// Since we don't know if the kernel unmapped the pre-reserved memory area3769// we can't unmap it, since that would potentially unmap memory that was3770// mapped from other threads.3771return NULL;3772}37733774return addr;3775}37763777static char* shmat_at_address(int shmid, char* req_addr) {3778if (!is_ptr_aligned(req_addr, SHMLBA)) {3779assert(false, "Requested address needs to be SHMLBA aligned");3780return NULL;3781}37823783char* addr = (char*)shmat(shmid, req_addr, 0);37843785if ((intptr_t)addr == -1) {3786shm_warning_with_errno("Failed to attach shared memory.");3787return NULL;3788}37893790return addr;3791}37923793static char* shmat_large_pages(int shmid, size_t bytes, size_t alignment, char* req_addr) {3794// If a req_addr has been provided, we assume that the caller has already aligned the address.3795if (req_addr != NULL) {3796assert(is_ptr_aligned(req_addr, os::large_page_size()), "Must be divisible by the large page size");3797assert(is_ptr_aligned(req_addr, alignment), "Must be divisible by given alignment");3798return shmat_at_address(shmid, req_addr);3799}38003801// Since shmid has been setup with SHM_HUGETLB, shmat will automatically3802// return large page size aligned memory addresses when req_addr == NULL.3803// However, if the alignment is larger than the large page size, we have3804// to manually ensure that the memory returned is 'alignment' aligned.3805if (alignment > os::large_page_size()) {3806assert(is_size_aligned(alignment, os::large_page_size()), "Must be divisible by the large page size");3807return shmat_with_alignment(shmid, bytes, alignment);3808} else {3809return shmat_at_address(shmid, NULL);3810}3811}3812#endif // !DISABLE_SHM38133814char* os::Linux::reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec) {3815#ifndef DISABLE_SHM3816// "exec" is passed in but not used. Creating the shared image for3817// the code cache doesn't have an SHM_X executable permission to check.3818assert(UseLargePages && UseSHM, "only for SHM large pages");3819assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address");3820assert(is_ptr_aligned(req_addr, alignment), "Unaligned address");38213822if (!is_size_aligned(bytes, os::large_page_size())) {3823return NULL; // Fallback to small pages.3824}38253826// Create a large shared memory region to attach to based on size.3827// Currently, size is the total size of the heap.3828int shmid = shmget(IPC_PRIVATE, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);3829if (shmid == -1) {3830// Possible reasons for shmget failure:3831// 1. shmmax is too small for Java heap.3832// > check shmmax value: cat /proc/sys/kernel/shmmax3833// > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax3834// 2. not enough large page memory.3835// > check available large pages: cat /proc/meminfo3836// > increase amount of large pages:3837// echo new_value > /proc/sys/vm/nr_hugepages3838// Note 1: different Linux may use different name for this property,3839// e.g. on Redhat AS-3 it is "hugetlb_pool".3840// Note 2: it's possible there's enough physical memory available but3841// they are so fragmented after a long run that they can't3842// coalesce into large pages. Try to reserve large pages when3843// the system is still "fresh".3844shm_warning_with_errno("Failed to reserve shared memory.");3845return NULL;3846}38473848// Attach to the region.3849char* addr = shmat_large_pages(shmid, bytes, alignment, req_addr);38503851// Remove shmid. If shmat() is successful, the actual shared memory segment3852// will be deleted when it's detached by shmdt() or when the process3853// terminates. If shmat() is not successful this will remove the shared3854// segment immediately.3855shmctl(shmid, IPC_RMID, NULL);38563857return addr;3858#else3859assert(0, "SHM was disabled on compile time");3860return NULL;3861#endif3862}38633864static void warn_on_large_pages_failure(char* req_addr, size_t bytes, int error) {3865assert(error == ENOMEM, "Only expect to fail if no memory is available");38663867bool warn_on_failure = UseLargePages &&3868(!FLAG_IS_DEFAULT(UseLargePages) ||3869!FLAG_IS_DEFAULT(UseHugeTLBFS) ||3870!FLAG_IS_DEFAULT(LargePageSizeInBytes));38713872if (warn_on_failure) {3873char msg[128];3874jio_snprintf(msg, sizeof(msg), "Failed to reserve large pages memory req_addr: "3875PTR_FORMAT " bytes: " SIZE_FORMAT " (errno = %d).", req_addr, bytes, error);3876warning("%s", msg);3877}3878}38793880char* os::Linux::reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec) {3881assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages");3882assert(is_size_aligned(bytes, os::large_page_size()), "Unaligned size");3883assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address");38843885int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;3886char* addr = (char*)::mmap(req_addr, bytes, prot,3887MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB,3888-1, 0);38893890if (addr == MAP_FAILED) {3891warn_on_large_pages_failure(req_addr, bytes, errno);3892return NULL;3893}38943895assert(is_ptr_aligned(addr, os::large_page_size()), "Must be");38963897return addr;3898}38993900// Reserve memory using mmap(MAP_HUGETLB).3901// - bytes shall be a multiple of alignment.3902// - req_addr can be NULL. If not NULL, it must be a multiple of alignment.3903// - alignment sets the alignment at which memory shall be allocated.3904// It must be a multiple of allocation granularity.3905// Returns address of memory or NULL. If req_addr was not NULL, will only return3906// req_addr or NULL.3907char* os::Linux::reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec) {3908size_t large_page_size = os::large_page_size();3909assert(bytes >= large_page_size, "Shouldn't allocate large pages for small sizes");39103911assert(is_ptr_aligned(req_addr, alignment), "Must be");3912assert(is_size_aligned(bytes, alignment), "Must be");39133914// First reserve - but not commit - the address range in small pages.3915char* const start = anon_mmap_aligned(bytes, alignment, req_addr);39163917if (start == NULL) {3918return NULL;3919}39203921assert(is_ptr_aligned(start, alignment), "Must be");39223923char* end = start + bytes;39243925// Find the regions of the allocated chunk that can be promoted to large pages.3926char* lp_start = (char*)align_ptr_up(start, large_page_size);3927char* lp_end = (char*)align_ptr_down(end, large_page_size);39283929size_t lp_bytes = lp_end - lp_start;39303931assert(is_size_aligned(lp_bytes, large_page_size), "Must be");39323933if (lp_bytes == 0) {3934// The mapped region doesn't even span the start and the end of a large page.3935// Fall back to allocate a non-special area.3936::munmap(start, end - start);3937return NULL;3938}39393940int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;39413942void* result;39433944// Commit small-paged leading area.3945if (start != lp_start) {3946result = ::mmap(start, lp_start - start, prot,3947MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,3948-1, 0);3949if (result == MAP_FAILED) {3950::munmap(lp_start, end - lp_start);3951return NULL;3952}3953}39543955// Commit large-paged area.3956result = ::mmap(lp_start, lp_bytes, prot,3957MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_HUGETLB,3958-1, 0);3959if (result == MAP_FAILED) {3960warn_on_large_pages_failure(lp_start, lp_bytes, errno);3961// If the mmap above fails, the large pages region will be unmapped and we3962// have regions before and after with small pages. Release these regions.3963//3964// | mapped | unmapped | mapped |3965// ^ ^ ^ ^3966// start lp_start lp_end end3967//3968::munmap(start, lp_start - start);3969::munmap(lp_end, end - lp_end);3970return NULL;3971}39723973// Commit small-paged trailing area.3974if (lp_end != end) {3975result = ::mmap(lp_end, end - lp_end, prot,3976MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,3977-1, 0);3978if (result == MAP_FAILED) {3979::munmap(start, lp_end - start);3980return NULL;3981}3982}39833984return start;3985}39863987char* os::Linux::reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec) {3988assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages");3989assert(is_ptr_aligned(req_addr, alignment), "Must be");3990assert(is_size_aligned(alignment, os::vm_allocation_granularity()), "Must be");3991assert(is_power_of_2(os::large_page_size()), "Must be");3992assert(bytes >= os::large_page_size(), "Shouldn't allocate large pages for small sizes");39933994if (is_size_aligned(bytes, os::large_page_size()) && alignment <= os::large_page_size()) {3995return reserve_memory_special_huge_tlbfs_only(bytes, req_addr, exec);3996} else {3997return reserve_memory_special_huge_tlbfs_mixed(bytes, alignment, req_addr, exec);3998}3999}40004001char* os::reserve_memory_special(size_t bytes, size_t alignment, char* req_addr, bool exec) {4002assert(UseLargePages, "only for large pages");40034004char* addr;4005if (UseSHM) {4006addr = os::Linux::reserve_memory_special_shm(bytes, alignment, req_addr, exec);4007} else {4008assert(UseHugeTLBFS, "must be");4009addr = os::Linux::reserve_memory_special_huge_tlbfs(bytes, alignment, req_addr, exec);4010}40114012if (addr != NULL) {4013if (UseNUMAInterleaving) {4014numa_make_global(addr, bytes);4015}40164017// The memory is committed4018MemTracker::record_virtual_memory_reserve_and_commit((address)addr, bytes, CALLER_PC);4019}40204021return addr;4022}40234024bool os::Linux::release_memory_special_shm(char* base, size_t bytes) {4025#ifndef DISABLE_SHM4026// detaching the SHM segment will also delete it, see reserve_memory_special_shm()4027return shmdt(base) == 0;4028#else4029assert(0, "SHM was disabled on compile time");4030#endif4031}40324033bool os::Linux::release_memory_special_huge_tlbfs(char* base, size_t bytes) {4034return pd_release_memory(base, bytes);4035}40364037bool os::release_memory_special(char* base, size_t bytes) {4038bool res;4039if (MemTracker::tracking_level() > NMT_minimal) {4040Tracker tkr = MemTracker::get_virtual_memory_release_tracker();4041res = os::Linux::release_memory_special_impl(base, bytes);4042if (res) {4043tkr.record((address)base, bytes);4044}40454046} else {4047res = os::Linux::release_memory_special_impl(base, bytes);4048}4049return res;4050}40514052bool os::Linux::release_memory_special_impl(char* base, size_t bytes) {4053assert(UseLargePages, "only for large pages");4054bool res;40554056if (UseSHM) {4057res = os::Linux::release_memory_special_shm(base, bytes);4058} else {4059assert(UseHugeTLBFS, "must be");4060res = os::Linux::release_memory_special_huge_tlbfs(base, bytes);4061}4062return res;4063}40644065size_t os::large_page_size() {4066return _large_page_size;4067}40684069// With SysV SHM the entire memory region must be allocated as shared4070// memory.4071// HugeTLBFS allows application to commit large page memory on demand.4072// However, when committing memory with HugeTLBFS fails, the region4073// that was supposed to be committed will lose the old reservation4074// and allow other threads to steal that memory region. Because of this4075// behavior we can't commit HugeTLBFS memory.4076bool os::can_commit_large_page_memory() {4077return UseTransparentHugePages;4078}40794080bool os::can_execute_large_page_memory() {4081return UseTransparentHugePages || UseHugeTLBFS;4082}40834084// Reserve memory at an arbitrary address, only if that area is4085// available (and not reserved for something else).40864087char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {4088const int max_tries = 10;4089char* base[max_tries];4090size_t size[max_tries];4091const size_t gap = 0x000000;40924093// Assert only that the size is a multiple of the page size, since4094// that's all that mmap requires, and since that's all we really know4095// about at this low abstraction level. If we need higher alignment,4096// we can either pass an alignment to this method or verify alignment4097// in one of the methods further up the call chain. See bug 5044738.4098assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");40994100// Repeatedly allocate blocks until the block is allocated at the4101// right spot. Give up after max_tries. Note that reserve_memory() will4102// automatically update _highest_vm_reserved_address if the call is4103// successful. The variable tracks the highest memory address every reserved4104// by JVM. It is used to detect heap-stack collision if running with4105// fixed-stack LinuxThreads. Because here we may attempt to reserve more4106// space than needed, it could confuse the collision detecting code. To4107// solve the problem, save current _highest_vm_reserved_address and4108// calculate the correct value before return.4109address old_highest = _highest_vm_reserved_address;41104111// Linux mmap allows caller to pass an address as hint; give it a try first,4112// if kernel honors the hint then we can return immediately.4113char * addr = anon_mmap(requested_addr, bytes, false);4114if (addr == requested_addr) {4115return requested_addr;4116}41174118if (addr != NULL) {4119// mmap() is successful but it fails to reserve at the requested address4120anon_munmap(addr, bytes);4121}41224123int i;4124for (i = 0; i < max_tries; ++i) {4125base[i] = reserve_memory(bytes);41264127if (base[i] != NULL) {4128// Is this the block we wanted?4129if (base[i] == requested_addr) {4130size[i] = bytes;4131break;4132}41334134// Does this overlap the block we wanted? Give back the overlapped4135// parts and try again.41364137size_t top_overlap = requested_addr + (bytes + gap) - base[i];4138if (top_overlap >= 0 && top_overlap < bytes) {4139unmap_memory(base[i], top_overlap);4140base[i] += top_overlap;4141size[i] = bytes - top_overlap;4142} else {4143size_t bottom_overlap = base[i] + bytes - requested_addr;4144if (bottom_overlap >= 0 && bottom_overlap < bytes) {4145unmap_memory(requested_addr, bottom_overlap);4146size[i] = bytes - bottom_overlap;4147} else {4148size[i] = bytes;4149}4150}4151}4152}41534154// Give back the unused reserved pieces.41554156for (int j = 0; j < i; ++j) {4157if (base[j] != NULL) {4158unmap_memory(base[j], size[j]);4159}4160}41614162if (i < max_tries) {4163_highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);4164return requested_addr;4165} else {4166_highest_vm_reserved_address = old_highest;4167return NULL;4168}4169}41704171size_t os::read(int fd, void *buf, unsigned int nBytes) {4172return ::read(fd, buf, nBytes);4173}41744175size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {4176return ::pread(fd, buf, nBytes, offset);4177}41784179// TODO-FIXME: reconcile Solaris' os::sleep with the linux variation.4180// Solaris uses poll(), linux uses park().4181// Poll() is likely a better choice, assuming that Thread.interrupt()4182// generates a SIGUSRx signal. Note that SIGUSR1 can interfere with4183// SIGSEGV, see 4355769.41844185int os::sleep(Thread* thread, jlong millis, bool interruptible) {4186assert(thread == Thread::current(), "thread consistency check");41874188ParkEvent * const slp = thread->_SleepEvent ;4189slp->reset() ;4190OrderAccess::fence() ;41914192if (interruptible) {4193jlong prevtime = javaTimeNanos();41944195for (;;) {4196if (os::is_interrupted(thread, true)) {4197return OS_INTRPT;4198}41994200jlong newtime = javaTimeNanos();42014202if (newtime - prevtime < 0) {4203// time moving backwards, should only happen if no monotonic clock4204// not a guarantee() because JVM should not abort on kernel/glibc bugs4205assert(!Linux::supports_monotonic_clock(), "time moving backwards");4206} else {4207millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;4208}42094210if(millis <= 0) {4211return OS_OK;4212}42134214prevtime = newtime;42154216{4217assert(thread->is_Java_thread(), "sanity check");4218JavaThread *jt = (JavaThread *) thread;4219ThreadBlockInVM tbivm(jt);4220OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);42214222jt->set_suspend_equivalent();4223// cleared by handle_special_suspend_equivalent_condition() or4224// java_suspend_self() via check_and_wait_while_suspended()42254226slp->park(millis);42274228// were we externally suspended while we were waiting?4229jt->check_and_wait_while_suspended();4230}4231}4232} else {4233OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);4234jlong prevtime = javaTimeNanos();42354236for (;;) {4237// It'd be nice to avoid the back-to-back javaTimeNanos() calls on4238// the 1st iteration ...4239jlong newtime = javaTimeNanos();42404241if (newtime - prevtime < 0) {4242// time moving backwards, should only happen if no monotonic clock4243// not a guarantee() because JVM should not abort on kernel/glibc bugs4244assert(!Linux::supports_monotonic_clock(), "time moving backwards");4245} else {4246millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;4247}42484249if(millis <= 0) break ;42504251prevtime = newtime;4252slp->park(millis);4253}4254return OS_OK ;4255}4256}42574258//4259// Short sleep, direct OS call.4260//4261// Note: certain versions of Linux CFS scheduler (since 2.6.23) do not guarantee4262// sched_yield(2) will actually give up the CPU:4263//4264// * Alone on this pariticular CPU, keeps running.4265// * Before the introduction of "skip_buddy" with "compat_yield" disabled4266// (pre 2.6.39).4267//4268// So calling this with 0 is an alternative.4269//4270void os::naked_short_sleep(jlong ms) {4271struct timespec req;42724273assert(ms < 1000, "Un-interruptable sleep, short time use only");4274req.tv_sec = 0;4275if (ms > 0) {4276req.tv_nsec = (ms % 1000) * 1000000;4277}4278else {4279req.tv_nsec = 1;4280}42814282nanosleep(&req, NULL);42834284return;4285}42864287// Sleep forever; naked call to OS-specific sleep; use with CAUTION4288void os::infinite_sleep() {4289while (true) { // sleep forever ...4290::sleep(100); // ... 100 seconds at a time4291}4292}42934294// Used to convert frequent JVM_Yield() to nops4295bool os::dont_yield() {4296return DontYieldALot;4297}42984299void os::yield() {4300sched_yield();4301}43024303os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}43044305void os::yield_all(int attempts) {4306// Yields to all threads, including threads with lower priorities4307// Threads on Linux are all with same priority. The Solaris style4308// os::yield_all() with nanosleep(1ms) is not necessary.4309sched_yield();4310}43114312// Called from the tight loops to possibly influence time-sharing heuristics4313void os::loop_breaker(int attempts) {4314os::yield_all(attempts);4315}43164317////////////////////////////////////////////////////////////////////////////////4318// thread priority support43194320// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER4321// only supports dynamic priority, static priority must be zero. For real-time4322// applications, Linux supports SCHED_RR which allows static priority (1-99).4323// However, for large multi-threaded applications, SCHED_RR is not only slower4324// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out4325// of 5 runs - Sep 2005).4326//4327// The following code actually changes the niceness of kernel-thread/LWP. It4328// has an assumption that setpriority() only modifies one kernel-thread/LWP,4329// not the entire user process, and user level threads are 1:1 mapped to kernel4330// threads. It has always been the case, but could change in the future. For4331// this reason, the code should not be used as default (ThreadPriorityPolicy=0).4332// It is only used when ThreadPriorityPolicy=1 and requires root privilege.43334334int os::java_to_os_priority[CriticalPriority + 1] = {433519, // 0 Entry should never be used433643374, // 1 MinPriority43383, // 243392, // 3434043411, // 443420, // 5 NormPriority4343-1, // 643444345-2, // 74346-3, // 84347-4, // 9 NearMaxPriority43484349-5, // 10 MaxPriority43504351-5 // 11 CriticalPriority4352};43534354static int prio_init() {4355if (ThreadPriorityPolicy == 1) {4356// Only root can raise thread priority. Don't allow ThreadPriorityPolicy=14357// if effective uid is not root. Perhaps, a more elegant way of doing4358// this is to test CAP_SYS_NICE capability, but that will require libcap.so4359if (geteuid() != 0) {4360if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {4361warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");4362}4363ThreadPriorityPolicy = 0;4364}4365}4366if (UseCriticalJavaThreadPriority) {4367os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];4368}4369return 0;4370}43714372OSReturn os::set_native_priority(Thread* thread, int newpri) {4373if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;43744375int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);4376return (ret == 0) ? OS_OK : OS_ERR;4377}43784379OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) {4380if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) {4381*priority_ptr = java_to_os_priority[NormPriority];4382return OS_OK;4383}43844385errno = 0;4386*priority_ptr = getpriority(PRIO_PROCESS, thread->osthread()->thread_id());4387return (*priority_ptr != -1 || errno == 0 ? OS_OK : OS_ERR);4388}43894390// Hint to the underlying OS that a task switch would not be good.4391// Void return because it's a hint and can fail.4392void os::hint_no_preempt() {}43934394////////////////////////////////////////////////////////////////////////////////4395// suspend/resume support43964397// the low-level signal-based suspend/resume support is a remnant from the4398// old VM-suspension that used to be for java-suspension, safepoints etc,4399// within hotspot. Now there is a single use-case for this:4400// - calling get_thread_pc() on the VMThread by the flat-profiler task4401// that runs in the watcher thread.4402// The remaining code is greatly simplified from the more general suspension4403// code that used to be used.4404//4405// The protocol is quite simple:4406// - suspend:4407// - sends a signal to the target thread4408// - polls the suspend state of the osthread using a yield loop4409// - target thread signal handler (SR_handler) sets suspend state4410// and blocks in sigsuspend until continued4411// - resume:4412// - sets target osthread state to continue4413// - sends signal to end the sigsuspend loop in the SR_handler4414//4415// Note that the SR_lock plays no role in this suspend/resume protocol.4416//44174418static void resume_clear_context(OSThread *osthread) {4419osthread->set_ucontext(NULL);4420osthread->set_siginfo(NULL);4421}44224423static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {4424osthread->set_ucontext(context);4425osthread->set_siginfo(siginfo);4426}44274428//4429// Handler function invoked when a thread's execution is suspended or4430// resumed. We have to be careful that only async-safe functions are4431// called here (Note: most pthread functions are not async safe and4432// should be avoided.)4433//4434// Note: sigwait() is a more natural fit than sigsuspend() from an4435// interface point of view, but sigwait() prevents the signal hander4436// from being run. libpthread would get very confused by not having4437// its signal handlers run and prevents sigwait()'s use with the4438// mutex granting granting signal.4439//4440// Currently only ever called on the VMThread and JavaThreads (PC sampling)4441//4442static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {4443// Save and restore errno to avoid confusing native code with EINTR4444// after sigsuspend.4445int old_errno = errno;44464447Thread* thread = Thread::current();4448OSThread* osthread = thread->osthread();4449assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");44504451os::SuspendResume::State current = osthread->sr.state();4452if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {4453suspend_save_context(osthread, siginfo, context);44544455// attempt to switch the state, we assume we had a SUSPEND_REQUEST4456os::SuspendResume::State state = osthread->sr.suspended();4457if (state == os::SuspendResume::SR_SUSPENDED) {4458sigset_t suspend_set; // signals for sigsuspend()44594460// get current set of blocked signals and unblock resume signal4461pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);4462sigdelset(&suspend_set, SR_signum);44634464sr_semaphore.signal();4465// wait here until we are resumed4466while (1) {4467sigsuspend(&suspend_set);44684469os::SuspendResume::State result = osthread->sr.running();4470if (result == os::SuspendResume::SR_RUNNING) {4471sr_semaphore.signal();4472break;4473}4474}44754476} else if (state == os::SuspendResume::SR_RUNNING) {4477// request was cancelled, continue4478} else {4479ShouldNotReachHere();4480}44814482resume_clear_context(osthread);4483} else if (current == os::SuspendResume::SR_RUNNING) {4484// request was cancelled, continue4485} else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {4486// ignore4487} else {4488// ignore4489}44904491errno = old_errno;4492}449344944495static int SR_initialize() {4496struct sigaction act;4497char *s;4498/* Get signal number to use for suspend/resume */4499if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {4500int sig = ::strtol(s, 0, 10);4501if (sig > 0 || sig < _NSIG) {4502SR_signum = sig;4503}4504}45054506assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,4507"SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");45084509sigemptyset(&SR_sigset);4510sigaddset(&SR_sigset, SR_signum);45114512/* Set up signal handler for suspend/resume */4513act.sa_flags = SA_RESTART|SA_SIGINFO;4514act.sa_handler = (void (*)(int)) SR_handler;45154516// SR_signum is blocked by default.4517// 4528190 - We also need to block pthread restart signal (32 on all4518// supported Linux platforms). Note that LinuxThreads need to block4519// this signal for all threads to work properly. So we don't have4520// to use hard-coded signal number when setting up the mask.4521pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);45224523if (sigaction(SR_signum, &act, 0) == -1) {4524return -1;4525}45264527// Save signal flag4528os::Linux::set_our_sigflags(SR_signum, act.sa_flags);4529return 0;4530}45314532static int sr_notify(OSThread* osthread) {4533int status = pthread_kill(osthread->pthread_id(), SR_signum);4534assert_status(status == 0, status, "pthread_kill");4535return status;4536}45374538// "Randomly" selected value for how long we want to spin4539// before bailing out on suspending a thread, also how often4540// we send a signal to a thread we want to resume4541static const int RANDOMLY_LARGE_INTEGER = 1000000;4542static const int RANDOMLY_LARGE_INTEGER2 = 100;45434544// returns true on success and false on error - really an error is fatal4545// but this seems the normal response to library errors4546static bool do_suspend(OSThread* osthread) {4547assert(osthread->sr.is_running(), "thread should be running");4548assert(!sr_semaphore.trywait(), "semaphore has invalid state");45494550// mark as suspended and send signal4551if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {4552// failed to switch, state wasn't running?4553ShouldNotReachHere();4554return false;4555}45564557if (sr_notify(osthread) != 0) {4558ShouldNotReachHere();4559}45604561// managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED4562while (true) {4563if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {4564break;4565} else {4566// timeout4567os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();4568if (cancelled == os::SuspendResume::SR_RUNNING) {4569return false;4570} else if (cancelled == os::SuspendResume::SR_SUSPENDED) {4571// make sure that we consume the signal on the semaphore as well4572sr_semaphore.wait();4573break;4574} else {4575ShouldNotReachHere();4576return false;4577}4578}4579}45804581guarantee(osthread->sr.is_suspended(), "Must be suspended");4582return true;4583}45844585static void do_resume(OSThread* osthread) {4586assert(osthread->sr.is_suspended(), "thread should be suspended");4587assert(!sr_semaphore.trywait(), "invalid semaphore state");45884589if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {4590// failed to switch to WAKEUP_REQUEST4591ShouldNotReachHere();4592return;4593}45944595while (true) {4596if (sr_notify(osthread) == 0) {4597if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {4598if (osthread->sr.is_running()) {4599return;4600}4601}4602} else {4603ShouldNotReachHere();4604}4605}46064607guarantee(osthread->sr.is_running(), "Must be running!");4608}46094610////////////////////////////////////////////////////////////////////////////////4611// interrupt support46124613void os::interrupt(Thread* thread) {4614assert(Thread::current() == thread || Threads_lock->owned_by_self(),4615"possibility of dangling Thread pointer");46164617OSThread* osthread = thread->osthread();46184619if (!osthread->interrupted()) {4620osthread->set_interrupted(true);4621// More than one thread can get here with the same value of osthread,4622// resulting in multiple notifications. We do, however, want the store4623// to interrupted() to be visible to other threads before we execute unpark().4624OrderAccess::fence();4625ParkEvent * const slp = thread->_SleepEvent ;4626if (slp != NULL) slp->unpark() ;4627}46284629// For JSR166. Unpark even if interrupt status already was set4630if (thread->is_Java_thread())4631((JavaThread*)thread)->parker()->unpark();46324633ParkEvent * ev = thread->_ParkEvent ;4634if (ev != NULL) ev->unpark() ;46354636}46374638bool os::is_interrupted(Thread* thread, bool clear_interrupted) {4639assert(Thread::current() == thread || Threads_lock->owned_by_self(),4640"possibility of dangling Thread pointer");46414642OSThread* osthread = thread->osthread();46434644bool interrupted = osthread->interrupted();46454646if (interrupted && clear_interrupted) {4647osthread->set_interrupted(false);4648// consider thread->_SleepEvent->reset() ... optional optimization4649}46504651return interrupted;4652}46534654///////////////////////////////////////////////////////////////////////////////////4655// signal handling (except suspend/resume)46564657// This routine may be used by user applications as a "hook" to catch signals.4658// The user-defined signal handler must pass unrecognized signals to this4659// routine, and if it returns true (non-zero), then the signal handler must4660// return immediately. If the flag "abort_if_unrecognized" is true, then this4661// routine will never retun false (zero), but instead will execute a VM panic4662// routine kill the process.4663//4664// If this routine returns false, it is OK to call it again. This allows4665// the user-defined signal handler to perform checks either before or after4666// the VM performs its own checks. Naturally, the user code would be making4667// a serious error if it tried to handle an exception (such as a null check4668// or breakpoint) that the VM was generating for its own correct operation.4669//4670// This routine may recognize any of the following kinds of signals:4671// SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.4672// It should be consulted by handlers for any of those signals.4673//4674// The caller of this routine must pass in the three arguments supplied4675// to the function referred to in the "sa_sigaction" (not the "sa_handler")4676// field of the structure passed to sigaction(). This routine assumes that4677// the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.4678//4679// Note that the VM will print warnings if it detects conflicting signal4680// handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".4681//4682extern "C" JNIEXPORT int4683JVM_handle_linux_signal(int signo, siginfo_t* siginfo,4684void* ucontext, int abort_if_unrecognized);46854686void signalHandler(int sig, siginfo_t* info, void* uc) {4687assert(info != NULL && uc != NULL, "it must be old kernel");4688int orig_errno = errno; // Preserve errno value over signal handler.4689JVM_handle_linux_signal(sig, info, uc, true);4690errno = orig_errno;4691}469246934694// This boolean allows users to forward their own non-matching signals4695// to JVM_handle_linux_signal, harmlessly.4696bool os::Linux::signal_handlers_are_installed = false;46974698// For signal-chaining4699struct sigaction os::Linux::sigact[MAXSIGNUM];4700unsigned int os::Linux::sigs = 0;4701bool os::Linux::libjsig_is_loaded = false;4702typedef struct sigaction *(*get_signal_t)(int);4703get_signal_t os::Linux::get_signal_action = NULL;47044705struct sigaction* os::Linux::get_chained_signal_action(int sig) {4706struct sigaction *actp = NULL;47074708if (libjsig_is_loaded) {4709// Retrieve the old signal handler from libjsig4710actp = (*get_signal_action)(sig);4711}4712if (actp == NULL) {4713// Retrieve the preinstalled signal handler from jvm4714actp = get_preinstalled_handler(sig);4715}47164717return actp;4718}47194720static bool call_chained_handler(struct sigaction *actp, int sig,4721siginfo_t *siginfo, void *context) {4722// Call the old signal handler4723if (actp->sa_handler == SIG_DFL) {4724// It's more reasonable to let jvm treat it as an unexpected exception4725// instead of taking the default action.4726return false;4727} else if (actp->sa_handler != SIG_IGN) {4728if ((actp->sa_flags & SA_NODEFER) == 0) {4729// automaticlly block the signal4730sigaddset(&(actp->sa_mask), sig);4731}47324733sa_handler_t hand = NULL;4734sa_sigaction_t sa = NULL;4735bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;4736// retrieve the chained handler4737if (siginfo_flag_set) {4738sa = actp->sa_sigaction;4739} else {4740hand = actp->sa_handler;4741}47424743if ((actp->sa_flags & SA_RESETHAND) != 0) {4744actp->sa_handler = SIG_DFL;4745}47464747// try to honor the signal mask4748sigset_t oset;4749pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset);47504751// call into the chained handler4752if (siginfo_flag_set) {4753(*sa)(sig, siginfo, context);4754} else {4755(*hand)(sig);4756}47574758// restore the signal mask4759pthread_sigmask(SIG_SETMASK, &oset, 0);4760}4761// Tell jvm's signal handler the signal is taken care of.4762return true;4763}47644765bool os::Linux::chained_handler(int sig, siginfo_t* siginfo, void* context) {4766bool chained = false;4767// signal-chaining4768if (UseSignalChaining) {4769struct sigaction *actp = get_chained_signal_action(sig);4770if (actp != NULL) {4771chained = call_chained_handler(actp, sig, siginfo, context);4772}4773}4774return chained;4775}47764777struct sigaction* os::Linux::get_preinstalled_handler(int sig) {4778if ((( (unsigned int)1 << sig ) & sigs) != 0) {4779return &sigact[sig];4780}4781return NULL;4782}47834784void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) {4785assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4786sigact[sig] = oldAct;4787sigs |= (unsigned int)1 << sig;4788}47894790// for diagnostic4791int os::Linux::sigflags[MAXSIGNUM];47924793int os::Linux::get_our_sigflags(int sig) {4794assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4795return sigflags[sig];4796}47974798void os::Linux::set_our_sigflags(int sig, int flags) {4799assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4800sigflags[sig] = flags;4801}48024803void os::Linux::set_signal_handler(int sig, bool set_installed) {4804// Check for overwrite.4805struct sigaction oldAct;4806sigaction(sig, (struct sigaction*)NULL, &oldAct);48074808void* oldhand = oldAct.sa_sigaction4809? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)4810: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);4811if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&4812oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&4813oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {4814if (AllowUserSignalHandlers || !set_installed) {4815// Do not overwrite; user takes responsibility to forward to us.4816return;4817} else if (UseSignalChaining) {4818// save the old handler in jvm4819save_preinstalled_handler(sig, oldAct);4820// libjsig also interposes the sigaction() call below and saves the4821// old sigaction on it own.4822} else {4823fatal(err_msg("Encountered unexpected pre-existing sigaction handler "4824"%#lx for signal %d.", (long)oldhand, sig));4825}4826}48274828struct sigaction sigAct;4829sigfillset(&(sigAct.sa_mask));4830sigAct.sa_handler = SIG_DFL;4831if (!set_installed) {4832sigAct.sa_flags = SA_SIGINFO|SA_RESTART;4833} else {4834sigAct.sa_sigaction = signalHandler;4835sigAct.sa_flags = SA_SIGINFO|SA_RESTART;4836}4837// Save flags, which are set by ours4838assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4839sigflags[sig] = sigAct.sa_flags;48404841int ret = sigaction(sig, &sigAct, &oldAct);4842assert(ret == 0, "check");48434844void* oldhand2 = oldAct.sa_sigaction4845? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)4846: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);4847assert(oldhand2 == oldhand, "no concurrent signal handler installation");4848}48494850// install signal handlers for signals that HotSpot needs to4851// handle in order to support Java-level exception handling.48524853void os::Linux::install_signal_handlers() {4854if (!signal_handlers_are_installed) {4855signal_handlers_are_installed = true;48564857// signal-chaining4858typedef void (*signal_setting_t)();4859signal_setting_t begin_signal_setting = NULL;4860signal_setting_t end_signal_setting = NULL;4861begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,4862dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));4863if (begin_signal_setting != NULL) {4864end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,4865dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));4866get_signal_action = CAST_TO_FN_PTR(get_signal_t,4867dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));4868libjsig_is_loaded = true;4869assert(UseSignalChaining, "should enable signal-chaining");4870}4871if (libjsig_is_loaded) {4872// Tell libjsig jvm is setting signal handlers4873(*begin_signal_setting)();4874}48754876set_signal_handler(SIGSEGV, true);4877set_signal_handler(SIGPIPE, true);4878set_signal_handler(SIGBUS, true);4879set_signal_handler(SIGILL, true);4880set_signal_handler(SIGFPE, true);4881#if defined(PPC64)4882set_signal_handler(SIGTRAP, true);4883#endif4884set_signal_handler(SIGXFSZ, true);48854886if (libjsig_is_loaded) {4887// Tell libjsig jvm finishes setting signal handlers4888(*end_signal_setting)();4889}48904891// We don't activate signal checker if libjsig is in place, we trust ourselves4892// and if UserSignalHandler is installed all bets are off.4893// Log that signal checking is off only if -verbose:jni is specified.4894if (CheckJNICalls) {4895if (libjsig_is_loaded) {4896if (PrintJNIResolving) {4897tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");4898}4899check_signals = false;4900}4901if (AllowUserSignalHandlers) {4902if (PrintJNIResolving) {4903tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");4904}4905check_signals = false;4906}4907}4908}4909}49104911// This is the fastest way to get thread cpu time on Linux.4912// Returns cpu time (user+sys) for any thread, not only for current.4913// POSIX compliant clocks are implemented in the kernels 2.6.16+.4914// It might work on 2.6.10+ with a special kernel/glibc patch.4915// For reference, please, see IEEE Std 1003.1-2004:4916// http://www.unix.org/single_unix_specification49174918jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {4919struct timespec tp;4920int rc = os::Linux::clock_gettime(clockid, &tp);4921assert(rc == 0, "clock_gettime is expected to return 0 code");49224923return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;4924}49254926/////4927// glibc on Linux platform uses non-documented flag4928// to indicate, that some special sort of signal4929// trampoline is used.4930// We will never set this flag, and we should4931// ignore this flag in our diagnostic4932#ifdef SIGNIFICANT_SIGNAL_MASK4933#undef SIGNIFICANT_SIGNAL_MASK4934#endif4935#define SIGNIFICANT_SIGNAL_MASK (~0x04000000)49364937static const char* get_signal_handler_name(address handler,4938char* buf, int buflen) {4939int offset = 0;4940bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);4941if (found) {4942// skip directory names4943const char *p1, *p2;4944p1 = buf;4945size_t len = strlen(os::file_separator());4946while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;4947jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);4948} else {4949jio_snprintf(buf, buflen, PTR_FORMAT, handler);4950}4951return buf;4952}49534954static void print_signal_handler(outputStream* st, int sig,4955char* buf, size_t buflen) {4956struct sigaction sa;49574958sigaction(sig, NULL, &sa);49594960// See comment for SIGNIFICANT_SIGNAL_MASK define4961sa.sa_flags &= SIGNIFICANT_SIGNAL_MASK;49624963st->print("%s: ", os::exception_name(sig, buf, buflen));49644965address handler = (sa.sa_flags & SA_SIGINFO)4966? CAST_FROM_FN_PTR(address, sa.sa_sigaction)4967: CAST_FROM_FN_PTR(address, sa.sa_handler);49684969if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {4970st->print("SIG_DFL");4971} else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {4972st->print("SIG_IGN");4973} else {4974st->print("[%s]", get_signal_handler_name(handler, buf, buflen));4975}49764977st->print(", sa_mask[0]=");4978os::Posix::print_signal_set_short(st, &sa.sa_mask);49794980address rh = VMError::get_resetted_sighandler(sig);4981// May be, handler was resetted by VMError?4982if(rh != NULL) {4983handler = rh;4984sa.sa_flags = VMError::get_resetted_sigflags(sig) & SIGNIFICANT_SIGNAL_MASK;4985}49864987st->print(", sa_flags=");4988os::Posix::print_sa_flags(st, sa.sa_flags);49894990// Check: is it our handler?4991if(handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler) ||4992handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) {4993// It is our signal handler4994// check for flags, reset system-used one!4995if((int)sa.sa_flags != os::Linux::get_our_sigflags(sig)) {4996st->print(4997", flags was changed from " PTR32_FORMAT ", consider using jsig library",4998os::Linux::get_our_sigflags(sig));4999}5000}5001st->cr();5002}500350045005#define DO_SIGNAL_CHECK(sig) \5006if (!sigismember(&check_signal_done, sig)) \5007os::Linux::check_signal_handler(sig)50085009// This method is a periodic task to check for misbehaving JNI applications5010// under CheckJNI, we can add any periodic checks here50115012void os::run_periodic_checks() {50135014if (check_signals == false) return;50155016// SEGV and BUS if overridden could potentially prevent5017// generation of hs*.log in the event of a crash, debugging5018// such a case can be very challenging, so we absolutely5019// check the following for a good measure:5020DO_SIGNAL_CHECK(SIGSEGV);5021DO_SIGNAL_CHECK(SIGILL);5022DO_SIGNAL_CHECK(SIGFPE);5023DO_SIGNAL_CHECK(SIGBUS);5024DO_SIGNAL_CHECK(SIGPIPE);5025DO_SIGNAL_CHECK(SIGXFSZ);5026#if defined(PPC64)5027DO_SIGNAL_CHECK(SIGTRAP);5028#endif50295030// ReduceSignalUsage allows the user to override these handlers5031// see comments at the very top and jvm_solaris.h5032if (!ReduceSignalUsage) {5033DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);5034DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);5035DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);5036DO_SIGNAL_CHECK(BREAK_SIGNAL);5037}50385039DO_SIGNAL_CHECK(SR_signum);5040DO_SIGNAL_CHECK(INTERRUPT_SIGNAL);5041}50425043typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);50445045static os_sigaction_t os_sigaction = NULL;50465047void os::Linux::check_signal_handler(int sig) {5048char buf[O_BUFLEN];5049address jvmHandler = NULL;505050515052struct sigaction act;5053if (os_sigaction == NULL) {5054// only trust the default sigaction, in case it has been interposed5055os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");5056if (os_sigaction == NULL) return;5057}50585059os_sigaction(sig, (struct sigaction*)NULL, &act);506050615062act.sa_flags &= SIGNIFICANT_SIGNAL_MASK;50635064address thisHandler = (act.sa_flags & SA_SIGINFO)5065? CAST_FROM_FN_PTR(address, act.sa_sigaction)5066: CAST_FROM_FN_PTR(address, act.sa_handler) ;506750685069switch(sig) {5070case SIGSEGV:5071case SIGBUS:5072case SIGFPE:5073case SIGPIPE:5074case SIGILL:5075case SIGXFSZ:5076jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler);5077break;50785079case SHUTDOWN1_SIGNAL:5080case SHUTDOWN2_SIGNAL:5081case SHUTDOWN3_SIGNAL:5082case BREAK_SIGNAL:5083jvmHandler = (address)user_handler();5084break;50855086case INTERRUPT_SIGNAL:5087jvmHandler = CAST_FROM_FN_PTR(address, SIG_DFL);5088break;50895090default:5091if (sig == SR_signum) {5092jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler);5093} else {5094return;5095}5096break;5097}50985099if (thisHandler != jvmHandler) {5100tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));5101tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));5102tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));5103// No need to check this sig any longer5104sigaddset(&check_signal_done, sig);5105// Running under non-interactive shell, SHUTDOWN2_SIGNAL will be reassigned SIG_IGN5106if (sig == SHUTDOWN2_SIGNAL && !isatty(fileno(stdin))) {5107tty->print_cr("Running in non-interactive shell, %s handler is replaced by shell",5108exception_name(sig, buf, O_BUFLEN));5109}5110} else if(os::Linux::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Linux::get_our_sigflags(sig)) {5111tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));5112tty->print("expected:" PTR32_FORMAT, os::Linux::get_our_sigflags(sig));5113tty->print_cr(" found:" PTR32_FORMAT, act.sa_flags);5114// No need to check this sig any longer5115sigaddset(&check_signal_done, sig);5116}51175118// Dump all the signal5119if (sigismember(&check_signal_done, sig)) {5120print_signal_handlers(tty, buf, O_BUFLEN);5121}5122}51235124extern void report_error(char* file_name, int line_no, char* title, char* format, ...);51255126extern bool signal_name(int signo, char* buf, size_t len);51275128const char* os::exception_name(int exception_code, char* buf, size_t size) {5129if (0 < exception_code && exception_code <= SIGRTMAX) {5130// signal5131if (!signal_name(exception_code, buf, size)) {5132jio_snprintf(buf, size, "SIG%d", exception_code);5133}5134return buf;5135} else {5136return NULL;5137}5138}51395140// this is called _before_ most of the global arguments have been parsed5141void os::init(void) {5142char dummy; /* used to get a guess on initial stack address */51435144// With LinuxThreads the JavaMain thread pid (primordial thread)5145// is different than the pid of the java launcher thread.5146// So, on Linux, the launcher thread pid is passed to the VM5147// via the sun.java.launcher.pid property.5148// Use this property instead of getpid() if it was correctly passed.5149// See bug 6351349.5150pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();51515152_initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();51535154clock_tics_per_sec = sysconf(_SC_CLK_TCK);51555156init_random(1234567);51575158ThreadCritical::initialize();51595160Linux::set_page_size(sysconf(_SC_PAGESIZE));5161if (Linux::page_size() == -1) {5162fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",5163strerror(errno)));5164}5165init_page_sizes((size_t) Linux::page_size());51665167Linux::initialize_system_info();51685169// _main_thread points to the thread that created/loaded the JVM.5170Linux::_main_thread = pthread_self();51715172Linux::clock_init();5173initial_time_count = javaTimeNanos();51745175// pthread_condattr initialization for monotonic clock5176int status;5177pthread_condattr_t* _condattr = os::Linux::condAttr();5178if ((status = pthread_condattr_init(_condattr)) != 0) {5179fatal(err_msg("pthread_condattr_init: %s", strerror(status)));5180}5181// Only set the clock if CLOCK_MONOTONIC is available5182if (Linux::supports_monotonic_clock()) {5183if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) {5184if (status == EINVAL) {5185warning("Unable to use monotonic clock with relative timed-waits" \5186" - changes to the time-of-day clock may have adverse affects");5187} else {5188fatal(err_msg("pthread_condattr_setclock: %s", strerror(status)));5189}5190}5191}5192// else it defaults to CLOCK_REALTIME51935194pthread_mutex_init(&dl_mutex, NULL);51955196// If the pagesize of the VM is greater than 8K determine the appropriate5197// number of initial guard pages. The user can change this with the5198// command line arguments, if needed.5199if (vm_page_size() > (int)Linux::vm_default_page_size()) {5200StackYellowPages = 1;5201StackRedPages = 1;5202StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size();5203}52045205// retrieve entry point for pthread_setname_np5206Linux::_pthread_setname_np =5207(int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");52085209}52105211// To install functions for atexit system call5212extern "C" {5213static void perfMemory_exit_helper() {5214perfMemory_exit();5215}5216}52175218void os::pd_init_container_support() {5219OSContainer::init();5220}52215222// this is called _after_ the global arguments have been parsed5223jint os::init_2(void)5224{5225Linux::fast_thread_clock_init();52265227// Allocate a single page and mark it as readable for safepoint polling5228address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);5229guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" );52305231os::set_polling_page( polling_page );52325233#ifndef PRODUCT5234if(Verbose && PrintMiscellaneous)5235tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);5236#endif52375238if (!UseMembar) {5239address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);5240guarantee( mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");5241os::set_memory_serialize_page( mem_serialize_page );52425243#ifndef PRODUCT5244if(Verbose && PrintMiscellaneous)5245tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);5246#endif5247}52485249// initialize suspend/resume support - must do this before signal_sets_init()5250if (SR_initialize() != 0) {5251perror("SR_initialize failed");5252return JNI_ERR;5253}52545255Linux::signal_sets_init();5256Linux::install_signal_handlers();52575258// Check minimum allowable stack size for thread creation and to initialize5259// the java system classes, including StackOverflowError - depends on page5260// size. Add a page for compiler2 recursion in main thread.5261// Add in 2*BytesPerWord times page size to account for VM stack during5262// class initialization depending on 32 or 64 bit VM.5263os::Linux::min_stack_allowed = MAX2(os::Linux::min_stack_allowed,5264(size_t)(StackYellowPages+StackRedPages+StackShadowPages) * Linux::page_size() +5265(2*BytesPerWord COMPILER2_PRESENT(+1)) * Linux::vm_default_page_size());52665267size_t threadStackSizeInBytes = ThreadStackSize * K;5268if (threadStackSizeInBytes != 0 &&5269threadStackSizeInBytes < os::Linux::min_stack_allowed) {5270tty->print_cr("\nThe stack size specified is too small, "5271"Specify at least %dk",5272os::Linux::min_stack_allowed/ K);5273return JNI_ERR;5274}52755276// Make the stack size a multiple of the page size so that5277// the yellow/red zones can be guarded.5278JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,5279vm_page_size()));52805281Linux::capture_initial_stack(JavaThread::stack_size_at_create());52825283#if defined(IA32) && !defined(ZERO)5284workaround_expand_exec_shield_cs_limit();5285#endif52865287Linux::libpthread_init();5288if (PrintMiscellaneous && (Verbose || WizardMode)) {5289tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",5290Linux::glibc_version(), Linux::libpthread_version(),5291Linux::is_floating_stack() ? "floating stack" : "fixed stack");5292}52935294if (UseNUMA) {5295if (!Linux::libnuma_init()) {5296UseNUMA = false;5297} else {5298if ((Linux::numa_max_node() < 1)) {5299// There's only one node(they start from 0), disable NUMA.5300UseNUMA = false;5301}5302}5303// With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way5304// we can make the adaptive lgrp chunk resizing work. If the user specified5305// both UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn and5306// disable adaptive resizing.5307if (UseNUMA && UseLargePages && !can_commit_large_page_memory()) {5308if (FLAG_IS_DEFAULT(UseNUMA)) {5309UseNUMA = false;5310} else {5311if (FLAG_IS_DEFAULT(UseLargePages) &&5312FLAG_IS_DEFAULT(UseSHM) &&5313FLAG_IS_DEFAULT(UseHugeTLBFS)) {5314UseLargePages = false;5315} else {5316warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, disabling adaptive resizing");5317UseAdaptiveSizePolicy = false;5318UseAdaptiveNUMAChunkSizing = false;5319}5320}5321}5322if (!UseNUMA && ForceNUMA) {5323UseNUMA = true;5324}5325}53265327if (MaxFDLimit) {5328// set the number of file descriptors to max. print out error5329// if getrlimit/setrlimit fails but continue regardless.5330struct rlimit nbr_files;5331int status = getrlimit(RLIMIT_NOFILE, &nbr_files);5332if (status != 0) {5333if (PrintMiscellaneous && (Verbose || WizardMode))5334perror("os::init_2 getrlimit failed");5335} else {5336nbr_files.rlim_cur = nbr_files.rlim_max;5337status = setrlimit(RLIMIT_NOFILE, &nbr_files);5338if (status != 0) {5339if (PrintMiscellaneous && (Verbose || WizardMode))5340perror("os::init_2 setrlimit failed");5341}5342}5343}53445345// Initialize lock used to serialize thread creation (see os::create_thread)5346Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));53475348// at-exit methods are called in the reverse order of their registration.5349// atexit functions are called on return from main or as a result of a5350// call to exit(3C). There can be only 32 of these functions registered5351// and atexit() does not set errno.53525353if (PerfAllowAtExitRegistration) {5354// only register atexit functions if PerfAllowAtExitRegistration is set.5355// atexit functions can be delayed until process exit time, which5356// can be problematic for embedded VM situations. Embedded VMs should5357// call DestroyJavaVM() to assure that VM resources are released.53585359// note: perfMemory_exit_helper atexit function may be removed in5360// the future if the appropriate cleanup code can be added to the5361// VM_Exit VMOperation's doit method.5362if (atexit(perfMemory_exit_helper) != 0) {5363warning("os::init_2 atexit(perfMemory_exit_helper) failed");5364}5365}53665367// initialize thread priority policy5368prio_init();53695370return JNI_OK;5371}53725373// Mark the polling page as unreadable5374void os::make_polling_page_unreadable(void) {5375if( !guard_memory((char*)_polling_page, Linux::page_size()) )5376fatal("Could not disable polling page");5377};53785379// Mark the polling page as readable5380void os::make_polling_page_readable(void) {5381if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {5382fatal("Could not enable polling page");5383}5384};53855386static int os_cpu_count(const cpu_set_t* cpus) {5387int count = 0;5388// only look up to the number of configured processors5389for (int i = 0; i < os::processor_count(); i++) {5390if (CPU_ISSET(i, cpus)) {5391count++;5392}5393}5394return count;5395}53965397// Get the current number of available processors for this process.5398// This value can change at any time during a process's lifetime.5399// sched_getaffinity gives an accurate answer as it accounts for cpusets.5400// If anything goes wrong we fallback to returning the number of online5401// processors - which can be greater than the number available to the process.5402int os::Linux::active_processor_count() {5403cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors5404int cpus_size = sizeof(cpu_set_t);5405int cpu_count = 0;54065407// pid 0 means the current thread - which we have to assume represents the process5408if (sched_getaffinity(0, cpus_size, &cpus) == 0) {5409cpu_count = os_cpu_count(&cpus);5410if (PrintActiveCpus) {5411tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);5412}5413}5414else {5415cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);5416warning("sched_getaffinity failed (%s)- using online processor count (%d) "5417"which may exceed available processors", strerror(errno), cpu_count);5418}54195420assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");5421return cpu_count;5422}54235424// Determine the active processor count from one of5425// three different sources:5426//5427// 1. User option -XX:ActiveProcessorCount5428// 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)5429// 3. extracted from cgroup cpu subsystem (shares and quotas)5430//5431// Option 1, if specified, will always override.5432// If the cgroup subsystem is active and configured, we5433// will return the min of the cgroup and option 2 results.5434// This is required since tools, such as numactl, that5435// alter cpu affinity do not update cgroup subsystem5436// cpuset configuration files.5437int os::active_processor_count() {5438// User has overridden the number of active processors5439if (ActiveProcessorCount > 0) {5440if (PrintActiveCpus) {5441tty->print_cr("active_processor_count: "5442"active processor count set by user : %d",5443ActiveProcessorCount);5444}5445return ActiveProcessorCount;5446}54475448int active_cpus;5449if (OSContainer::is_containerized()) {5450active_cpus = OSContainer::active_processor_count();5451if (PrintActiveCpus) {5452tty->print_cr("active_processor_count: determined by OSContainer: %d",5453active_cpus);5454}5455} else {5456active_cpus = os::Linux::active_processor_count();5457}54585459return active_cpus;5460}54615462void os::set_native_thread_name(const char *name) {5463if (Linux::_pthread_setname_np) {5464char buf [16]; // according to glibc manpage, 16 chars incl. '/0'5465snprintf(buf, sizeof(buf), "%s", name);5466buf[sizeof(buf) - 1] = '\0';5467const int rc = Linux::_pthread_setname_np(pthread_self(), buf);5468// ERANGE should not happen; all other errors should just be ignored.5469assert(rc != ERANGE, "pthread_setname_np failed");5470}5471}54725473bool os::distribute_processes(uint length, uint* distribution) {5474// Not yet implemented.5475return false;5476}54775478bool os::bind_to_processor(uint processor_id) {5479// Not yet implemented.5480return false;5481}54825483///54845485void os::SuspendedThreadTask::internal_do_task() {5486if (do_suspend(_thread->osthread())) {5487SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());5488do_task(context);5489do_resume(_thread->osthread());5490}5491}54925493class PcFetcher : public os::SuspendedThreadTask {5494public:5495PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}5496ExtendedPC result();5497protected:5498void do_task(const os::SuspendedThreadTaskContext& context);5499private:5500ExtendedPC _epc;5501};55025503ExtendedPC PcFetcher::result() {5504guarantee(is_done(), "task is not done yet.");5505return _epc;5506}55075508void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {5509Thread* thread = context.thread();5510OSThread* osthread = thread->osthread();5511if (osthread->ucontext() != NULL) {5512_epc = os::Linux::ucontext_get_pc((ucontext_t *) context.ucontext());5513} else {5514// NULL context is unexpected, double-check this is the VMThread5515guarantee(thread->is_VM_thread(), "can only be called for VMThread");5516}5517}55185519// Suspends the target using the signal mechanism and then grabs the PC before5520// resuming the target. Used by the flat-profiler only5521ExtendedPC os::get_thread_pc(Thread* thread) {5522// Make sure that it is called by the watcher for the VMThread5523assert(Thread::current()->is_Watcher_thread(), "Must be watcher");5524assert(thread->is_VM_thread(), "Can only be called for VMThread");55255526PcFetcher fetcher(thread);5527fetcher.run();5528return fetcher.result();5529}55305531int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)5532{5533if (is_NPTL()) {5534return pthread_cond_timedwait(_cond, _mutex, _abstime);5535} else {5536// 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control5537// word back to default 64bit precision if condvar is signaled. Java5538// wants 53bit precision. Save and restore current value.5539int fpu = get_fpu_control_word();5540int status = pthread_cond_timedwait(_cond, _mutex, _abstime);5541set_fpu_control_word(fpu);5542return status;5543}5544}55455546////////////////////////////////////////////////////////////////////////////////5547// debug support55485549bool os::find(address addr, outputStream* st) {5550Dl_info dlinfo;5551memset(&dlinfo, 0, sizeof(dlinfo));5552if (dladdr(addr, &dlinfo) != 0) {5553st->print(PTR_FORMAT ": ", addr);5554if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) {5555st->print("%s+%#x", dlinfo.dli_sname,5556addr - (intptr_t)dlinfo.dli_saddr);5557} else if (dlinfo.dli_fbase != NULL) {5558st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);5559} else {5560st->print("<absolute address>");5561}5562if (dlinfo.dli_fname != NULL) {5563st->print(" in %s", dlinfo.dli_fname);5564}5565if (dlinfo.dli_fbase != NULL) {5566st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);5567}5568st->cr();55695570if (Verbose) {5571// decode some bytes around the PC5572address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());5573address end = clamp_address_in_page(addr+40, addr, os::vm_page_size());5574address lowest = (address) dlinfo.dli_sname;5575if (!lowest) lowest = (address) dlinfo.dli_fbase;5576if (begin < lowest) begin = lowest;5577Dl_info dlinfo2;5578if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr5579&& end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)5580end = (address) dlinfo2.dli_saddr;5581Disassembler::decode(begin, end, st);5582}5583return true;5584}5585return false;5586}55875588////////////////////////////////////////////////////////////////////////////////5589// misc55905591// This does not do anything on Linux. This is basically a hook for being5592// able to use structured exception handling (thread-local exception filters)5593// on, e.g., Win32.5594void5595os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,5596JavaCallArguments* args, Thread* thread) {5597f(value, method, args, thread);5598}55995600void os::print_statistics() {5601}56025603int os::message_box(const char* title, const char* message) {5604int i;5605fdStream err(defaultStream::error_fd());5606for (i = 0; i < 78; i++) err.print_raw("=");5607err.cr();5608err.print_raw_cr(title);5609for (i = 0; i < 78; i++) err.print_raw("-");5610err.cr();5611err.print_raw_cr(message);5612for (i = 0; i < 78; i++) err.print_raw("=");5613err.cr();56145615char buf[16];5616// Prevent process from exiting upon "read error" without consuming all CPU5617while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }56185619return buf[0] == 'y' || buf[0] == 'Y';5620}56215622int os::stat(const char *path, struct stat *sbuf) {5623char pathbuf[MAX_PATH];5624if (strlen(path) > MAX_PATH - 1) {5625errno = ENAMETOOLONG;5626return -1;5627}5628os::native_path(strcpy(pathbuf, path));5629return ::stat(pathbuf, sbuf);5630}56315632bool os::check_heap(bool force) {5633return true;5634}56355636int local_vsnprintf(char* buf, size_t count, const char* format, va_list args) {5637return ::vsnprintf(buf, count, format, args);5638}56395640// Is a (classpath) directory empty?5641bool os::dir_is_empty(const char* path) {5642DIR *dir = NULL;5643struct dirent *ptr;56445645dir = opendir(path);5646if (dir == NULL) return true;56475648/* Scan the directory */5649bool result = true;5650while (result && (ptr = readdir(dir)) != NULL) {5651if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {5652result = false;5653}5654}5655closedir(dir);5656return result;5657}56585659// This code originates from JDK's sysOpen and open64_w5660// from src/solaris/hpi/src/system_md.c56615662#ifndef O_DELETE5663#define O_DELETE 0x100005664#endif56655666#ifdef __ANDROID__5667int open64(const char* pathName, int flags, int mode) {5668return ::open(pathName, flags, mode);5669}5670#endif //__ANDROID__56715672// Open a file. Unlink the file immediately after open returns5673// if the specified oflag has the O_DELETE flag set.5674// O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c56755676int os::open(const char *path, int oflag, int mode) {56775678if (strlen(path) > MAX_PATH - 1) {5679errno = ENAMETOOLONG;5680return -1;5681}5682int fd;5683int o_delete = (oflag & O_DELETE);5684oflag = oflag & ~O_DELETE;56855686fd = ::open64(path, oflag, mode);5687if (fd == -1) return -1;56885689//If the open succeeded, the file might still be a directory5690{5691struct stat64 buf64;5692int ret = ::fstat64(fd, &buf64);5693int st_mode = buf64.st_mode;56945695if (ret != -1) {5696if ((st_mode & S_IFMT) == S_IFDIR) {5697errno = EISDIR;5698::close(fd);5699return -1;5700}5701} else {5702::close(fd);5703return -1;5704}5705}57065707/*5708* All file descriptors that are opened in the JVM and not5709* specifically destined for a subprocess should have the5710* close-on-exec flag set. If we don't set it, then careless 3rd5711* party native code might fork and exec without closing all5712* appropriate file descriptors (e.g. as we do in closeDescriptors in5713* UNIXProcess.c), and this in turn might:5714*5715* - cause end-of-file to fail to be detected on some file5716* descriptors, resulting in mysterious hangs, or5717*5718* - might cause an fopen in the subprocess to fail on a system5719* suffering from bug 1085341.5720*5721* (Yes, the default setting of the close-on-exec flag is a Unix5722* design flaw)5723*5724* See:5725* 1085341: 32-bit stdio routines should support file descriptors >2555726* 4843136: (process) pipe file descriptor from Runtime.exec not being closed5727* 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 95728*/5729#ifdef FD_CLOEXEC5730{5731int flags = ::fcntl(fd, F_GETFD);5732if (flags != -1)5733::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);5734}5735#endif57365737if (o_delete != 0) {5738::unlink(path);5739}5740return fd;5741}57425743#ifdef __ANDROID__5744#define S_IREAD S_IRUSR5745#define S_IWRITE S_IWUSR5746#endif5747// create binary file, rewriting existing file if required5748int os::create_binary_file(const char* path, bool rewrite_existing) {5749int oflags = O_WRONLY | O_CREAT;5750if (!rewrite_existing) {5751oflags |= O_EXCL;5752}5753return ::open64(path, oflags, S_IREAD | S_IWRITE);5754}57555756// return current position of file pointer5757jlong os::current_file_offset(int fd) {5758return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);5759}57605761// move file pointer to the specified offset5762jlong os::seek_to_file_offset(int fd, jlong offset) {5763return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);5764}57655766// This code originates from JDK's sysAvailable5767// from src/solaris/hpi/src/native_threads/src/sys_api_td.c57685769int os::available(int fd, jlong *bytes) {5770jlong cur, end;5771int mode;5772struct stat64 buf64;57735774if (::fstat64(fd, &buf64) >= 0) {5775mode = buf64.st_mode;5776if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {5777/*5778* XXX: is the following call interruptible? If so, this might5779* need to go through the INTERRUPT_IO() wrapper as for other5780* blocking, interruptible calls in this file.5781*/5782int n;5783if (::ioctl(fd, FIONREAD, &n) >= 0) {5784*bytes = n;5785return 1;5786}5787}5788}5789if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {5790return 0;5791} else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {5792return 0;5793} else if (::lseek64(fd, cur, SEEK_SET) == -1) {5794return 0;5795}5796*bytes = end - cur;5797return 1;5798}57995800int os::socket_available(int fd, jint *pbytes) {5801// Linux doc says EINTR not returned, unlike Solaris5802int ret = ::ioctl(fd, FIONREAD, pbytes);58035804//%% note ioctl can return 0 when successful, JVM_SocketAvailable5805// is expected to return 0 on failure and 1 on success to the jdk.5806return (ret < 0) ? 0 : 1;5807}58085809// Map a block of memory.5810char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,5811char *addr, size_t bytes, bool read_only,5812bool allow_exec) {5813int prot;5814int flags = MAP_PRIVATE;58155816if (read_only) {5817prot = PROT_READ;5818} else {5819prot = PROT_READ | PROT_WRITE;5820}58215822if (allow_exec) {5823prot |= PROT_EXEC;5824}58255826if (addr != NULL) {5827flags |= MAP_FIXED;5828}58295830char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,5831fd, file_offset);5832if (mapped_address == MAP_FAILED) {5833return NULL;5834}5835return mapped_address;5836}583758385839// Remap a block of memory.5840char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,5841char *addr, size_t bytes, bool read_only,5842bool allow_exec) {5843// same as map_memory() on this OS5844return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,5845allow_exec);5846}584758485849// Unmap a block of memory.5850bool os::pd_unmap_memory(char* addr, size_t bytes) {5851return munmap(addr, bytes) == 0;5852}58535854static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);58555856static clockid_t thread_cpu_clockid(Thread* thread) {5857pthread_t tid = thread->osthread()->pthread_id();5858clockid_t clockid;58595860// Get thread clockid5861int rc = os::Linux::pthread_getcpuclockid(tid, &clockid);5862assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");5863return clockid;5864}58655866// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)5867// are used by JVM M&M and JVMTI to get user+sys or user CPU time5868// of a thread.5869//5870// current_thread_cpu_time() and thread_cpu_time(Thread*) returns5871// the fast estimate available on the platform.58725873jlong os::current_thread_cpu_time() {5874if (os::Linux::supports_fast_thread_cpu_time()) {5875return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);5876} else {5877// return user + sys since the cost is the same5878return slow_thread_cpu_time(Thread::current(), true /* user + sys */);5879}5880}58815882jlong os::thread_cpu_time(Thread* thread) {5883// consistent with what current_thread_cpu_time() returns5884if (os::Linux::supports_fast_thread_cpu_time()) {5885return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));5886} else {5887return slow_thread_cpu_time(thread, true /* user + sys */);5888}5889}58905891jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {5892if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {5893return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);5894} else {5895return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);5896}5897}58985899jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {5900if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {5901return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));5902} else {5903return slow_thread_cpu_time(thread, user_sys_cpu_time);5904}5905}59065907//5908// -1 on error.5909//59105911PRAGMA_DIAG_PUSH5912PRAGMA_FORMAT_NONLITERAL_IGNORED5913static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {5914pid_t tid = thread->osthread()->thread_id();5915char *s;5916char stat[2048];5917int statlen;5918char proc_name[64];5919int count;5920long sys_time, user_time;5921char cdummy;5922int idummy;5923long ldummy;5924FILE *fp;59255926snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);5927fp = fopen(proc_name, "r");5928if ( fp == NULL ) return -1;5929statlen = fread(stat, 1, 2047, fp);5930stat[statlen] = '\0';5931fclose(fp);59325933// Skip pid and the command string. Note that we could be dealing with5934// weird command names, e.g. user could decide to rename java launcher5935// to "java 1.4.2 :)", then the stat file would look like5936// 1234 (java 1.4.2 :)) R ... ...5937// We don't really need to know the command string, just find the last5938// occurrence of ")" and then start parsing from there. See bug 4726580.5939s = strrchr(stat, ')');5940if (s == NULL ) return -1;59415942// Skip blank chars5943do s++; while (isspace(*s));59445945count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",5946&cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,5947&ldummy, &ldummy, &ldummy, &ldummy, &ldummy,5948&user_time, &sys_time);5949if ( count != 13 ) return -1;5950if (user_sys_cpu_time) {5951return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);5952} else {5953return (jlong)user_time * (1000000000 / clock_tics_per_sec);5954}5955}5956PRAGMA_DIAG_POP59575958void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {5959info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits5960info_ptr->may_skip_backward = false; // elapsed time not wall time5961info_ptr->may_skip_forward = false; // elapsed time not wall time5962info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned5963}59645965void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {5966info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits5967info_ptr->may_skip_backward = false; // elapsed time not wall time5968info_ptr->may_skip_forward = false; // elapsed time not wall time5969info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned5970}59715972bool os::is_thread_cpu_time_supported() {5973return true;5974}59755976// System loadavg support. Returns -1 if load average cannot be obtained.5977// Linux doesn't yet have a (official) notion of processor sets,5978// so just return the system wide load average.5979int os::loadavg(double loadavg[], int nelem) {5980#if !defined(__UCLIBC__) && !defined(__ANDROID__)5981return ::getloadavg(loadavg, nelem);5982#else5983return -1;5984#endif5985}59865987void os::pause() {5988char filename[MAX_PATH];5989if (PauseAtStartupFile && PauseAtStartupFile[0]) {5990jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);5991} else {5992jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());5993}59945995int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);5996if (fd != -1) {5997struct stat buf;5998::close(fd);5999while (::stat(filename, &buf) == 0) {6000(void)::poll(NULL, 0, 100);6001}6002} else {6003jio_fprintf(stderr,6004"Could not open pause file '%s', continuing immediately.\n", filename);6005}6006}600760086009// Refer to the comments in os_solaris.cpp park-unpark.6010//6011// Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can6012// hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable.6013// For specifics regarding the bug see GLIBC BUGID 261237 :6014// http://www.mail-archive.com/[email protected]/msg10837.html.6015// Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future6016// will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar6017// is used. (The simple C test-case provided in the GLIBC bug report manifests the6018// hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos()6019// and monitorenter when we're using 1-0 locking. All those operations may result in6020// calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version6021// of libpthread avoids the problem, but isn't practical.6022//6023// Possible remedies:6024//6025// 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work.6026// This is palliative and probabilistic, however. If the thread is preempted6027// between the call to compute_abstime() and pthread_cond_timedwait(), more6028// than the minimum period may have passed, and the abstime may be stale (in the6029// past) resultin in a hang. Using this technique reduces the odds of a hang6030// but the JVM is still vulnerable, particularly on heavily loaded systems.6031//6032// 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead6033// of the usual flag-condvar-mutex idiom. The write side of the pipe is set6034// NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo)6035// reduces to poll()+read(). This works well, but consumes 2 FDs per extant6036// thread.6037//6038// 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread6039// that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing6040// a timeout request to the chron thread and then blocking via pthread_cond_wait().6041// This also works well. In fact it avoids kernel-level scalability impediments6042// on certain platforms that don't handle lots of active pthread_cond_timedwait()6043// timers in a graceful fashion.6044//6045// 4. When the abstime value is in the past it appears that control returns6046// correctly from pthread_cond_timedwait(), but the condvar is left corrupt.6047// Subsequent timedwait/wait calls may hang indefinitely. Given that, we6048// can avoid the problem by reinitializing the condvar -- by cond_destroy()6049// followed by cond_init() -- after all calls to pthread_cond_timedwait().6050// It may be possible to avoid reinitialization by checking the return6051// value from pthread_cond_timedwait(). In addition to reinitializing the6052// condvar we must establish the invariant that cond_signal() is only called6053// within critical sections protected by the adjunct mutex. This prevents6054// cond_signal() from "seeing" a condvar that's in the midst of being6055// reinitialized or that is corrupt. Sadly, this invariant obviates the6056// desirable signal-after-unlock optimization that avoids futile context switching.6057//6058// I'm also concerned that some versions of NTPL might allocate an auxilliary6059// structure when a condvar is used or initialized. cond_destroy() would6060// release the helper structure. Our reinitialize-after-timedwait fix6061// put excessive stress on malloc/free and locks protecting the c-heap.6062//6063// We currently use (4). See the WorkAroundNTPLTimedWaitHang flag.6064// It may be possible to refine (4) by checking the kernel and NTPL verisons6065// and only enabling the work-around for vulnerable environments.60666067// utility to compute the abstime argument to timedwait:6068// millis is the relative timeout time6069// abstime will be the absolute timeout time6070// TODO: replace compute_abstime() with unpackTime()60716072static struct timespec* compute_abstime(timespec* abstime, jlong millis) {6073if (millis < 0) millis = 0;60746075jlong seconds = millis / 1000;6076millis %= 1000;6077if (seconds > 50000000) { // see man cond_timedwait(3T)6078seconds = 50000000;6079}60806081if (os::Linux::supports_monotonic_clock()) {6082struct timespec now;6083int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);6084assert_status(status == 0, status, "clock_gettime");6085abstime->tv_sec = now.tv_sec + seconds;6086long nanos = now.tv_nsec + millis * NANOSECS_PER_MILLISEC;6087if (nanos >= NANOSECS_PER_SEC) {6088abstime->tv_sec += 1;6089nanos -= NANOSECS_PER_SEC;6090}6091abstime->tv_nsec = nanos;6092} else {6093struct timeval now;6094int status = gettimeofday(&now, NULL);6095assert(status == 0, "gettimeofday");6096abstime->tv_sec = now.tv_sec + seconds;6097long usec = now.tv_usec + millis * 1000;6098if (usec >= 1000000) {6099abstime->tv_sec += 1;6100usec -= 1000000;6101}6102abstime->tv_nsec = usec * 1000;6103}6104return abstime;6105}610661076108// Test-and-clear _Event, always leaves _Event set to 0, returns immediately.6109// Conceptually TryPark() should be equivalent to park(0).61106111int os::PlatformEvent::TryPark() {6112for (;;) {6113const int v = _Event ;6114guarantee ((v == 0) || (v == 1), "invariant") ;6115if (Atomic::cmpxchg (0, &_Event, v) == v) return v ;6116}6117}61186119void os::PlatformEvent::park() { // AKA "down()"6120// Invariant: Only the thread associated with the Event/PlatformEvent6121// may call park().6122// TODO: assert that _Assoc != NULL or _Assoc == Self6123int v ;6124for (;;) {6125v = _Event ;6126if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;6127}6128guarantee (v >= 0, "invariant") ;6129if (v == 0) {6130// Do this the hard way by blocking ...6131int status = pthread_mutex_lock(_mutex);6132assert_status(status == 0, status, "mutex_lock");6133guarantee (_nParked == 0, "invariant") ;6134++ _nParked ;6135while (_Event < 0) {6136status = pthread_cond_wait(_cond, _mutex);6137// for some reason, under 2.7 lwp_cond_wait() may return ETIME ...6138// Treat this the same as if the wait was interrupted6139if (status == ETIME) { status = EINTR; }6140assert_status(status == 0 || status == EINTR, status, "cond_wait");6141}6142-- _nParked ;61436144_Event = 0 ;6145status = pthread_mutex_unlock(_mutex);6146assert_status(status == 0, status, "mutex_unlock");6147// Paranoia to ensure our locked and lock-free paths interact6148// correctly with each other.6149OrderAccess::fence();6150}6151guarantee (_Event >= 0, "invariant") ;6152}61536154int os::PlatformEvent::park(jlong millis) {6155guarantee (_nParked == 0, "invariant") ;61566157int v ;6158for (;;) {6159v = _Event ;6160if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;6161}6162guarantee (v >= 0, "invariant") ;6163if (v != 0) return OS_OK ;61646165// We do this the hard way, by blocking the thread.6166// Consider enforcing a minimum timeout value.6167struct timespec abst;6168compute_abstime(&abst, millis);61696170int ret = OS_TIMEOUT;6171int status = pthread_mutex_lock(_mutex);6172assert_status(status == 0, status, "mutex_lock");6173guarantee (_nParked == 0, "invariant") ;6174++_nParked ;61756176// Object.wait(timo) will return because of6177// (a) notification6178// (b) timeout6179// (c) thread.interrupt6180//6181// Thread.interrupt and object.notify{All} both call Event::set.6182// That is, we treat thread.interrupt as a special case of notification.6183// The underlying Solaris implementation, cond_timedwait, admits6184// spurious/premature wakeups, but the JLS/JVM spec prevents the6185// JVM from making those visible to Java code. As such, we must6186// filter out spurious wakeups. We assume all ETIME returns are valid.6187//6188// TODO: properly differentiate simultaneous notify+interrupt.6189// In that case, we should propagate the notify to another waiter.61906191while (_Event < 0) {6192status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);6193if (status != 0 && WorkAroundNPTLTimedWaitHang) {6194pthread_cond_destroy (_cond);6195pthread_cond_init (_cond, os::Linux::condAttr()) ;6196}6197assert_status(status == 0 || status == EINTR ||6198status == ETIME || status == ETIMEDOUT,6199status, "cond_timedwait");6200if (!FilterSpuriousWakeups) break ; // previous semantics6201if (status == ETIME || status == ETIMEDOUT) break ;6202// We consume and ignore EINTR and spurious wakeups.6203}6204--_nParked ;6205if (_Event >= 0) {6206ret = OS_OK;6207}6208_Event = 0 ;6209status = pthread_mutex_unlock(_mutex);6210assert_status(status == 0, status, "mutex_unlock");6211assert (_nParked == 0, "invariant") ;6212// Paranoia to ensure our locked and lock-free paths interact6213// correctly with each other.6214OrderAccess::fence();6215return ret;6216}62176218void os::PlatformEvent::unpark() {6219// Transitions for _Event:6220// 0 :=> 16221// 1 :=> 16222// -1 :=> either 0 or 1; must signal target thread6223// That is, we can safely transition _Event from -1 to either6224// 0 or 1. Forcing 1 is slightly more efficient for back-to-back6225// unpark() calls.6226// See also: "Semaphores in Plan 9" by Mullender & Cox6227//6228// Note: Forcing a transition from "-1" to "1" on an unpark() means6229// that it will take two back-to-back park() calls for the owning6230// thread to block. This has the benefit of forcing a spurious return6231// from the first park() call after an unpark() call which will help6232// shake out uses of park() and unpark() without condition variables.62336234if (Atomic::xchg(1, &_Event) >= 0) return;62356236// Wait for the thread associated with the event to vacate6237int status = pthread_mutex_lock(_mutex);6238assert_status(status == 0, status, "mutex_lock");6239int AnyWaiters = _nParked;6240assert(AnyWaiters == 0 || AnyWaiters == 1, "invariant");6241if (AnyWaiters != 0 && WorkAroundNPTLTimedWaitHang) {6242AnyWaiters = 0;6243pthread_cond_signal(_cond);6244}6245status = pthread_mutex_unlock(_mutex);6246assert_status(status == 0, status, "mutex_unlock");6247if (AnyWaiters != 0) {6248status = pthread_cond_signal(_cond);6249assert_status(status == 0, status, "cond_signal");6250}62516252// Note that we signal() _after dropping the lock for "immortal" Events.6253// This is safe and avoids a common class of futile wakeups. In rare6254// circumstances this can cause a thread to return prematurely from6255// cond_{timed}wait() but the spurious wakeup is benign and the victim will6256// simply re-test the condition and re-park itself.6257}625862596260// JSR1666261// -------------------------------------------------------62626263/*6264* The solaris and linux implementations of park/unpark are fairly6265* conservative for now, but can be improved. They currently use a6266* mutex/condvar pair, plus a a count.6267* Park decrements count if > 0, else does a condvar wait. Unpark6268* sets count to 1 and signals condvar. Only one thread ever waits6269* on the condvar. Contention seen when trying to park implies that someone6270* is unparking you, so don't wait. And spurious returns are fine, so there6271* is no need to track notifications.6272*/62736274/*6275* This code is common to linux and solaris and will be moved to a6276* common place in dolphin.6277*6278* The passed in time value is either a relative time in nanoseconds6279* or an absolute time in milliseconds. Either way it has to be unpacked6280* into suitable seconds and nanoseconds components and stored in the6281* given timespec structure.6282* Given time is a 64-bit value and the time_t used in the timespec is only6283* a signed-32-bit value (except on 64-bit Linux) we have to watch for6284* overflow if times way in the future are given. Further on Solaris versions6285* prior to 10 there is a restriction (see cond_timedwait) that the specified6286* number of seconds, in abstime, is less than current_time + 100,000,000.6287* As it will be 28 years before "now + 100000000" will overflow we can6288* ignore overflow and just impose a hard-limit on seconds using the value6289* of "now + 100,000,000". This places a limit on the timeout of about 3.176290* years from "now".6291*/62926293static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {6294assert (time > 0, "convertTime");6295time_t max_secs = 0;62966297if (!os::Linux::supports_monotonic_clock() || isAbsolute) {6298struct timeval now;6299int status = gettimeofday(&now, NULL);6300assert(status == 0, "gettimeofday");63016302max_secs = now.tv_sec + MAX_SECS;63036304if (isAbsolute) {6305jlong secs = time / 1000;6306if (secs > max_secs) {6307absTime->tv_sec = max_secs;6308} else {6309absTime->tv_sec = secs;6310}6311absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;6312} else {6313jlong secs = time / NANOSECS_PER_SEC;6314if (secs >= MAX_SECS) {6315absTime->tv_sec = max_secs;6316absTime->tv_nsec = 0;6317} else {6318absTime->tv_sec = now.tv_sec + secs;6319absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;6320if (absTime->tv_nsec >= NANOSECS_PER_SEC) {6321absTime->tv_nsec -= NANOSECS_PER_SEC;6322++absTime->tv_sec; // note: this must be <= max_secs6323}6324}6325}6326} else {6327// must be relative using monotonic clock6328struct timespec now;6329int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);6330assert_status(status == 0, status, "clock_gettime");6331max_secs = now.tv_sec + MAX_SECS;6332jlong secs = time / NANOSECS_PER_SEC;6333if (secs >= MAX_SECS) {6334absTime->tv_sec = max_secs;6335absTime->tv_nsec = 0;6336} else {6337absTime->tv_sec = now.tv_sec + secs;6338absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_nsec;6339if (absTime->tv_nsec >= NANOSECS_PER_SEC) {6340absTime->tv_nsec -= NANOSECS_PER_SEC;6341++absTime->tv_sec; // note: this must be <= max_secs6342}6343}6344}6345assert(absTime->tv_sec >= 0, "tv_sec < 0");6346assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");6347assert(absTime->tv_nsec >= 0, "tv_nsec < 0");6348assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");6349}63506351void Parker::park(bool isAbsolute, jlong time) {6352// Ideally we'd do something useful while spinning, such6353// as calling unpackTime().63546355// Optional fast-path check:6356// Return immediately if a permit is available.6357// We depend on Atomic::xchg() having full barrier semantics6358// since we are doing a lock-free update to _counter.6359if (Atomic::xchg(0, &_counter) > 0) return;63606361Thread* thread = Thread::current();6362assert(thread->is_Java_thread(), "Must be JavaThread");6363JavaThread *jt = (JavaThread *)thread;63646365// Optional optimization -- avoid state transitions if there's an interrupt pending.6366// Check interrupt before trying to wait6367if (Thread::is_interrupted(thread, false)) {6368return;6369}63706371// Next, demultiplex/decode time arguments6372timespec absTime;6373if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all6374return;6375}6376if (time > 0) {6377unpackTime(&absTime, isAbsolute, time);6378}637963806381// Enter safepoint region6382// Beware of deadlocks such as 6317397.6383// The per-thread Parker:: mutex is a classic leaf-lock.6384// In particular a thread must never block on the Threads_lock while6385// holding the Parker:: mutex. If safepoints are pending both the6386// the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.6387ThreadBlockInVM tbivm(jt);63886389// Don't wait if cannot get lock since interference arises from6390// unblocking. Also. check interrupt before trying wait6391if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {6392return;6393}63946395int status ;6396if (_counter > 0) { // no wait needed6397_counter = 0;6398status = pthread_mutex_unlock(_mutex);6399assert (status == 0, "invariant") ;6400// Paranoia to ensure our locked and lock-free paths interact6401// correctly with each other and Java-level accesses.6402OrderAccess::fence();6403return;6404}64056406#ifdef ASSERT6407// Don't catch signals while blocked; let the running threads have the signals.6408// (This allows a debugger to break into the running thread.)6409sigset_t oldsigs;6410sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals();6411pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);6412#endif64136414OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);6415jt->set_suspend_equivalent();6416// cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()64176418assert(_cur_index == -1, "invariant");6419if (time == 0) {6420_cur_index = REL_INDEX; // arbitrary choice when not timed6421status = pthread_cond_wait (&_cond[_cur_index], _mutex) ;6422} else {6423_cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;6424status = os::Linux::safe_cond_timedwait (&_cond[_cur_index], _mutex, &absTime) ;6425if (status != 0 && WorkAroundNPTLTimedWaitHang) {6426pthread_cond_destroy (&_cond[_cur_index]) ;6427pthread_cond_init (&_cond[_cur_index], isAbsolute ? NULL : os::Linux::condAttr());6428}6429}6430_cur_index = -1;6431assert_status(status == 0 || status == EINTR ||6432status == ETIME || status == ETIMEDOUT,6433status, "cond_timedwait");64346435#ifdef ASSERT6436pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);6437#endif64386439_counter = 0 ;6440status = pthread_mutex_unlock(_mutex) ;6441assert_status(status == 0, status, "invariant") ;6442// Paranoia to ensure our locked and lock-free paths interact6443// correctly with each other and Java-level accesses.6444OrderAccess::fence();64456446// If externally suspended while waiting, re-suspend6447if (jt->handle_special_suspend_equivalent_condition()) {6448jt->java_suspend_self();6449}6450}64516452void Parker::unpark() {6453int s, status ;6454status = pthread_mutex_lock(_mutex);6455assert (status == 0, "invariant") ;6456s = _counter;6457_counter = 1;6458if (s < 1) {6459// thread might be parked6460if (_cur_index != -1) {6461// thread is definitely parked6462if (WorkAroundNPTLTimedWaitHang) {6463status = pthread_cond_signal (&_cond[_cur_index]);6464assert (status == 0, "invariant");6465status = pthread_mutex_unlock(_mutex);6466assert (status == 0, "invariant");6467} else {6468// must capture correct index before unlocking6469int index = _cur_index;6470status = pthread_mutex_unlock(_mutex);6471assert (status == 0, "invariant");6472status = pthread_cond_signal (&_cond[index]);6473assert (status == 0, "invariant");6474}6475} else {6476pthread_mutex_unlock(_mutex);6477assert (status == 0, "invariant") ;6478}6479} else {6480pthread_mutex_unlock(_mutex);6481assert (status == 0, "invariant") ;6482}6483}648464856486extern char** environ;64876488// Run the specified command in a separate process. Return its exit value,6489// or -1 on failure (e.g. can't fork a new process).6490// Unlike system(), this function can be called from signal handler. It6491// doesn't block SIGINT et al.6492int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {6493const char * argv[4] = {"sh", "-c", cmd, NULL};64946495pid_t pid ;64966497if (use_vfork_if_available) {6498pid = vfork();6499} else {6500pid = fork();6501}65026503if (pid < 0) {6504// fork failed6505return -1;65066507} else if (pid == 0) {6508// child process65096510execve("/bin/sh", (char* const*)argv, environ);65116512// execve failed6513_exit(-1);65146515} else {6516// copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't6517// care about the actual exit code, for now.65186519int status;65206521// Wait for the child process to exit. This returns immediately if6522// the child has already exited. */6523while (waitpid(pid, &status, 0) < 0) {6524switch (errno) {6525case ECHILD: return 0;6526case EINTR: break;6527default: return -1;6528}6529}65306531if (WIFEXITED(status)) {6532// The child exited normally; get its exit code.6533return WEXITSTATUS(status);6534} else if (WIFSIGNALED(status)) {6535// The child exited because of a signal6536// The best value to return is 0x80 + signal number,6537// because that is what all Unix shells do, and because6538// it allows callers to distinguish between process exit and6539// process death by signal.6540return 0x80 + WTERMSIG(status);6541} else {6542// Unknown exit code; pass it through6543return status;6544}6545}6546}65476548// is_headless_jre()6549//6550// Test for the existence of xawt/libmawt.so or libawt_xawt.so6551// in order to report if we are running in a headless jre6552//6553// Since JDK8 xawt/libmawt.so was moved into the same directory6554// as libawt.so, and renamed libawt_xawt.so6555//6556bool os::is_headless_jre() {6557struct stat statbuf;6558char buf[MAXPATHLEN];6559char libmawtpath[MAXPATHLEN];6560const char *xawtstr = "/xawt/libmawt.so";6561const char *new_xawtstr = "/libawt_xawt.so";6562char *p;65636564// Get path to libjvm.so6565os::jvm_path(buf, sizeof(buf));65666567// Get rid of libjvm.so6568p = strrchr(buf, '/');6569if (p == NULL) return false;6570else *p = '\0';65716572// Get rid of client or server6573p = strrchr(buf, '/');6574if (p == NULL) return false;6575else *p = '\0';65766577// check xawt/libmawt.so6578strcpy(libmawtpath, buf);6579strcat(libmawtpath, xawtstr);6580if (::stat(libmawtpath, &statbuf) == 0) return false;65816582// check libawt_xawt.so6583strcpy(libmawtpath, buf);6584strcat(libmawtpath, new_xawtstr);6585if (::stat(libmawtpath, &statbuf) == 0) return false;65866587return true;6588}65896590// Get the default path to the core file6591// Returns the length of the string6592int os::get_core_path(char* buffer, size_t bufferSize) {6593const char* p = get_current_directory(buffer, bufferSize);65946595if (p == NULL) {6596assert(p != NULL, "failed to get current directory");6597return 0;6598}65996600return strlen(buffer);6601}66026603/////////////// Unit tests ///////////////66046605#ifndef PRODUCT66066607#define test_log(...) \6608do {\6609if (VerboseInternalVMTests) { \6610tty->print_cr(__VA_ARGS__); \6611tty->flush(); \6612}\6613} while (false)66146615class TestReserveMemorySpecial : AllStatic {6616public:6617static void small_page_write(void* addr, size_t size) {6618size_t page_size = os::vm_page_size();66196620char* end = (char*)addr + size;6621for (char* p = (char*)addr; p < end; p += page_size) {6622*p = 1;6623}6624}66256626static void test_reserve_memory_special_huge_tlbfs_only(size_t size) {6627if (!UseHugeTLBFS) {6628return;6629}66306631test_log("test_reserve_memory_special_huge_tlbfs_only(" SIZE_FORMAT ")", size);66326633char* addr = os::Linux::reserve_memory_special_huge_tlbfs_only(size, NULL, false);66346635if (addr != NULL) {6636small_page_write(addr, size);66376638os::Linux::release_memory_special_huge_tlbfs(addr, size);6639}6640}66416642static void test_reserve_memory_special_huge_tlbfs_only() {6643if (!UseHugeTLBFS) {6644return;6645}66466647size_t lp = os::large_page_size();66486649for (size_t size = lp; size <= lp * 10; size += lp) {6650test_reserve_memory_special_huge_tlbfs_only(size);6651}6652}66536654static void test_reserve_memory_special_huge_tlbfs_mixed() {6655size_t lp = os::large_page_size();6656size_t ag = os::vm_allocation_granularity();66576658// sizes to test6659const size_t sizes[] = {6660lp, lp + ag, lp + lp / 2, lp * 2,6661lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,6662lp * 10, lp * 10 + lp / 26663};6664const int num_sizes = sizeof(sizes) / sizeof(size_t);66656666// For each size/alignment combination, we test three scenarios:6667// 1) with req_addr == NULL6668// 2) with a non-null req_addr at which we expect to successfully allocate6669// 3) with a non-null req_addr which contains a pre-existing mapping, at which we6670// expect the allocation to either fail or to ignore req_addr66716672// Pre-allocate two areas; they shall be as large as the largest allocation6673// and aligned to the largest alignment we will be testing.6674const size_t mapping_size = sizes[num_sizes - 1] * 2;6675char* const mapping1 = (char*) ::mmap(NULL, mapping_size,6676PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,6677-1, 0);6678assert(mapping1 != MAP_FAILED, "should work");66796680char* const mapping2 = (char*) ::mmap(NULL, mapping_size,6681PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,6682-1, 0);6683assert(mapping2 != MAP_FAILED, "should work");66846685// Unmap the first mapping, but leave the second mapping intact: the first6686// mapping will serve as a value for a "good" req_addr (case 2). The second6687// mapping, still intact, as "bad" req_addr (case 3).6688::munmap(mapping1, mapping_size);66896690// Case 16691test_log("%s, req_addr NULL:", __FUNCTION__);6692test_log("size align result");66936694for (int i = 0; i < num_sizes; i++) {6695const size_t size = sizes[i];6696for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6697char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, NULL, false);6698test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " -> " PTR_FORMAT " %s",6699size, alignment, p, (p != NULL ? "" : "(failed)"));6700if (p != NULL) {6701assert(is_ptr_aligned(p, alignment), "must be");6702small_page_write(p, size);6703os::Linux::release_memory_special_huge_tlbfs(p, size);6704}6705}6706}67076708// Case 26709test_log("%s, req_addr non-NULL:", __FUNCTION__);6710test_log("size align req_addr result");67116712for (int i = 0; i < num_sizes; i++) {6713const size_t size = sizes[i];6714for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6715char* const req_addr = (char*) align_ptr_up(mapping1, alignment);6716char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);6717test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s",6718size, alignment, req_addr, p,6719((p != NULL ? (p == req_addr ? "(exact match)" : "") : "(failed)")));6720if (p != NULL) {6721assert(p == req_addr, "must be");6722small_page_write(p, size);6723os::Linux::release_memory_special_huge_tlbfs(p, size);6724}6725}6726}67276728// Case 36729test_log("%s, req_addr non-NULL with preexisting mapping:", __FUNCTION__);6730test_log("size align req_addr result");67316732for (int i = 0; i < num_sizes; i++) {6733const size_t size = sizes[i];6734for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6735char* const req_addr = (char*) align_ptr_up(mapping2, alignment);6736char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);6737test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s",6738size, alignment, req_addr, p,6739((p != NULL ? "" : "(failed)")));6740// as the area around req_addr contains already existing mappings, the API should always6741// return NULL (as per contract, it cannot return another address)6742assert(p == NULL, "must be");6743}6744}67456746::munmap(mapping2, mapping_size);67476748}67496750static void test_reserve_memory_special_huge_tlbfs() {6751if (!UseHugeTLBFS) {6752return;6753}67546755test_reserve_memory_special_huge_tlbfs_only();6756test_reserve_memory_special_huge_tlbfs_mixed();6757}67586759static void test_reserve_memory_special_shm(size_t size, size_t alignment) {6760if (!UseSHM) {6761return;6762}67636764test_log("test_reserve_memory_special_shm(" SIZE_FORMAT ", " SIZE_FORMAT ")", size, alignment);67656766char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false);67676768if (addr != NULL) {6769assert(is_ptr_aligned(addr, alignment), "Check");6770assert(is_ptr_aligned(addr, os::large_page_size()), "Check");67716772small_page_write(addr, size);67736774os::Linux::release_memory_special_shm(addr, size);6775}6776}67776778static void test_reserve_memory_special_shm() {6779size_t lp = os::large_page_size();6780size_t ag = os::vm_allocation_granularity();67816782for (size_t size = ag; size < lp * 3; size += ag) {6783for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6784test_reserve_memory_special_shm(size, alignment);6785}6786}6787}67886789static void test() {6790test_reserve_memory_special_huge_tlbfs();6791test_reserve_memory_special_shm();6792}6793};67946795void TestReserveMemorySpecial_test() {6796TestReserveMemorySpecial::test();6797}67986799#endif680068016802