Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/os/linux/vm/os_linux.cpp
32285 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// [TODO if need] Android SHM: use https://github.com/pelya/android-shmem2526// no precompiled headers27#include "classfile/classLoader.hpp"28#include "classfile/systemDictionary.hpp"29#include "classfile/vmSymbols.hpp"30#include "code/icBuffer.hpp"31#include "code/vtableStubs.hpp"32#include "compiler/compileBroker.hpp"33#include "compiler/disassembler.hpp"34#include "interpreter/interpreter.hpp"35#include "jvm_linux.h"36#include "memory/allocation.inline.hpp"37#include "memory/filemap.hpp"38#include "mutex_linux.inline.hpp"39#include "oops/oop.inline.hpp"40#include "os_share_linux.hpp"41#include "osContainer_linux.hpp"42#include "prims/jniFastGetField.hpp"43#include "prims/jvm.h"44#include "prims/jvm_misc.hpp"45#include "runtime/arguments.hpp"46#include "runtime/extendedPC.hpp"47#include "runtime/globals.hpp"48#include "runtime/interfaceSupport.hpp"49#include "runtime/init.hpp"50#include "runtime/java.hpp"51#include "runtime/javaCalls.hpp"52#include "runtime/mutexLocker.hpp"53#include "runtime/objectMonitor.hpp"54#include "runtime/orderAccess.inline.hpp"55#include "runtime/osThread.hpp"56#include "runtime/perfMemory.hpp"57#include "runtime/sharedRuntime.hpp"58#include "runtime/statSampler.hpp"59#include "runtime/stubRoutines.hpp"60#include "runtime/thread.inline.hpp"61#include "runtime/threadCritical.hpp"62#include "runtime/timer.hpp"63#include "services/attachListener.hpp"64#include "services/memTracker.hpp"65#include "services/runtimeService.hpp"66#include "utilities/decoder.hpp"67#include "utilities/defaultStream.hpp"68#include "utilities/events.hpp"69#include "utilities/elfFile.hpp"70#include "utilities/growableArray.hpp"71#include "utilities/vmError.hpp"7273// put OS-includes here74# include <sys/types.h>75# include <sys/mman.h>76# include <sys/stat.h>77# include <sys/select.h>78# include <pthread.h>79# include <signal.h>80# include <errno.h>81# include <dlfcn.h>82# include <stdio.h>83# include <unistd.h>84# include <sys/resource.h>85# include <pthread.h>86# include <sys/stat.h>87# include <sys/time.h>88# include <sys/times.h>89# include <sys/utsname.h>90# include <sys/socket.h>91# include <sys/wait.h>92# include <pwd.h>93# include <poll.h>94# include <semaphore.h>95# include <fcntl.h>96# include <string.h>97# include <sys/sysinfo.h>98#ifndef __ANDROID__99# include <gnu/libc-version.h>100#endif101# include <sys/ipc.h>102#if !defined(__ANDROID__)103# include <syscall.h>104# include <sys/shm.h>105#else106# include <sys/syscall.h>107#endif108# include <link.h>109# include <stdint.h>110# include <inttypes.h>111# include <sys/ioctl.h>112113PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC114115#ifdef __ANDROID__116# define DISABLE_SHM117#endif118119#ifndef _GNU_SOURCE120#define _GNU_SOURCE121#include <sched.h>122#undef _GNU_SOURCE123#else124#include <sched.h>125#endif126127// if RUSAGE_THREAD for getrusage() has not been defined, do it here. The code calling128// getrusage() is prepared to handle the associated failure.129#ifndef RUSAGE_THREAD130#define RUSAGE_THREAD (1) /* only the calling thread */131#endif132133#define MAX_PATH (2 * K)134135#define MAX_SECS 100000000136137// for timer info max values which include all bits138#define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)139140#define LARGEPAGES_BIT (1 << 6)141////////////////////////////////////////////////////////////////////////////////142// global variables143julong os::Linux::_physical_memory = 0;144145address os::Linux::_initial_thread_stack_bottom = NULL;146uintptr_t os::Linux::_initial_thread_stack_size = 0;147148int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;149int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;150int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL;151Mutex* os::Linux::_createThread_lock = NULL;152pthread_t os::Linux::_main_thread;153int os::Linux::_page_size = -1;154const int os::Linux::_vm_default_page_size = (8 * K);155bool os::Linux::_is_floating_stack = false;156bool os::Linux::_is_NPTL = false;157bool os::Linux::_supports_fast_thread_cpu_time = false;158const char * os::Linux::_glibc_version = NULL;159const char * os::Linux::_libpthread_version = NULL;160pthread_condattr_t os::Linux::_condattr[1];161162static jlong initial_time_count=0;163164static int clock_tics_per_sec = 100;165166// For diagnostics to print a message once. see run_periodic_checks167static sigset_t check_signal_done;168static bool check_signals = true;169170static pid_t _initial_pid = 0;171172/* Signal number used to suspend/resume a thread */173174/* do not use any signal number less than SIGSEGV, see 4355769 */175static int SR_signum = SIGUSR2;176sigset_t SR_sigset;177178/* Used to protect dlsym() calls */179static pthread_mutex_t dl_mutex;180181// Declarations182static bool read_so_path_from_maps(const char* so_name, char* buf, int buflen);183static void unpackTime(timespec* absTime, bool isAbsolute, jlong time);184185// utility functions186187static int SR_initialize();188189julong os::available_memory() {190return Linux::available_memory();191}192193julong os::Linux::available_memory() {194// values in struct sysinfo are "unsigned long"195struct sysinfo si;196julong avail_mem;197198if (OSContainer::is_containerized()) {199jlong mem_limit, mem_usage;200if ((mem_limit = OSContainer::memory_limit_in_bytes()) < 1) {201if (PrintContainerInfo) {202tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",203mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);204}205}206207if (mem_limit > 0 && (mem_usage = OSContainer::memory_usage_in_bytes()) < 1) {208if (PrintContainerInfo) {209tty->print_cr("container memory usage failed: " JLONG_FORMAT ", using host value", mem_usage);210}211}212213if (mem_limit > 0 && mem_usage > 0 ) {214avail_mem = mem_limit > mem_usage ? (julong)mem_limit - (julong)mem_usage : 0;215if (PrintContainerInfo) {216tty->print_cr("available container memory: " JULONG_FORMAT, avail_mem);217}218return avail_mem;219}220}221222sysinfo(&si);223avail_mem = (julong)si.freeram * si.mem_unit;224if (Verbose) {225tty->print_cr("available memory: " JULONG_FORMAT, avail_mem);226}227return avail_mem;228}229230julong os::physical_memory() {231jlong phys_mem = 0;232if (OSContainer::is_containerized()) {233jlong mem_limit;234if ((mem_limit = OSContainer::memory_limit_in_bytes()) > 0) {235if (PrintContainerInfo) {236tty->print_cr("total container memory: " JLONG_FORMAT, mem_limit);237}238return mem_limit;239}240241if (PrintContainerInfo) {242tty->print_cr("container memory limit %s: " JLONG_FORMAT ", using host value",243mem_limit == OSCONTAINER_ERROR ? "failed" : "unlimited", mem_limit);244}245}246247phys_mem = Linux::physical_memory();248if (Verbose) {249tty->print_cr("total system memory: " JLONG_FORMAT, phys_mem);250}251return phys_mem;252}253254////////////////////////////////////////////////////////////////////////////////255// environment support256257bool os::getenv(const char* name, char* buf, int len) {258const char* val = ::getenv(name);259if (val != NULL && strlen(val) < (size_t)len) {260strcpy(buf, val);261return true;262}263if (len > 0) buf[0] = 0; // return a null string264return false;265}266267268// Return true if user is running as root.269270bool os::have_special_privileges() {271static bool init = false;272static bool privileges = false;273if (!init) {274privileges = (getuid() != geteuid()) || (getgid() != getegid());275init = true;276}277return privileges;278}279280281#ifndef SYS_gettid282// ref: https://chromium.googlesource.com/chromiumos/docs/+/master/constants/syscalls.md283// i386 & arm: 224, ia64: 1105, amd64: 186, sparc: 143, aarch64: 178284#ifdef __ia64__285#define SYS_gettid 1105286#else287#if defined(__i386__) || defined(__arm__)288#define SYS_gettid 224289#else290#ifdef __amd64__291#define SYS_gettid 186292#else293#ifdef __sparc__294#define SYS_gettid 143295#else296#if defined(__arm64__) || defined(__aarch64__)297#define SYS_gettid 178298#else299#error define gettid for the arch300#endif301#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// _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()623static char _gnu_libc_version[32];624jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),625"glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());626os::Linux::set_glibc_version(_gnu_libc_version);627}628629n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);630if (n > 0) {631char *str = (char *)malloc(n, mtInternal);632confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);633// Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells634// us "NPTL-0.29" even we are running with LinuxThreads. Check if this635// is the case. LinuxThreads has a hard limit on max number of threads.636// So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.637// On the other hand, NPTL does not have such a limit, sysconf()638// will return -1 and errno is not changed. Check if it is really NPTL.639if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&640strstr(str, "NPTL") &&641sysconf(_SC_THREAD_THREADS_MAX) > 0) {642free(str);643os::Linux::set_libpthread_version("linuxthreads");644} else {645os::Linux::set_libpthread_version(str);646}647} else {648// glibc before 2.3.2 only has LinuxThreads.649os::Linux::set_libpthread_version("linuxthreads");650}651#endif // __ANDROID__652653if (strstr(libpthread_version(), "NPTL")) {654os::Linux::set_is_NPTL();655} else {656os::Linux::set_is_LinuxThreads();657}658659// LinuxThreads have two flavors: floating-stack mode, which allows variable660// stack size; and fixed-stack mode. NPTL is always floating-stack.661if (os::Linux::is_NPTL() || os::Linux::supports_variable_stack_size()) {662os::Linux::set_is_floating_stack();663}664}665666/////////////////////////////////////////////////////////////////////////////667// thread stack668669// Force Linux kernel to expand current thread stack. If "bottom" is close670// to the stack guard, caller should block all signals.671//672// MAP_GROWSDOWN:673// A special mmap() flag that is used to implement thread stacks. It tells674// kernel that the memory region should extend downwards when needed. This675// allows early versions of LinuxThreads to only mmap the first few pages676// when creating a new thread. Linux kernel will automatically expand thread677// stack as needed (on page faults).678//679// However, because the memory region of a MAP_GROWSDOWN stack can grow on680// demand, if a page fault happens outside an already mapped MAP_GROWSDOWN681// region, it's hard to tell if the fault is due to a legitimate stack682// access or because of reading/writing non-exist memory (e.g. buffer683// overrun). As a rule, if the fault happens below current stack pointer,684// Linux kernel does not expand stack, instead a SIGSEGV is sent to the685// application (see Linux kernel fault.c).686//687// This Linux feature can cause SIGSEGV when VM bangs thread stack for688// stack overflow detection.689//690// Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do691// not use this flag. However, the stack of initial thread is not created692// by pthread, it is still MAP_GROWSDOWN. Also it's possible (though693// unlikely) that user code can create a thread with MAP_GROWSDOWN stack694// and then attach the thread to JVM.695//696// To get around the problem and allow stack banging on Linux, we need to697// manually expand thread stack after receiving the SIGSEGV.698//699// There are two ways to expand thread stack to address "bottom", we used700// both of them in JVM before 1.5:701// 1. adjust stack pointer first so that it is below "bottom", and then702// touch "bottom"703// 2. mmap() the page in question704//705// Now alternate signal stack is gone, it's harder to use 2. For instance,706// if current sp is already near the lower end of page 101, and we need to707// call mmap() to map page 100, it is possible that part of the mmap() frame708// will be placed in page 100. When page 100 is mapped, it is zero-filled.709// That will destroy the mmap() frame and cause VM to crash.710//711// The following code works by adjusting sp first, then accessing the "bottom"712// page to force a page fault. Linux kernel will then automatically expand the713// stack mapping.714//715// _expand_stack_to() assumes its frame size is less than page size, which716// should always be true if the function is not inlined.717718#if __GNUC__ < 3 // gcc 2.x does not support noinline attribute719#define NOINLINE720#else721#define NOINLINE __attribute__ ((noinline))722#endif723724static void _expand_stack_to(address bottom) NOINLINE;725726static void _expand_stack_to(address bottom) {727address sp;728size_t size;729volatile char *p;730731// Adjust bottom to point to the largest address within the same page, it732// gives us a one-page buffer if alloca() allocates slightly more memory.733bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size());734bottom += os::Linux::page_size() - 1;735736// sp might be slightly above current stack pointer; if that's the case, we737// will alloca() a little more space than necessary, which is OK. Don't use738// os::current_stack_pointer(), as its result can be slightly below current739// stack pointer, causing us to not alloca enough to reach "bottom".740sp = (address)&sp;741742if (sp > bottom) {743size = sp - bottom;744p = (volatile char *)alloca(size);745assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");746p[0] = '\0';747}748}749750void os::Linux::expand_stack_to(address bottom) {751_expand_stack_to(bottom);752}753754bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {755assert(t!=NULL, "just checking");756assert(t->osthread()->expanding_stack(), "expand should be set");757assert(t->stack_base() != NULL, "stack_base was not initialized");758759if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {760sigset_t mask_all, old_sigset;761sigfillset(&mask_all);762pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);763_expand_stack_to(addr);764pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);765return true;766}767return false;768}769770//////////////////////////////////////////////////////////////////////////////771// create new thread772773static address highest_vm_reserved_address();774775// check if it's safe to start a new thread776static bool _thread_safety_check(Thread* thread) {777if (os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack()) {778// Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat)779// Heap is mmap'ed at lower end of memory space. Thread stacks are780// allocated (MAP_FIXED) from high address space. Every thread stack781// occupies a fixed size slot (usually 2Mbytes, but user can change782// it to other values if they rebuild LinuxThreads).783//784// Problem with MAP_FIXED is that mmap() can still succeed even part of785// the memory region has already been mmap'ed. That means if we have too786// many threads and/or very large heap, eventually thread stack will787// collide with heap.788//789// Here we try to prevent heap/stack collision by comparing current790// stack bottom with the highest address that has been mmap'ed by JVM791// plus a safety margin for memory maps created by native code.792//793// This feature can be disabled by setting ThreadSafetyMargin to 0794//795if (ThreadSafetyMargin > 0) {796address stack_bottom = os::current_stack_base() - os::current_stack_size();797798// not safe if our stack extends below the safety margin799return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();800} else {801return true;802}803} else {804// Floating stack LinuxThreads or NPTL:805// Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When806// there's not enough space left, pthread_create() will fail. If we come807// here, that means enough space has been reserved for stack.808return true;809}810}811812// Thread start routine for all newly created threads813static void *java_start(Thread *thread) {814// Try to randomize the cache line index of hot stack frames.815// This helps when threads of the same stack traces evict each other's816// cache lines. The threads can be either from the same JVM instance, or817// from different JVM instances. The benefit is especially true for818// processors with hyperthreading technology.819static int counter = 0;820int pid = os::current_process_id();821alloca(((pid ^ counter++) & 7) * 128);822823ThreadLocalStorage::set_thread(thread);824825OSThread* osthread = thread->osthread();826Monitor* sync = osthread->startThread_lock();827828// non floating stack LinuxThreads needs extra check, see above829if (!_thread_safety_check(thread)) {830// notify parent thread831MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);832osthread->set_state(ZOMBIE);833sync->notify_all();834return NULL;835}836837// thread_id is kernel thread id (similar to Solaris LWP id)838osthread->set_thread_id(os::Linux::gettid());839840if (UseNUMA) {841int lgrp_id = os::numa_get_group_id();842if (lgrp_id != -1) {843thread->set_lgrp_id(lgrp_id);844}845}846// initialize signal mask for this thread847os::Linux::hotspot_sigmask(thread);848849// initialize floating point control register850os::Linux::init_thread_fpu_state();851852// handshaking with parent thread853{854MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);855856// notify parent thread857osthread->set_state(INITIALIZED);858sync->notify_all();859860// wait until os::start_thread()861while (osthread->get_state() == INITIALIZED) {862sync->wait(Mutex::_no_safepoint_check_flag);863}864}865866// call one more level start routine867thread->run();868869return 0;870}871872bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {873assert(thread->osthread() == NULL, "caller responsible");874875// Allocate the OSThread object876OSThread* osthread = new OSThread(NULL, NULL);877if (osthread == NULL) {878return false;879}880881// set the correct thread state882osthread->set_thread_type(thr_type);883884// Initial state is ALLOCATED but not INITIALIZED885osthread->set_state(ALLOCATED);886887thread->set_osthread(osthread);888889// init thread attributes890pthread_attr_t attr;891pthread_attr_init(&attr);892pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);893894// stack size895if (os::Linux::supports_variable_stack_size()) {896// calculate stack size if it's not specified by caller897if (stack_size == 0) {898stack_size = os::Linux::default_stack_size(thr_type);899900switch (thr_type) {901case os::java_thread:902// Java threads use ThreadStackSize which default value can be903// changed with the flag -Xss904assert (JavaThread::stack_size_at_create() > 0, "this should be set");905stack_size = JavaThread::stack_size_at_create();906break;907case os::compiler_thread:908if (CompilerThreadStackSize > 0) {909stack_size = (size_t)(CompilerThreadStackSize * K);910break;911} // else fall through:912// use VMThreadStackSize if CompilerThreadStackSize is not defined913case os::vm_thread:914case os::pgc_thread:915case os::cgc_thread:916case os::watcher_thread:917if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);918break;919}920}921922stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);923pthread_attr_setstacksize(&attr, stack_size);924} else {925// let pthread_create() pick the default value.926}927928// glibc guard page929pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));930931ThreadState state;932933{934// Serialize thread creation if we are running with fixed stack LinuxThreads935bool lock = os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack();936if (lock) {937os::Linux::createThread_lock()->lock_without_safepoint_check();938}939940pthread_t tid;941int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);942943pthread_attr_destroy(&attr);944945if (ret != 0) {946if (PrintMiscellaneous && (Verbose || WizardMode)) {947perror("pthread_create()");948}949// Need to clean up stuff we've allocated so far950thread->set_osthread(NULL);951delete osthread;952if (lock) os::Linux::createThread_lock()->unlock();953return false;954}955956// Store pthread info into the OSThread957osthread->set_pthread_id(tid);958959// Wait until child thread is either initialized or aborted960{961Monitor* sync_with_child = osthread->startThread_lock();962MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);963while ((state = osthread->get_state()) == ALLOCATED) {964sync_with_child->wait(Mutex::_no_safepoint_check_flag);965}966}967968if (lock) {969os::Linux::createThread_lock()->unlock();970}971}972973// Aborted due to thread limit being reached974if (state == ZOMBIE) {975thread->set_osthread(NULL);976delete osthread;977return false;978}979980// The thread is returned suspended (in state INITIALIZED),981// and is started higher up in the call chain982assert(state == INITIALIZED, "race condition");983return true;984}985986/////////////////////////////////////////////////////////////////////////////987// attach existing thread988989// bootstrap the main thread990bool os::create_main_thread(JavaThread* thread) {991assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread");992return create_attached_thread(thread);993}994995bool os::create_attached_thread(JavaThread* thread) {996#ifdef ASSERT997thread->verify_not_published();998#endif9991000// Allocate the OSThread object1001OSThread* osthread = new OSThread(NULL, NULL);10021003if (osthread == NULL) {1004return false;1005}10061007// Store pthread info into the OSThread1008osthread->set_thread_id(os::Linux::gettid());1009osthread->set_pthread_id(::pthread_self());10101011// initialize floating point control register1012os::Linux::init_thread_fpu_state();10131014// Initial thread state is RUNNABLE1015osthread->set_state(RUNNABLE);10161017thread->set_osthread(osthread);10181019if (UseNUMA) {1020int lgrp_id = os::numa_get_group_id();1021if (lgrp_id != -1) {1022thread->set_lgrp_id(lgrp_id);1023}1024}10251026if (os::is_primordial_thread()) {1027// If current thread is primordial thread, its stack is mapped on demand,1028// see notes about MAP_GROWSDOWN. Here we try to force kernel to map1029// the entire stack region to avoid SEGV in stack banging.1030// It is also useful to get around the heap-stack-gap problem on SuSE1031// kernel (see 4821821 for details). We first expand stack to the top1032// of yellow zone, then enable stack yellow zone (order is significant,1033// enabling yellow zone first will crash JVM on SuSE Linux), so there1034// is no gap between the last two virtual memory regions.10351036JavaThread *jt = (JavaThread *)thread;1037address addr = jt->stack_yellow_zone_base();1038assert(addr != NULL, "initialization problem?");1039assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");10401041osthread->set_expanding_stack();1042os::Linux::manually_expand_stack(jt, addr);1043osthread->clear_expanding_stack();1044}10451046// initialize signal mask for this thread1047// and save the caller's signal mask1048os::Linux::hotspot_sigmask(thread);10491050return true;1051}10521053void os::pd_start_thread(Thread* thread) {1054OSThread * osthread = thread->osthread();1055assert(osthread->get_state() != INITIALIZED, "just checking");1056Monitor* sync_with_child = osthread->startThread_lock();1057MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);1058sync_with_child->notify();1059}10601061// Free Linux resources related to the OSThread1062void os::free_thread(OSThread* osthread) {1063assert(osthread != NULL, "osthread not set");10641065if (Thread::current()->osthread() == osthread) {1066// Restore caller's signal mask1067sigset_t sigmask = osthread->caller_sigmask();1068pthread_sigmask(SIG_SETMASK, &sigmask, NULL);1069}10701071delete osthread;1072}10731074//////////////////////////////////////////////////////////////////////////////1075// thread local storage10761077// Restore the thread pointer if the destructor is called. This is in case1078// someone from JNI code sets up a destructor with pthread_key_create to run1079// detachCurrentThread on thread death. Unless we restore the thread pointer we1080// will hang or crash. When detachCurrentThread is called the key will be set1081// to null and we will not be called again. If detachCurrentThread is never1082// called we could loop forever depending on the pthread implementation.1083static void restore_thread_pointer(void* p) {1084Thread* thread = (Thread*) p;1085os::thread_local_storage_at_put(ThreadLocalStorage::thread_index(), thread);1086}10871088int os::allocate_thread_local_storage() {1089pthread_key_t key;1090int rslt = pthread_key_create(&key, restore_thread_pointer);1091assert(rslt == 0, "cannot allocate thread local storage");1092return (int)key;1093}10941095// Note: This is currently not used by VM, as we don't destroy TLS key1096// on VM exit.1097void os::free_thread_local_storage(int index) {1098int rslt = pthread_key_delete((pthread_key_t)index);1099assert(rslt == 0, "invalid index");1100}11011102void os::thread_local_storage_at_put(int index, void* value) {1103int rslt = pthread_setspecific((pthread_key_t)index, value);1104assert(rslt == 0, "pthread_setspecific failed");1105}11061107extern "C" Thread* get_thread() {1108return ThreadLocalStorage::thread();1109}11101111//////////////////////////////////////////////////////////////////////////////1112// primordial thread11131114// Check if current thread is the primordial thread, similar to Solaris thr_main.1115bool os::is_primordial_thread(void) {1116char dummy;1117// If called before init complete, thread stack bottom will be null.1118// Can be called if fatal error occurs before initialization.1119if (os::Linux::initial_thread_stack_bottom() == NULL) return false;1120assert(os::Linux::initial_thread_stack_bottom() != NULL &&1121os::Linux::initial_thread_stack_size() != 0,1122"os::init did not locate primordial thread's stack region");1123if ((address)&dummy >= os::Linux::initial_thread_stack_bottom() &&1124(address)&dummy < os::Linux::initial_thread_stack_bottom() +1125os::Linux::initial_thread_stack_size()) {1126return true;1127} else {1128return false;1129}1130}11311132// Find the virtual memory area that contains addr1133static bool find_vma(address addr, address* vma_low, address* vma_high) {1134FILE *fp = fopen("/proc/self/maps", "r");1135if (fp) {1136address low, high;1137while (!feof(fp)) {1138if (fscanf(fp, "%p-%p", &low, &high) == 2) {1139if (low <= addr && addr < high) {1140if (vma_low) *vma_low = low;1141if (vma_high) *vma_high = high;1142fclose (fp);1143return true;1144}1145}1146for (;;) {1147int ch = fgetc(fp);1148if (ch == EOF || ch == (int)'\n') break;1149}1150}1151fclose(fp);1152}1153return false;1154}11551156// Locate primordial thread stack. This special handling of primordial thread stack1157// is needed because pthread_getattr_np() on most (all?) Linux distros returns1158// bogus value for the primordial process thread. While the launcher has created1159// the VM in a new thread since JDK 6, we still have to allow for the use of the1160// JNI invocation API from a primordial thread.1161void os::Linux::capture_initial_stack(size_t max_size) {11621163// max_size is either 0 (which means accept OS default for thread stacks) or1164// a user-specified value known to be at least the minimum needed. If we1165// are actually on the primordial thread we can make it appear that we have a1166// smaller max_size stack by inserting the guard pages at that location. But we1167// cannot do anything to emulate a larger stack than what has been provided by1168// the OS or threading library. In fact if we try to use a stack greater than1169// what is set by rlimit then we will crash the hosting process.11701171// Maximum stack size is the easy part, get it from RLIMIT_STACK.1172// If this is "unlimited" then it will be a huge value.1173struct rlimit rlim;1174getrlimit(RLIMIT_STACK, &rlim);1175size_t stack_size = rlim.rlim_cur;11761177// 6308388: a bug in ld.so will relocate its own .data section to the1178// lower end of primordial stack; reduce ulimit -s value a little bit1179// so we won't install guard page on ld.so's data section.1180// But ensure we don't underflow the stack size - allow 1 page spare1181if (stack_size >= (size_t)(3 * page_size())) {1182stack_size -= 2 * page_size();1183}11841185// Try to figure out where the stack base (top) is. This is harder.1186//1187// When an application is started, glibc saves the initial stack pointer in1188// a global variable "__libc_stack_end", which is then used by system1189// libraries. __libc_stack_end should be pretty close to stack top. The1190// variable is available since the very early days. However, because it is1191// a private interface, it could disappear in the future.1192//1193// Linux kernel saves start_stack information in /proc/<pid>/stat. Similar1194// to __libc_stack_end, it is very close to stack top, but isn't the real1195// stack top. Note that /proc may not exist if VM is running as a chroot1196// program, so reading /proc/<pid>/stat could fail. Also the contents of1197// /proc/<pid>/stat could change in the future (though unlikely).1198//1199// We try __libc_stack_end first. If that doesn't work, look for1200// /proc/<pid>/stat. If neither of them works, we use current stack pointer1201// as a hint, which should work well in most cases.12021203uintptr_t stack_start;12041205// try __libc_stack_end first1206uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");1207if (p && *p) {1208stack_start = *p;1209} else {1210// see if we can get the start_stack field from /proc/self/stat1211FILE *fp;1212int pid;1213char state;1214int ppid;1215int pgrp;1216int session;1217int nr;1218int tpgrp;1219unsigned long flags;1220unsigned long minflt;1221unsigned long cminflt;1222unsigned long majflt;1223unsigned long cmajflt;1224unsigned long utime;1225unsigned long stime;1226long cutime;1227long cstime;1228long prio;1229long nice;1230long junk;1231long it_real;1232uintptr_t start;1233uintptr_t vsize;1234intptr_t rss;1235uintptr_t rsslim;1236uintptr_t scodes;1237uintptr_t ecode;1238int i;12391240// Figure what the primordial thread stack base is. Code is inspired1241// by email from Hans Boehm. /proc/self/stat begins with current pid,1242// followed by command name surrounded by parentheses, state, etc.1243char stat[2048];1244int statlen;12451246fp = fopen("/proc/self/stat", "r");1247if (fp) {1248statlen = fread(stat, 1, 2047, fp);1249stat[statlen] = '\0';1250fclose(fp);12511252// Skip pid and the command string. Note that we could be dealing with1253// weird command names, e.g. user could decide to rename java launcher1254// to "java 1.4.2 :)", then the stat file would look like1255// 1234 (java 1.4.2 :)) R ... ...1256// We don't really need to know the command string, just find the last1257// occurrence of ")" and then start parsing from there. See bug 4726580.1258char * s = strrchr(stat, ')');12591260i = 0;1261if (s) {1262// Skip blank chars1263do s++; while (isspace(*s));12641265#define _UFM UINTX_FORMAT1266#define _DFM INTX_FORMAT12671268/* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */1269/* 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 */1270i = 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,1271&state, /* 3 %c */1272&ppid, /* 4 %d */1273&pgrp, /* 5 %d */1274&session, /* 6 %d */1275&nr, /* 7 %d */1276&tpgrp, /* 8 %d */1277&flags, /* 9 %lu */1278&minflt, /* 10 %lu */1279&cminflt, /* 11 %lu */1280&majflt, /* 12 %lu */1281&cmajflt, /* 13 %lu */1282&utime, /* 14 %lu */1283&stime, /* 15 %lu */1284&cutime, /* 16 %ld */1285&cstime, /* 17 %ld */1286&prio, /* 18 %ld */1287&nice, /* 19 %ld */1288&junk, /* 20 %ld */1289&it_real, /* 21 %ld */1290&start, /* 22 UINTX_FORMAT */1291&vsize, /* 23 UINTX_FORMAT */1292&rss, /* 24 INTX_FORMAT */1293&rsslim, /* 25 UINTX_FORMAT */1294&scodes, /* 26 UINTX_FORMAT */1295&ecode, /* 27 UINTX_FORMAT */1296&stack_start); /* 28 UINTX_FORMAT */1297}12981299#undef _UFM1300#undef _DFM13011302if (i != 28 - 2) {1303assert(false, "Bad conversion from /proc/self/stat");1304// product mode - assume we are the primordial thread, good luck in the1305// embedded case.1306warning("Can't detect primordial thread stack location - bad conversion");1307stack_start = (uintptr_t) &rlim;1308}1309} else {1310// For some reason we can't open /proc/self/stat (for example, running on1311// FreeBSD with a Linux emulator, or inside chroot), this should work for1312// most cases, so don't abort:1313warning("Can't detect primordial thread stack location - no /proc/self/stat");1314stack_start = (uintptr_t) &rlim;1315}1316}13171318// Now we have a pointer (stack_start) very close to the stack top, the1319// next thing to do is to figure out the exact location of stack top. We1320// can find out the virtual memory area that contains stack_start by1321// reading /proc/self/maps, it should be the last vma in /proc/self/maps,1322// and its upper limit is the real stack top. (again, this would fail if1323// running inside chroot, because /proc may not exist.)13241325uintptr_t stack_top;1326address low, high;1327if (find_vma((address)stack_start, &low, &high)) {1328// success, "high" is the true stack top. (ignore "low", because initial1329// thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)1330stack_top = (uintptr_t)high;1331} else {1332// failed, likely because /proc/self/maps does not exist1333warning("Can't detect primordial thread stack location - find_vma failed");1334// best effort: stack_start is normally within a few pages below the real1335// stack top, use it as stack top, and reduce stack size so we won't put1336// guard page outside stack.1337stack_top = stack_start;1338stack_size -= 16 * page_size();1339}13401341// stack_top could be partially down the page so align it1342stack_top = align_size_up(stack_top, page_size());13431344// Allowed stack value is minimum of max_size and what we derived from rlimit1345if (max_size > 0) {1346_initial_thread_stack_size = MIN2(max_size, stack_size);1347} else {1348// Accept the rlimit max, but if stack is unlimited then it will be huge, so1349// clamp it at 8MB as we do on Solaris1350_initial_thread_stack_size = MIN2(stack_size, 8*M);1351}13521353_initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());1354_initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;1355assert(_initial_thread_stack_bottom < (address)stack_top, "overflow!");1356}13571358////////////////////////////////////////////////////////////////////////////////1359// time support13601361// Time since start-up in seconds to a fine granularity.1362// Used by VMSelfDestructTimer and the MemProfiler.1363double os::elapsedTime() {13641365return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution1366}13671368jlong os::elapsed_counter() {1369return javaTimeNanos() - initial_time_count;1370}13711372jlong os::elapsed_frequency() {1373return NANOSECS_PER_SEC; // nanosecond resolution1374}13751376bool os::supports_vtime() { return true; }1377bool os::enable_vtime() { return false; }1378bool os::vtime_enabled() { return false; }13791380double os::elapsedVTime() {1381struct rusage usage;1382int retval = getrusage(RUSAGE_THREAD, &usage);1383if (retval == 0) {1384return (double) (usage.ru_utime.tv_sec + usage.ru_stime.tv_sec) + (double) (usage.ru_utime.tv_usec + usage.ru_stime.tv_usec) / (1000 * 1000);1385} else {1386// better than nothing, but not much1387return elapsedTime();1388}1389}13901391jlong os::javaTimeMillis() {1392timeval time;1393int status = gettimeofday(&time, NULL);1394assert(status != -1, "linux error");1395return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);1396}13971398#ifndef CLOCK_MONOTONIC1399#define CLOCK_MONOTONIC (1)1400#endif14011402void os::Linux::clock_init() {1403// we do dlopen's in this particular order due to bug in linux1404// dynamical loader (see 6348968) leading to crash on exit1405void* handle = dlopen("librt.so.1", RTLD_LAZY);1406if (handle == NULL) {1407handle = dlopen("librt.so", RTLD_LAZY);1408}1409#ifdef __ANDROID__1410if (handle == NULL) {1411// libc has clock_getres and clock_gettime1412handle = RTLD_DEFAULT;1413}1414#endif14151416if (handle) {1417int (*clock_getres_func)(clockid_t, struct timespec*) =1418(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");1419int (*clock_gettime_func)(clockid_t, struct timespec*) =1420(int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");1421if (clock_getres_func && clock_gettime_func) {1422// See if monotonic clock is supported by the kernel. Note that some1423// early implementations simply return kernel jiffies (updated every1424// 1/100 or 1/1000 second). It would be bad to use such a low res clock1425// for nano time (though the monotonic property is still nice to have).1426// It's fixed in newer kernels, however clock_getres() still returns1427// 1/HZ. We check if clock_getres() works, but will ignore its reported1428// resolution for now. Hopefully as people move to new kernels, this1429// won't be a problem.1430struct timespec res;1431struct timespec tp;1432if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&1433clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {1434// yes, monotonic clock is supported1435_clock_gettime = clock_gettime_func;1436return;1437} else {1438// close librt if there is no monotonic clock1439#ifndef __ANDROID__ // we should not close RTLD_DEFAULT :)1440dlclose(handle);1441#endif1442}1443}1444}1445warning("No monotonic clock was available - timed services may " \1446"be adversely affected if the time-of-day clock changes");1447}14481449#ifndef SYS_clock_getres14501451#if defined(IA32) || defined(AMD64) || defined(AARCH64)1452#define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229) AARCH64_ONLY(114)1453#define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)1454#else1455#warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"1456#define sys_clock_getres(x,y) -11457#endif14581459#else1460#define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)1461#endif14621463void os::Linux::fast_thread_clock_init() {1464if (!UseLinuxPosixThreadCPUClocks) {1465return;1466}1467clockid_t clockid;1468struct timespec tp;1469int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =1470(int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");14711472// Switch to using fast clocks for thread cpu time if1473// the sys_clock_getres() returns 0 error code.1474// Note, that some kernels may support the current thread1475// clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks1476// returned by the pthread_getcpuclockid().1477// If the fast Posix clocks are supported then the sys_clock_getres()1478// must return at least tp.tv_sec == 0 which means a resolution1479// better than 1 sec. This is extra check for reliability.14801481if(pthread_getcpuclockid_func &&1482pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&1483sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {14841485_supports_fast_thread_cpu_time = true;1486_pthread_getcpuclockid = pthread_getcpuclockid_func;1487}1488}14891490jlong os::javaTimeNanos() {1491if (Linux::supports_monotonic_clock()) {1492struct timespec tp;1493int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);1494assert(status == 0, "gettime error");1495jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);1496return result;1497} else {1498timeval time;1499int status = gettimeofday(&time, NULL);1500assert(status != -1, "linux error");1501jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);1502return 1000 * usecs;1503}1504}15051506void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {1507if (Linux::supports_monotonic_clock()) {1508info_ptr->max_value = ALL_64_BITS;15091510// CLOCK_MONOTONIC - amount of time since some arbitrary point in the past1511info_ptr->may_skip_backward = false; // not subject to resetting or drifting1512info_ptr->may_skip_forward = false; // not subject to resetting or drifting1513} else {1514// gettimeofday - based on time in seconds since the Epoch thus does not wrap1515info_ptr->max_value = ALL_64_BITS;15161517// gettimeofday is a real time clock so it skips1518info_ptr->may_skip_backward = true;1519info_ptr->may_skip_forward = true;1520}15211522info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time1523}15241525// Return the real, user, and system times in seconds from an1526// arbitrary fixed point in the past.1527bool os::getTimesSecs(double* process_real_time,1528double* process_user_time,1529double* process_system_time) {1530struct tms ticks;1531clock_t real_ticks = times(&ticks);15321533if (real_ticks == (clock_t) (-1)) {1534return false;1535} else {1536double ticks_per_second = (double) clock_tics_per_sec;1537*process_user_time = ((double) ticks.tms_utime) / ticks_per_second;1538*process_system_time = ((double) ticks.tms_stime) / ticks_per_second;1539*process_real_time = ((double) real_ticks) / ticks_per_second;15401541return true;1542}1543}154415451546char * os::local_time_string(char *buf, size_t buflen) {1547struct tm t;1548time_t long_time;1549time(&long_time);1550localtime_r(&long_time, &t);1551jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",1552t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,1553t.tm_hour, t.tm_min, t.tm_sec);1554return buf;1555}15561557struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {1558return localtime_r(clock, res);1559}15601561////////////////////////////////////////////////////////////////////////////////1562// runtime exit support15631564// Note: os::shutdown() might be called very early during initialization, or1565// called from signal handler. Before adding something to os::shutdown(), make1566// sure it is async-safe and can handle partially initialized VM.1567void os::shutdown() {15681569// allow PerfMemory to attempt cleanup of any persistent resources1570perfMemory_exit();15711572// needs to remove object in file system1573AttachListener::abort();15741575// flush buffered output, finish log files1576ostream_abort();15771578// Check for abort hook1579abort_hook_t abort_hook = Arguments::abort_hook();1580if (abort_hook != NULL) {1581abort_hook();1582}15831584}15851586// Note: os::abort() might be called very early during initialization, or1587// called from signal handler. Before adding something to os::abort(), make1588// sure it is async-safe and can handle partially initialized VM.1589void os::abort(bool dump_core) {1590os::shutdown();1591if (dump_core) {1592#ifndef PRODUCT1593fdStream out(defaultStream::output_fd());1594out.print_raw("Current thread is ");1595char buf[16];1596jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());1597out.print_raw_cr(buf);1598out.print_raw_cr("Dumping core ...");1599#endif1600::abort(); // dump core1601}16021603::exit(1);1604}16051606// Die immediately, no exit hook, no abort hook, no cleanup.1607void os::die() {1608// _exit() on LinuxThreads only kills current thread1609::abort();1610}161116121613// This method is a copy of JDK's sysGetLastErrorString1614// from src/solaris/hpi/src/system_md.c16151616size_t os::lasterror(char *buf, size_t len) {16171618if (errno == 0) return 0;16191620const char *s = ::strerror(errno);1621size_t n = ::strlen(s);1622if (n >= len) {1623n = len - 1;1624}1625::strncpy(buf, s, n);1626buf[n] = '\0';1627return n;1628}16291630intx os::current_thread_id() { return (intx)pthread_self(); }1631int os::current_process_id() {16321633// Under the old linux thread library, linux gives each thread1634// its own process id. Because of this each thread will return1635// a different pid if this method were to return the result1636// of getpid(2). Linux provides no api that returns the pid1637// of the launcher thread for the vm. This implementation1638// returns a unique pid, the pid of the launcher thread1639// that starts the vm 'process'.16401641// Under the NPTL, getpid() returns the same pid as the1642// launcher thread rather than a unique pid per thread.1643// Use gettid() if you want the old pre NPTL behaviour.16441645// if you are looking for the result of a call to getpid() that1646// returns a unique pid for the calling thread, then look at the1647// OSThread::thread_id() method in osThread_linux.hpp file16481649return (int)(_initial_pid ? _initial_pid : getpid());1650}16511652// DLL functions16531654const char* os::dll_file_extension() { return ".so"; }16551656// This must be hard coded because it's the system's temporary1657// directory not the java application's temp directory, ala java.io.tmpdir.1658const char* os::get_temp_directory() { return "/tmp"; }16591660static bool file_exists(const char* filename) {1661struct stat statbuf;1662if (filename == NULL || strlen(filename) == 0) {1663return false;1664}1665return os::stat(filename, &statbuf) == 0;1666}16671668bool os::dll_build_name(char* buffer, size_t buflen,1669const char* pname, const char* fname) {1670bool retval = false;1671// Copied from libhpi1672const size_t pnamelen = pname ? strlen(pname) : 0;16731674// Return error on buffer overflow.1675if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {1676return retval;1677}16781679if (pnamelen == 0) {1680snprintf(buffer, buflen, "lib%s.so", fname);1681retval = true;1682} else if (strchr(pname, *os::path_separator()) != NULL) {1683int n;1684char** pelements = split_path(pname, &n);1685if (pelements == NULL) {1686return false;1687}1688for (int i = 0 ; i < n ; i++) {1689// Really shouldn't be NULL, but check can't hurt1690if (pelements[i] == NULL || strlen(pelements[i]) == 0) {1691continue; // skip the empty path values1692}1693snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);1694if (file_exists(buffer)) {1695retval = true;1696break;1697}1698}1699// release the storage1700for (int i = 0 ; i < n ; i++) {1701if (pelements[i] != NULL) {1702FREE_C_HEAP_ARRAY(char, pelements[i], mtInternal);1703}1704}1705if (pelements != NULL) {1706FREE_C_HEAP_ARRAY(char*, pelements, mtInternal);1707}1708} else {1709snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);1710retval = true;1711}1712return retval;1713}17141715// check if addr is inside libjvm.so1716bool os::address_is_in_vm(address addr) {1717static address libjvm_base_addr;1718Dl_info dlinfo;17191720if (libjvm_base_addr == NULL) {1721if (dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo) != 0) {1722libjvm_base_addr = (address)dlinfo.dli_fbase;1723}1724assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");1725}17261727if (dladdr((void *)addr, &dlinfo) != 0) {1728if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;1729}17301731return false;1732}17331734bool os::dll_address_to_function_name(address addr, char *buf,1735int buflen, int *offset) {1736// buf is not optional, but offset is optional1737assert(buf != NULL, "sanity check");17381739Dl_info dlinfo;17401741if (dladdr((void*)addr, &dlinfo) != 0) {1742// see if we have a matching symbol1743if (dlinfo.dli_saddr != NULL && dlinfo.dli_sname != NULL) {1744if (!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {1745jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);1746}1747if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;1748return true;1749}1750// no matching symbol so try for just file info1751if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != NULL) {1752if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),1753buf, buflen, offset, dlinfo.dli_fname)) {1754return true;1755}1756}1757}17581759buf[0] = '\0';1760if (offset != NULL) *offset = -1;1761return false;1762}17631764struct _address_to_library_name {1765address addr; // input : memory address1766size_t buflen; // size of fname1767char* fname; // output: library name1768address base; // library base addr1769};17701771static int address_to_library_name_callback(struct dl_phdr_info *info,1772size_t size, void *data) {1773int i;1774bool found = false;1775address libbase = NULL;1776struct _address_to_library_name * d = (struct _address_to_library_name *)data;17771778// iterate through all loadable segments1779for (i = 0; i < info->dlpi_phnum; i++) {1780address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);1781if (info->dlpi_phdr[i].p_type == PT_LOAD) {1782// base address of a library is the lowest address of its loaded1783// segments.1784if (libbase == NULL || libbase > segbase) {1785libbase = segbase;1786}1787// see if 'addr' is within current segment1788if (segbase <= d->addr &&1789d->addr < segbase + info->dlpi_phdr[i].p_memsz) {1790found = true;1791}1792}1793}17941795// dlpi_name is NULL or empty if the ELF file is executable, return 01796// so dll_address_to_library_name() can fall through to use dladdr() which1797// can figure out executable name from argv[0].1798if (found && info->dlpi_name && info->dlpi_name[0]) {1799d->base = libbase;1800if (d->fname) {1801jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);1802}1803return 1;1804}1805return 0;1806}18071808bool os::dll_address_to_library_name(address addr, char* buf,1809int buflen, int* offset) {1810// buf is not optional, but offset is optional1811assert(buf != NULL, "sanity check");18121813Dl_info dlinfo;18141815// Android bionic libc does not have the bug below.1816#ifndef __ANDROID__1817struct _address_to_library_name data;18181819// There is a bug in old glibc dladdr() implementation that it could resolve1820// to wrong library name if the .so file has a base address != NULL. Here1821// we iterate through the program headers of all loaded libraries to find1822// out which library 'addr' really belongs to. This workaround can be1823// removed once the minimum requirement for glibc is moved to 2.3.x.1824data.addr = addr;1825data.fname = buf;1826data.buflen = buflen;1827data.base = NULL;1828int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);18291830if (rslt) {1831// buf already contains library name1832if (offset) *offset = addr - data.base;1833return true;1834}1835#endif // !__ANDROID__1836if (dladdr((void*)addr, &dlinfo) != 0) {1837if (dlinfo.dli_fname != NULL) {1838jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);1839}1840if (dlinfo.dli_fbase != NULL && offset != NULL) {1841*offset = addr - (address)dlinfo.dli_fbase;1842}1843return true;1844}18451846buf[0] = '\0';1847if (offset) *offset = -1;1848return false;1849}18501851static bool read_so_path_from_maps(const char* so_name, char* buf, int buflen) {1852FILE *fp = fopen("/proc/self/maps", "r");1853assert(fp, "Failed to open /proc/self/maps");1854if (!fp) {1855return false;1856}18571858char maps_buffer[2048];1859while (fgets(maps_buffer, 2048, fp) != NULL) {1860if (strstr(maps_buffer, so_name) == NULL) {1861continue;1862}18631864char *so_path = strchr(maps_buffer, '/');1865so_path[strlen(so_path) - 1] = '\0'; // Cut trailing \n1866jio_snprintf(buf, buflen, "%s", so_path);1867fclose(fp);1868return true;1869}18701871fclose(fp);1872return false;1873}18741875// Loads .dll/.so and1876// in case of error it checks if .dll/.so was built for the1877// same architecture as Hotspot is running on187818791880// Remember the stack's state. The Linux dynamic linker will change1881// the stack to 'executable' at most once, so we must safepoint only once.1882bool os::Linux::_stack_is_executable = false;18831884// VM operation that loads a library. This is necessary if stack protection1885// of the Java stacks can be lost during loading the library. If we1886// do not stop the Java threads, they can stack overflow before the stacks1887// are protected again.1888class VM_LinuxDllLoad: public VM_Operation {1889private:1890const char *_filename;1891char *_ebuf;1892int _ebuflen;1893void *_lib;1894public:1895VM_LinuxDllLoad(const char *fn, char *ebuf, int ebuflen) :1896_filename(fn), _ebuf(ebuf), _ebuflen(ebuflen), _lib(NULL) {}1897VMOp_Type type() const { return VMOp_LinuxDllLoad; }1898void doit() {1899_lib = os::Linux::dll_load_in_vmthread(_filename, _ebuf, _ebuflen);1900os::Linux::_stack_is_executable = true;1901}1902void* loaded_library() { return _lib; }1903};19041905void * os::dll_load(const char *filename, char *ebuf, int ebuflen)1906{1907void * result = NULL;1908bool load_attempted = false;19091910// Check whether the library to load might change execution rights1911// of the stack. If they are changed, the protection of the stack1912// guard pages will be lost. We need a safepoint to fix this.1913//1914// See Linux man page execstack(8) for more info.1915if (os::uses_stack_guard_pages() && !os::Linux::_stack_is_executable) {1916ElfFile ef(filename);1917if (!ef.specifies_noexecstack()) {1918if (!is_init_completed()) {1919os::Linux::_stack_is_executable = true;1920// This is OK - No Java threads have been created yet, and hence no1921// stack guard pages to fix.1922//1923// This should happen only when you are building JDK7 using a very1924// old version of JDK6 (e.g., with JPRT) and running test_gamma.1925//1926// Dynamic loader will make all stacks executable after1927// this function returns, and will not do that again.1928assert(Threads::first() == NULL, "no Java threads should exist yet.");1929} else {1930warning("You have loaded library %s which might have disabled stack guard. "1931"The VM will try to fix the stack guard now.\n"1932"It's highly recommended that you fix the library with "1933"'execstack -c <libfile>', or link it with '-z noexecstack'.",1934filename);19351936assert(Thread::current()->is_Java_thread(), "must be Java thread");1937JavaThread *jt = JavaThread::current();1938if (jt->thread_state() != _thread_in_native) {1939// This happens when a compiler thread tries to load a hsdis-<arch>.so file1940// that requires ExecStack. Cannot enter safe point. Let's give up.1941warning("Unable to fix stack guard. Giving up.");1942} else {1943if (!LoadExecStackDllInVMThread) {1944// This is for the case where the DLL has an static1945// constructor function that executes JNI code. We cannot1946// load such DLLs in the VMThread.1947result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);1948}19491950ThreadInVMfromNative tiv(jt);1951debug_only(VMNativeEntryWrapper vew;)19521953VM_LinuxDllLoad op(filename, ebuf, ebuflen);1954VMThread::execute(&op);1955if (LoadExecStackDllInVMThread) {1956result = op.loaded_library();1957}1958load_attempted = true;1959}1960}1961}1962}19631964if (!load_attempted) {1965result = os::Linux::dlopen_helper(filename, ebuf, ebuflen);1966}19671968if (result != NULL) {1969// Successful loading1970return result;1971}19721973Elf32_Ehdr elf_head;1974int diag_msg_max_length=ebuflen-strlen(ebuf);1975char* diag_msg_buf=ebuf+strlen(ebuf);19761977if (diag_msg_max_length==0) {1978// No more space in ebuf for additional diagnostics message1979return NULL;1980}198119821983int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);19841985if (file_descriptor < 0) {1986// Can't open library, report dlerror() message1987return NULL;1988}19891990bool failed_to_read_elf_head=1991(sizeof(elf_head)!=1992(::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;19931994::close(file_descriptor);1995if (failed_to_read_elf_head) {1996// file i/o error - report dlerror() msg1997return NULL;1998}19992000typedef struct {2001Elf32_Half code; // Actual value as defined in elf.h2002Elf32_Half compat_class; // Compatibility of archs at VM's sense2003char elf_class; // 32 or 64 bit2004char endianess; // MSB or LSB2005char* name; // String representation2006} arch_t;20072008#ifndef EM_4862009#define EM_486 6 /* Intel 80486 */2010#endif2011#ifndef EM_AARCH642012#define EM_AARCH64 183 /* ARM AARCH64 */2013#endif20142015static const arch_t arch_array[]={2016{EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},2017{EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},2018{EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},2019{EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},2020{EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},2021{EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},2022{EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},2023{EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},2024#if defined(VM_LITTLE_ENDIAN)2025{EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2LSB, (char*)"Power PC 64 LE"},2026#else2027{EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},2028#endif2029{EM_ARM, EM_ARM, ELFCLASS32, ELFDATA2LSB, (char*)"ARM"},2030{EM_S390, EM_S390, ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},2031{EM_ALPHA, EM_ALPHA, ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},2032{EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},2033{EM_MIPS, EM_MIPS, ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},2034{EM_PARISC, EM_PARISC, ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},2035{EM_68K, EM_68K, ELFCLASS32, ELFDATA2MSB, (char*)"M68k"},2036{EM_AARCH64, EM_AARCH64, ELFCLASS64, ELFDATA2LSB, (char*)"AARCH64"},2037};20382039#if (defined IA32)2040static Elf32_Half running_arch_code=EM_386;2041#elif (defined AMD64)2042static Elf32_Half running_arch_code=EM_X86_64;2043#elif (defined IA64)2044static Elf32_Half running_arch_code=EM_IA_64;2045#elif (defined __sparc) && (defined _LP64)2046static Elf32_Half running_arch_code=EM_SPARCV9;2047#elif (defined __sparc) && (!defined _LP64)2048static Elf32_Half running_arch_code=EM_SPARC;2049#elif (defined __powerpc64__)2050static Elf32_Half running_arch_code=EM_PPC64;2051#elif (defined __powerpc__)2052static Elf32_Half running_arch_code=EM_PPC;2053#elif (defined ARM)2054static Elf32_Half running_arch_code=EM_ARM;2055#elif (defined S390)2056static Elf32_Half running_arch_code=EM_S390;2057#elif (defined ALPHA)2058static Elf32_Half running_arch_code=EM_ALPHA;2059#elif (defined MIPSEL)2060static Elf32_Half running_arch_code=EM_MIPS_RS3_LE;2061#elif (defined PARISC)2062static Elf32_Half running_arch_code=EM_PARISC;2063#elif (defined MIPS)2064static Elf32_Half running_arch_code=EM_MIPS;2065#elif (defined M68K)2066static Elf32_Half running_arch_code=EM_68K;2067#elif (defined AARCH64)2068static Elf32_Half running_arch_code=EM_AARCH64;2069#else2070#error Method os::dll_load requires that one of following is defined:\2071IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K, AARCH642072#endif20732074// Identify compatability class for VM's architecture and library's architecture2075// Obtain string descriptions for architectures20762077arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};2078int running_arch_index=-1;20792080for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {2081if (running_arch_code == arch_array[i].code) {2082running_arch_index = i;2083}2084if (lib_arch.code == arch_array[i].code) {2085lib_arch.compat_class = arch_array[i].compat_class;2086lib_arch.name = arch_array[i].name;2087}2088}20892090assert(running_arch_index != -1,2091"Didn't find running architecture code (running_arch_code) in arch_array");2092if (running_arch_index == -1) {2093// Even though running architecture detection failed2094// we may still continue with reporting dlerror() message2095return NULL;2096}20972098if (lib_arch.endianess != arch_array[running_arch_index].endianess) {2099::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");2100return NULL;2101}21022103#ifndef S3902104if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {2105::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");2106return NULL;2107}2108#endif // !S39021092110if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {2111if ( lib_arch.name!=NULL ) {2112::snprintf(diag_msg_buf, diag_msg_max_length-1,2113" (Possible cause: can't load %s-bit .so on a %s-bit platform)",2114lib_arch.name, arch_array[running_arch_index].name);2115} else {2116::snprintf(diag_msg_buf, diag_msg_max_length-1,2117" (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",2118lib_arch.code,2119arch_array[running_arch_index].name);2120}2121}21222123return NULL;2124}21252126void * os::Linux::dlopen_helper(const char *filename, char *ebuf, int ebuflen) {2127void * result = ::dlopen(filename, RTLD_LAZY);2128if (result == NULL) {2129::strncpy(ebuf, ::dlerror(), ebuflen - 1);2130ebuf[ebuflen-1] = '\0';2131}2132return result;2133}21342135void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf, int ebuflen) {2136void * result = NULL;2137if (LoadExecStackDllInVMThread) {2138result = dlopen_helper(filename, ebuf, ebuflen);2139}21402141// Since 7019808, libjvm.so is linked with -noexecstack. If the VM loads a2142// library that requires an executable stack, or which does not have this2143// stack attribute set, dlopen changes the stack attribute to executable. The2144// read protection of the guard pages gets lost.2145//2146// Need to check _stack_is_executable again as multiple VM_LinuxDllLoad2147// may have been queued at the same time.21482149if (!_stack_is_executable) {2150JavaThread *jt = Threads::first();21512152while (jt) {2153if (!jt->stack_guard_zone_unused() && // Stack not yet fully initialized2154jt->stack_yellow_zone_enabled()) { // No pending stack overflow exceptions2155if (!os::guard_memory((char *) jt->stack_red_zone_base() - jt->stack_red_zone_size(),2156jt->stack_yellow_zone_size() + jt->stack_red_zone_size())) {2157warning("Attempt to reguard stack yellow zone failed.");2158}2159}2160jt = jt->next();2161}2162}21632164return result;2165}21662167/*2168* glibc-2.0 libdl is not MT safe. If you are building with any glibc,2169* chances are you might want to run the generated bits against glibc-2.02170* libdl.so, so always use locking for any version of glibc.2171*/2172void* os::dll_lookup(void* handle, const char* name) {2173pthread_mutex_lock(&dl_mutex);2174void* res = dlsym(handle, name);2175pthread_mutex_unlock(&dl_mutex);2176return res;2177}21782179void* os::get_default_process_handle() {2180return (void*)::dlopen(NULL, RTLD_LAZY);2181}21822183static bool _print_ascii_file(const char* filename, outputStream* st) {2184int fd = ::open(filename, O_RDONLY);2185if (fd == -1) {2186return false;2187}21882189char buf[32];2190int bytes;2191while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {2192st->print_raw(buf, bytes);2193}21942195::close(fd);21962197return true;2198}21992200void os::print_dll_info(outputStream *st) {2201st->print_cr("Dynamic libraries:");22022203char fname[32];2204pid_t pid = os::Linux::gettid();22052206jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);22072208if (!_print_ascii_file(fname, st)) {2209st->print("Can not get library information for pid = %d\n", pid);2210}2211}22122213int os::get_loaded_modules_info(os::LoadedModulesCallbackFunc callback, void *param) {2214FILE *procmapsFile = NULL;22152216// Open the procfs maps file for the current process2217if ((procmapsFile = fopen("/proc/self/maps", "r")) != NULL) {2218// Allocate PATH_MAX for file name plus a reasonable size for other fields.2219char line[PATH_MAX + 100];22202221// Read line by line from 'file'2222while (fgets(line, sizeof(line), procmapsFile) != NULL) {2223u8 base, top, offset, inode;2224char permissions[5];2225char device[6];2226char name[PATH_MAX + 1];22272228// Parse fields from line2229sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %4s " UINT64_FORMAT_X " %7s " INT64_FORMAT " %s",2230&base, &top, permissions, &offset, device, &inode, name);22312232// Filter by device id '00:00' so that we only get file system mapped files.2233if (strcmp(device, "00:00") != 0) {22342235// Call callback with the fields of interest2236if(callback(name, (address)base, (address)top, param)) {2237// Oops abort, callback aborted2238fclose(procmapsFile);2239return 1;2240}2241}2242}2243fclose(procmapsFile);2244}2245return 0;2246}22472248void os::print_os_info_brief(outputStream* st) {2249os::Linux::print_distro_info(st);22502251os::Posix::print_uname_info(st);22522253os::Linux::print_libversion_info(st);22542255}22562257void os::print_os_info(outputStream* st) {2258st->print("OS:");22592260os::Linux::print_distro_info(st);22612262os::Posix::print_uname_info(st);22632264// Print warning if unsafe chroot environment detected2265if (unsafe_chroot_detected) {2266st->print("WARNING!! ");2267st->print_cr("%s", unstable_chroot_error);2268}22692270os::Linux::print_libversion_info(st);22712272os::Posix::print_rlimit_info(st);22732274os::Posix::print_load_average(st);22752276os::Linux::print_full_memory_info(st);22772278os::Linux::print_container_info(st);2279}22802281// Try to identify popular distros.2282// Most Linux distributions have a /etc/XXX-release file, which contains2283// the OS version string. Newer Linux distributions have a /etc/lsb-release2284// file that also contains the OS version string. Some have more than one2285// /etc/XXX-release file (e.g. Mandrake has both /etc/mandrake-release and2286// /etc/redhat-release.), so the order is important.2287// Any Linux that is based on Redhat (i.e. Oracle, Mandrake, Sun JDS...) have2288// their own specific XXX-release file as well as a redhat-release file.2289// Because of this the XXX-release file needs to be searched for before the2290// redhat-release file.2291// Since Red Hat has a lsb-release file that is not very descriptive the2292// search for redhat-release needs to be before lsb-release.2293// Since the lsb-release file is the new standard it needs to be searched2294// before the older style release files.2295// Searching system-release (Red Hat) and os-release (other Linuxes) are a2296// next to last resort. The os-release file is a new standard that contains2297// distribution information and the system-release file seems to be an old2298// standard that has been replaced by the lsb-release and os-release files.2299// Searching for the debian_version file is the last resort. It contains2300// an informative string like "6.0.6" or "wheezy/sid". Because of this2301// "Debian " is printed before the contents of the debian_version file.2302void os::Linux::print_distro_info(outputStream* st) {2303if (!_print_ascii_file("/etc/oracle-release", st) &&2304!_print_ascii_file("/etc/mandriva-release", st) &&2305!_print_ascii_file("/etc/mandrake-release", st) &&2306!_print_ascii_file("/etc/sun-release", st) &&2307!_print_ascii_file("/etc/redhat-release", st) &&2308!_print_ascii_file("/etc/lsb-release", st) &&2309!_print_ascii_file("/etc/SuSE-release", st) &&2310!_print_ascii_file("/etc/turbolinux-release", st) &&2311!_print_ascii_file("/etc/gentoo-release", st) &&2312!_print_ascii_file("/etc/ltib-release", st) &&2313!_print_ascii_file("/etc/angstrom-version", st) &&2314!_print_ascii_file("/etc/system-release", st) &&2315!_print_ascii_file("/etc/os-release", st)) {23162317if (file_exists("/etc/debian_version")) {2318st->print("Debian ");2319_print_ascii_file("/etc/debian_version", st);2320} else {2321st->print("Linux");2322}2323}2324st->cr();2325}23262327void os::Linux::print_libversion_info(outputStream* st) {2328// libc, pthread2329st->print("libc:");2330st->print("%s ", os::Linux::glibc_version());2331st->print("%s ", os::Linux::libpthread_version());2332if (os::Linux::is_LinuxThreads()) {2333st->print("(%s stack)", os::Linux::is_floating_stack() ? "floating" : "fixed");2334}2335st->cr();2336}23372338void os::Linux::print_full_memory_info(outputStream* st) {2339st->print("\n/proc/meminfo:\n");2340_print_ascii_file("/proc/meminfo", st);2341st->cr();2342}23432344void os::Linux::print_container_info(outputStream* st) {2345if (!OSContainer::is_containerized()) {2346return;2347}23482349st->print("container (cgroup) information:\n");23502351const char *p_ct = OSContainer::container_type();2352st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");23532354char *p = OSContainer::cpu_cpuset_cpus();2355st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");2356free(p);23572358p = OSContainer::cpu_cpuset_memory_nodes();2359st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");2360free(p);23612362int i = OSContainer::active_processor_count();2363if (i > 0) {2364st->print("active_processor_count: %d\n", i);2365} else {2366st->print("active_processor_count: failed\n");2367}23682369i = OSContainer::cpu_quota();2370st->print("cpu_quota: %d\n", i);23712372i = OSContainer::cpu_period();2373st->print("cpu_period: %d\n", i);23742375i = OSContainer::cpu_shares();2376st->print("cpu_shares: %d\n", i);23772378jlong j = OSContainer::memory_limit_in_bytes();2379st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);23802381j = OSContainer::memory_and_swap_limit_in_bytes();2382st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);23832384j = OSContainer::memory_soft_limit_in_bytes();2385st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);23862387j = OSContainer::OSContainer::memory_usage_in_bytes();2388st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);23892390j = OSContainer::OSContainer::memory_max_usage_in_bytes();2391st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);2392st->cr();2393}23942395void os::print_memory_info(outputStream* st) {23962397st->print("Memory:");2398st->print(" %dk page", os::vm_page_size()>>10);23992400// values in struct sysinfo are "unsigned long"2401struct sysinfo si;2402sysinfo(&si);24032404st->print(", physical " UINT64_FORMAT "k",2405os::physical_memory() >> 10);2406st->print("(" UINT64_FORMAT "k free)",2407os::available_memory() >> 10);2408st->print(", swap " UINT64_FORMAT "k",2409((jlong)si.totalswap * si.mem_unit) >> 10);2410st->print("(" UINT64_FORMAT "k free)",2411((jlong)si.freeswap * si.mem_unit) >> 10);2412st->cr();2413}24142415void os::pd_print_cpu_info(outputStream* st) {2416st->print("\n/proc/cpuinfo:\n");2417if (!_print_ascii_file("/proc/cpuinfo", st)) {2418st->print(" <Not Available>");2419}2420st->cr();2421}24222423void os::print_siginfo(outputStream* st, void* siginfo) {2424const siginfo_t* si = (const siginfo_t*)siginfo;24252426os::Posix::print_siginfo_brief(st, si);2427#if INCLUDE_CDS2428if (si && (si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&2429UseSharedSpaces) {2430FileMapInfo* mapinfo = FileMapInfo::current_info();2431if (mapinfo->is_in_shared_space(si->si_addr)) {2432st->print("\n\nError accessing class data sharing archive." \2433" Mapped file inaccessible during execution, " \2434" possible disk/network problem.");2435}2436}2437#endif2438st->cr();2439}244024412442static void print_signal_handler(outputStream* st, int sig,2443char* buf, size_t buflen);24442445void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {2446st->print_cr("Signal Handlers:");2447print_signal_handler(st, SIGSEGV, buf, buflen);2448print_signal_handler(st, SIGBUS , buf, buflen);2449print_signal_handler(st, SIGFPE , buf, buflen);2450print_signal_handler(st, SIGPIPE, buf, buflen);2451print_signal_handler(st, SIGXFSZ, buf, buflen);2452print_signal_handler(st, SIGILL , buf, buflen);2453print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);2454print_signal_handler(st, SR_signum, buf, buflen);2455print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen);2456print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);2457print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen);2458print_signal_handler(st, BREAK_SIGNAL, buf, buflen);2459#if defined(PPC64)2460print_signal_handler(st, SIGTRAP, buf, buflen);2461#endif2462}24632464static char saved_jvm_path[MAXPATHLEN] = {0};24652466// Find the full path to the current module, libjvm.so2467void os::jvm_path(char *buf, jint buflen) {2468// Error checking.2469if (buflen < MAXPATHLEN) {2470assert(false, "must use a large-enough buffer");2471buf[0] = '\0';2472return;2473}2474// Lazy resolve the path to current module.2475if (saved_jvm_path[0] != 0) {2476strcpy(buf, saved_jvm_path);2477return;2478}24792480char dli_fname[MAXPATHLEN];2481bool ret = dll_address_to_library_name(2482CAST_FROM_FN_PTR(address, os::jvm_path),2483dli_fname, sizeof(dli_fname), NULL);2484assert(ret, "cannot locate libjvm");2485#ifdef __ANDROID__2486if (dli_fname[0] == '\0') {2487return;2488}24892490if (strchr(dli_fname, '/') == NULL) {2491bool ok = read_so_path_from_maps(dli_fname, buf, buflen);2492assert(ok, "unable to turn relative libjvm.so path into absolute");2493return;2494}24952496snprintf(buf, buflen, /* "%s/lib/%s/server/%s", java_home_var, cpu_arch, */ "%s", dli_fname);2497#else // !__ANDROID__2498char *rp = NULL;2499if (ret && dli_fname[0] != '\0') {2500rp = realpath(dli_fname, buf);2501}2502if (rp == NULL)2503return;25042505if (Arguments::created_by_gamma_launcher()) {2506// Support for the gamma launcher. Typical value for buf is2507// "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so". If "/jre/lib/" appears at2508// the right place in the string, then assume we are installed in a JDK and2509// we're done. Otherwise, check for a JAVA_HOME environment variable and fix2510// up the path so it looks like libjvm.so is installed there (append a2511// fake suffix hotspot/libjvm.so).2512const char *p = buf + strlen(buf) - 1;2513for (int count = 0; p > buf && count < 5; ++count) {2514for (--p; p > buf && *p != '/'; --p)2515/* empty */ ;2516}25172518if (strncmp(p, "/jre/lib/", 9) != 0) {2519// Look for JAVA_HOME in the environment.2520char* java_home_var = ::getenv("JAVA_HOME");2521if (java_home_var != NULL && java_home_var[0] != 0) {2522char* jrelib_p;2523int len;25242525// Check the current module name "libjvm.so".2526p = strrchr(buf, '/');2527assert(strstr(p, "/libjvm") == p, "invalid library name");25282529rp = realpath(java_home_var, buf);2530if (rp == NULL)2531return;25322533// determine if this is a legacy image or modules image2534// modules image doesn't have "jre" subdirectory2535len = strlen(buf);2536assert(len < buflen, "Ran out of buffer room");2537jrelib_p = buf + len;2538snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);2539if (0 != access(buf, F_OK)) {2540snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);2541}25422543if (0 == access(buf, F_OK)) {2544// Use current module name "libjvm.so"2545len = strlen(buf);2546snprintf(buf + len, buflen-len, "/hotspot/libjvm.so");2547} else {2548// Go back to path of .so2549rp = realpath(dli_fname, buf);2550if (rp == NULL)2551return;2552}2553}2554}2555}2556#endif // __ANDROID__2557strncpy(saved_jvm_path, buf, MAXPATHLEN);2558}25592560void os::print_jni_name_prefix_on(outputStream* st, int args_size) {2561// no prefix required, not even "_"2562}25632564void os::print_jni_name_suffix_on(outputStream* st, int args_size) {2565// no suffix required2566}25672568////////////////////////////////////////////////////////////////////////////////2569// sun.misc.Signal support25702571static volatile jint sigint_count = 0;25722573static void2574UserHandler(int sig, void *siginfo, void *context) {2575// 4511530 - sem_post is serialized and handled by the manager thread. When2576// the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We2577// don't want to flood the manager thread with sem_post requests.2578if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)2579return;25802581// Ctrl-C is pressed during error reporting, likely because the error2582// handler fails to abort. Let VM die immediately.2583if (sig == SIGINT && is_error_reported()) {2584os::die();2585}25862587os::signal_notify(sig);2588}25892590void* os::user_handler() {2591return CAST_FROM_FN_PTR(void*, UserHandler);2592}25932594class Semaphore : public StackObj {2595public:2596Semaphore();2597~Semaphore();2598void signal();2599void wait();2600bool trywait();2601bool timedwait(unsigned int sec, int nsec);2602private:2603sem_t _semaphore;2604};26052606Semaphore::Semaphore() {2607sem_init(&_semaphore, 0, 0);2608}26092610Semaphore::~Semaphore() {2611sem_destroy(&_semaphore);2612}26132614void Semaphore::signal() {2615sem_post(&_semaphore);2616}26172618void Semaphore::wait() {2619sem_wait(&_semaphore);2620}26212622bool Semaphore::trywait() {2623return sem_trywait(&_semaphore) == 0;2624}26252626bool Semaphore::timedwait(unsigned int sec, int nsec) {26272628struct timespec ts;2629// Semaphore's are always associated with CLOCK_REALTIME2630os::Linux::clock_gettime(CLOCK_REALTIME, &ts);2631// see unpackTime for discussion on overflow checking2632if (sec >= MAX_SECS) {2633ts.tv_sec += MAX_SECS;2634ts.tv_nsec = 0;2635} else {2636ts.tv_sec += sec;2637ts.tv_nsec += nsec;2638if (ts.tv_nsec >= NANOSECS_PER_SEC) {2639ts.tv_nsec -= NANOSECS_PER_SEC;2640++ts.tv_sec; // note: this must be <= max_secs2641}2642}26432644while (1) {2645int result = sem_timedwait(&_semaphore, &ts);2646if (result == 0) {2647return true;2648} else if (errno == EINTR) {2649continue;2650} else if (errno == ETIMEDOUT) {2651return false;2652} else {2653return false;2654}2655}2656}26572658extern "C" {2659typedef void (*sa_handler_t)(int);2660typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);2661}26622663void* os::signal(int signal_number, void* handler) {2664struct sigaction sigAct, oldSigAct;26652666sigfillset(&(sigAct.sa_mask));2667sigAct.sa_flags = SA_RESTART|SA_SIGINFO;2668sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);26692670if (sigaction(signal_number, &sigAct, &oldSigAct)) {2671// -1 means registration failed2672return (void *)-1;2673}26742675return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);2676}26772678void os::signal_raise(int signal_number) {2679::raise(signal_number);2680}26812682/*2683* The following code is moved from os.cpp for making this2684* code platform specific, which it is by its very nature.2685*/26862687// Will be modified when max signal is changed to be dynamic2688int os::sigexitnum_pd() {2689return NSIG;2690}26912692// a counter for each possible signal value2693static volatile jint pending_signals[NSIG+1] = { 0 };26942695// Linux(POSIX) specific hand shaking semaphore.2696static sem_t sig_sem;2697static Semaphore sr_semaphore;26982699void os::signal_init_pd() {2700// Initialize signal structures2701::memset((void*)pending_signals, 0, sizeof(pending_signals));27022703// Initialize signal semaphore2704::sem_init(&sig_sem, 0, 0);2705}27062707void os::signal_notify(int sig) {2708Atomic::inc(&pending_signals[sig]);2709::sem_post(&sig_sem);2710}27112712static int check_pending_signals(bool wait) {2713Atomic::store(0, &sigint_count);2714for (;;) {2715for (int i = 0; i < NSIG + 1; i++) {2716jint n = pending_signals[i];2717if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {2718return i;2719}2720}2721if (!wait) {2722return -1;2723}2724JavaThread *thread = JavaThread::current();2725ThreadBlockInVM tbivm(thread);27262727bool threadIsSuspended;2728do {2729thread->set_suspend_equivalent();2730// cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()2731::sem_wait(&sig_sem);27322733// were we externally suspended while we were waiting?2734threadIsSuspended = thread->handle_special_suspend_equivalent_condition();2735if (threadIsSuspended) {2736//2737// The semaphore has been incremented, but while we were waiting2738// another thread suspended us. We don't want to continue running2739// while suspended because that would surprise the thread that2740// suspended us.2741//2742::sem_post(&sig_sem);27432744thread->java_suspend_self();2745}2746} while (threadIsSuspended);2747}2748}27492750int os::signal_lookup() {2751return check_pending_signals(false);2752}27532754int os::signal_wait() {2755return check_pending_signals(true);2756}27572758////////////////////////////////////////////////////////////////////////////////2759// Virtual Memory27602761int os::vm_page_size() {2762// Seems redundant as all get out2763assert(os::Linux::page_size() != -1, "must call os::init");2764return os::Linux::page_size();2765}27662767// Solaris allocates memory by pages.2768int os::vm_allocation_granularity() {2769assert(os::Linux::page_size() != -1, "must call os::init");2770return os::Linux::page_size();2771}27722773// Rationale behind this function:2774// current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable2775// mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get2776// samples for JITted code. Here we create private executable mapping over the code cache2777// and then we can use standard (well, almost, as mapping can change) way to provide2778// info for the reporting script by storing timestamp and location of symbol2779void linux_wrap_code(char* base, size_t size) {2780static volatile jint cnt = 0;27812782if (!UseOprofile) {2783return;2784}27852786char buf[PATH_MAX+1];2787int num = Atomic::add(1, &cnt);27882789snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",2790os::get_temp_directory(), os::current_process_id(), num);2791unlink(buf);27922793int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU);27942795if (fd != -1) {2796off_t rv = ::lseek(fd, size-2, SEEK_SET);2797if (rv != (off_t)-1) {2798if (::write(fd, "", 1) == 1) {2799mmap(base, size,2800PROT_READ|PROT_WRITE|PROT_EXEC,2801MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);2802}2803}2804::close(fd);2805unlink(buf);2806}2807}28082809static bool recoverable_mmap_error(int err) {2810// See if the error is one we can let the caller handle. This2811// list of errno values comes from JBS-6843484. I can't find a2812// Linux man page that documents this specific set of errno2813// values so while this list currently matches Solaris, it may2814// change as we gain experience with this failure mode.2815switch (err) {2816case EBADF:2817case EINVAL:2818case ENOTSUP:2819// let the caller deal with these errors2820return true;28212822default:2823// Any remaining errors on this OS can cause our reserved mapping2824// to be lost. That can cause confusion where different data2825// structures think they have the same memory mapped. The worst2826// scenario is if both the VM and a library think they have the2827// same memory mapped.2828return false;2829}2830}28312832static void warn_fail_commit_memory(char* addr, size_t size, bool exec,2833int err) {2834warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT2835", %d) failed; error='%s' (errno=%d)", addr, size, exec,2836strerror(err), err);2837}28382839static void warn_fail_commit_memory(char* addr, size_t size,2840size_t alignment_hint, bool exec,2841int err) {2842warning("INFO: os::commit_memory(" PTR_FORMAT ", " SIZE_FORMAT2843", " SIZE_FORMAT ", %d) failed; error='%s' (errno=%d)", addr, size,2844alignment_hint, exec, strerror(err), err);2845}28462847// NOTE: Linux kernel does not really reserve the pages for us.2848// All it does is to check if there are enough free pages2849// left at the time of mmap(). This could be a potential2850// problem.2851int os::Linux::commit_memory_impl(char* addr, size_t size, bool exec) {2852int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;2853uintptr_t res = (uintptr_t) ::mmap(addr, size, prot,2854MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);2855if (res != (uintptr_t) MAP_FAILED) {2856if (UseNUMAInterleaving) {2857numa_make_global(addr, size);2858}2859return 0;2860}28612862int err = errno; // save errno from mmap() call above28632864if (!recoverable_mmap_error(err)) {2865warn_fail_commit_memory(addr, size, exec, err);2866vm_exit_out_of_memory(size, OOM_MMAP_ERROR, "committing reserved memory.");2867}28682869return err;2870}28712872bool os::pd_commit_memory(char* addr, size_t size, bool exec) {2873return os::Linux::commit_memory_impl(addr, size, exec) == 0;2874}28752876void os::pd_commit_memory_or_exit(char* addr, size_t size, bool exec,2877const char* mesg) {2878assert(mesg != NULL, "mesg must be specified");2879int err = os::Linux::commit_memory_impl(addr, size, exec);2880if (err != 0) {2881// the caller wants all commit errors to exit with the specified mesg:2882warn_fail_commit_memory(addr, size, exec, err);2883vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);2884}2885}28862887// Define MAP_HUGETLB here so we can build HotSpot on old systems.2888#ifndef MAP_HUGETLB2889#define MAP_HUGETLB 0x400002890#endif28912892// Define MADV_HUGEPAGE here so we can build HotSpot on old systems.2893#ifndef MADV_HUGEPAGE2894#define MADV_HUGEPAGE 142895#endif28962897int os::Linux::commit_memory_impl(char* addr, size_t size,2898size_t alignment_hint, bool exec) {2899int err = os::Linux::commit_memory_impl(addr, size, exec);2900if (err == 0) {2901realign_memory(addr, size, alignment_hint);2902}2903return err;2904}29052906bool os::pd_commit_memory(char* addr, size_t size, size_t alignment_hint,2907bool exec) {2908return os::Linux::commit_memory_impl(addr, size, alignment_hint, exec) == 0;2909}29102911void os::pd_commit_memory_or_exit(char* addr, size_t size,2912size_t alignment_hint, bool exec,2913const char* mesg) {2914assert(mesg != NULL, "mesg must be specified");2915int err = os::Linux::commit_memory_impl(addr, size, alignment_hint, exec);2916if (err != 0) {2917// the caller wants all commit errors to exit with the specified mesg:2918warn_fail_commit_memory(addr, size, alignment_hint, exec, err);2919vm_exit_out_of_memory(size, OOM_MMAP_ERROR, mesg);2920}2921}29222923void os::pd_realign_memory(char *addr, size_t bytes, size_t alignment_hint) {2924if (UseTransparentHugePages && alignment_hint > (size_t)vm_page_size()) {2925// We don't check the return value: madvise(MADV_HUGEPAGE) may not2926// be supported or the memory may already be backed by huge pages.2927::madvise(addr, bytes, MADV_HUGEPAGE);2928}2929}29302931void os::pd_free_memory(char *addr, size_t bytes, size_t alignment_hint) {2932// This method works by doing an mmap over an existing mmaping and effectively discarding2933// the existing pages. However it won't work for SHM-based large pages that cannot be2934// uncommitted at all. We don't do anything in this case to avoid creating a segment with2935// small pages on top of the SHM segment. This method always works for small pages, so we2936// allow that in any case.2937if (alignment_hint <= (size_t)os::vm_page_size() || can_commit_large_page_memory()) {2938commit_memory(addr, bytes, alignment_hint, !ExecMem);2939}2940}29412942void os::numa_make_global(char *addr, size_t bytes) {2943Linux::numa_interleave_memory(addr, bytes);2944}29452946// Define for numa_set_bind_policy(int). Setting the argument to 0 will set the2947// bind policy to MPOL_PREFERRED for the current thread.2948#define USE_MPOL_PREFERRED 029492950void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {2951// To make NUMA and large pages more robust when both enabled, we need to ease2952// the requirements on where the memory should be allocated. MPOL_BIND is the2953// default policy and it will force memory to be allocated on the specified2954// node. Changing this to MPOL_PREFERRED will prefer to allocate the memory on2955// the specified node, but will not force it. Using this policy will prevent2956// getting SIGBUS when trying to allocate large pages on NUMA nodes with no2957// free large pages.2958Linux::numa_set_bind_policy(USE_MPOL_PREFERRED);2959Linux::numa_tonode_memory(addr, bytes, lgrp_hint);2960}29612962bool os::numa_topology_changed() { return false; }29632964size_t os::numa_get_groups_num() {2965// Return just the number of nodes in which it's possible to allocate memory2966// (in numa terminology, configured nodes).2967return Linux::numa_num_configured_nodes();2968}29692970int os::numa_get_group_id() {2971int cpu_id = Linux::sched_getcpu();2972if (cpu_id != -1) {2973int lgrp_id = Linux::get_node_by_cpu(cpu_id);2974if (lgrp_id != -1) {2975return lgrp_id;2976}2977}2978return 0;2979}29802981int os::Linux::get_existing_num_nodes() {2982size_t node;2983size_t highest_node_number = Linux::numa_max_node();2984int num_nodes = 0;29852986// Get the total number of nodes in the system including nodes without memory.2987for (node = 0; node <= highest_node_number; node++) {2988if (isnode_in_existing_nodes(node)) {2989num_nodes++;2990}2991}2992return num_nodes;2993}29942995size_t os::numa_get_leaf_groups(int *ids, size_t size) {2996size_t highest_node_number = Linux::numa_max_node();2997size_t i = 0;29982999// Map all node ids in which is possible to allocate memory. Also nodes are3000// not always consecutively available, i.e. available from 0 to the highest3001// node number.3002for (size_t node = 0; node <= highest_node_number; node++) {3003if (Linux::isnode_in_configured_nodes(node)) {3004ids[i++] = node;3005}3006}3007return i;3008}30093010bool os::get_page_info(char *start, page_info* info) {3011return false;3012}30133014char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {3015return end;3016}301730183019int os::Linux::sched_getcpu_syscall(void) {3020unsigned int cpu = 0;3021int retval = -1;30223023#if defined(AMD64)3024// Unfortunately we have to bring all these macros here from vsyscall.h3025// to be able to compile on old linuxes.3026# define __NR_vgetcpu 23027# define VSYSCALL_START (-10UL << 20)3028# define VSYSCALL_SIZE 10243029# define VSYSCALL_ADDR(vsyscall_nr) (VSYSCALL_START+VSYSCALL_SIZE*(vsyscall_nr))3030typedef long (*vgetcpu_t)(unsigned int *cpu, unsigned int *node, unsigned long *tcache);3031vgetcpu_t vgetcpu = (vgetcpu_t)VSYSCALL_ADDR(__NR_vgetcpu);3032retval = vgetcpu(&cpu, NULL, NULL);3033#elif defined(IA32) || defined(AARCH64)3034# ifndef SYS_getcpu3035# define SYS_getcpu AARCH64_ONLY(168) IA32_ONLY(318)3036# endif3037retval = syscall(SYS_getcpu, &cpu, NULL, NULL);3038#endif30393040return (retval == -1) ? retval : cpu;3041}30423043// Something to do with the numa-aware allocator needs these symbols3044extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }3045extern "C" JNIEXPORT void numa_error(char *where) { }3046extern "C" JNIEXPORT int fork1() { return fork(); }30473048// Handle request to load libnuma symbol version 1.1 (API v1). If it fails3049// load symbol from base version instead.3050void* os::Linux::libnuma_dlsym(void* handle, const char *name) {3051#ifndef __ANDROID__3052void *f = dlvsym(handle, name, "libnuma_1.1");3053#else3054void *f = NULL;3055#endif3056if (f == NULL) {3057f = dlsym(handle, name);3058}3059return f;3060}30613062// Handle request to load libnuma symbol version 1.2 (API v2) only.3063// Return NULL if the symbol is not defined in this particular version.3064void* os::Linux::libnuma_v2_dlsym(void* handle, const char* name) {3065#ifndef __ANDROID__3066return dlvsym(handle, name, "libnuma_1.2");3067#else // __ANDROID__3068return NULL;3069#endif // !__ANDROID__3070}30713072bool os::Linux::libnuma_init() {3073// sched_getcpu() should be in libc.3074set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,3075dlsym(RTLD_DEFAULT, "sched_getcpu")));30763077// If it's not, try a direct syscall.3078if (sched_getcpu() == -1)3079set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t, (void*)&sched_getcpu_syscall));30803081if (sched_getcpu() != -1) { // Does it work?3082void *handle = dlopen("libnuma.so.1", RTLD_LAZY);30833084if (handle == NULL) {3085handle = dlopen("libnuma.so", RTLD_LAZY);3086}30873088if (handle != NULL) {3089set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,3090libnuma_dlsym(handle, "numa_node_to_cpus")));3091set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,3092libnuma_dlsym(handle, "numa_max_node")));3093set_numa_num_configured_nodes(CAST_TO_FN_PTR(numa_num_configured_nodes_func_t,3094libnuma_dlsym(handle, "numa_num_configured_nodes")));3095set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,3096libnuma_dlsym(handle, "numa_available")));3097set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,3098libnuma_dlsym(handle, "numa_tonode_memory")));3099set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,3100libnuma_dlsym(handle, "numa_interleave_memory")));3101set_numa_interleave_memory_v2(CAST_TO_FN_PTR(numa_interleave_memory_v2_func_t,3102libnuma_v2_dlsym(handle, "numa_interleave_memory")));3103set_numa_set_bind_policy(CAST_TO_FN_PTR(numa_set_bind_policy_func_t,3104libnuma_dlsym(handle, "numa_set_bind_policy")));3105set_numa_bitmask_isbitset(CAST_TO_FN_PTR(numa_bitmask_isbitset_func_t,3106libnuma_dlsym(handle, "numa_bitmask_isbitset")));3107set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,3108libnuma_dlsym(handle, "numa_distance")));31093110if (numa_available() != -1) {3111set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));3112set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));3113set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));3114// Create an index -> node mapping, since nodes are not always consecutive3115_nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);3116rebuild_nindex_to_node_map();3117// Create a cpu -> node mapping3118_cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);3119rebuild_cpu_to_node_map();3120return true;3121}3122}3123}3124return false;3125}31263127void os::Linux::rebuild_nindex_to_node_map() {3128int highest_node_number = Linux::numa_max_node();31293130nindex_to_node()->clear();3131for (int node = 0; node <= highest_node_number; node++) {3132if (Linux::isnode_in_existing_nodes(node)) {3133nindex_to_node()->append(node);3134}3135}3136}31373138// rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.3139// The table is later used in get_node_by_cpu().3140void os::Linux::rebuild_cpu_to_node_map() {3141const size_t NCPUS = 32768; // Since the buffer size computation is very obscure3142// in libnuma (possible values are starting from 16,3143// and continuing up with every other power of 2, but less3144// than the maximum number of CPUs supported by kernel), and3145// is a subject to change (in libnuma version 2 the requirements3146// are more reasonable) we'll just hardcode the number they use3147// in the library.3148const size_t BitsPerCLong = sizeof(long) * CHAR_BIT;31493150size_t cpu_num = processor_count();3151size_t cpu_map_size = NCPUS / BitsPerCLong;3152size_t cpu_map_valid_size =3153MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);31543155cpu_to_node()->clear();3156cpu_to_node()->at_grow(cpu_num - 1);31573158size_t node_num = get_existing_num_nodes();31593160int distance = 0;3161int closest_distance = INT_MAX;3162int closest_node = 0;3163unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size, mtInternal);3164for (size_t i = 0; i < node_num; i++) {3165// Check if node is configured (not a memory-less node). If it is not, find3166// the closest configured node.3167if (!isnode_in_configured_nodes(nindex_to_node()->at(i))) {3168closest_distance = INT_MAX;3169// Check distance from all remaining nodes in the system. Ignore distance3170// from itself and from another non-configured node.3171for (size_t m = 0; m < node_num; m++) {3172if (m != i && isnode_in_configured_nodes(nindex_to_node()->at(m))) {3173distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));3174// If a closest node is found, update. There is always at least one3175// configured node in the system so there is always at least one node3176// close.3177if (distance != 0 && distance < closest_distance) {3178closest_distance = distance;3179closest_node = nindex_to_node()->at(m);3180}3181}3182}3183} else {3184// Current node is already a configured node.3185closest_node = nindex_to_node()->at(i);3186}31873188// Get cpus from the original node and map them to the closest node. If node3189// is a configured node (not a memory-less node), then original node and3190// closest node are the same.3191if (numa_node_to_cpus(nindex_to_node()->at(i), cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {3192for (size_t j = 0; j < cpu_map_valid_size; j++) {3193if (cpu_map[j] != 0) {3194for (size_t k = 0; k < BitsPerCLong; k++) {3195if (cpu_map[j] & (1UL << k)) {3196cpu_to_node()->at_put(j * BitsPerCLong + k, closest_node);3197}3198}3199}3200}3201}3202}3203FREE_C_HEAP_ARRAY(unsigned long, cpu_map, mtInternal);3204}32053206int os::Linux::get_node_by_cpu(int cpu_id) {3207if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {3208return cpu_to_node()->at(cpu_id);3209}3210return -1;3211}32123213GrowableArray<int>* os::Linux::_cpu_to_node;3214GrowableArray<int>* os::Linux::_nindex_to_node;3215os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;3216os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;3217os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;3218os::Linux::numa_num_configured_nodes_func_t os::Linux::_numa_num_configured_nodes;3219os::Linux::numa_available_func_t os::Linux::_numa_available;3220os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;3221os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;3222os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;3223os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;3224os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;3225os::Linux::numa_distance_func_t os::Linux::_numa_distance;3226unsigned long* os::Linux::_numa_all_nodes;3227struct bitmask* os::Linux::_numa_all_nodes_ptr;3228struct bitmask* os::Linux::_numa_nodes_ptr;32293230bool os::pd_uncommit_memory(char* addr, size_t size) {3231uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,3232MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);3233return res != (uintptr_t) MAP_FAILED;3234}32353236static3237address get_stack_commited_bottom(address bottom, size_t size) {3238address nbot = bottom;3239address ntop = bottom + size;32403241size_t page_sz = os::vm_page_size();3242unsigned pages = size / page_sz;32433244unsigned char vec[1];3245unsigned imin = 1, imax = pages + 1, imid;3246int mincore_return_value = 0;32473248assert(imin <= imax, "Unexpected page size");32493250while (imin < imax) {3251imid = (imax + imin) / 2;3252nbot = ntop - (imid * page_sz);32533254// Use a trick with mincore to check whether the page is mapped or not.3255// mincore sets vec to 1 if page resides in memory and to 0 if page3256// is swapped output but if page we are asking for is unmapped3257// it returns -1,ENOMEM3258mincore_return_value = mincore(nbot, page_sz, vec);32593260if (mincore_return_value == -1) {3261// Page is not mapped go up3262// to find first mapped page3263if (errno != EAGAIN) {3264assert(errno == ENOMEM, "Unexpected mincore errno");3265imax = imid;3266}3267} else {3268// Page is mapped go down3269// to find first not mapped page3270imin = imid + 1;3271}3272}32733274nbot = nbot + page_sz;32753276// Adjust stack bottom one page up if last checked page is not mapped3277if (mincore_return_value == -1) {3278nbot = nbot + page_sz;3279}32803281return nbot;3282}328332843285// Linux uses a growable mapping for the stack, and if the mapping for3286// the stack guard pages is not removed when we detach a thread the3287// stack cannot grow beyond the pages where the stack guard was3288// mapped. If at some point later in the process the stack expands to3289// that point, the Linux kernel cannot expand the stack any further3290// because the guard pages are in the way, and a segfault occurs.3291//3292// However, it's essential not to split the stack region by unmapping3293// a region (leaving a hole) that's already part of the stack mapping,3294// so if the stack mapping has already grown beyond the guard pages at3295// the time we create them, we have to truncate the stack mapping.3296// So, we need to know the extent of the stack mapping when3297// create_stack_guard_pages() is called.32983299// We only need this for stacks that are growable: at the time of3300// writing thread stacks don't use growable mappings (i.e. those3301// creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this3302// only applies to the main thread.33033304// If the (growable) stack mapping already extends beyond the point3305// where we're going to put our guard pages, truncate the mapping at3306// that point by munmap()ping it. This ensures that when we later3307// munmap() the guard pages we don't leave a hole in the stack3308// mapping. This only affects the main/primordial thread33093310bool os::pd_create_stack_guard_pages(char* addr, size_t size) {33113312if (os::is_primordial_thread()) {3313// As we manually grow stack up to bottom inside create_attached_thread(),3314// it's likely that os::Linux::initial_thread_stack_bottom is mapped and3315// we don't need to do anything special.3316// Check it first, before calling heavy function.3317uintptr_t stack_extent = (uintptr_t) os::Linux::initial_thread_stack_bottom();3318unsigned char vec[1];33193320if (mincore((address)stack_extent, os::vm_page_size(), vec) == -1) {3321// Fallback to slow path on all errors, including EAGAIN3322stack_extent = (uintptr_t) get_stack_commited_bottom(3323os::Linux::initial_thread_stack_bottom(),3324(size_t)addr - stack_extent);3325}33263327if (stack_extent < (uintptr_t)addr) {3328::munmap((void*)stack_extent, (uintptr_t)(addr - stack_extent));3329}3330}33313332return os::commit_memory(addr, size, !ExecMem);3333}33343335// If this is a growable mapping, remove the guard pages entirely by3336// munmap()ping them. If not, just call uncommit_memory(). This only3337// affects the main/primordial thread, but guard against future OS changes.3338// It's safe to always unmap guard pages for primordial thread because we3339// always place it right after end of the mapped region.33403341bool os::remove_stack_guard_pages(char* addr, size_t size) {3342uintptr_t stack_extent, stack_base;33433344if (os::is_primordial_thread()) {3345return ::munmap(addr, size) == 0;3346}33473348return os::uncommit_memory(addr, size);3349}33503351static address _highest_vm_reserved_address = NULL;33523353// If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory3354// at 'requested_addr'. If there are existing memory mappings at the same3355// location, however, they will be overwritten. If 'fixed' is false,3356// 'requested_addr' is only treated as a hint, the return value may or3357// may not start from the requested address. Unlike Linux mmap(), this3358// function returns NULL to indicate failure.3359static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {3360char * addr;3361int flags;33623363flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;3364if (fixed) {3365assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address");3366flags |= MAP_FIXED;3367}33683369// Map reserved/uncommitted pages PROT_NONE so we fail early if we3370// touch an uncommitted page. Otherwise, the read/write might3371// succeed if we have enough swap space to back the physical page.3372addr = (char*)::mmap(requested_addr, bytes, PROT_NONE,3373flags, -1, 0);33743375if (addr != MAP_FAILED) {3376// anon_mmap() should only get called during VM initialization,3377// don't need lock (actually we can skip locking even it can be called3378// from multiple threads, because _highest_vm_reserved_address is just a3379// hint about the upper limit of non-stack memory regions.)3380if ((address)addr + bytes > _highest_vm_reserved_address) {3381_highest_vm_reserved_address = (address)addr + bytes;3382}3383}33843385return addr == MAP_FAILED ? NULL : addr;3386}33873388// Allocate (using mmap, NO_RESERVE, with small pages) at either a given request address3389// (req_addr != NULL) or with a given alignment.3390// - bytes shall be a multiple of alignment.3391// - req_addr can be NULL. If not NULL, it must be a multiple of alignment.3392// - alignment sets the alignment at which memory shall be allocated.3393// It must be a multiple of allocation granularity.3394// Returns address of memory or NULL. If req_addr was not NULL, will only return3395// req_addr or NULL.3396static char* anon_mmap_aligned(size_t bytes, size_t alignment, char* req_addr) {33973398size_t extra_size = bytes;3399if (req_addr == NULL && alignment > 0) {3400extra_size += alignment;3401}34023403char* start = (char*) ::mmap(req_addr, extra_size, PROT_NONE,3404MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,3405-1, 0);3406if (start == MAP_FAILED) {3407start = NULL;3408} else {3409if (req_addr != NULL) {3410if (start != req_addr) {3411::munmap(start, extra_size);3412start = NULL;3413}3414} else {3415char* const start_aligned = (char*) align_ptr_up(start, alignment);3416char* const end_aligned = start_aligned + bytes;3417char* const end = start + extra_size;3418if (start_aligned > start) {3419::munmap(start, start_aligned - start);3420}3421if (end_aligned < end) {3422::munmap(end_aligned, end - end_aligned);3423}3424start = start_aligned;3425}3426}3427return start;3428}34293430// Don't update _highest_vm_reserved_address, because there might be memory3431// regions above addr + size. If so, releasing a memory region only creates3432// a hole in the address space, it doesn't help prevent heap-stack collision.3433//3434static int anon_munmap(char * addr, size_t size) {3435return ::munmap(addr, size) == 0;3436}34373438char* os::pd_reserve_memory(size_t bytes, char* requested_addr,3439size_t alignment_hint) {3440return anon_mmap(requested_addr, bytes, (requested_addr != NULL));3441}34423443bool os::pd_release_memory(char* addr, size_t size) {3444return anon_munmap(addr, size);3445}34463447static address highest_vm_reserved_address() {3448return _highest_vm_reserved_address;3449}34503451static bool linux_mprotect(char* addr, size_t size, int prot) {3452// Linux wants the mprotect address argument to be page aligned.3453char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size());34543455// According to SUSv3, mprotect() should only be used with mappings3456// established by mmap(), and mmap() always maps whole pages. Unaligned3457// 'addr' likely indicates problem in the VM (e.g. trying to change3458// protection of malloc'ed or statically allocated memory). Check the3459// caller if you hit this assert.3460assert(addr == bottom, "sanity check");34613462size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());3463return ::mprotect(bottom, size, prot) == 0;3464}34653466// Set protections specified3467bool os::protect_memory(char* addr, size_t bytes, ProtType prot,3468bool is_committed) {3469unsigned int p = 0;3470switch (prot) {3471case MEM_PROT_NONE: p = PROT_NONE; break;3472case MEM_PROT_READ: p = PROT_READ; break;3473case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break;3474case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break;3475default:3476ShouldNotReachHere();3477}3478// is_committed is unused.3479return linux_mprotect(addr, bytes, p);3480}34813482bool os::guard_memory(char* addr, size_t size) {3483return linux_mprotect(addr, size, PROT_NONE);3484}34853486bool os::unguard_memory(char* addr, size_t size) {3487return linux_mprotect(addr, size, PROT_READ|PROT_WRITE);3488}34893490bool os::Linux::transparent_huge_pages_sanity_check(bool warn, size_t page_size) {3491bool result = false;3492void *p = mmap(NULL, page_size * 2, PROT_READ|PROT_WRITE,3493MAP_ANONYMOUS|MAP_PRIVATE,3494-1, 0);3495if (p != MAP_FAILED) {3496void *aligned_p = align_ptr_up(p, page_size);34973498result = madvise(aligned_p, page_size, MADV_HUGEPAGE) == 0;34993500munmap(p, page_size * 2);3501}35023503if (warn && !result) {3504warning("TransparentHugePages is not supported by the operating system.");3505}35063507return result;3508}35093510bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) {3511bool result = false;3512void *p = mmap(NULL, page_size, PROT_READ|PROT_WRITE,3513MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB,3514-1, 0);35153516if (p != MAP_FAILED) {3517// We don't know if this really is a huge page or not.3518FILE *fp = fopen("/proc/self/maps", "r");3519if (fp) {3520while (!feof(fp)) {3521char chars[257];3522long x = 0;3523if (fgets(chars, sizeof(chars), fp)) {3524if (sscanf(chars, "%lx-%*x", &x) == 13525&& x == (long)p) {3526if (strstr (chars, "hugepage")) {3527result = true;3528break;3529}3530}3531}3532}3533fclose(fp);3534}3535munmap(p, page_size);3536}35373538if (warn && !result) {3539warning("HugeTLBFS is not supported by the operating system.");3540}35413542return result;3543}35443545/*3546* Set the coredump_filter bits to include largepages in core dump (bit 6)3547*3548* From the coredump_filter documentation:3549*3550* - (bit 0) anonymous private memory3551* - (bit 1) anonymous shared memory3552* - (bit 2) file-backed private memory3553* - (bit 3) file-backed shared memory3554* - (bit 4) ELF header pages in file-backed private memory areas (it is3555* effective only if the bit 2 is cleared)3556* - (bit 5) hugetlb private memory3557* - (bit 6) hugetlb shared memory3558*/3559static void set_coredump_filter(void) {3560FILE *f;3561long cdm;35623563if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) {3564return;3565}35663567if (fscanf(f, "%lx", &cdm) != 1) {3568fclose(f);3569return;3570}35713572rewind(f);35733574if ((cdm & LARGEPAGES_BIT) == 0) {3575cdm |= LARGEPAGES_BIT;3576fprintf(f, "%#lx", cdm);3577}35783579fclose(f);3580}35813582// Large page support35833584static size_t _large_page_size = 0;35853586size_t os::Linux::find_large_page_size() {3587size_t large_page_size = 0;35883589// large_page_size on Linux is used to round up heap size. x86 uses either3590// 2M or 4M page, depending on whether PAE (Physical Address Extensions)3591// mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use3592// page as large as 256M.3593//3594// Here we try to figure out page size by parsing /proc/meminfo and looking3595// for a line with the following format:3596// Hugepagesize: 2048 kB3597//3598// If we can't determine the value (e.g. /proc is not mounted, or the text3599// format has been changed), we'll use the largest page size supported by3600// the processor.36013602#ifndef ZERO3603large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)3604ARM_ONLY(2 * M) PPC_ONLY(4 * M) AARCH64_ONLY(2 * M);3605#endif // ZERO36063607FILE *fp = fopen("/proc/meminfo", "r");3608if (fp) {3609while (!feof(fp)) {3610int x = 0;3611char buf[16];3612if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {3613if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {3614large_page_size = x * K;3615break;3616}3617} else {3618// skip to next line3619for (;;) {3620int ch = fgetc(fp);3621if (ch == EOF || ch == (int)'\n') break;3622}3623}3624}3625fclose(fp);3626}36273628if (!FLAG_IS_DEFAULT(LargePageSizeInBytes) && LargePageSizeInBytes != large_page_size) {3629warning("Setting LargePageSizeInBytes has no effect on this OS. Large page size is "3630SIZE_FORMAT "%s.", byte_size_in_proper_unit(large_page_size),3631proper_unit_for_byte_size(large_page_size));3632}36333634return large_page_size;3635}36363637size_t os::Linux::setup_large_page_size() {3638_large_page_size = Linux::find_large_page_size();3639const size_t default_page_size = (size_t)Linux::page_size();3640if (_large_page_size > default_page_size) {3641_page_sizes[0] = _large_page_size;3642_page_sizes[1] = default_page_size;3643_page_sizes[2] = 0;3644}36453646return _large_page_size;3647}36483649bool os::Linux::setup_large_page_type(size_t page_size) {3650if (FLAG_IS_DEFAULT(UseHugeTLBFS) &&3651FLAG_IS_DEFAULT(UseSHM) &&3652FLAG_IS_DEFAULT(UseTransparentHugePages)) {36533654// The type of large pages has not been specified by the user.36553656// Try UseHugeTLBFS and then UseSHM.3657UseHugeTLBFS = UseSHM = true;36583659// Don't try UseTransparentHugePages since there are known3660// performance issues with it turned on. This might change in the future.3661UseTransparentHugePages = false;3662}36633664if (UseTransparentHugePages) {3665bool warn_on_failure = !FLAG_IS_DEFAULT(UseTransparentHugePages);3666if (transparent_huge_pages_sanity_check(warn_on_failure, page_size)) {3667UseHugeTLBFS = false;3668UseSHM = false;3669return true;3670}3671UseTransparentHugePages = false;3672}36733674if (UseHugeTLBFS) {3675bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS);3676if (hugetlbfs_sanity_check(warn_on_failure, page_size)) {3677UseSHM = false;3678return true;3679}3680UseHugeTLBFS = false;3681}36823683#ifdef DISABLE_SHM3684if (UseSHM) {3685warning("UseSHM is disabled");3686UseSHM = false;3687}3688#endif //DISABLE_SHM36893690return UseSHM;3691}36923693void os::large_page_init() {3694if (!UseLargePages &&3695!UseTransparentHugePages &&3696!UseHugeTLBFS &&3697!UseSHM) {3698// Not using large pages.3699return;3700}37013702if (!FLAG_IS_DEFAULT(UseLargePages) && !UseLargePages) {3703// The user explicitly turned off large pages.3704// Ignore the rest of the large pages flags.3705UseTransparentHugePages = false;3706UseHugeTLBFS = false;3707UseSHM = false;3708return;3709}37103711size_t large_page_size = Linux::setup_large_page_size();3712UseLargePages = Linux::setup_large_page_type(large_page_size);37133714set_coredump_filter();3715}37163717#ifndef SHM_HUGETLB3718#define SHM_HUGETLB 040003719#endif37203721#ifndef DISABLE_SHM3722#define shm_warning_format(format, ...) \3723do { \3724if (UseLargePages && \3725(!FLAG_IS_DEFAULT(UseLargePages) || \3726!FLAG_IS_DEFAULT(UseSHM) || \3727!FLAG_IS_DEFAULT(LargePageSizeInBytes))) { \3728warning(format, __VA_ARGS__); \3729} \3730} while (0)37313732#define shm_warning(str) shm_warning_format("%s", str)37333734#define shm_warning_with_errno(str) \3735do { \3736int err = errno; \3737shm_warning_format(str " (error = %d)", err); \3738} while (0)37393740static char* shmat_with_alignment(int shmid, size_t bytes, size_t alignment) {3741assert(is_size_aligned(bytes, alignment), "Must be divisible by the alignment");37423743if (!is_size_aligned(alignment, SHMLBA)) {3744assert(false, "Code below assumes that alignment is at least SHMLBA aligned");3745return NULL;3746}37473748// To ensure that we get 'alignment' aligned memory from shmat,3749// we pre-reserve aligned virtual memory and then attach to that.37503751char* pre_reserved_addr = anon_mmap_aligned(bytes, alignment, NULL);3752if (pre_reserved_addr == NULL) {3753// Couldn't pre-reserve aligned memory.3754shm_warning("Failed to pre-reserve aligned memory for shmat.");3755return NULL;3756}37573758// SHM_REMAP is needed to allow shmat to map over an existing mapping.3759char* addr = (char*)shmat(shmid, pre_reserved_addr, SHM_REMAP);37603761if ((intptr_t)addr == -1) {3762int err = errno;3763shm_warning_with_errno("Failed to attach shared memory.");37643765assert(err != EACCES, "Unexpected error");3766assert(err != EIDRM, "Unexpected error");3767assert(err != EINVAL, "Unexpected error");37683769// Since we don't know if the kernel unmapped the pre-reserved memory area3770// we can't unmap it, since that would potentially unmap memory that was3771// mapped from other threads.3772return NULL;3773}37743775return addr;3776}37773778static char* shmat_at_address(int shmid, char* req_addr) {3779if (!is_ptr_aligned(req_addr, SHMLBA)) {3780assert(false, "Requested address needs to be SHMLBA aligned");3781return NULL;3782}37833784char* addr = (char*)shmat(shmid, req_addr, 0);37853786if ((intptr_t)addr == -1) {3787shm_warning_with_errno("Failed to attach shared memory.");3788return NULL;3789}37903791return addr;3792}37933794static char* shmat_large_pages(int shmid, size_t bytes, size_t alignment, char* req_addr) {3795// If a req_addr has been provided, we assume that the caller has already aligned the address.3796if (req_addr != NULL) {3797assert(is_ptr_aligned(req_addr, os::large_page_size()), "Must be divisible by the large page size");3798assert(is_ptr_aligned(req_addr, alignment), "Must be divisible by given alignment");3799return shmat_at_address(shmid, req_addr);3800}38013802// Since shmid has been setup with SHM_HUGETLB, shmat will automatically3803// return large page size aligned memory addresses when req_addr == NULL.3804// However, if the alignment is larger than the large page size, we have3805// to manually ensure that the memory returned is 'alignment' aligned.3806if (alignment > os::large_page_size()) {3807assert(is_size_aligned(alignment, os::large_page_size()), "Must be divisible by the large page size");3808return shmat_with_alignment(shmid, bytes, alignment);3809} else {3810return shmat_at_address(shmid, NULL);3811}3812}3813#endif // !DISABLE_SHM38143815char* os::Linux::reserve_memory_special_shm(size_t bytes, size_t alignment, char* req_addr, bool exec) {3816#ifndef DISABLE_SHM3817// "exec" is passed in but not used. Creating the shared image for3818// the code cache doesn't have an SHM_X executable permission to check.3819assert(UseLargePages && UseSHM, "only for SHM large pages");3820assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address");3821assert(is_ptr_aligned(req_addr, alignment), "Unaligned address");38223823if (!is_size_aligned(bytes, os::large_page_size())) {3824return NULL; // Fallback to small pages.3825}38263827// Create a large shared memory region to attach to based on size.3828// Currently, size is the total size of the heap.3829int shmid = shmget(IPC_PRIVATE, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);3830if (shmid == -1) {3831// Possible reasons for shmget failure:3832// 1. shmmax is too small for Java heap.3833// > check shmmax value: cat /proc/sys/kernel/shmmax3834// > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax3835// 2. not enough large page memory.3836// > check available large pages: cat /proc/meminfo3837// > increase amount of large pages:3838// echo new_value > /proc/sys/vm/nr_hugepages3839// Note 1: different Linux may use different name for this property,3840// e.g. on Redhat AS-3 it is "hugetlb_pool".3841// Note 2: it's possible there's enough physical memory available but3842// they are so fragmented after a long run that they can't3843// coalesce into large pages. Try to reserve large pages when3844// the system is still "fresh".3845shm_warning_with_errno("Failed to reserve shared memory.");3846return NULL;3847}38483849// Attach to the region.3850char* addr = shmat_large_pages(shmid, bytes, alignment, req_addr);38513852// Remove shmid. If shmat() is successful, the actual shared memory segment3853// will be deleted when it's detached by shmdt() or when the process3854// terminates. If shmat() is not successful this will remove the shared3855// segment immediately.3856shmctl(shmid, IPC_RMID, NULL);38573858return addr;3859#else3860assert(0, "SHM was disabled on compile time");3861return NULL;3862#endif3863}38643865static void warn_on_large_pages_failure(char* req_addr, size_t bytes, int error) {3866assert(error == ENOMEM, "Only expect to fail if no memory is available");38673868bool warn_on_failure = UseLargePages &&3869(!FLAG_IS_DEFAULT(UseLargePages) ||3870!FLAG_IS_DEFAULT(UseHugeTLBFS) ||3871!FLAG_IS_DEFAULT(LargePageSizeInBytes));38723873if (warn_on_failure) {3874char msg[128];3875jio_snprintf(msg, sizeof(msg), "Failed to reserve large pages memory req_addr: "3876PTR_FORMAT " bytes: " SIZE_FORMAT " (errno = %d).", req_addr, bytes, error);3877warning("%s", msg);3878}3879}38803881char* os::Linux::reserve_memory_special_huge_tlbfs_only(size_t bytes, char* req_addr, bool exec) {3882assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages");3883assert(is_size_aligned(bytes, os::large_page_size()), "Unaligned size");3884assert(is_ptr_aligned(req_addr, os::large_page_size()), "Unaligned address");38853886int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;3887char* addr = (char*)::mmap(req_addr, bytes, prot,3888MAP_PRIVATE|MAP_ANONYMOUS|MAP_HUGETLB,3889-1, 0);38903891if (addr == MAP_FAILED) {3892warn_on_large_pages_failure(req_addr, bytes, errno);3893return NULL;3894}38953896assert(is_ptr_aligned(addr, os::large_page_size()), "Must be");38973898return addr;3899}39003901// Reserve memory using mmap(MAP_HUGETLB).3902// - bytes shall be a multiple of alignment.3903// - req_addr can be NULL. If not NULL, it must be a multiple of alignment.3904// - alignment sets the alignment at which memory shall be allocated.3905// It must be a multiple of allocation granularity.3906// Returns address of memory or NULL. If req_addr was not NULL, will only return3907// req_addr or NULL.3908char* os::Linux::reserve_memory_special_huge_tlbfs_mixed(size_t bytes, size_t alignment, char* req_addr, bool exec) {3909size_t large_page_size = os::large_page_size();3910assert(bytes >= large_page_size, "Shouldn't allocate large pages for small sizes");39113912assert(is_ptr_aligned(req_addr, alignment), "Must be");3913assert(is_size_aligned(bytes, alignment), "Must be");39143915// First reserve - but not commit - the address range in small pages.3916char* const start = anon_mmap_aligned(bytes, alignment, req_addr);39173918if (start == NULL) {3919return NULL;3920}39213922assert(is_ptr_aligned(start, alignment), "Must be");39233924char* end = start + bytes;39253926// Find the regions of the allocated chunk that can be promoted to large pages.3927char* lp_start = (char*)align_ptr_up(start, large_page_size);3928char* lp_end = (char*)align_ptr_down(end, large_page_size);39293930size_t lp_bytes = lp_end - lp_start;39313932assert(is_size_aligned(lp_bytes, large_page_size), "Must be");39333934if (lp_bytes == 0) {3935// The mapped region doesn't even span the start and the end of a large page.3936// Fall back to allocate a non-special area.3937::munmap(start, end - start);3938return NULL;3939}39403941int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;39423943void* result;39443945// Commit small-paged leading area.3946if (start != lp_start) {3947result = ::mmap(start, lp_start - start, prot,3948MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,3949-1, 0);3950if (result == MAP_FAILED) {3951::munmap(lp_start, end - lp_start);3952return NULL;3953}3954}39553956// Commit large-paged area.3957result = ::mmap(lp_start, lp_bytes, prot,3958MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED|MAP_HUGETLB,3959-1, 0);3960if (result == MAP_FAILED) {3961warn_on_large_pages_failure(lp_start, lp_bytes, errno);3962// If the mmap above fails, the large pages region will be unmapped and we3963// have regions before and after with small pages. Release these regions.3964//3965// | mapped | unmapped | mapped |3966// ^ ^ ^ ^3967// start lp_start lp_end end3968//3969::munmap(start, lp_start - start);3970::munmap(lp_end, end - lp_end);3971return NULL;3972}39733974// Commit small-paged trailing area.3975if (lp_end != end) {3976result = ::mmap(lp_end, end - lp_end, prot,3977MAP_PRIVATE|MAP_ANONYMOUS|MAP_FIXED,3978-1, 0);3979if (result == MAP_FAILED) {3980::munmap(start, lp_end - start);3981return NULL;3982}3983}39843985return start;3986}39873988char* os::Linux::reserve_memory_special_huge_tlbfs(size_t bytes, size_t alignment, char* req_addr, bool exec) {3989assert(UseLargePages && UseHugeTLBFS, "only for Huge TLBFS large pages");3990assert(is_ptr_aligned(req_addr, alignment), "Must be");3991assert(is_size_aligned(alignment, os::vm_allocation_granularity()), "Must be");3992assert(is_power_of_2(os::large_page_size()), "Must be");3993assert(bytes >= os::large_page_size(), "Shouldn't allocate large pages for small sizes");39943995if (is_size_aligned(bytes, os::large_page_size()) && alignment <= os::large_page_size()) {3996return reserve_memory_special_huge_tlbfs_only(bytes, req_addr, exec);3997} else {3998return reserve_memory_special_huge_tlbfs_mixed(bytes, alignment, req_addr, exec);3999}4000}40014002char* os::reserve_memory_special(size_t bytes, size_t alignment, char* req_addr, bool exec) {4003assert(UseLargePages, "only for large pages");40044005char* addr;4006if (UseSHM) {4007addr = os::Linux::reserve_memory_special_shm(bytes, alignment, req_addr, exec);4008} else {4009assert(UseHugeTLBFS, "must be");4010addr = os::Linux::reserve_memory_special_huge_tlbfs(bytes, alignment, req_addr, exec);4011}40124013if (addr != NULL) {4014if (UseNUMAInterleaving) {4015numa_make_global(addr, bytes);4016}40174018// The memory is committed4019MemTracker::record_virtual_memory_reserve_and_commit((address)addr, bytes, CALLER_PC);4020}40214022return addr;4023}40244025bool os::Linux::release_memory_special_shm(char* base, size_t bytes) {4026#ifndef DISABLE_SHM4027// detaching the SHM segment will also delete it, see reserve_memory_special_shm()4028return shmdt(base) == 0;4029#else4030assert(0, "SHM was disabled on compile time");4031#endif4032}40334034bool os::Linux::release_memory_special_huge_tlbfs(char* base, size_t bytes) {4035return pd_release_memory(base, bytes);4036}40374038bool os::release_memory_special(char* base, size_t bytes) {4039bool res;4040if (MemTracker::tracking_level() > NMT_minimal) {4041Tracker tkr = MemTracker::get_virtual_memory_release_tracker();4042res = os::Linux::release_memory_special_impl(base, bytes);4043if (res) {4044tkr.record((address)base, bytes);4045}40464047} else {4048res = os::Linux::release_memory_special_impl(base, bytes);4049}4050return res;4051}40524053bool os::Linux::release_memory_special_impl(char* base, size_t bytes) {4054assert(UseLargePages, "only for large pages");4055bool res;40564057if (UseSHM) {4058res = os::Linux::release_memory_special_shm(base, bytes);4059} else {4060assert(UseHugeTLBFS, "must be");4061res = os::Linux::release_memory_special_huge_tlbfs(base, bytes);4062}4063return res;4064}40654066size_t os::large_page_size() {4067return _large_page_size;4068}40694070// With SysV SHM the entire memory region must be allocated as shared4071// memory.4072// HugeTLBFS allows application to commit large page memory on demand.4073// However, when committing memory with HugeTLBFS fails, the region4074// that was supposed to be committed will lose the old reservation4075// and allow other threads to steal that memory region. Because of this4076// behavior we can't commit HugeTLBFS memory.4077bool os::can_commit_large_page_memory() {4078return UseTransparentHugePages;4079}40804081bool os::can_execute_large_page_memory() {4082return UseTransparentHugePages || UseHugeTLBFS;4083}40844085// Reserve memory at an arbitrary address, only if that area is4086// available (and not reserved for something else).40874088char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {4089const int max_tries = 10;4090char* base[max_tries];4091size_t size[max_tries];4092const size_t gap = 0x000000;40934094// Assert only that the size is a multiple of the page size, since4095// that's all that mmap requires, and since that's all we really know4096// about at this low abstraction level. If we need higher alignment,4097// we can either pass an alignment to this method or verify alignment4098// in one of the methods further up the call chain. See bug 5044738.4099assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");41004101// Repeatedly allocate blocks until the block is allocated at the4102// right spot. Give up after max_tries. Note that reserve_memory() will4103// automatically update _highest_vm_reserved_address if the call is4104// successful. The variable tracks the highest memory address every reserved4105// by JVM. It is used to detect heap-stack collision if running with4106// fixed-stack LinuxThreads. Because here we may attempt to reserve more4107// space than needed, it could confuse the collision detecting code. To4108// solve the problem, save current _highest_vm_reserved_address and4109// calculate the correct value before return.4110address old_highest = _highest_vm_reserved_address;41114112// Linux mmap allows caller to pass an address as hint; give it a try first,4113// if kernel honors the hint then we can return immediately.4114char * addr = anon_mmap(requested_addr, bytes, false);4115if (addr == requested_addr) {4116return requested_addr;4117}41184119if (addr != NULL) {4120// mmap() is successful but it fails to reserve at the requested address4121anon_munmap(addr, bytes);4122}41234124int i;4125for (i = 0; i < max_tries; ++i) {4126base[i] = reserve_memory(bytes);41274128if (base[i] != NULL) {4129// Is this the block we wanted?4130if (base[i] == requested_addr) {4131size[i] = bytes;4132break;4133}41344135// Does this overlap the block we wanted? Give back the overlapped4136// parts and try again.41374138size_t top_overlap = requested_addr + (bytes + gap) - base[i];4139if (top_overlap >= 0 && top_overlap < bytes) {4140unmap_memory(base[i], top_overlap);4141base[i] += top_overlap;4142size[i] = bytes - top_overlap;4143} else {4144size_t bottom_overlap = base[i] + bytes - requested_addr;4145if (bottom_overlap >= 0 && bottom_overlap < bytes) {4146unmap_memory(requested_addr, bottom_overlap);4147size[i] = bytes - bottom_overlap;4148} else {4149size[i] = bytes;4150}4151}4152}4153}41544155// Give back the unused reserved pieces.41564157for (int j = 0; j < i; ++j) {4158if (base[j] != NULL) {4159unmap_memory(base[j], size[j]);4160}4161}41624163if (i < max_tries) {4164_highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);4165return requested_addr;4166} else {4167_highest_vm_reserved_address = old_highest;4168return NULL;4169}4170}41714172size_t os::read(int fd, void *buf, unsigned int nBytes) {4173return ::read(fd, buf, nBytes);4174}41754176size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {4177return ::pread(fd, buf, nBytes, offset);4178}41794180// TODO-FIXME: reconcile Solaris' os::sleep with the linux variation.4181// Solaris uses poll(), linux uses park().4182// Poll() is likely a better choice, assuming that Thread.interrupt()4183// generates a SIGUSRx signal. Note that SIGUSR1 can interfere with4184// SIGSEGV, see 4355769.41854186int os::sleep(Thread* thread, jlong millis, bool interruptible) {4187assert(thread == Thread::current(), "thread consistency check");41884189ParkEvent * const slp = thread->_SleepEvent ;4190slp->reset() ;4191OrderAccess::fence() ;41924193if (interruptible) {4194jlong prevtime = javaTimeNanos();41954196for (;;) {4197if (os::is_interrupted(thread, true)) {4198return OS_INTRPT;4199}42004201jlong newtime = javaTimeNanos();42024203if (newtime - prevtime < 0) {4204// time moving backwards, should only happen if no monotonic clock4205// not a guarantee() because JVM should not abort on kernel/glibc bugs4206assert(!Linux::supports_monotonic_clock(), "time moving backwards");4207} else {4208millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;4209}42104211if(millis <= 0) {4212return OS_OK;4213}42144215prevtime = newtime;42164217{4218assert(thread->is_Java_thread(), "sanity check");4219JavaThread *jt = (JavaThread *) thread;4220ThreadBlockInVM tbivm(jt);4221OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);42224223jt->set_suspend_equivalent();4224// cleared by handle_special_suspend_equivalent_condition() or4225// java_suspend_self() via check_and_wait_while_suspended()42264227slp->park(millis);42284229// were we externally suspended while we were waiting?4230jt->check_and_wait_while_suspended();4231}4232}4233} else {4234OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);4235jlong prevtime = javaTimeNanos();42364237for (;;) {4238// It'd be nice to avoid the back-to-back javaTimeNanos() calls on4239// the 1st iteration ...4240jlong newtime = javaTimeNanos();42414242if (newtime - prevtime < 0) {4243// time moving backwards, should only happen if no monotonic clock4244// not a guarantee() because JVM should not abort on kernel/glibc bugs4245assert(!Linux::supports_monotonic_clock(), "time moving backwards");4246} else {4247millis -= (newtime - prevtime) / NANOSECS_PER_MILLISEC;4248}42494250if(millis <= 0) break ;42514252prevtime = newtime;4253slp->park(millis);4254}4255return OS_OK ;4256}4257}42584259//4260// Short sleep, direct OS call.4261//4262// Note: certain versions of Linux CFS scheduler (since 2.6.23) do not guarantee4263// sched_yield(2) will actually give up the CPU:4264//4265// * Alone on this pariticular CPU, keeps running.4266// * Before the introduction of "skip_buddy" with "compat_yield" disabled4267// (pre 2.6.39).4268//4269// So calling this with 0 is an alternative.4270//4271void os::naked_short_sleep(jlong ms) {4272struct timespec req;42734274assert(ms < 1000, "Un-interruptable sleep, short time use only");4275req.tv_sec = 0;4276if (ms > 0) {4277req.tv_nsec = (ms % 1000) * 1000000;4278}4279else {4280req.tv_nsec = 1;4281}42824283nanosleep(&req, NULL);42844285return;4286}42874288// Sleep forever; naked call to OS-specific sleep; use with CAUTION4289void os::infinite_sleep() {4290while (true) { // sleep forever ...4291::sleep(100); // ... 100 seconds at a time4292}4293}42944295// Used to convert frequent JVM_Yield() to nops4296bool os::dont_yield() {4297return DontYieldALot;4298}42994300void os::yield() {4301sched_yield();4302}43034304os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}43054306void os::yield_all(int attempts) {4307// Yields to all threads, including threads with lower priorities4308// Threads on Linux are all with same priority. The Solaris style4309// os::yield_all() with nanosleep(1ms) is not necessary.4310sched_yield();4311}43124313// Called from the tight loops to possibly influence time-sharing heuristics4314void os::loop_breaker(int attempts) {4315os::yield_all(attempts);4316}43174318////////////////////////////////////////////////////////////////////////////////4319// thread priority support43204321// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER4322// only supports dynamic priority, static priority must be zero. For real-time4323// applications, Linux supports SCHED_RR which allows static priority (1-99).4324// However, for large multi-threaded applications, SCHED_RR is not only slower4325// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out4326// of 5 runs - Sep 2005).4327//4328// The following code actually changes the niceness of kernel-thread/LWP. It4329// has an assumption that setpriority() only modifies one kernel-thread/LWP,4330// not the entire user process, and user level threads are 1:1 mapped to kernel4331// threads. It has always been the case, but could change in the future. For4332// this reason, the code should not be used as default (ThreadPriorityPolicy=0).4333// It is only used when ThreadPriorityPolicy=1 and requires root privilege.43344335int os::java_to_os_priority[CriticalPriority + 1] = {433619, // 0 Entry should never be used433743384, // 1 MinPriority43393, // 243402, // 3434143421, // 443430, // 5 NormPriority4344-1, // 643454346-2, // 74347-3, // 84348-4, // 9 NearMaxPriority43494350-5, // 10 MaxPriority43514352-5 // 11 CriticalPriority4353};43544355static int prio_init() {4356if (ThreadPriorityPolicy == 1) {4357// Only root can raise thread priority. Don't allow ThreadPriorityPolicy=14358// if effective uid is not root. Perhaps, a more elegant way of doing4359// this is to test CAP_SYS_NICE capability, but that will require libcap.so4360if (geteuid() != 0) {4361if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {4362warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");4363}4364ThreadPriorityPolicy = 0;4365}4366}4367if (UseCriticalJavaThreadPriority) {4368os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];4369}4370return 0;4371}43724373OSReturn os::set_native_priority(Thread* thread, int newpri) {4374if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;43754376int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);4377return (ret == 0) ? OS_OK : OS_ERR;4378}43794380OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) {4381if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) {4382*priority_ptr = java_to_os_priority[NormPriority];4383return OS_OK;4384}43854386errno = 0;4387*priority_ptr = getpriority(PRIO_PROCESS, thread->osthread()->thread_id());4388return (*priority_ptr != -1 || errno == 0 ? OS_OK : OS_ERR);4389}43904391// Hint to the underlying OS that a task switch would not be good.4392// Void return because it's a hint and can fail.4393void os::hint_no_preempt() {}43944395////////////////////////////////////////////////////////////////////////////////4396// suspend/resume support43974398// the low-level signal-based suspend/resume support is a remnant from the4399// old VM-suspension that used to be for java-suspension, safepoints etc,4400// within hotspot. Now there is a single use-case for this:4401// - calling get_thread_pc() on the VMThread by the flat-profiler task4402// that runs in the watcher thread.4403// The remaining code is greatly simplified from the more general suspension4404// code that used to be used.4405//4406// The protocol is quite simple:4407// - suspend:4408// - sends a signal to the target thread4409// - polls the suspend state of the osthread using a yield loop4410// - target thread signal handler (SR_handler) sets suspend state4411// and blocks in sigsuspend until continued4412// - resume:4413// - sets target osthread state to continue4414// - sends signal to end the sigsuspend loop in the SR_handler4415//4416// Note that the SR_lock plays no role in this suspend/resume protocol.4417//44184419static void resume_clear_context(OSThread *osthread) {4420osthread->set_ucontext(NULL);4421osthread->set_siginfo(NULL);4422}44234424static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {4425osthread->set_ucontext(context);4426osthread->set_siginfo(siginfo);4427}44284429//4430// Handler function invoked when a thread's execution is suspended or4431// resumed. We have to be careful that only async-safe functions are4432// called here (Note: most pthread functions are not async safe and4433// should be avoided.)4434//4435// Note: sigwait() is a more natural fit than sigsuspend() from an4436// interface point of view, but sigwait() prevents the signal hander4437// from being run. libpthread would get very confused by not having4438// its signal handlers run and prevents sigwait()'s use with the4439// mutex granting granting signal.4440//4441// Currently only ever called on the VMThread and JavaThreads (PC sampling)4442//4443static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {4444// Save and restore errno to avoid confusing native code with EINTR4445// after sigsuspend.4446int old_errno = errno;44474448Thread* thread = Thread::current();4449OSThread* osthread = thread->osthread();4450assert(thread->is_VM_thread() || thread->is_Java_thread(), "Must be VMThread or JavaThread");44514452os::SuspendResume::State current = osthread->sr.state();4453if (current == os::SuspendResume::SR_SUSPEND_REQUEST) {4454suspend_save_context(osthread, siginfo, context);44554456// attempt to switch the state, we assume we had a SUSPEND_REQUEST4457os::SuspendResume::State state = osthread->sr.suspended();4458if (state == os::SuspendResume::SR_SUSPENDED) {4459sigset_t suspend_set; // signals for sigsuspend()44604461// get current set of blocked signals and unblock resume signal4462pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);4463sigdelset(&suspend_set, SR_signum);44644465sr_semaphore.signal();4466// wait here until we are resumed4467while (1) {4468sigsuspend(&suspend_set);44694470os::SuspendResume::State result = osthread->sr.running();4471if (result == os::SuspendResume::SR_RUNNING) {4472sr_semaphore.signal();4473break;4474}4475}44764477} else if (state == os::SuspendResume::SR_RUNNING) {4478// request was cancelled, continue4479} else {4480ShouldNotReachHere();4481}44824483resume_clear_context(osthread);4484} else if (current == os::SuspendResume::SR_RUNNING) {4485// request was cancelled, continue4486} else if (current == os::SuspendResume::SR_WAKEUP_REQUEST) {4487// ignore4488} else {4489// ignore4490}44914492errno = old_errno;4493}449444954496static int SR_initialize() {4497struct sigaction act;4498char *s;4499/* Get signal number to use for suspend/resume */4500if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {4501int sig = ::strtol(s, 0, 10);4502if (sig > 0 || sig < _NSIG) {4503SR_signum = sig;4504}4505}45064507assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,4508"SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");45094510sigemptyset(&SR_sigset);4511sigaddset(&SR_sigset, SR_signum);45124513/* Set up signal handler for suspend/resume */4514act.sa_flags = SA_RESTART|SA_SIGINFO;4515act.sa_handler = (void (*)(int)) SR_handler;45164517// SR_signum is blocked by default.4518// 4528190 - We also need to block pthread restart signal (32 on all4519// supported Linux platforms). Note that LinuxThreads need to block4520// this signal for all threads to work properly. So we don't have4521// to use hard-coded signal number when setting up the mask.4522pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);45234524if (sigaction(SR_signum, &act, 0) == -1) {4525return -1;4526}45274528// Save signal flag4529os::Linux::set_our_sigflags(SR_signum, act.sa_flags);4530return 0;4531}45324533static int sr_notify(OSThread* osthread) {4534int status = pthread_kill(osthread->pthread_id(), SR_signum);4535assert_status(status == 0, status, "pthread_kill");4536return status;4537}45384539// "Randomly" selected value for how long we want to spin4540// before bailing out on suspending a thread, also how often4541// we send a signal to a thread we want to resume4542static const int RANDOMLY_LARGE_INTEGER = 1000000;4543static const int RANDOMLY_LARGE_INTEGER2 = 100;45444545// returns true on success and false on error - really an error is fatal4546// but this seems the normal response to library errors4547static bool do_suspend(OSThread* osthread) {4548assert(osthread->sr.is_running(), "thread should be running");4549assert(!sr_semaphore.trywait(), "semaphore has invalid state");45504551// mark as suspended and send signal4552if (osthread->sr.request_suspend() != os::SuspendResume::SR_SUSPEND_REQUEST) {4553// failed to switch, state wasn't running?4554ShouldNotReachHere();4555return false;4556}45574558if (sr_notify(osthread) != 0) {4559ShouldNotReachHere();4560}45614562// managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED4563while (true) {4564if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {4565break;4566} else {4567// timeout4568os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();4569if (cancelled == os::SuspendResume::SR_RUNNING) {4570return false;4571} else if (cancelled == os::SuspendResume::SR_SUSPENDED) {4572// make sure that we consume the signal on the semaphore as well4573sr_semaphore.wait();4574break;4575} else {4576ShouldNotReachHere();4577return false;4578}4579}4580}45814582guarantee(osthread->sr.is_suspended(), "Must be suspended");4583return true;4584}45854586static void do_resume(OSThread* osthread) {4587assert(osthread->sr.is_suspended(), "thread should be suspended");4588assert(!sr_semaphore.trywait(), "invalid semaphore state");45894590if (osthread->sr.request_wakeup() != os::SuspendResume::SR_WAKEUP_REQUEST) {4591// failed to switch to WAKEUP_REQUEST4592ShouldNotReachHere();4593return;4594}45954596while (true) {4597if (sr_notify(osthread) == 0) {4598if (sr_semaphore.timedwait(0, 2 * NANOSECS_PER_MILLISEC)) {4599if (osthread->sr.is_running()) {4600return;4601}4602}4603} else {4604ShouldNotReachHere();4605}4606}46074608guarantee(osthread->sr.is_running(), "Must be running!");4609}46104611////////////////////////////////////////////////////////////////////////////////4612// interrupt support46134614void os::interrupt(Thread* thread) {4615assert(Thread::current() == thread || Threads_lock->owned_by_self(),4616"possibility of dangling Thread pointer");46174618OSThread* osthread = thread->osthread();46194620if (!osthread->interrupted()) {4621osthread->set_interrupted(true);4622// More than one thread can get here with the same value of osthread,4623// resulting in multiple notifications. We do, however, want the store4624// to interrupted() to be visible to other threads before we execute unpark().4625OrderAccess::fence();4626ParkEvent * const slp = thread->_SleepEvent ;4627if (slp != NULL) slp->unpark() ;4628}46294630// For JSR166. Unpark even if interrupt status already was set4631if (thread->is_Java_thread())4632((JavaThread*)thread)->parker()->unpark();46334634ParkEvent * ev = thread->_ParkEvent ;4635if (ev != NULL) ev->unpark() ;46364637}46384639bool os::is_interrupted(Thread* thread, bool clear_interrupted) {4640assert(Thread::current() == thread || Threads_lock->owned_by_self(),4641"possibility of dangling Thread pointer");46424643OSThread* osthread = thread->osthread();46444645bool interrupted = osthread->interrupted();46464647if (interrupted && clear_interrupted) {4648osthread->set_interrupted(false);4649// consider thread->_SleepEvent->reset() ... optional optimization4650}46514652return interrupted;4653}46544655///////////////////////////////////////////////////////////////////////////////////4656// signal handling (except suspend/resume)46574658// This routine may be used by user applications as a "hook" to catch signals.4659// The user-defined signal handler must pass unrecognized signals to this4660// routine, and if it returns true (non-zero), then the signal handler must4661// return immediately. If the flag "abort_if_unrecognized" is true, then this4662// routine will never retun false (zero), but instead will execute a VM panic4663// routine kill the process.4664//4665// If this routine returns false, it is OK to call it again. This allows4666// the user-defined signal handler to perform checks either before or after4667// the VM performs its own checks. Naturally, the user code would be making4668// a serious error if it tried to handle an exception (such as a null check4669// or breakpoint) that the VM was generating for its own correct operation.4670//4671// This routine may recognize any of the following kinds of signals:4672// SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.4673// It should be consulted by handlers for any of those signals.4674//4675// The caller of this routine must pass in the three arguments supplied4676// to the function referred to in the "sa_sigaction" (not the "sa_handler")4677// field of the structure passed to sigaction(). This routine assumes that4678// the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.4679//4680// Note that the VM will print warnings if it detects conflicting signal4681// handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".4682//4683extern "C" JNIEXPORT int4684JVM_handle_linux_signal(int signo, siginfo_t* siginfo,4685void* ucontext, int abort_if_unrecognized);46864687void signalHandler(int sig, siginfo_t* info, void* uc) {4688assert(info != NULL && uc != NULL, "it must be old kernel");4689int orig_errno = errno; // Preserve errno value over signal handler.4690JVM_handle_linux_signal(sig, info, uc, true);4691errno = orig_errno;4692}469346944695// This boolean allows users to forward their own non-matching signals4696// to JVM_handle_linux_signal, harmlessly.4697bool os::Linux::signal_handlers_are_installed = false;46984699// For signal-chaining4700struct sigaction os::Linux::sigact[MAXSIGNUM];4701unsigned int os::Linux::sigs = 0;4702bool os::Linux::libjsig_is_loaded = false;4703typedef struct sigaction *(*get_signal_t)(int);4704get_signal_t os::Linux::get_signal_action = NULL;47054706struct sigaction* os::Linux::get_chained_signal_action(int sig) {4707struct sigaction *actp = NULL;47084709if (libjsig_is_loaded) {4710// Retrieve the old signal handler from libjsig4711actp = (*get_signal_action)(sig);4712}4713if (actp == NULL) {4714// Retrieve the preinstalled signal handler from jvm4715actp = get_preinstalled_handler(sig);4716}47174718return actp;4719}47204721static bool call_chained_handler(struct sigaction *actp, int sig,4722siginfo_t *siginfo, void *context) {4723// Call the old signal handler4724if (actp->sa_handler == SIG_DFL) {4725// It's more reasonable to let jvm treat it as an unexpected exception4726// instead of taking the default action.4727return false;4728} else if (actp->sa_handler != SIG_IGN) {4729if ((actp->sa_flags & SA_NODEFER) == 0) {4730// automaticlly block the signal4731sigaddset(&(actp->sa_mask), sig);4732}47334734sa_handler_t hand = NULL;4735sa_sigaction_t sa = NULL;4736bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;4737// retrieve the chained handler4738if (siginfo_flag_set) {4739sa = actp->sa_sigaction;4740} else {4741hand = actp->sa_handler;4742}47434744if ((actp->sa_flags & SA_RESETHAND) != 0) {4745actp->sa_handler = SIG_DFL;4746}47474748// try to honor the signal mask4749sigset_t oset;4750pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset);47514752// call into the chained handler4753if (siginfo_flag_set) {4754(*sa)(sig, siginfo, context);4755} else {4756(*hand)(sig);4757}47584759// restore the signal mask4760pthread_sigmask(SIG_SETMASK, &oset, 0);4761}4762// Tell jvm's signal handler the signal is taken care of.4763return true;4764}47654766bool os::Linux::chained_handler(int sig, siginfo_t* siginfo, void* context) {4767bool chained = false;4768// signal-chaining4769if (UseSignalChaining) {4770struct sigaction *actp = get_chained_signal_action(sig);4771if (actp != NULL) {4772chained = call_chained_handler(actp, sig, siginfo, context);4773}4774}4775return chained;4776}47774778struct sigaction* os::Linux::get_preinstalled_handler(int sig) {4779if ((( (unsigned int)1 << sig ) & sigs) != 0) {4780return &sigact[sig];4781}4782return NULL;4783}47844785void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) {4786assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4787sigact[sig] = oldAct;4788sigs |= (unsigned int)1 << sig;4789}47904791// for diagnostic4792int os::Linux::sigflags[MAXSIGNUM];47934794int os::Linux::get_our_sigflags(int sig) {4795assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4796return sigflags[sig];4797}47984799void os::Linux::set_our_sigflags(int sig, int flags) {4800assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4801sigflags[sig] = flags;4802}48034804void os::Linux::set_signal_handler(int sig, bool set_installed) {4805// Check for overwrite.4806struct sigaction oldAct;4807sigaction(sig, (struct sigaction*)NULL, &oldAct);48084809void* oldhand = oldAct.sa_sigaction4810? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)4811: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);4812if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&4813oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&4814oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {4815if (AllowUserSignalHandlers || !set_installed) {4816// Do not overwrite; user takes responsibility to forward to us.4817return;4818} else if (UseSignalChaining) {4819// save the old handler in jvm4820save_preinstalled_handler(sig, oldAct);4821// libjsig also interposes the sigaction() call below and saves the4822// old sigaction on it own.4823} else {4824fatal(err_msg("Encountered unexpected pre-existing sigaction handler "4825"%#lx for signal %d.", (long)oldhand, sig));4826}4827}48284829struct sigaction sigAct;4830sigfillset(&(sigAct.sa_mask));4831sigAct.sa_handler = SIG_DFL;4832if (!set_installed) {4833sigAct.sa_flags = SA_SIGINFO|SA_RESTART;4834} else {4835sigAct.sa_sigaction = signalHandler;4836sigAct.sa_flags = SA_SIGINFO|SA_RESTART;4837}4838// Save flags, which are set by ours4839assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");4840sigflags[sig] = sigAct.sa_flags;48414842int ret = sigaction(sig, &sigAct, &oldAct);4843assert(ret == 0, "check");48444845void* oldhand2 = oldAct.sa_sigaction4846? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)4847: CAST_FROM_FN_PTR(void*, oldAct.sa_handler);4848assert(oldhand2 == oldhand, "no concurrent signal handler installation");4849}48504851// install signal handlers for signals that HotSpot needs to4852// handle in order to support Java-level exception handling.48534854void os::Linux::install_signal_handlers() {4855if (!signal_handlers_are_installed) {4856signal_handlers_are_installed = true;48574858// signal-chaining4859typedef void (*signal_setting_t)();4860signal_setting_t begin_signal_setting = NULL;4861signal_setting_t end_signal_setting = NULL;4862begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,4863dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));4864if (begin_signal_setting != NULL) {4865end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,4866dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));4867get_signal_action = CAST_TO_FN_PTR(get_signal_t,4868dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));4869libjsig_is_loaded = true;4870assert(UseSignalChaining, "should enable signal-chaining");4871}4872if (libjsig_is_loaded) {4873// Tell libjsig jvm is setting signal handlers4874(*begin_signal_setting)();4875}48764877set_signal_handler(SIGSEGV, true);4878set_signal_handler(SIGPIPE, true);4879set_signal_handler(SIGBUS, true);4880set_signal_handler(SIGILL, true);4881set_signal_handler(SIGFPE, true);4882#if defined(PPC64)4883set_signal_handler(SIGTRAP, true);4884#endif4885set_signal_handler(SIGXFSZ, true);48864887if (libjsig_is_loaded) {4888// Tell libjsig jvm finishes setting signal handlers4889(*end_signal_setting)();4890}48914892// We don't activate signal checker if libjsig is in place, we trust ourselves4893// and if UserSignalHandler is installed all bets are off.4894// Log that signal checking is off only if -verbose:jni is specified.4895if (CheckJNICalls) {4896if (libjsig_is_loaded) {4897if (PrintJNIResolving) {4898tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");4899}4900check_signals = false;4901}4902if (AllowUserSignalHandlers) {4903if (PrintJNIResolving) {4904tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");4905}4906check_signals = false;4907}4908}4909}4910}49114912// This is the fastest way to get thread cpu time on Linux.4913// Returns cpu time (user+sys) for any thread, not only for current.4914// POSIX compliant clocks are implemented in the kernels 2.6.16+.4915// It might work on 2.6.10+ with a special kernel/glibc patch.4916// For reference, please, see IEEE Std 1003.1-2004:4917// http://www.unix.org/single_unix_specification49184919jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {4920struct timespec tp;4921int rc = os::Linux::clock_gettime(clockid, &tp);4922assert(rc == 0, "clock_gettime is expected to return 0 code");49234924return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;4925}49264927/////4928// glibc on Linux platform uses non-documented flag4929// to indicate, that some special sort of signal4930// trampoline is used.4931// We will never set this flag, and we should4932// ignore this flag in our diagnostic4933#ifdef SIGNIFICANT_SIGNAL_MASK4934#undef SIGNIFICANT_SIGNAL_MASK4935#endif4936#define SIGNIFICANT_SIGNAL_MASK (~0x04000000)49374938static const char* get_signal_handler_name(address handler,4939char* buf, int buflen) {4940int offset = 0;4941bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);4942if (found) {4943// skip directory names4944const char *p1, *p2;4945p1 = buf;4946size_t len = strlen(os::file_separator());4947while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;4948jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);4949} else {4950jio_snprintf(buf, buflen, PTR_FORMAT, handler);4951}4952return buf;4953}49544955static void print_signal_handler(outputStream* st, int sig,4956char* buf, size_t buflen) {4957struct sigaction sa;49584959sigaction(sig, NULL, &sa);49604961// See comment for SIGNIFICANT_SIGNAL_MASK define4962sa.sa_flags &= SIGNIFICANT_SIGNAL_MASK;49634964st->print("%s: ", os::exception_name(sig, buf, buflen));49654966address handler = (sa.sa_flags & SA_SIGINFO)4967? CAST_FROM_FN_PTR(address, sa.sa_sigaction)4968: CAST_FROM_FN_PTR(address, sa.sa_handler);49694970if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {4971st->print("SIG_DFL");4972} else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {4973st->print("SIG_IGN");4974} else {4975st->print("[%s]", get_signal_handler_name(handler, buf, buflen));4976}49774978st->print(", sa_mask[0]=");4979os::Posix::print_signal_set_short(st, &sa.sa_mask);49804981address rh = VMError::get_resetted_sighandler(sig);4982// May be, handler was resetted by VMError?4983if(rh != NULL) {4984handler = rh;4985sa.sa_flags = VMError::get_resetted_sigflags(sig) & SIGNIFICANT_SIGNAL_MASK;4986}49874988st->print(", sa_flags=");4989os::Posix::print_sa_flags(st, sa.sa_flags);49904991// Check: is it our handler?4992if(handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler) ||4993handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) {4994// It is our signal handler4995// check for flags, reset system-used one!4996if((int)sa.sa_flags != os::Linux::get_our_sigflags(sig)) {4997st->print(4998", flags was changed from " PTR32_FORMAT ", consider using jsig library",4999os::Linux::get_our_sigflags(sig));5000}5001}5002st->cr();5003}500450055006#define DO_SIGNAL_CHECK(sig) \5007if (!sigismember(&check_signal_done, sig)) \5008os::Linux::check_signal_handler(sig)50095010// This method is a periodic task to check for misbehaving JNI applications5011// under CheckJNI, we can add any periodic checks here50125013void os::run_periodic_checks() {50145015if (check_signals == false) return;50165017// SEGV and BUS if overridden could potentially prevent5018// generation of hs*.log in the event of a crash, debugging5019// such a case can be very challenging, so we absolutely5020// check the following for a good measure:5021DO_SIGNAL_CHECK(SIGSEGV);5022DO_SIGNAL_CHECK(SIGILL);5023DO_SIGNAL_CHECK(SIGFPE);5024DO_SIGNAL_CHECK(SIGBUS);5025DO_SIGNAL_CHECK(SIGPIPE);5026DO_SIGNAL_CHECK(SIGXFSZ);5027#if defined(PPC64)5028DO_SIGNAL_CHECK(SIGTRAP);5029#endif50305031// ReduceSignalUsage allows the user to override these handlers5032// see comments at the very top and jvm_solaris.h5033if (!ReduceSignalUsage) {5034DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);5035DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);5036DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);5037DO_SIGNAL_CHECK(BREAK_SIGNAL);5038}50395040DO_SIGNAL_CHECK(SR_signum);5041DO_SIGNAL_CHECK(INTERRUPT_SIGNAL);5042}50435044typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);50455046static os_sigaction_t os_sigaction = NULL;50475048void os::Linux::check_signal_handler(int sig) {5049char buf[O_BUFLEN];5050address jvmHandler = NULL;505150525053struct sigaction act;5054if (os_sigaction == NULL) {5055// only trust the default sigaction, in case it has been interposed5056os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");5057if (os_sigaction == NULL) return;5058}50595060os_sigaction(sig, (struct sigaction*)NULL, &act);506150625063act.sa_flags &= SIGNIFICANT_SIGNAL_MASK;50645065address thisHandler = (act.sa_flags & SA_SIGINFO)5066? CAST_FROM_FN_PTR(address, act.sa_sigaction)5067: CAST_FROM_FN_PTR(address, act.sa_handler) ;506850695070switch(sig) {5071case SIGSEGV:5072case SIGBUS:5073case SIGFPE:5074case SIGPIPE:5075case SIGILL:5076case SIGXFSZ:5077jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler);5078break;50795080case SHUTDOWN1_SIGNAL:5081case SHUTDOWN2_SIGNAL:5082case SHUTDOWN3_SIGNAL:5083case BREAK_SIGNAL:5084jvmHandler = (address)user_handler();5085break;50865087case INTERRUPT_SIGNAL:5088jvmHandler = CAST_FROM_FN_PTR(address, SIG_DFL);5089break;50905091default:5092if (sig == SR_signum) {5093jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler);5094} else {5095return;5096}5097break;5098}50995100if (thisHandler != jvmHandler) {5101tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));5102tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));5103tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));5104// No need to check this sig any longer5105sigaddset(&check_signal_done, sig);5106// Running under non-interactive shell, SHUTDOWN2_SIGNAL will be reassigned SIG_IGN5107if (sig == SHUTDOWN2_SIGNAL && !isatty(fileno(stdin))) {5108tty->print_cr("Running in non-interactive shell, %s handler is replaced by shell",5109exception_name(sig, buf, O_BUFLEN));5110}5111} else if(os::Linux::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Linux::get_our_sigflags(sig)) {5112tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));5113tty->print("expected:" PTR32_FORMAT, os::Linux::get_our_sigflags(sig));5114tty->print_cr(" found:" PTR32_FORMAT, act.sa_flags);5115// No need to check this sig any longer5116sigaddset(&check_signal_done, sig);5117}51185119// Dump all the signal5120if (sigismember(&check_signal_done, sig)) {5121print_signal_handlers(tty, buf, O_BUFLEN);5122}5123}51245125extern void report_error(char* file_name, int line_no, char* title, char* format, ...);51265127extern bool signal_name(int signo, char* buf, size_t len);51285129const char* os::exception_name(int exception_code, char* buf, size_t size) {5130if (0 < exception_code && exception_code <= SIGRTMAX) {5131// signal5132if (!signal_name(exception_code, buf, size)) {5133jio_snprintf(buf, size, "SIG%d", exception_code);5134}5135return buf;5136} else {5137return NULL;5138}5139}51405141// this is called _before_ most of the global arguments have been parsed5142void os::init(void) {5143char dummy; /* used to get a guess on initial stack address */51445145// With LinuxThreads the JavaMain thread pid (primordial thread)5146// is different than the pid of the java launcher thread.5147// So, on Linux, the launcher thread pid is passed to the VM5148// via the sun.java.launcher.pid property.5149// Use this property instead of getpid() if it was correctly passed.5150// See bug 6351349.5151pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();51525153_initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();51545155clock_tics_per_sec = sysconf(_SC_CLK_TCK);51565157init_random(1234567);51585159ThreadCritical::initialize();51605161Linux::set_page_size(sysconf(_SC_PAGESIZE));5162if (Linux::page_size() == -1) {5163fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",5164strerror(errno)));5165}5166init_page_sizes((size_t) Linux::page_size());51675168Linux::initialize_system_info();51695170// _main_thread points to the thread that created/loaded the JVM.5171Linux::_main_thread = pthread_self();51725173Linux::clock_init();5174initial_time_count = javaTimeNanos();51755176// pthread_condattr initialization for monotonic clock5177int status;5178pthread_condattr_t* _condattr = os::Linux::condAttr();5179if ((status = pthread_condattr_init(_condattr)) != 0) {5180fatal(err_msg("pthread_condattr_init: %s", strerror(status)));5181}5182// Only set the clock if CLOCK_MONOTONIC is available5183if (Linux::supports_monotonic_clock()) {5184if ((status = pthread_condattr_setclock(_condattr, CLOCK_MONOTONIC)) != 0) {5185if (status == EINVAL) {5186warning("Unable to use monotonic clock with relative timed-waits" \5187" - changes to the time-of-day clock may have adverse affects");5188} else {5189fatal(err_msg("pthread_condattr_setclock: %s", strerror(status)));5190}5191}5192}5193// else it defaults to CLOCK_REALTIME51945195pthread_mutex_init(&dl_mutex, NULL);51965197// If the pagesize of the VM is greater than 8K determine the appropriate5198// number of initial guard pages. The user can change this with the5199// command line arguments, if needed.5200if (vm_page_size() > (int)Linux::vm_default_page_size()) {5201StackYellowPages = 1;5202StackRedPages = 1;5203StackShadowPages = round_to((StackShadowPages*Linux::vm_default_page_size()), vm_page_size()) / vm_page_size();5204}52055206// retrieve entry point for pthread_setname_np5207Linux::_pthread_setname_np =5208(int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");52095210}52115212// To install functions for atexit system call5213extern "C" {5214static void perfMemory_exit_helper() {5215perfMemory_exit();5216}5217}52185219void os::pd_init_container_support() {5220OSContainer::init();5221}52225223// this is called _after_ the global arguments have been parsed5224jint os::init_2(void)5225{5226Linux::fast_thread_clock_init();52275228// Allocate a single page and mark it as readable for safepoint polling5229address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);5230guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" );52315232os::set_polling_page( polling_page );52335234#ifndef PRODUCT5235if(Verbose && PrintMiscellaneous)5236tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);5237#endif52385239if (!UseMembar) {5240address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);5241guarantee( mem_serialize_page != MAP_FAILED, "mmap Failed for memory serialize page");5242os::set_memory_serialize_page( mem_serialize_page );52435244#ifndef PRODUCT5245if(Verbose && PrintMiscellaneous)5246tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);5247#endif5248}52495250// initialize suspend/resume support - must do this before signal_sets_init()5251if (SR_initialize() != 0) {5252perror("SR_initialize failed");5253return JNI_ERR;5254}52555256Linux::signal_sets_init();5257Linux::install_signal_handlers();52585259// Check minimum allowable stack size for thread creation and to initialize5260// the java system classes, including StackOverflowError - depends on page5261// size. Add a page for compiler2 recursion in main thread.5262// Add in 2*BytesPerWord times page size to account for VM stack during5263// class initialization depending on 32 or 64 bit VM.5264os::Linux::min_stack_allowed = MAX2(os::Linux::min_stack_allowed,5265(size_t)(StackYellowPages+StackRedPages+StackShadowPages) * Linux::page_size() +5266(2*BytesPerWord COMPILER2_PRESENT(+1)) * Linux::vm_default_page_size());52675268size_t threadStackSizeInBytes = ThreadStackSize * K;5269if (threadStackSizeInBytes != 0 &&5270threadStackSizeInBytes < os::Linux::min_stack_allowed) {5271tty->print_cr("\nThe stack size specified is too small, "5272"Specify at least %dk",5273os::Linux::min_stack_allowed/ K);5274return JNI_ERR;5275}52765277// Make the stack size a multiple of the page size so that5278// the yellow/red zones can be guarded.5279JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,5280vm_page_size()));52815282Linux::capture_initial_stack(JavaThread::stack_size_at_create());52835284#if defined(IA32) && !defined(ZERO)5285workaround_expand_exec_shield_cs_limit();5286#endif52875288Linux::libpthread_init();5289if (PrintMiscellaneous && (Verbose || WizardMode)) {5290tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",5291Linux::glibc_version(), Linux::libpthread_version(),5292Linux::is_floating_stack() ? "floating stack" : "fixed stack");5293}52945295if (UseNUMA) {5296if (!Linux::libnuma_init()) {5297UseNUMA = false;5298} else {5299if ((Linux::numa_max_node() < 1)) {5300// There's only one node(they start from 0), disable NUMA.5301UseNUMA = false;5302}5303}5304// With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way5305// we can make the adaptive lgrp chunk resizing work. If the user specified5306// both UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn and5307// disable adaptive resizing.5308if (UseNUMA && UseLargePages && !can_commit_large_page_memory()) {5309if (FLAG_IS_DEFAULT(UseNUMA)) {5310UseNUMA = false;5311} else {5312if (FLAG_IS_DEFAULT(UseLargePages) &&5313FLAG_IS_DEFAULT(UseSHM) &&5314FLAG_IS_DEFAULT(UseHugeTLBFS)) {5315UseLargePages = false;5316} else {5317warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, disabling adaptive resizing");5318UseAdaptiveSizePolicy = false;5319UseAdaptiveNUMAChunkSizing = false;5320}5321}5322}5323if (!UseNUMA && ForceNUMA) {5324UseNUMA = true;5325}5326}53275328if (MaxFDLimit) {5329// set the number of file descriptors to max. print out error5330// if getrlimit/setrlimit fails but continue regardless.5331struct rlimit nbr_files;5332int status = getrlimit(RLIMIT_NOFILE, &nbr_files);5333if (status != 0) {5334if (PrintMiscellaneous && (Verbose || WizardMode))5335perror("os::init_2 getrlimit failed");5336} else {5337nbr_files.rlim_cur = nbr_files.rlim_max;5338status = setrlimit(RLIMIT_NOFILE, &nbr_files);5339if (status != 0) {5340if (PrintMiscellaneous && (Verbose || WizardMode))5341perror("os::init_2 setrlimit failed");5342}5343}5344}53455346// Initialize lock used to serialize thread creation (see os::create_thread)5347Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));53485349// at-exit methods are called in the reverse order of their registration.5350// atexit functions are called on return from main or as a result of a5351// call to exit(3C). There can be only 32 of these functions registered5352// and atexit() does not set errno.53535354if (PerfAllowAtExitRegistration) {5355// only register atexit functions if PerfAllowAtExitRegistration is set.5356// atexit functions can be delayed until process exit time, which5357// can be problematic for embedded VM situations. Embedded VMs should5358// call DestroyJavaVM() to assure that VM resources are released.53595360// note: perfMemory_exit_helper atexit function may be removed in5361// the future if the appropriate cleanup code can be added to the5362// VM_Exit VMOperation's doit method.5363if (atexit(perfMemory_exit_helper) != 0) {5364warning("os::init_2 atexit(perfMemory_exit_helper) failed");5365}5366}53675368// initialize thread priority policy5369prio_init();53705371return JNI_OK;5372}53735374// Mark the polling page as unreadable5375void os::make_polling_page_unreadable(void) {5376if( !guard_memory((char*)_polling_page, Linux::page_size()) )5377fatal("Could not disable polling page");5378};53795380// Mark the polling page as readable5381void os::make_polling_page_readable(void) {5382if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {5383fatal("Could not enable polling page");5384}5385};53865387static int os_cpu_count(const cpu_set_t* cpus) {5388int count = 0;5389// only look up to the number of configured processors5390for (int i = 0; i < os::processor_count(); i++) {5391if (CPU_ISSET(i, cpus)) {5392count++;5393}5394}5395return count;5396}53975398// Get the current number of available processors for this process.5399// This value can change at any time during a process's lifetime.5400// sched_getaffinity gives an accurate answer as it accounts for cpusets.5401// If anything goes wrong we fallback to returning the number of online5402// processors - which can be greater than the number available to the process.5403int os::Linux::active_processor_count() {5404cpu_set_t cpus; // can represent at most 1024 (CPU_SETSIZE) processors5405int cpus_size = sizeof(cpu_set_t);5406int cpu_count = 0;54075408// pid 0 means the current thread - which we have to assume represents the process5409if (sched_getaffinity(0, cpus_size, &cpus) == 0) {5410cpu_count = os_cpu_count(&cpus);5411if (PrintActiveCpus) {5412tty->print_cr("active_processor_count: sched_getaffinity processor count: %d", cpu_count);5413}5414}5415else {5416cpu_count = ::sysconf(_SC_NPROCESSORS_ONLN);5417warning("sched_getaffinity failed (%s)- using online processor count (%d) "5418"which may exceed available processors", strerror(errno), cpu_count);5419}54205421assert(cpu_count > 0 && cpu_count <= os::processor_count(), "sanity check");5422return cpu_count;5423}54245425// Determine the active processor count from one of5426// three different sources:5427//5428// 1. User option -XX:ActiveProcessorCount5429// 2. kernel os calls (sched_getaffinity or sysconf(_SC_NPROCESSORS_ONLN)5430// 3. extracted from cgroup cpu subsystem (shares and quotas)5431//5432// Option 1, if specified, will always override.5433// If the cgroup subsystem is active and configured, we5434// will return the min of the cgroup and option 2 results.5435// This is required since tools, such as numactl, that5436// alter cpu affinity do not update cgroup subsystem5437// cpuset configuration files.5438int os::active_processor_count() {5439// User has overridden the number of active processors5440if (ActiveProcessorCount > 0) {5441if (PrintActiveCpus) {5442tty->print_cr("active_processor_count: "5443"active processor count set by user : %d",5444ActiveProcessorCount);5445}5446return ActiveProcessorCount;5447}54485449int active_cpus;5450if (OSContainer::is_containerized()) {5451active_cpus = OSContainer::active_processor_count();5452if (PrintActiveCpus) {5453tty->print_cr("active_processor_count: determined by OSContainer: %d",5454active_cpus);5455}5456} else {5457active_cpus = os::Linux::active_processor_count();5458}54595460return active_cpus;5461}54625463void os::set_native_thread_name(const char *name) {5464if (Linux::_pthread_setname_np) {5465char buf [16]; // according to glibc manpage, 16 chars incl. '/0'5466snprintf(buf, sizeof(buf), "%s", name);5467buf[sizeof(buf) - 1] = '\0';5468const int rc = Linux::_pthread_setname_np(pthread_self(), buf);5469// ERANGE should not happen; all other errors should just be ignored.5470assert(rc != ERANGE, "pthread_setname_np failed");5471}5472}54735474bool os::distribute_processes(uint length, uint* distribution) {5475// Not yet implemented.5476return false;5477}54785479bool os::bind_to_processor(uint processor_id) {5480// Not yet implemented.5481return false;5482}54835484///54855486void os::SuspendedThreadTask::internal_do_task() {5487if (do_suspend(_thread->osthread())) {5488SuspendedThreadTaskContext context(_thread, _thread->osthread()->ucontext());5489do_task(context);5490do_resume(_thread->osthread());5491}5492}54935494class PcFetcher : public os::SuspendedThreadTask {5495public:5496PcFetcher(Thread* thread) : os::SuspendedThreadTask(thread) {}5497ExtendedPC result();5498protected:5499void do_task(const os::SuspendedThreadTaskContext& context);5500private:5501ExtendedPC _epc;5502};55035504ExtendedPC PcFetcher::result() {5505guarantee(is_done(), "task is not done yet.");5506return _epc;5507}55085509void PcFetcher::do_task(const os::SuspendedThreadTaskContext& context) {5510Thread* thread = context.thread();5511OSThread* osthread = thread->osthread();5512if (osthread->ucontext() != NULL) {5513_epc = os::Linux::ucontext_get_pc((ucontext_t *) context.ucontext());5514} else {5515// NULL context is unexpected, double-check this is the VMThread5516guarantee(thread->is_VM_thread(), "can only be called for VMThread");5517}5518}55195520// Suspends the target using the signal mechanism and then grabs the PC before5521// resuming the target. Used by the flat-profiler only5522ExtendedPC os::get_thread_pc(Thread* thread) {5523// Make sure that it is called by the watcher for the VMThread5524assert(Thread::current()->is_Watcher_thread(), "Must be watcher");5525assert(thread->is_VM_thread(), "Can only be called for VMThread");55265527PcFetcher fetcher(thread);5528fetcher.run();5529return fetcher.result();5530}55315532int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)5533{5534if (is_NPTL()) {5535return pthread_cond_timedwait(_cond, _mutex, _abstime);5536} else {5537// 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control5538// word back to default 64bit precision if condvar is signaled. Java5539// wants 53bit precision. Save and restore current value.5540int fpu = get_fpu_control_word();5541int status = pthread_cond_timedwait(_cond, _mutex, _abstime);5542set_fpu_control_word(fpu);5543return status;5544}5545}55465547////////////////////////////////////////////////////////////////////////////////5548// debug support55495550bool os::find(address addr, outputStream* st) {5551Dl_info dlinfo;5552memset(&dlinfo, 0, sizeof(dlinfo));5553if (dladdr(addr, &dlinfo) != 0) {5554st->print(PTR_FORMAT ": ", addr);5555if (dlinfo.dli_sname != NULL && dlinfo.dli_saddr != NULL) {5556st->print("%s+%#x", dlinfo.dli_sname,5557addr - (intptr_t)dlinfo.dli_saddr);5558} else if (dlinfo.dli_fbase != NULL) {5559st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);5560} else {5561st->print("<absolute address>");5562}5563if (dlinfo.dli_fname != NULL) {5564st->print(" in %s", dlinfo.dli_fname);5565}5566if (dlinfo.dli_fbase != NULL) {5567st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);5568}5569st->cr();55705571if (Verbose) {5572// decode some bytes around the PC5573address begin = clamp_address_in_page(addr-40, addr, os::vm_page_size());5574address end = clamp_address_in_page(addr+40, addr, os::vm_page_size());5575address lowest = (address) dlinfo.dli_sname;5576if (!lowest) lowest = (address) dlinfo.dli_fbase;5577if (begin < lowest) begin = lowest;5578Dl_info dlinfo2;5579if (dladdr(end, &dlinfo2) != 0 && dlinfo2.dli_saddr != dlinfo.dli_saddr5580&& end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)5581end = (address) dlinfo2.dli_saddr;5582Disassembler::decode(begin, end, st);5583}5584return true;5585}5586return false;5587}55885589////////////////////////////////////////////////////////////////////////////////5590// misc55915592// This does not do anything on Linux. This is basically a hook for being5593// able to use structured exception handling (thread-local exception filters)5594// on, e.g., Win32.5595void5596os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,5597JavaCallArguments* args, Thread* thread) {5598f(value, method, args, thread);5599}56005601void os::print_statistics() {5602}56035604int os::message_box(const char* title, const char* message) {5605int i;5606fdStream err(defaultStream::error_fd());5607for (i = 0; i < 78; i++) err.print_raw("=");5608err.cr();5609err.print_raw_cr(title);5610for (i = 0; i < 78; i++) err.print_raw("-");5611err.cr();5612err.print_raw_cr(message);5613for (i = 0; i < 78; i++) err.print_raw("=");5614err.cr();56155616char buf[16];5617// Prevent process from exiting upon "read error" without consuming all CPU5618while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }56195620return buf[0] == 'y' || buf[0] == 'Y';5621}56225623int os::stat(const char *path, struct stat *sbuf) {5624char pathbuf[MAX_PATH];5625if (strlen(path) > MAX_PATH - 1) {5626errno = ENAMETOOLONG;5627return -1;5628}5629os::native_path(strcpy(pathbuf, path));5630return ::stat(pathbuf, sbuf);5631}56325633bool os::check_heap(bool force) {5634return true;5635}56365637int local_vsnprintf(char* buf, size_t count, const char* format, va_list args) {5638return ::vsnprintf(buf, count, format, args);5639}56405641// Is a (classpath) directory empty?5642bool os::dir_is_empty(const char* path) {5643DIR *dir = NULL;5644struct dirent *ptr;56455646dir = opendir(path);5647if (dir == NULL) return true;56485649/* Scan the directory */5650bool result = true;5651while (result && (ptr = readdir(dir)) != NULL) {5652if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {5653result = false;5654}5655}5656closedir(dir);5657return result;5658}56595660// This code originates from JDK's sysOpen and open64_w5661// from src/solaris/hpi/src/system_md.c56625663#ifndef O_DELETE5664#define O_DELETE 0x100005665#endif56665667#ifdef __ANDROID__5668int open64(const char* pathName, int flags, int mode) {5669return ::open(pathName, flags, mode);5670}5671#endif //__ANDROID__56725673// Open a file. Unlink the file immediately after open returns5674// if the specified oflag has the O_DELETE flag set.5675// O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c56765677int os::open(const char *path, int oflag, int mode) {56785679if (strlen(path) > MAX_PATH - 1) {5680errno = ENAMETOOLONG;5681return -1;5682}5683int fd;5684int o_delete = (oflag & O_DELETE);5685oflag = oflag & ~O_DELETE;56865687fd = ::open64(path, oflag, mode);5688if (fd == -1) return -1;56895690//If the open succeeded, the file might still be a directory5691{5692struct stat64 buf64;5693int ret = ::fstat64(fd, &buf64);5694int st_mode = buf64.st_mode;56955696if (ret != -1) {5697if ((st_mode & S_IFMT) == S_IFDIR) {5698errno = EISDIR;5699::close(fd);5700return -1;5701}5702} else {5703::close(fd);5704return -1;5705}5706}57075708/*5709* All file descriptors that are opened in the JVM and not5710* specifically destined for a subprocess should have the5711* close-on-exec flag set. If we don't set it, then careless 3rd5712* party native code might fork and exec without closing all5713* appropriate file descriptors (e.g. as we do in closeDescriptors in5714* UNIXProcess.c), and this in turn might:5715*5716* - cause end-of-file to fail to be detected on some file5717* descriptors, resulting in mysterious hangs, or5718*5719* - might cause an fopen in the subprocess to fail on a system5720* suffering from bug 1085341.5721*5722* (Yes, the default setting of the close-on-exec flag is a Unix5723* design flaw)5724*5725* See:5726* 1085341: 32-bit stdio routines should support file descriptors >2555727* 4843136: (process) pipe file descriptor from Runtime.exec not being closed5728* 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 95729*/5730#ifdef FD_CLOEXEC5731{5732int flags = ::fcntl(fd, F_GETFD);5733if (flags != -1)5734::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);5735}5736#endif57375738if (o_delete != 0) {5739::unlink(path);5740}5741return fd;5742}57435744#ifdef __ANDROID__5745#define S_IREAD S_IRUSR5746#define S_IWRITE S_IWUSR5747#endif5748// create binary file, rewriting existing file if required5749int os::create_binary_file(const char* path, bool rewrite_existing) {5750int oflags = O_WRONLY | O_CREAT;5751if (!rewrite_existing) {5752oflags |= O_EXCL;5753}5754return ::open64(path, oflags, S_IREAD | S_IWRITE);5755}57565757// return current position of file pointer5758jlong os::current_file_offset(int fd) {5759return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);5760}57615762// move file pointer to the specified offset5763jlong os::seek_to_file_offset(int fd, jlong offset) {5764return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);5765}57665767// This code originates from JDK's sysAvailable5768// from src/solaris/hpi/src/native_threads/src/sys_api_td.c57695770int os::available(int fd, jlong *bytes) {5771jlong cur, end;5772int mode;5773struct stat64 buf64;57745775if (::fstat64(fd, &buf64) >= 0) {5776mode = buf64.st_mode;5777if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {5778/*5779* XXX: is the following call interruptible? If so, this might5780* need to go through the INTERRUPT_IO() wrapper as for other5781* blocking, interruptible calls in this file.5782*/5783int n;5784if (::ioctl(fd, FIONREAD, &n) >= 0) {5785*bytes = n;5786return 1;5787}5788}5789}5790if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {5791return 0;5792} else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {5793return 0;5794} else if (::lseek64(fd, cur, SEEK_SET) == -1) {5795return 0;5796}5797*bytes = end - cur;5798return 1;5799}58005801int os::socket_available(int fd, jint *pbytes) {5802// Linux doc says EINTR not returned, unlike Solaris5803int ret = ::ioctl(fd, FIONREAD, pbytes);58045805//%% note ioctl can return 0 when successful, JVM_SocketAvailable5806// is expected to return 0 on failure and 1 on success to the jdk.5807return (ret < 0) ? 0 : 1;5808}58095810// Map a block of memory.5811char* os::pd_map_memory(int fd, const char* file_name, size_t file_offset,5812char *addr, size_t bytes, bool read_only,5813bool allow_exec) {5814int prot;5815int flags = MAP_PRIVATE;58165817if (read_only) {5818prot = PROT_READ;5819} else {5820prot = PROT_READ | PROT_WRITE;5821}58225823if (allow_exec) {5824prot |= PROT_EXEC;5825}58265827if (addr != NULL) {5828flags |= MAP_FIXED;5829}58305831char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,5832fd, file_offset);5833if (mapped_address == MAP_FAILED) {5834return NULL;5835}5836return mapped_address;5837}583858395840// Remap a block of memory.5841char* os::pd_remap_memory(int fd, const char* file_name, size_t file_offset,5842char *addr, size_t bytes, bool read_only,5843bool allow_exec) {5844// same as map_memory() on this OS5845return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,5846allow_exec);5847}584858495850// Unmap a block of memory.5851bool os::pd_unmap_memory(char* addr, size_t bytes) {5852return munmap(addr, bytes) == 0;5853}58545855static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);58565857static clockid_t thread_cpu_clockid(Thread* thread) {5858pthread_t tid = thread->osthread()->pthread_id();5859clockid_t clockid;58605861// Get thread clockid5862int rc = os::Linux::pthread_getcpuclockid(tid, &clockid);5863assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");5864return clockid;5865}58665867// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)5868// are used by JVM M&M and JVMTI to get user+sys or user CPU time5869// of a thread.5870//5871// current_thread_cpu_time() and thread_cpu_time(Thread*) returns5872// the fast estimate available on the platform.58735874jlong os::current_thread_cpu_time() {5875if (os::Linux::supports_fast_thread_cpu_time()) {5876return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);5877} else {5878// return user + sys since the cost is the same5879return slow_thread_cpu_time(Thread::current(), true /* user + sys */);5880}5881}58825883jlong os::thread_cpu_time(Thread* thread) {5884// consistent with what current_thread_cpu_time() returns5885if (os::Linux::supports_fast_thread_cpu_time()) {5886return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));5887} else {5888return slow_thread_cpu_time(thread, true /* user + sys */);5889}5890}58915892jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {5893if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {5894return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);5895} else {5896return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);5897}5898}58995900jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {5901if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {5902return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));5903} else {5904return slow_thread_cpu_time(thread, user_sys_cpu_time);5905}5906}59075908//5909// -1 on error.5910//59115912PRAGMA_DIAG_PUSH5913PRAGMA_FORMAT_NONLITERAL_IGNORED5914static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {5915pid_t tid = thread->osthread()->thread_id();5916char *s;5917char stat[2048];5918int statlen;5919char proc_name[64];5920int count;5921long sys_time, user_time;5922char cdummy;5923int idummy;5924long ldummy;5925FILE *fp;59265927snprintf(proc_name, 64, "/proc/self/task/%d/stat", tid);5928fp = fopen(proc_name, "r");5929if ( fp == NULL ) return -1;5930statlen = fread(stat, 1, 2047, fp);5931stat[statlen] = '\0';5932fclose(fp);59335934// Skip pid and the command string. Note that we could be dealing with5935// weird command names, e.g. user could decide to rename java launcher5936// to "java 1.4.2 :)", then the stat file would look like5937// 1234 (java 1.4.2 :)) R ... ...5938// We don't really need to know the command string, just find the last5939// occurrence of ")" and then start parsing from there. See bug 4726580.5940s = strrchr(stat, ')');5941if (s == NULL ) return -1;59425943// Skip blank chars5944do s++; while (isspace(*s));59455946count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",5947&cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,5948&ldummy, &ldummy, &ldummy, &ldummy, &ldummy,5949&user_time, &sys_time);5950if ( count != 13 ) return -1;5951if (user_sys_cpu_time) {5952return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);5953} else {5954return (jlong)user_time * (1000000000 / clock_tics_per_sec);5955}5956}5957PRAGMA_DIAG_POP59585959void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {5960info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits5961info_ptr->may_skip_backward = false; // elapsed time not wall time5962info_ptr->may_skip_forward = false; // elapsed time not wall time5963info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned5964}59655966void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {5967info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits5968info_ptr->may_skip_backward = false; // elapsed time not wall time5969info_ptr->may_skip_forward = false; // elapsed time not wall time5970info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned5971}59725973bool os::is_thread_cpu_time_supported() {5974return true;5975}59765977// System loadavg support. Returns -1 if load average cannot be obtained.5978// Linux doesn't yet have a (official) notion of processor sets,5979// so just return the system wide load average.5980int os::loadavg(double loadavg[], int nelem) {5981#ifdef __ANDROID__5982return -1;5983#else5984return ::getloadavg(loadavg, nelem);5985#endif // !__ANDROID__5986}59875988void os::pause() {5989char filename[MAX_PATH];5990if (PauseAtStartupFile && PauseAtStartupFile[0]) {5991jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);5992} else {5993jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());5994}59955996int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);5997if (fd != -1) {5998struct stat buf;5999::close(fd);6000while (::stat(filename, &buf) == 0) {6001(void)::poll(NULL, 0, 100);6002}6003} else {6004jio_fprintf(stderr,6005"Could not open pause file '%s', continuing immediately.\n", filename);6006}6007}600860096010// Refer to the comments in os_solaris.cpp park-unpark.6011//6012// Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can6013// hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable.6014// For specifics regarding the bug see GLIBC BUGID 261237 :6015// http://www.mail-archive.com/[email protected]/msg10837.html.6016// Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future6017// will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar6018// is used. (The simple C test-case provided in the GLIBC bug report manifests the6019// hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos()6020// and monitorenter when we're using 1-0 locking. All those operations may result in6021// calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version6022// of libpthread avoids the problem, but isn't practical.6023//6024// Possible remedies:6025//6026// 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work.6027// This is palliative and probabilistic, however. If the thread is preempted6028// between the call to compute_abstime() and pthread_cond_timedwait(), more6029// than the minimum period may have passed, and the abstime may be stale (in the6030// past) resultin in a hang. Using this technique reduces the odds of a hang6031// but the JVM is still vulnerable, particularly on heavily loaded systems.6032//6033// 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead6034// of the usual flag-condvar-mutex idiom. The write side of the pipe is set6035// NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo)6036// reduces to poll()+read(). This works well, but consumes 2 FDs per extant6037// thread.6038//6039// 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread6040// that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing6041// a timeout request to the chron thread and then blocking via pthread_cond_wait().6042// This also works well. In fact it avoids kernel-level scalability impediments6043// on certain platforms that don't handle lots of active pthread_cond_timedwait()6044// timers in a graceful fashion.6045//6046// 4. When the abstime value is in the past it appears that control returns6047// correctly from pthread_cond_timedwait(), but the condvar is left corrupt.6048// Subsequent timedwait/wait calls may hang indefinitely. Given that, we6049// can avoid the problem by reinitializing the condvar -- by cond_destroy()6050// followed by cond_init() -- after all calls to pthread_cond_timedwait().6051// It may be possible to avoid reinitialization by checking the return6052// value from pthread_cond_timedwait(). In addition to reinitializing the6053// condvar we must establish the invariant that cond_signal() is only called6054// within critical sections protected by the adjunct mutex. This prevents6055// cond_signal() from "seeing" a condvar that's in the midst of being6056// reinitialized or that is corrupt. Sadly, this invariant obviates the6057// desirable signal-after-unlock optimization that avoids futile context switching.6058//6059// I'm also concerned that some versions of NTPL might allocate an auxilliary6060// structure when a condvar is used or initialized. cond_destroy() would6061// release the helper structure. Our reinitialize-after-timedwait fix6062// put excessive stress on malloc/free and locks protecting the c-heap.6063//6064// We currently use (4). See the WorkAroundNTPLTimedWaitHang flag.6065// It may be possible to refine (4) by checking the kernel and NTPL verisons6066// and only enabling the work-around for vulnerable environments.60676068// utility to compute the abstime argument to timedwait:6069// millis is the relative timeout time6070// abstime will be the absolute timeout time6071// TODO: replace compute_abstime() with unpackTime()60726073static struct timespec* compute_abstime(timespec* abstime, jlong millis) {6074if (millis < 0) millis = 0;60756076jlong seconds = millis / 1000;6077millis %= 1000;6078if (seconds > 50000000) { // see man cond_timedwait(3T)6079seconds = 50000000;6080}60816082if (os::Linux::supports_monotonic_clock()) {6083struct timespec now;6084int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);6085assert_status(status == 0, status, "clock_gettime");6086abstime->tv_sec = now.tv_sec + seconds;6087long nanos = now.tv_nsec + millis * NANOSECS_PER_MILLISEC;6088if (nanos >= NANOSECS_PER_SEC) {6089abstime->tv_sec += 1;6090nanos -= NANOSECS_PER_SEC;6091}6092abstime->tv_nsec = nanos;6093} else {6094struct timeval now;6095int status = gettimeofday(&now, NULL);6096assert(status == 0, "gettimeofday");6097abstime->tv_sec = now.tv_sec + seconds;6098long usec = now.tv_usec + millis * 1000;6099if (usec >= 1000000) {6100abstime->tv_sec += 1;6101usec -= 1000000;6102}6103abstime->tv_nsec = usec * 1000;6104}6105return abstime;6106}610761086109// Test-and-clear _Event, always leaves _Event set to 0, returns immediately.6110// Conceptually TryPark() should be equivalent to park(0).61116112int os::PlatformEvent::TryPark() {6113for (;;) {6114const int v = _Event ;6115guarantee ((v == 0) || (v == 1), "invariant") ;6116if (Atomic::cmpxchg (0, &_Event, v) == v) return v ;6117}6118}61196120void os::PlatformEvent::park() { // AKA "down()"6121// Invariant: Only the thread associated with the Event/PlatformEvent6122// may call park().6123// TODO: assert that _Assoc != NULL or _Assoc == Self6124int v ;6125for (;;) {6126v = _Event ;6127if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;6128}6129guarantee (v >= 0, "invariant") ;6130if (v == 0) {6131// Do this the hard way by blocking ...6132int status = pthread_mutex_lock(_mutex);6133assert_status(status == 0, status, "mutex_lock");6134guarantee (_nParked == 0, "invariant") ;6135++ _nParked ;6136while (_Event < 0) {6137status = pthread_cond_wait(_cond, _mutex);6138// for some reason, under 2.7 lwp_cond_wait() may return ETIME ...6139// Treat this the same as if the wait was interrupted6140if (status == ETIME) { status = EINTR; }6141assert_status(status == 0 || status == EINTR, status, "cond_wait");6142}6143-- _nParked ;61446145_Event = 0 ;6146status = pthread_mutex_unlock(_mutex);6147assert_status(status == 0, status, "mutex_unlock");6148// Paranoia to ensure our locked and lock-free paths interact6149// correctly with each other.6150OrderAccess::fence();6151}6152guarantee (_Event >= 0, "invariant") ;6153}61546155int os::PlatformEvent::park(jlong millis) {6156guarantee (_nParked == 0, "invariant") ;61576158int v ;6159for (;;) {6160v = _Event ;6161if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;6162}6163guarantee (v >= 0, "invariant") ;6164if (v != 0) return OS_OK ;61656166// We do this the hard way, by blocking the thread.6167// Consider enforcing a minimum timeout value.6168struct timespec abst;6169compute_abstime(&abst, millis);61706171int ret = OS_TIMEOUT;6172int status = pthread_mutex_lock(_mutex);6173assert_status(status == 0, status, "mutex_lock");6174guarantee (_nParked == 0, "invariant") ;6175++_nParked ;61766177// Object.wait(timo) will return because of6178// (a) notification6179// (b) timeout6180// (c) thread.interrupt6181//6182// Thread.interrupt and object.notify{All} both call Event::set.6183// That is, we treat thread.interrupt as a special case of notification.6184// The underlying Solaris implementation, cond_timedwait, admits6185// spurious/premature wakeups, but the JLS/JVM spec prevents the6186// JVM from making those visible to Java code. As such, we must6187// filter out spurious wakeups. We assume all ETIME returns are valid.6188//6189// TODO: properly differentiate simultaneous notify+interrupt.6190// In that case, we should propagate the notify to another waiter.61916192while (_Event < 0) {6193status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);6194if (status != 0 && WorkAroundNPTLTimedWaitHang) {6195pthread_cond_destroy (_cond);6196pthread_cond_init (_cond, os::Linux::condAttr()) ;6197}6198assert_status(status == 0 || status == EINTR ||6199status == ETIME || status == ETIMEDOUT,6200status, "cond_timedwait");6201if (!FilterSpuriousWakeups) break ; // previous semantics6202if (status == ETIME || status == ETIMEDOUT) break ;6203// We consume and ignore EINTR and spurious wakeups.6204}6205--_nParked ;6206if (_Event >= 0) {6207ret = OS_OK;6208}6209_Event = 0 ;6210status = pthread_mutex_unlock(_mutex);6211assert_status(status == 0, status, "mutex_unlock");6212assert (_nParked == 0, "invariant") ;6213// Paranoia to ensure our locked and lock-free paths interact6214// correctly with each other.6215OrderAccess::fence();6216return ret;6217}62186219void os::PlatformEvent::unpark() {6220// Transitions for _Event:6221// 0 :=> 16222// 1 :=> 16223// -1 :=> either 0 or 1; must signal target thread6224// That is, we can safely transition _Event from -1 to either6225// 0 or 1. Forcing 1 is slightly more efficient for back-to-back6226// unpark() calls.6227// See also: "Semaphores in Plan 9" by Mullender & Cox6228//6229// Note: Forcing a transition from "-1" to "1" on an unpark() means6230// that it will take two back-to-back park() calls for the owning6231// thread to block. This has the benefit of forcing a spurious return6232// from the first park() call after an unpark() call which will help6233// shake out uses of park() and unpark() without condition variables.62346235if (Atomic::xchg(1, &_Event) >= 0) return;62366237// Wait for the thread associated with the event to vacate6238int status = pthread_mutex_lock(_mutex);6239assert_status(status == 0, status, "mutex_lock");6240int AnyWaiters = _nParked;6241assert(AnyWaiters == 0 || AnyWaiters == 1, "invariant");6242if (AnyWaiters != 0 && WorkAroundNPTLTimedWaitHang) {6243AnyWaiters = 0;6244pthread_cond_signal(_cond);6245}6246status = pthread_mutex_unlock(_mutex);6247assert_status(status == 0, status, "mutex_unlock");6248if (AnyWaiters != 0) {6249status = pthread_cond_signal(_cond);6250assert_status(status == 0, status, "cond_signal");6251}62526253// Note that we signal() _after dropping the lock for "immortal" Events.6254// This is safe and avoids a common class of futile wakeups. In rare6255// circumstances this can cause a thread to return prematurely from6256// cond_{timed}wait() but the spurious wakeup is benign and the victim will6257// simply re-test the condition and re-park itself.6258}625962606261// JSR1666262// -------------------------------------------------------62636264/*6265* The solaris and linux implementations of park/unpark are fairly6266* conservative for now, but can be improved. They currently use a6267* mutex/condvar pair, plus a a count.6268* Park decrements count if > 0, else does a condvar wait. Unpark6269* sets count to 1 and signals condvar. Only one thread ever waits6270* on the condvar. Contention seen when trying to park implies that someone6271* is unparking you, so don't wait. And spurious returns are fine, so there6272* is no need to track notifications.6273*/62746275/*6276* This code is common to linux and solaris and will be moved to a6277* common place in dolphin.6278*6279* The passed in time value is either a relative time in nanoseconds6280* or an absolute time in milliseconds. Either way it has to be unpacked6281* into suitable seconds and nanoseconds components and stored in the6282* given timespec structure.6283* Given time is a 64-bit value and the time_t used in the timespec is only6284* a signed-32-bit value (except on 64-bit Linux) we have to watch for6285* overflow if times way in the future are given. Further on Solaris versions6286* prior to 10 there is a restriction (see cond_timedwait) that the specified6287* number of seconds, in abstime, is less than current_time + 100,000,000.6288* As it will be 28 years before "now + 100000000" will overflow we can6289* ignore overflow and just impose a hard-limit on seconds using the value6290* of "now + 100,000,000". This places a limit on the timeout of about 3.176291* years from "now".6292*/62936294static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {6295assert (time > 0, "convertTime");6296time_t max_secs = 0;62976298if (!os::Linux::supports_monotonic_clock() || isAbsolute) {6299struct timeval now;6300int status = gettimeofday(&now, NULL);6301assert(status == 0, "gettimeofday");63026303max_secs = now.tv_sec + MAX_SECS;63046305if (isAbsolute) {6306jlong secs = time / 1000;6307if (secs > max_secs) {6308absTime->tv_sec = max_secs;6309} else {6310absTime->tv_sec = secs;6311}6312absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;6313} else {6314jlong secs = time / NANOSECS_PER_SEC;6315if (secs >= MAX_SECS) {6316absTime->tv_sec = max_secs;6317absTime->tv_nsec = 0;6318} else {6319absTime->tv_sec = now.tv_sec + secs;6320absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;6321if (absTime->tv_nsec >= NANOSECS_PER_SEC) {6322absTime->tv_nsec -= NANOSECS_PER_SEC;6323++absTime->tv_sec; // note: this must be <= max_secs6324}6325}6326}6327} else {6328// must be relative using monotonic clock6329struct timespec now;6330int status = os::Linux::clock_gettime(CLOCK_MONOTONIC, &now);6331assert_status(status == 0, status, "clock_gettime");6332max_secs = now.tv_sec + MAX_SECS;6333jlong secs = time / NANOSECS_PER_SEC;6334if (secs >= MAX_SECS) {6335absTime->tv_sec = max_secs;6336absTime->tv_nsec = 0;6337} else {6338absTime->tv_sec = now.tv_sec + secs;6339absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_nsec;6340if (absTime->tv_nsec >= NANOSECS_PER_SEC) {6341absTime->tv_nsec -= NANOSECS_PER_SEC;6342++absTime->tv_sec; // note: this must be <= max_secs6343}6344}6345}6346assert(absTime->tv_sec >= 0, "tv_sec < 0");6347assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");6348assert(absTime->tv_nsec >= 0, "tv_nsec < 0");6349assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");6350}63516352void Parker::park(bool isAbsolute, jlong time) {6353// Ideally we'd do something useful while spinning, such6354// as calling unpackTime().63556356// Optional fast-path check:6357// Return immediately if a permit is available.6358// We depend on Atomic::xchg() having full barrier semantics6359// since we are doing a lock-free update to _counter.6360if (Atomic::xchg(0, &_counter) > 0) return;63616362Thread* thread = Thread::current();6363assert(thread->is_Java_thread(), "Must be JavaThread");6364JavaThread *jt = (JavaThread *)thread;63656366// Optional optimization -- avoid state transitions if there's an interrupt pending.6367// Check interrupt before trying to wait6368if (Thread::is_interrupted(thread, false)) {6369return;6370}63716372// Next, demultiplex/decode time arguments6373timespec absTime;6374if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all6375return;6376}6377if (time > 0) {6378unpackTime(&absTime, isAbsolute, time);6379}638063816382// Enter safepoint region6383// Beware of deadlocks such as 6317397.6384// The per-thread Parker:: mutex is a classic leaf-lock.6385// In particular a thread must never block on the Threads_lock while6386// holding the Parker:: mutex. If safepoints are pending both the6387// the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.6388ThreadBlockInVM tbivm(jt);63896390// Don't wait if cannot get lock since interference arises from6391// unblocking. Also. check interrupt before trying wait6392if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {6393return;6394}63956396int status ;6397if (_counter > 0) { // no wait needed6398_counter = 0;6399status = pthread_mutex_unlock(_mutex);6400assert (status == 0, "invariant") ;6401// Paranoia to ensure our locked and lock-free paths interact6402// correctly with each other and Java-level accesses.6403OrderAccess::fence();6404return;6405}64066407#ifdef ASSERT6408// Don't catch signals while blocked; let the running threads have the signals.6409// (This allows a debugger to break into the running thread.)6410sigset_t oldsigs;6411sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals();6412pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);6413#endif64146415OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);6416jt->set_suspend_equivalent();6417// cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()64186419assert(_cur_index == -1, "invariant");6420if (time == 0) {6421_cur_index = REL_INDEX; // arbitrary choice when not timed6422status = pthread_cond_wait (&_cond[_cur_index], _mutex) ;6423} else {6424_cur_index = isAbsolute ? ABS_INDEX : REL_INDEX;6425status = os::Linux::safe_cond_timedwait (&_cond[_cur_index], _mutex, &absTime) ;6426if (status != 0 && WorkAroundNPTLTimedWaitHang) {6427pthread_cond_destroy (&_cond[_cur_index]) ;6428pthread_cond_init (&_cond[_cur_index], isAbsolute ? NULL : os::Linux::condAttr());6429}6430}6431_cur_index = -1;6432assert_status(status == 0 || status == EINTR ||6433status == ETIME || status == ETIMEDOUT,6434status, "cond_timedwait");64356436#ifdef ASSERT6437pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);6438#endif64396440_counter = 0 ;6441status = pthread_mutex_unlock(_mutex) ;6442assert_status(status == 0, status, "invariant") ;6443// Paranoia to ensure our locked and lock-free paths interact6444// correctly with each other and Java-level accesses.6445OrderAccess::fence();64466447// If externally suspended while waiting, re-suspend6448if (jt->handle_special_suspend_equivalent_condition()) {6449jt->java_suspend_self();6450}6451}64526453void Parker::unpark() {6454int s, status ;6455status = pthread_mutex_lock(_mutex);6456assert (status == 0, "invariant") ;6457s = _counter;6458_counter = 1;6459if (s < 1) {6460// thread might be parked6461if (_cur_index != -1) {6462// thread is definitely parked6463if (WorkAroundNPTLTimedWaitHang) {6464status = pthread_cond_signal (&_cond[_cur_index]);6465assert (status == 0, "invariant");6466status = pthread_mutex_unlock(_mutex);6467assert (status == 0, "invariant");6468} else {6469// must capture correct index before unlocking6470int index = _cur_index;6471status = pthread_mutex_unlock(_mutex);6472assert (status == 0, "invariant");6473status = pthread_cond_signal (&_cond[index]);6474assert (status == 0, "invariant");6475}6476} else {6477pthread_mutex_unlock(_mutex);6478assert (status == 0, "invariant") ;6479}6480} else {6481pthread_mutex_unlock(_mutex);6482assert (status == 0, "invariant") ;6483}6484}648564866487extern char** environ;64886489// Run the specified command in a separate process. Return its exit value,6490// or -1 on failure (e.g. can't fork a new process).6491// Unlike system(), this function can be called from signal handler. It6492// doesn't block SIGINT et al.6493int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {6494const char * argv[4] = {"sh", "-c", cmd, NULL};64956496pid_t pid ;64976498if (use_vfork_if_available) {6499pid = vfork();6500} else {6501pid = fork();6502}65036504if (pid < 0) {6505// fork failed6506return -1;65076508} else if (pid == 0) {6509// child process65106511execve("/bin/sh", (char* const*)argv, environ);65126513// execve failed6514_exit(-1);65156516} else {6517// copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't6518// care about the actual exit code, for now.65196520int status;65216522// Wait for the child process to exit. This returns immediately if6523// the child has already exited. */6524while (waitpid(pid, &status, 0) < 0) {6525switch (errno) {6526case ECHILD: return 0;6527case EINTR: break;6528default: return -1;6529}6530}65316532if (WIFEXITED(status)) {6533// The child exited normally; get its exit code.6534return WEXITSTATUS(status);6535} else if (WIFSIGNALED(status)) {6536// The child exited because of a signal6537// The best value to return is 0x80 + signal number,6538// because that is what all Unix shells do, and because6539// it allows callers to distinguish between process exit and6540// process death by signal.6541return 0x80 + WTERMSIG(status);6542} else {6543// Unknown exit code; pass it through6544return status;6545}6546}6547}65486549// is_headless_jre()6550//6551// Test for the existence of xawt/libmawt.so or libawt_xawt.so6552// in order to report if we are running in a headless jre6553//6554// Since JDK8 xawt/libmawt.so was moved into the same directory6555// as libawt.so, and renamed libawt_xawt.so6556//6557bool os::is_headless_jre() {6558struct stat statbuf;6559char buf[MAXPATHLEN];6560char libmawtpath[MAXPATHLEN];6561const char *xawtstr = "/xawt/libmawt.so";6562const char *new_xawtstr = "/libawt_xawt.so";6563char *p;65646565// Get path to libjvm.so6566os::jvm_path(buf, sizeof(buf));65676568// Get rid of libjvm.so6569p = strrchr(buf, '/');6570if (p == NULL) return false;6571else *p = '\0';65726573// Get rid of client or server6574p = strrchr(buf, '/');6575if (p == NULL) return false;6576else *p = '\0';65776578// check xawt/libmawt.so6579strcpy(libmawtpath, buf);6580strcat(libmawtpath, xawtstr);6581if (::stat(libmawtpath, &statbuf) == 0) return false;65826583// check libawt_xawt.so6584strcpy(libmawtpath, buf);6585strcat(libmawtpath, new_xawtstr);6586if (::stat(libmawtpath, &statbuf) == 0) return false;65876588return true;6589}65906591// Get the default path to the core file6592// Returns the length of the string6593int os::get_core_path(char* buffer, size_t bufferSize) {6594const char* p = get_current_directory(buffer, bufferSize);65956596if (p == NULL) {6597assert(p != NULL, "failed to get current directory");6598return 0;6599}66006601return strlen(buffer);6602}66036604/////////////// Unit tests ///////////////66056606#ifndef PRODUCT66076608#define test_log(...) \6609do {\6610if (VerboseInternalVMTests) { \6611tty->print_cr(__VA_ARGS__); \6612tty->flush(); \6613}\6614} while (false)66156616class TestReserveMemorySpecial : AllStatic {6617public:6618static void small_page_write(void* addr, size_t size) {6619size_t page_size = os::vm_page_size();66206621char* end = (char*)addr + size;6622for (char* p = (char*)addr; p < end; p += page_size) {6623*p = 1;6624}6625}66266627static void test_reserve_memory_special_huge_tlbfs_only(size_t size) {6628if (!UseHugeTLBFS) {6629return;6630}66316632test_log("test_reserve_memory_special_huge_tlbfs_only(" SIZE_FORMAT ")", size);66336634char* addr = os::Linux::reserve_memory_special_huge_tlbfs_only(size, NULL, false);66356636if (addr != NULL) {6637small_page_write(addr, size);66386639os::Linux::release_memory_special_huge_tlbfs(addr, size);6640}6641}66426643static void test_reserve_memory_special_huge_tlbfs_only() {6644if (!UseHugeTLBFS) {6645return;6646}66476648size_t lp = os::large_page_size();66496650for (size_t size = lp; size <= lp * 10; size += lp) {6651test_reserve_memory_special_huge_tlbfs_only(size);6652}6653}66546655static void test_reserve_memory_special_huge_tlbfs_mixed() {6656size_t lp = os::large_page_size();6657size_t ag = os::vm_allocation_granularity();66586659// sizes to test6660const size_t sizes[] = {6661lp, lp + ag, lp + lp / 2, lp * 2,6662lp * 2 + ag, lp * 2 - ag, lp * 2 + lp / 2,6663lp * 10, lp * 10 + lp / 26664};6665const int num_sizes = sizeof(sizes) / sizeof(size_t);66666667// For each size/alignment combination, we test three scenarios:6668// 1) with req_addr == NULL6669// 2) with a non-null req_addr at which we expect to successfully allocate6670// 3) with a non-null req_addr which contains a pre-existing mapping, at which we6671// expect the allocation to either fail or to ignore req_addr66726673// Pre-allocate two areas; they shall be as large as the largest allocation6674// and aligned to the largest alignment we will be testing.6675const size_t mapping_size = sizes[num_sizes - 1] * 2;6676char* const mapping1 = (char*) ::mmap(NULL, mapping_size,6677PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,6678-1, 0);6679assert(mapping1 != MAP_FAILED, "should work");66806681char* const mapping2 = (char*) ::mmap(NULL, mapping_size,6682PROT_NONE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_NORESERVE,6683-1, 0);6684assert(mapping2 != MAP_FAILED, "should work");66856686// Unmap the first mapping, but leave the second mapping intact: the first6687// mapping will serve as a value for a "good" req_addr (case 2). The second6688// mapping, still intact, as "bad" req_addr (case 3).6689::munmap(mapping1, mapping_size);66906691// Case 16692test_log("%s, req_addr NULL:", __FUNCTION__);6693test_log("size align result");66946695for (int i = 0; i < num_sizes; i++) {6696const size_t size = sizes[i];6697for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6698char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, NULL, false);6699test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " -> " PTR_FORMAT " %s",6700size, alignment, p, (p != NULL ? "" : "(failed)"));6701if (p != NULL) {6702assert(is_ptr_aligned(p, alignment), "must be");6703small_page_write(p, size);6704os::Linux::release_memory_special_huge_tlbfs(p, size);6705}6706}6707}67086709// Case 26710test_log("%s, req_addr non-NULL:", __FUNCTION__);6711test_log("size align req_addr result");67126713for (int i = 0; i < num_sizes; i++) {6714const size_t size = sizes[i];6715for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6716char* const req_addr = (char*) align_ptr_up(mapping1, alignment);6717char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);6718test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s",6719size, alignment, req_addr, p,6720((p != NULL ? (p == req_addr ? "(exact match)" : "") : "(failed)")));6721if (p != NULL) {6722assert(p == req_addr, "must be");6723small_page_write(p, size);6724os::Linux::release_memory_special_huge_tlbfs(p, size);6725}6726}6727}67286729// Case 36730test_log("%s, req_addr non-NULL with preexisting mapping:", __FUNCTION__);6731test_log("size align req_addr result");67326733for (int i = 0; i < num_sizes; i++) {6734const size_t size = sizes[i];6735for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6736char* const req_addr = (char*) align_ptr_up(mapping2, alignment);6737char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);6738test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " -> " PTR_FORMAT " %s",6739size, alignment, req_addr, p,6740((p != NULL ? "" : "(failed)")));6741// as the area around req_addr contains already existing mappings, the API should always6742// return NULL (as per contract, it cannot return another address)6743assert(p == NULL, "must be");6744}6745}67466747::munmap(mapping2, mapping_size);67486749}67506751static void test_reserve_memory_special_huge_tlbfs() {6752if (!UseHugeTLBFS) {6753return;6754}67556756test_reserve_memory_special_huge_tlbfs_only();6757test_reserve_memory_special_huge_tlbfs_mixed();6758}67596760static void test_reserve_memory_special_shm(size_t size, size_t alignment) {6761if (!UseSHM) {6762return;6763}67646765test_log("test_reserve_memory_special_shm(" SIZE_FORMAT ", " SIZE_FORMAT ")", size, alignment);67666767char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false);67686769if (addr != NULL) {6770assert(is_ptr_aligned(addr, alignment), "Check");6771assert(is_ptr_aligned(addr, os::large_page_size()), "Check");67726773small_page_write(addr, size);67746775os::Linux::release_memory_special_shm(addr, size);6776}6777}67786779static void test_reserve_memory_special_shm() {6780size_t lp = os::large_page_size();6781size_t ag = os::vm_allocation_granularity();67826783for (size_t size = ag; size < lp * 3; size += ag) {6784for (size_t alignment = ag; is_size_aligned(size, alignment); alignment *= 2) {6785test_reserve_memory_special_shm(size, alignment);6786}6787}6788}67896790static void test() {6791test_reserve_memory_special_huge_tlbfs();6792test_reserve_memory_special_shm();6793}6794};67956796void TestReserveMemorySpecial_test() {6797TestReserveMemorySpecial::test();6798}67996800#endif680168026803