os_linux.cpp revision 761
579N/A * Copyright 1999-2009 Sun Microsystems, Inc. All Rights Reserved. 0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 0N/A * This code is free software; you can redistribute it and/or modify it 0N/A * under the terms of the GNU General Public License version 2 only, as 0N/A * published by the Free Software Foundation. 0N/A * This code is distributed in the hope that it will be useful, but WITHOUT 0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 0N/A * version 2 for more details (a copy is included in the LICENSE file that 0N/A * accompanied this code). 0N/A * You should have received a copy of the GNU General Public License version 0N/A * 2 along with this work; if not, write to the Free Software Foundation, 0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 0N/A * CA 95054 USA or visit www.sun.com if you need additional information or 0N/A * have any questions. 0N/A// do not include precompiled header file 0N/A// put OS-includes here 0N/A// for timer info max values which include all bits 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// For diagnostics to print a message once. see run_periodic_checks 0N/A/* do not use any signal number less than SIGSEGV, see 4355769 */ 242N/A/* Used to protect dlsym() calls */ 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A // values in struct sysinfo are "unsigned long" 20N/A // See comments under solaris for alignment considerations 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// environment support 0N/A if (
len > 0)
buf[0] = 0;
// return a null string 0N/A// Return true if user is running as root. 0N/A// i386: 224, ia64: 1105, amd64: 186, sparc 143 0N/A// Cpu architecture string 0N/A// Returns the kernel thread id of the currently running thread. Kernel 0N/A// thread id is used to access /proc. 0N/A// (Note that getpid() on LinuxThreads returns kernel thread id too; but 0N/A// on NPTL, it returns the same pid for all threads, as required by POSIX.) 0N/A // old kernel, no NPTL support 0N/A// Most versions of linux have a bug where the number of processors are 0N/A// determined by looking at the /proc file system. In a chroot environment, 0N/A// the system call returns 1. This causes the VM to act as if it is 0N/A// a single processor and elide locking (see is_MP() call). 199N/A "Java may be unstable running multithreaded in a chroot " 199N/A "environment on Linux when /proc filesystem is not mounted.";
0N/A// sysinfo(SI_ARCHITECTURE, arch, sizeof(arch)); 0N/A // The next steps are taken in the product version: 0N/A // Obtain the JAVA_HOME value from the location of libjvm[_g].so. 0N/A // This library should be located at: 0N/A // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm[_g].so. 0N/A // If "/jre/lib/" appears at the right place in the path, then we 0N/A // assume libjvm[_g].so is installed in a JDK and we use this path. 0N/A // Otherwise exit with message: "Could not create the Java virtual machine." 0N/A // The following extra steps are taken in the debugging version: 0N/A // If "/jre/lib/" does NOT appear at the right place in the path 0N/A // instead of exit check for $JAVA_HOME environment variable. 0N/A // it looks like libjvm[_g].so is installed there 0N/A // Important note: if the location of libjvm.so changes this 0N/A // code needs to be changed accordingly. 0N/A // The next few definitions allow the code to be verbatim: 0N/A * The linker uses the following search paths to locate required 0N/A * 7: The default directories, normally /lib and /usr/lib. 0N/A /* sysclasspath, java_home, dll_dir */ 0N/A // Now cut the path to <java_home>/jre if we can. 0N/A *
pslash =
'\0';
/* get rid of /{client|server|hotspot} */ 0N/A * Where to look for native libraries 0N/A * Note: Due to a legacy implementation, most of the library path 0N/A * is set in the launcher. This was to accomodate linking restrictions 0N/A * on legacy Linux implementations (which are no longer supported). 0N/A * Eventually, all the library path setting will be done here. 0N/A * However, to prevent the proliferation of improperly built native 0N/A * Eventually, all the library path setting will be done here. 0N/A * Construct the invariant part of ld_library_path. Note that the 0N/A * space for the colon and the trailing null are provided by the 0N/A * nulls included by the sizeof operator (so actually we allocate 0N/A * a byte more than necessary). 0N/A * Get the user setting of LD_LIBRARY_PATH, and prepended it. It 0N/A * should always exist (until the legacy problem cited above is 0N/A /* That's +1 for the colon and +1 for the trailing '\0' */ 0N/A * Extensions directories. 0N/A * Note that the space for the colon and the trailing null are provided 0N/A * by the nulls included by the sizeof operator (so actually one byte more 0N/A * than necessary is allocated). 0N/A /* Endorsed standards default directory. */ 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// breakpoint support 0N/A // use debugger to set breakpoint here 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A // Should also have an assertion stating we are still single-threaded. 0N/A // Fill in signals that are necessarily unblocked for all threads in 0N/A // the VM. Currently, we unblock the following signals: 0N/A // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden 0N/A // by -Xrs (=ReduceSignalUsage)); 0N/A // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all 0N/A // other threads. The "ReduceSignalUsage" boolean tells us not to alter 0N/A // the dispositions or masks wrt these signals. 0N/A // Programs embedding the VM that want to use the above signals for their 0N/A // own purposes must, at this time, use the "-Xrs" option to prevent 0N/A // interference with shutdown hooks and BREAK_SIGNAL thread dumping. 0N/A // (See bug 4345157, and other related bugs). 0N/A // In reality, though, unblocking these signals is really a nop, since 0N/A // these signals are not blocked by default. 0N/A // Fill in signals that are blocked by all but the VM thread. 0N/A// These are signals that are unblocked while a thread is running Java. 0N/A// (For some reason, they get blocked by default.) 0N/A// These are the signals that are blocked while a (non-VM) thread is 0N/A// running Java. Only the VM thread handles these signals. 0N/A// These are signals that are blocked during cond_wait to allow debugger in 0N/A //Save caller's signal mask before setting VM signal mask 0N/A // Only the VM thread handles BREAK_SIGNAL ... 0N/A // ... all other threads block BREAK_SIGNAL 0N/A////////////////////////////////////////////////////////////////////////////// 0N/A// detecting pthread library 0N/A // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION 0N/A // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a 0N/A // generic name for earlier versions. 0N/A // Define macros here so we can build HotSpot on old systems. 0N/A // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version() 0N/A // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells 0N/A // us "NPTL-0.29" even we are running with LinuxThreads. Check if this 199N/A // is the case. LinuxThreads has a hard limit on max number of threads. 199N/A // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value. 199N/A // On the other hand, NPTL does not have such a limit, sysconf() 199N/A // will return -1 and errno is not changed. Check if it is really NPTL. 199N/A // glibc before 2.3.2 only has LinuxThreads. 0N/A // LinuxThreads have two flavors: floating-stack mode, which allows variable 0N/A // stack size; and fixed-stack mode. NPTL is always floating-stack. 0N/A///////////////////////////////////////////////////////////////////////////// 0N/A// Force Linux kernel to expand current thread stack. If "bottom" is close 0N/A// to the stack guard, caller should block all signals. 0N/A// A special mmap() flag that is used to implement thread stacks. It tells 0N/A// kernel that the memory region should extend downwards when needed. This 0N/A// allows early versions of LinuxThreads to only mmap the first few pages 0N/A// when creating a new thread. Linux kernel will automatically expand thread 0N/A// stack as needed (on page faults). 0N/A// However, because the memory region of a MAP_GROWSDOWN stack can grow on 0N/A// demand, if a page fault happens outside an already mapped MAP_GROWSDOWN 0N/A// region, it's hard to tell if the fault is due to a legitimate stack 0N/A// overrun). As a rule, if the fault happens below current stack pointer, 0N/A// Linux kernel does not expand stack, instead a SIGSEGV is sent to the 0N/A// This Linux feature can cause SIGSEGV when VM bangs thread stack for 0N/A// stack overflow detection. 0N/A// Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do 0N/A// not use this flag. However, the stack of initial thread is not created 0N/A// by pthread, it is still MAP_GROWSDOWN. Also it's possible (though 0N/A// unlikely) that user code can create a thread with MAP_GROWSDOWN stack 0N/A// and then attach the thread to JVM. 0N/A// To get around the problem and allow stack banging on Linux, we need to 0N/A// manually expand thread stack after receiving the SIGSEGV. 0N/A// There are two ways to expand thread stack to address "bottom", we used 0N/A// both of them in JVM before 1.5: 0N/A// 1. adjust stack pointer first so that it is below "bottom", and then 0N/A// 2. mmap() the page in question 0N/A// Now alternate signal stack is gone, it's harder to use 2. For instance, 0N/A// if current sp is already near the lower end of page 101, and we need to 0N/A// call mmap() to map page 100, it is possible that part of the mmap() frame 0N/A// will be placed in page 100. When page 100 is mapped, it is zero-filled. 0N/A// That will destroy the mmap() frame and cause VM to crash. 0N/A// The following code works by adjusting sp first, then accessing the "bottom" 0N/A// page to force a page fault. Linux kernel will then automatically expand the 0N/A// _expand_stack_to() assumes its frame size is less than page size, which 0N/A// should always be true if the function is not inlined. 0N/A#
if __GNUC__ <
3 // gcc 2.x does not support noinline attribute 0N/A // Adjust bottom to point to the largest address within the same page, it 0N/A // gives us a one-page buffer if alloca() allocates slightly more memory. 0N/A // sp might be slightly above current stack pointer; if that's the case, we 0N/A // will alloca() a little more space than necessary, which is OK. Don't use 0N/A // os::current_stack_pointer(), as its result can be slightly below current 0N/A // stack pointer, causing us to not alloca enough to reach "bottom". 0N/A////////////////////////////////////////////////////////////////////////////// 0N/A// check if it's safe to start a new thread 0N/A // Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat) 0N/A // Heap is mmap'ed at lower end of memory space. Thread stacks are 0N/A // allocated (MAP_FIXED) from high address space. Every thread stack 0N/A // occupies a fixed size slot (usually 2Mbytes, but user can change 0N/A // it to other values if they rebuild LinuxThreads). 0N/A // Problem with MAP_FIXED is that mmap() can still succeed even part of 0N/A // the memory region has already been mmap'ed. That means if we have too 0N/A // many threads and/or very large heap, eventually thread stack will 0N/A // collide with heap. 0N/A // Here we try to prevent heap/stack collision by comparing current 0N/A // stack bottom with the highest address that has been mmap'ed by JVM 0N/A // plus a safety margin for memory maps created by native code. 0N/A // This feature can be disabled by setting ThreadSafetyMargin to 0 0N/A // not safe if our stack extends below the safety margin 0N/A // Floating stack LinuxThreads or NPTL: 0N/A // Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When 0N/A // there's not enough space left, pthread_create() will fail. If we come 0N/A // here, that means enough space has been reserved for stack. 0N/A// Thread start routine for all newly created threads 0N/A // Try to randomize the cache line index of hot stack frames. 0N/A // This helps when threads of the same stack traces evict each other's 0N/A // cache lines. The threads can be either from the same JVM instance, or 0N/A // from different JVM instances. The benefit is especially true for 0N/A // processors with hyperthreading technology. 0N/A // non floating stack LinuxThreads needs extra check, see above 0N/A // notify parent thread 0N/A // thread_id is kernel thread id (similar to Solaris LWP id) 0N/A // initialize signal mask for this thread 0N/A // initialize floating point control register 0N/A // handshaking with parent thread 0N/A // notify parent thread 0N/A // wait until os::start_thread() 0N/A // call one more level start routine 0N/A // Allocate the OSThread object 0N/A // set the correct thread state 0N/A // Initial state is ALLOCATED but not INITIALIZED 0N/A // init thread attributes 0N/A // calculate stack size if it's not specified by caller 0N/A // Java threads use ThreadStackSize which default value can be changed with the flag -Xss 0N/A }
// else fall through: 0N/A // use VMThreadStackSize if CompilerThreadStackSize is not defined 0N/A // let pthread_create() pick the default value. 0N/A // Serialize thread creation if we are running with fixed stack LinuxThreads 0N/A // Need to clean up stuff we've allocated so far 0N/A // Store pthread info into the OSThread 0N/A // Wait until child thread is either initialized or aborted 0N/A // Aborted due to thread limit being reached 0N/A // The thread is returned suspended (in state INITIALIZED), 0N/A // and is started higher up in the call chain 0N/A///////////////////////////////////////////////////////////////////////////// 0N/A// attach existing thread 0N/A// bootstrap the main thread 0N/A // Allocate the OSThread object 0N/A // Store pthread info into the OSThread 0N/A // initialize floating point control register 0N/A // Initial thread state is RUNNABLE 0N/A // If current thread is initial thread, its stack is mapped on demand, 0N/A // see notes about MAP_GROWSDOWN. Here we try to force kernel to map 0N/A // the entire stack region to avoid SEGV in stack banging. 0N/A // It is also useful to get around the heap-stack-gap problem on SuSE 0N/A // kernel (see 4821821 for details). We first expand stack to the top 0N/A // of yellow zone, then enable stack yellow zone (order is significant, 0N/A // enabling yellow zone first will crash JVM on SuSE Linux), so there 0N/A // is no gap between the last two virtual memory regions. 0N/A // initialize signal mask for this thread 0N/A // and save the caller's signal mask 0N/A// Free Linux resources related to the OSThread 0N/A // Restore caller's signal mask 0N/A////////////////////////////////////////////////////////////////////////////// 0N/A// thread local storage 0N/A// Note: This is currently not used by VM, as we don't destroy TLS key 0N/A////////////////////////////////////////////////////////////////////////////// 0N/A// Check if current thread is the initial thread, similar to Solaris thr_main. 0N/A // If called before init complete, thread stack bottom will be null. 0N/A // Can be called if fatal error occurs before initialization. 0N/A "os::init did not locate initial thread's stack region");
0N/A// Find the virtual memory area that contains addr 0N/A// Locate initial thread stack. This special handling of initial thread stack 0N/A// is needed because pthread_getattr_np() on most (all?) Linux distros returns 0N/A// bogus value for initial thread. 0N/A // stack size is the easy part, get it from RLIMIT_STACK 0N/A // 6308388: a bug in ld.so will relocate its own .data section to the 0N/A // lower end of primordial stack; reduce ulimit -s value a little bit 0N/A // so we won't install guard page on ld.so's data section. 0N/A // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat 0N/A // 7.1, in both cases we will get 2G in return value. 0N/A // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0, 0N/A // SuSE 7.2, Debian) can not handle alternate signal stack correctly 0N/A // for initial thread if its stack size exceeds 6M. Cap it at 2M, 0N/A // in case other parts in glibc still assumes 2M max stack size. 0N/A // FIXME: alt signal stack is gone, maybe we can relax this constraint? 0N/A // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small 0N/A // Try to figure out where the stack base (top) is. This is harder. 0N/A // When an application is started, glibc saves the initial stack pointer in 0N/A // a global variable "__libc_stack_end", which is then used by system 0N/A // libraries. __libc_stack_end should be pretty close to stack top. The 0N/A // variable is available since the very early days. However, because it is 0N/A // a private interface, it could disappear in the future. 0N/A // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar 0N/A // to __libc_stack_end, it is very close to stack top, but isn't the real 0N/A // stack top. Note that /proc may not exist if VM is running as a chroot 0N/A // program, so reading /proc/<pid>/stat could fail. Also the contents of 0N/A // /proc/<pid>/stat could change in the future (though unlikely). 0N/A // We try __libc_stack_end first. If that doesn't work, look for 0N/A // /proc/<pid>/stat. If neither of them works, we use current stack pointer 0N/A // as a hint, which should work well in most cases. 0N/A // try __libc_stack_end first 0N/A // Figure what the primordial thread stack base is. Code is inspired 0N/A // followed by command name surrounded by parentheses, state, etc. 0N/A // Skip pid and the command string. Note that we could be dealing with 0N/A // weird command names, e.g. user could decide to rename java launcher 0N/A // to "java 1.4.2 :)", then the stat file would look like 0N/A // 1234 (java 1.4.2 :)) R ... ... 0N/A // We don't really need to know the command string, just find the last 0N/A // occurrence of ")" and then start parsing from there. See bug 4726580. 0N/A /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */ 0N/A /* 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 */ 513N/A i =
sscanf(s,
"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " 0N/A // product mode - assume we are the initial thread, good luck in the 0N/A warning(
"Can't detect initial thread stack location - bad conversion");
0N/A // FreeBSD with a Linux emulator, or inside chroot), this should work for 0N/A // most cases, so don't abort: 0N/A // Now we have a pointer (stack_start) very close to the stack top, the 0N/A // next thing to do is to figure out the exact location of stack top. We 0N/A // can find out the virtual memory area that contains stack_start by 0N/A // and its upper limit is the real stack top. (again, this would fail if 0N/A // running inside chroot, because /proc may not exist.) 0N/A // success, "high" is the true stack top. (ignore "low", because initial 0N/A // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.) 0N/A warning(
"Can't detect initial thread stack location - find_vma failed");
0N/A // best effort: stack_start is normally within a few pages below the real 0N/A // stack top, use it as stack top, and reduce stack size so we won't put 0N/A // guard page outside stack. 0N/A // stack_top could be partially down the page so align it 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// Time since start-up in seconds to a fine granularity. 0N/A// Used by VMSelfDestructTimer and the MemProfiler. 0N/A return (
1000 *
1000);
342N/A// For now, we say that linux does not support vtime. I have no idea 342N/A// whether it can actually be made to (DLD, 9/13/05). 342N/A // better than nothing, but not much 0N/A // we do dlopen's in this particular order due to bug in linux 0N/A // dynamical loader (see 6348968) leading to crash on exit 0N/A // See if monotonic clock is supported by the kernel. Note that some 0N/A // early implementations simply return kernel jiffies (updated every 0N/A // 1/100 or 1/1000 second). It would be bad to use such a low res clock 0N/A // for nano time (though the monotonic property is still nice to have). 0N/A // It's fixed in newer kernels, however clock_getres() still returns 0N/A // 1/HZ. We check if clock_getres() works, but will ignore its reported 0N/A // resolution for now. Hopefully as people move to new kernels, this 0N/A // won't be a problem. 0N/A // yes, monotonic clock is supported 0N/A // close librt if there is no monotonic clock 0N/A // Switch to using fast clocks for thread cpu time if 0N/A // the sys_clock_getres() returns 0 error code. 0N/A // Note, that some kernels may support the current thread 0N/A // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks 0N/A // returned by the pthread_getcpuclockid(). 0N/A // If the fast Posix clocks are supported then the sys_clock_getres() 0N/A // must return at least tp.tv_sec == 0 which means a resolution 0N/A // better than 1 sec. This is extra check for reliability. 0N/A // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past 0N/A // gettimeofday - based on time in seconds since the Epoch thus does not wrap 0N/A // gettimeofday is a real time clock so it skips 0N/A// Return the real, user, and system times in seconds from an 0N/A// arbitrary fixed point in the past. 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// runtime exit support 0N/A// Note: os::shutdown() might be called very early during initialization, or 0N/A// called from signal handler. Before adding something to os::shutdown(), make 0N/A// sure it is async-safe and can handle partially initialized VM. 0N/A // allow PerfMemory to attempt cleanup of any persistent resources 0N/A // needs to remove object in file system 0N/A // flush buffered output, finish log files 0N/A // Check for abort hook 0N/A// Note: os::abort() might be called very early during initialization, or 0N/A// called from signal handler. Before adding something to os::abort(), make 0N/A// sure it is async-safe and can handle partially initialized VM. 0N/A// Die immediately, no exit hook, no abort hook, no cleanup. 0N/A // _exit() on LinuxThreads only kills current thread 0N/A// unused on linux for now. 0N/A // Under the old linux thread library, linux gives each thread 0N/A // its own process id. Because of this each thread will return 0N/A // a different pid if this method were to return the result 0N/A // of getpid(2). Linux provides no api that returns the pid 0N/A // of the launcher thread for the vm. This implementation 0N/A // returns a unique pid, the pid of the launcher thread 0N/A // that starts the vm 'process'. 0N/A // Under the NPTL, getpid() returns the same pid as the 0N/A // launcher thread rather than a unique pid per thread. 0N/A // Use gettid() if you want the old pre NPTL behaviour. 0N/A // if you are looking for the result of a call to getpid() that 0N/A // returns a unique pid for the calling thread, then look at the 691N/A // Quietly truncate on buffer overflow. Should be an error. 691N/A for (
int i = 0 ; i < n ; i++) {
691N/A // Really shouldn't be NULL, but check can't hurt 691N/A continue;
// skip the empty path values 691N/A for (
int i = 0 ; i < n ; i++) {
0N/A// check if addr is inside libjvm[_g].so 0N/A // iterate through all loadable segments 0N/A // base address of a library is the lowest address of its loaded 0N/A // see if 'addr' is within current segment 0N/A // dlpi_name is NULL or empty if the ELF file is executable, return 0 0N/A // so dll_address_to_library_name() can fall through to use dladdr() which 0N/A // can figure out executable name from argv[0]. 0N/A // There is a bug in old glibc dladdr() implementation that it could resolve 0N/A // to wrong library name if the .so file has a base address != NULL. Here 0N/A // we iterate through the program headers of all loaded libraries to find 0N/A // out which library 'addr' really belongs to. This workaround can be 0N/A // removed once the minimum requirement for glibc is moved to 2.3.x. 0N/A // buf already contains library name 0N/A // Loads .dll/.so and 0N/A // in case of error it checks if .dll/.so was built for the 0N/A // same architecture as Hotspot is running on 0N/A // Successful loading 0N/A // Read system error message into ebuf 0N/A // It may or may not be overwritten below 0N/A // No more space in ebuf for additional diagnostics message 0N/A // Can't open library, report dlerror() message 0N/A // file i/o error - report dlerror() msg 0N/A char*
name;
// String representation 0N/A // Identify compatability class for VM's architecture and library's architecture 0N/A // Obtain string descriptions for architectures 0N/A "Didn't find running architecture code (running_arch_code) in arch_array");
0N/A // Even though running architecture detection failed 0N/A // we may still continue with reporting dlerror() message 0N/A " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
0N/A " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
242N/A * glibc-2.0 libdl is not MT safe. If you are building with any glibc, 242N/A * chances are you might want to run the generated bits against glibc-2.0 242N/A * libdl.so, so always use locking for any version of glibc. 0N/A st->
print(
"Can not get library information for pid = %d\n",
pid);
0N/A // Try to identify popular distros. 0N/A // so the order is important. 0N/A // Print warning if unsafe chroot environment detected 0N/A // values in struct sysinfo are "unsigned long" 0N/A// but they're the same for all the linux arch that we support 0N/A// and they're the same for solaris but there's no common place to put this. 0N/Aconst char *
ill_names[] = {
"ILL0",
"ILL_ILLOPC",
"ILL_ILLOPN",
"ILL_ILLADR",
0N/A "ILL_ILLTRP",
"ILL_PRVOPC",
"ILL_PRVREG",
0N/A "ILL_COPROC",
"ILL_BADSTK" };
0N/Aconst char *
fpe_names[] = {
"FPE0",
"FPE_INTDIV",
"FPE_INTOVF",
"FPE_FLTDIV",
0N/A "FPE_FLTOVF",
"FPE_FLTUND",
"FPE_FLTRES",
0N/A "FPE_FLTINV",
"FPE_FLTSUB",
"FPE_FLTDEN" };
0N/Aconst char *
segv_names[] = {
"SEGV0",
"SEGV_MAPERR",
"SEGV_ACCERR" };
0N/Aconst char *
bus_names[] = {
"BUS0",
"BUS_ADRALN",
"BUS_ADRERR",
"BUS_OBJERR" };
0N/A st->
print(
"\n\nError accessing class data sharing archive." \
0N/A " Mapped file inaccessible during execution, " \
0N/A assert(
false,
"must use a large-enough buffer");
0N/A // Lazy resolve the path to current module. 0N/A // Support for the gamma launcher. Typical value for buf is 0N/A // the right place in the string, then assume we are installed in a JDK and 0N/A // we're done. Otherwise, check for a JAVA_HOME environment variable and fix 0N/A // up the path so it looks like libjvm.so is installed there (append a 0N/A for (--p; p >
buf && *p !=
'/'; --p)
0N/A // Look for JAVA_HOME in the environment. 0N/A // Use current module name "libjvm[_g].so" instead of 0N/A // "libjvm"debug_only("_g")".so" since for fastdebug version 0N/A // we should have "libjvm.so" but debug_only("_g") adds "_g"! 0N/A // It is used when we are choosing the HPI library's name 0N/A // "libhpi[_g].so" in hpi::initialize_get_interface(). 0N/A // Go back to path of .so 0N/A // no prefix required, not even "_" 0N/A // no suffix required 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// sun.misc.Signal support 0N/A // 4511530 - sem_post is serialized and handled by the manager thread. When 0N/A // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We 0N/A // don't want to flood the manager thread with sem_post requests. 0N/A // Ctrl-C is pressed during error reporting, likely because the error 0N/A // handler fails to abort. Let VM die immediately. 0N/A // -1 means registration failed 0N/A * The following code is moved from os.cpp for making this 0N/A * code platform specific, which it is by its very nature. 0N/A// Will be modified when max signal is changed to be dynamic 0N/A// a counter for each possible signal value 0N/A// Linux(POSIX) specific hand shaking semaphore. 0N/A // Initialize signal structures 0N/A // Initialize signal semaphore 0N/A for (
int i = 0; i <
NSIG +
1; i++) {
0N/A // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 0N/A // were we externally suspended while we were waiting? 0N/A // The semaphore has been incremented, but while we were waiting 0N/A // another thread suspended us. We don't want to continue running 0N/A // while suspended because that would surprise the thread that 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A // Seems redundant as all get out 0N/A// Solaris allocates memory by pages. 0N/A// Rationale behind this function: 0N/A// current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable 0N/A// mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get 0N/A// samples for JITted code. Here we create private executable mapping over the code cache 0N/A// and then we can use standard (well, almost, as mapping can change) way to provide 0N/A// info for the reporting script by storing timestamp and location of symbol 0N/A// NOTE: Linux kernel does not really reserve the pages for us. 0N/A// All it does is to check if there are enough free pages 0N/A// left at the time of mmap(). This could be a potential 141N/A // sched_getcpu() should be in libc. 141N/A // Create a cpu -> node mapping 141N/A// rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id. 141N/A// The table is later used in get_node_by_cpu(). 462N/A const size_t NCPUS =
32768;
// Since the buffer size computation is very obscure 462N/A // in libnuma (possible values are starting from 16, 462N/A // and continuing up with every other power of 2, but less 462N/A // than the maximum number of CPUs supported by kernel), and 462N/A // is a subject to change (in libnuma version 2 the requirements 462N/A // are more reasonable) we'll just hardcode the number they use 0N/A// If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory 0N/A// at 'requested_addr'. If there are existing memory mappings at the same 0N/A// location, however, they will be overwritten. If 'fixed' is false, 0N/A// 'requested_addr' is only treated as a hint, the return value may or 0N/A// may not start from the requested address. Unlike Linux mmap(), this 0N/A// function returns NULL to indicate failure. 656N/A // Map uncommitted pages PROT_READ and PROT_WRITE, change access 656N/A // to PROT_EXEC if executable when we commit the page. 0N/A // anon_mmap() should only get called during VM initialization, 0N/A // don't need lock (actually we can skip locking even it can be called 0N/A // from multiple threads, because _highest_vm_reserved_address is just a 0N/A // hint about the upper limit of non-stack memory regions.) 0N/A// Don't update _highest_vm_reserved_address, because there might be memory 0N/A// regions above addr + size. If so, releasing a memory region only creates 0N/A// a hole in the address space, it doesn't help prevent heap-stack collision. 0N/A // Linux wants the mprotect address argument to be page aligned. 0N/A // According to SUSv3, mprotect() should only be used with mappings 0N/A // established by mmap(), and mmap() always maps whole pages. Unaligned 0N/A // 'addr' likely indicates problem in the VM (e.g. trying to change 0N/A // protection of malloc'ed or statically allocated memory). Check the 0N/A // caller if you hit this assert. 237N/A// Set protections specified 237N/A // is_committed is unused. 0N/A// Large page support 0N/A // large_page_size on Linux is used to round up heap size. x86 uses either 0N/A // 2M or 4M page, depending on whether PAE (Physical Address Extensions) 0N/A // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use 0N/A // page as large as 256M. 0N/A // Here we try to figure out page size by parsing /proc/meminfo and looking 0N/A // for a line with the following format: 0N/A // Hugepagesize: 2048 kB 0N/A // If we can't determine the value (e.g. /proc is not mounted, or the text 0N/A // format has been changed), we'll use the largest page size supported by 0N/A // skip to next line 0N/A // Large page support is available on 2.6 or newer kernel, some vendors 0N/A // (e.g. Redhat) have backported it to their 2.4 based distributions. 0N/A // We optimistically assume the support is available. If later it turns out 0N/A // not true, VM will automatically switch to use regular page size. 656N/A // "exec" is passed in but not used. Creating the shared image for 656N/A // the code cache doesn't have an SHM_X executable permission to check. 0N/A // Create a large shared memory region to attach to based on size. 0N/A // Currently, size is the total size of the heap 0N/A // Possible reasons for shmget failure: 0N/A // 1. shmmax is too small for Java heap. 0N/A // 2. not enough large page memory. 0N/A // > increase amount of large pages: 0N/A // Note 1: different Linux may use different name for this property, 0N/A // e.g. on Redhat AS-3 it is "hugetlb_pool". 0N/A // Note 2: it's possible there's enough physical memory available but 0N/A // they are so fragmented after a long run that they can't 0N/A // coalesce into large pages. Try to reserve large pages when 0N/A // the system is still "fresh". 0N/A // attach to the region 0N/A // Remove shmid. If shmat() is successful, the actual shared memory segment 0N/A // will be deleted when it's detached by shmdt() or when the process 0N/A // terminates. If shmat() is not successful this will remove the shared 0N/A // segment immediately. 0N/A // detaching the SHM segment will also delete it, see reserve_memory_special() 0N/A// Linux does not support anonymous mmap with large page memory. The only way 0N/A// to reserve large page memory without file backing is through SysV shared 0N/A// memory API. The entire memory region is committed and pinned upfront. 0N/A// Hopefully this will change in the future... 0N/A// Reserve memory at an arbitrary address, only if that area is 0N/A// available (and not reserved for something else). 0N/A // Assert only that the size is a multiple of the page size, since 0N/A // that's all that mmap requires, and since that's all we really know 0N/A // about at this low abstraction level. If we need higher alignment, 0N/A // we can either pass an alignment to this method or verify alignment 0N/A // in one of the methods further up the call chain. See bug 5044738. 0N/A // Repeatedly allocate blocks until the block is allocated at the 0N/A // right spot. Give up after max_tries. Note that reserve_memory() will 0N/A // automatically update _highest_vm_reserved_address if the call is 0N/A // successful. The variable tracks the highest memory address every reserved 0N/A // by JVM. It is used to detect heap-stack collision if running with 0N/A // fixed-stack LinuxThreads. Because here we may attempt to reserve more 0N/A // space than needed, it could confuse the collision detecting code. To 0N/A // solve the problem, save current _highest_vm_reserved_address and 0N/A // calculate the correct value before return. 0N/A // Linux mmap allows caller to pass an address as hint; give it a try first, 0N/A // if kernel honors the hint then we can return immediately. 0N/A // mmap() is successful but it fails to reserve at the requested address 0N/A // Is this the block we wanted? 0N/A // Does this overlap the block we wanted? Give back the overlapped 0N/A // parts and try again. 0N/A // Give back the unused reserved pieces. 0N/A for (
int j = 0; j < i; ++j) {
0N/A// TODO-FIXME: reconcile Solaris' os::sleep with the linux variation. 0N/A// Solaris uses poll(), linux uses park(). 0N/A// Poll() is likely a better choice, assuming that Thread.interrupt() 0N/A// generates a SIGUSRx signal. Note that SIGUSR1 can interfere with 0N/A// SIGSEGV, see 4355769. 0N/A // time moving backwards, should only happen if no monotonic clock 0N/A // not a guarantee() because JVM should not abort on kernel/glibc bugs 0N/A // cleared by handle_special_suspend_equivalent_condition() or 0N/A // java_suspend_self() via check_and_wait_while_suspended() 0N/A // were we externally suspended while we were waiting? 0N/A // It'd be nice to avoid the back-to-back javaTimeNanos() calls on 0N/A // the 1st iteration ... 0N/A // time moving backwards, should only happen if no monotonic clock 0N/A // not a guarantee() because JVM should not abort on kernel/glibc bugs 0N/A // %% make the sleep time an integer flag. for now use 1 millisec. 0N/A// Sleep forever; naked call to OS-specific sleep; use with CAUTION 0N/A while (
true) {
// sleep forever ... 0N/A ::
sleep(
100);
// ... 100 seconds at a time 0N/A// Used to convert frequent JVM_Yield() to nops 0N/A // Yields to all threads, including threads with lower priorities 0N/A // Threads on Linux are all with same priority. The Solaris style 0N/A // os::yield_all() with nanosleep(1ms) is not necessary. 0N/A// Called from the tight loops to possibly influence time-sharing heuristics 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// thread priority support 0N/A// Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER 0N/A// only supports dynamic priority, static priority must be zero. For real-time 0N/A// applications, Linux supports SCHED_RR which allows static priority (1-99). 0N/A// However, for large multi-threaded applications, SCHED_RR is not only slower 0N/A// than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out 0N/A// of 5 runs - Sep 2005). 0N/A// not the entire user process, and user level threads are 1:1 mapped to kernel 0N/A// threads. It has always been the case, but could change in the future. For 0N/A// this reason, the code should not be used as default (ThreadPriorityPolicy=0). 0N/A// It is only used when ThreadPriorityPolicy=1 and requires root privilege. 0N/A 19,
// 0 Entry should never be used 0N/A 0,
// 5 NormPriority 0N/A -
4,
// 9 NearMaxPriority 0N/A -
5 // 10 MaxPriority 0N/A // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1 0N/A // if effective uid is not root. Perhaps, a more elegant way of doing 0N/A // this is to test CAP_SYS_NICE capability, but that will require libcap.so 0N/A warning(
"-XX:ThreadPriorityPolicy requires root privilege on Linux");
0N/A// Hint to the underlying OS that a task switch would not be good. 0N/A// Void return because it's a hint and can fail. 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// old VM-suspension that used to be for java-suspension, safepoints etc, 0N/A// within hotspot. Now there is a single use-case for this: 0N/A// - calling get_thread_pc() on the VMThread by the flat-profiler task 0N/A// that runs in the watcher thread. 0N/A// The remaining code is greatly simplified from the more general suspension 0N/A// code that used to be used. 0N/A// The protocol is quite simple: 0N/A// - sends a signal to the target thread 0N/A// - polls the suspend state of the osthread using a yield loop 0N/A// - target thread signal handler (SR_handler) sets suspend state 0N/A// and blocks in sigsuspend until continued 0N/A// - sets target osthread state to continue 0N/A// - sends signal to end the sigsuspend loop in the SR_handler 0N/A // notify the suspend action is completed, we have now resumed 0N/A// Handler function invoked when a thread's execution is suspended or 0N/A// resumed. We have to be careful that only async-safe functions are 0N/A// called here (Note: most pthread functions are not async safe and 0N/A// should be avoided.) 0N/A// Note: sigwait() is a more natural fit than sigsuspend() from an 0N/A// interface point of view, but sigwait() prevents the signal hander 0N/A// from being run. libpthread would get very confused by not having 0N/A// its signal handlers run and prevents sigwait()'s use with the 0N/A// mutex granting granting signal. 0N/A// Currently only ever called on the VMThread 0N/A // Save and restore errno to avoid confusing native code with EINTR 0N/A // after sigsuspend. 0N/A // read current suspend action 0N/A // Notify the suspend action is about to be completed. do_suspend() 0N/A // waits until SR_SUSPENDED is set and then returns. We will wait 0N/A // here for a resume signal and that completes the suspend-other 0N/A // the same thread - so there are no races 0N/A // notify the caller 0N/A // get current set of blocked signals and unblock resume signal 0N/A // wait here until we are resumed 0N/A // ignore all returns until we get a resume signal 0N/A // nothing special to do - just leave the handler 0N/A if ((s = ::
getenv(
"_JAVA_SR_SIGNUM")) != 0) {
0N/A "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
0N/A // SR_signum is blocked by default. 0N/A // 4528190 - We also need to block pthread restart signal (32 on all 0N/A // supported Linux platforms). Note that LinuxThreads need to block 0N/A // this signal for all threads to work properly. So we don't have 0N/A // to use hard-coded signal number when setting up the mask. 0N/A// returns true on success and false on error - really an error is fatal 0N/A// but this seems the normal response to library errors 0N/A // mark as suspended and send signal 0N/A // check status and wait until notified of suspension 0N/A // check status and wait unit notified of resumption 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A "possibility of dangling Thread pointer");
0N/A // More than one thread can get here with the same value of osthread, 0N/A // resulting in multiple notifications. We do, however, want the store 0N/A // to interrupted() to be visible to other threads before we execute unpark(). 0N/A // For JSR166. Unpark even if interrupt status already was set 0N/A "possibility of dangling Thread pointer");
0N/A // consider thread->_SleepEvent->reset() ... optional optimization 0N/A/////////////////////////////////////////////////////////////////////////////////// 0N/A// This routine may be used by user applications as a "hook" to catch signals. 0N/A// The user-defined signal handler must pass unrecognized signals to this 0N/A// routine, and if it returns true (non-zero), then the signal handler must 0N/A// return immediately. If the flag "abort_if_unrecognized" is true, then this 0N/A// routine will never retun false (zero), but instead will execute a VM panic 0N/A// routine kill the process. 0N/A// If this routine returns false, it is OK to call it again. This allows 0N/A// the user-defined signal handler to perform checks either before or after 0N/A// the VM performs its own checks. Naturally, the user code would be making 0N/A// a serious error if it tried to handle an exception (such as a null check 0N/A// or breakpoint) that the VM was generating for its own correct operation. 0N/A// This routine may recognize any of the following kinds of signals: 0N/A// SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1. 0N/A// It should be consulted by handlers for any of those signals. 0N/A// The caller of this routine must pass in the three arguments supplied 0N/A// to the function referred to in the "sa_sigaction" (not the "sa_handler") 0N/A// field of the structure passed to sigaction(). This routine assumes that 0N/A// the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART. 0N/A// Note that the VM will print warnings if it detects conflicting signal 0N/A// handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers". 0N/A// This boolean allows users to forward their own non-matching signals 0N/A// to JVM_handle_linux_signal, harmlessly. 0N/A// For signal-chaining 0N/A // Retrieve the old signal handler from libjsig 0N/A // Retrieve the preinstalled signal handler from jvm 0N/A // Call the old signal handler 0N/A // It's more reasonable to let jvm treat it as an unexpected exception 0N/A // instead of taking the default action. 0N/A // automaticlly block the signal 0N/A // retrieve the chained handler 0N/A // try to honor the signal mask 0N/A // call into the chained handler 0N/A // restore the signal mask 0N/A // Tell jvm's signal handler the signal is taken care of. 0N/A // Check for overwrite. 0N/A // Do not overwrite; user takes responsibility to forward to us. 0N/A // save the old handler in jvm 0N/A // libjsig also interposes the sigaction() call below and saves the 0N/A // old sigaction on it own. 0N/A fatal2(
"Encountered unexpected pre-existing sigaction handler %#lx for signal %d.", (
long)
oldhand,
sig);
0N/A // Save flags, which are set by ours 0N/A// install signal handlers for signals that HotSpot needs to 0N/A// handle in order to support Java-level exception handling. 0N/A // Tell libjsig jvm is setting signal handlers 0N/A // Tell libjsig jvm finishes setting signal handlers 0N/A // We don't activate signal checker if libjsig is in place, we trust ourselves 0N/A // and if UserSignalHandler is installed all bets are off 0N/A tty->
print_cr(
"Info: libjsig is activated, all active signal checking is disabled");
0N/A tty->
print_cr(
"Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
0N/A// This is the fastest way to get thread cpu time on Linux. 0N/A// Returns cpu time (user+sys) for any thread, not only for current. 0N/A// POSIX compliant clocks are implemented in the kernels 2.6.16+. 0N/A// For reference, please, see IEEE Std 1003.1-2004: 0N/A assert(
rc == 0,
"clock_gettime is expected to return 0 code");
0N/A// glibc on Linux platform uses non-documented flag 0N/A// to indicate, that some special sort of signal 0N/A// trampoline is used. 0N/A// We will never set this flag, and we should 0N/A// ignore this flag in our diagnostic 0N/A // skip directory names 0N/A // See comment for SIGNIFICANT_SIGNAL_MASK define 0N/A // May be, handler was resetted by VMError? 0N/A // Check: is it our handler? 0N/A // It is our signal handler 0N/A // check for flags, reset system-used one! 0N/A// This method is a periodic task to check for misbehaving JNI applications 0N/A// under CheckJNI, we can add any periodic checks here 0N/A // SEGV and BUS if overridden could potentially prevent 0N/A // generation of hs*.log in the event of a crash, debugging 0N/A // such a case can be very challenging, so we absolutely 0N/A // check the following for a good measure: 0N/A // ReduceSignalUsage allows the user to override these handlers 0N/A // only trust the default sigaction, in case it has been interposed 0N/A // No need to check this sig any longer 0N/A // No need to check this sig any longer 0N/A // Dump all the signal 0N/A// this is called _before_ the most of global arguments have been parsed 0N/A char dummy;
/* used to get a guess on initial stack address */ 0N/A// first_hrtime = gethrtime(); 0N/A // With LinuxThreads the JavaMain thread pid (primordial thread) 0N/A // is different than the pid of the java launcher thread. 0N/A // So, on Linux, the launcher thread pid is passed to the VM 0N/A // Use this property instead of getpid() if it was correctly passed. 0N/A // main_thread points to the aboriginal thread 0N/A// To install functions for atexit system call 0N/A// this is called _after_ the global arguments have been parsed 0N/A // Allocate a single page and mark it as readable for safepoint polling 0N/A "Specify at least %dk",
0N/A // Make the stack size a multiple of the page size so that 462N/A // There's only one node(they start from 0), disable NUMA. 0N/A // set the number of file descriptors to max. print out error 0N/A // Initialize lock used to serialize thread creation (see os::create_thread) 0N/A tty->
print_cr(
"There was an error trying to initialize the HPI library.");
0N/A // at-exit methods are called in the reverse order of their registration. 0N/A // atexit functions are called on return from main or as a result of a 0N/A // call to exit(3C). There can be only 32 of these functions registered 0N/A // and atexit() does not set errno. 0N/A // only register atexit functions if PerfAllowAtExitRegistration is set. 0N/A // atexit functions can be delayed until process exit time, which 0N/A // can be problematic for embedded VM situations. Embedded VMs should 0N/A // call DestroyJavaVM() to assure that VM resources are released. 0N/A // note: perfMemory_exit_helper atexit function may be removed in 0N/A // the future if the appropriate cleanup code can be added to the 0N/A // VM_Exit VMOperation's doit method. 0N/A warning(
"os::init2 atexit(perfMemory_exit_helper) failed");
0N/A // initialize thread priority policy 0N/A// Mark the polling page as unreadable 0N/A fatal(
"Could not disable polling page");
0N/A// Mark the polling page as readable 0N/A // Linux doesn't yet have a (official) notion of processor sets, 0N/A // so just return the number of online processors. 0N/A // Not yet implemented. 0N/A // Not yet implemented. 0N/A// Suspends the target using the signal mechanism and then grabs the PC before 0N/A// resuming the target. Used by the flat-profiler only 0N/A // Make sure that it is called by the watcher for the VMThread 0N/A // NULL context is unexpected, double-check this is the VMThread 0N/A // failure means pthread_kill failed for some reason - arguably this is 0N/A // a fatal problem, but such problems are ignored elsewhere 0N/A // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control 0N/A // word back to default 64bit precision if condvar is signaled. Java 0N/A // wants 53bit precision. Save and restore current value. 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A // decode some bytes around the PC 0N/A//////////////////////////////////////////////////////////////////////////////// 0N/A// This does not do anything on Linux. This is basically a hook for being 0N/A// able to use structured exception handling (thread-local exception filters) 0N/A // Prevent process from exiting upon "read error" without consuming all CPU 0N/A// Is a (classpath) directory empty? 0N/A /* Scan the directory */ 0N/A// create binary file, rewriting existing file if required 0N/A// return current position of file pointer 0N/A// move file pointer to the specified offset 0N/A// Map a block of memory. 0N/A// Remap a block of memory. 0N/A // same as map_memory() on this OS 0N/A// Unmap a block of memory. 0N/A // Get thread clockid 0N/A assert(
rc == 0,
"pthread_getcpuclockid is expected to return 0 code");
0N/A// current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool) 0N/A// are used by JVM M&M and JVMTI to get user+sys or user CPU time 0N/A// current_thread_cpu_time() and thread_cpu_time(Thread*) returns 0N/A// the fast estimate available on the platform. 0N/A // return user + sys since the cost is the same 0N/A // consistent with what current_thread_cpu_time() returns 0N/A // We first try accessing /proc/<pid>/cpu since this is faster to 0N/A // process. If this file is not present (linux kernels 2.5 and above) 0N/A // then we open /proc/<pid>/stat. 0N/A // The /proc/<tid>/stat aggregates per-process usage on 0N/A // new Linux kernels 2.6+ where NPTL is supported. 0N/A // and possibly in some other cases, so we check its availability. 0N/A // This is executed only once 0N/A // Skip pid and the command string. Note that we could be dealing with 0N/A // weird command names, e.g. user could decide to rename java launcher 0N/A // to "java 1.4.2 :)", then the stat file would look like 0N/A // 1234 (java 1.4.2 :)) R ... ... 0N/A // We don't really need to know the command string, just find the last 0N/A // occurrence of ")" and then start parsing from there. See bug 4726580. 0N/A// System loadavg support. Returns -1 if load average cannot be obtained. 0N/A// Linux doesn't yet have a (official) notion of processor sets, 0N/A// so just return the system wide load average. 0N/A "Could not open pause file '%s', continuing immediately.\n",
filename);
0N/A * NOTE: the following code is to keep the green threads code 0N/A * in the libjava.so happy. Once the green threads is removed, 0N/A * these code will no longer be needed. 0N/A// Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can 0N/A// hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable. 0N/A// For specifics regarding the bug see GLIBC BUGID 261237 : 0N/A// Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future 0N/A// will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar 0N/A// is used. (The simple C test-case provided in the GLIBC bug report manifests the 0N/A// hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos() 0N/A// and monitorenter when we're using 1-0 locking. All those operations may result in 0N/A// calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version 0N/A// of libpthread avoids the problem, but isn't practical. 0N/A// Possible remedies: 0N/A// 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work. 0N/A// This is palliative and probabilistic, however. If the thread is preempted 0N/A// between the call to compute_abstime() and pthread_cond_timedwait(), more 0N/A// than the minimum period may have passed, and the abstime may be stale (in the 0N/A// past) resultin in a hang. Using this technique reduces the odds of a hang 0N/A// but the JVM is still vulnerable, particularly on heavily loaded systems. 0N/A// 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead 0N/A// of the usual flag-condvar-mutex idiom. The write side of the pipe is set 0N/A// NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo) 0N/A// reduces to poll()+read(). This works well, but consumes 2 FDs per extant 0N/A// 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread 0N/A// that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing 0N/A// a timeout request to the chron thread and then blocking via pthread_cond_wait(). 0N/A// This also works well. In fact it avoids kernel-level scalability impediments 0N/A// on certain platforms that don't handle lots of active pthread_cond_timedwait() 0N/A// timers in a graceful fashion. 0N/A// 4. When the abstime value is in the past it appears that control returns 0N/A// correctly from pthread_cond_timedwait(), but the condvar is left corrupt. 0N/A// can avoid the problem by reinitializing the condvar -- by cond_destroy() 0N/A// followed by cond_init() -- after all calls to pthread_cond_timedwait(). 0N/A// It may be possible to avoid reinitialization by checking the return 0N/A// value from pthread_cond_timedwait(). In addition to reinitializing the 0N/A// condvar we must establish the invariant that cond_signal() is only called 0N/A// within critical sections protected by the adjunct mutex. This prevents 0N/A// cond_signal() from "seeing" a condvar that's in the midst of being 0N/A// reinitialized or that is corrupt. Sadly, this invariant obviates the 0N/A// desirable signal-after-unlock optimization that avoids futile context switching. 0N/A// I'm also concerned that some versions of NTPL might allocate an auxilliary 0N/A// structure when a condvar is used or initialized. cond_destroy() would 0N/A// release the helper structure. Our reinitialize-after-timedwait fix 0N/A// put excessive stress on malloc/free and locks protecting the c-heap. 0N/A// We currently use (4). See the WorkAroundNTPLTimedWaitHang flag. 0N/A// It may be possible to refine (4) by checking the kernel and NTPL verisons 0N/A// and only enabling the work-around for vulnerable environments. 0N/A// utility to compute the abstime argument to timedwait: 0N/A// millis is the relative timeout time 0N/A// abstime will be the absolute timeout time 0N/A// TODO: replace compute_abstime() with unpackTime() 0N/A if (
seconds >
50000000) {
// see man cond_timedwait(3T) 0N/A// Test-and-clear _Event, always leaves _Event set to 0, returns immediately. 0N/A// Conceptually TryPark() should be equivalent to park(0). 0N/A // TODO: assert that _Assoc != NULL or _Assoc == Self 0N/A // Do this the hard way by blocking ... 0N/A // for some reason, under 2.7 lwp_cond_wait() may return ETIME ... 0N/A // Treat this the same as if the wait was interrupted 0N/A // In theory we could move the ST of 0 into _Event past the unlock(), 0N/A // but then we'd need a MEMBAR after the ST. 0N/A // We do this the hard way, by blocking the thread. 0N/A // Consider enforcing a minimum timeout value. 0N/A // Object.wait(timo) will return because of 0N/A // (c) thread.interrupt 0N/A // Thread.interrupt and object.notify{All} both call Event::set. 0N/A // That is, we treat thread.interrupt as a special case of notification. 0N/A // The underlying Solaris implementation, cond_timedwait, admits 0N/A // JVM from making those visible to Java code. As such, we must 0N/A // filter out spurious wakeups. We assume all ETIME returns are valid. 0N/A // TODO: properly differentiate simultaneous notify+interrupt. 0N/A // In that case, we should propagate the notify to another waiter. 0N/A // We consume and ignore EINTR and spurious wakeups. 0N/A // The LD of _Event could have reordered or be satisfied 0N/A // by a read-aside from this processor's write buffer. 0N/A // To avoid problems execute a barrier and then 0N/A // ratify the value. 0N/A // Wait for the thread associated with the event to vacate 0N/A // Note that we signal() _after dropping the lock for "immortal" Events. 0N/A // This is safe and avoids a common class of futile wakeups. In rare 0N/A // circumstances this can cause a thread to return prematurely from 0N/A // cond_{timed}wait() but the spurious wakeup is benign and the victim will 0N/A // simply re-test the condition and re-park itself. 0N/A// ------------------------------------------------------- 0N/A * The solaris and linux implementations of park/unpark are fairly 0N/A * conservative for now, but can be improved. They currently use a 0N/A * Park decrements count if > 0, else does a condvar wait. Unpark 0N/A * sets count to 1 and signals condvar. Only one thread ever waits 0N/A * on the condvar. Contention seen when trying to park implies that someone 0N/A * is unparking you, so don't wait. And spurious returns are fine, so there 0N/A * is no need to track notifications. 0N/A * This code is common to linux and solaris and will be moved to a 0N/A * common place in dolphin. 0N/A * The passed in time value is either a relative time in nanoseconds 0N/A * or an absolute time in milliseconds. Either way it has to be unpacked 0N/A * into suitable seconds and nanoseconds components and stored in the 0N/A * given timespec structure. 0N/A * Given time is a 64-bit value and the time_t used in the timespec is only 0N/A * a signed-32-bit value (except on 64-bit Linux) we have to watch for 0N/A * overflow if times way in the future are given. Further on Solaris versions 0N/A * prior to 10 there is a restriction (see cond_timedwait) that the specified 0N/A * number of seconds, in abstime, is less than current_time + 100,000,000. 0N/A * As it will be 28 years before "now + 100000000" will overflow we can 0N/A * ignore overflow and just impose a hard-limit on seconds using the value 0N/A * of "now + 100,000,000". This places a limit on the timeout of about 3.17 0N/A // Optional fast-path check: 0N/A // Return immediately if a permit is available. 0N/A // Optional optimization -- avoid state transitions if there's an interrupt pending. 0N/A // Check interrupt before trying to wait 0N/A if (
time < 0) {
// don't wait at all 0N/A // Enter safepoint region 0N/A // Beware of deadlocks such as 6317397. 0N/A // The per-thread Parker:: mutex is a classic leaf-lock. 0N/A // In particular a thread must never block on the Threads_lock while 0N/A // holding the Parker:: mutex. If safepoints are pending both the 0N/A // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock. 0N/A // Don't wait if cannot get lock since interference arises from 0N/A // unblocking. Also. check interrupt before trying wait 0N/A // Don't catch signals while blocked; let the running threads have the signals. 0N/A // (This allows a debugger to break into the running thread.) 0N/A // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self() 0N/A // If externally suspended while waiting, re-suspend 0N/A// Run the specified command in a separate process. Return its exit value, 0N/A// or -1 on failure (e.g. can't fork a new process). 0N/A// Unlike system(), this function can be called from signal handler. It 0N/A// doesn't block SIGINT et al. 0N/A // pthread_atfork handlers and reset pthread library. All we need is a 0N/A // separate process to execve. Make a direct syscall to fork process. 0N/A // On IA64 there's no fork syscall, we have to use fork() and hope for 0N/A // execve() in LinuxThreads will call pthread_kill_other_threads_np() 0N/A // first to kill every thread on the thread list. Because this list is 0N/A // not reset by fork() (see notes above), execve() will instead kill 0N/A // every thread in the parent process. We know this is the only thread 0N/A // in the new process, so make a system call directly. 0N/A // IA64 should use normal execve() from glibc to match the glibc fork() 0N/A // care about the actual exit code, for now. 0N/A // Wait for the child process to exit. This returns immediately if 0N/A // the child has already exited. */ 0N/A // The child exited normally; get its exit code. 0N/A // The child exited because of a signal 0N/A // The best value to return is 0x80 + signal number, 0N/A // because that is what all Unix shells do, and because 0N/A // it allows callers to distinguish between process exit and 0N/A // process death by signal. 0N/A // Unknown exit code; pass it through