src/hotspot/os/linux/os_linux.cpp
branchihse-cflags-rewrite-branch
changeset 58665 30a5049a36bb
parent 56900 d5d542d50e3c
parent 58654 562bf1878089
equal deleted inserted replaced
56900:d5d542d50e3c 58665:30a5049a36bb
     1 /*
     1 /*
     2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1999, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    31 #include "code/vtableStubs.hpp"
    31 #include "code/vtableStubs.hpp"
    32 #include "compiler/compileBroker.hpp"
    32 #include "compiler/compileBroker.hpp"
    33 #include "compiler/disassembler.hpp"
    33 #include "compiler/disassembler.hpp"
    34 #include "interpreter/interpreter.hpp"
    34 #include "interpreter/interpreter.hpp"
    35 #include "logging/log.hpp"
    35 #include "logging/log.hpp"
       
    36 #include "logging/logStream.hpp"
    36 #include "memory/allocation.inline.hpp"
    37 #include "memory/allocation.inline.hpp"
    37 #include "memory/filemap.hpp"
    38 #include "memory/filemap.hpp"
    38 #include "oops/oop.inline.hpp"
    39 #include "oops/oop.inline.hpp"
    39 #include "os_linux.inline.hpp"
    40 #include "os_linux.inline.hpp"
       
    41 #include "os_posix.inline.hpp"
    40 #include "os_share_linux.hpp"
    42 #include "os_share_linux.hpp"
    41 #include "osContainer_linux.hpp"
    43 #include "osContainer_linux.hpp"
    42 #include "prims/jniFastGetField.hpp"
    44 #include "prims/jniFastGetField.hpp"
    43 #include "prims/jvm_misc.hpp"
    45 #include "prims/jvm_misc.hpp"
    44 #include "runtime/arguments.hpp"
    46 #include "runtime/arguments.hpp"
    59 #include "runtime/stubRoutines.hpp"
    61 #include "runtime/stubRoutines.hpp"
    60 #include "runtime/thread.inline.hpp"
    62 #include "runtime/thread.inline.hpp"
    61 #include "runtime/threadCritical.hpp"
    63 #include "runtime/threadCritical.hpp"
    62 #include "runtime/threadSMR.hpp"
    64 #include "runtime/threadSMR.hpp"
    63 #include "runtime/timer.hpp"
    65 #include "runtime/timer.hpp"
       
    66 #include "runtime/vm_version.hpp"
    64 #include "semaphore_posix.hpp"
    67 #include "semaphore_posix.hpp"
    65 #include "services/attachListener.hpp"
    68 #include "services/attachListener.hpp"
    66 #include "services/memTracker.hpp"
    69 #include "services/memTracker.hpp"
    67 #include "services/runtimeService.hpp"
    70 #include "services/runtimeService.hpp"
    68 #include "utilities/align.hpp"
    71 #include "utilities/align.hpp"
    79 # include <sys/mman.h>
    82 # include <sys/mman.h>
    80 # include <sys/stat.h>
    83 # include <sys/stat.h>
    81 # include <sys/select.h>
    84 # include <sys/select.h>
    82 # include <pthread.h>
    85 # include <pthread.h>
    83 # include <signal.h>
    86 # include <signal.h>
       
    87 # include <endian.h>
    84 # include <errno.h>
    88 # include <errno.h>
    85 # include <dlfcn.h>
    89 # include <dlfcn.h>
    86 # include <stdio.h>
    90 # include <stdio.h>
    87 # include <unistd.h>
    91 # include <unistd.h>
    88 # include <sys/resource.h>
    92 # include <sys/resource.h>
   126 #define MAX_SECS 100000000
   130 #define MAX_SECS 100000000
   127 
   131 
   128 // for timer info max values which include all bits
   132 // for timer info max values which include all bits
   129 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
   133 #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
   130 
   134 
   131 #define LARGEPAGES_BIT (1 << 6)
   135 enum CoredumpFilterBit {
   132 #define DAX_SHARED_BIT (1 << 8)
   136   FILE_BACKED_PVT_BIT = 1 << 2,
       
   137   FILE_BACKED_SHARED_BIT = 1 << 3,
       
   138   LARGEPAGES_BIT = 1 << 6,
       
   139   DAX_SHARED_BIT = 1 << 8
       
   140 };
       
   141 
   133 ////////////////////////////////////////////////////////////////////////////////
   142 ////////////////////////////////////////////////////////////////////////////////
   134 // global variables
   143 // global variables
   135 julong os::Linux::_physical_memory = 0;
   144 julong os::Linux::_physical_memory = 0;
   136 
   145 
   137 address   os::Linux::_initial_thread_stack_bottom = NULL;
   146 address   os::Linux::_initial_thread_stack_bottom = NULL;
   138 uintptr_t os::Linux::_initial_thread_stack_size   = 0;
   147 uintptr_t os::Linux::_initial_thread_stack_size   = 0;
   139 
   148 
   140 int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;
       
   141 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
   149 int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
   142 int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL;
   150 int (*os::Linux::_pthread_setname_np)(pthread_t, const char*) = NULL;
   143 Mutex* os::Linux::_createThread_lock = NULL;
       
   144 pthread_t os::Linux::_main_thread;
   151 pthread_t os::Linux::_main_thread;
   145 int os::Linux::_page_size = -1;
   152 int os::Linux::_page_size = -1;
   146 bool os::Linux::_supports_fast_thread_cpu_time = false;
   153 bool os::Linux::_supports_fast_thread_cpu_time = false;
   147 uint32_t os::Linux::_os_version = 0;
       
   148 const char * os::Linux::_glibc_version = NULL;
   154 const char * os::Linux::_glibc_version = NULL;
   149 const char * os::Linux::_libpthread_version = NULL;
   155 const char * os::Linux::_libpthread_version = NULL;
   150 
   156 
   151 static jlong initial_time_count=0;
   157 static jlong initial_time_count=0;
   152 
   158 
   217   }
   223   }
   218 
   224 
   219   phys_mem = Linux::physical_memory();
   225   phys_mem = Linux::physical_memory();
   220   log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
   226   log_trace(os)("total system memory: " JLONG_FORMAT, phys_mem);
   221   return phys_mem;
   227   return phys_mem;
       
   228 }
       
   229 
       
   230 static uint64_t initial_total_ticks = 0;
       
   231 static uint64_t initial_steal_ticks = 0;
       
   232 static bool     has_initial_tick_info = false;
       
   233 
       
   234 static void next_line(FILE *f) {
       
   235   int c;
       
   236   do {
       
   237     c = fgetc(f);
       
   238   } while (c != '\n' && c != EOF);
       
   239 }
       
   240 
       
   241 bool os::Linux::get_tick_information(CPUPerfTicks* pticks, int which_logical_cpu) {
       
   242   FILE*         fh;
       
   243   uint64_t      userTicks, niceTicks, systemTicks, idleTicks;
       
   244   // since at least kernel 2.6 : iowait: time waiting for I/O to complete
       
   245   // irq: time  servicing interrupts; softirq: time servicing softirqs
       
   246   uint64_t      iowTicks = 0, irqTicks = 0, sirqTicks= 0;
       
   247   // steal (since kernel 2.6.11): time spent in other OS when running in a virtualized environment
       
   248   uint64_t      stealTicks = 0;
       
   249   // guest (since kernel 2.6.24): time spent running a virtual CPU for guest OS under the
       
   250   // control of the Linux kernel
       
   251   uint64_t      guestNiceTicks = 0;
       
   252   int           logical_cpu = -1;
       
   253   const int     required_tickinfo_count = (which_logical_cpu == -1) ? 4 : 5;
       
   254   int           n;
       
   255 
       
   256   memset(pticks, 0, sizeof(CPUPerfTicks));
       
   257 
       
   258   if ((fh = fopen("/proc/stat", "r")) == NULL) {
       
   259     return false;
       
   260   }
       
   261 
       
   262   if (which_logical_cpu == -1) {
       
   263     n = fscanf(fh, "cpu " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
       
   264             UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
       
   265             UINT64_FORMAT " " UINT64_FORMAT " ",
       
   266             &userTicks, &niceTicks, &systemTicks, &idleTicks,
       
   267             &iowTicks, &irqTicks, &sirqTicks,
       
   268             &stealTicks, &guestNiceTicks);
       
   269   } else {
       
   270     // Move to next line
       
   271     next_line(fh);
       
   272 
       
   273     // find the line for requested cpu faster to just iterate linefeeds?
       
   274     for (int i = 0; i < which_logical_cpu; i++) {
       
   275       next_line(fh);
       
   276     }
       
   277 
       
   278     n = fscanf(fh, "cpu%u " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
       
   279                UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " " UINT64_FORMAT " "
       
   280                UINT64_FORMAT " " UINT64_FORMAT " ",
       
   281                &logical_cpu, &userTicks, &niceTicks,
       
   282                &systemTicks, &idleTicks, &iowTicks, &irqTicks, &sirqTicks,
       
   283                &stealTicks, &guestNiceTicks);
       
   284   }
       
   285 
       
   286   fclose(fh);
       
   287   if (n < required_tickinfo_count || logical_cpu != which_logical_cpu) {
       
   288     return false;
       
   289   }
       
   290   pticks->used       = userTicks + niceTicks;
       
   291   pticks->usedKernel = systemTicks + irqTicks + sirqTicks;
       
   292   pticks->total      = userTicks + niceTicks + systemTicks + idleTicks +
       
   293                        iowTicks + irqTicks + sirqTicks + stealTicks + guestNiceTicks;
       
   294 
       
   295   if (n > required_tickinfo_count + 3) {
       
   296     pticks->steal = stealTicks;
       
   297     pticks->has_steal_ticks = true;
       
   298   } else {
       
   299     pticks->steal = 0;
       
   300     pticks->has_steal_ticks = false;
       
   301   }
       
   302 
       
   303   return true;
   222 }
   304 }
   223 
   305 
   224 // Return true if user is running as root.
   306 // Return true if user is running as root.
   225 
   307 
   226 bool os::have_special_privileges() {
   308 bool os::have_special_privileges() {
   266   return (pid_t)rslt;
   348   return (pid_t)rslt;
   267 }
   349 }
   268 
   350 
   269 // Most versions of linux have a bug where the number of processors are
   351 // Most versions of linux have a bug where the number of processors are
   270 // determined by looking at the /proc file system.  In a chroot environment,
   352 // determined by looking at the /proc file system.  In a chroot environment,
   271 // the system call returns 1.  This causes the VM to act as if it is
   353 // the system call returns 1.
   272 // a single processor and elide locking (see is_MP() call).
       
   273 static bool unsafe_chroot_detected = false;
   354 static bool unsafe_chroot_detected = false;
   274 static const char *unstable_chroot_error = "/proc file system not found.\n"
   355 static const char *unstable_chroot_error = "/proc file system not found.\n"
   275                      "Java may be unstable running multithreaded in a chroot "
   356                      "Java may be unstable running multithreaded in a chroot "
   276                      "environment on Linux when /proc filesystem is not mounted.";
   357                      "environment on Linux when /proc filesystem is not mounted.";
   277 
   358 
   323   //      The linker uses the following search paths to locate required
   404   //      The linker uses the following search paths to locate required
   324   //      shared libraries:
   405   //      shared libraries:
   325   //        1: ...
   406   //        1: ...
   326   //        ...
   407   //        ...
   327   //        7: The default directories, normally /lib and /usr/lib.
   408   //        7: The default directories, normally /lib and /usr/lib.
   328 #if defined(AMD64) || (defined(_LP64) && defined(SPARC)) || defined(PPC64) || defined(S390)
   409 #ifndef OVERRIDE_LIBPATH
   329   #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
   410   #if defined(AMD64) || (defined(_LP64) && defined(SPARC)) || defined(PPC64) || defined(S390)
       
   411     #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
       
   412   #else
       
   413     #define DEFAULT_LIBPATH "/lib:/usr/lib"
       
   414   #endif
   330 #else
   415 #else
   331   #define DEFAULT_LIBPATH "/lib:/usr/lib"
   416   #define DEFAULT_LIBPATH OVERRIDE_LIBPATH
   332 #endif
   417 #endif
   333 
   418 
   334 // Base path of extensions installed on the system.
   419 // Base path of extensions installed on the system.
   335 #define SYS_EXT_DIR     "/usr/java/packages"
   420 #define SYS_EXT_DIR     "/usr/java/packages"
   336 #define EXTENSIONS_DIR  "/lib/ext"
   421 #define EXTENSIONS_DIR  "/lib/ext"
   339   // Note that the space for the colon and the trailing null are provided
   424   // Note that the space for the colon and the trailing null are provided
   340   // by the nulls included by the sizeof operator.
   425   // by the nulls included by the sizeof operator.
   341   const size_t bufsize =
   426   const size_t bufsize =
   342     MAX2((size_t)MAXPATHLEN,  // For dll_dir & friends.
   427     MAX2((size_t)MAXPATHLEN,  // For dll_dir & friends.
   343          (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR)); // extensions dir
   428          (size_t)MAXPATHLEN + sizeof(EXTENSIONS_DIR) + sizeof(SYS_EXT_DIR) + sizeof(EXTENSIONS_DIR)); // extensions dir
   344   char *buf = (char *)NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
   429   char *buf = NEW_C_HEAP_ARRAY(char, bufsize, mtInternal);
   345 
   430 
   346   // sysclasspath, java_home, dll_dir
   431   // sysclasspath, java_home, dll_dir
   347   {
   432   {
   348     char *pslash;
   433     char *pslash;
   349     os::jvm_path(buf, bufsize);
   434     os::jvm_path(buf, bufsize);
   388     // addressed).
   473     // addressed).
   389     const char *v = ::getenv("LD_LIBRARY_PATH");
   474     const char *v = ::getenv("LD_LIBRARY_PATH");
   390     const char *v_colon = ":";
   475     const char *v_colon = ":";
   391     if (v == NULL) { v = ""; v_colon = ""; }
   476     if (v == NULL) { v = ""; v_colon = ""; }
   392     // That's +1 for the colon and +1 for the trailing '\0'.
   477     // That's +1 for the colon and +1 for the trailing '\0'.
   393     char *ld_library_path = (char *)NEW_C_HEAP_ARRAY(char,
   478     char *ld_library_path = NEW_C_HEAP_ARRAY(char,
   394                                                      strlen(v) + 1 +
   479                                              strlen(v) + 1 +
   395                                                      sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1,
   480                                              sizeof(SYS_EXT_DIR) + sizeof("/lib/") + sizeof(DEFAULT_LIBPATH) + 1,
   396                                                      mtInternal);
   481                                              mtInternal);
   397     sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
   482     sprintf(ld_library_path, "%s%s" SYS_EXT_DIR "/lib:" DEFAULT_LIBPATH, v, v_colon);
   398     Arguments::set_library_path(ld_library_path);
   483     Arguments::set_library_path(ld_library_path);
   399     FREE_C_HEAP_ARRAY(char, ld_library_path);
   484     FREE_C_HEAP_ARRAY(char, ld_library_path);
   400   }
   485   }
   401 
   486 
   649 //////////////////////////////////////////////////////////////////////////////
   734 //////////////////////////////////////////////////////////////////////////////
   650 // create new thread
   735 // create new thread
   651 
   736 
   652 // Thread start routine for all newly created threads
   737 // Thread start routine for all newly created threads
   653 static void *thread_native_entry(Thread *thread) {
   738 static void *thread_native_entry(Thread *thread) {
       
   739 
       
   740   thread->record_stack_base_and_size();
       
   741 
   654   // Try to randomize the cache line index of hot stack frames.
   742   // Try to randomize the cache line index of hot stack frames.
   655   // This helps when threads of the same stack traces evict each other's
   743   // This helps when threads of the same stack traces evict each other's
   656   // cache lines. The threads can be either from the same JVM instance, or
   744   // cache lines. The threads can be either from the same JVM instance, or
   657   // from different JVM instances. The benefit is especially true for
   745   // from different JVM instances. The benefit is especially true for
   658   // processors with hyperthreading technology.
   746   // processors with hyperthreading technology.
   682   // initialize floating point control register
   770   // initialize floating point control register
   683   os::Linux::init_thread_fpu_state();
   771   os::Linux::init_thread_fpu_state();
   684 
   772 
   685   // handshaking with parent thread
   773   // handshaking with parent thread
   686   {
   774   {
   687     MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
   775     MutexLocker ml(sync, Mutex::_no_safepoint_check_flag);
   688 
   776 
   689     // notify parent thread
   777     // notify parent thread
   690     osthread->set_state(INITIALIZED);
   778     osthread->set_state(INITIALIZED);
   691     sync->notify_all();
   779     sync->notify_all();
   692 
   780 
   693     // wait until os::start_thread()
   781     // wait until os::start_thread()
   694     while (osthread->get_state() == INITIALIZED) {
   782     while (osthread->get_state() == INITIALIZED) {
   695       sync->wait(Mutex::_no_safepoint_check_flag);
   783       sync->wait_without_safepoint_check();
   696     }
   784     }
   697   }
   785   }
       
   786 
       
   787   assert(osthread->pthread_id() != 0, "pthread_id was not set as expected");
   698 
   788 
   699   // call one more level start routine
   789   // call one more level start routine
   700   thread->run();
   790   thread->call_run();
       
   791 
       
   792   // Note: at this point the thread object may already have deleted itself.
       
   793   // Prevent dereferencing it from here on out.
       
   794   thread = NULL;
   701 
   795 
   702   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
   796   log_info(os, thread)("Thread finished (tid: " UINTX_FORMAT ", pthread id: " UINTX_FORMAT ").",
   703     os::current_thread_id(), (uintx) pthread_self());
   797     os::current_thread_id(), (uintx) pthread_self());
   704 
   798 
   705   // If a thread has not deleted itself ("delete this") as part of its
       
   706   // termination sequence, we have to ensure thread-local-storage is
       
   707   // cleared before we actually terminate. No threads should ever be
       
   708   // deleted asynchronously with respect to their termination.
       
   709   if (Thread::current_or_null_safe() != NULL) {
       
   710     assert(Thread::current_or_null_safe() == thread, "current thread is wrong");
       
   711     thread->clear_thread_current();
       
   712   }
       
   713 
       
   714   return 0;
   799   return 0;
       
   800 }
       
   801 
       
   802 // On Linux, glibc places static TLS blocks (for __thread variables) on
       
   803 // the thread stack. This decreases the stack size actually available
       
   804 // to threads.
       
   805 //
       
   806 // For large static TLS sizes, this may cause threads to malfunction due
       
   807 // to insufficient stack space. This is a well-known issue in glibc:
       
   808 // http://sourceware.org/bugzilla/show_bug.cgi?id=11787.
       
   809 //
       
   810 // As a workaround, we call a private but assumed-stable glibc function,
       
   811 // __pthread_get_minstack() to obtain the minstack size and derive the
       
   812 // static TLS size from it. We then increase the user requested stack
       
   813 // size by this TLS size.
       
   814 //
       
   815 // Due to compatibility concerns, this size adjustment is opt-in and
       
   816 // controlled via AdjustStackSizeForTLS.
       
   817 typedef size_t (*GetMinStack)(const pthread_attr_t *attr);
       
   818 
       
   819 GetMinStack _get_minstack_func = NULL;
       
   820 
       
   821 static void get_minstack_init() {
       
   822   _get_minstack_func =
       
   823         (GetMinStack)dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
       
   824   log_info(os, thread)("Lookup of __pthread_get_minstack %s",
       
   825                        _get_minstack_func == NULL ? "failed" : "succeeded");
       
   826 }
       
   827 
       
   828 // Returns the size of the static TLS area glibc puts on thread stacks.
       
   829 // The value is cached on first use, which occurs when the first thread
       
   830 // is created during VM initialization.
       
   831 static size_t get_static_tls_area_size(const pthread_attr_t *attr) {
       
   832   size_t tls_size = 0;
       
   833   if (_get_minstack_func != NULL) {
       
   834     // Obtain the pthread minstack size by calling __pthread_get_minstack.
       
   835     size_t minstack_size = _get_minstack_func(attr);
       
   836 
       
   837     // Remove non-TLS area size included in minstack size returned
       
   838     // by __pthread_get_minstack() to get the static TLS size.
       
   839     // In glibc before 2.27, minstack size includes guard_size.
       
   840     // In glibc 2.27 and later, guard_size is automatically added
       
   841     // to the stack size by pthread_create and is no longer included
       
   842     // in minstack size. In both cases, the guard_size is taken into
       
   843     // account, so there is no need to adjust the result for that.
       
   844     //
       
   845     // Although __pthread_get_minstack() is a private glibc function,
       
   846     // it is expected to have a stable behavior across future glibc
       
   847     // versions while glibc still allocates the static TLS blocks off
       
   848     // the stack. Following is glibc 2.28 __pthread_get_minstack():
       
   849     //
       
   850     // size_t
       
   851     // __pthread_get_minstack (const pthread_attr_t *attr)
       
   852     // {
       
   853     //   return GLRO(dl_pagesize) + __static_tls_size + PTHREAD_STACK_MIN;
       
   854     // }
       
   855     //
       
   856     //
       
   857     // The following 'minstack_size > os::vm_page_size() + PTHREAD_STACK_MIN'
       
   858     // if check is done for precaution.
       
   859     if (minstack_size > (size_t)os::vm_page_size() + PTHREAD_STACK_MIN) {
       
   860       tls_size = minstack_size - os::vm_page_size() - PTHREAD_STACK_MIN;
       
   861     }
       
   862   }
       
   863 
       
   864   log_info(os, thread)("Stack size adjustment for TLS is " SIZE_FORMAT,
       
   865                        tls_size);
       
   866   return tls_size;
   715 }
   867 }
   716 
   868 
   717 bool os::create_thread(Thread* thread, ThreadType thr_type,
   869 bool os::create_thread(Thread* thread, ThreadType thr_type,
   718                        size_t req_stack_size) {
   870                        size_t req_stack_size) {
   719   assert(thread->osthread() == NULL, "caller responsible");
   871   assert(thread->osthread() == NULL, "caller responsible");
   737   pthread_attr_init(&attr);
   889   pthread_attr_init(&attr);
   738   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
   890   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
   739 
   891 
   740   // Calculate stack size if it's not specified by caller.
   892   // Calculate stack size if it's not specified by caller.
   741   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
   893   size_t stack_size = os::Posix::get_initial_stack_size(thr_type, req_stack_size);
   742   // In the Linux NPTL pthread implementation the guard size mechanism
   894   // In glibc versions prior to 2.7 the guard size mechanism
   743   // is not implemented properly. The posix standard requires adding
   895   // is not implemented properly. The posix standard requires adding
   744   // the size of the guard pages to the stack size, instead Linux
   896   // the size of the guard pages to the stack size, instead Linux
   745   // takes the space out of 'stacksize'. Thus we adapt the requested
   897   // takes the space out of 'stacksize'. Thus we adapt the requested
   746   // stack_size by the size of the guard pages to mimick proper
   898   // stack_size by the size of the guard pages to mimick proper
   747   // behaviour. However, be careful not to end up with a size
   899   // behaviour. However, be careful not to end up with a size
   748   // of zero due to overflow. Don't add the guard page in that case.
   900   // of zero due to overflow. Don't add the guard page in that case.
   749   size_t guard_size = os::Linux::default_guard_size(thr_type);
   901   size_t guard_size = os::Linux::default_guard_size(thr_type);
   750   if (stack_size <= SIZE_MAX - guard_size) {
   902   // Configure glibc guard page. Must happen before calling
   751     stack_size += guard_size;
   903   // get_static_tls_area_size(), which uses the guard_size.
       
   904   pthread_attr_setguardsize(&attr, guard_size);
       
   905 
       
   906   size_t stack_adjust_size = 0;
       
   907   if (AdjustStackSizeForTLS) {
       
   908     // Adjust the stack_size for on-stack TLS - see get_static_tls_area_size().
       
   909     stack_adjust_size += get_static_tls_area_size(&attr);
       
   910   } else {
       
   911     stack_adjust_size += guard_size;
       
   912   }
       
   913 
       
   914   stack_adjust_size = align_up(stack_adjust_size, os::vm_page_size());
       
   915   if (stack_size <= SIZE_MAX - stack_adjust_size) {
       
   916     stack_size += stack_adjust_size;
   752   }
   917   }
   753   assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");
   918   assert(is_aligned(stack_size, os::vm_page_size()), "stack_size not aligned");
   754 
   919 
   755   int status = pthread_attr_setstacksize(&attr, stack_size);
   920   int status = pthread_attr_setstacksize(&attr, stack_size);
   756   assert_status(status == 0, status, "pthread_attr_setstacksize");
   921   assert_status(status == 0, status, "pthread_attr_setstacksize");
   757 
       
   758   // Configure glibc guard page.
       
   759   pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
       
   760 
   922 
   761   ThreadState state;
   923   ThreadState state;
   762 
   924 
   763   {
   925   {
   764     pthread_t tid;
   926     pthread_t tid;
   769       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
   931       log_info(os, thread)("Thread started (pthread id: " UINTX_FORMAT ", attributes: %s). ",
   770         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
   932         (uintx) tid, os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
   771     } else {
   933     } else {
   772       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
   934       log_warning(os, thread)("Failed to start thread - pthread_create failed (%s) for attributes: %s.",
   773         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
   935         os::errno_name(ret), os::Posix::describe_pthread_attr(buf, sizeof(buf), &attr));
       
   936       // Log some OS information which might explain why creating the thread failed.
       
   937       log_info(os, thread)("Number of threads approx. running in the VM: %d", Threads::number_of_threads());
       
   938       LogStream st(Log(os, thread)::info());
       
   939       os::Posix::print_rlimit_info(&st);
       
   940       os::print_memory_info(&st);
       
   941       os::Linux::print_proc_sys_info(&st);
       
   942       os::Linux::print_container_info(&st);
   774     }
   943     }
   775 
   944 
   776     pthread_attr_destroy(&attr);
   945     pthread_attr_destroy(&attr);
   777 
   946 
   778     if (ret != 0) {
   947     if (ret != 0) {
   786     osthread->set_pthread_id(tid);
   955     osthread->set_pthread_id(tid);
   787 
   956 
   788     // Wait until child thread is either initialized or aborted
   957     // Wait until child thread is either initialized or aborted
   789     {
   958     {
   790       Monitor* sync_with_child = osthread->startThread_lock();
   959       Monitor* sync_with_child = osthread->startThread_lock();
   791       MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
   960       MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);
   792       while ((state = osthread->get_state()) == ALLOCATED) {
   961       while ((state = osthread->get_state()) == ALLOCATED) {
   793         sync_with_child->wait(Mutex::_no_safepoint_check_flag);
   962         sync_with_child->wait_without_safepoint_check();
   794       }
   963       }
   795     }
   964     }
   796   }
   965   }
   797 
   966 
   798   // Aborted due to thread limit being reached
   967   // Aborted due to thread limit being reached
   880 
  1049 
   881 void os::pd_start_thread(Thread* thread) {
  1050 void os::pd_start_thread(Thread* thread) {
   882   OSThread * osthread = thread->osthread();
  1051   OSThread * osthread = thread->osthread();
   883   assert(osthread->get_state() != INITIALIZED, "just checking");
  1052   assert(osthread->get_state() != INITIALIZED, "just checking");
   884   Monitor* sync_with_child = osthread->startThread_lock();
  1053   Monitor* sync_with_child = osthread->startThread_lock();
   885   MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
  1054   MutexLocker ml(sync_with_child, Mutex::_no_safepoint_check_flag);
   886   sync_with_child->notify();
  1055   sync_with_child->notify();
   887 }
  1056 }
   888 
  1057 
   889 // Free Linux resources related to the OSThread
  1058 // Free Linux resources related to the OSThread
   890 void os::free_thread(OSThread* osthread) {
  1059 void os::free_thread(OSThread* osthread) {
  1171 }
  1340 }
  1172 
  1341 
  1173 ////////////////////////////////////////////////////////////////////////////////
  1342 ////////////////////////////////////////////////////////////////////////////////
  1174 // time support
  1343 // time support
  1175 
  1344 
       
  1345 #ifndef SUPPORTS_CLOCK_MONOTONIC
       
  1346 #error "Build platform doesn't support clock_gettime and related functionality"
       
  1347 #endif
       
  1348 
  1176 // Time since start-up in seconds to a fine granularity.
  1349 // Time since start-up in seconds to a fine granularity.
  1177 // Used by VMSelfDestructTimer and the MemProfiler.
  1350 // Used by VMSelfDestructTimer and the MemProfiler.
  1178 double os::elapsedTime() {
  1351 double os::elapsedTime() {
  1179 
  1352 
  1180   return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
  1353   return ((double)os::elapsed_counter()) / os::elapsed_frequency(); // nanosecond resolution
  1187 jlong os::elapsed_frequency() {
  1360 jlong os::elapsed_frequency() {
  1188   return NANOSECS_PER_SEC; // nanosecond resolution
  1361   return NANOSECS_PER_SEC; // nanosecond resolution
  1189 }
  1362 }
  1190 
  1363 
  1191 bool os::supports_vtime() { return true; }
  1364 bool os::supports_vtime() { return true; }
  1192 bool os::enable_vtime()   { return false; }
       
  1193 bool os::vtime_enabled()  { return false; }
       
  1194 
  1365 
  1195 double os::elapsedVTime() {
  1366 double os::elapsedVTime() {
  1196   struct rusage usage;
  1367   struct rusage usage;
  1197   int retval = getrusage(RUSAGE_THREAD, &usage);
  1368   int retval = getrusage(RUSAGE_THREAD, &usage);
  1198   if (retval == 0) {
  1369   if (retval == 0) {
  1216   assert(status != -1, "linux error");
  1387   assert(status != -1, "linux error");
  1217   seconds = jlong(time.tv_sec);
  1388   seconds = jlong(time.tv_sec);
  1218   nanos = jlong(time.tv_usec) * 1000;
  1389   nanos = jlong(time.tv_usec) * 1000;
  1219 }
  1390 }
  1220 
  1391 
  1221 
       
  1222 #ifndef CLOCK_MONOTONIC
       
  1223   #define CLOCK_MONOTONIC (1)
       
  1224 #endif
       
  1225 
       
  1226 void os::Linux::clock_init() {
       
  1227   // we do dlopen's in this particular order due to bug in linux
       
  1228   // dynamical loader (see 6348968) leading to crash on exit
       
  1229   void* handle = dlopen("librt.so.1", RTLD_LAZY);
       
  1230   if (handle == NULL) {
       
  1231     handle = dlopen("librt.so", RTLD_LAZY);
       
  1232   }
       
  1233 
       
  1234   if (handle) {
       
  1235     int (*clock_getres_func)(clockid_t, struct timespec*) =
       
  1236            (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
       
  1237     int (*clock_gettime_func)(clockid_t, struct timespec*) =
       
  1238            (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
       
  1239     if (clock_getres_func && clock_gettime_func) {
       
  1240       // See if monotonic clock is supported by the kernel. Note that some
       
  1241       // early implementations simply return kernel jiffies (updated every
       
  1242       // 1/100 or 1/1000 second). It would be bad to use such a low res clock
       
  1243       // for nano time (though the monotonic property is still nice to have).
       
  1244       // It's fixed in newer kernels, however clock_getres() still returns
       
  1245       // 1/HZ. We check if clock_getres() works, but will ignore its reported
       
  1246       // resolution for now. Hopefully as people move to new kernels, this
       
  1247       // won't be a problem.
       
  1248       struct timespec res;
       
  1249       struct timespec tp;
       
  1250       if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
       
  1251           clock_gettime_func(CLOCK_MONOTONIC, &tp)  == 0) {
       
  1252         // yes, monotonic clock is supported
       
  1253         _clock_gettime = clock_gettime_func;
       
  1254         return;
       
  1255       } else {
       
  1256         // close librt if there is no monotonic clock
       
  1257         dlclose(handle);
       
  1258       }
       
  1259     }
       
  1260   }
       
  1261   warning("No monotonic clock was available - timed services may " \
       
  1262           "be adversely affected if the time-of-day clock changes");
       
  1263 }
       
  1264 
       
  1265 #ifndef SYS_clock_getres
       
  1266   #if defined(X86) || defined(PPC64) || defined(S390)
       
  1267     #define SYS_clock_getres AMD64_ONLY(229) IA32_ONLY(266) PPC64_ONLY(247) S390_ONLY(261)
       
  1268     #define sys_clock_getres(x,y)  ::syscall(SYS_clock_getres, x, y)
       
  1269   #else
       
  1270     #warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"
       
  1271     #define sys_clock_getres(x,y)  -1
       
  1272   #endif
       
  1273 #else
       
  1274   #define sys_clock_getres(x,y)  ::syscall(SYS_clock_getres, x, y)
       
  1275 #endif
       
  1276 
       
  1277 void os::Linux::fast_thread_clock_init() {
  1392 void os::Linux::fast_thread_clock_init() {
  1278   if (!UseLinuxPosixThreadCPUClocks) {
  1393   if (!UseLinuxPosixThreadCPUClocks) {
  1279     return;
  1394     return;
  1280   }
  1395   }
  1281   clockid_t clockid;
  1396   clockid_t clockid;
  1282   struct timespec tp;
  1397   struct timespec tp;
  1283   int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
  1398   int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
  1284       (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
  1399       (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
  1285 
  1400 
  1286   // Switch to using fast clocks for thread cpu time if
  1401   // Switch to using fast clocks for thread cpu time if
  1287   // the sys_clock_getres() returns 0 error code.
  1402   // the clock_getres() returns 0 error code.
  1288   // Note, that some kernels may support the current thread
  1403   // Note, that some kernels may support the current thread
  1289   // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
  1404   // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
  1290   // returned by the pthread_getcpuclockid().
  1405   // returned by the pthread_getcpuclockid().
  1291   // If the fast Posix clocks are supported then the sys_clock_getres()
  1406   // If the fast Posix clocks are supported then the clock_getres()
  1292   // must return at least tp.tv_sec == 0 which means a resolution
  1407   // must return at least tp.tv_sec == 0 which means a resolution
  1293   // better than 1 sec. This is extra check for reliability.
  1408   // better than 1 sec. This is extra check for reliability.
  1294 
  1409 
  1295   if (pthread_getcpuclockid_func &&
  1410   if (pthread_getcpuclockid_func &&
  1296       pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
  1411       pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
  1297       sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
  1412       os::Posix::clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
  1298     _supports_fast_thread_cpu_time = true;
  1413     _supports_fast_thread_cpu_time = true;
  1299     _pthread_getcpuclockid = pthread_getcpuclockid_func;
  1414     _pthread_getcpuclockid = pthread_getcpuclockid_func;
  1300   }
  1415   }
  1301 }
  1416 }
  1302 
  1417 
  1303 jlong os::javaTimeNanos() {
  1418 jlong os::javaTimeNanos() {
  1304   if (os::supports_monotonic_clock()) {
  1419   if (os::supports_monotonic_clock()) {
  1305     struct timespec tp;
  1420     struct timespec tp;
  1306     int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
  1421     int status = os::Posix::clock_gettime(CLOCK_MONOTONIC, &tp);
  1307     assert(status == 0, "gettime error");
  1422     assert(status == 0, "gettime error");
  1308     jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
  1423     jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
  1309     return result;
  1424     return result;
  1310   } else {
  1425   } else {
  1311     timeval time;
  1426     timeval time;
  1400 // called from signal handler. Before adding something to os::abort(), make
  1515 // called from signal handler. Before adding something to os::abort(), make
  1401 // sure it is async-safe and can handle partially initialized VM.
  1516 // sure it is async-safe and can handle partially initialized VM.
  1402 void os::abort(bool dump_core, void* siginfo, const void* context) {
  1517 void os::abort(bool dump_core, void* siginfo, const void* context) {
  1403   os::shutdown();
  1518   os::shutdown();
  1404   if (dump_core) {
  1519   if (dump_core) {
       
  1520     if (DumpPrivateMappingsInCore) {
       
  1521       ClassLoader::close_jrt_image();
       
  1522     }
  1405 #ifndef PRODUCT
  1523 #ifndef PRODUCT
  1406     fdStream out(defaultStream::output_fd());
  1524     fdStream out(defaultStream::output_fd());
  1407     out.print_raw("Current thread is ");
  1525     out.print_raw("Current thread is ");
  1408     char buf[16];
  1526     char buf[16];
  1409     jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
  1527     jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
  1415 
  1533 
  1416   ::exit(1);
  1534   ::exit(1);
  1417 }
  1535 }
  1418 
  1536 
  1419 // Die immediately, no exit hook, no abort hook, no cleanup.
  1537 // Die immediately, no exit hook, no abort hook, no cleanup.
       
  1538 // Dump a core file, if possible, for debugging.
  1420 void os::die() {
  1539 void os::die() {
  1421   ::abort();
  1540   if (TestUnresponsiveErrorHandler && !CreateCoredumpOnCrash) {
       
  1541     // For TimeoutInErrorHandlingTest.java, we just kill the VM
       
  1542     // and don't take the time to generate a core file.
       
  1543     os::signal_raise(SIGKILL);
       
  1544   } else {
       
  1545     ::abort();
       
  1546   }
  1422 }
  1547 }
  1423 
  1548 
  1424 // thread_id is kernel thread id (similar to Solaris LWP id)
  1549 // thread_id is kernel thread id (similar to Solaris LWP id)
  1425 intx os::current_thread_id() { return os::Linux::gettid(); }
  1550 intx os::current_thread_id() { return os::Linux::gettid(); }
  1426 int os::current_process_id() {
  1551 int os::current_process_id() {
  1608 
  1733 
  1609 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
  1734 void * os::dll_load(const char *filename, char *ebuf, int ebuflen) {
  1610   void * result = NULL;
  1735   void * result = NULL;
  1611   bool load_attempted = false;
  1736   bool load_attempted = false;
  1612 
  1737 
       
  1738   log_info(os)("attempting shared library load of %s", filename);
       
  1739 
  1613   // Check whether the library to load might change execution rights
  1740   // Check whether the library to load might change execution rights
  1614   // of the stack. If they are changed, the protection of the stack
  1741   // of the stack. If they are changed, the protection of the stack
  1615   // guard pages will be lost. We need a safepoint to fix this.
  1742   // guard pages will be lost. We need a safepoint to fix this.
  1616   //
  1743   //
  1617   // See Linux man page execstack(8) for more info.
  1744   // See Linux man page execstack(8) for more info.
  1619     if (!ElfFile::specifies_noexecstack(filename)) {
  1746     if (!ElfFile::specifies_noexecstack(filename)) {
  1620       if (!is_init_completed()) {
  1747       if (!is_init_completed()) {
  1621         os::Linux::_stack_is_executable = true;
  1748         os::Linux::_stack_is_executable = true;
  1622         // This is OK - No Java threads have been created yet, and hence no
  1749         // This is OK - No Java threads have been created yet, and hence no
  1623         // stack guard pages to fix.
  1750         // stack guard pages to fix.
  1624         //
       
  1625         // This should happen only when you are building JDK7 using a very
       
  1626         // old version of JDK6 (e.g., with JPRT) and running test_gamma.
       
  1627         //
  1751         //
  1628         // Dynamic loader will make all stacks executable after
  1752         // Dynamic loader will make all stacks executable after
  1629         // this function returns, and will not do that again.
  1753         // this function returns, and will not do that again.
  1630         assert(Threads::number_of_threads() == 0, "no Java threads should exist yet.");
  1754         assert(Threads::number_of_threads() == 0, "no Java threads should exist yet.");
  1631       } else {
  1755       } else {
  1697   if (failed_to_read_elf_head) {
  1821   if (failed_to_read_elf_head) {
  1698     // file i/o error - report dlerror() msg
  1822     // file i/o error - report dlerror() msg
  1699     return NULL;
  1823     return NULL;
  1700   }
  1824   }
  1701 
  1825 
       
  1826   if (elf_head.e_ident[EI_DATA] != LITTLE_ENDIAN_ONLY(ELFDATA2LSB) BIG_ENDIAN_ONLY(ELFDATA2MSB)) {
       
  1827     // handle invalid/out of range endianness values
       
  1828     if (elf_head.e_ident[EI_DATA] == 0 || elf_head.e_ident[EI_DATA] > 2) {
       
  1829       return NULL;
       
  1830     }
       
  1831 
       
  1832 #if defined(VM_LITTLE_ENDIAN)
       
  1833     // VM is LE, shared object BE
       
  1834     elf_head.e_machine = be16toh(elf_head.e_machine);
       
  1835 #else
       
  1836     // VM is BE, shared object LE
       
  1837     elf_head.e_machine = le16toh(elf_head.e_machine);
       
  1838 #endif
       
  1839   }
       
  1840 
  1702   typedef struct {
  1841   typedef struct {
  1703     Elf32_Half    code;         // Actual value as defined in elf.h
  1842     Elf32_Half    code;         // Actual value as defined in elf.h
  1704     Elf32_Half    compat_class; // Compatibility of archs at VM's sense
  1843     Elf32_Half    compat_class; // Compatibility of archs at VM's sense
  1705     unsigned char elf_class;    // 32 or 64 bit
  1844     unsigned char elf_class;    // 32 or 64 bit
  1706     unsigned char endianess;    // MSB or LSB
  1845     unsigned char endianness;   // MSB or LSB
  1707     char*         name;         // String representation
  1846     char*         name;         // String representation
  1708   } arch_t;
  1847   } arch_t;
  1709 
  1848 
  1710 #ifndef EM_486
  1849 #ifndef EM_486
  1711   #define EM_486          6               /* Intel 80486 */
  1850   #define EM_486          6               /* Intel 80486 */
  1728     {EM_SH,          EM_SH,      ELFCLASS32, ELFDATA2LSB, (char*)"SuperH"},
  1867     {EM_SH,          EM_SH,      ELFCLASS32, ELFDATA2LSB, (char*)"SuperH"},
  1729 #else
  1868 #else
  1730     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
  1869     {EM_PPC64,       EM_PPC64,   ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
  1731     {EM_SH,          EM_SH,      ELFCLASS32, ELFDATA2MSB, (char*)"SuperH BE"},
  1870     {EM_SH,          EM_SH,      ELFCLASS32, ELFDATA2MSB, (char*)"SuperH BE"},
  1732 #endif
  1871 #endif
  1733     {EM_ARM,         EM_ARM,     ELFCLASS32,   ELFDATA2LSB, (char*)"ARM"},
  1872     {EM_ARM,         EM_ARM,     ELFCLASS32, ELFDATA2LSB, (char*)"ARM"},
  1734     {EM_S390,        EM_S390,    ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},
  1873     // we only support 64 bit z architecture
       
  1874     {EM_S390,        EM_S390,    ELFCLASS64, ELFDATA2MSB, (char*)"IBM System/390"},
  1735     {EM_ALPHA,       EM_ALPHA,   ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
  1875     {EM_ALPHA,       EM_ALPHA,   ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
  1736     {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
  1876     {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
  1737     {EM_MIPS,        EM_MIPS,    ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
  1877     {EM_MIPS,        EM_MIPS,    ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
  1738     {EM_PARISC,      EM_PARISC,  ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
  1878     {EM_PARISC,      EM_PARISC,  ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
  1739     {EM_68K,         EM_68K,     ELFCLASS32, ELFDATA2MSB, (char*)"M68k"},
  1879     {EM_68K,         EM_68K,     ELFCLASS32, ELFDATA2MSB, (char*)"M68k"},
  1775 #else
  1915 #else
  1776     #error Method os::dll_load requires that one of following is defined:\
  1916     #error Method os::dll_load requires that one of following is defined:\
  1777         AARCH64, ALPHA, ARM, AMD64, IA32, IA64, M68K, MIPS, MIPSEL, PARISC, __powerpc__, __powerpc64__, S390, SH, __sparc
  1917         AARCH64, ALPHA, ARM, AMD64, IA32, IA64, M68K, MIPS, MIPSEL, PARISC, __powerpc__, __powerpc64__, S390, SH, __sparc
  1778 #endif
  1918 #endif
  1779 
  1919 
  1780   // Identify compatability class for VM's architecture and library's architecture
  1920   // Identify compatibility class for VM's architecture and library's architecture
  1781   // Obtain string descriptions for architectures
  1921   // Obtain string descriptions for architectures
  1782 
  1922 
  1783   arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
  1923   arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
  1784   int running_arch_index=-1;
  1924   int running_arch_index=-1;
  1785 
  1925 
  1799     // Even though running architecture detection failed
  1939     // Even though running architecture detection failed
  1800     // we may still continue with reporting dlerror() message
  1940     // we may still continue with reporting dlerror() message
  1801     return NULL;
  1941     return NULL;
  1802   }
  1942   }
  1803 
  1943 
  1804   if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
       
  1805     ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
       
  1806     return NULL;
       
  1807   }
       
  1808 
       
  1809 #ifndef S390
       
  1810   if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
       
  1811     ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
       
  1812     return NULL;
       
  1813   }
       
  1814 #endif // !S390
       
  1815 
       
  1816   if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
  1944   if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
  1817     if (lib_arch.name!=NULL) {
  1945     if (lib_arch.name != NULL) {
  1818       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1946       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1819                  " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
  1947                  " (Possible cause: can't load %s .so on a %s platform)",
  1820                  lib_arch.name, arch_array[running_arch_index].name);
  1948                  lib_arch.name, arch_array[running_arch_index].name);
  1821     } else {
  1949     } else {
  1822       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1950       ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1823                  " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
  1951                  " (Possible cause: can't load this .so (machine code=0x%x) on a %s platform)",
  1824                  lib_arch.code,
  1952                  lib_arch.code, arch_array[running_arch_index].name);
  1825                  arch_array[running_arch_index].name);
  1953     }
  1826     }
  1954     return NULL;
       
  1955   }
       
  1956 
       
  1957   if (lib_arch.endianness != arch_array[running_arch_index].endianness) {
       
  1958     ::snprintf(diag_msg_buf, diag_msg_max_length-1, " (Possible cause: endianness mismatch)");
       
  1959     return NULL;
       
  1960   }
       
  1961 
       
  1962   // ELF file class/capacity : 0 - invalid, 1 - 32bit, 2 - 64bit
       
  1963   if (lib_arch.elf_class > 2 || lib_arch.elf_class < 1) {
       
  1964     ::snprintf(diag_msg_buf, diag_msg_max_length-1, " (Possible cause: invalid ELF file class)");
       
  1965     return NULL;
       
  1966   }
       
  1967 
       
  1968   if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
       
  1969     ::snprintf(diag_msg_buf, diag_msg_max_length-1,
       
  1970                " (Possible cause: architecture word width mismatch, can't load %d-bit .so on a %d-bit platform)",
       
  1971                (int) lib_arch.elf_class * 32, arch_array[running_arch_index].elf_class * 32);
       
  1972     return NULL;
  1827   }
  1973   }
  1828 
  1974 
  1829   return NULL;
  1975   return NULL;
  1830 }
  1976 }
  1831 
  1977 
  1832 void * os::Linux::dlopen_helper(const char *filename, char *ebuf,
  1978 void * os::Linux::dlopen_helper(const char *filename, char *ebuf,
  1833                                 int ebuflen) {
  1979                                 int ebuflen) {
  1834   void * result = ::dlopen(filename, RTLD_LAZY);
  1980   void * result = ::dlopen(filename, RTLD_LAZY);
  1835   if (result == NULL) {
  1981   if (result == NULL) {
  1836     ::strncpy(ebuf, ::dlerror(), ebuflen - 1);
  1982     const char* error_report = ::dlerror();
  1837     ebuf[ebuflen-1] = '\0';
  1983     if (error_report == NULL) {
       
  1984       error_report = "dlerror returned no error description";
       
  1985     }
       
  1986     if (ebuf != NULL && ebuflen > 0) {
       
  1987       ::strncpy(ebuf, error_report, ebuflen-1);
       
  1988       ebuf[ebuflen-1]='\0';
       
  1989     }
       
  1990     Events::log(NULL, "Loading shared library %s failed, %s", filename, error_report);
       
  1991     log_info(os)("shared library load of %s failed, %s", filename, error_report);
       
  1992   } else {
       
  1993     Events::log(NULL, "Loaded shared library %s", filename);
       
  1994     log_info(os)("shared library load of %s was successful", filename);
  1838   }
  1995   }
  1839   return result;
  1996   return result;
  1840 }
  1997 }
  1841 
  1998 
  1842 void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf,
  1999 void * os::Linux::dll_load_in_vmthread(const char *filename, char *ebuf,
  1875 
  2032 
  1876 void* os::get_default_process_handle() {
  2033 void* os::get_default_process_handle() {
  1877   return (void*)::dlopen(NULL, RTLD_LAZY);
  2034   return (void*)::dlopen(NULL, RTLD_LAZY);
  1878 }
  2035 }
  1879 
  2036 
  1880 static bool _print_ascii_file(const char* filename, outputStream* st) {
  2037 static bool _print_ascii_file(const char* filename, outputStream* st, const char* hdr = NULL) {
  1881   int fd = ::open(filename, O_RDONLY);
  2038   int fd = ::open(filename, O_RDONLY);
  1882   if (fd == -1) {
  2039   if (fd == -1) {
  1883     return false;
  2040     return false;
       
  2041   }
       
  2042 
       
  2043   if (hdr != NULL) {
       
  2044     st->print_cr("%s", hdr);
  1884   }
  2045   }
  1885 
  2046 
  1886   char buf[33];
  2047   char buf[33];
  1887   int bytes;
  2048   int bytes;
  1888   buf[32] = '\0';
  2049   buf[32] = '\0';
  1922       char permissions[5];
  2083       char permissions[5];
  1923       char device[6];
  2084       char device[6];
  1924       char name[PATH_MAX + 1];
  2085       char name[PATH_MAX + 1];
  1925 
  2086 
  1926       // Parse fields from line
  2087       // Parse fields from line
  1927       sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %4s " UINT64_FORMAT_X " %5s " INT64_FORMAT " %s",
  2088       sscanf(line, UINT64_FORMAT_X "-" UINT64_FORMAT_X " %4s " UINT64_FORMAT_X " %7s " INT64_FORMAT " %s",
  1928              &base, &top, permissions, &offset, device, &inode, name);
  2089              &base, &top, permissions, &offset, device, &inode, name);
  1929 
  2090 
  1930       // Filter by device id '00:00' so that we only get file system mapped files.
  2091       // Filter by device id '00:00' so that we only get file system mapped files.
  1931       if (strcmp(device, "00:00") != 0) {
  2092       if (strcmp(device, "00:00") != 0) {
  1932 
  2093 
  1973 
  2134 
  1974   os::Linux::print_full_memory_info(st);
  2135   os::Linux::print_full_memory_info(st);
  1975 
  2136 
  1976   os::Linux::print_proc_sys_info(st);
  2137   os::Linux::print_proc_sys_info(st);
  1977 
  2138 
       
  2139   os::Linux::print_ld_preload_file(st);
       
  2140 
  1978   os::Linux::print_container_info(st);
  2141   os::Linux::print_container_info(st);
       
  2142 
       
  2143   VM_Version::print_platform_virtualization_info(st);
       
  2144 
       
  2145   os::Linux::print_steal_info(st);
  1979 }
  2146 }
  1980 
  2147 
  1981 // Try to identify popular distros.
  2148 // Try to identify popular distros.
  1982 // Most Linux distributions have a /etc/XXX-release file, which contains
  2149 // Most Linux distributions have a /etc/XXX-release file, which contains
  1983 // the OS version string. Newer Linux distributions have a /etc/lsb-release
  2150 // the OS version string. Newer Linux distributions have a /etc/lsb-release
  2131   st->print("\n/proc/meminfo:\n");
  2298   st->print("\n/proc/meminfo:\n");
  2132   _print_ascii_file("/proc/meminfo", st);
  2299   _print_ascii_file("/proc/meminfo", st);
  2133   st->cr();
  2300   st->cr();
  2134 }
  2301 }
  2135 
  2302 
       
  2303 void os::Linux::print_ld_preload_file(outputStream* st) {
       
  2304   _print_ascii_file("/etc/ld.so.preload", st, "\n/etc/ld.so.preload:");
       
  2305   st->cr();
       
  2306 }
       
  2307 
  2136 void os::Linux::print_container_info(outputStream* st) {
  2308 void os::Linux::print_container_info(outputStream* st) {
  2137   if (!OSContainer::is_containerized()) {
  2309   if (!OSContainer::is_containerized()) {
  2138     return;
  2310     return;
  2139   }
  2311   }
  2140 
  2312 
  2141   st->print("container (cgroup) information:\n");
  2313   st->print("container (cgroup) information:\n");
  2142 
  2314 
  2143   const char *p_ct = OSContainer::container_type();
  2315   const char *p_ct = OSContainer::container_type();
  2144   st->print("container_type: %s\n", p_ct != NULL ? p_ct : "failed");
  2316   st->print("container_type: %s\n", p_ct != NULL ? p_ct : "not supported");
  2145 
  2317 
  2146   char *p = OSContainer::cpu_cpuset_cpus();
  2318   char *p = OSContainer::cpu_cpuset_cpus();
  2147   st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "failed");
  2319   st->print("cpu_cpuset_cpus: %s\n", p != NULL ? p : "not supported");
  2148   free(p);
  2320   free(p);
  2149 
  2321 
  2150   p = OSContainer::cpu_cpuset_memory_nodes();
  2322   p = OSContainer::cpu_cpuset_memory_nodes();
  2151   st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "failed");
  2323   st->print("cpu_memory_nodes: %s\n", p != NULL ? p : "not supported");
  2152   free(p);
  2324   free(p);
  2153 
  2325 
  2154   int i = OSContainer::active_processor_count();
  2326   int i = OSContainer::active_processor_count();
       
  2327   st->print("active_processor_count: ");
  2155   if (i > 0) {
  2328   if (i > 0) {
  2156     st->print("active_processor_count: %d\n", i);
  2329     st->print("%d\n", i);
  2157   } else {
  2330   } else {
  2158     st->print("active_processor_count: failed\n");
  2331     st->print("not supported\n");
  2159   }
  2332   }
  2160 
  2333 
  2161   i = OSContainer::cpu_quota();
  2334   i = OSContainer::cpu_quota();
  2162   st->print("cpu_quota: %d\n", i);
  2335   st->print("cpu_quota: ");
       
  2336   if (i > 0) {
       
  2337     st->print("%d\n", i);
       
  2338   } else {
       
  2339     st->print("%s\n", i == OSCONTAINER_ERROR ? "not supported" : "no quota");
       
  2340   }
  2163 
  2341 
  2164   i = OSContainer::cpu_period();
  2342   i = OSContainer::cpu_period();
  2165   st->print("cpu_period: %d\n", i);
  2343   st->print("cpu_period: ");
       
  2344   if (i > 0) {
       
  2345     st->print("%d\n", i);
       
  2346   } else {
       
  2347     st->print("%s\n", i == OSCONTAINER_ERROR ? "not supported" : "no period");
       
  2348   }
  2166 
  2349 
  2167   i = OSContainer::cpu_shares();
  2350   i = OSContainer::cpu_shares();
  2168   st->print("cpu_shares: %d\n", i);
  2351   st->print("cpu_shares: ");
       
  2352   if (i > 0) {
       
  2353     st->print("%d\n", i);
       
  2354   } else {
       
  2355     st->print("%s\n", i == OSCONTAINER_ERROR ? "not supported" : "no shares");
       
  2356   }
  2169 
  2357 
  2170   jlong j = OSContainer::memory_limit_in_bytes();
  2358   jlong j = OSContainer::memory_limit_in_bytes();
  2171   st->print("memory_limit_in_bytes: " JLONG_FORMAT "\n", j);
  2359   st->print("memory_limit_in_bytes: ");
       
  2360   if (j > 0) {
       
  2361     st->print(JLONG_FORMAT "\n", j);
       
  2362   } else {
       
  2363     st->print("%s\n", j == OSCONTAINER_ERROR ? "not supported" : "unlimited");
       
  2364   }
  2172 
  2365 
  2173   j = OSContainer::memory_and_swap_limit_in_bytes();
  2366   j = OSContainer::memory_and_swap_limit_in_bytes();
  2174   st->print("memory_and_swap_limit_in_bytes: " JLONG_FORMAT "\n", j);
  2367   st->print("memory_and_swap_limit_in_bytes: ");
       
  2368   if (j > 0) {
       
  2369     st->print(JLONG_FORMAT "\n", j);
       
  2370   } else {
       
  2371     st->print("%s\n", j == OSCONTAINER_ERROR ? "not supported" : "unlimited");
       
  2372   }
  2175 
  2373 
  2176   j = OSContainer::memory_soft_limit_in_bytes();
  2374   j = OSContainer::memory_soft_limit_in_bytes();
  2177   st->print("memory_soft_limit_in_bytes: " JLONG_FORMAT "\n", j);
  2375   st->print("memory_soft_limit_in_bytes: ");
       
  2376   if (j > 0) {
       
  2377     st->print(JLONG_FORMAT "\n", j);
       
  2378   } else {
       
  2379     st->print("%s\n", j == OSCONTAINER_ERROR ? "not supported" : "unlimited");
       
  2380   }
  2178 
  2381 
  2179   j = OSContainer::OSContainer::memory_usage_in_bytes();
  2382   j = OSContainer::OSContainer::memory_usage_in_bytes();
  2180   st->print("memory_usage_in_bytes: " JLONG_FORMAT "\n", j);
  2383   st->print("memory_usage_in_bytes: ");
       
  2384   if (j > 0) {
       
  2385     st->print(JLONG_FORMAT "\n", j);
       
  2386   } else {
       
  2387     st->print("%s\n", j == OSCONTAINER_ERROR ? "not supported" : "unlimited");
       
  2388   }
  2181 
  2389 
  2182   j = OSContainer::OSContainer::memory_max_usage_in_bytes();
  2390   j = OSContainer::OSContainer::memory_max_usage_in_bytes();
  2183   st->print("memory_max_usage_in_bytes: " JLONG_FORMAT "\n", j);
  2391   st->print("memory_max_usage_in_bytes: ");
       
  2392   if (j > 0) {
       
  2393     st->print(JLONG_FORMAT "\n", j);
       
  2394   } else {
       
  2395     st->print("%s\n", j == OSCONTAINER_ERROR ? "not supported" : "unlimited");
       
  2396   }
  2184   st->cr();
  2397   st->cr();
       
  2398 }
       
  2399 
       
  2400 void os::Linux::print_steal_info(outputStream* st) {
       
  2401   if (has_initial_tick_info) {
       
  2402     CPUPerfTicks pticks;
       
  2403     bool res = os::Linux::get_tick_information(&pticks, -1);
       
  2404 
       
  2405     if (res && pticks.has_steal_ticks) {
       
  2406       uint64_t steal_ticks_difference = pticks.steal - initial_steal_ticks;
       
  2407       uint64_t total_ticks_difference = pticks.total - initial_total_ticks;
       
  2408       double steal_ticks_perc = 0.0;
       
  2409       if (total_ticks_difference != 0) {
       
  2410         steal_ticks_perc = (double) steal_ticks_difference / total_ticks_difference;
       
  2411       }
       
  2412       st->print_cr("Steal ticks since vm start: " UINT64_FORMAT, steal_ticks_difference);
       
  2413       st->print_cr("Steal ticks percentage since vm start:%7.3f", steal_ticks_perc);
       
  2414     }
       
  2415   }
  2185 }
  2416 }
  2186 
  2417 
  2187 void os::print_memory_info(outputStream* st) {
  2418 void os::print_memory_info(outputStream* st) {
  2188 
  2419 
  2189   st->print("Memory:");
  2420   st->print("Memory:");
  2257 #elif defined(M68K)
  2488 #elif defined(M68K)
  2258 const char* search_string = "CPU";
  2489 const char* search_string = "CPU";
  2259 #elif defined(PPC64)
  2490 #elif defined(PPC64)
  2260 const char* search_string = "cpu";
  2491 const char* search_string = "cpu";
  2261 #elif defined(S390)
  2492 #elif defined(S390)
  2262 const char* search_string = "processor";
  2493 const char* search_string = "machine =";
  2263 #elif defined(SPARC)
  2494 #elif defined(SPARC)
  2264 const char* search_string = "cpu";
  2495 const char* search_string = "cpu";
  2265 #else
  2496 #else
  2266 const char* search_string = "Processor";
  2497 const char* search_string = "Processor";
  2267 #endif
  2498 #endif
  2443 }
  2674 }
  2444 
  2675 
  2445 ////////////////////////////////////////////////////////////////////////////////
  2676 ////////////////////////////////////////////////////////////////////////////////
  2446 // sun.misc.Signal support
  2677 // sun.misc.Signal support
  2447 
  2678 
  2448 static volatile jint sigint_count = 0;
       
  2449 
       
  2450 static void UserHandler(int sig, void *siginfo, void *context) {
  2679 static void UserHandler(int sig, void *siginfo, void *context) {
  2451   // 4511530 - sem_post is serialized and handled by the manager thread. When
       
  2452   // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
       
  2453   // don't want to flood the manager thread with sem_post requests.
       
  2454   if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1) {
       
  2455     return;
       
  2456   }
       
  2457 
       
  2458   // Ctrl-C is pressed during error reporting, likely because the error
  2680   // Ctrl-C is pressed during error reporting, likely because the error
  2459   // handler fails to abort. Let VM die immediately.
  2681   // handler fails to abort. Let VM die immediately.
  2460   if (sig == SIGINT && VMError::is_error_reported()) {
  2682   if (sig == SIGINT && VMError::is_error_reported()) {
  2461     os::die();
  2683     os::die();
  2462   }
  2684   }
  2464   os::signal_notify(sig);
  2686   os::signal_notify(sig);
  2465 }
  2687 }
  2466 
  2688 
  2467 void* os::user_handler() {
  2689 void* os::user_handler() {
  2468   return CAST_FROM_FN_PTR(void*, UserHandler);
  2690   return CAST_FROM_FN_PTR(void*, UserHandler);
  2469 }
       
  2470 
       
  2471 static struct timespec create_semaphore_timespec(unsigned int sec, int nsec) {
       
  2472   struct timespec ts;
       
  2473   // Semaphore's are always associated with CLOCK_REALTIME
       
  2474   os::Linux::clock_gettime(CLOCK_REALTIME, &ts);
       
  2475   // see os_posix.cpp for discussion on overflow checking
       
  2476   if (sec >= MAX_SECS) {
       
  2477     ts.tv_sec += MAX_SECS;
       
  2478     ts.tv_nsec = 0;
       
  2479   } else {
       
  2480     ts.tv_sec += sec;
       
  2481     ts.tv_nsec += nsec;
       
  2482     if (ts.tv_nsec >= NANOSECS_PER_SEC) {
       
  2483       ts.tv_nsec -= NANOSECS_PER_SEC;
       
  2484       ++ts.tv_sec; // note: this must be <= max_secs
       
  2485     }
       
  2486   }
       
  2487 
       
  2488   return ts;
       
  2489 }
  2691 }
  2490 
  2692 
  2491 extern "C" {
  2693 extern "C" {
  2492   typedef void (*sa_handler_t)(int);
  2694   typedef void (*sa_handler_t)(int);
  2493   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  2695   typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  2545     assert(ReduceSignalUsage, "signal semaphore should be created");
  2747     assert(ReduceSignalUsage, "signal semaphore should be created");
  2546   }
  2748   }
  2547 }
  2749 }
  2548 
  2750 
  2549 static int check_pending_signals() {
  2751 static int check_pending_signals() {
  2550   Atomic::store(0, &sigint_count);
       
  2551   for (;;) {
  2752   for (;;) {
  2552     for (int i = 0; i < NSIG + 1; i++) {
  2753     for (int i = 0; i < NSIG + 1; i++) {
  2553       jint n = pending_signals[i];
  2754       jint n = pending_signals[i];
  2554       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
  2755       if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
  2555         return i;
  2756         return i;
  2811   int highest_node_number = Linux::numa_max_node();
  3012   int highest_node_number = Linux::numa_max_node();
  2812   int num_nodes = 0;
  3013   int num_nodes = 0;
  2813 
  3014 
  2814   // Get the total number of nodes in the system including nodes without memory.
  3015   // Get the total number of nodes in the system including nodes without memory.
  2815   for (node = 0; node <= highest_node_number; node++) {
  3016   for (node = 0; node <= highest_node_number; node++) {
  2816     if (isnode_in_existing_nodes(node)) {
  3017     if (is_node_in_existing_nodes(node)) {
  2817       num_nodes++;
  3018       num_nodes++;
  2818     }
  3019     }
  2819   }
  3020   }
  2820   return num_nodes;
  3021   return num_nodes;
  2821 }
  3022 }
  2827   // Map all node ids in which it is possible to allocate memory. Also nodes are
  3028   // Map all node ids in which it is possible to allocate memory. Also nodes are
  2828   // not always consecutively available, i.e. available from 0 to the highest
  3029   // not always consecutively available, i.e. available from 0 to the highest
  2829   // node number. If the nodes have been bound explicitly using numactl membind,
  3030   // node number. If the nodes have been bound explicitly using numactl membind,
  2830   // then allocate memory from those nodes only.
  3031   // then allocate memory from those nodes only.
  2831   for (int node = 0; node <= highest_node_number; node++) {
  3032   for (int node = 0; node <= highest_node_number; node++) {
  2832     if (Linux::isnode_in_bound_nodes((unsigned int)node)) {
  3033     if (Linux::is_node_in_bound_nodes((unsigned int)node)) {
  2833       ids[i++] = node;
  3034       ids[i++] = node;
  2834     }
  3035     }
  2835   }
  3036   }
  2836   return i;
  3037   return i;
  2837 }
  3038 }
  2930                                                libnuma_dlsym(handle, "numa_bitmask_isbitset")));
  3131                                                libnuma_dlsym(handle, "numa_bitmask_isbitset")));
  2931       set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,
  3132       set_numa_distance(CAST_TO_FN_PTR(numa_distance_func_t,
  2932                                        libnuma_dlsym(handle, "numa_distance")));
  3133                                        libnuma_dlsym(handle, "numa_distance")));
  2933       set_numa_get_membind(CAST_TO_FN_PTR(numa_get_membind_func_t,
  3134       set_numa_get_membind(CAST_TO_FN_PTR(numa_get_membind_func_t,
  2934                                           libnuma_v2_dlsym(handle, "numa_get_membind")));
  3135                                           libnuma_v2_dlsym(handle, "numa_get_membind")));
       
  3136       set_numa_get_interleave_mask(CAST_TO_FN_PTR(numa_get_interleave_mask_func_t,
       
  3137                                                   libnuma_v2_dlsym(handle, "numa_get_interleave_mask")));
  2935 
  3138 
  2936       if (numa_available() != -1) {
  3139       if (numa_available() != -1) {
  2937         set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
  3140         set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
  2938         set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));
  3141         set_numa_all_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_all_nodes_ptr"));
  2939         set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));
  3142         set_numa_nodes_ptr((struct bitmask **)libnuma_dlsym(handle, "numa_nodes_ptr"));
       
  3143         set_numa_interleave_bitmask(_numa_get_interleave_mask());
       
  3144         set_numa_membind_bitmask(_numa_get_membind());
  2940         // Create an index -> node mapping, since nodes are not always consecutive
  3145         // Create an index -> node mapping, since nodes are not always consecutive
  2941         _nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
  3146         _nindex_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
  2942         rebuild_nindex_to_node_map();
  3147         rebuild_nindex_to_node_map();
  2943         // Create a cpu -> node mapping
  3148         // Create a cpu -> node mapping
  2944         _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
  3149         _cpu_to_node = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<int>(0, true);
  2960 void os::Linux::rebuild_nindex_to_node_map() {
  3165 void os::Linux::rebuild_nindex_to_node_map() {
  2961   int highest_node_number = Linux::numa_max_node();
  3166   int highest_node_number = Linux::numa_max_node();
  2962 
  3167 
  2963   nindex_to_node()->clear();
  3168   nindex_to_node()->clear();
  2964   for (int node = 0; node <= highest_node_number; node++) {
  3169   for (int node = 0; node <= highest_node_number; node++) {
  2965     if (Linux::isnode_in_existing_nodes(node)) {
  3170     if (Linux::is_node_in_existing_nodes(node)) {
  2966       nindex_to_node()->append(node);
  3171       nindex_to_node()->append(node);
  2967     }
  3172     }
  2968   }
  3173   }
  2969 }
  3174 }
  2970 
  3175 
  2997   for (size_t i = 0; i < node_num; i++) {
  3202   for (size_t i = 0; i < node_num; i++) {
  2998     // Check if node is configured (not a memory-less node). If it is not, find
  3203     // Check if node is configured (not a memory-less node). If it is not, find
  2999     // the closest configured node. Check also if node is bound, i.e. it's allowed
  3204     // the closest configured node. Check also if node is bound, i.e. it's allowed
  3000     // to allocate memory from the node. If it's not allowed, map cpus in that node
  3205     // to allocate memory from the node. If it's not allowed, map cpus in that node
  3001     // to the closest node from which memory allocation is allowed.
  3206     // to the closest node from which memory allocation is allowed.
  3002     if (!isnode_in_configured_nodes(nindex_to_node()->at(i)) ||
  3207     if (!is_node_in_configured_nodes(nindex_to_node()->at(i)) ||
  3003         !isnode_in_bound_nodes(nindex_to_node()->at(i))) {
  3208         !is_node_in_bound_nodes(nindex_to_node()->at(i))) {
  3004       closest_distance = INT_MAX;
  3209       closest_distance = INT_MAX;
  3005       // Check distance from all remaining nodes in the system. Ignore distance
  3210       // Check distance from all remaining nodes in the system. Ignore distance
  3006       // from itself, from another non-configured node, and from another non-bound
  3211       // from itself, from another non-configured node, and from another non-bound
  3007       // node.
  3212       // node.
  3008       for (size_t m = 0; m < node_num; m++) {
  3213       for (size_t m = 0; m < node_num; m++) {
  3009         if (m != i &&
  3214         if (m != i &&
  3010             isnode_in_configured_nodes(nindex_to_node()->at(m)) &&
  3215             is_node_in_configured_nodes(nindex_to_node()->at(m)) &&
  3011             isnode_in_bound_nodes(nindex_to_node()->at(m))) {
  3216             is_node_in_bound_nodes(nindex_to_node()->at(m))) {
  3012           distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));
  3217           distance = numa_distance(nindex_to_node()->at(i), nindex_to_node()->at(m));
  3013           // If a closest node is found, update. There is always at least one
  3218           // If a closest node is found, update. There is always at least one
  3014           // configured and bound node in the system so there is always at least
  3219           // configured and bound node in the system so there is always at least
  3015           // one node close.
  3220           // one node close.
  3016           if (distance != 0 && distance < closest_distance) {
  3221           if (distance != 0 && distance < closest_distance) {
  3061 os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;
  3266 os::Linux::numa_interleave_memory_v2_func_t os::Linux::_numa_interleave_memory_v2;
  3062 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
  3267 os::Linux::numa_set_bind_policy_func_t os::Linux::_numa_set_bind_policy;
  3063 os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;
  3268 os::Linux::numa_bitmask_isbitset_func_t os::Linux::_numa_bitmask_isbitset;
  3064 os::Linux::numa_distance_func_t os::Linux::_numa_distance;
  3269 os::Linux::numa_distance_func_t os::Linux::_numa_distance;
  3065 os::Linux::numa_get_membind_func_t os::Linux::_numa_get_membind;
  3270 os::Linux::numa_get_membind_func_t os::Linux::_numa_get_membind;
       
  3271 os::Linux::numa_get_interleave_mask_func_t os::Linux::_numa_get_interleave_mask;
       
  3272 os::Linux::NumaAllocationPolicy os::Linux::_current_numa_policy;
  3066 unsigned long* os::Linux::_numa_all_nodes;
  3273 unsigned long* os::Linux::_numa_all_nodes;
  3067 struct bitmask* os::Linux::_numa_all_nodes_ptr;
  3274 struct bitmask* os::Linux::_numa_all_nodes_ptr;
  3068 struct bitmask* os::Linux::_numa_nodes_ptr;
  3275 struct bitmask* os::Linux::_numa_nodes_ptr;
       
  3276 struct bitmask* os::Linux::_numa_interleave_bitmask;
       
  3277 struct bitmask* os::Linux::_numa_membind_bitmask;
  3069 
  3278 
  3070 bool os::pd_uncommit_memory(char* addr, size_t size) {
  3279 bool os::pd_uncommit_memory(char* addr, size_t size) {
  3071   uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
  3280   uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
  3072                                      MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
  3281                                      MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
  3073   return res  != (uintptr_t) MAP_FAILED;
  3282   return res  != (uintptr_t) MAP_FAILED;
  3345   // protection of malloc'ed or statically allocated memory). Check the
  3554   // protection of malloc'ed or statically allocated memory). Check the
  3346   // caller if you hit this assert.
  3555   // caller if you hit this assert.
  3347   assert(addr == bottom, "sanity check");
  3556   assert(addr == bottom, "sanity check");
  3348 
  3557 
  3349   size = align_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
  3558   size = align_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
       
  3559   Events::log(NULL, "Protecting memory [" INTPTR_FORMAT "," INTPTR_FORMAT "] with protection modes %x", p2i(bottom), p2i(bottom+size), prot);
  3350   return ::mprotect(bottom, size, prot) == 0;
  3560   return ::mprotect(bottom, size, prot) == 0;
  3351 }
  3561 }
  3352 
  3562 
  3353 // Set protections specified
  3563 // Set protections specified
  3354 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
  3564 bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
  3428   }
  3638   }
  3429 
  3639 
  3430   return result;
  3640   return result;
  3431 }
  3641 }
  3432 
  3642 
  3433 // Set the coredump_filter bits to include largepages in core dump (bit 6)
       
  3434 //
       
  3435 // From the coredump_filter documentation:
  3643 // From the coredump_filter documentation:
  3436 //
  3644 //
  3437 // - (bit 0) anonymous private memory
  3645 // - (bit 0) anonymous private memory
  3438 // - (bit 1) anonymous shared memory
  3646 // - (bit 1) anonymous shared memory
  3439 // - (bit 2) file-backed private memory
  3647 // - (bit 2) file-backed private memory
  3443 // - (bit 5) hugetlb private memory
  3651 // - (bit 5) hugetlb private memory
  3444 // - (bit 6) hugetlb shared memory
  3652 // - (bit 6) hugetlb shared memory
  3445 // - (bit 7) dax private memory
  3653 // - (bit 7) dax private memory
  3446 // - (bit 8) dax shared memory
  3654 // - (bit 8) dax shared memory
  3447 //
  3655 //
  3448 static void set_coredump_filter(bool largepages, bool dax_shared) {
  3656 static void set_coredump_filter(CoredumpFilterBit bit) {
  3449   FILE *f;
  3657   FILE *f;
  3450   long cdm;
  3658   long cdm;
  3451   bool filter_changed = false;
       
  3452 
  3659 
  3453   if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) {
  3660   if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) {
  3454     return;
  3661     return;
  3455   }
  3662   }
  3456 
  3663 
  3457   if (fscanf(f, "%lx", &cdm) != 1) {
  3664   if (fscanf(f, "%lx", &cdm) != 1) {
  3458     fclose(f);
  3665     fclose(f);
  3459     return;
  3666     return;
  3460   }
  3667   }
  3461 
  3668 
       
  3669   long saved_cdm = cdm;
  3462   rewind(f);
  3670   rewind(f);
  3463 
  3671   cdm |= bit;
  3464   if (largepages && (cdm & LARGEPAGES_BIT) == 0) {
  3672 
  3465     cdm |= LARGEPAGES_BIT;
  3673   if (cdm != saved_cdm) {
  3466     filter_changed = true;
       
  3467   }
       
  3468   if (dax_shared && (cdm & DAX_SHARED_BIT) == 0) {
       
  3469     cdm |= DAX_SHARED_BIT;
       
  3470     filter_changed = true;
       
  3471   }
       
  3472   if (filter_changed) {
       
  3473     fprintf(f, "%#lx", cdm);
  3674     fprintf(f, "%#lx", cdm);
  3474   }
  3675   }
  3475 
  3676 
  3476   fclose(f);
  3677   fclose(f);
  3477 }
  3678 }
  3606   }
  3807   }
  3607 
  3808 
  3608   size_t large_page_size = Linux::setup_large_page_size();
  3809   size_t large_page_size = Linux::setup_large_page_size();
  3609   UseLargePages          = Linux::setup_large_page_type(large_page_size);
  3810   UseLargePages          = Linux::setup_large_page_type(large_page_size);
  3610 
  3811 
  3611   set_coredump_filter(true /*largepages*/, false /*dax_shared*/);
  3812   set_coredump_filter(LARGEPAGES_BIT);
  3612 }
  3813 }
  3613 
  3814 
  3614 #ifndef SHM_HUGETLB
  3815 #ifndef SHM_HUGETLB
  3615   #define SHM_HUGETLB 04000
  3816   #define SHM_HUGETLB 04000
  3616 #endif
  3817 #endif
  3992 
  4193 
  3993 // Reserve memory at an arbitrary address, only if that area is
  4194 // Reserve memory at an arbitrary address, only if that area is
  3994 // available (and not reserved for something else).
  4195 // available (and not reserved for something else).
  3995 
  4196 
  3996 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
  4197 char* os::pd_attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
  3997   const int max_tries = 10;
       
  3998   char* base[max_tries];
       
  3999   size_t size[max_tries];
       
  4000   const size_t gap = 0x000000;
       
  4001 
       
  4002   // Assert only that the size is a multiple of the page size, since
  4198   // Assert only that the size is a multiple of the page size, since
  4003   // that's all that mmap requires, and since that's all we really know
  4199   // that's all that mmap requires, and since that's all we really know
  4004   // about at this low abstraction level.  If we need higher alignment,
  4200   // about at this low abstraction level.  If we need higher alignment,
  4005   // we can either pass an alignment to this method or verify alignment
  4201   // we can either pass an alignment to this method or verify alignment
  4006   // in one of the methods further up the call chain.  See bug 5044738.
  4202   // in one of the methods further up the call chain.  See bug 5044738.
  4019   if (addr != NULL) {
  4215   if (addr != NULL) {
  4020     // mmap() is successful but it fails to reserve at the requested address
  4216     // mmap() is successful but it fails to reserve at the requested address
  4021     anon_munmap(addr, bytes);
  4217     anon_munmap(addr, bytes);
  4022   }
  4218   }
  4023 
  4219 
  4024   int i;
  4220   return NULL;
  4025   for (i = 0; i < max_tries; ++i) {
       
  4026     base[i] = reserve_memory(bytes);
       
  4027 
       
  4028     if (base[i] != NULL) {
       
  4029       // Is this the block we wanted?
       
  4030       if (base[i] == requested_addr) {
       
  4031         size[i] = bytes;
       
  4032         break;
       
  4033       }
       
  4034 
       
  4035       // Does this overlap the block we wanted? Give back the overlapped
       
  4036       // parts and try again.
       
  4037 
       
  4038       ptrdiff_t top_overlap = requested_addr + (bytes + gap) - base[i];
       
  4039       if (top_overlap >= 0 && (size_t)top_overlap < bytes) {
       
  4040         unmap_memory(base[i], top_overlap);
       
  4041         base[i] += top_overlap;
       
  4042         size[i] = bytes - top_overlap;
       
  4043       } else {
       
  4044         ptrdiff_t bottom_overlap = base[i] + bytes - requested_addr;
       
  4045         if (bottom_overlap >= 0 && (size_t)bottom_overlap < bytes) {
       
  4046           unmap_memory(requested_addr, bottom_overlap);
       
  4047           size[i] = bytes - bottom_overlap;
       
  4048         } else {
       
  4049           size[i] = bytes;
       
  4050         }
       
  4051       }
       
  4052     }
       
  4053   }
       
  4054 
       
  4055   // Give back the unused reserved pieces.
       
  4056 
       
  4057   for (int j = 0; j < i; ++j) {
       
  4058     if (base[j] != NULL) {
       
  4059       unmap_memory(base[j], size[j]);
       
  4060     }
       
  4061   }
       
  4062 
       
  4063   if (i < max_tries) {
       
  4064     return requested_addr;
       
  4065   } else {
       
  4066     return NULL;
       
  4067   }
       
  4068 }
       
  4069 
       
  4070 size_t os::read(int fd, void *buf, unsigned int nBytes) {
       
  4071   return ::read(fd, buf, nBytes);
       
  4072 }
       
  4073 
       
  4074 size_t os::read_at(int fd, void *buf, unsigned int nBytes, jlong offset) {
       
  4075   return ::pread(fd, buf, nBytes, offset);
       
  4076 }
       
  4077 
       
  4078 // Short sleep, direct OS call.
       
  4079 //
       
  4080 // Note: certain versions of Linux CFS scheduler (since 2.6.23) do not guarantee
       
  4081 // sched_yield(2) will actually give up the CPU:
       
  4082 //
       
  4083 //   * Alone on this pariticular CPU, keeps running.
       
  4084 //   * Before the introduction of "skip_buddy" with "compat_yield" disabled
       
  4085 //     (pre 2.6.39).
       
  4086 //
       
  4087 // So calling this with 0 is an alternative.
       
  4088 //
       
  4089 void os::naked_short_sleep(jlong ms) {
       
  4090   struct timespec req;
       
  4091 
       
  4092   assert(ms < 1000, "Un-interruptable sleep, short time use only");
       
  4093   req.tv_sec = 0;
       
  4094   if (ms > 0) {
       
  4095     req.tv_nsec = (ms % 1000) * 1000000;
       
  4096   } else {
       
  4097     req.tv_nsec = 1;
       
  4098   }
       
  4099 
       
  4100   nanosleep(&req, NULL);
       
  4101 
       
  4102   return;
       
  4103 }
  4221 }
  4104 
  4222 
  4105 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
  4223 // Sleep forever; naked call to OS-specific sleep; use with CAUTION
  4106 void os::infinite_sleep() {
  4224 void os::infinite_sleep() {
  4107   while (true) {    // sleep forever ...
  4225   while (true) {    // sleep forever ...
  4112 // Used to convert frequent JVM_Yield() to nops
  4230 // Used to convert frequent JVM_Yield() to nops
  4113 bool os::dont_yield() {
  4231 bool os::dont_yield() {
  4114   return DontYieldALot;
  4232   return DontYieldALot;
  4115 }
  4233 }
  4116 
  4234 
       
  4235 // Linux CFS scheduler (since 2.6.23) does not guarantee sched_yield(2) will
       
  4236 // actually give up the CPU. Since skip buddy (v2.6.28):
       
  4237 //
       
  4238 // * Sets the yielding task as skip buddy for current CPU's run queue.
       
  4239 // * Picks next from run queue, if empty, picks a skip buddy (can be the yielding task).
       
  4240 // * Clears skip buddies for this run queue (yielding task no longer a skip buddy).
       
  4241 //
       
  4242 // An alternative is calling os::naked_short_nanosleep with a small number to avoid
       
  4243 // getting re-scheduled immediately.
       
  4244 //
  4117 void os::naked_yield() {
  4245 void os::naked_yield() {
  4118   sched_yield();
  4246   sched_yield();
  4119 }
  4247 }
  4120 
  4248 
  4121 ////////////////////////////////////////////////////////////////////////////////
  4249 ////////////////////////////////////////////////////////////////////////////////
  4131 // The following code actually changes the niceness of kernel-thread/LWP. It
  4259 // The following code actually changes the niceness of kernel-thread/LWP. It
  4132 // has an assumption that setpriority() only modifies one kernel-thread/LWP,
  4260 // has an assumption that setpriority() only modifies one kernel-thread/LWP,
  4133 // not the entire user process, and user level threads are 1:1 mapped to kernel
  4261 // not the entire user process, and user level threads are 1:1 mapped to kernel
  4134 // threads. It has always been the case, but could change in the future. For
  4262 // threads. It has always been the case, but could change in the future. For
  4135 // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
  4263 // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
  4136 // It is only used when ThreadPriorityPolicy=1 and requires root privilege.
  4264 // It is only used when ThreadPriorityPolicy=1 and may require system level permission
       
  4265 // (e.g., root privilege or CAP_SYS_NICE capability).
  4137 
  4266 
  4138 int os::java_to_os_priority[CriticalPriority + 1] = {
  4267 int os::java_to_os_priority[CriticalPriority + 1] = {
  4139   19,              // 0 Entry should never be used
  4268   19,              // 0 Entry should never be used
  4140 
  4269 
  4141    4,              // 1 MinPriority
  4270    4,              // 1 MinPriority
  4155   -5               // 11 CriticalPriority
  4284   -5               // 11 CriticalPriority
  4156 };
  4285 };
  4157 
  4286 
  4158 static int prio_init() {
  4287 static int prio_init() {
  4159   if (ThreadPriorityPolicy == 1) {
  4288   if (ThreadPriorityPolicy == 1) {
  4160     // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
       
  4161     // if effective uid is not root. Perhaps, a more elegant way of doing
       
  4162     // this is to test CAP_SYS_NICE capability, but that will require libcap.so
       
  4163     if (geteuid() != 0) {
  4289     if (geteuid() != 0) {
  4164       if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
  4290       if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
  4165         warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");
  4291         warning("-XX:ThreadPriorityPolicy=1 may require system level permission, " \
       
  4292                 "e.g., being the root user. If the necessary permission is not " \
       
  4293                 "possessed, changes to priority will be silently ignored.");
  4166       }
  4294       }
  4167       ThreadPriorityPolicy = 0;
       
  4168     }
  4295     }
  4169   }
  4296   }
  4170   if (UseCriticalJavaThreadPriority) {
  4297   if (UseCriticalJavaThreadPriority) {
  4171     os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];
  4298     os::java_to_os_priority[MaxPriority] = os::java_to_os_priority[CriticalPriority];
  4172   }
  4299   }
  4382     ShouldNotReachHere();
  4509     ShouldNotReachHere();
  4383   }
  4510   }
  4384 
  4511 
  4385   // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
  4512   // managed to send the signal and switch to SUSPEND_REQUEST, now wait for SUSPENDED
  4386   while (true) {
  4513   while (true) {
  4387     if (sr_semaphore.timedwait(create_semaphore_timespec(0, 2 * NANOSECS_PER_MILLISEC))) {
  4514     if (sr_semaphore.timedwait(2)) {
  4388       break;
  4515       break;
  4389     } else {
  4516     } else {
  4390       // timeout
  4517       // timeout
  4391       os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
  4518       os::SuspendResume::State cancelled = osthread->sr.cancel_suspend();
  4392       if (cancelled == os::SuspendResume::SR_RUNNING) {
  4519       if (cancelled == os::SuspendResume::SR_RUNNING) {
  4416     return;
  4543     return;
  4417   }
  4544   }
  4418 
  4545 
  4419   while (true) {
  4546   while (true) {
  4420     if (sr_notify(osthread) == 0) {
  4547     if (sr_notify(osthread) == 0) {
  4421       if (sr_semaphore.timedwait(create_semaphore_timespec(0, 2 * NANOSECS_PER_MILLISEC))) {
  4548       if (sr_semaphore.timedwait(2)) {
  4422         if (osthread->sr.is_running()) {
  4549         if (osthread->sr.is_running()) {
  4423           return;
  4550           return;
  4424         }
  4551         }
  4425       }
  4552       }
  4426     } else {
  4553     } else {
  4475 // This boolean allows users to forward their own non-matching signals
  4602 // This boolean allows users to forward their own non-matching signals
  4476 // to JVM_handle_linux_signal, harmlessly.
  4603 // to JVM_handle_linux_signal, harmlessly.
  4477 bool os::Linux::signal_handlers_are_installed = false;
  4604 bool os::Linux::signal_handlers_are_installed = false;
  4478 
  4605 
  4479 // For signal-chaining
  4606 // For signal-chaining
  4480 struct sigaction sigact[NSIG];
       
  4481 uint64_t sigs = 0;
       
  4482 #if (64 < NSIG-1)
       
  4483 #error "Not all signals can be encoded in sigs. Adapt its type!"
       
  4484 #endif
       
  4485 bool os::Linux::libjsig_is_loaded = false;
  4607 bool os::Linux::libjsig_is_loaded = false;
  4486 typedef struct sigaction *(*get_signal_t)(int);
  4608 typedef struct sigaction *(*get_signal_t)(int);
  4487 get_signal_t os::Linux::get_signal_action = NULL;
  4609 get_signal_t os::Linux::get_signal_action = NULL;
  4488 
  4610 
  4489 struct sigaction* os::Linux::get_chained_signal_action(int sig) {
  4611 struct sigaction* os::Linux::get_chained_signal_action(int sig) {
  4493     // Retrieve the old signal handler from libjsig
  4615     // Retrieve the old signal handler from libjsig
  4494     actp = (*get_signal_action)(sig);
  4616     actp = (*get_signal_action)(sig);
  4495   }
  4617   }
  4496   if (actp == NULL) {
  4618   if (actp == NULL) {
  4497     // Retrieve the preinstalled signal handler from jvm
  4619     // Retrieve the preinstalled signal handler from jvm
  4498     actp = get_preinstalled_handler(sig);
  4620     actp = os::Posix::get_preinstalled_handler(sig);
  4499   }
  4621   }
  4500 
  4622 
  4501   return actp;
  4623   return actp;
  4502 }
  4624 }
  4503 
  4625 
  4557     }
  4679     }
  4558   }
  4680   }
  4559   return chained;
  4681   return chained;
  4560 }
  4682 }
  4561 
  4683 
  4562 struct sigaction* os::Linux::get_preinstalled_handler(int sig) {
       
  4563   if ((((uint64_t)1 << (sig-1)) & sigs) != 0) {
       
  4564     return &sigact[sig];
       
  4565   }
       
  4566   return NULL;
       
  4567 }
       
  4568 
       
  4569 void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
       
  4570   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
       
  4571   sigact[sig] = oldAct;
       
  4572   sigs |= (uint64_t)1 << (sig-1);
       
  4573 }
       
  4574 
       
  4575 // for diagnostic
  4684 // for diagnostic
  4576 int sigflags[NSIG];
  4685 int sigflags[NSIG];
  4577 
  4686 
  4578 int os::Linux::get_our_sigflags(int sig) {
  4687 int os::Linux::get_our_sigflags(int sig) {
  4579   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
  4688   assert(sig > 0 && sig < NSIG, "vm signal out of expected range");
  4601     if (AllowUserSignalHandlers || !set_installed) {
  4710     if (AllowUserSignalHandlers || !set_installed) {
  4602       // Do not overwrite; user takes responsibility to forward to us.
  4711       // Do not overwrite; user takes responsibility to forward to us.
  4603       return;
  4712       return;
  4604     } else if (UseSignalChaining) {
  4713     } else if (UseSignalChaining) {
  4605       // save the old handler in jvm
  4714       // save the old handler in jvm
  4606       save_preinstalled_handler(sig, oldAct);
  4715       os::Posix::save_preinstalled_handler(sig, oldAct);
  4607       // libjsig also interposes the sigaction() call below and saves the
  4716       // libjsig also interposes the sigaction() call below and saves the
  4608       // old sigaction on it own.
  4717       // old sigaction on it own.
  4609     } else {
  4718     } else {
  4610       fatal("Encountered unexpected pre-existing sigaction handler "
  4719       fatal("Encountered unexpected pre-existing sigaction handler "
  4611             "%#lx for signal %d.", (long)oldhand, sig);
  4720             "%#lx for signal %d.", (long)oldhand, sig);
  4702 // For reference, please, see IEEE Std 1003.1-2004:
  4811 // For reference, please, see IEEE Std 1003.1-2004:
  4703 //   http://www.unix.org/single_unix_specification
  4812 //   http://www.unix.org/single_unix_specification
  4704 
  4813 
  4705 jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
  4814 jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
  4706   struct timespec tp;
  4815   struct timespec tp;
  4707   int rc = os::Linux::clock_gettime(clockid, &tp);
  4816   int rc = os::Posix::clock_gettime(clockid, &tp);
  4708   assert(rc == 0, "clock_gettime is expected to return 0 code");
  4817   assert(rc == 0, "clock_gettime is expected to return 0 code");
  4709 
  4818 
  4710   return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
  4819   return (tp.tv_sec * NANOSECS_PER_SEC) + tp.tv_nsec;
  4711 }
       
  4712 
       
  4713 void os::Linux::initialize_os_info() {
       
  4714   assert(_os_version == 0, "OS info already initialized");
       
  4715 
       
  4716   struct utsname _uname;
       
  4717 
       
  4718   uint32_t major;
       
  4719   uint32_t minor;
       
  4720   uint32_t fix;
       
  4721 
       
  4722   int rc;
       
  4723 
       
  4724   // Kernel version is unknown if
       
  4725   // verification below fails.
       
  4726   _os_version = 0x01000000;
       
  4727 
       
  4728   rc = uname(&_uname);
       
  4729   if (rc != -1) {
       
  4730 
       
  4731     rc = sscanf(_uname.release,"%d.%d.%d", &major, &minor, &fix);
       
  4732     if (rc == 3) {
       
  4733 
       
  4734       if (major < 256 && minor < 256 && fix < 256) {
       
  4735         // Kernel version format is as expected,
       
  4736         // set it overriding unknown state.
       
  4737         _os_version = (major << 16) |
       
  4738                       (minor << 8 ) |
       
  4739                       (fix   << 0 ) ;
       
  4740       }
       
  4741     }
       
  4742   }
       
  4743 }
       
  4744 
       
  4745 uint32_t os::Linux::os_version() {
       
  4746   assert(_os_version != 0, "not initialized");
       
  4747   return _os_version & 0x00FFFFFF;
       
  4748 }
       
  4749 
       
  4750 bool os::Linux::os_version_is_known() {
       
  4751   assert(_os_version != 0, "not initialized");
       
  4752   return _os_version & 0x01000000 ? false : true;
       
  4753 }
  4820 }
  4754 
  4821 
  4755 /////
  4822 /////
  4756 // glibc on Linux platform uses non-documented flag
  4823 // glibc on Linux platform uses non-documented flag
  4757 // to indicate, that some special sort of signal
  4824 // to indicate, that some special sort of signal
  4969   }
  5036   }
  4970   init_page_sizes((size_t) Linux::page_size());
  5037   init_page_sizes((size_t) Linux::page_size());
  4971 
  5038 
  4972   Linux::initialize_system_info();
  5039   Linux::initialize_system_info();
  4973 
  5040 
  4974   Linux::initialize_os_info();
  5041   os::Linux::CPUPerfTicks pticks;
       
  5042   bool res = os::Linux::get_tick_information(&pticks, -1);
       
  5043 
       
  5044   if (res && pticks.has_steal_ticks) {
       
  5045     has_initial_tick_info = true;
       
  5046     initial_total_ticks = pticks.total;
       
  5047     initial_steal_ticks = pticks.steal;
       
  5048   }
  4975 
  5049 
  4976   // _main_thread points to the thread that created/loaded the JVM.
  5050   // _main_thread points to the thread that created/loaded the JVM.
  4977   Linux::_main_thread = pthread_self();
  5051   Linux::_main_thread = pthread_self();
  4978 
       
  4979   Linux::clock_init();
       
  4980   initial_time_count = javaTimeNanos();
       
  4981 
  5052 
  4982   // retrieve entry point for pthread_setname_np
  5053   // retrieve entry point for pthread_setname_np
  4983   Linux::_pthread_setname_np =
  5054   Linux::_pthread_setname_np =
  4984     (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
  5055     (int(*)(pthread_t, const char*))dlsym(RTLD_DEFAULT, "pthread_setname_np");
  4985 
  5056 
  4986   os::Posix::init();
  5057   os::Posix::init();
       
  5058 
       
  5059   initial_time_count = javaTimeNanos();
       
  5060 
       
  5061   // Always warn if no monotonic clock available
       
  5062   if (!os::Posix::supports_monotonic_clock()) {
       
  5063     warning("No monotonic clock was available - timed services may "    \
       
  5064             "be adversely affected if the time-of-day clock changes");
       
  5065   }
  4987 }
  5066 }
  4988 
  5067 
  4989 // To install functions for atexit system call
  5068 // To install functions for atexit system call
  4990 extern "C" {
  5069 extern "C" {
  4991   static void perfMemory_exit_helper() {
  5070   static void perfMemory_exit_helper() {
  4995 
  5074 
  4996 void os::pd_init_container_support() {
  5075 void os::pd_init_container_support() {
  4997   OSContainer::init();
  5076   OSContainer::init();
  4998 }
  5077 }
  4999 
  5078 
       
  5079 void os::Linux::numa_init() {
       
  5080 
       
  5081   // Java can be invoked as
       
  5082   // 1. Without numactl and heap will be allocated/configured on all nodes as
       
  5083   //    per the system policy.
       
  5084   // 2. With numactl --interleave:
       
  5085   //      Use numa_get_interleave_mask(v2) API to get nodes bitmask. The same
       
  5086   //      API for membind case bitmask is reset.
       
  5087   //      Interleave is only hint and Kernel can fallback to other nodes if
       
  5088   //      no memory is available on the target nodes.
       
  5089   // 3. With numactl --membind:
       
  5090   //      Use numa_get_membind(v2) API to get nodes bitmask. The same API for
       
  5091   //      interleave case returns bitmask of all nodes.
       
  5092   // numa_all_nodes_ptr holds bitmask of all nodes.
       
  5093   // numa_get_interleave_mask(v2) and numa_get_membind(v2) APIs returns correct
       
  5094   // bitmask when externally configured to run on all or fewer nodes.
       
  5095 
       
  5096   if (!Linux::libnuma_init()) {
       
  5097     UseNUMA = false;
       
  5098   } else {
       
  5099     if ((Linux::numa_max_node() < 1) || Linux::is_bound_to_single_node()) {
       
  5100       // If there's only one node (they start from 0) or if the process
       
  5101       // is bound explicitly to a single node using membind, disable NUMA.
       
  5102       UseNUMA = false;
       
  5103     } else {
       
  5104 
       
  5105       LogTarget(Info,os) log;
       
  5106       LogStream ls(log);
       
  5107 
       
  5108       Linux::set_configured_numa_policy(Linux::identify_numa_policy());
       
  5109 
       
  5110       struct bitmask* bmp = Linux::_numa_membind_bitmask;
       
  5111       const char* numa_mode = "membind";
       
  5112 
       
  5113       if (Linux::is_running_in_interleave_mode()) {
       
  5114         bmp = Linux::_numa_interleave_bitmask;
       
  5115         numa_mode = "interleave";
       
  5116       }
       
  5117 
       
  5118       ls.print("UseNUMA is enabled and invoked in '%s' mode."
       
  5119                " Heap will be configured using NUMA memory nodes:", numa_mode);
       
  5120 
       
  5121       for (int node = 0; node <= Linux::numa_max_node(); node++) {
       
  5122         if (Linux::_numa_bitmask_isbitset(bmp, node)) {
       
  5123           ls.print(" %d", node);
       
  5124         }
       
  5125       }
       
  5126     }
       
  5127   }
       
  5128 
       
  5129   if (UseParallelGC && UseNUMA && UseLargePages && !can_commit_large_page_memory()) {
       
  5130     // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way
       
  5131     // we can make the adaptive lgrp chunk resizing work. If the user specified both
       
  5132     // UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn
       
  5133     // and disable adaptive resizing.
       
  5134     if (UseAdaptiveSizePolicy || UseAdaptiveNUMAChunkSizing) {
       
  5135       warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, "
       
  5136               "disabling adaptive resizing (-XX:-UseAdaptiveSizePolicy -XX:-UseAdaptiveNUMAChunkSizing)");
       
  5137       UseAdaptiveSizePolicy = false;
       
  5138       UseAdaptiveNUMAChunkSizing = false;
       
  5139     }
       
  5140   }
       
  5141 
       
  5142   if (!UseNUMA && ForceNUMA) {
       
  5143     UseNUMA = true;
       
  5144   }
       
  5145 }
       
  5146 
  5000 // this is called _after_ the global arguments have been parsed
  5147 // this is called _after_ the global arguments have been parsed
  5001 jint os::init_2(void) {
  5148 jint os::init_2(void) {
       
  5149 
       
  5150   // This could be set after os::Posix::init() but all platforms
       
  5151   // have to set it the same so we have to mirror Solaris.
       
  5152   DEBUG_ONLY(os::set_mutex_init_done();)
  5002 
  5153 
  5003   os::Posix::init_2();
  5154   os::Posix::init_2();
  5004 
  5155 
  5005   Linux::fast_thread_clock_init();
  5156   Linux::fast_thread_clock_init();
  5006 
  5157 
  5015   // Initialize data for jdk.internal.misc.Signal
  5166   // Initialize data for jdk.internal.misc.Signal
  5016   if (!ReduceSignalUsage) {
  5167   if (!ReduceSignalUsage) {
  5017     jdk_misc_signal_init();
  5168     jdk_misc_signal_init();
  5018   }
  5169   }
  5019 
  5170 
       
  5171   if (AdjustStackSizeForTLS) {
       
  5172     get_minstack_init();
       
  5173   }
       
  5174 
  5020   // Check and sets minimum stack sizes against command line options
  5175   // Check and sets minimum stack sizes against command line options
  5021   if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
  5176   if (Posix::set_minimum_stack_sizes() == JNI_ERR) {
  5022     return JNI_ERR;
  5177     return JNI_ERR;
  5023   }
  5178   }
  5024 
  5179 
       
  5180 #if defined(IA32)
       
  5181   // Need to ensure we've determined the process's initial stack to
       
  5182   // perform the workaround
       
  5183   Linux::capture_initial_stack(JavaThread::stack_size_at_create());
       
  5184   workaround_expand_exec_shield_cs_limit();
       
  5185 #else
  5025   suppress_primordial_thread_resolution = Arguments::created_by_java_launcher();
  5186   suppress_primordial_thread_resolution = Arguments::created_by_java_launcher();
  5026   if (!suppress_primordial_thread_resolution) {
  5187   if (!suppress_primordial_thread_resolution) {
  5027     Linux::capture_initial_stack(JavaThread::stack_size_at_create());
  5188     Linux::capture_initial_stack(JavaThread::stack_size_at_create());
  5028   }
  5189   }
  5029 
       
  5030 #if defined(IA32)
       
  5031   workaround_expand_exec_shield_cs_limit();
       
  5032 #endif
  5190 #endif
  5033 
  5191 
  5034   Linux::libpthread_init();
  5192   Linux::libpthread_init();
  5035   Linux::sched_getcpu_init();
  5193   Linux::sched_getcpu_init();
  5036   log_info(os)("HotSpot is running with %s, %s",
  5194   log_info(os)("HotSpot is running with %s, %s",
  5037                Linux::glibc_version(), Linux::libpthread_version());
  5195                Linux::glibc_version(), Linux::libpthread_version());
  5038 
  5196 
  5039   if (UseNUMA) {
  5197   if (UseNUMA) {
  5040     if (!Linux::libnuma_init()) {
  5198     Linux::numa_init();
  5041       UseNUMA = false;
       
  5042     } else {
       
  5043       if ((Linux::numa_max_node() < 1) || Linux::isbound_to_single_node()) {
       
  5044         // If there's only one node (they start from 0) or if the process
       
  5045         // is bound explicitly to a single node using membind, disable NUMA.
       
  5046         UseNUMA = false;
       
  5047       }
       
  5048     }
       
  5049 
       
  5050     if (UseParallelGC && UseNUMA && UseLargePages && !can_commit_large_page_memory()) {
       
  5051       // With SHM and HugeTLBFS large pages we cannot uncommit a page, so there's no way
       
  5052       // we can make the adaptive lgrp chunk resizing work. If the user specified both
       
  5053       // UseNUMA and UseLargePages (or UseSHM/UseHugeTLBFS) on the command line - warn
       
  5054       // and disable adaptive resizing.
       
  5055       if (UseAdaptiveSizePolicy || UseAdaptiveNUMAChunkSizing) {
       
  5056         warning("UseNUMA is not fully compatible with SHM/HugeTLBFS large pages, "
       
  5057                 "disabling adaptive resizing (-XX:-UseAdaptiveSizePolicy -XX:-UseAdaptiveNUMAChunkSizing)");
       
  5058         UseAdaptiveSizePolicy = false;
       
  5059         UseAdaptiveNUMAChunkSizing = false;
       
  5060       }
       
  5061     }
       
  5062 
       
  5063     if (!UseNUMA && ForceNUMA) {
       
  5064       UseNUMA = true;
       
  5065     }
       
  5066   }
  5199   }
  5067 
  5200 
  5068   if (MaxFDLimit) {
  5201   if (MaxFDLimit) {
  5069     // set the number of file descriptors to max. print out error
  5202     // set the number of file descriptors to max. print out error
  5070     // if getrlimit/setrlimit fails but continue regardless.
  5203     // if getrlimit/setrlimit fails but continue regardless.
  5079         log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno));
  5212         log_info(os)("os::init_2 setrlimit failed: %s", os::strerror(errno));
  5080       }
  5213       }
  5081     }
  5214     }
  5082   }
  5215   }
  5083 
  5216 
  5084   // Initialize lock used to serialize thread creation (see os::create_thread)
       
  5085   Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));
       
  5086 
       
  5087   // at-exit methods are called in the reverse order of their registration.
  5217   // at-exit methods are called in the reverse order of their registration.
  5088   // atexit functions are called on return from main or as a result of a
  5218   // atexit functions are called on return from main or as a result of a
  5089   // call to exit(3C). There can be only 32 of these functions registered
  5219   // call to exit(3C). There can be only 32 of these functions registered
  5090   // and atexit() does not set errno.
  5220   // and atexit() does not set errno.
  5091 
  5221 
  5104   }
  5234   }
  5105 
  5235 
  5106   // initialize thread priority policy
  5236   // initialize thread priority policy
  5107   prio_init();
  5237   prio_init();
  5108 
  5238 
  5109   if (!FLAG_IS_DEFAULT(AllocateHeapAt)) {
  5239   if (!FLAG_IS_DEFAULT(AllocateHeapAt) || !FLAG_IS_DEFAULT(AllocateOldGenAt)) {
  5110     set_coredump_filter(false /*largepages*/, true /*dax_shared*/);
  5240     set_coredump_filter(DAX_SHARED_BIT);
  5111   }
  5241   }
       
  5242 
       
  5243   if (DumpPrivateMappingsInCore) {
       
  5244     set_coredump_filter(FILE_BACKED_PVT_BIT);
       
  5245   }
       
  5246 
       
  5247   if (DumpSharedMappingsInCore) {
       
  5248     set_coredump_filter(FILE_BACKED_SHARED_BIT);
       
  5249   }
       
  5250 
  5112   return JNI_OK;
  5251   return JNI_OK;
  5113 }
  5252 }
  5114 
  5253 
  5115 // Mark the polling page as unreadable
  5254 // Mark the polling page as unreadable
  5116 void os::make_polling_page_unreadable(void) {
  5255 void os::make_polling_page_unreadable(void) {
  5271     buf[sizeof(buf) - 1] = '\0';
  5410     buf[sizeof(buf) - 1] = '\0';
  5272     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
  5411     const int rc = Linux::_pthread_setname_np(pthread_self(), buf);
  5273     // ERANGE should not happen; all other errors should just be ignored.
  5412     // ERANGE should not happen; all other errors should just be ignored.
  5274     assert(rc != ERANGE, "pthread_setname_np failed");
  5413     assert(rc != ERANGE, "pthread_setname_np failed");
  5275   }
  5414   }
  5276 }
       
  5277 
       
  5278 bool os::distribute_processes(uint length, uint* distribution) {
       
  5279   // Not yet implemented.
       
  5280   return false;
       
  5281 }
  5415 }
  5282 
  5416 
  5283 bool os::bind_to_processor(uint processor_id) {
  5417 bool os::bind_to_processor(uint processor_id) {
  5284   // Not yet implemented.
  5418   // Not yet implemented.
  5285   return false;
  5419   return false;
  5715 
  5849 
  5716 // Run the specified command in a separate process. Return its exit value,
  5850 // Run the specified command in a separate process. Return its exit value,
  5717 // or -1 on failure (e.g. can't fork a new process).
  5851 // or -1 on failure (e.g. can't fork a new process).
  5718 // Unlike system(), this function can be called from signal handler. It
  5852 // Unlike system(), this function can be called from signal handler. It
  5719 // doesn't block SIGINT et al.
  5853 // doesn't block SIGINT et al.
  5720 int os::fork_and_exec(char* cmd) {
  5854 int os::fork_and_exec(char* cmd, bool use_vfork_if_available) {
  5721   const char * argv[4] = {"sh", "-c", cmd, NULL};
  5855   const char * argv[4] = {"sh", "-c", cmd, NULL};
  5722 
  5856 
  5723   pid_t pid = fork();
  5857   pid_t pid ;
       
  5858 
       
  5859   if (use_vfork_if_available) {
       
  5860     pid = vfork();
       
  5861   } else {
       
  5862     pid = fork();
       
  5863   }
  5724 
  5864 
  5725   if (pid < 0) {
  5865   if (pid < 0) {
  5726     // fork failed
  5866     // fork failed
  5727     return -1;
  5867     return -1;
  5728 
  5868 
  5973 #endif
  6113 #endif
  5974 
  6114 
  5975 static inline struct timespec get_mtime(const char* filename) {
  6115 static inline struct timespec get_mtime(const char* filename) {
  5976   struct stat st;
  6116   struct stat st;
  5977   int ret = os::stat(filename, &st);
  6117   int ret = os::stat(filename, &st);
  5978   assert(ret == 0, "failed to stat() file '%s': %s", filename, strerror(errno));
  6118   assert(ret == 0, "failed to stat() file '%s': %s", filename, os::strerror(errno));
  5979   return st.st_mtim;
  6119   return st.st_mtim;
  5980 }
  6120 }
  5981 
  6121 
  5982 int os::compare_file_modified_times(const char* file1, const char* file2) {
  6122 int os::compare_file_modified_times(const char* file1, const char* file2) {
  5983   struct timespec filetime1 = get_mtime(file1);
  6123   struct timespec filetime1 = get_mtime(file1);
  5987     return filetime1.tv_nsec - filetime2.tv_nsec;
  6127     return filetime1.tv_nsec - filetime2.tv_nsec;
  5988   }
  6128   }
  5989   return diff;
  6129   return diff;
  5990 }
  6130 }
  5991 
  6131 
       
  6132 bool os::supports_map_sync() {
       
  6133   return true;
       
  6134 }
       
  6135 
  5992 /////////////// Unit tests ///////////////
  6136 /////////////// Unit tests ///////////////
  5993 
  6137 
  5994 #ifndef PRODUCT
  6138 #ifndef PRODUCT
  5995 
       
  5996 #define test_log(...)              \
       
  5997   do {                             \
       
  5998     if (VerboseInternalVMTests) {  \
       
  5999       tty->print_cr(__VA_ARGS__);  \
       
  6000       tty->flush();                \
       
  6001     }                              \
       
  6002   } while (false)
       
  6003 
  6139 
  6004 class TestReserveMemorySpecial : AllStatic {
  6140 class TestReserveMemorySpecial : AllStatic {
  6005  public:
  6141  public:
  6006   static void small_page_write(void* addr, size_t size) {
  6142   static void small_page_write(void* addr, size_t size) {
  6007     size_t page_size = os::vm_page_size();
  6143     size_t page_size = os::vm_page_size();
  6014 
  6150 
  6015   static void test_reserve_memory_special_huge_tlbfs_only(size_t size) {
  6151   static void test_reserve_memory_special_huge_tlbfs_only(size_t size) {
  6016     if (!UseHugeTLBFS) {
  6152     if (!UseHugeTLBFS) {
  6017       return;
  6153       return;
  6018     }
  6154     }
  6019 
       
  6020     test_log("test_reserve_memory_special_huge_tlbfs_only(" SIZE_FORMAT ")", size);
       
  6021 
  6155 
  6022     char* addr = os::Linux::reserve_memory_special_huge_tlbfs_only(size, NULL, false);
  6156     char* addr = os::Linux::reserve_memory_special_huge_tlbfs_only(size, NULL, false);
  6023 
  6157 
  6024     if (addr != NULL) {
  6158     if (addr != NULL) {
  6025       small_page_write(addr, size);
  6159       small_page_write(addr, size);
  6075     // mapping will serve as a value for a "good" req_addr (case 2). The second
  6209     // mapping will serve as a value for a "good" req_addr (case 2). The second
  6076     // mapping, still intact, as "bad" req_addr (case 3).
  6210     // mapping, still intact, as "bad" req_addr (case 3).
  6077     ::munmap(mapping1, mapping_size);
  6211     ::munmap(mapping1, mapping_size);
  6078 
  6212 
  6079     // Case 1
  6213     // Case 1
  6080     test_log("%s, req_addr NULL:", __FUNCTION__);
       
  6081     test_log("size            align           result");
       
  6082 
       
  6083     for (int i = 0; i < num_sizes; i++) {
  6214     for (int i = 0; i < num_sizes; i++) {
  6084       const size_t size = sizes[i];
  6215       const size_t size = sizes[i];
  6085       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6216       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6086         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, NULL, false);
  6217         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, NULL, false);
  6087         test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " ->  " PTR_FORMAT " %s",
       
  6088                  size, alignment, p2i(p), (p != NULL ? "" : "(failed)"));
       
  6089         if (p != NULL) {
  6218         if (p != NULL) {
  6090           assert(is_aligned(p, alignment), "must be");
  6219           assert(is_aligned(p, alignment), "must be");
  6091           small_page_write(p, size);
  6220           small_page_write(p, size);
  6092           os::Linux::release_memory_special_huge_tlbfs(p, size);
  6221           os::Linux::release_memory_special_huge_tlbfs(p, size);
  6093         }
  6222         }
  6094       }
  6223       }
  6095     }
  6224     }
  6096 
  6225 
  6097     // Case 2
  6226     // Case 2
  6098     test_log("%s, req_addr non-NULL:", __FUNCTION__);
       
  6099     test_log("size            align           req_addr         result");
       
  6100 
       
  6101     for (int i = 0; i < num_sizes; i++) {
  6227     for (int i = 0; i < num_sizes; i++) {
  6102       const size_t size = sizes[i];
  6228       const size_t size = sizes[i];
  6103       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6229       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6104         char* const req_addr = align_up(mapping1, alignment);
  6230         char* const req_addr = align_up(mapping1, alignment);
  6105         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);
  6231         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);
  6106         test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " ->  " PTR_FORMAT " %s",
       
  6107                  size, alignment, p2i(req_addr), p2i(p),
       
  6108                  ((p != NULL ? (p == req_addr ? "(exact match)" : "") : "(failed)")));
       
  6109         if (p != NULL) {
  6232         if (p != NULL) {
  6110           assert(p == req_addr, "must be");
  6233           assert(p == req_addr, "must be");
  6111           small_page_write(p, size);
  6234           small_page_write(p, size);
  6112           os::Linux::release_memory_special_huge_tlbfs(p, size);
  6235           os::Linux::release_memory_special_huge_tlbfs(p, size);
  6113         }
  6236         }
  6114       }
  6237       }
  6115     }
  6238     }
  6116 
  6239 
  6117     // Case 3
  6240     // Case 3
  6118     test_log("%s, req_addr non-NULL with preexisting mapping:", __FUNCTION__);
       
  6119     test_log("size            align           req_addr         result");
       
  6120 
       
  6121     for (int i = 0; i < num_sizes; i++) {
  6241     for (int i = 0; i < num_sizes; i++) {
  6122       const size_t size = sizes[i];
  6242       const size_t size = sizes[i];
  6123       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6243       for (size_t alignment = ag; is_aligned(size, alignment); alignment *= 2) {
  6124         char* const req_addr = align_up(mapping2, alignment);
  6244         char* const req_addr = align_up(mapping2, alignment);
  6125         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);
  6245         char* p = os::Linux::reserve_memory_special_huge_tlbfs_mixed(size, alignment, req_addr, false);
  6126         test_log(SIZE_FORMAT_HEX " " SIZE_FORMAT_HEX " " PTR_FORMAT " ->  " PTR_FORMAT " %s",
       
  6127                  size, alignment, p2i(req_addr), p2i(p), ((p != NULL ? "" : "(failed)")));
       
  6128         // as the area around req_addr contains already existing mappings, the API should always
  6246         // as the area around req_addr contains already existing mappings, the API should always
  6129         // return NULL (as per contract, it cannot return another address)
  6247         // return NULL (as per contract, it cannot return another address)
  6130         assert(p == NULL, "must be");
  6248         assert(p == NULL, "must be");
  6131       }
  6249       }
  6132     }
  6250     }
  6147   static void test_reserve_memory_special_shm(size_t size, size_t alignment) {
  6265   static void test_reserve_memory_special_shm(size_t size, size_t alignment) {
  6148     if (!UseSHM) {
  6266     if (!UseSHM) {
  6149       return;
  6267       return;
  6150     }
  6268     }
  6151 
  6269 
  6152     test_log("test_reserve_memory_special_shm(" SIZE_FORMAT ", " SIZE_FORMAT ")", size, alignment);
       
  6153 
       
  6154     char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false);
  6270     char* addr = os::Linux::reserve_memory_special_shm(size, alignment, NULL, false);
  6155 
  6271 
  6156     if (addr != NULL) {
  6272     if (addr != NULL) {
  6157       assert(is_aligned(addr, alignment), "Check");
  6273       assert(is_aligned(addr, alignment), "Check");
  6158       assert(is_aligned(addr, os::large_page_size()), "Check");
  6274       assert(is_aligned(addr, os::large_page_size()), "Check");