/src/os/linux/vm/os_linux.cpp

https://bitbucket.org/hamishm/haiku-jdk-hotspot · C++ · 5362 lines · 3346 code · 781 blank · 1235 comment · 937 complexity · f341bdabb9233bf267f12d43c7348b66 MD5 · raw file

  1. /*
  2. * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  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
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. *
  23. */
  24. # define __STDC_FORMAT_MACROS
  25. // no precompiled headers
  26. #include "classfile/classLoader.hpp"
  27. #include "classfile/systemDictionary.hpp"
  28. #include "classfile/vmSymbols.hpp"
  29. #include "code/icBuffer.hpp"
  30. #include "code/vtableStubs.hpp"
  31. #include "compiler/compileBroker.hpp"
  32. #include "interpreter/interpreter.hpp"
  33. #include "jvm_linux.h"
  34. #include "memory/allocation.inline.hpp"
  35. #include "memory/filemap.hpp"
  36. #include "mutex_linux.inline.hpp"
  37. #include "oops/oop.inline.hpp"
  38. #include "os_share_linux.hpp"
  39. #include "prims/jniFastGetField.hpp"
  40. #include "prims/jvm.h"
  41. #include "prims/jvm_misc.hpp"
  42. #include "runtime/arguments.hpp"
  43. #include "runtime/extendedPC.hpp"
  44. #include "runtime/globals.hpp"
  45. #include "runtime/interfaceSupport.hpp"
  46. #include "runtime/java.hpp"
  47. #include "runtime/javaCalls.hpp"
  48. #include "runtime/mutexLocker.hpp"
  49. #include "runtime/objectMonitor.hpp"
  50. #include "runtime/osThread.hpp"
  51. #include "runtime/perfMemory.hpp"
  52. #include "runtime/sharedRuntime.hpp"
  53. #include "runtime/statSampler.hpp"
  54. #include "runtime/stubRoutines.hpp"
  55. #include "runtime/threadCritical.hpp"
  56. #include "runtime/timer.hpp"
  57. #include "services/attachListener.hpp"
  58. #include "services/runtimeService.hpp"
  59. #include "thread_linux.inline.hpp"
  60. #include "utilities/decoder.hpp"
  61. #include "utilities/defaultStream.hpp"
  62. #include "utilities/events.hpp"
  63. #include "utilities/growableArray.hpp"
  64. #include "utilities/vmError.hpp"
  65. #ifdef TARGET_ARCH_x86
  66. # include "assembler_x86.inline.hpp"
  67. # include "nativeInst_x86.hpp"
  68. #endif
  69. #ifdef TARGET_ARCH_sparc
  70. # include "assembler_sparc.inline.hpp"
  71. # include "nativeInst_sparc.hpp"
  72. #endif
  73. #ifdef TARGET_ARCH_zero
  74. # include "assembler_zero.inline.hpp"
  75. # include "nativeInst_zero.hpp"
  76. #endif
  77. #ifdef TARGET_ARCH_arm
  78. # include "assembler_arm.inline.hpp"
  79. # include "nativeInst_arm.hpp"
  80. #endif
  81. #ifdef TARGET_ARCH_ppc
  82. # include "assembler_ppc.inline.hpp"
  83. # include "nativeInst_ppc.hpp"
  84. #endif
  85. #ifdef COMPILER1
  86. #include "c1/c1_Runtime1.hpp"
  87. #endif
  88. #ifdef COMPILER2
  89. #include "opto/runtime.hpp"
  90. #endif
  91. // put OS-includes here
  92. # include <sys/types.h>
  93. # include <sys/mman.h>
  94. # include <sys/stat.h>
  95. # include <sys/select.h>
  96. # include <pthread.h>
  97. # include <signal.h>
  98. # include <errno.h>
  99. # include <dlfcn.h>
  100. # include <stdio.h>
  101. # include <unistd.h>
  102. # include <sys/resource.h>
  103. # include <pthread.h>
  104. # include <sys/stat.h>
  105. # include <sys/time.h>
  106. # include <sys/times.h>
  107. # include <sys/utsname.h>
  108. # include <sys/socket.h>
  109. # include <sys/wait.h>
  110. # include <pwd.h>
  111. # include <poll.h>
  112. # include <semaphore.h>
  113. # include <fcntl.h>
  114. # include <string.h>
  115. # include <syscall.h>
  116. # include <sys/sysinfo.h>
  117. # include <gnu/libc-version.h>
  118. # include <sys/ipc.h>
  119. # include <sys/shm.h>
  120. # include <link.h>
  121. # include <stdint.h>
  122. # include <inttypes.h>
  123. # include <sys/ioctl.h>
  124. #define MAX_PATH (2 * K)
  125. // for timer info max values which include all bits
  126. #define ALL_64_BITS CONST64(0xFFFFFFFFFFFFFFFF)
  127. #define SEC_IN_NANOSECS 1000000000LL
  128. #define LARGEPAGES_BIT (1 << 6)
  129. ////////////////////////////////////////////////////////////////////////////////
  130. // global variables
  131. julong os::Linux::_physical_memory = 0;
  132. address os::Linux::_initial_thread_stack_bottom = NULL;
  133. uintptr_t os::Linux::_initial_thread_stack_size = 0;
  134. int (*os::Linux::_clock_gettime)(clockid_t, struct timespec *) = NULL;
  135. int (*os::Linux::_pthread_getcpuclockid)(pthread_t, clockid_t *) = NULL;
  136. Mutex* os::Linux::_createThread_lock = NULL;
  137. pthread_t os::Linux::_main_thread;
  138. int os::Linux::_page_size = -1;
  139. bool os::Linux::_is_floating_stack = false;
  140. bool os::Linux::_is_NPTL = false;
  141. bool os::Linux::_supports_fast_thread_cpu_time = false;
  142. const char * os::Linux::_glibc_version = NULL;
  143. const char * os::Linux::_libpthread_version = NULL;
  144. static jlong initial_time_count=0;
  145. static int clock_tics_per_sec = 100;
  146. // For diagnostics to print a message once. see run_periodic_checks
  147. static sigset_t check_signal_done;
  148. static bool check_signals = true;;
  149. static pid_t _initial_pid = 0;
  150. /* Signal number used to suspend/resume a thread */
  151. /* do not use any signal number less than SIGSEGV, see 4355769 */
  152. static int SR_signum = SIGUSR2;
  153. sigset_t SR_sigset;
  154. /* Used to protect dlsym() calls */
  155. static pthread_mutex_t dl_mutex;
  156. ////////////////////////////////////////////////////////////////////////////////
  157. // utility functions
  158. static int SR_initialize();
  159. static int SR_finalize();
  160. julong os::available_memory() {
  161. return Linux::available_memory();
  162. }
  163. julong os::Linux::available_memory() {
  164. // values in struct sysinfo are "unsigned long"
  165. struct sysinfo si;
  166. sysinfo(&si);
  167. return (julong)si.freeram * si.mem_unit;
  168. }
  169. julong os::physical_memory() {
  170. return Linux::physical_memory();
  171. }
  172. julong os::allocatable_physical_memory(julong size) {
  173. #ifdef _LP64
  174. return size;
  175. #else
  176. julong result = MIN2(size, (julong)3800*M);
  177. if (!is_allocatable(result)) {
  178. // See comments under solaris for alignment considerations
  179. julong reasonable_size = (julong)2*G - 2 * os::vm_page_size();
  180. result = MIN2(size, reasonable_size);
  181. }
  182. return result;
  183. #endif // _LP64
  184. }
  185. ////////////////////////////////////////////////////////////////////////////////
  186. // environment support
  187. bool os::getenv(const char* name, char* buf, int len) {
  188. const char* val = ::getenv(name);
  189. if (val != NULL && strlen(val) < (size_t)len) {
  190. strcpy(buf, val);
  191. return true;
  192. }
  193. if (len > 0) buf[0] = 0; // return a null string
  194. return false;
  195. }
  196. // Return true if user is running as root.
  197. bool os::have_special_privileges() {
  198. static bool init = false;
  199. static bool privileges = false;
  200. if (!init) {
  201. privileges = (getuid() != geteuid()) || (getgid() != getegid());
  202. init = true;
  203. }
  204. return privileges;
  205. }
  206. #ifndef SYS_gettid
  207. // i386: 224, ia64: 1105, amd64: 186, sparc 143
  208. #ifdef __ia64__
  209. #define SYS_gettid 1105
  210. #elif __i386__
  211. #define SYS_gettid 224
  212. #elif __amd64__
  213. #define SYS_gettid 186
  214. #elif __sparc__
  215. #define SYS_gettid 143
  216. #else
  217. #error define gettid for the arch
  218. #endif
  219. #endif
  220. // Cpu architecture string
  221. #if defined(ZERO)
  222. static char cpu_arch[] = ZERO_LIBARCH;
  223. #elif defined(IA64)
  224. static char cpu_arch[] = "ia64";
  225. #elif defined(IA32)
  226. static char cpu_arch[] = "i386";
  227. #elif defined(AMD64)
  228. static char cpu_arch[] = "amd64";
  229. #elif defined(ARM)
  230. static char cpu_arch[] = "arm";
  231. #elif defined(PPC)
  232. static char cpu_arch[] = "ppc";
  233. #elif defined(SPARC)
  234. # ifdef _LP64
  235. static char cpu_arch[] = "sparcv9";
  236. # else
  237. static char cpu_arch[] = "sparc";
  238. # endif
  239. #else
  240. #error Add appropriate cpu_arch setting
  241. #endif
  242. // pid_t gettid()
  243. //
  244. // Returns the kernel thread id of the currently running thread. Kernel
  245. // thread id is used to access /proc.
  246. //
  247. // (Note that getpid() on LinuxThreads returns kernel thread id too; but
  248. // on NPTL, it returns the same pid for all threads, as required by POSIX.)
  249. //
  250. pid_t os::Linux::gettid() {
  251. int rslt = syscall(SYS_gettid);
  252. if (rslt == -1) {
  253. // old kernel, no NPTL support
  254. return getpid();
  255. } else {
  256. return (pid_t)rslt;
  257. }
  258. }
  259. // Most versions of linux have a bug where the number of processors are
  260. // determined by looking at the /proc file system. In a chroot environment,
  261. // the system call returns 1. This causes the VM to act as if it is
  262. // a single processor and elide locking (see is_MP() call).
  263. static bool unsafe_chroot_detected = false;
  264. static const char *unstable_chroot_error = "/proc file system not found.\n"
  265. "Java may be unstable running multithreaded in a chroot "
  266. "environment on Linux when /proc filesystem is not mounted.";
  267. void os::Linux::initialize_system_info() {
  268. set_processor_count(sysconf(_SC_NPROCESSORS_CONF));
  269. if (processor_count() == 1) {
  270. pid_t pid = os::Linux::gettid();
  271. char fname[32];
  272. jio_snprintf(fname, sizeof(fname), "/proc/%d", pid);
  273. FILE *fp = fopen(fname, "r");
  274. if (fp == NULL) {
  275. unsafe_chroot_detected = true;
  276. } else {
  277. fclose(fp);
  278. }
  279. }
  280. _physical_memory = (julong)sysconf(_SC_PHYS_PAGES) * (julong)sysconf(_SC_PAGESIZE);
  281. assert(processor_count() > 0, "linux error");
  282. }
  283. void os::init_system_properties_values() {
  284. // char arch[12];
  285. // sysinfo(SI_ARCHITECTURE, arch, sizeof(arch));
  286. // The next steps are taken in the product version:
  287. //
  288. // Obtain the JAVA_HOME value from the location of libjvm[_g].so.
  289. // This library should be located at:
  290. // <JAVA_HOME>/jre/lib/<arch>/{client|server}/libjvm[_g].so.
  291. //
  292. // If "/jre/lib/" appears at the right place in the path, then we
  293. // assume libjvm[_g].so is installed in a JDK and we use this path.
  294. //
  295. // Otherwise exit with message: "Could not create the Java virtual machine."
  296. //
  297. // The following extra steps are taken in the debugging version:
  298. //
  299. // If "/jre/lib/" does NOT appear at the right place in the path
  300. // instead of exit check for $JAVA_HOME environment variable.
  301. //
  302. // If it is defined and we are able to locate $JAVA_HOME/jre/lib/<arch>,
  303. // then we append a fake suffix "hotspot/libjvm[_g].so" to this path so
  304. // it looks like libjvm[_g].so is installed there
  305. // <JAVA_HOME>/jre/lib/<arch>/hotspot/libjvm[_g].so.
  306. //
  307. // Otherwise exit.
  308. //
  309. // Important note: if the location of libjvm.so changes this
  310. // code needs to be changed accordingly.
  311. // The next few definitions allow the code to be verbatim:
  312. #define malloc(n) (char*)NEW_C_HEAP_ARRAY(char, (n))
  313. #define getenv(n) ::getenv(n)
  314. /*
  315. * See ld(1):
  316. * The linker uses the following search paths to locate required
  317. * shared libraries:
  318. * 1: ...
  319. * ...
  320. * 7: The default directories, normally /lib and /usr/lib.
  321. */
  322. #if defined(AMD64) || defined(_LP64) && (defined(SPARC) || defined(PPC) || defined(S390))
  323. #define DEFAULT_LIBPATH "/usr/lib64:/lib64:/lib:/usr/lib"
  324. #else
  325. #define DEFAULT_LIBPATH "/lib:/usr/lib"
  326. #endif
  327. #define EXTENSIONS_DIR "/lib/ext"
  328. #define ENDORSED_DIR "/lib/endorsed"
  329. #define REG_DIR "/usr/java/packages"
  330. {
  331. /* sysclasspath, java_home, dll_dir */
  332. {
  333. char *home_path;
  334. char *dll_path;
  335. char *pslash;
  336. char buf[MAXPATHLEN];
  337. os::jvm_path(buf, sizeof(buf));
  338. // Found the full path to libjvm.so.
  339. // Now cut the path to <java_home>/jre if we can.
  340. *(strrchr(buf, '/')) = '\0'; /* get rid of /libjvm.so */
  341. pslash = strrchr(buf, '/');
  342. if (pslash != NULL)
  343. *pslash = '\0'; /* get rid of /{client|server|hotspot} */
  344. dll_path = malloc(strlen(buf) + 1);
  345. if (dll_path == NULL)
  346. return;
  347. strcpy(dll_path, buf);
  348. Arguments::set_dll_dir(dll_path);
  349. if (pslash != NULL) {
  350. pslash = strrchr(buf, '/');
  351. if (pslash != NULL) {
  352. *pslash = '\0'; /* get rid of /<arch> */
  353. pslash = strrchr(buf, '/');
  354. if (pslash != NULL)
  355. *pslash = '\0'; /* get rid of /lib */
  356. }
  357. }
  358. home_path = malloc(strlen(buf) + 1);
  359. if (home_path == NULL)
  360. return;
  361. strcpy(home_path, buf);
  362. Arguments::set_java_home(home_path);
  363. if (!set_boot_path('/', ':'))
  364. return;
  365. }
  366. /*
  367. * Where to look for native libraries
  368. *
  369. * Note: Due to a legacy implementation, most of the library path
  370. * is set in the launcher. This was to accomodate linking restrictions
  371. * on legacy Linux implementations (which are no longer supported).
  372. * Eventually, all the library path setting will be done here.
  373. *
  374. * However, to prevent the proliferation of improperly built native
  375. * libraries, the new path component /usr/java/packages is added here.
  376. * Eventually, all the library path setting will be done here.
  377. */
  378. {
  379. char *ld_library_path;
  380. /*
  381. * Construct the invariant part of ld_library_path. Note that the
  382. * space for the colon and the trailing null are provided by the
  383. * nulls included by the sizeof operator (so actually we allocate
  384. * a byte more than necessary).
  385. */
  386. ld_library_path = (char *) malloc(sizeof(REG_DIR) + sizeof("/lib/") +
  387. strlen(cpu_arch) + sizeof(DEFAULT_LIBPATH));
  388. sprintf(ld_library_path, REG_DIR "/lib/%s:" DEFAULT_LIBPATH, cpu_arch);
  389. /*
  390. * Get the user setting of LD_LIBRARY_PATH, and prepended it. It
  391. * should always exist (until the legacy problem cited above is
  392. * addressed).
  393. */
  394. char *v = getenv("LD_LIBRARY_PATH");
  395. if (v != NULL) {
  396. char *t = ld_library_path;
  397. /* That's +1 for the colon and +1 for the trailing '\0' */
  398. ld_library_path = (char *) malloc(strlen(v) + 1 + strlen(t) + 1);
  399. sprintf(ld_library_path, "%s:%s", v, t);
  400. }
  401. Arguments::set_library_path(ld_library_path);
  402. }
  403. /*
  404. * Extensions directories.
  405. *
  406. * Note that the space for the colon and the trailing null are provided
  407. * by the nulls included by the sizeof operator (so actually one byte more
  408. * than necessary is allocated).
  409. */
  410. {
  411. char *buf = malloc(strlen(Arguments::get_java_home()) +
  412. sizeof(EXTENSIONS_DIR) + sizeof(REG_DIR) + sizeof(EXTENSIONS_DIR));
  413. sprintf(buf, "%s" EXTENSIONS_DIR ":" REG_DIR EXTENSIONS_DIR,
  414. Arguments::get_java_home());
  415. Arguments::set_ext_dirs(buf);
  416. }
  417. /* Endorsed standards default directory. */
  418. {
  419. char * buf;
  420. buf = malloc(strlen(Arguments::get_java_home()) + sizeof(ENDORSED_DIR));
  421. sprintf(buf, "%s" ENDORSED_DIR, Arguments::get_java_home());
  422. Arguments::set_endorsed_dirs(buf);
  423. }
  424. }
  425. #undef malloc
  426. #undef getenv
  427. #undef EXTENSIONS_DIR
  428. #undef ENDORSED_DIR
  429. // Done
  430. return;
  431. }
  432. ////////////////////////////////////////////////////////////////////////////////
  433. // breakpoint support
  434. void os::breakpoint() {
  435. BREAKPOINT;
  436. }
  437. extern "C" void breakpoint() {
  438. // use debugger to set breakpoint here
  439. }
  440. ////////////////////////////////////////////////////////////////////////////////
  441. // signal support
  442. debug_only(static bool signal_sets_initialized = false);
  443. static sigset_t unblocked_sigs, vm_sigs, allowdebug_blocked_sigs;
  444. bool os::Linux::is_sig_ignored(int sig) {
  445. struct sigaction oact;
  446. sigaction(sig, (struct sigaction*)NULL, &oact);
  447. void* ohlr = oact.sa_sigaction ? CAST_FROM_FN_PTR(void*, oact.sa_sigaction)
  448. : CAST_FROM_FN_PTR(void*, oact.sa_handler);
  449. if (ohlr == CAST_FROM_FN_PTR(void*, SIG_IGN))
  450. return true;
  451. else
  452. return false;
  453. }
  454. void os::Linux::signal_sets_init() {
  455. // Should also have an assertion stating we are still single-threaded.
  456. assert(!signal_sets_initialized, "Already initialized");
  457. // Fill in signals that are necessarily unblocked for all threads in
  458. // the VM. Currently, we unblock the following signals:
  459. // SHUTDOWN{1,2,3}_SIGNAL: for shutdown hooks support (unless over-ridden
  460. // by -Xrs (=ReduceSignalUsage));
  461. // BREAK_SIGNAL which is unblocked only by the VM thread and blocked by all
  462. // other threads. The "ReduceSignalUsage" boolean tells us not to alter
  463. // the dispositions or masks wrt these signals.
  464. // Programs embedding the VM that want to use the above signals for their
  465. // own purposes must, at this time, use the "-Xrs" option to prevent
  466. // interference with shutdown hooks and BREAK_SIGNAL thread dumping.
  467. // (See bug 4345157, and other related bugs).
  468. // In reality, though, unblocking these signals is really a nop, since
  469. // these signals are not blocked by default.
  470. sigemptyset(&unblocked_sigs);
  471. sigemptyset(&allowdebug_blocked_sigs);
  472. sigaddset(&unblocked_sigs, SIGILL);
  473. sigaddset(&unblocked_sigs, SIGSEGV);
  474. sigaddset(&unblocked_sigs, SIGBUS);
  475. sigaddset(&unblocked_sigs, SIGFPE);
  476. sigaddset(&unblocked_sigs, SR_signum);
  477. if (!ReduceSignalUsage) {
  478. if (!os::Linux::is_sig_ignored(SHUTDOWN1_SIGNAL)) {
  479. sigaddset(&unblocked_sigs, SHUTDOWN1_SIGNAL);
  480. sigaddset(&allowdebug_blocked_sigs, SHUTDOWN1_SIGNAL);
  481. }
  482. if (!os::Linux::is_sig_ignored(SHUTDOWN2_SIGNAL)) {
  483. sigaddset(&unblocked_sigs, SHUTDOWN2_SIGNAL);
  484. sigaddset(&allowdebug_blocked_sigs, SHUTDOWN2_SIGNAL);
  485. }
  486. if (!os::Linux::is_sig_ignored(SHUTDOWN3_SIGNAL)) {
  487. sigaddset(&unblocked_sigs, SHUTDOWN3_SIGNAL);
  488. sigaddset(&allowdebug_blocked_sigs, SHUTDOWN3_SIGNAL);
  489. }
  490. }
  491. // Fill in signals that are blocked by all but the VM thread.
  492. sigemptyset(&vm_sigs);
  493. if (!ReduceSignalUsage)
  494. sigaddset(&vm_sigs, BREAK_SIGNAL);
  495. debug_only(signal_sets_initialized = true);
  496. }
  497. // These are signals that are unblocked while a thread is running Java.
  498. // (For some reason, they get blocked by default.)
  499. sigset_t* os::Linux::unblocked_signals() {
  500. assert(signal_sets_initialized, "Not initialized");
  501. return &unblocked_sigs;
  502. }
  503. // These are the signals that are blocked while a (non-VM) thread is
  504. // running Java. Only the VM thread handles these signals.
  505. sigset_t* os::Linux::vm_signals() {
  506. assert(signal_sets_initialized, "Not initialized");
  507. return &vm_sigs;
  508. }
  509. // These are signals that are blocked during cond_wait to allow debugger in
  510. sigset_t* os::Linux::allowdebug_blocked_signals() {
  511. assert(signal_sets_initialized, "Not initialized");
  512. return &allowdebug_blocked_sigs;
  513. }
  514. void os::Linux::hotspot_sigmask(Thread* thread) {
  515. //Save caller's signal mask before setting VM signal mask
  516. sigset_t caller_sigmask;
  517. pthread_sigmask(SIG_BLOCK, NULL, &caller_sigmask);
  518. OSThread* osthread = thread->osthread();
  519. osthread->set_caller_sigmask(caller_sigmask);
  520. pthread_sigmask(SIG_UNBLOCK, os::Linux::unblocked_signals(), NULL);
  521. if (!ReduceSignalUsage) {
  522. if (thread->is_VM_thread()) {
  523. // Only the VM thread handles BREAK_SIGNAL ...
  524. pthread_sigmask(SIG_UNBLOCK, vm_signals(), NULL);
  525. } else {
  526. // ... all other threads block BREAK_SIGNAL
  527. pthread_sigmask(SIG_BLOCK, vm_signals(), NULL);
  528. }
  529. }
  530. }
  531. //////////////////////////////////////////////////////////////////////////////
  532. // detecting pthread library
  533. void os::Linux::libpthread_init() {
  534. // Save glibc and pthread version strings. Note that _CS_GNU_LIBC_VERSION
  535. // and _CS_GNU_LIBPTHREAD_VERSION are supported in glibc >= 2.3.2. Use a
  536. // generic name for earlier versions.
  537. // Define macros here so we can build HotSpot on old systems.
  538. # ifndef _CS_GNU_LIBC_VERSION
  539. # define _CS_GNU_LIBC_VERSION 2
  540. # endif
  541. # ifndef _CS_GNU_LIBPTHREAD_VERSION
  542. # define _CS_GNU_LIBPTHREAD_VERSION 3
  543. # endif
  544. size_t n = confstr(_CS_GNU_LIBC_VERSION, NULL, 0);
  545. if (n > 0) {
  546. char *str = (char *)malloc(n);
  547. confstr(_CS_GNU_LIBC_VERSION, str, n);
  548. os::Linux::set_glibc_version(str);
  549. } else {
  550. // _CS_GNU_LIBC_VERSION is not supported, try gnu_get_libc_version()
  551. static char _gnu_libc_version[32];
  552. jio_snprintf(_gnu_libc_version, sizeof(_gnu_libc_version),
  553. "glibc %s %s", gnu_get_libc_version(), gnu_get_libc_release());
  554. os::Linux::set_glibc_version(_gnu_libc_version);
  555. }
  556. n = confstr(_CS_GNU_LIBPTHREAD_VERSION, NULL, 0);
  557. if (n > 0) {
  558. char *str = (char *)malloc(n);
  559. confstr(_CS_GNU_LIBPTHREAD_VERSION, str, n);
  560. // Vanilla RH-9 (glibc 2.3.2) has a bug that confstr() always tells
  561. // us "NPTL-0.29" even we are running with LinuxThreads. Check if this
  562. // is the case. LinuxThreads has a hard limit on max number of threads.
  563. // So sysconf(_SC_THREAD_THREADS_MAX) will return a positive value.
  564. // On the other hand, NPTL does not have such a limit, sysconf()
  565. // will return -1 and errno is not changed. Check if it is really NPTL.
  566. if (strcmp(os::Linux::glibc_version(), "glibc 2.3.2") == 0 &&
  567. strstr(str, "NPTL") &&
  568. sysconf(_SC_THREAD_THREADS_MAX) > 0) {
  569. free(str);
  570. os::Linux::set_libpthread_version("linuxthreads");
  571. } else {
  572. os::Linux::set_libpthread_version(str);
  573. }
  574. } else {
  575. // glibc before 2.3.2 only has LinuxThreads.
  576. os::Linux::set_libpthread_version("linuxthreads");
  577. }
  578. if (strstr(libpthread_version(), "NPTL")) {
  579. os::Linux::set_is_NPTL();
  580. } else {
  581. os::Linux::set_is_LinuxThreads();
  582. }
  583. // LinuxThreads have two flavors: floating-stack mode, which allows variable
  584. // stack size; and fixed-stack mode. NPTL is always floating-stack.
  585. if (os::Linux::is_NPTL() || os::Linux::supports_variable_stack_size()) {
  586. os::Linux::set_is_floating_stack();
  587. }
  588. }
  589. /////////////////////////////////////////////////////////////////////////////
  590. // thread stack
  591. // Force Linux kernel to expand current thread stack. If "bottom" is close
  592. // to the stack guard, caller should block all signals.
  593. //
  594. // MAP_GROWSDOWN:
  595. // A special mmap() flag that is used to implement thread stacks. It tells
  596. // kernel that the memory region should extend downwards when needed. This
  597. // allows early versions of LinuxThreads to only mmap the first few pages
  598. // when creating a new thread. Linux kernel will automatically expand thread
  599. // stack as needed (on page faults).
  600. //
  601. // However, because the memory region of a MAP_GROWSDOWN stack can grow on
  602. // demand, if a page fault happens outside an already mapped MAP_GROWSDOWN
  603. // region, it's hard to tell if the fault is due to a legitimate stack
  604. // access or because of reading/writing non-exist memory (e.g. buffer
  605. // overrun). As a rule, if the fault happens below current stack pointer,
  606. // Linux kernel does not expand stack, instead a SIGSEGV is sent to the
  607. // application (see Linux kernel fault.c).
  608. //
  609. // This Linux feature can cause SIGSEGV when VM bangs thread stack for
  610. // stack overflow detection.
  611. //
  612. // Newer version of LinuxThreads (since glibc-2.2, or, RH-7.x) and NPTL do
  613. // not use this flag. However, the stack of initial thread is not created
  614. // by pthread, it is still MAP_GROWSDOWN. Also it's possible (though
  615. // unlikely) that user code can create a thread with MAP_GROWSDOWN stack
  616. // and then attach the thread to JVM.
  617. //
  618. // To get around the problem and allow stack banging on Linux, we need to
  619. // manually expand thread stack after receiving the SIGSEGV.
  620. //
  621. // There are two ways to expand thread stack to address "bottom", we used
  622. // both of them in JVM before 1.5:
  623. // 1. adjust stack pointer first so that it is below "bottom", and then
  624. // touch "bottom"
  625. // 2. mmap() the page in question
  626. //
  627. // Now alternate signal stack is gone, it's harder to use 2. For instance,
  628. // if current sp is already near the lower end of page 101, and we need to
  629. // call mmap() to map page 100, it is possible that part of the mmap() frame
  630. // will be placed in page 100. When page 100 is mapped, it is zero-filled.
  631. // That will destroy the mmap() frame and cause VM to crash.
  632. //
  633. // The following code works by adjusting sp first, then accessing the "bottom"
  634. // page to force a page fault. Linux kernel will then automatically expand the
  635. // stack mapping.
  636. //
  637. // _expand_stack_to() assumes its frame size is less than page size, which
  638. // should always be true if the function is not inlined.
  639. #if __GNUC__ < 3 // gcc 2.x does not support noinline attribute
  640. #define NOINLINE
  641. #else
  642. #define NOINLINE __attribute__ ((noinline))
  643. #endif
  644. static void _expand_stack_to(address bottom) NOINLINE;
  645. static void _expand_stack_to(address bottom) {
  646. address sp;
  647. size_t size;
  648. volatile char *p;
  649. // Adjust bottom to point to the largest address within the same page, it
  650. // gives us a one-page buffer if alloca() allocates slightly more memory.
  651. bottom = (address)align_size_down((uintptr_t)bottom, os::Linux::page_size());
  652. bottom += os::Linux::page_size() - 1;
  653. // sp might be slightly above current stack pointer; if that's the case, we
  654. // will alloca() a little more space than necessary, which is OK. Don't use
  655. // os::current_stack_pointer(), as its result can be slightly below current
  656. // stack pointer, causing us to not alloca enough to reach "bottom".
  657. sp = (address)&sp;
  658. if (sp > bottom) {
  659. size = sp - bottom;
  660. p = (volatile char *)alloca(size);
  661. assert(p != NULL && p <= (volatile char *)bottom, "alloca problem?");
  662. p[0] = '\0';
  663. }
  664. }
  665. bool os::Linux::manually_expand_stack(JavaThread * t, address addr) {
  666. assert(t!=NULL, "just checking");
  667. assert(t->osthread()->expanding_stack(), "expand should be set");
  668. assert(t->stack_base() != NULL, "stack_base was not initialized");
  669. if (addr < t->stack_base() && addr >= t->stack_yellow_zone_base()) {
  670. sigset_t mask_all, old_sigset;
  671. sigfillset(&mask_all);
  672. pthread_sigmask(SIG_SETMASK, &mask_all, &old_sigset);
  673. _expand_stack_to(addr);
  674. pthread_sigmask(SIG_SETMASK, &old_sigset, NULL);
  675. return true;
  676. }
  677. return false;
  678. }
  679. //////////////////////////////////////////////////////////////////////////////
  680. // create new thread
  681. static address highest_vm_reserved_address();
  682. // check if it's safe to start a new thread
  683. static bool _thread_safety_check(Thread* thread) {
  684. if (os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack()) {
  685. // Fixed stack LinuxThreads (SuSE Linux/x86, and some versions of Redhat)
  686. // Heap is mmap'ed at lower end of memory space. Thread stacks are
  687. // allocated (MAP_FIXED) from high address space. Every thread stack
  688. // occupies a fixed size slot (usually 2Mbytes, but user can change
  689. // it to other values if they rebuild LinuxThreads).
  690. //
  691. // Problem with MAP_FIXED is that mmap() can still succeed even part of
  692. // the memory region has already been mmap'ed. That means if we have too
  693. // many threads and/or very large heap, eventually thread stack will
  694. // collide with heap.
  695. //
  696. // Here we try to prevent heap/stack collision by comparing current
  697. // stack bottom with the highest address that has been mmap'ed by JVM
  698. // plus a safety margin for memory maps created by native code.
  699. //
  700. // This feature can be disabled by setting ThreadSafetyMargin to 0
  701. //
  702. if (ThreadSafetyMargin > 0) {
  703. address stack_bottom = os::current_stack_base() - os::current_stack_size();
  704. // not safe if our stack extends below the safety margin
  705. return stack_bottom - ThreadSafetyMargin >= highest_vm_reserved_address();
  706. } else {
  707. return true;
  708. }
  709. } else {
  710. // Floating stack LinuxThreads or NPTL:
  711. // Unlike fixed stack LinuxThreads, thread stacks are not MAP_FIXED. When
  712. // there's not enough space left, pthread_create() will fail. If we come
  713. // here, that means enough space has been reserved for stack.
  714. return true;
  715. }
  716. }
  717. // Thread start routine for all newly created threads
  718. static void *java_start(Thread *thread) {
  719. // Try to randomize the cache line index of hot stack frames.
  720. // This helps when threads of the same stack traces evict each other's
  721. // cache lines. The threads can be either from the same JVM instance, or
  722. // from different JVM instances. The benefit is especially true for
  723. // processors with hyperthreading technology.
  724. static int counter = 0;
  725. int pid = os::current_process_id();
  726. alloca(((pid ^ counter++) & 7) * 128);
  727. ThreadLocalStorage::set_thread(thread);
  728. OSThread* osthread = thread->osthread();
  729. Monitor* sync = osthread->startThread_lock();
  730. // non floating stack LinuxThreads needs extra check, see above
  731. if (!_thread_safety_check(thread)) {
  732. // notify parent thread
  733. MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
  734. osthread->set_state(ZOMBIE);
  735. sync->notify_all();
  736. return NULL;
  737. }
  738. // thread_id is kernel thread id (similar to Solaris LWP id)
  739. osthread->set_thread_id(os::Linux::gettid());
  740. if (UseNUMA) {
  741. int lgrp_id = os::numa_get_group_id();
  742. if (lgrp_id != -1) {
  743. thread->set_lgrp_id(lgrp_id);
  744. }
  745. }
  746. // initialize signal mask for this thread
  747. os::Linux::hotspot_sigmask(thread);
  748. // initialize floating point control register
  749. os::Linux::init_thread_fpu_state();
  750. // handshaking with parent thread
  751. {
  752. MutexLockerEx ml(sync, Mutex::_no_safepoint_check_flag);
  753. // notify parent thread
  754. osthread->set_state(INITIALIZED);
  755. sync->notify_all();
  756. // wait until os::start_thread()
  757. while (osthread->get_state() == INITIALIZED) {
  758. sync->wait(Mutex::_no_safepoint_check_flag);
  759. }
  760. }
  761. // call one more level start routine
  762. thread->run();
  763. return 0;
  764. }
  765. bool os::create_thread(Thread* thread, ThreadType thr_type, size_t stack_size) {
  766. assert(thread->osthread() == NULL, "caller responsible");
  767. // Allocate the OSThread object
  768. OSThread* osthread = new OSThread(NULL, NULL);
  769. if (osthread == NULL) {
  770. return false;
  771. }
  772. // set the correct thread state
  773. osthread->set_thread_type(thr_type);
  774. // Initial state is ALLOCATED but not INITIALIZED
  775. osthread->set_state(ALLOCATED);
  776. thread->set_osthread(osthread);
  777. // init thread attributes
  778. pthread_attr_t attr;
  779. pthread_attr_init(&attr);
  780. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  781. // stack size
  782. if (os::Linux::supports_variable_stack_size()) {
  783. // calculate stack size if it's not specified by caller
  784. if (stack_size == 0) {
  785. stack_size = os::Linux::default_stack_size(thr_type);
  786. switch (thr_type) {
  787. case os::java_thread:
  788. // Java threads use ThreadStackSize which default value can be
  789. // changed with the flag -Xss
  790. assert (JavaThread::stack_size_at_create() > 0, "this should be set");
  791. stack_size = JavaThread::stack_size_at_create();
  792. break;
  793. case os::compiler_thread:
  794. if (CompilerThreadStackSize > 0) {
  795. stack_size = (size_t)(CompilerThreadStackSize * K);
  796. break;
  797. } // else fall through:
  798. // use VMThreadStackSize if CompilerThreadStackSize is not defined
  799. case os::vm_thread:
  800. case os::pgc_thread:
  801. case os::cgc_thread:
  802. case os::watcher_thread:
  803. if (VMThreadStackSize > 0) stack_size = (size_t)(VMThreadStackSize * K);
  804. break;
  805. }
  806. }
  807. stack_size = MAX2(stack_size, os::Linux::min_stack_allowed);
  808. pthread_attr_setstacksize(&attr, stack_size);
  809. } else {
  810. // let pthread_create() pick the default value.
  811. }
  812. // glibc guard page
  813. pthread_attr_setguardsize(&attr, os::Linux::default_guard_size(thr_type));
  814. ThreadState state;
  815. {
  816. // Serialize thread creation if we are running with fixed stack LinuxThreads
  817. bool lock = os::Linux::is_LinuxThreads() && !os::Linux::is_floating_stack();
  818. if (lock) {
  819. os::Linux::createThread_lock()->lock_without_safepoint_check();
  820. }
  821. pthread_t tid;
  822. int ret = pthread_create(&tid, &attr, (void* (*)(void*)) java_start, thread);
  823. pthread_attr_destroy(&attr);
  824. if (ret != 0) {
  825. if (PrintMiscellaneous && (Verbose || WizardMode)) {
  826. perror("pthread_create()");
  827. }
  828. // Need to clean up stuff we've allocated so far
  829. thread->set_osthread(NULL);
  830. delete osthread;
  831. if (lock) os::Linux::createThread_lock()->unlock();
  832. return false;
  833. }
  834. // Store pthread info into the OSThread
  835. osthread->set_pthread_id(tid);
  836. // Wait until child thread is either initialized or aborted
  837. {
  838. Monitor* sync_with_child = osthread->startThread_lock();
  839. MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
  840. while ((state = osthread->get_state()) == ALLOCATED) {
  841. sync_with_child->wait(Mutex::_no_safepoint_check_flag);
  842. }
  843. }
  844. if (lock) {
  845. os::Linux::createThread_lock()->unlock();
  846. }
  847. }
  848. // Aborted due to thread limit being reached
  849. if (state == ZOMBIE) {
  850. thread->set_osthread(NULL);
  851. delete osthread;
  852. return false;
  853. }
  854. // The thread is returned suspended (in state INITIALIZED),
  855. // and is started higher up in the call chain
  856. assert(state == INITIALIZED, "race condition");
  857. return true;
  858. }
  859. /////////////////////////////////////////////////////////////////////////////
  860. // attach existing thread
  861. // bootstrap the main thread
  862. bool os::create_main_thread(JavaThread* thread) {
  863. assert(os::Linux::_main_thread == pthread_self(), "should be called inside main thread");
  864. return create_attached_thread(thread);
  865. }
  866. bool os::create_attached_thread(JavaThread* thread) {
  867. #ifdef ASSERT
  868. thread->verify_not_published();
  869. #endif
  870. // Allocate the OSThread object
  871. OSThread* osthread = new OSThread(NULL, NULL);
  872. if (osthread == NULL) {
  873. return false;
  874. }
  875. // Store pthread info into the OSThread
  876. osthread->set_thread_id(os::Linux::gettid());
  877. osthread->set_pthread_id(::pthread_self());
  878. // initialize floating point control register
  879. os::Linux::init_thread_fpu_state();
  880. // Initial thread state is RUNNABLE
  881. osthread->set_state(RUNNABLE);
  882. thread->set_osthread(osthread);
  883. if (UseNUMA) {
  884. int lgrp_id = os::numa_get_group_id();
  885. if (lgrp_id != -1) {
  886. thread->set_lgrp_id(lgrp_id);
  887. }
  888. }
  889. if (os::Linux::is_initial_thread()) {
  890. // If current thread is initial thread, its stack is mapped on demand,
  891. // see notes about MAP_GROWSDOWN. Here we try to force kernel to map
  892. // the entire stack region to avoid SEGV in stack banging.
  893. // It is also useful to get around the heap-stack-gap problem on SuSE
  894. // kernel (see 4821821 for details). We first expand stack to the top
  895. // of yellow zone, then enable stack yellow zone (order is significant,
  896. // enabling yellow zone first will crash JVM on SuSE Linux), so there
  897. // is no gap between the last two virtual memory regions.
  898. JavaThread *jt = (JavaThread *)thread;
  899. address addr = jt->stack_yellow_zone_base();
  900. assert(addr != NULL, "initialization problem?");
  901. assert(jt->stack_available(addr) > 0, "stack guard should not be enabled");
  902. osthread->set_expanding_stack();
  903. os::Linux::manually_expand_stack(jt, addr);
  904. osthread->clear_expanding_stack();
  905. }
  906. // initialize signal mask for this thread
  907. // and save the caller's signal mask
  908. os::Linux::hotspot_sigmask(thread);
  909. return true;
  910. }
  911. void os::pd_start_thread(Thread* thread) {
  912. OSThread * osthread = thread->osthread();
  913. assert(osthread->get_state() != INITIALIZED, "just checking");
  914. Monitor* sync_with_child = osthread->startThread_lock();
  915. MutexLockerEx ml(sync_with_child, Mutex::_no_safepoint_check_flag);
  916. sync_with_child->notify();
  917. }
  918. // Free Linux resources related to the OSThread
  919. void os::free_thread(OSThread* osthread) {
  920. assert(osthread != NULL, "osthread not set");
  921. if (Thread::current()->osthread() == osthread) {
  922. // Restore caller's signal mask
  923. sigset_t sigmask = osthread->caller_sigmask();
  924. pthread_sigmask(SIG_SETMASK, &sigmask, NULL);
  925. }
  926. delete osthread;
  927. }
  928. //////////////////////////////////////////////////////////////////////////////
  929. // thread local storage
  930. int os::allocate_thread_local_storage() {
  931. pthread_key_t key;
  932. int rslt = pthread_key_create(&key, NULL);
  933. assert(rslt == 0, "cannot allocate thread local storage");
  934. return (int)key;
  935. }
  936. // Note: This is currently not used by VM, as we don't destroy TLS key
  937. // on VM exit.
  938. void os::free_thread_local_storage(int index) {
  939. int rslt = pthread_key_delete((pthread_key_t)index);
  940. assert(rslt == 0, "invalid index");
  941. }
  942. void os::thread_local_storage_at_put(int index, void* value) {
  943. int rslt = pthread_setspecific((pthread_key_t)index, value);
  944. assert(rslt == 0, "pthread_setspecific failed");
  945. }
  946. extern "C" Thread* get_thread() {
  947. return ThreadLocalStorage::thread();
  948. }
  949. //////////////////////////////////////////////////////////////////////////////
  950. // initial thread
  951. // Check if current thread is the initial thread, similar to Solaris thr_main.
  952. bool os::Linux::is_initial_thread(void) {
  953. char dummy;
  954. // If called before init complete, thread stack bottom will be null.
  955. // Can be called if fatal error occurs before initialization.
  956. if (initial_thread_stack_bottom() == NULL) return false;
  957. assert(initial_thread_stack_bottom() != NULL &&
  958. initial_thread_stack_size() != 0,
  959. "os::init did not locate initial thread's stack region");
  960. if ((address)&dummy >= initial_thread_stack_bottom() &&
  961. (address)&dummy < initial_thread_stack_bottom() + initial_thread_stack_size())
  962. return true;
  963. else return false;
  964. }
  965. // Find the virtual memory area that contains addr
  966. static bool find_vma(address addr, address* vma_low, address* vma_high) {
  967. FILE *fp = fopen("/proc/self/maps", "r");
  968. if (fp) {
  969. address low, high;
  970. while (!feof(fp)) {
  971. if (fscanf(fp, "%p-%p", &low, &high) == 2) {
  972. if (low <= addr && addr < high) {
  973. if (vma_low) *vma_low = low;
  974. if (vma_high) *vma_high = high;
  975. fclose (fp);
  976. return true;
  977. }
  978. }
  979. for (;;) {
  980. int ch = fgetc(fp);
  981. if (ch == EOF || ch == (int)'\n') break;
  982. }
  983. }
  984. fclose(fp);
  985. }
  986. return false;
  987. }
  988. // Locate initial thread stack. This special handling of initial thread stack
  989. // is needed because pthread_getattr_np() on most (all?) Linux distros returns
  990. // bogus value for initial thread.
  991. void os::Linux::capture_initial_stack(size_t max_size) {
  992. // stack size is the easy part, get it from RLIMIT_STACK
  993. size_t stack_size;
  994. struct rlimit rlim;
  995. getrlimit(RLIMIT_STACK, &rlim);
  996. stack_size = rlim.rlim_cur;
  997. // 6308388: a bug in ld.so will relocate its own .data section to the
  998. // lower end of primordial stack; reduce ulimit -s value a little bit
  999. // so we won't install guard page on ld.so's data section.
  1000. stack_size -= 2 * page_size();
  1001. // 4441425: avoid crash with "unlimited" stack size on SuSE 7.1 or Redhat
  1002. // 7.1, in both cases we will get 2G in return value.
  1003. // 4466587: glibc 2.2.x compiled w/o "--enable-kernel=2.4.0" (RH 7.0,
  1004. // SuSE 7.2, Debian) can not handle alternate signal stack correctly
  1005. // for initial thread if its stack size exceeds 6M. Cap it at 2M,
  1006. // in case other parts in glibc still assumes 2M max stack size.
  1007. // FIXME: alt signal stack is gone, maybe we can relax this constraint?
  1008. #ifndef IA64
  1009. if (stack_size > 2 * K * K) stack_size = 2 * K * K;
  1010. #else
  1011. // Problem still exists RH7.2 (IA64 anyway) but 2MB is a little small
  1012. if (stack_size > 4 * K * K) stack_size = 4 * K * K;
  1013. #endif
  1014. // Try to figure out where the stack base (top) is. This is harder.
  1015. //
  1016. // When an application is started, glibc saves the initial stack pointer in
  1017. // a global variable "__libc_stack_end", which is then used by system
  1018. // libraries. __libc_stack_end should be pretty close to stack top. The
  1019. // variable is available since the very early days. However, because it is
  1020. // a private interface, it could disappear in the future.
  1021. //
  1022. // Linux kernel saves start_stack information in /proc/<pid>/stat. Similar
  1023. // to __libc_stack_end, it is very close to stack top, but isn't the real
  1024. // stack top. Note that /proc may not exist if VM is running as a chroot
  1025. // program, so reading /proc/<pid>/stat could fail. Also the contents of
  1026. // /proc/<pid>/stat could change in the future (though unlikely).
  1027. //
  1028. // We try __libc_stack_end first. If that doesn't work, look for
  1029. // /proc/<pid>/stat. If neither of them works, we use current stack pointer
  1030. // as a hint, which should work well in most cases.
  1031. uintptr_t stack_start;
  1032. // try __libc_stack_end first
  1033. uintptr_t *p = (uintptr_t *)dlsym(RTLD_DEFAULT, "__libc_stack_end");
  1034. if (p && *p) {
  1035. stack_start = *p;
  1036. } else {
  1037. // see if we can get the start_stack field from /proc/self/stat
  1038. FILE *fp;
  1039. int pid;
  1040. char state;
  1041. int ppid;
  1042. int pgrp;
  1043. int session;
  1044. int nr;
  1045. int tpgrp;
  1046. unsigned long flags;
  1047. unsigned long minflt;
  1048. unsigned long cminflt;
  1049. unsigned long majflt;
  1050. unsigned long cmajflt;
  1051. unsigned long utime;
  1052. unsigned long stime;
  1053. long cutime;
  1054. long cstime;
  1055. long prio;
  1056. long nice;
  1057. long junk;
  1058. long it_real;
  1059. uintptr_t start;
  1060. uintptr_t vsize;
  1061. intptr_t rss;
  1062. uintptr_t rsslim;
  1063. uintptr_t scodes;
  1064. uintptr_t ecode;
  1065. int i;
  1066. // Figure what the primordial thread stack base is. Code is inspired
  1067. // by email from Hans Boehm. /proc/self/stat begins with current pid,
  1068. // followed by command name surrounded by parentheses, state, etc.
  1069. char stat[2048];
  1070. int statlen;
  1071. fp = fopen("/proc/self/stat", "r");
  1072. if (fp) {
  1073. statlen = fread(stat, 1, 2047, fp);
  1074. stat[statlen] = '\0';
  1075. fclose(fp);
  1076. // Skip pid and the command string. Note that we could be dealing with
  1077. // weird command names, e.g. user could decide to rename java launcher
  1078. // to "java 1.4.2 :)", then the stat file would look like
  1079. // 1234 (java 1.4.2 :)) R ... ...
  1080. // We don't really need to know the command string, just find the last
  1081. // occurrence of ")" and then start parsing from there. See bug 4726580.
  1082. char * s = strrchr(stat, ')');
  1083. i = 0;
  1084. if (s) {
  1085. // Skip blank chars
  1086. do s++; while (isspace(*s));
  1087. #define _UFM UINTX_FORMAT
  1088. #define _DFM INTX_FORMAT
  1089. /* 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 */
  1090. /* 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 */
  1091. i = sscanf(s, "%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu %ld %ld %ld %ld %ld %ld " _UFM _UFM _DFM _UFM _UFM _UFM _UFM,
  1092. &state, /* 3 %c */
  1093. &ppid, /* 4 %d */
  1094. &pgrp, /* 5 %d */
  1095. &session, /* 6 %d */
  1096. &nr, /* 7 %d */
  1097. &tpgrp, /* 8 %d */
  1098. &flags, /* 9 %lu */
  1099. &minflt, /* 10 %lu */
  1100. &cminflt, /* 11 %lu */
  1101. &majflt, /* 12 %lu */
  1102. &cmajflt, /* 13 %lu */
  1103. &utime, /* 14 %lu */
  1104. &stime, /* 15 %lu */
  1105. &cutime, /* 16 %ld */
  1106. &cstime, /* 17 %ld */
  1107. &prio, /* 18 %ld */
  1108. &nice, /* 19 %ld */
  1109. &junk, /* 20 %ld */
  1110. &it_real, /* 21 %ld */
  1111. &start, /* 22 UINTX_FORMAT */
  1112. &vsize, /* 23 UINTX_FORMAT */
  1113. &rss, /* 24 INTX_FORMAT */
  1114. &rsslim, /* 25 UINTX_FORMAT */
  1115. &scodes, /* 26 UINTX_FORMAT */
  1116. &ecode, /* 27 UINTX_FORMAT */
  1117. &stack_start); /* 28 UINTX_FORMAT */
  1118. }
  1119. #undef _UFM
  1120. #undef _DFM
  1121. if (i != 28 - 2) {
  1122. assert(false, "Bad conversion from /proc/self/stat");
  1123. // product mode - assume we are the initial thread, good luck in the
  1124. // embedded case.
  1125. warning("Can't detect initial thread stack location - bad conversion");
  1126. stack_start = (uintptr_t) &rlim;
  1127. }
  1128. } else {
  1129. // For some reason we can't open /proc/self/stat (for example, running on
  1130. // FreeBSD with a Linux emulator, or inside chroot), this should work for
  1131. // most cases, so don't abort:
  1132. warning("Can't detect initial thread stack location - no /proc/self/stat");
  1133. stack_start = (uintptr_t) &rlim;
  1134. }
  1135. }
  1136. // Now we have a pointer (stack_start) very close to the stack top, the
  1137. // next thing to do is to figure out the exact location of stack top. We
  1138. // can find out the virtual memory area that contains stack_start by
  1139. // reading /proc/self/maps, it should be the last vma in /proc/self/maps,
  1140. // and its upper limit is the real stack top. (again, this would fail if
  1141. // running inside chroot, because /proc may not exist.)
  1142. uintptr_t stack_top;
  1143. address low, high;
  1144. if (find_vma((address)stack_start, &low, &high)) {
  1145. // success, "high" is the true stack top. (ignore "low", because initial
  1146. // thread stack grows on demand, its real bottom is high - RLIMIT_STACK.)
  1147. stack_top = (uintptr_t)high;
  1148. } else {
  1149. // failed, likely because /proc/self/maps does not exist
  1150. warning("Can't detect initial thread stack location - find_vma failed");
  1151. // best effort: stack_start is normally within a few pages below the real
  1152. // stack top, use it as stack top, and reduce stack size so we won't put
  1153. // guard page outside stack.
  1154. stack_top = stack_start;
  1155. stack_size -= 16 * page_size();
  1156. }
  1157. // stack_top could be partially down the page so align it
  1158. stack_top = align_size_up(stack_top, page_size());
  1159. if (max_size && stack_size > max_size) {
  1160. _initial_thread_stack_size = max_size;
  1161. } else {
  1162. _initial_thread_stack_size = stack_size;
  1163. }
  1164. _initial_thread_stack_size = align_size_down(_initial_thread_stack_size, page_size());
  1165. _initial_thread_stack_bottom = (address)stack_top - _initial_thread_stack_size;
  1166. }
  1167. ////////////////////////////////////////////////////////////////////////////////
  1168. // time support
  1169. // Time since start-up in seconds to a fine granularity.
  1170. // Used by VMSelfDestructTimer and the MemProfiler.
  1171. double os::elapsedTime() {
  1172. return (double)(os::elapsed_counter()) * 0.000001;
  1173. }
  1174. jlong os::elapsed_counter() {
  1175. timeval time;
  1176. int status = gettimeofday(&time, NULL);
  1177. return jlong(time.tv_sec) * 1000 * 1000 + jlong(time.tv_usec) - initial_time_count;
  1178. }
  1179. jlong os::elapsed_frequency() {
  1180. return (1000 * 1000);
  1181. }
  1182. // For now, we say that linux does not support vtime. I have no idea
  1183. // whether it can actually be made to (DLD, 9/13/05).
  1184. bool os::supports_vtime() { return false; }
  1185. bool os::enable_vtime() { return false; }
  1186. bool os::vtime_enabled() { return false; }
  1187. double os::elapsedVTime() {
  1188. // better than nothing, but not much
  1189. return elapsedTime();
  1190. }
  1191. jlong os::javaTimeMillis() {
  1192. timeval time;
  1193. int status = gettimeofday(&time, NULL);
  1194. assert(status != -1, "linux error");
  1195. return jlong(time.tv_sec) * 1000 + jlong(time.tv_usec / 1000);
  1196. }
  1197. #ifndef CLOCK_MONOTONIC
  1198. #define CLOCK_MONOTONIC (1)
  1199. #endif
  1200. void os::Linux::clock_init() {
  1201. // we do dlopen's in this particular order due to bug in linux
  1202. // dynamical loader (see 6348968) leading to crash on exit
  1203. void* handle = dlopen("librt.so.1", RTLD_LAZY);
  1204. if (handle == NULL) {
  1205. handle = dlopen("librt.so", RTLD_LAZY);
  1206. }
  1207. if (handle) {
  1208. int (*clock_getres_func)(clockid_t, struct timespec*) =
  1209. (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_getres");
  1210. int (*clock_gettime_func)(clockid_t, struct timespec*) =
  1211. (int(*)(clockid_t, struct timespec*))dlsym(handle, "clock_gettime");
  1212. if (clock_getres_func && clock_gettime_func) {
  1213. // See if monotonic clock is supported by the kernel. Note that some
  1214. // early implementations simply return kernel jiffies (updated every
  1215. // 1/100 or 1/1000 second). It would be bad to use such a low res clock
  1216. // for nano time (though the monotonic property is still nice to have).
  1217. // It's fixed in newer kernels, however clock_getres() still returns
  1218. // 1/HZ. We check if clock_getres() works, but will ignore its reported
  1219. // resolution for now. Hopefully as people move to new kernels, this
  1220. // won't be a problem.
  1221. struct timespec res;
  1222. struct timespec tp;
  1223. if (clock_getres_func (CLOCK_MONOTONIC, &res) == 0 &&
  1224. clock_gettime_func(CLOCK_MONOTONIC, &tp) == 0) {
  1225. // yes, monotonic clock is supported
  1226. _clock_gettime = clock_gettime_func;
  1227. } else {
  1228. // close librt if there is no monotonic clock
  1229. dlclose(handle);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. #ifndef SYS_clock_getres
  1235. #if defined(IA32) || defined(AMD64)
  1236. #define SYS_clock_getres IA32_ONLY(266) AMD64_ONLY(229)
  1237. #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
  1238. #else
  1239. #warning "SYS_clock_getres not defined for this platform, disabling fast_thread_cpu_time"
  1240. #define sys_clock_getres(x,y) -1
  1241. #endif
  1242. #else
  1243. #define sys_clock_getres(x,y) ::syscall(SYS_clock_getres, x, y)
  1244. #endif
  1245. void os::Linux::fast_thread_clock_init() {
  1246. if (!UseLinuxPosixThreadCPUClocks) {
  1247. return;
  1248. }
  1249. clockid_t clockid;
  1250. struct timespec tp;
  1251. int (*pthread_getcpuclockid_func)(pthread_t, clockid_t *) =
  1252. (int(*)(pthread_t, clockid_t *)) dlsym(RTLD_DEFAULT, "pthread_getcpuclockid");
  1253. // Switch to using fast clocks for thread cpu time if
  1254. // the sys_clock_getres() returns 0 error code.
  1255. // Note, that some kernels may support the current thread
  1256. // clock (CLOCK_THREAD_CPUTIME_ID) but not the clocks
  1257. // returned by the pthread_getcpuclockid().
  1258. // If the fast Posix clocks are supported then the sys_clock_getres()
  1259. // must return at least tp.tv_sec == 0 which means a resolution
  1260. // better than 1 sec. This is extra check for reliability.
  1261. if(pthread_getcpuclockid_func &&
  1262. pthread_getcpuclockid_func(_main_thread, &clockid) == 0 &&
  1263. sys_clock_getres(clockid, &tp) == 0 && tp.tv_sec == 0) {
  1264. _supports_fast_thread_cpu_time = true;
  1265. _pthread_getcpuclockid = pthread_getcpuclockid_func;
  1266. }
  1267. }
  1268. jlong os::javaTimeNanos() {
  1269. if (Linux::supports_monotonic_clock()) {
  1270. struct timespec tp;
  1271. int status = Linux::clock_gettime(CLOCK_MONOTONIC, &tp);
  1272. assert(status == 0, "gettime error");
  1273. jlong result = jlong(tp.tv_sec) * (1000 * 1000 * 1000) + jlong(tp.tv_nsec);
  1274. return result;
  1275. } else {
  1276. timeval time;
  1277. int status = gettimeofday(&time, NULL);
  1278. assert(status != -1, "linux error");
  1279. jlong usecs = jlong(time.tv_sec) * (1000 * 1000) + jlong(time.tv_usec);
  1280. return 1000 * usecs;
  1281. }
  1282. }
  1283. void os::javaTimeNanos_info(jvmtiTimerInfo *info_ptr) {
  1284. if (Linux::supports_monotonic_clock()) {
  1285. info_ptr->max_value = ALL_64_BITS;
  1286. // CLOCK_MONOTONIC - amount of time since some arbitrary point in the past
  1287. info_ptr->may_skip_backward = false; // not subject to resetting or drifting
  1288. info_ptr->may_skip_forward = false; // not subject to resetting or drifting
  1289. } else {
  1290. // gettimeofday - based on time in seconds since the Epoch thus does not wrap
  1291. info_ptr->max_value = ALL_64_BITS;
  1292. // gettimeofday is a real time clock so it skips
  1293. info_ptr->may_skip_backward = true;
  1294. info_ptr->may_skip_forward = true;
  1295. }
  1296. info_ptr->kind = JVMTI_TIMER_ELAPSED; // elapsed not CPU time
  1297. }
  1298. // Return the real, user, and system times in seconds from an
  1299. // arbitrary fixed point in the past.
  1300. bool os::getTimesSecs(double* process_real_time,
  1301. double* process_user_time,
  1302. double* process_system_time) {
  1303. struct tms ticks;
  1304. clock_t real_ticks = times(&ticks);
  1305. if (real_ticks == (clock_t) (-1)) {
  1306. return false;
  1307. } else {
  1308. double ticks_per_second = (double) clock_tics_per_sec;
  1309. *process_user_time = ((double) ticks.tms_utime) / ticks_per_second;
  1310. *process_system_time = ((double) ticks.tms_stime) / ticks_per_second;
  1311. *process_real_time = ((double) real_ticks) / ticks_per_second;
  1312. return true;
  1313. }
  1314. }
  1315. char * os::local_time_string(char *buf, size_t buflen) {
  1316. struct tm t;
  1317. time_t long_time;
  1318. time(&long_time);
  1319. localtime_r(&long_time, &t);
  1320. jio_snprintf(buf, buflen, "%d-%02d-%02d %02d:%02d:%02d",
  1321. t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
  1322. t.tm_hour, t.tm_min, t.tm_sec);
  1323. return buf;
  1324. }
  1325. struct tm* os::localtime_pd(const time_t* clock, struct tm* res) {
  1326. return localtime_r(clock, res);
  1327. }
  1328. ////////////////////////////////////////////////////////////////////////////////
  1329. // runtime exit support
  1330. // Note: os::shutdown() might be called very early during initialization, or
  1331. // called from signal handler. Before adding something to os::shutdown(), make
  1332. // sure it is async-safe and can handle partially initialized VM.
  1333. void os::shutdown() {
  1334. // allow PerfMemory to attempt cleanup of any persistent resources
  1335. perfMemory_exit();
  1336. // needs to remove object in file system
  1337. AttachListener::abort();
  1338. // flush buffered output, finish log files
  1339. ostream_abort();
  1340. // Check for abort hook
  1341. abort_hook_t abort_hook = Arguments::abort_hook();
  1342. if (abort_hook != NULL) {
  1343. abort_hook();
  1344. }
  1345. }
  1346. // Note: os::abort() might be called very early during initialization, or
  1347. // called from signal handler. Before adding something to os::abort(), make
  1348. // sure it is async-safe and can handle partially initialized VM.
  1349. void os::abort(bool dump_core) {
  1350. os::shutdown();
  1351. if (dump_core) {
  1352. #ifndef PRODUCT
  1353. fdStream out(defaultStream::output_fd());
  1354. out.print_raw("Current thread is ");
  1355. char buf[16];
  1356. jio_snprintf(buf, sizeof(buf), UINTX_FORMAT, os::current_thread_id());
  1357. out.print_raw_cr(buf);
  1358. out.print_raw_cr("Dumping core ...");
  1359. #endif
  1360. ::abort(); // dump core
  1361. }
  1362. ::exit(1);
  1363. }
  1364. // Die immediately, no exit hook, no abort hook, no cleanup.
  1365. void os::die() {
  1366. // _exit() on LinuxThreads only kills current thread
  1367. ::abort();
  1368. }
  1369. // unused on linux for now.
  1370. void os::set_error_file(const char *logfile) {}
  1371. // This method is a copy of JDK's sysGetLastErrorString
  1372. // from src/solaris/hpi/src/system_md.c
  1373. size_t os::lasterror(char *buf, size_t len) {
  1374. if (errno == 0) return 0;
  1375. const char *s = ::strerror(errno);
  1376. size_t n = ::strlen(s);
  1377. if (n >= len) {
  1378. n = len - 1;
  1379. }
  1380. ::strncpy(buf, s, n);
  1381. buf[n] = '\0';
  1382. return n;
  1383. }
  1384. intx os::current_thread_id() { return (intx)pthread_self(); }
  1385. int os::current_process_id() {
  1386. // Under the old linux thread library, linux gives each thread
  1387. // its own process id. Because of this each thread will return
  1388. // a different pid if this method were to return the result
  1389. // of getpid(2). Linux provides no api that returns the pid
  1390. // of the launcher thread for the vm. This implementation
  1391. // returns a unique pid, the pid of the launcher thread
  1392. // that starts the vm 'process'.
  1393. // Under the NPTL, getpid() returns the same pid as the
  1394. // launcher thread rather than a unique pid per thread.
  1395. // Use gettid() if you want the old pre NPTL behaviour.
  1396. // if you are looking for the result of a call to getpid() that
  1397. // returns a unique pid for the calling thread, then look at the
  1398. // OSThread::thread_id() method in osThread_linux.hpp file
  1399. return (int)(_initial_pid ? _initial_pid : getpid());
  1400. }
  1401. // DLL functions
  1402. const char* os::dll_file_extension() { return ".so"; }
  1403. // This must be hard coded because it's the system's temporary
  1404. // directory not the java application's temp directory, ala java.io.tmpdir.
  1405. const char* os::get_temp_directory() { return "/tmp"; }
  1406. static bool file_exists(const char* filename) {
  1407. struct stat statbuf;
  1408. if (filename == NULL || strlen(filename) == 0) {
  1409. return false;
  1410. }
  1411. return os::stat(filename, &statbuf) == 0;
  1412. }
  1413. void os::dll_build_name(char* buffer, size_t buflen,
  1414. const char* pname, const char* fname) {
  1415. // Copied from libhpi
  1416. const size_t pnamelen = pname ? strlen(pname) : 0;
  1417. // Quietly truncate on buffer overflow. Should be an error.
  1418. if (pnamelen + strlen(fname) + 10 > (size_t) buflen) {
  1419. *buffer = '\0';
  1420. return;
  1421. }
  1422. if (pnamelen == 0) {
  1423. snprintf(buffer, buflen, "lib%s.so", fname);
  1424. } else if (strchr(pname, *os::path_separator()) != NULL) {
  1425. int n;
  1426. char** pelements = split_path(pname, &n);
  1427. for (int i = 0 ; i < n ; i++) {
  1428. // Really shouldn't be NULL, but check can't hurt
  1429. if (pelements[i] == NULL || strlen(pelements[i]) == 0) {
  1430. continue; // skip the empty path values
  1431. }
  1432. snprintf(buffer, buflen, "%s/lib%s.so", pelements[i], fname);
  1433. if (file_exists(buffer)) {
  1434. break;
  1435. }
  1436. }
  1437. // release the storage
  1438. for (int i = 0 ; i < n ; i++) {
  1439. if (pelements[i] != NULL) {
  1440. FREE_C_HEAP_ARRAY(char, pelements[i]);
  1441. }
  1442. }
  1443. if (pelements != NULL) {
  1444. FREE_C_HEAP_ARRAY(char*, pelements);
  1445. }
  1446. } else {
  1447. snprintf(buffer, buflen, "%s/lib%s.so", pname, fname);
  1448. }
  1449. }
  1450. const char* os::get_current_directory(char *buf, int buflen) {
  1451. return getcwd(buf, buflen);
  1452. }
  1453. // check if addr is inside libjvm[_g].so
  1454. bool os::address_is_in_vm(address addr) {
  1455. static address libjvm_base_addr;
  1456. Dl_info dlinfo;
  1457. if (libjvm_base_addr == NULL) {
  1458. dladdr(CAST_FROM_FN_PTR(void *, os::address_is_in_vm), &dlinfo);
  1459. libjvm_base_addr = (address)dlinfo.dli_fbase;
  1460. assert(libjvm_base_addr !=NULL, "Cannot obtain base address for libjvm");
  1461. }
  1462. if (dladdr((void *)addr, &dlinfo)) {
  1463. if (libjvm_base_addr == (address)dlinfo.dli_fbase) return true;
  1464. }
  1465. return false;
  1466. }
  1467. bool os::dll_address_to_function_name(address addr, char *buf,
  1468. int buflen, int *offset) {
  1469. Dl_info dlinfo;
  1470. if (dladdr((void*)addr, &dlinfo) && dlinfo.dli_sname != NULL) {
  1471. if (buf != NULL) {
  1472. if(!Decoder::demangle(dlinfo.dli_sname, buf, buflen)) {
  1473. jio_snprintf(buf, buflen, "%s", dlinfo.dli_sname);
  1474. }
  1475. }
  1476. if (offset != NULL) *offset = addr - (address)dlinfo.dli_saddr;
  1477. return true;
  1478. } else if (dlinfo.dli_fname != NULL && dlinfo.dli_fbase != 0) {
  1479. if (Decoder::decode((address)(addr - (address)dlinfo.dli_fbase),
  1480. dlinfo.dli_fname, buf, buflen, offset) == Decoder::no_error) {
  1481. return true;
  1482. }
  1483. }
  1484. if (buf != NULL) buf[0] = '\0';
  1485. if (offset != NULL) *offset = -1;
  1486. return false;
  1487. }
  1488. struct _address_to_library_name {
  1489. address addr; // input : memory address
  1490. size_t buflen; // size of fname
  1491. char* fname; // output: library name
  1492. address base; // library base addr
  1493. };
  1494. static int address_to_library_name_callback(struct dl_phdr_info *info,
  1495. size_t size, void *data) {
  1496. int i;
  1497. bool found = false;
  1498. address libbase = NULL;
  1499. struct _address_to_library_name * d = (struct _address_to_library_name *)data;
  1500. // iterate through all loadable segments
  1501. for (i = 0; i < info->dlpi_phnum; i++) {
  1502. address segbase = (address)(info->dlpi_addr + info->dlpi_phdr[i].p_vaddr);
  1503. if (info->dlpi_phdr[i].p_type == PT_LOAD) {
  1504. // base address of a library is the lowest address of its loaded
  1505. // segments.
  1506. if (libbase == NULL || libbase > segbase) {
  1507. libbase = segbase;
  1508. }
  1509. // see if 'addr' is within current segment
  1510. if (segbase <= d->addr &&
  1511. d->addr < segbase + info->dlpi_phdr[i].p_memsz) {
  1512. found = true;
  1513. }
  1514. }
  1515. }
  1516. // dlpi_name is NULL or empty if the ELF file is executable, return 0
  1517. // so dll_address_to_library_name() can fall through to use dladdr() which
  1518. // can figure out executable name from argv[0].
  1519. if (found && info->dlpi_name && info->dlpi_name[0]) {
  1520. d->base = libbase;
  1521. if (d->fname) {
  1522. jio_snprintf(d->fname, d->buflen, "%s", info->dlpi_name);
  1523. }
  1524. return 1;
  1525. }
  1526. return 0;
  1527. }
  1528. bool os::dll_address_to_library_name(address addr, char* buf,
  1529. int buflen, int* offset) {
  1530. Dl_info dlinfo;
  1531. struct _address_to_library_name data;
  1532. // There is a bug in old glibc dladdr() implementation that it could resolve
  1533. // to wrong library name if the .so file has a base address != NULL. Here
  1534. // we iterate through the program headers of all loaded libraries to find
  1535. // out which library 'addr' really belongs to. This workaround can be
  1536. // removed once the minimum requirement for glibc is moved to 2.3.x.
  1537. data.addr = addr;
  1538. data.fname = buf;
  1539. data.buflen = buflen;
  1540. data.base = NULL;
  1541. int rslt = dl_iterate_phdr(address_to_library_name_callback, (void *)&data);
  1542. if (rslt) {
  1543. // buf already contains library name
  1544. if (offset) *offset = addr - data.base;
  1545. return true;
  1546. } else if (dladdr((void*)addr, &dlinfo)){
  1547. if (buf) jio_snprintf(buf, buflen, "%s", dlinfo.dli_fname);
  1548. if (offset) *offset = addr - (address)dlinfo.dli_fbase;
  1549. return true;
  1550. } else {
  1551. if (buf) buf[0] = '\0';
  1552. if (offset) *offset = -1;
  1553. return false;
  1554. }
  1555. }
  1556. // Loads .dll/.so and
  1557. // in case of error it checks if .dll/.so was built for the
  1558. // same architecture as Hotspot is running on
  1559. void * os::dll_load(const char *filename, char *ebuf, int ebuflen)
  1560. {
  1561. void * result= ::dlopen(filename, RTLD_LAZY);
  1562. if (result != NULL) {
  1563. // Successful loading
  1564. return result;
  1565. }
  1566. Elf32_Ehdr elf_head;
  1567. // Read system error message into ebuf
  1568. // It may or may not be overwritten below
  1569. ::strncpy(ebuf, ::dlerror(), ebuflen-1);
  1570. ebuf[ebuflen-1]='\0';
  1571. int diag_msg_max_length=ebuflen-strlen(ebuf);
  1572. char* diag_msg_buf=ebuf+strlen(ebuf);
  1573. if (diag_msg_max_length==0) {
  1574. // No more space in ebuf for additional diagnostics message
  1575. return NULL;
  1576. }
  1577. int file_descriptor= ::open(filename, O_RDONLY | O_NONBLOCK);
  1578. if (file_descriptor < 0) {
  1579. // Can't open library, report dlerror() message
  1580. return NULL;
  1581. }
  1582. bool failed_to_read_elf_head=
  1583. (sizeof(elf_head)!=
  1584. (::read(file_descriptor, &elf_head,sizeof(elf_head)))) ;
  1585. ::close(file_descriptor);
  1586. if (failed_to_read_elf_head) {
  1587. // file i/o error - report dlerror() msg
  1588. return NULL;
  1589. }
  1590. typedef struct {
  1591. Elf32_Half code; // Actual value as defined in elf.h
  1592. Elf32_Half compat_class; // Compatibility of archs at VM's sense
  1593. char elf_class; // 32 or 64 bit
  1594. char endianess; // MSB or LSB
  1595. char* name; // String representation
  1596. } arch_t;
  1597. #ifndef EM_486
  1598. #define EM_486 6 /* Intel 80486 */
  1599. #endif
  1600. static const arch_t arch_array[]={
  1601. {EM_386, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
  1602. {EM_486, EM_386, ELFCLASS32, ELFDATA2LSB, (char*)"IA 32"},
  1603. {EM_IA_64, EM_IA_64, ELFCLASS64, ELFDATA2LSB, (char*)"IA 64"},
  1604. {EM_X86_64, EM_X86_64, ELFCLASS64, ELFDATA2LSB, (char*)"AMD 64"},
  1605. {EM_SPARC, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
  1606. {EM_SPARC32PLUS, EM_SPARC, ELFCLASS32, ELFDATA2MSB, (char*)"Sparc 32"},
  1607. {EM_SPARCV9, EM_SPARCV9, ELFCLASS64, ELFDATA2MSB, (char*)"Sparc v9 64"},
  1608. {EM_PPC, EM_PPC, ELFCLASS32, ELFDATA2MSB, (char*)"Power PC 32"},
  1609. {EM_PPC64, EM_PPC64, ELFCLASS64, ELFDATA2MSB, (char*)"Power PC 64"},
  1610. {EM_ARM, EM_ARM, ELFCLASS32, ELFDATA2LSB, (char*)"ARM"},
  1611. {EM_S390, EM_S390, ELFCLASSNONE, ELFDATA2MSB, (char*)"IBM System/390"},
  1612. {EM_ALPHA, EM_ALPHA, ELFCLASS64, ELFDATA2LSB, (char*)"Alpha"},
  1613. {EM_MIPS_RS3_LE, EM_MIPS_RS3_LE, ELFCLASS32, ELFDATA2LSB, (char*)"MIPSel"},
  1614. {EM_MIPS, EM_MIPS, ELFCLASS32, ELFDATA2MSB, (char*)"MIPS"},
  1615. {EM_PARISC, EM_PARISC, ELFCLASS32, ELFDATA2MSB, (char*)"PARISC"},
  1616. {EM_68K, EM_68K, ELFCLASS32, ELFDATA2MSB, (char*)"M68k"}
  1617. };
  1618. #if (defined IA32)
  1619. static Elf32_Half running_arch_code=EM_386;
  1620. #elif (defined AMD64)
  1621. static Elf32_Half running_arch_code=EM_X86_64;
  1622. #elif (defined IA64)
  1623. static Elf32_Half running_arch_code=EM_IA_64;
  1624. #elif (defined __sparc) && (defined _LP64)
  1625. static Elf32_Half running_arch_code=EM_SPARCV9;
  1626. #elif (defined __sparc) && (!defined _LP64)
  1627. static Elf32_Half running_arch_code=EM_SPARC;
  1628. #elif (defined __powerpc64__)
  1629. static Elf32_Half running_arch_code=EM_PPC64;
  1630. #elif (defined __powerpc__)
  1631. static Elf32_Half running_arch_code=EM_PPC;
  1632. #elif (defined ARM)
  1633. static Elf32_Half running_arch_code=EM_ARM;
  1634. #elif (defined S390)
  1635. static Elf32_Half running_arch_code=EM_S390;
  1636. #elif (defined ALPHA)
  1637. static Elf32_Half running_arch_code=EM_ALPHA;
  1638. #elif (defined MIPSEL)
  1639. static Elf32_Half running_arch_code=EM_MIPS_RS3_LE;
  1640. #elif (defined PARISC)
  1641. static Elf32_Half running_arch_code=EM_PARISC;
  1642. #elif (defined MIPS)
  1643. static Elf32_Half running_arch_code=EM_MIPS;
  1644. #elif (defined M68K)
  1645. static Elf32_Half running_arch_code=EM_68K;
  1646. #else
  1647. #error Method os::dll_load requires that one of following is defined:\
  1648. IA32, AMD64, IA64, __sparc, __powerpc__, ARM, S390, ALPHA, MIPS, MIPSEL, PARISC, M68K
  1649. #endif
  1650. // Identify compatability class for VM's architecture and library's architecture
  1651. // Obtain string descriptions for architectures
  1652. arch_t lib_arch={elf_head.e_machine,0,elf_head.e_ident[EI_CLASS], elf_head.e_ident[EI_DATA], NULL};
  1653. int running_arch_index=-1;
  1654. for (unsigned int i=0 ; i < ARRAY_SIZE(arch_array) ; i++ ) {
  1655. if (running_arch_code == arch_array[i].code) {
  1656. running_arch_index = i;
  1657. }
  1658. if (lib_arch.code == arch_array[i].code) {
  1659. lib_arch.compat_class = arch_array[i].compat_class;
  1660. lib_arch.name = arch_array[i].name;
  1661. }
  1662. }
  1663. assert(running_arch_index != -1,
  1664. "Didn't find running architecture code (running_arch_code) in arch_array");
  1665. if (running_arch_index == -1) {
  1666. // Even though running architecture detection failed
  1667. // we may still continue with reporting dlerror() message
  1668. return NULL;
  1669. }
  1670. if (lib_arch.endianess != arch_array[running_arch_index].endianess) {
  1671. ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: endianness mismatch)");
  1672. return NULL;
  1673. }
  1674. #ifndef S390
  1675. if (lib_arch.elf_class != arch_array[running_arch_index].elf_class) {
  1676. ::snprintf(diag_msg_buf, diag_msg_max_length-1," (Possible cause: architecture word width mismatch)");
  1677. return NULL;
  1678. }
  1679. #endif // !S390
  1680. if (lib_arch.compat_class != arch_array[running_arch_index].compat_class) {
  1681. if ( lib_arch.name!=NULL ) {
  1682. ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1683. " (Possible cause: can't load %s-bit .so on a %s-bit platform)",
  1684. lib_arch.name, arch_array[running_arch_index].name);
  1685. } else {
  1686. ::snprintf(diag_msg_buf, diag_msg_max_length-1,
  1687. " (Possible cause: can't load this .so (machine code=0x%x) on a %s-bit platform)",
  1688. lib_arch.code,
  1689. arch_array[running_arch_index].name);
  1690. }
  1691. }
  1692. return NULL;
  1693. }
  1694. /*
  1695. * glibc-2.0 libdl is not MT safe. If you are building with any glibc,
  1696. * chances are you might want to run the generated bits against glibc-2.0
  1697. * libdl.so, so always use locking for any version of glibc.
  1698. */
  1699. void* os::dll_lookup(void* handle, const char* name) {
  1700. pthread_mutex_lock(&dl_mutex);
  1701. void* res = dlsym(handle, name);
  1702. pthread_mutex_unlock(&dl_mutex);
  1703. return res;
  1704. }
  1705. static bool _print_ascii_file(const char* filename, outputStream* st) {
  1706. int fd = ::open(filename, O_RDONLY);
  1707. if (fd == -1) {
  1708. return false;
  1709. }
  1710. char buf[32];
  1711. int bytes;
  1712. while ((bytes = ::read(fd, buf, sizeof(buf))) > 0) {
  1713. st->print_raw(buf, bytes);
  1714. }
  1715. ::close(fd);
  1716. return true;
  1717. }
  1718. void os::print_dll_info(outputStream *st) {
  1719. st->print_cr("Dynamic libraries:");
  1720. char fname[32];
  1721. pid_t pid = os::Linux::gettid();
  1722. jio_snprintf(fname, sizeof(fname), "/proc/%d/maps", pid);
  1723. if (!_print_ascii_file(fname, st)) {
  1724. st->print("Can not get library information for pid = %d\n", pid);
  1725. }
  1726. }
  1727. void os::print_os_info(outputStream* st) {
  1728. st->print("OS:");
  1729. // Try to identify popular distros.
  1730. // Most Linux distributions have /etc/XXX-release file, which contains
  1731. // the OS version string. Some have more than one /etc/XXX-release file
  1732. // (e.g. Mandrake has both /etc/mandrake-release and /etc/redhat-release.),
  1733. // so the order is important.
  1734. if (!_print_ascii_file("/etc/mandrake-release", st) &&
  1735. !_print_ascii_file("/etc/sun-release", st) &&
  1736. !_print_ascii_file("/etc/redhat-release", st) &&
  1737. !_print_ascii_file("/etc/SuSE-release", st) &&
  1738. !_print_ascii_file("/etc/turbolinux-release", st) &&
  1739. !_print_ascii_file("/etc/gentoo-release", st) &&
  1740. !_print_ascii_file("/etc/debian_version", st) &&
  1741. !_print_ascii_file("/etc/ltib-release", st) &&
  1742. !_print_ascii_file("/etc/angstrom-version", st)) {
  1743. st->print("Linux");
  1744. }
  1745. st->cr();
  1746. // kernel
  1747. st->print("uname:");
  1748. struct utsname name;
  1749. uname(&name);
  1750. st->print(name.sysname); st->print(" ");
  1751. st->print(name.release); st->print(" ");
  1752. st->print(name.version); st->print(" ");
  1753. st->print(name.machine);
  1754. st->cr();
  1755. // Print warning if unsafe chroot environment detected
  1756. if (unsafe_chroot_detected) {
  1757. st->print("WARNING!! ");
  1758. st->print_cr(unstable_chroot_error);
  1759. }
  1760. // libc, pthread
  1761. st->print("libc:");
  1762. st->print(os::Linux::glibc_version()); st->print(" ");
  1763. st->print(os::Linux::libpthread_version()); st->print(" ");
  1764. if (os::Linux::is_LinuxThreads()) {
  1765. st->print("(%s stack)", os::Linux::is_floating_stack() ? "floating" : "fixed");
  1766. }
  1767. st->cr();
  1768. // rlimit
  1769. st->print("rlimit:");
  1770. struct rlimit rlim;
  1771. st->print(" STACK ");
  1772. getrlimit(RLIMIT_STACK, &rlim);
  1773. if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
  1774. else st->print("%uk", rlim.rlim_cur >> 10);
  1775. st->print(", CORE ");
  1776. getrlimit(RLIMIT_CORE, &rlim);
  1777. if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
  1778. else st->print("%uk", rlim.rlim_cur >> 10);
  1779. st->print(", NPROC ");
  1780. getrlimit(RLIMIT_NPROC, &rlim);
  1781. if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
  1782. else st->print("%d", rlim.rlim_cur);
  1783. st->print(", NOFILE ");
  1784. getrlimit(RLIMIT_NOFILE, &rlim);
  1785. if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
  1786. else st->print("%d", rlim.rlim_cur);
  1787. st->print(", AS ");
  1788. getrlimit(RLIMIT_AS, &rlim);
  1789. if (rlim.rlim_cur == RLIM_INFINITY) st->print("infinity");
  1790. else st->print("%uk", rlim.rlim_cur >> 10);
  1791. st->cr();
  1792. // load average
  1793. st->print("load average:");
  1794. double loadavg[3];
  1795. os::loadavg(loadavg, 3);
  1796. st->print("%0.02f %0.02f %0.02f", loadavg[0], loadavg[1], loadavg[2]);
  1797. st->cr();
  1798. // meminfo
  1799. st->print("\n/proc/meminfo:\n");
  1800. _print_ascii_file("/proc/meminfo", st);
  1801. st->cr();
  1802. }
  1803. void os::print_memory_info(outputStream* st) {
  1804. st->print("Memory:");
  1805. st->print(" %dk page", os::vm_page_size()>>10);
  1806. // values in struct sysinfo are "unsigned long"
  1807. struct sysinfo si;
  1808. sysinfo(&si);
  1809. st->print(", physical " UINT64_FORMAT "k",
  1810. os::physical_memory() >> 10);
  1811. st->print("(" UINT64_FORMAT "k free)",
  1812. os::available_memory() >> 10);
  1813. st->print(", swap " UINT64_FORMAT "k",
  1814. ((jlong)si.totalswap * si.mem_unit) >> 10);
  1815. st->print("(" UINT64_FORMAT "k free)",
  1816. ((jlong)si.freeswap * si.mem_unit) >> 10);
  1817. st->cr();
  1818. }
  1819. // Taken from /usr/include/bits/siginfo.h Supposed to be architecture specific
  1820. // but they're the same for all the linux arch that we support
  1821. // and they're the same for solaris but there's no common place to put this.
  1822. const char *ill_names[] = { "ILL0", "ILL_ILLOPC", "ILL_ILLOPN", "ILL_ILLADR",
  1823. "ILL_ILLTRP", "ILL_PRVOPC", "ILL_PRVREG",
  1824. "ILL_COPROC", "ILL_BADSTK" };
  1825. const char *fpe_names[] = { "FPE0", "FPE_INTDIV", "FPE_INTOVF", "FPE_FLTDIV",
  1826. "FPE_FLTOVF", "FPE_FLTUND", "FPE_FLTRES",
  1827. "FPE_FLTINV", "FPE_FLTSUB", "FPE_FLTDEN" };
  1828. const char *segv_names[] = { "SEGV0", "SEGV_MAPERR", "SEGV_ACCERR" };
  1829. const char *bus_names[] = { "BUS0", "BUS_ADRALN", "BUS_ADRERR", "BUS_OBJERR" };
  1830. void os::print_siginfo(outputStream* st, void* siginfo) {
  1831. st->print("siginfo:");
  1832. const int buflen = 100;
  1833. char buf[buflen];
  1834. siginfo_t *si = (siginfo_t*)siginfo;
  1835. st->print("si_signo=%s: ", os::exception_name(si->si_signo, buf, buflen));
  1836. if (si->si_errno != 0 && strerror_r(si->si_errno, buf, buflen) == 0) {
  1837. st->print("si_errno=%s", buf);
  1838. } else {
  1839. st->print("si_errno=%d", si->si_errno);
  1840. }
  1841. const int c = si->si_code;
  1842. assert(c > 0, "unexpected si_code");
  1843. switch (si->si_signo) {
  1844. case SIGILL:
  1845. st->print(", si_code=%d (%s)", c, c > 8 ? "" : ill_names[c]);
  1846. st->print(", si_addr=" PTR_FORMAT, si->si_addr);
  1847. break;
  1848. case SIGFPE:
  1849. st->print(", si_code=%d (%s)", c, c > 9 ? "" : fpe_names[c]);
  1850. st->print(", si_addr=" PTR_FORMAT, si->si_addr);
  1851. break;
  1852. case SIGSEGV:
  1853. st->print(", si_code=%d (%s)", c, c > 2 ? "" : segv_names[c]);
  1854. st->print(", si_addr=" PTR_FORMAT, si->si_addr);
  1855. break;
  1856. case SIGBUS:
  1857. st->print(", si_code=%d (%s)", c, c > 3 ? "" : bus_names[c]);
  1858. st->print(", si_addr=" PTR_FORMAT, si->si_addr);
  1859. break;
  1860. default:
  1861. st->print(", si_code=%d", si->si_code);
  1862. // no si_addr
  1863. }
  1864. if ((si->si_signo == SIGBUS || si->si_signo == SIGSEGV) &&
  1865. UseSharedSpaces) {
  1866. FileMapInfo* mapinfo = FileMapInfo::current_info();
  1867. if (mapinfo->is_in_shared_space(si->si_addr)) {
  1868. st->print("\n\nError accessing class data sharing archive." \
  1869. " Mapped file inaccessible during execution, " \
  1870. " possible disk/network problem.");
  1871. }
  1872. }
  1873. st->cr();
  1874. }
  1875. static void print_signal_handler(outputStream* st, int sig,
  1876. char* buf, size_t buflen);
  1877. void os::print_signal_handlers(outputStream* st, char* buf, size_t buflen) {
  1878. st->print_cr("Signal Handlers:");
  1879. print_signal_handler(st, SIGSEGV, buf, buflen);
  1880. print_signal_handler(st, SIGBUS , buf, buflen);
  1881. print_signal_handler(st, SIGFPE , buf, buflen);
  1882. print_signal_handler(st, SIGPIPE, buf, buflen);
  1883. print_signal_handler(st, SIGXFSZ, buf, buflen);
  1884. print_signal_handler(st, SIGILL , buf, buflen);
  1885. print_signal_handler(st, INTERRUPT_SIGNAL, buf, buflen);
  1886. print_signal_handler(st, SR_signum, buf, buflen);
  1887. print_signal_handler(st, SHUTDOWN1_SIGNAL, buf, buflen);
  1888. print_signal_handler(st, SHUTDOWN2_SIGNAL , buf, buflen);
  1889. print_signal_handler(st, SHUTDOWN3_SIGNAL , buf, buflen);
  1890. print_signal_handler(st, BREAK_SIGNAL, buf, buflen);
  1891. }
  1892. static char saved_jvm_path[MAXPATHLEN] = {0};
  1893. // Find the full path to the current module, libjvm.so or libjvm_g.so
  1894. void os::jvm_path(char *buf, jint buflen) {
  1895. // Error checking.
  1896. if (buflen < MAXPATHLEN) {
  1897. assert(false, "must use a large-enough buffer");
  1898. buf[0] = '\0';
  1899. return;
  1900. }
  1901. // Lazy resolve the path to current module.
  1902. if (saved_jvm_path[0] != 0) {
  1903. strcpy(buf, saved_jvm_path);
  1904. return;
  1905. }
  1906. char dli_fname[MAXPATHLEN];
  1907. bool ret = dll_address_to_library_name(
  1908. CAST_FROM_FN_PTR(address, os::jvm_path),
  1909. dli_fname, sizeof(dli_fname), NULL);
  1910. assert(ret != 0, "cannot locate libjvm");
  1911. char *rp = realpath(dli_fname, buf);
  1912. if (rp == NULL)
  1913. return;
  1914. if (Arguments::created_by_gamma_launcher()) {
  1915. // Support for the gamma launcher. Typical value for buf is
  1916. // "<JAVA_HOME>/jre/lib/<arch>/<vmtype>/libjvm.so". If "/jre/lib/" appears at
  1917. // the right place in the string, then assume we are installed in a JDK and
  1918. // we're done. Otherwise, check for a JAVA_HOME environment variable and fix
  1919. // up the path so it looks like libjvm.so is installed there (append a
  1920. // fake suffix hotspot/libjvm.so).
  1921. const char *p = buf + strlen(buf) - 1;
  1922. for (int count = 0; p > buf && count < 5; ++count) {
  1923. for (--p; p > buf && *p != '/'; --p)
  1924. /* empty */ ;
  1925. }
  1926. if (strncmp(p, "/jre/lib/", 9) != 0) {
  1927. // Look for JAVA_HOME in the environment.
  1928. char* java_home_var = ::getenv("JAVA_HOME");
  1929. if (java_home_var != NULL && java_home_var[0] != 0) {
  1930. char* jrelib_p;
  1931. int len;
  1932. // Check the current module name "libjvm.so" or "libjvm_g.so".
  1933. p = strrchr(buf, '/');
  1934. assert(strstr(p, "/libjvm") == p, "invalid library name");
  1935. p = strstr(p, "_g") ? "_g" : "";
  1936. rp = realpath(java_home_var, buf);
  1937. if (rp == NULL)
  1938. return;
  1939. // determine if this is a legacy image or modules image
  1940. // modules image doesn't have "jre" subdirectory
  1941. len = strlen(buf);
  1942. jrelib_p = buf + len;
  1943. snprintf(jrelib_p, buflen-len, "/jre/lib/%s", cpu_arch);
  1944. if (0 != access(buf, F_OK)) {
  1945. snprintf(jrelib_p, buflen-len, "/lib/%s", cpu_arch);
  1946. }
  1947. if (0 == access(buf, F_OK)) {
  1948. // Use current module name "libjvm[_g].so" instead of
  1949. // "libjvm"debug_only("_g")".so" since for fastdebug version
  1950. // we should have "libjvm.so" but debug_only("_g") adds "_g"!
  1951. len = strlen(buf);
  1952. snprintf(buf + len, buflen-len, "/hotspot/libjvm%s.so", p);
  1953. } else {
  1954. // Go back to path of .so
  1955. rp = realpath(dli_fname, buf);
  1956. if (rp == NULL)
  1957. return;
  1958. }
  1959. }
  1960. }
  1961. }
  1962. strcpy(saved_jvm_path, buf);
  1963. }
  1964. void os::print_jni_name_prefix_on(outputStream* st, int args_size) {
  1965. // no prefix required, not even "_"
  1966. }
  1967. void os::print_jni_name_suffix_on(outputStream* st, int args_size) {
  1968. // no suffix required
  1969. }
  1970. ////////////////////////////////////////////////////////////////////////////////
  1971. // sun.misc.Signal support
  1972. static volatile jint sigint_count = 0;
  1973. static void
  1974. UserHandler(int sig, void *siginfo, void *context) {
  1975. // 4511530 - sem_post is serialized and handled by the manager thread. When
  1976. // the program is interrupted by Ctrl-C, SIGINT is sent to every thread. We
  1977. // don't want to flood the manager thread with sem_post requests.
  1978. if (sig == SIGINT && Atomic::add(1, &sigint_count) > 1)
  1979. return;
  1980. // Ctrl-C is pressed during error reporting, likely because the error
  1981. // handler fails to abort. Let VM die immediately.
  1982. if (sig == SIGINT && is_error_reported()) {
  1983. os::die();
  1984. }
  1985. os::signal_notify(sig);
  1986. }
  1987. void* os::user_handler() {
  1988. return CAST_FROM_FN_PTR(void*, UserHandler);
  1989. }
  1990. extern "C" {
  1991. typedef void (*sa_handler_t)(int);
  1992. typedef void (*sa_sigaction_t)(int, siginfo_t *, void *);
  1993. }
  1994. void* os::signal(int signal_number, void* handler) {
  1995. struct sigaction sigAct, oldSigAct;
  1996. sigfillset(&(sigAct.sa_mask));
  1997. sigAct.sa_flags = SA_RESTART|SA_SIGINFO;
  1998. sigAct.sa_handler = CAST_TO_FN_PTR(sa_handler_t, handler);
  1999. if (sigaction(signal_number, &sigAct, &oldSigAct)) {
  2000. // -1 means registration failed
  2001. return (void *)-1;
  2002. }
  2003. return CAST_FROM_FN_PTR(void*, oldSigAct.sa_handler);
  2004. }
  2005. void os::signal_raise(int signal_number) {
  2006. ::raise(signal_number);
  2007. }
  2008. /*
  2009. * The following code is moved from os.cpp for making this
  2010. * code platform specific, which it is by its very nature.
  2011. */
  2012. // Will be modified when max signal is changed to be dynamic
  2013. int os::sigexitnum_pd() {
  2014. return NSIG;
  2015. }
  2016. // a counter for each possible signal value
  2017. static volatile jint pending_signals[NSIG+1] = { 0 };
  2018. // Linux(POSIX) specific hand shaking semaphore.
  2019. static sem_t sig_sem;
  2020. void os::signal_init_pd() {
  2021. // Initialize signal structures
  2022. ::memset((void*)pending_signals, 0, sizeof(pending_signals));
  2023. // Initialize signal semaphore
  2024. ::sem_init(&sig_sem, 0, 0);
  2025. }
  2026. void os::signal_notify(int sig) {
  2027. Atomic::inc(&pending_signals[sig]);
  2028. ::sem_post(&sig_sem);
  2029. }
  2030. static int check_pending_signals(bool wait) {
  2031. Atomic::store(0, &sigint_count);
  2032. for (;;) {
  2033. for (int i = 0; i < NSIG + 1; i++) {
  2034. jint n = pending_signals[i];
  2035. if (n > 0 && n == Atomic::cmpxchg(n - 1, &pending_signals[i], n)) {
  2036. return i;
  2037. }
  2038. }
  2039. if (!wait) {
  2040. return -1;
  2041. }
  2042. JavaThread *thread = JavaThread::current();
  2043. ThreadBlockInVM tbivm(thread);
  2044. bool threadIsSuspended;
  2045. do {
  2046. thread->set_suspend_equivalent();
  2047. // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
  2048. ::sem_wait(&sig_sem);
  2049. // were we externally suspended while we were waiting?
  2050. threadIsSuspended = thread->handle_special_suspend_equivalent_condition();
  2051. if (threadIsSuspended) {
  2052. //
  2053. // The semaphore has been incremented, but while we were waiting
  2054. // another thread suspended us. We don't want to continue running
  2055. // while suspended because that would surprise the thread that
  2056. // suspended us.
  2057. //
  2058. ::sem_post(&sig_sem);
  2059. thread->java_suspend_self();
  2060. }
  2061. } while (threadIsSuspended);
  2062. }
  2063. }
  2064. int os::signal_lookup() {
  2065. return check_pending_signals(false);
  2066. }
  2067. int os::signal_wait() {
  2068. return check_pending_signals(true);
  2069. }
  2070. ////////////////////////////////////////////////////////////////////////////////
  2071. // Virtual Memory
  2072. int os::vm_page_size() {
  2073. // Seems redundant as all get out
  2074. assert(os::Linux::page_size() != -1, "must call os::init");
  2075. return os::Linux::page_size();
  2076. }
  2077. // Solaris allocates memory by pages.
  2078. int os::vm_allocation_granularity() {
  2079. assert(os::Linux::page_size() != -1, "must call os::init");
  2080. return os::Linux::page_size();
  2081. }
  2082. // Rationale behind this function:
  2083. // current (Mon Apr 25 20:12:18 MSD 2005) oprofile drops samples without executable
  2084. // mapping for address (see lookup_dcookie() in the kernel module), thus we cannot get
  2085. // samples for JITted code. Here we create private executable mapping over the code cache
  2086. // and then we can use standard (well, almost, as mapping can change) way to provide
  2087. // info for the reporting script by storing timestamp and location of symbol
  2088. void linux_wrap_code(char* base, size_t size) {
  2089. static volatile jint cnt = 0;
  2090. if (!UseOprofile) {
  2091. return;
  2092. }
  2093. char buf[PATH_MAX+1];
  2094. int num = Atomic::add(1, &cnt);
  2095. snprintf(buf, sizeof(buf), "%s/hs-vm-%d-%d",
  2096. os::get_temp_directory(), os::current_process_id(), num);
  2097. unlink(buf);
  2098. int fd = ::open(buf, O_CREAT | O_RDWR, S_IRWXU);
  2099. if (fd != -1) {
  2100. off_t rv = ::lseek(fd, size-2, SEEK_SET);
  2101. if (rv != (off_t)-1) {
  2102. if (::write(fd, "", 1) == 1) {
  2103. mmap(base, size,
  2104. PROT_READ|PROT_WRITE|PROT_EXEC,
  2105. MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE, fd, 0);
  2106. }
  2107. }
  2108. ::close(fd);
  2109. unlink(buf);
  2110. }
  2111. }
  2112. // NOTE: Linux kernel does not really reserve the pages for us.
  2113. // All it does is to check if there are enough free pages
  2114. // left at the time of mmap(). This could be a potential
  2115. // problem.
  2116. bool os::commit_memory(char* addr, size_t size, bool exec) {
  2117. int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
  2118. uintptr_t res = (uintptr_t) ::mmap(addr, size, prot,
  2119. MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0);
  2120. return res != (uintptr_t) MAP_FAILED;
  2121. }
  2122. // Define MAP_HUGETLB here so we can build HotSpot on old systems.
  2123. #ifndef MAP_HUGETLB
  2124. #define MAP_HUGETLB 0x40000
  2125. #endif
  2126. // Define MADV_HUGEPAGE here so we can build HotSpot on old systems.
  2127. #ifndef MADV_HUGEPAGE
  2128. #define MADV_HUGEPAGE 14
  2129. #endif
  2130. bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
  2131. bool exec) {
  2132. if (UseHugeTLBFS && alignment_hint > (size_t)vm_page_size()) {
  2133. int prot = exec ? PROT_READ|PROT_WRITE|PROT_EXEC : PROT_READ|PROT_WRITE;
  2134. uintptr_t res =
  2135. (uintptr_t) ::mmap(addr, size, prot,
  2136. MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS|MAP_HUGETLB,
  2137. -1, 0);
  2138. return res != (uintptr_t) MAP_FAILED;
  2139. }
  2140. return commit_memory(addr, size, exec);
  2141. }
  2142. void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
  2143. if (UseHugeTLBFS && alignment_hint > (size_t)vm_page_size()) {
  2144. // We don't check the return value: madvise(MADV_HUGEPAGE) may not
  2145. // be supported or the memory may already be backed by huge pages.
  2146. ::madvise(addr, bytes, MADV_HUGEPAGE);
  2147. }
  2148. }
  2149. void os::free_memory(char *addr, size_t bytes) {
  2150. ::madvise(addr, bytes, MADV_DONTNEED);
  2151. }
  2152. void os::numa_make_global(char *addr, size_t bytes) {
  2153. Linux::numa_interleave_memory(addr, bytes);
  2154. }
  2155. void os::numa_make_local(char *addr, size_t bytes, int lgrp_hint) {
  2156. Linux::numa_tonode_memory(addr, bytes, lgrp_hint);
  2157. }
  2158. bool os::numa_topology_changed() { return false; }
  2159. size_t os::numa_get_groups_num() {
  2160. int max_node = Linux::numa_max_node();
  2161. return max_node > 0 ? max_node + 1 : 1;
  2162. }
  2163. int os::numa_get_group_id() {
  2164. int cpu_id = Linux::sched_getcpu();
  2165. if (cpu_id != -1) {
  2166. int lgrp_id = Linux::get_node_by_cpu(cpu_id);
  2167. if (lgrp_id != -1) {
  2168. return lgrp_id;
  2169. }
  2170. }
  2171. return 0;
  2172. }
  2173. size_t os::numa_get_leaf_groups(int *ids, size_t size) {
  2174. for (size_t i = 0; i < size; i++) {
  2175. ids[i] = i;
  2176. }
  2177. return size;
  2178. }
  2179. bool os::get_page_info(char *start, page_info* info) {
  2180. return false;
  2181. }
  2182. char *os::scan_pages(char *start, char* end, page_info* page_expected, page_info* page_found) {
  2183. return end;
  2184. }
  2185. // Something to do with the numa-aware allocator needs these symbols
  2186. extern "C" JNIEXPORT void numa_warn(int number, char *where, ...) { }
  2187. extern "C" JNIEXPORT void numa_error(char *where) { }
  2188. extern "C" JNIEXPORT int fork1() { return fork(); }
  2189. // If we are running with libnuma version > 2, then we should
  2190. // be trying to use symbols with versions 1.1
  2191. // If we are running with earlier version, which did not have symbol versions,
  2192. // we should use the base version.
  2193. void* os::Linux::libnuma_dlsym(void* handle, const char *name) {
  2194. void *f = dlvsym(handle, name, "libnuma_1.1");
  2195. if (f == NULL) {
  2196. f = dlsym(handle, name);
  2197. }
  2198. return f;
  2199. }
  2200. bool os::Linux::libnuma_init() {
  2201. // sched_getcpu() should be in libc.
  2202. set_sched_getcpu(CAST_TO_FN_PTR(sched_getcpu_func_t,
  2203. dlsym(RTLD_DEFAULT, "sched_getcpu")));
  2204. if (sched_getcpu() != -1) { // Does it work?
  2205. void *handle = dlopen("libnuma.so.1", RTLD_LAZY);
  2206. if (handle != NULL) {
  2207. set_numa_node_to_cpus(CAST_TO_FN_PTR(numa_node_to_cpus_func_t,
  2208. libnuma_dlsym(handle, "numa_node_to_cpus")));
  2209. set_numa_max_node(CAST_TO_FN_PTR(numa_max_node_func_t,
  2210. libnuma_dlsym(handle, "numa_max_node")));
  2211. set_numa_available(CAST_TO_FN_PTR(numa_available_func_t,
  2212. libnuma_dlsym(handle, "numa_available")));
  2213. set_numa_tonode_memory(CAST_TO_FN_PTR(numa_tonode_memory_func_t,
  2214. libnuma_dlsym(handle, "numa_tonode_memory")));
  2215. set_numa_interleave_memory(CAST_TO_FN_PTR(numa_interleave_memory_func_t,
  2216. libnuma_dlsym(handle, "numa_interleave_memory")));
  2217. if (numa_available() != -1) {
  2218. set_numa_all_nodes((unsigned long*)libnuma_dlsym(handle, "numa_all_nodes"));
  2219. // Create a cpu -> node mapping
  2220. _cpu_to_node = new (ResourceObj::C_HEAP) GrowableArray<int>(0, true);
  2221. rebuild_cpu_to_node_map();
  2222. return true;
  2223. }
  2224. }
  2225. }
  2226. return false;
  2227. }
  2228. // rebuild_cpu_to_node_map() constructs a table mapping cpud id to node id.
  2229. // The table is later used in get_node_by_cpu().
  2230. void os::Linux::rebuild_cpu_to_node_map() {
  2231. const size_t NCPUS = 32768; // Since the buffer size computation is very obscure
  2232. // in libnuma (possible values are starting from 16,
  2233. // and continuing up with every other power of 2, but less
  2234. // than the maximum number of CPUs supported by kernel), and
  2235. // is a subject to change (in libnuma version 2 the requirements
  2236. // are more reasonable) we'll just hardcode the number they use
  2237. // in the library.
  2238. const size_t BitsPerCLong = sizeof(long) * CHAR_BIT;
  2239. size_t cpu_num = os::active_processor_count();
  2240. size_t cpu_map_size = NCPUS / BitsPerCLong;
  2241. size_t cpu_map_valid_size =
  2242. MIN2((cpu_num + BitsPerCLong - 1) / BitsPerCLong, cpu_map_size);
  2243. cpu_to_node()->clear();
  2244. cpu_to_node()->at_grow(cpu_num - 1);
  2245. size_t node_num = numa_get_groups_num();
  2246. unsigned long *cpu_map = NEW_C_HEAP_ARRAY(unsigned long, cpu_map_size);
  2247. for (size_t i = 0; i < node_num; i++) {
  2248. if (numa_node_to_cpus(i, cpu_map, cpu_map_size * sizeof(unsigned long)) != -1) {
  2249. for (size_t j = 0; j < cpu_map_valid_size; j++) {
  2250. if (cpu_map[j] != 0) {
  2251. for (size_t k = 0; k < BitsPerCLong; k++) {
  2252. if (cpu_map[j] & (1UL << k)) {
  2253. cpu_to_node()->at_put(j * BitsPerCLong + k, i);
  2254. }
  2255. }
  2256. }
  2257. }
  2258. }
  2259. }
  2260. FREE_C_HEAP_ARRAY(unsigned long, cpu_map);
  2261. }
  2262. int os::Linux::get_node_by_cpu(int cpu_id) {
  2263. if (cpu_to_node() != NULL && cpu_id >= 0 && cpu_id < cpu_to_node()->length()) {
  2264. return cpu_to_node()->at(cpu_id);
  2265. }
  2266. return -1;
  2267. }
  2268. GrowableArray<int>* os::Linux::_cpu_to_node;
  2269. os::Linux::sched_getcpu_func_t os::Linux::_sched_getcpu;
  2270. os::Linux::numa_node_to_cpus_func_t os::Linux::_numa_node_to_cpus;
  2271. os::Linux::numa_max_node_func_t os::Linux::_numa_max_node;
  2272. os::Linux::numa_available_func_t os::Linux::_numa_available;
  2273. os::Linux::numa_tonode_memory_func_t os::Linux::_numa_tonode_memory;
  2274. os::Linux::numa_interleave_memory_func_t os::Linux::_numa_interleave_memory;
  2275. unsigned long* os::Linux::_numa_all_nodes;
  2276. bool os::uncommit_memory(char* addr, size_t size) {
  2277. uintptr_t res = (uintptr_t) ::mmap(addr, size, PROT_NONE,
  2278. MAP_PRIVATE|MAP_FIXED|MAP_NORESERVE|MAP_ANONYMOUS, -1, 0);
  2279. return res != (uintptr_t) MAP_FAILED;
  2280. }
  2281. // Linux uses a growable mapping for the stack, and if the mapping for
  2282. // the stack guard pages is not removed when we detach a thread the
  2283. // stack cannot grow beyond the pages where the stack guard was
  2284. // mapped. If at some point later in the process the stack expands to
  2285. // that point, the Linux kernel cannot expand the stack any further
  2286. // because the guard pages are in the way, and a segfault occurs.
  2287. //
  2288. // However, it's essential not to split the stack region by unmapping
  2289. // a region (leaving a hole) that's already part of the stack mapping,
  2290. // so if the stack mapping has already grown beyond the guard pages at
  2291. // the time we create them, we have to truncate the stack mapping.
  2292. // So, we need to know the extent of the stack mapping when
  2293. // create_stack_guard_pages() is called.
  2294. // Find the bounds of the stack mapping. Return true for success.
  2295. //
  2296. // We only need this for stacks that are growable: at the time of
  2297. // writing thread stacks don't use growable mappings (i.e. those
  2298. // creeated with MAP_GROWSDOWN), and aren't marked "[stack]", so this
  2299. // only applies to the main thread.
  2300. static
  2301. bool get_stack_bounds(uintptr_t *bottom, uintptr_t *top) {
  2302. char buf[128];
  2303. int fd, sz;
  2304. if ((fd = ::open("/proc/self/maps", O_RDONLY)) < 0) {
  2305. return false;
  2306. }
  2307. const char kw[] = "[stack]";
  2308. const int kwlen = sizeof(kw)-1;
  2309. // Address part of /proc/self/maps couldn't be more than 128 bytes
  2310. while ((sz = os::get_line_chars(fd, buf, sizeof(buf))) > 0) {
  2311. if (sz > kwlen && ::memcmp(buf+sz-kwlen, kw, kwlen) == 0) {
  2312. // Extract addresses
  2313. if (sscanf(buf, "%" SCNxPTR "-%" SCNxPTR, bottom, top) == 2) {
  2314. uintptr_t sp = (uintptr_t) __builtin_frame_address(0);
  2315. if (sp >= *bottom && sp <= *top) {
  2316. ::close(fd);
  2317. return true;
  2318. }
  2319. }
  2320. }
  2321. }
  2322. ::close(fd);
  2323. return false;
  2324. }
  2325. // If the (growable) stack mapping already extends beyond the point
  2326. // where we're going to put our guard pages, truncate the mapping at
  2327. // that point by munmap()ping it. This ensures that when we later
  2328. // munmap() the guard pages we don't leave a hole in the stack
  2329. // mapping. This only affects the main/initial thread, but guard
  2330. // against future OS changes
  2331. bool os::create_stack_guard_pages(char* addr, size_t size) {
  2332. uintptr_t stack_extent, stack_base;
  2333. bool chk_bounds = NOT_DEBUG(os::Linux::is_initial_thread()) DEBUG_ONLY(true);
  2334. if (chk_bounds && get_stack_bounds(&stack_extent, &stack_base)) {
  2335. assert(os::Linux::is_initial_thread(),
  2336. "growable stack in non-initial thread");
  2337. if (stack_extent < (uintptr_t)addr)
  2338. ::munmap((void*)stack_extent, (uintptr_t)addr - stack_extent);
  2339. }
  2340. return os::commit_memory(addr, size);
  2341. }
  2342. // If this is a growable mapping, remove the guard pages entirely by
  2343. // munmap()ping them. If not, just call uncommit_memory(). This only
  2344. // affects the main/initial thread, but guard against future OS changes
  2345. bool os::remove_stack_guard_pages(char* addr, size_t size) {
  2346. uintptr_t stack_extent, stack_base;
  2347. bool chk_bounds = NOT_DEBUG(os::Linux::is_initial_thread()) DEBUG_ONLY(true);
  2348. if (chk_bounds && get_stack_bounds(&stack_extent, &stack_base)) {
  2349. assert(os::Linux::is_initial_thread(),
  2350. "growable stack in non-initial thread");
  2351. return ::munmap(addr, size) == 0;
  2352. }
  2353. return os::uncommit_memory(addr, size);
  2354. }
  2355. static address _highest_vm_reserved_address = NULL;
  2356. // If 'fixed' is true, anon_mmap() will attempt to reserve anonymous memory
  2357. // at 'requested_addr'. If there are existing memory mappings at the same
  2358. // location, however, they will be overwritten. If 'fixed' is false,
  2359. // 'requested_addr' is only treated as a hint, the return value may or
  2360. // may not start from the requested address. Unlike Linux mmap(), this
  2361. // function returns NULL to indicate failure.
  2362. static char* anon_mmap(char* requested_addr, size_t bytes, bool fixed) {
  2363. char * addr;
  2364. int flags;
  2365. flags = MAP_PRIVATE | MAP_NORESERVE | MAP_ANONYMOUS;
  2366. if (fixed) {
  2367. assert((uintptr_t)requested_addr % os::Linux::page_size() == 0, "unaligned address");
  2368. flags |= MAP_FIXED;
  2369. }
  2370. // Map uncommitted pages PROT_READ and PROT_WRITE, change access
  2371. // to PROT_EXEC if executable when we commit the page.
  2372. addr = (char*)::mmap(requested_addr, bytes, PROT_READ|PROT_WRITE,
  2373. flags, -1, 0);
  2374. if (addr != MAP_FAILED) {
  2375. // anon_mmap() should only get called during VM initialization,
  2376. // don't need lock (actually we can skip locking even it can be called
  2377. // from multiple threads, because _highest_vm_reserved_address is just a
  2378. // hint about the upper limit of non-stack memory regions.)
  2379. if ((address)addr + bytes > _highest_vm_reserved_address) {
  2380. _highest_vm_reserved_address = (address)addr + bytes;
  2381. }
  2382. }
  2383. return addr == MAP_FAILED ? NULL : addr;
  2384. }
  2385. // Don't update _highest_vm_reserved_address, because there might be memory
  2386. // regions above addr + size. If so, releasing a memory region only creates
  2387. // a hole in the address space, it doesn't help prevent heap-stack collision.
  2388. //
  2389. static int anon_munmap(char * addr, size_t size) {
  2390. return ::munmap(addr, size) == 0;
  2391. }
  2392. char* os::reserve_memory(size_t bytes, char* requested_addr,
  2393. size_t alignment_hint) {
  2394. return anon_mmap(requested_addr, bytes, (requested_addr != NULL));
  2395. }
  2396. bool os::release_memory(char* addr, size_t size) {
  2397. return anon_munmap(addr, size);
  2398. }
  2399. static address highest_vm_reserved_address() {
  2400. return _highest_vm_reserved_address;
  2401. }
  2402. static bool linux_mprotect(char* addr, size_t size, int prot) {
  2403. // Linux wants the mprotect address argument to be page aligned.
  2404. char* bottom = (char*)align_size_down((intptr_t)addr, os::Linux::page_size());
  2405. // According to SUSv3, mprotect() should only be used with mappings
  2406. // established by mmap(), and mmap() always maps whole pages. Unaligned
  2407. // 'addr' likely indicates problem in the VM (e.g. trying to change
  2408. // protection of malloc'ed or statically allocated memory). Check the
  2409. // caller if you hit this assert.
  2410. assert(addr == bottom, "sanity check");
  2411. size = align_size_up(pointer_delta(addr, bottom, 1) + size, os::Linux::page_size());
  2412. return ::mprotect(bottom, size, prot) == 0;
  2413. }
  2414. // Set protections specified
  2415. bool os::protect_memory(char* addr, size_t bytes, ProtType prot,
  2416. bool is_committed) {
  2417. unsigned int p = 0;
  2418. switch (prot) {
  2419. case MEM_PROT_NONE: p = PROT_NONE; break;
  2420. case MEM_PROT_READ: p = PROT_READ; break;
  2421. case MEM_PROT_RW: p = PROT_READ|PROT_WRITE; break;
  2422. case MEM_PROT_RWX: p = PROT_READ|PROT_WRITE|PROT_EXEC; break;
  2423. default:
  2424. ShouldNotReachHere();
  2425. }
  2426. // is_committed is unused.
  2427. return linux_mprotect(addr, bytes, p);
  2428. }
  2429. bool os::guard_memory(char* addr, size_t size) {
  2430. return linux_mprotect(addr, size, PROT_NONE);
  2431. }
  2432. bool os::unguard_memory(char* addr, size_t size) {
  2433. return linux_mprotect(addr, size, PROT_READ|PROT_WRITE);
  2434. }
  2435. bool os::Linux::hugetlbfs_sanity_check(bool warn, size_t page_size) {
  2436. bool result = false;
  2437. void *p = mmap (NULL, page_size, PROT_READ|PROT_WRITE,
  2438. MAP_ANONYMOUS|MAP_PRIVATE|MAP_HUGETLB,
  2439. -1, 0);
  2440. if (p != (void *) -1) {
  2441. // We don't know if this really is a huge page or not.
  2442. FILE *fp = fopen("/proc/self/maps", "r");
  2443. if (fp) {
  2444. while (!feof(fp)) {
  2445. char chars[257];
  2446. long x = 0;
  2447. if (fgets(chars, sizeof(chars), fp)) {
  2448. if (sscanf(chars, "%lx-%*x", &x) == 1
  2449. && x == (long)p) {
  2450. if (strstr (chars, "hugepage")) {
  2451. result = true;
  2452. break;
  2453. }
  2454. }
  2455. }
  2456. }
  2457. fclose(fp);
  2458. }
  2459. munmap (p, page_size);
  2460. if (result)
  2461. return true;
  2462. }
  2463. if (warn) {
  2464. warning("HugeTLBFS is not supported by the operating system.");
  2465. }
  2466. return result;
  2467. }
  2468. /*
  2469. * Set the coredump_filter bits to include largepages in core dump (bit 6)
  2470. *
  2471. * From the coredump_filter documentation:
  2472. *
  2473. * - (bit 0) anonymous private memory
  2474. * - (bit 1) anonymous shared memory
  2475. * - (bit 2) file-backed private memory
  2476. * - (bit 3) file-backed shared memory
  2477. * - (bit 4) ELF header pages in file-backed private memory areas (it is
  2478. * effective only if the bit 2 is cleared)
  2479. * - (bit 5) hugetlb private memory
  2480. * - (bit 6) hugetlb shared memory
  2481. */
  2482. static void set_coredump_filter(void) {
  2483. FILE *f;
  2484. long cdm;
  2485. if ((f = fopen("/proc/self/coredump_filter", "r+")) == NULL) {
  2486. return;
  2487. }
  2488. if (fscanf(f, "%lx", &cdm) != 1) {
  2489. fclose(f);
  2490. return;
  2491. }
  2492. rewind(f);
  2493. if ((cdm & LARGEPAGES_BIT) == 0) {
  2494. cdm |= LARGEPAGES_BIT;
  2495. fprintf(f, "%#lx", cdm);
  2496. }
  2497. fclose(f);
  2498. }
  2499. // Large page support
  2500. static size_t _large_page_size = 0;
  2501. void os::large_page_init() {
  2502. if (!UseLargePages) {
  2503. UseHugeTLBFS = false;
  2504. UseSHM = false;
  2505. return;
  2506. }
  2507. if (FLAG_IS_DEFAULT(UseHugeTLBFS) && FLAG_IS_DEFAULT(UseSHM)) {
  2508. // If UseLargePages is specified on the command line try both methods,
  2509. // if it's default, then try only HugeTLBFS.
  2510. if (FLAG_IS_DEFAULT(UseLargePages)) {
  2511. UseHugeTLBFS = true;
  2512. } else {
  2513. UseHugeTLBFS = UseSHM = true;
  2514. }
  2515. }
  2516. if (LargePageSizeInBytes) {
  2517. _large_page_size = LargePageSizeInBytes;
  2518. } else {
  2519. // large_page_size on Linux is used to round up heap size. x86 uses either
  2520. // 2M or 4M page, depending on whether PAE (Physical Address Extensions)
  2521. // mode is enabled. AMD64/EM64T uses 2M page in 64bit mode. IA64 can use
  2522. // page as large as 256M.
  2523. //
  2524. // Here we try to figure out page size by parsing /proc/meminfo and looking
  2525. // for a line with the following format:
  2526. // Hugepagesize: 2048 kB
  2527. //
  2528. // If we can't determine the value (e.g. /proc is not mounted, or the text
  2529. // format has been changed), we'll use the largest page size supported by
  2530. // the processor.
  2531. #ifndef ZERO
  2532. _large_page_size = IA32_ONLY(4 * M) AMD64_ONLY(2 * M) IA64_ONLY(256 * M) SPARC_ONLY(4 * M)
  2533. ARM_ONLY(2 * M) PPC_ONLY(4 * M);
  2534. #endif // ZERO
  2535. FILE *fp = fopen("/proc/meminfo", "r");
  2536. if (fp) {
  2537. while (!feof(fp)) {
  2538. int x = 0;
  2539. char buf[16];
  2540. if (fscanf(fp, "Hugepagesize: %d", &x) == 1) {
  2541. if (x && fgets(buf, sizeof(buf), fp) && strcmp(buf, " kB\n") == 0) {
  2542. _large_page_size = x * K;
  2543. break;
  2544. }
  2545. } else {
  2546. // skip to next line
  2547. for (;;) {
  2548. int ch = fgetc(fp);
  2549. if (ch == EOF || ch == (int)'\n') break;
  2550. }
  2551. }
  2552. }
  2553. fclose(fp);
  2554. }
  2555. }
  2556. // print a warning if any large page related flag is specified on command line
  2557. bool warn_on_failure = !FLAG_IS_DEFAULT(UseHugeTLBFS);
  2558. const size_t default_page_size = (size_t)Linux::page_size();
  2559. if (_large_page_size > default_page_size) {
  2560. _page_sizes[0] = _large_page_size;
  2561. _page_sizes[1] = default_page_size;
  2562. _page_sizes[2] = 0;
  2563. }
  2564. UseHugeTLBFS = UseHugeTLBFS &&
  2565. Linux::hugetlbfs_sanity_check(warn_on_failure, _large_page_size);
  2566. if (UseHugeTLBFS)
  2567. UseSHM = false;
  2568. UseLargePages = UseHugeTLBFS || UseSHM;
  2569. set_coredump_filter();
  2570. }
  2571. #ifndef SHM_HUGETLB
  2572. #define SHM_HUGETLB 04000
  2573. #endif
  2574. char* os::reserve_memory_special(size_t bytes, char* req_addr, bool exec) {
  2575. // "exec" is passed in but not used. Creating the shared image for
  2576. // the code cache doesn't have an SHM_X executable permission to check.
  2577. assert(UseLargePages && UseSHM, "only for SHM large pages");
  2578. key_t key = IPC_PRIVATE;
  2579. char *addr;
  2580. bool warn_on_failure = UseLargePages &&
  2581. (!FLAG_IS_DEFAULT(UseLargePages) ||
  2582. !FLAG_IS_DEFAULT(LargePageSizeInBytes)
  2583. );
  2584. char msg[128];
  2585. // Create a large shared memory region to attach to based on size.
  2586. // Currently, size is the total size of the heap
  2587. int shmid = shmget(key, bytes, SHM_HUGETLB|IPC_CREAT|SHM_R|SHM_W);
  2588. if (shmid == -1) {
  2589. // Possible reasons for shmget failure:
  2590. // 1. shmmax is too small for Java heap.
  2591. // > check shmmax value: cat /proc/sys/kernel/shmmax
  2592. // > increase shmmax value: echo "0xffffffff" > /proc/sys/kernel/shmmax
  2593. // 2. not enough large page memory.
  2594. // > check available large pages: cat /proc/meminfo
  2595. // > increase amount of large pages:
  2596. // echo new_value > /proc/sys/vm/nr_hugepages
  2597. // Note 1: different Linux may use different name for this property,
  2598. // e.g. on Redhat AS-3 it is "hugetlb_pool".
  2599. // Note 2: it's possible there's enough physical memory available but
  2600. // they are so fragmented after a long run that they can't
  2601. // coalesce into large pages. Try to reserve large pages when
  2602. // the system is still "fresh".
  2603. if (warn_on_failure) {
  2604. jio_snprintf(msg, sizeof(msg), "Failed to reserve shared memory (errno = %d).", errno);
  2605. warning(msg);
  2606. }
  2607. return NULL;
  2608. }
  2609. // attach to the region
  2610. addr = (char*)shmat(shmid, req_addr, 0);
  2611. int err = errno;
  2612. // Remove shmid. If shmat() is successful, the actual shared memory segment
  2613. // will be deleted when it's detached by shmdt() or when the process
  2614. // terminates. If shmat() is not successful this will remove the shared
  2615. // segment immediately.
  2616. shmctl(shmid, IPC_RMID, NULL);
  2617. if ((intptr_t)addr == -1) {
  2618. if (warn_on_failure) {
  2619. jio_snprintf(msg, sizeof(msg), "Failed to attach shared memory (errno = %d).", err);
  2620. warning(msg);
  2621. }
  2622. return NULL;
  2623. }
  2624. return addr;
  2625. }
  2626. bool os::release_memory_special(char* base, size_t bytes) {
  2627. // detaching the SHM segment will also delete it, see reserve_memory_special()
  2628. int rslt = shmdt(base);
  2629. return rslt == 0;
  2630. }
  2631. size_t os::large_page_size() {
  2632. return _large_page_size;
  2633. }
  2634. // HugeTLBFS allows application to commit large page memory on demand;
  2635. // with SysV SHM the entire memory region must be allocated as shared
  2636. // memory.
  2637. bool os::can_commit_large_page_memory() {
  2638. return UseHugeTLBFS;
  2639. }
  2640. bool os::can_execute_large_page_memory() {
  2641. return UseHugeTLBFS;
  2642. }
  2643. // Reserve memory at an arbitrary address, only if that area is
  2644. // available (and not reserved for something else).
  2645. char* os::attempt_reserve_memory_at(size_t bytes, char* requested_addr) {
  2646. const int max_tries = 10;
  2647. char* base[max_tries];
  2648. size_t size[max_tries];
  2649. const size_t gap = 0x000000;
  2650. // Assert only that the size is a multiple of the page size, since
  2651. // that's all that mmap requires, and since that's all we really know
  2652. // about at this low abstraction level. If we need higher alignment,
  2653. // we can either pass an alignment to this method or verify alignment
  2654. // in one of the methods further up the call chain. See bug 5044738.
  2655. assert(bytes % os::vm_page_size() == 0, "reserving unexpected size block");
  2656. // Repeatedly allocate blocks until the block is allocated at the
  2657. // right spot. Give up after max_tries. Note that reserve_memory() will
  2658. // automatically update _highest_vm_reserved_address if the call is
  2659. // successful. The variable tracks the highest memory address every reserved
  2660. // by JVM. It is used to detect heap-stack collision if running with
  2661. // fixed-stack LinuxThreads. Because here we may attempt to reserve more
  2662. // space than needed, it could confuse the collision detecting code. To
  2663. // solve the problem, save current _highest_vm_reserved_address and
  2664. // calculate the correct value before return.
  2665. address old_highest = _highest_vm_reserved_address;
  2666. // Linux mmap allows caller to pass an address as hint; give it a try first,
  2667. // if kernel honors the hint then we can return immediately.
  2668. char * addr = anon_mmap(requested_addr, bytes, false);
  2669. if (addr == requested_addr) {
  2670. return requested_addr;
  2671. }
  2672. if (addr != NULL) {
  2673. // mmap() is successful but it fails to reserve at the requested address
  2674. anon_munmap(addr, bytes);
  2675. }
  2676. int i;
  2677. for (i = 0; i < max_tries; ++i) {
  2678. base[i] = reserve_memory(bytes);
  2679. if (base[i] != NULL) {
  2680. // Is this the block we wanted?
  2681. if (base[i] == requested_addr) {
  2682. size[i] = bytes;
  2683. break;
  2684. }
  2685. // Does this overlap the block we wanted? Give back the overlapped
  2686. // parts and try again.
  2687. size_t top_overlap = requested_addr + (bytes + gap) - base[i];
  2688. if (top_overlap >= 0 && top_overlap < bytes) {
  2689. unmap_memory(base[i], top_overlap);
  2690. base[i] += top_overlap;
  2691. size[i] = bytes - top_overlap;
  2692. } else {
  2693. size_t bottom_overlap = base[i] + bytes - requested_addr;
  2694. if (bottom_overlap >= 0 && bottom_overlap < bytes) {
  2695. unmap_memory(requested_addr, bottom_overlap);
  2696. size[i] = bytes - bottom_overlap;
  2697. } else {
  2698. size[i] = bytes;
  2699. }
  2700. }
  2701. }
  2702. }
  2703. // Give back the unused reserved pieces.
  2704. for (int j = 0; j < i; ++j) {
  2705. if (base[j] != NULL) {
  2706. unmap_memory(base[j], size[j]);
  2707. }
  2708. }
  2709. if (i < max_tries) {
  2710. _highest_vm_reserved_address = MAX2(old_highest, (address)requested_addr + bytes);
  2711. return requested_addr;
  2712. } else {
  2713. _highest_vm_reserved_address = old_highest;
  2714. return NULL;
  2715. }
  2716. }
  2717. size_t os::read(int fd, void *buf, unsigned int nBytes) {
  2718. return ::read(fd, buf, nBytes);
  2719. }
  2720. // TODO-FIXME: reconcile Solaris' os::sleep with the linux variation.
  2721. // Solaris uses poll(), linux uses park().
  2722. // Poll() is likely a better choice, assuming that Thread.interrupt()
  2723. // generates a SIGUSRx signal. Note that SIGUSR1 can interfere with
  2724. // SIGSEGV, see 4355769.
  2725. const int NANOSECS_PER_MILLISECS = 1000000;
  2726. int os::sleep(Thread* thread, jlong millis, bool interruptible) {
  2727. assert(thread == Thread::current(), "thread consistency check");
  2728. ParkEvent * const slp = thread->_SleepEvent ;
  2729. slp->reset() ;
  2730. OrderAccess::fence() ;
  2731. if (interruptible) {
  2732. jlong prevtime = javaTimeNanos();
  2733. for (;;) {
  2734. if (os::is_interrupted(thread, true)) {
  2735. return OS_INTRPT;
  2736. }
  2737. jlong newtime = javaTimeNanos();
  2738. if (newtime - prevtime < 0) {
  2739. // time moving backwards, should only happen if no monotonic clock
  2740. // not a guarantee() because JVM should not abort on kernel/glibc bugs
  2741. assert(!Linux::supports_monotonic_clock(), "time moving backwards");
  2742. } else {
  2743. millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
  2744. }
  2745. if(millis <= 0) {
  2746. return OS_OK;
  2747. }
  2748. prevtime = newtime;
  2749. {
  2750. assert(thread->is_Java_thread(), "sanity check");
  2751. JavaThread *jt = (JavaThread *) thread;
  2752. ThreadBlockInVM tbivm(jt);
  2753. OSThreadWaitState osts(jt->osthread(), false /* not Object.wait() */);
  2754. jt->set_suspend_equivalent();
  2755. // cleared by handle_special_suspend_equivalent_condition() or
  2756. // java_suspend_self() via check_and_wait_while_suspended()
  2757. slp->park(millis);
  2758. // were we externally suspended while we were waiting?
  2759. jt->check_and_wait_while_suspended();
  2760. }
  2761. }
  2762. } else {
  2763. OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
  2764. jlong prevtime = javaTimeNanos();
  2765. for (;;) {
  2766. // It'd be nice to avoid the back-to-back javaTimeNanos() calls on
  2767. // the 1st iteration ...
  2768. jlong newtime = javaTimeNanos();
  2769. if (newtime - prevtime < 0) {
  2770. // time moving backwards, should only happen if no monotonic clock
  2771. // not a guarantee() because JVM should not abort on kernel/glibc bugs
  2772. assert(!Linux::supports_monotonic_clock(), "time moving backwards");
  2773. } else {
  2774. millis -= (newtime - prevtime) / NANOSECS_PER_MILLISECS;
  2775. }
  2776. if(millis <= 0) break ;
  2777. prevtime = newtime;
  2778. slp->park(millis);
  2779. }
  2780. return OS_OK ;
  2781. }
  2782. }
  2783. int os::naked_sleep() {
  2784. // %% make the sleep time an integer flag. for now use 1 millisec.
  2785. return os::sleep(Thread::current(), 1, false);
  2786. }
  2787. // Sleep forever; naked call to OS-specific sleep; use with CAUTION
  2788. void os::infinite_sleep() {
  2789. while (true) { // sleep forever ...
  2790. ::sleep(100); // ... 100 seconds at a time
  2791. }
  2792. }
  2793. // Used to convert frequent JVM_Yield() to nops
  2794. bool os::dont_yield() {
  2795. return DontYieldALot;
  2796. }
  2797. void os::yield() {
  2798. sched_yield();
  2799. }
  2800. os::YieldResult os::NakedYield() { sched_yield(); return os::YIELD_UNKNOWN ;}
  2801. void os::yield_all(int attempts) {
  2802. // Yields to all threads, including threads with lower priorities
  2803. // Threads on Linux are all with same priority. The Solaris style
  2804. // os::yield_all() with nanosleep(1ms) is not necessary.
  2805. sched_yield();
  2806. }
  2807. // Called from the tight loops to possibly influence time-sharing heuristics
  2808. void os::loop_breaker(int attempts) {
  2809. os::yield_all(attempts);
  2810. }
  2811. ////////////////////////////////////////////////////////////////////////////////
  2812. // thread priority support
  2813. // Note: Normal Linux applications are run with SCHED_OTHER policy. SCHED_OTHER
  2814. // only supports dynamic priority, static priority must be zero. For real-time
  2815. // applications, Linux supports SCHED_RR which allows static priority (1-99).
  2816. // However, for large multi-threaded applications, SCHED_RR is not only slower
  2817. // than SCHED_OTHER, but also very unstable (my volano tests hang hard 4 out
  2818. // of 5 runs - Sep 2005).
  2819. //
  2820. // The following code actually changes the niceness of kernel-thread/LWP. It
  2821. // has an assumption that setpriority() only modifies one kernel-thread/LWP,
  2822. // not the entire user process, and user level threads are 1:1 mapped to kernel
  2823. // threads. It has always been the case, but could change in the future. For
  2824. // this reason, the code should not be used as default (ThreadPriorityPolicy=0).
  2825. // It is only used when ThreadPriorityPolicy=1 and requires root privilege.
  2826. int os::java_to_os_priority[MaxPriority + 1] = {
  2827. 19, // 0 Entry should never be used
  2828. 4, // 1 MinPriority
  2829. 3, // 2
  2830. 2, // 3
  2831. 1, // 4
  2832. 0, // 5 NormPriority
  2833. -1, // 6
  2834. -2, // 7
  2835. -3, // 8
  2836. -4, // 9 NearMaxPriority
  2837. -5 // 10 MaxPriority
  2838. };
  2839. static int prio_init() {
  2840. if (ThreadPriorityPolicy == 1) {
  2841. // Only root can raise thread priority. Don't allow ThreadPriorityPolicy=1
  2842. // if effective uid is not root. Perhaps, a more elegant way of doing
  2843. // this is to test CAP_SYS_NICE capability, but that will require libcap.so
  2844. if (geteuid() != 0) {
  2845. if (!FLAG_IS_DEFAULT(ThreadPriorityPolicy)) {
  2846. warning("-XX:ThreadPriorityPolicy requires root privilege on Linux");
  2847. }
  2848. ThreadPriorityPolicy = 0;
  2849. }
  2850. }
  2851. return 0;
  2852. }
  2853. OSReturn os::set_native_priority(Thread* thread, int newpri) {
  2854. if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) return OS_OK;
  2855. int ret = setpriority(PRIO_PROCESS, thread->osthread()->thread_id(), newpri);
  2856. return (ret == 0) ? OS_OK : OS_ERR;
  2857. }
  2858. OSReturn os::get_native_priority(const Thread* const thread, int *priority_ptr) {
  2859. if ( !UseThreadPriorities || ThreadPriorityPolicy == 0 ) {
  2860. *priority_ptr = java_to_os_priority[NormPriority];
  2861. return OS_OK;
  2862. }
  2863. errno = 0;
  2864. *priority_ptr = getpriority(PRIO_PROCESS, thread->osthread()->thread_id());
  2865. return (*priority_ptr != -1 || errno == 0 ? OS_OK : OS_ERR);
  2866. }
  2867. // Hint to the underlying OS that a task switch would not be good.
  2868. // Void return because it's a hint and can fail.
  2869. void os::hint_no_preempt() {}
  2870. ////////////////////////////////////////////////////////////////////////////////
  2871. // suspend/resume support
  2872. // the low-level signal-based suspend/resume support is a remnant from the
  2873. // old VM-suspension that used to be for java-suspension, safepoints etc,
  2874. // within hotspot. Now there is a single use-case for this:
  2875. // - calling get_thread_pc() on the VMThread by the flat-profiler task
  2876. // that runs in the watcher thread.
  2877. // The remaining code is greatly simplified from the more general suspension
  2878. // code that used to be used.
  2879. //
  2880. // The protocol is quite simple:
  2881. // - suspend:
  2882. // - sends a signal to the target thread
  2883. // - polls the suspend state of the osthread using a yield loop
  2884. // - target thread signal handler (SR_handler) sets suspend state
  2885. // and blocks in sigsuspend until continued
  2886. // - resume:
  2887. // - sets target osthread state to continue
  2888. // - sends signal to end the sigsuspend loop in the SR_handler
  2889. //
  2890. // Note that the SR_lock plays no role in this suspend/resume protocol.
  2891. //
  2892. static void resume_clear_context(OSThread *osthread) {
  2893. osthread->set_ucontext(NULL);
  2894. osthread->set_siginfo(NULL);
  2895. // notify the suspend action is completed, we have now resumed
  2896. osthread->sr.clear_suspended();
  2897. }
  2898. static void suspend_save_context(OSThread *osthread, siginfo_t* siginfo, ucontext_t* context) {
  2899. osthread->set_ucontext(context);
  2900. osthread->set_siginfo(siginfo);
  2901. }
  2902. //
  2903. // Handler function invoked when a thread's execution is suspended or
  2904. // resumed. We have to be careful that only async-safe functions are
  2905. // called here (Note: most pthread functions are not async safe and
  2906. // should be avoided.)
  2907. //
  2908. // Note: sigwait() is a more natural fit than sigsuspend() from an
  2909. // interface point of view, but sigwait() prevents the signal hander
  2910. // from being run. libpthread would get very confused by not having
  2911. // its signal handlers run and prevents sigwait()'s use with the
  2912. // mutex granting granting signal.
  2913. //
  2914. // Currently only ever called on the VMThread
  2915. //
  2916. static void SR_handler(int sig, siginfo_t* siginfo, ucontext_t* context) {
  2917. // Save and restore errno to avoid confusing native code with EINTR
  2918. // after sigsuspend.
  2919. int old_errno = errno;
  2920. Thread* thread = Thread::current();
  2921. OSThread* osthread = thread->osthread();
  2922. assert(thread->is_VM_thread(), "Must be VMThread");
  2923. // read current suspend action
  2924. int action = osthread->sr.suspend_action();
  2925. if (action == SR_SUSPEND) {
  2926. suspend_save_context(osthread, siginfo, context);
  2927. // Notify the suspend action is about to be completed. do_suspend()
  2928. // waits until SR_SUSPENDED is set and then returns. We will wait
  2929. // here for a resume signal and that completes the suspend-other
  2930. // action. do_suspend/do_resume is always called as a pair from
  2931. // the same thread - so there are no races
  2932. // notify the caller
  2933. osthread->sr.set_suspended();
  2934. sigset_t suspend_set; // signals for sigsuspend()
  2935. // get current set of blocked signals and unblock resume signal
  2936. pthread_sigmask(SIG_BLOCK, NULL, &suspend_set);
  2937. sigdelset(&suspend_set, SR_signum);
  2938. // wait here until we are resumed
  2939. do {
  2940. sigsuspend(&suspend_set);
  2941. // ignore all returns until we get a resume signal
  2942. } while (osthread->sr.suspend_action() != SR_CONTINUE);
  2943. resume_clear_context(osthread);
  2944. } else {
  2945. assert(action == SR_CONTINUE, "unexpected sr action");
  2946. // nothing special to do - just leave the handler
  2947. }
  2948. errno = old_errno;
  2949. }
  2950. static int SR_initialize() {
  2951. struct sigaction act;
  2952. char *s;
  2953. /* Get signal number to use for suspend/resume */
  2954. if ((s = ::getenv("_JAVA_SR_SIGNUM")) != 0) {
  2955. int sig = ::strtol(s, 0, 10);
  2956. if (sig > 0 || sig < _NSIG) {
  2957. SR_signum = sig;
  2958. }
  2959. }
  2960. assert(SR_signum > SIGSEGV && SR_signum > SIGBUS,
  2961. "SR_signum must be greater than max(SIGSEGV, SIGBUS), see 4355769");
  2962. sigemptyset(&SR_sigset);
  2963. sigaddset(&SR_sigset, SR_signum);
  2964. /* Set up signal handler for suspend/resume */
  2965. act.sa_flags = SA_RESTART|SA_SIGINFO;
  2966. act.sa_handler = (void (*)(int)) SR_handler;
  2967. // SR_signum is blocked by default.
  2968. // 4528190 - We also need to block pthread restart signal (32 on all
  2969. // supported Linux platforms). Note that LinuxThreads need to block
  2970. // this signal for all threads to work properly. So we don't have
  2971. // to use hard-coded signal number when setting up the mask.
  2972. pthread_sigmask(SIG_BLOCK, NULL, &act.sa_mask);
  2973. if (sigaction(SR_signum, &act, 0) == -1) {
  2974. return -1;
  2975. }
  2976. // Save signal flag
  2977. os::Linux::set_our_sigflags(SR_signum, act.sa_flags);
  2978. return 0;
  2979. }
  2980. static int SR_finalize() {
  2981. return 0;
  2982. }
  2983. // returns true on success and false on error - really an error is fatal
  2984. // but this seems the normal response to library errors
  2985. static bool do_suspend(OSThread* osthread) {
  2986. // mark as suspended and send signal
  2987. osthread->sr.set_suspend_action(SR_SUSPEND);
  2988. int status = pthread_kill(osthread->pthread_id(), SR_signum);
  2989. assert_status(status == 0, status, "pthread_kill");
  2990. // check status and wait until notified of suspension
  2991. if (status == 0) {
  2992. for (int i = 0; !osthread->sr.is_suspended(); i++) {
  2993. os::yield_all(i);
  2994. }
  2995. osthread->sr.set_suspend_action(SR_NONE);
  2996. return true;
  2997. }
  2998. else {
  2999. osthread->sr.set_suspend_action(SR_NONE);
  3000. return false;
  3001. }
  3002. }
  3003. static void do_resume(OSThread* osthread) {
  3004. assert(osthread->sr.is_suspended(), "thread should be suspended");
  3005. osthread->sr.set_suspend_action(SR_CONTINUE);
  3006. int status = pthread_kill(osthread->pthread_id(), SR_signum);
  3007. assert_status(status == 0, status, "pthread_kill");
  3008. // check status and wait unit notified of resumption
  3009. if (status == 0) {
  3010. for (int i = 0; osthread->sr.is_suspended(); i++) {
  3011. os::yield_all(i);
  3012. }
  3013. }
  3014. osthread->sr.set_suspend_action(SR_NONE);
  3015. }
  3016. ////////////////////////////////////////////////////////////////////////////////
  3017. // interrupt support
  3018. void os::interrupt(Thread* thread) {
  3019. assert(Thread::current() == thread || Threads_lock->owned_by_self(),
  3020. "possibility of dangling Thread pointer");
  3021. OSThread* osthread = thread->osthread();
  3022. if (!osthread->interrupted()) {
  3023. osthread->set_interrupted(true);
  3024. // More than one thread can get here with the same value of osthread,
  3025. // resulting in multiple notifications. We do, however, want the store
  3026. // to interrupted() to be visible to other threads before we execute unpark().
  3027. OrderAccess::fence();
  3028. ParkEvent * const slp = thread->_SleepEvent ;
  3029. if (slp != NULL) slp->unpark() ;
  3030. }
  3031. // For JSR166. Unpark even if interrupt status already was set
  3032. if (thread->is_Java_thread())
  3033. ((JavaThread*)thread)->parker()->unpark();
  3034. ParkEvent * ev = thread->_ParkEvent ;
  3035. if (ev != NULL) ev->unpark() ;
  3036. }
  3037. bool os::is_interrupted(Thread* thread, bool clear_interrupted) {
  3038. assert(Thread::current() == thread || Threads_lock->owned_by_self(),
  3039. "possibility of dangling Thread pointer");
  3040. OSThread* osthread = thread->osthread();
  3041. bool interrupted = osthread->interrupted();
  3042. if (interrupted && clear_interrupted) {
  3043. osthread->set_interrupted(false);
  3044. // consider thread->_SleepEvent->reset() ... optional optimization
  3045. }
  3046. return interrupted;
  3047. }
  3048. ///////////////////////////////////////////////////////////////////////////////////
  3049. // signal handling (except suspend/resume)
  3050. // This routine may be used by user applications as a "hook" to catch signals.
  3051. // The user-defined signal handler must pass unrecognized signals to this
  3052. // routine, and if it returns true (non-zero), then the signal handler must
  3053. // return immediately. If the flag "abort_if_unrecognized" is true, then this
  3054. // routine will never retun false (zero), but instead will execute a VM panic
  3055. // routine kill the process.
  3056. //
  3057. // If this routine returns false, it is OK to call it again. This allows
  3058. // the user-defined signal handler to perform checks either before or after
  3059. // the VM performs its own checks. Naturally, the user code would be making
  3060. // a serious error if it tried to handle an exception (such as a null check
  3061. // or breakpoint) that the VM was generating for its own correct operation.
  3062. //
  3063. // This routine may recognize any of the following kinds of signals:
  3064. // SIGBUS, SIGSEGV, SIGILL, SIGFPE, SIGQUIT, SIGPIPE, SIGXFSZ, SIGUSR1.
  3065. // It should be consulted by handlers for any of those signals.
  3066. //
  3067. // The caller of this routine must pass in the three arguments supplied
  3068. // to the function referred to in the "sa_sigaction" (not the "sa_handler")
  3069. // field of the structure passed to sigaction(). This routine assumes that
  3070. // the sa_flags field passed to sigaction() includes SA_SIGINFO and SA_RESTART.
  3071. //
  3072. // Note that the VM will print warnings if it detects conflicting signal
  3073. // handlers, unless invoked with the option "-XX:+AllowUserSignalHandlers".
  3074. //
  3075. extern "C" JNIEXPORT int
  3076. JVM_handle_linux_signal(int signo, siginfo_t* siginfo,
  3077. void* ucontext, int abort_if_unrecognized);
  3078. void signalHandler(int sig, siginfo_t* info, void* uc) {
  3079. assert(info != NULL && uc != NULL, "it must be old kernel");
  3080. JVM_handle_linux_signal(sig, info, uc, true);
  3081. }
  3082. // This boolean allows users to forward their own non-matching signals
  3083. // to JVM_handle_linux_signal, harmlessly.
  3084. bool os::Linux::signal_handlers_are_installed = false;
  3085. // For signal-chaining
  3086. struct sigaction os::Linux::sigact[MAXSIGNUM];
  3087. unsigned int os::Linux::sigs = 0;
  3088. bool os::Linux::libjsig_is_loaded = false;
  3089. typedef struct sigaction *(*get_signal_t)(int);
  3090. get_signal_t os::Linux::get_signal_action = NULL;
  3091. struct sigaction* os::Linux::get_chained_signal_action(int sig) {
  3092. struct sigaction *actp = NULL;
  3093. if (libjsig_is_loaded) {
  3094. // Retrieve the old signal handler from libjsig
  3095. actp = (*get_signal_action)(sig);
  3096. }
  3097. if (actp == NULL) {
  3098. // Retrieve the preinstalled signal handler from jvm
  3099. actp = get_preinstalled_handler(sig);
  3100. }
  3101. return actp;
  3102. }
  3103. static bool call_chained_handler(struct sigaction *actp, int sig,
  3104. siginfo_t *siginfo, void *context) {
  3105. // Call the old signal handler
  3106. if (actp->sa_handler == SIG_DFL) {
  3107. // It's more reasonable to let jvm treat it as an unexpected exception
  3108. // instead of taking the default action.
  3109. return false;
  3110. } else if (actp->sa_handler != SIG_IGN) {
  3111. if ((actp->sa_flags & SA_NODEFER) == 0) {
  3112. // automaticlly block the signal
  3113. sigaddset(&(actp->sa_mask), sig);
  3114. }
  3115. sa_handler_t hand;
  3116. sa_sigaction_t sa;
  3117. bool siginfo_flag_set = (actp->sa_flags & SA_SIGINFO) != 0;
  3118. // retrieve the chained handler
  3119. if (siginfo_flag_set) {
  3120. sa = actp->sa_sigaction;
  3121. } else {
  3122. hand = actp->sa_handler;
  3123. }
  3124. if ((actp->sa_flags & SA_RESETHAND) != 0) {
  3125. actp->sa_handler = SIG_DFL;
  3126. }
  3127. // try to honor the signal mask
  3128. sigset_t oset;
  3129. pthread_sigmask(SIG_SETMASK, &(actp->sa_mask), &oset);
  3130. // call into the chained handler
  3131. if (siginfo_flag_set) {
  3132. (*sa)(sig, siginfo, context);
  3133. } else {
  3134. (*hand)(sig);
  3135. }
  3136. // restore the signal mask
  3137. pthread_sigmask(SIG_SETMASK, &oset, 0);
  3138. }
  3139. // Tell jvm's signal handler the signal is taken care of.
  3140. return true;
  3141. }
  3142. bool os::Linux::chained_handler(int sig, siginfo_t* siginfo, void* context) {
  3143. bool chained = false;
  3144. // signal-chaining
  3145. if (UseSignalChaining) {
  3146. struct sigaction *actp = get_chained_signal_action(sig);
  3147. if (actp != NULL) {
  3148. chained = call_chained_handler(actp, sig, siginfo, context);
  3149. }
  3150. }
  3151. return chained;
  3152. }
  3153. struct sigaction* os::Linux::get_preinstalled_handler(int sig) {
  3154. if ((( (unsigned int)1 << sig ) & sigs) != 0) {
  3155. return &sigact[sig];
  3156. }
  3157. return NULL;
  3158. }
  3159. void os::Linux::save_preinstalled_handler(int sig, struct sigaction& oldAct) {
  3160. assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
  3161. sigact[sig] = oldAct;
  3162. sigs |= (unsigned int)1 << sig;
  3163. }
  3164. // for diagnostic
  3165. int os::Linux::sigflags[MAXSIGNUM];
  3166. int os::Linux::get_our_sigflags(int sig) {
  3167. assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
  3168. return sigflags[sig];
  3169. }
  3170. void os::Linux::set_our_sigflags(int sig, int flags) {
  3171. assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
  3172. sigflags[sig] = flags;
  3173. }
  3174. void os::Linux::set_signal_handler(int sig, bool set_installed) {
  3175. // Check for overwrite.
  3176. struct sigaction oldAct;
  3177. sigaction(sig, (struct sigaction*)NULL, &oldAct);
  3178. void* oldhand = oldAct.sa_sigaction
  3179. ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
  3180. : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
  3181. if (oldhand != CAST_FROM_FN_PTR(void*, SIG_DFL) &&
  3182. oldhand != CAST_FROM_FN_PTR(void*, SIG_IGN) &&
  3183. oldhand != CAST_FROM_FN_PTR(void*, (sa_sigaction_t)signalHandler)) {
  3184. if (AllowUserSignalHandlers || !set_installed) {
  3185. // Do not overwrite; user takes responsibility to forward to us.
  3186. return;
  3187. } else if (UseSignalChaining) {
  3188. // save the old handler in jvm
  3189. save_preinstalled_handler(sig, oldAct);
  3190. // libjsig also interposes the sigaction() call below and saves the
  3191. // old sigaction on it own.
  3192. } else {
  3193. fatal(err_msg("Encountered unexpected pre-existing sigaction handler "
  3194. "%#lx for signal %d.", (long)oldhand, sig));
  3195. }
  3196. }
  3197. struct sigaction sigAct;
  3198. sigfillset(&(sigAct.sa_mask));
  3199. sigAct.sa_handler = SIG_DFL;
  3200. if (!set_installed) {
  3201. sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
  3202. } else {
  3203. sigAct.sa_sigaction = signalHandler;
  3204. sigAct.sa_flags = SA_SIGINFO|SA_RESTART;
  3205. }
  3206. // Save flags, which are set by ours
  3207. assert(sig > 0 && sig < MAXSIGNUM, "vm signal out of expected range");
  3208. sigflags[sig] = sigAct.sa_flags;
  3209. int ret = sigaction(sig, &sigAct, &oldAct);
  3210. assert(ret == 0, "check");
  3211. void* oldhand2 = oldAct.sa_sigaction
  3212. ? CAST_FROM_FN_PTR(void*, oldAct.sa_sigaction)
  3213. : CAST_FROM_FN_PTR(void*, oldAct.sa_handler);
  3214. assert(oldhand2 == oldhand, "no concurrent signal handler installation");
  3215. }
  3216. // install signal handlers for signals that HotSpot needs to
  3217. // handle in order to support Java-level exception handling.
  3218. void os::Linux::install_signal_handlers() {
  3219. if (!signal_handlers_are_installed) {
  3220. signal_handlers_are_installed = true;
  3221. // signal-chaining
  3222. typedef void (*signal_setting_t)();
  3223. signal_setting_t begin_signal_setting = NULL;
  3224. signal_setting_t end_signal_setting = NULL;
  3225. begin_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
  3226. dlsym(RTLD_DEFAULT, "JVM_begin_signal_setting"));
  3227. if (begin_signal_setting != NULL) {
  3228. end_signal_setting = CAST_TO_FN_PTR(signal_setting_t,
  3229. dlsym(RTLD_DEFAULT, "JVM_end_signal_setting"));
  3230. get_signal_action = CAST_TO_FN_PTR(get_signal_t,
  3231. dlsym(RTLD_DEFAULT, "JVM_get_signal_action"));
  3232. libjsig_is_loaded = true;
  3233. assert(UseSignalChaining, "should enable signal-chaining");
  3234. }
  3235. if (libjsig_is_loaded) {
  3236. // Tell libjsig jvm is setting signal handlers
  3237. (*begin_signal_setting)();
  3238. }
  3239. set_signal_handler(SIGSEGV, true);
  3240. set_signal_handler(SIGPIPE, true);
  3241. set_signal_handler(SIGBUS, true);
  3242. set_signal_handler(SIGILL, true);
  3243. set_signal_handler(SIGFPE, true);
  3244. set_signal_handler(SIGXFSZ, true);
  3245. if (libjsig_is_loaded) {
  3246. // Tell libjsig jvm finishes setting signal handlers
  3247. (*end_signal_setting)();
  3248. }
  3249. // We don't activate signal checker if libjsig is in place, we trust ourselves
  3250. // and if UserSignalHandler is installed all bets are off
  3251. if (CheckJNICalls) {
  3252. if (libjsig_is_loaded) {
  3253. tty->print_cr("Info: libjsig is activated, all active signal checking is disabled");
  3254. check_signals = false;
  3255. }
  3256. if (AllowUserSignalHandlers) {
  3257. tty->print_cr("Info: AllowUserSignalHandlers is activated, all active signal checking is disabled");
  3258. check_signals = false;
  3259. }
  3260. }
  3261. }
  3262. }
  3263. // This is the fastest way to get thread cpu time on Linux.
  3264. // Returns cpu time (user+sys) for any thread, not only for current.
  3265. // POSIX compliant clocks are implemented in the kernels 2.6.16+.
  3266. // It might work on 2.6.10+ with a special kernel/glibc patch.
  3267. // For reference, please, see IEEE Std 1003.1-2004:
  3268. // http://www.unix.org/single_unix_specification
  3269. jlong os::Linux::fast_thread_cpu_time(clockid_t clockid) {
  3270. struct timespec tp;
  3271. int rc = os::Linux::clock_gettime(clockid, &tp);
  3272. assert(rc == 0, "clock_gettime is expected to return 0 code");
  3273. return (tp.tv_sec * SEC_IN_NANOSECS) + tp.tv_nsec;
  3274. }
  3275. /////
  3276. // glibc on Linux platform uses non-documented flag
  3277. // to indicate, that some special sort of signal
  3278. // trampoline is used.
  3279. // We will never set this flag, and we should
  3280. // ignore this flag in our diagnostic
  3281. #ifdef SIGNIFICANT_SIGNAL_MASK
  3282. #undef SIGNIFICANT_SIGNAL_MASK
  3283. #endif
  3284. #define SIGNIFICANT_SIGNAL_MASK (~0x04000000)
  3285. static const char* get_signal_handler_name(address handler,
  3286. char* buf, int buflen) {
  3287. int offset;
  3288. bool found = os::dll_address_to_library_name(handler, buf, buflen, &offset);
  3289. if (found) {
  3290. // skip directory names
  3291. const char *p1, *p2;
  3292. p1 = buf;
  3293. size_t len = strlen(os::file_separator());
  3294. while ((p2 = strstr(p1, os::file_separator())) != NULL) p1 = p2 + len;
  3295. jio_snprintf(buf, buflen, "%s+0x%x", p1, offset);
  3296. } else {
  3297. jio_snprintf(buf, buflen, PTR_FORMAT, handler);
  3298. }
  3299. return buf;
  3300. }
  3301. static void print_signal_handler(outputStream* st, int sig,
  3302. char* buf, size_t buflen) {
  3303. struct sigaction sa;
  3304. sigaction(sig, NULL, &sa);
  3305. // See comment for SIGNIFICANT_SIGNAL_MASK define
  3306. sa.sa_flags &= SIGNIFICANT_SIGNAL_MASK;
  3307. st->print("%s: ", os::exception_name(sig, buf, buflen));
  3308. address handler = (sa.sa_flags & SA_SIGINFO)
  3309. ? CAST_FROM_FN_PTR(address, sa.sa_sigaction)
  3310. : CAST_FROM_FN_PTR(address, sa.sa_handler);
  3311. if (handler == CAST_FROM_FN_PTR(address, SIG_DFL)) {
  3312. st->print("SIG_DFL");
  3313. } else if (handler == CAST_FROM_FN_PTR(address, SIG_IGN)) {
  3314. st->print("SIG_IGN");
  3315. } else {
  3316. st->print("[%s]", get_signal_handler_name(handler, buf, buflen));
  3317. }
  3318. st->print(", sa_mask[0]=" PTR32_FORMAT, *(uint32_t*)&sa.sa_mask);
  3319. address rh = VMError::get_resetted_sighandler(sig);
  3320. // May be, handler was resetted by VMError?
  3321. if(rh != NULL) {
  3322. handler = rh;
  3323. sa.sa_flags = VMError::get_resetted_sigflags(sig) & SIGNIFICANT_SIGNAL_MASK;
  3324. }
  3325. st->print(", sa_flags=" PTR32_FORMAT, sa.sa_flags);
  3326. // Check: is it our handler?
  3327. if(handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler) ||
  3328. handler == CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler)) {
  3329. // It is our signal handler
  3330. // check for flags, reset system-used one!
  3331. if((int)sa.sa_flags != os::Linux::get_our_sigflags(sig)) {
  3332. st->print(
  3333. ", flags was changed from " PTR32_FORMAT ", consider using jsig library",
  3334. os::Linux::get_our_sigflags(sig));
  3335. }
  3336. }
  3337. st->cr();
  3338. }
  3339. #define DO_SIGNAL_CHECK(sig) \
  3340. if (!sigismember(&check_signal_done, sig)) \
  3341. os::Linux::check_signal_handler(sig)
  3342. // This method is a periodic task to check for misbehaving JNI applications
  3343. // under CheckJNI, we can add any periodic checks here
  3344. void os::run_periodic_checks() {
  3345. if (check_signals == false) return;
  3346. // SEGV and BUS if overridden could potentially prevent
  3347. // generation of hs*.log in the event of a crash, debugging
  3348. // such a case can be very challenging, so we absolutely
  3349. // check the following for a good measure:
  3350. DO_SIGNAL_CHECK(SIGSEGV);
  3351. DO_SIGNAL_CHECK(SIGILL);
  3352. DO_SIGNAL_CHECK(SIGFPE);
  3353. DO_SIGNAL_CHECK(SIGBUS);
  3354. DO_SIGNAL_CHECK(SIGPIPE);
  3355. DO_SIGNAL_CHECK(SIGXFSZ);
  3356. // ReduceSignalUsage allows the user to override these handlers
  3357. // see comments at the very top and jvm_solaris.h
  3358. if (!ReduceSignalUsage) {
  3359. DO_SIGNAL_CHECK(SHUTDOWN1_SIGNAL);
  3360. DO_SIGNAL_CHECK(SHUTDOWN2_SIGNAL);
  3361. DO_SIGNAL_CHECK(SHUTDOWN3_SIGNAL);
  3362. DO_SIGNAL_CHECK(BREAK_SIGNAL);
  3363. }
  3364. DO_SIGNAL_CHECK(SR_signum);
  3365. DO_SIGNAL_CHECK(INTERRUPT_SIGNAL);
  3366. }
  3367. typedef int (*os_sigaction_t)(int, const struct sigaction *, struct sigaction *);
  3368. static os_sigaction_t os_sigaction = NULL;
  3369. void os::Linux::check_signal_handler(int sig) {
  3370. char buf[O_BUFLEN];
  3371. address jvmHandler = NULL;
  3372. struct sigaction act;
  3373. if (os_sigaction == NULL) {
  3374. // only trust the default sigaction, in case it has been interposed
  3375. os_sigaction = (os_sigaction_t)dlsym(RTLD_DEFAULT, "sigaction");
  3376. if (os_sigaction == NULL) return;
  3377. }
  3378. os_sigaction(sig, (struct sigaction*)NULL, &act);
  3379. act.sa_flags &= SIGNIFICANT_SIGNAL_MASK;
  3380. address thisHandler = (act.sa_flags & SA_SIGINFO)
  3381. ? CAST_FROM_FN_PTR(address, act.sa_sigaction)
  3382. : CAST_FROM_FN_PTR(address, act.sa_handler) ;
  3383. switch(sig) {
  3384. case SIGSEGV:
  3385. case SIGBUS:
  3386. case SIGFPE:
  3387. case SIGPIPE:
  3388. case SIGILL:
  3389. case SIGXFSZ:
  3390. jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)signalHandler);
  3391. break;
  3392. case SHUTDOWN1_SIGNAL:
  3393. case SHUTDOWN2_SIGNAL:
  3394. case SHUTDOWN3_SIGNAL:
  3395. case BREAK_SIGNAL:
  3396. jvmHandler = (address)user_handler();
  3397. break;
  3398. case INTERRUPT_SIGNAL:
  3399. jvmHandler = CAST_FROM_FN_PTR(address, SIG_DFL);
  3400. break;
  3401. default:
  3402. if (sig == SR_signum) {
  3403. jvmHandler = CAST_FROM_FN_PTR(address, (sa_sigaction_t)SR_handler);
  3404. } else {
  3405. return;
  3406. }
  3407. break;
  3408. }
  3409. if (thisHandler != jvmHandler) {
  3410. tty->print("Warning: %s handler ", exception_name(sig, buf, O_BUFLEN));
  3411. tty->print("expected:%s", get_signal_handler_name(jvmHandler, buf, O_BUFLEN));
  3412. tty->print_cr(" found:%s", get_signal_handler_name(thisHandler, buf, O_BUFLEN));
  3413. // No need to check this sig any longer
  3414. sigaddset(&check_signal_done, sig);
  3415. } else if(os::Linux::get_our_sigflags(sig) != 0 && (int)act.sa_flags != os::Linux::get_our_sigflags(sig)) {
  3416. tty->print("Warning: %s handler flags ", exception_name(sig, buf, O_BUFLEN));
  3417. tty->print("expected:" PTR32_FORMAT, os::Linux::get_our_sigflags(sig));
  3418. tty->print_cr(" found:" PTR32_FORMAT, act.sa_flags);
  3419. // No need to check this sig any longer
  3420. sigaddset(&check_signal_done, sig);
  3421. }
  3422. // Dump all the signal
  3423. if (sigismember(&check_signal_done, sig)) {
  3424. print_signal_handlers(tty, buf, O_BUFLEN);
  3425. }
  3426. }
  3427. extern void report_error(char* file_name, int line_no, char* title, char* format, ...);
  3428. extern bool signal_name(int signo, char* buf, size_t len);
  3429. const char* os::exception_name(int exception_code, char* buf, size_t size) {
  3430. if (0 < exception_code && exception_code <= SIGRTMAX) {
  3431. // signal
  3432. if (!signal_name(exception_code, buf, size)) {
  3433. jio_snprintf(buf, size, "SIG%d", exception_code);
  3434. }
  3435. return buf;
  3436. } else {
  3437. return NULL;
  3438. }
  3439. }
  3440. // this is called _before_ the most of global arguments have been parsed
  3441. void os::init(void) {
  3442. char dummy; /* used to get a guess on initial stack address */
  3443. // first_hrtime = gethrtime();
  3444. // With LinuxThreads the JavaMain thread pid (primordial thread)
  3445. // is different than the pid of the java launcher thread.
  3446. // So, on Linux, the launcher thread pid is passed to the VM
  3447. // via the sun.java.launcher.pid property.
  3448. // Use this property instead of getpid() if it was correctly passed.
  3449. // See bug 6351349.
  3450. pid_t java_launcher_pid = (pid_t) Arguments::sun_java_launcher_pid();
  3451. _initial_pid = (java_launcher_pid > 0) ? java_launcher_pid : getpid();
  3452. clock_tics_per_sec = sysconf(_SC_CLK_TCK);
  3453. init_random(1234567);
  3454. ThreadCritical::initialize();
  3455. Linux::set_page_size(sysconf(_SC_PAGESIZE));
  3456. if (Linux::page_size() == -1) {
  3457. fatal(err_msg("os_linux.cpp: os::init: sysconf failed (%s)",
  3458. strerror(errno)));
  3459. }
  3460. init_page_sizes((size_t) Linux::page_size());
  3461. Linux::initialize_system_info();
  3462. // main_thread points to the aboriginal thread
  3463. Linux::_main_thread = pthread_self();
  3464. Linux::clock_init();
  3465. initial_time_count = os::elapsed_counter();
  3466. pthread_mutex_init(&dl_mutex, NULL);
  3467. }
  3468. // To install functions for atexit system call
  3469. extern "C" {
  3470. static void perfMemory_exit_helper() {
  3471. perfMemory_exit();
  3472. }
  3473. }
  3474. // this is called _after_ the global arguments have been parsed
  3475. jint os::init_2(void)
  3476. {
  3477. Linux::fast_thread_clock_init();
  3478. // Allocate a single page and mark it as readable for safepoint polling
  3479. address polling_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  3480. guarantee( polling_page != MAP_FAILED, "os::init_2: failed to allocate polling page" );
  3481. os::set_polling_page( polling_page );
  3482. #ifndef PRODUCT
  3483. if(Verbose && PrintMiscellaneous)
  3484. tty->print("[SafePoint Polling address: " INTPTR_FORMAT "]\n", (intptr_t)polling_page);
  3485. #endif
  3486. if (!UseMembar) {
  3487. address mem_serialize_page = (address) ::mmap(NULL, Linux::page_size(), PROT_READ | PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  3488. guarantee( mem_serialize_page != NULL, "mmap Failed for memory serialize page");
  3489. os::set_memory_serialize_page( mem_serialize_page );
  3490. #ifndef PRODUCT
  3491. if(Verbose && PrintMiscellaneous)
  3492. tty->print("[Memory Serialize Page address: " INTPTR_FORMAT "]\n", (intptr_t)mem_serialize_page);
  3493. #endif
  3494. }
  3495. os::large_page_init();
  3496. // initialize suspend/resume support - must do this before signal_sets_init()
  3497. if (SR_initialize() != 0) {
  3498. perror("SR_initialize failed");
  3499. return JNI_ERR;
  3500. }
  3501. Linux::signal_sets_init();
  3502. Linux::install_signal_handlers();
  3503. // Check minimum allowable stack size for thread creation and to initialize
  3504. // the java system classes, including StackOverflowError - depends on page
  3505. // size. Add a page for compiler2 recursion in main thread.
  3506. // Add in 2*BytesPerWord times page size to account for VM stack during
  3507. // class initialization depending on 32 or 64 bit VM.
  3508. os::Linux::min_stack_allowed = MAX2(os::Linux::min_stack_allowed,
  3509. (size_t)(StackYellowPages+StackRedPages+StackShadowPages+
  3510. 2*BytesPerWord COMPILER2_PRESENT(+1)) * Linux::page_size());
  3511. size_t threadStackSizeInBytes = ThreadStackSize * K;
  3512. if (threadStackSizeInBytes != 0 &&
  3513. threadStackSizeInBytes < os::Linux::min_stack_allowed) {
  3514. tty->print_cr("\nThe stack size specified is too small, "
  3515. "Specify at least %dk",
  3516. os::Linux::min_stack_allowed/ K);
  3517. return JNI_ERR;
  3518. }
  3519. // Make the stack size a multiple of the page size so that
  3520. // the yellow/red zones can be guarded.
  3521. JavaThread::set_stack_size_at_create(round_to(threadStackSizeInBytes,
  3522. vm_page_size()));
  3523. Linux::capture_initial_stack(JavaThread::stack_size_at_create());
  3524. Linux::libpthread_init();
  3525. if (PrintMiscellaneous && (Verbose || WizardMode)) {
  3526. tty->print_cr("[HotSpot is running with %s, %s(%s)]\n",
  3527. Linux::glibc_version(), Linux::libpthread_version(),
  3528. Linux::is_floating_stack() ? "floating stack" : "fixed stack");
  3529. }
  3530. if (UseNUMA) {
  3531. if (!Linux::libnuma_init()) {
  3532. UseNUMA = false;
  3533. } else {
  3534. if ((Linux::numa_max_node() < 1)) {
  3535. // There's only one node(they start from 0), disable NUMA.
  3536. UseNUMA = false;
  3537. }
  3538. }
  3539. // With SHM large pages we cannot uncommit a page, so there's not way
  3540. // we can make the adaptive lgrp chunk resizing work. If the user specified
  3541. // both UseNUMA and UseLargePages (or UseSHM) on the command line - warn and
  3542. // disable adaptive resizing.
  3543. if (UseNUMA && UseLargePages && UseSHM) {
  3544. if (!FLAG_IS_DEFAULT(UseNUMA)) {
  3545. if (FLAG_IS_DEFAULT(UseLargePages) && FLAG_IS_DEFAULT(UseSHM)) {
  3546. UseLargePages = false;
  3547. } else {
  3548. warning("UseNUMA is not fully compatible with SHM large pages, disabling adaptive resizing");
  3549. UseAdaptiveSizePolicy = false;
  3550. UseAdaptiveNUMAChunkSizing = false;
  3551. }
  3552. } else {
  3553. UseNUMA = false;
  3554. }
  3555. }
  3556. if (!UseNUMA && ForceNUMA) {
  3557. UseNUMA = true;
  3558. }
  3559. }
  3560. if (MaxFDLimit) {
  3561. // set the number of file descriptors to max. print out error
  3562. // if getrlimit/setrlimit fails but continue regardless.
  3563. struct rlimit nbr_files;
  3564. int status = getrlimit(RLIMIT_NOFILE, &nbr_files);
  3565. if (status != 0) {
  3566. if (PrintMiscellaneous && (Verbose || WizardMode))
  3567. perror("os::init_2 getrlimit failed");
  3568. } else {
  3569. nbr_files.rlim_cur = nbr_files.rlim_max;
  3570. status = setrlimit(RLIMIT_NOFILE, &nbr_files);
  3571. if (status != 0) {
  3572. if (PrintMiscellaneous && (Verbose || WizardMode))
  3573. perror("os::init_2 setrlimit failed");
  3574. }
  3575. }
  3576. }
  3577. // Initialize lock used to serialize thread creation (see os::create_thread)
  3578. Linux::set_createThread_lock(new Mutex(Mutex::leaf, "createThread_lock", false));
  3579. // at-exit methods are called in the reverse order of their registration.
  3580. // atexit functions are called on return from main or as a result of a
  3581. // call to exit(3C). There can be only 32 of these functions registered
  3582. // and atexit() does not set errno.
  3583. if (PerfAllowAtExitRegistration) {
  3584. // only register atexit functions if PerfAllowAtExitRegistration is set.
  3585. // atexit functions can be delayed until process exit time, which
  3586. // can be problematic for embedded VM situations. Embedded VMs should
  3587. // call DestroyJavaVM() to assure that VM resources are released.
  3588. // note: perfMemory_exit_helper atexit function may be removed in
  3589. // the future if the appropriate cleanup code can be added to the
  3590. // VM_Exit VMOperation's doit method.
  3591. if (atexit(perfMemory_exit_helper) != 0) {
  3592. warning("os::init2 atexit(perfMemory_exit_helper) failed");
  3593. }
  3594. }
  3595. // initialize thread priority policy
  3596. prio_init();
  3597. return JNI_OK;
  3598. }
  3599. // this is called at the end of vm_initialization
  3600. void os::init_3(void) { }
  3601. // Mark the polling page as unreadable
  3602. void os::make_polling_page_unreadable(void) {
  3603. if( !guard_memory((char*)_polling_page, Linux::page_size()) )
  3604. fatal("Could not disable polling page");
  3605. };
  3606. // Mark the polling page as readable
  3607. void os::make_polling_page_readable(void) {
  3608. if( !linux_mprotect((char *)_polling_page, Linux::page_size(), PROT_READ)) {
  3609. fatal("Could not enable polling page");
  3610. }
  3611. };
  3612. int os::active_processor_count() {
  3613. // Linux doesn't yet have a (official) notion of processor sets,
  3614. // so just return the number of online processors.
  3615. int online_cpus = ::sysconf(_SC_NPROCESSORS_ONLN);
  3616. assert(online_cpus > 0 && online_cpus <= processor_count(), "sanity check");
  3617. return online_cpus;
  3618. }
  3619. bool os::distribute_processes(uint length, uint* distribution) {
  3620. // Not yet implemented.
  3621. return false;
  3622. }
  3623. bool os::bind_to_processor(uint processor_id) {
  3624. // Not yet implemented.
  3625. return false;
  3626. }
  3627. ///
  3628. // Suspends the target using the signal mechanism and then grabs the PC before
  3629. // resuming the target. Used by the flat-profiler only
  3630. ExtendedPC os::get_thread_pc(Thread* thread) {
  3631. // Make sure that it is called by the watcher for the VMThread
  3632. assert(Thread::current()->is_Watcher_thread(), "Must be watcher");
  3633. assert(thread->is_VM_thread(), "Can only be called for VMThread");
  3634. ExtendedPC epc;
  3635. OSThread* osthread = thread->osthread();
  3636. if (do_suspend(osthread)) {
  3637. if (osthread->ucontext() != NULL) {
  3638. epc = os::Linux::ucontext_get_pc(osthread->ucontext());
  3639. } else {
  3640. // NULL context is unexpected, double-check this is the VMThread
  3641. guarantee(thread->is_VM_thread(), "can only be called for VMThread");
  3642. }
  3643. do_resume(osthread);
  3644. }
  3645. // failure means pthread_kill failed for some reason - arguably this is
  3646. // a fatal problem, but such problems are ignored elsewhere
  3647. return epc;
  3648. }
  3649. int os::Linux::safe_cond_timedwait(pthread_cond_t *_cond, pthread_mutex_t *_mutex, const struct timespec *_abstime)
  3650. {
  3651. if (is_NPTL()) {
  3652. return pthread_cond_timedwait(_cond, _mutex, _abstime);
  3653. } else {
  3654. #ifndef IA64
  3655. // 6292965: LinuxThreads pthread_cond_timedwait() resets FPU control
  3656. // word back to default 64bit precision if condvar is signaled. Java
  3657. // wants 53bit precision. Save and restore current value.
  3658. int fpu = get_fpu_control_word();
  3659. #endif // IA64
  3660. int status = pthread_cond_timedwait(_cond, _mutex, _abstime);
  3661. #ifndef IA64
  3662. set_fpu_control_word(fpu);
  3663. #endif // IA64
  3664. return status;
  3665. }
  3666. }
  3667. ////////////////////////////////////////////////////////////////////////////////
  3668. // debug support
  3669. static address same_page(address x, address y) {
  3670. int page_bits = -os::vm_page_size();
  3671. if ((intptr_t(x) & page_bits) == (intptr_t(y) & page_bits))
  3672. return x;
  3673. else if (x > y)
  3674. return (address)(intptr_t(y) | ~page_bits) + 1;
  3675. else
  3676. return (address)(intptr_t(y) & page_bits);
  3677. }
  3678. bool os::find(address addr, outputStream* st) {
  3679. Dl_info dlinfo;
  3680. memset(&dlinfo, 0, sizeof(dlinfo));
  3681. if (dladdr(addr, &dlinfo)) {
  3682. st->print(PTR_FORMAT ": ", addr);
  3683. if (dlinfo.dli_sname != NULL) {
  3684. st->print("%s+%#x", dlinfo.dli_sname,
  3685. addr - (intptr_t)dlinfo.dli_saddr);
  3686. } else if (dlinfo.dli_fname) {
  3687. st->print("<offset %#x>", addr - (intptr_t)dlinfo.dli_fbase);
  3688. } else {
  3689. st->print("<absolute address>");
  3690. }
  3691. if (dlinfo.dli_fname) {
  3692. st->print(" in %s", dlinfo.dli_fname);
  3693. }
  3694. if (dlinfo.dli_fbase) {
  3695. st->print(" at " PTR_FORMAT, dlinfo.dli_fbase);
  3696. }
  3697. st->cr();
  3698. if (Verbose) {
  3699. // decode some bytes around the PC
  3700. address begin = same_page(addr-40, addr);
  3701. address end = same_page(addr+40, addr);
  3702. address lowest = (address) dlinfo.dli_sname;
  3703. if (!lowest) lowest = (address) dlinfo.dli_fbase;
  3704. if (begin < lowest) begin = lowest;
  3705. Dl_info dlinfo2;
  3706. if (dladdr(end, &dlinfo2) && dlinfo2.dli_saddr != dlinfo.dli_saddr
  3707. && end > dlinfo2.dli_saddr && dlinfo2.dli_saddr > begin)
  3708. end = (address) dlinfo2.dli_saddr;
  3709. Disassembler::decode(begin, end, st);
  3710. }
  3711. return true;
  3712. }
  3713. return false;
  3714. }
  3715. ////////////////////////////////////////////////////////////////////////////////
  3716. // misc
  3717. // This does not do anything on Linux. This is basically a hook for being
  3718. // able to use structured exception handling (thread-local exception filters)
  3719. // on, e.g., Win32.
  3720. void
  3721. os::os_exception_wrapper(java_call_t f, JavaValue* value, methodHandle* method,
  3722. JavaCallArguments* args, Thread* thread) {
  3723. f(value, method, args, thread);
  3724. }
  3725. void os::print_statistics() {
  3726. }
  3727. int os::message_box(const char* title, const char* message) {
  3728. int i;
  3729. fdStream err(defaultStream::error_fd());
  3730. for (i = 0; i < 78; i++) err.print_raw("=");
  3731. err.cr();
  3732. err.print_raw_cr(title);
  3733. for (i = 0; i < 78; i++) err.print_raw("-");
  3734. err.cr();
  3735. err.print_raw_cr(message);
  3736. for (i = 0; i < 78; i++) err.print_raw("=");
  3737. err.cr();
  3738. char buf[16];
  3739. // Prevent process from exiting upon "read error" without consuming all CPU
  3740. while (::read(0, buf, sizeof(buf)) <= 0) { ::sleep(100); }
  3741. return buf[0] == 'y' || buf[0] == 'Y';
  3742. }
  3743. int os::stat(const char *path, struct stat *sbuf) {
  3744. char pathbuf[MAX_PATH];
  3745. if (strlen(path) > MAX_PATH - 1) {
  3746. errno = ENAMETOOLONG;
  3747. return -1;
  3748. }
  3749. os::native_path(strcpy(pathbuf, path));
  3750. return ::stat(pathbuf, sbuf);
  3751. }
  3752. bool os::check_heap(bool force) {
  3753. return true;
  3754. }
  3755. int local_vsnprintf(char* buf, size_t count, const char* format, va_list args) {
  3756. return ::vsnprintf(buf, count, format, args);
  3757. }
  3758. // Is a (classpath) directory empty?
  3759. bool os::dir_is_empty(const char* path) {
  3760. DIR *dir = NULL;
  3761. struct dirent *ptr;
  3762. dir = opendir(path);
  3763. if (dir == NULL) return true;
  3764. /* Scan the directory */
  3765. bool result = true;
  3766. char buf[sizeof(struct dirent) + MAX_PATH];
  3767. while (result && (ptr = ::readdir(dir)) != NULL) {
  3768. if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0) {
  3769. result = false;
  3770. }
  3771. }
  3772. closedir(dir);
  3773. return result;
  3774. }
  3775. // This code originates from JDK's sysOpen and open64_w
  3776. // from src/solaris/hpi/src/system_md.c
  3777. #ifndef O_DELETE
  3778. #define O_DELETE 0x10000
  3779. #endif
  3780. // Open a file. Unlink the file immediately after open returns
  3781. // if the specified oflag has the O_DELETE flag set.
  3782. // O_DELETE is used only in j2se/src/share/native/java/util/zip/ZipFile.c
  3783. int os::open(const char *path, int oflag, int mode) {
  3784. if (strlen(path) > MAX_PATH - 1) {
  3785. errno = ENAMETOOLONG;
  3786. return -1;
  3787. }
  3788. int fd;
  3789. int o_delete = (oflag & O_DELETE);
  3790. oflag = oflag & ~O_DELETE;
  3791. fd = ::open64(path, oflag, mode);
  3792. if (fd == -1) return -1;
  3793. //If the open succeeded, the file might still be a directory
  3794. {
  3795. struct stat64 buf64;
  3796. int ret = ::fstat64(fd, &buf64);
  3797. int st_mode = buf64.st_mode;
  3798. if (ret != -1) {
  3799. if ((st_mode & S_IFMT) == S_IFDIR) {
  3800. errno = EISDIR;
  3801. ::close(fd);
  3802. return -1;
  3803. }
  3804. } else {
  3805. ::close(fd);
  3806. return -1;
  3807. }
  3808. }
  3809. /*
  3810. * All file descriptors that are opened in the JVM and not
  3811. * specifically destined for a subprocess should have the
  3812. * close-on-exec flag set. If we don't set it, then careless 3rd
  3813. * party native code might fork and exec without closing all
  3814. * appropriate file descriptors (e.g. as we do in closeDescriptors in
  3815. * UNIXProcess.c), and this in turn might:
  3816. *
  3817. * - cause end-of-file to fail to be detected on some file
  3818. * descriptors, resulting in mysterious hangs, or
  3819. *
  3820. * - might cause an fopen in the subprocess to fail on a system
  3821. * suffering from bug 1085341.
  3822. *
  3823. * (Yes, the default setting of the close-on-exec flag is a Unix
  3824. * design flaw)
  3825. *
  3826. * See:
  3827. * 1085341: 32-bit stdio routines should support file descriptors >255
  3828. * 4843136: (process) pipe file descriptor from Runtime.exec not being closed
  3829. * 6339493: (process) Runtime.exec does not close all file descriptors on Solaris 9
  3830. */
  3831. #ifdef FD_CLOEXEC
  3832. {
  3833. int flags = ::fcntl(fd, F_GETFD);
  3834. if (flags != -1)
  3835. ::fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
  3836. }
  3837. #endif
  3838. if (o_delete != 0) {
  3839. ::unlink(path);
  3840. }
  3841. return fd;
  3842. }
  3843. // create binary file, rewriting existing file if required
  3844. int os::create_binary_file(const char* path, bool rewrite_existing) {
  3845. int oflags = O_WRONLY | O_CREAT;
  3846. if (!rewrite_existing) {
  3847. oflags |= O_EXCL;
  3848. }
  3849. return ::open64(path, oflags, S_IREAD | S_IWRITE);
  3850. }
  3851. // return current position of file pointer
  3852. jlong os::current_file_offset(int fd) {
  3853. return (jlong)::lseek64(fd, (off64_t)0, SEEK_CUR);
  3854. }
  3855. // move file pointer to the specified offset
  3856. jlong os::seek_to_file_offset(int fd, jlong offset) {
  3857. return (jlong)::lseek64(fd, (off64_t)offset, SEEK_SET);
  3858. }
  3859. // This code originates from JDK's sysAvailable
  3860. // from src/solaris/hpi/src/native_threads/src/sys_api_td.c
  3861. int os::available(int fd, jlong *bytes) {
  3862. jlong cur, end;
  3863. int mode;
  3864. struct stat64 buf64;
  3865. if (::fstat64(fd, &buf64) >= 0) {
  3866. mode = buf64.st_mode;
  3867. if (S_ISCHR(mode) || S_ISFIFO(mode) || S_ISSOCK(mode)) {
  3868. /*
  3869. * XXX: is the following call interruptible? If so, this might
  3870. * need to go through the INTERRUPT_IO() wrapper as for other
  3871. * blocking, interruptible calls in this file.
  3872. */
  3873. int n;
  3874. if (::ioctl(fd, FIONREAD, &n) >= 0) {
  3875. *bytes = n;
  3876. return 1;
  3877. }
  3878. }
  3879. }
  3880. if ((cur = ::lseek64(fd, 0L, SEEK_CUR)) == -1) {
  3881. return 0;
  3882. } else if ((end = ::lseek64(fd, 0L, SEEK_END)) == -1) {
  3883. return 0;
  3884. } else if (::lseek64(fd, cur, SEEK_SET) == -1) {
  3885. return 0;
  3886. }
  3887. *bytes = end - cur;
  3888. return 1;
  3889. }
  3890. int os::socket_available(int fd, jint *pbytes) {
  3891. // Linux doc says EINTR not returned, unlike Solaris
  3892. int ret = ::ioctl(fd, FIONREAD, pbytes);
  3893. //%% note ioctl can return 0 when successful, JVM_SocketAvailable
  3894. // is expected to return 0 on failure and 1 on success to the jdk.
  3895. return (ret < 0) ? 0 : 1;
  3896. }
  3897. // Map a block of memory.
  3898. char* os::map_memory(int fd, const char* file_name, size_t file_offset,
  3899. char *addr, size_t bytes, bool read_only,
  3900. bool allow_exec) {
  3901. int prot;
  3902. int flags;
  3903. if (read_only) {
  3904. prot = PROT_READ;
  3905. flags = MAP_SHARED;
  3906. } else {
  3907. prot = PROT_READ | PROT_WRITE;
  3908. flags = MAP_PRIVATE;
  3909. }
  3910. if (allow_exec) {
  3911. prot |= PROT_EXEC;
  3912. }
  3913. if (addr != NULL) {
  3914. flags |= MAP_FIXED;
  3915. }
  3916. char* mapped_address = (char*)mmap(addr, (size_t)bytes, prot, flags,
  3917. fd, file_offset);
  3918. if (mapped_address == MAP_FAILED) {
  3919. return NULL;
  3920. }
  3921. return mapped_address;
  3922. }
  3923. // Remap a block of memory.
  3924. char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
  3925. char *addr, size_t bytes, bool read_only,
  3926. bool allow_exec) {
  3927. // same as map_memory() on this OS
  3928. return os::map_memory(fd, file_name, file_offset, addr, bytes, read_only,
  3929. allow_exec);
  3930. }
  3931. // Unmap a block of memory.
  3932. bool os::unmap_memory(char* addr, size_t bytes) {
  3933. return munmap(addr, bytes) == 0;
  3934. }
  3935. static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time);
  3936. static clockid_t thread_cpu_clockid(Thread* thread) {
  3937. pthread_t tid = thread->osthread()->pthread_id();
  3938. clockid_t clockid;
  3939. // Get thread clockid
  3940. int rc = os::Linux::pthread_getcpuclockid(tid, &clockid);
  3941. assert(rc == 0, "pthread_getcpuclockid is expected to return 0 code");
  3942. return clockid;
  3943. }
  3944. // current_thread_cpu_time(bool) and thread_cpu_time(Thread*, bool)
  3945. // are used by JVM M&M and JVMTI to get user+sys or user CPU time
  3946. // of a thread.
  3947. //
  3948. // current_thread_cpu_time() and thread_cpu_time(Thread*) returns
  3949. // the fast estimate available on the platform.
  3950. jlong os::current_thread_cpu_time() {
  3951. if (os::Linux::supports_fast_thread_cpu_time()) {
  3952. return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
  3953. } else {
  3954. // return user + sys since the cost is the same
  3955. return slow_thread_cpu_time(Thread::current(), true /* user + sys */);
  3956. }
  3957. }
  3958. jlong os::thread_cpu_time(Thread* thread) {
  3959. // consistent with what current_thread_cpu_time() returns
  3960. if (os::Linux::supports_fast_thread_cpu_time()) {
  3961. return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
  3962. } else {
  3963. return slow_thread_cpu_time(thread, true /* user + sys */);
  3964. }
  3965. }
  3966. jlong os::current_thread_cpu_time(bool user_sys_cpu_time) {
  3967. if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
  3968. return os::Linux::fast_thread_cpu_time(CLOCK_THREAD_CPUTIME_ID);
  3969. } else {
  3970. return slow_thread_cpu_time(Thread::current(), user_sys_cpu_time);
  3971. }
  3972. }
  3973. jlong os::thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
  3974. if (user_sys_cpu_time && os::Linux::supports_fast_thread_cpu_time()) {
  3975. return os::Linux::fast_thread_cpu_time(thread_cpu_clockid(thread));
  3976. } else {
  3977. return slow_thread_cpu_time(thread, user_sys_cpu_time);
  3978. }
  3979. }
  3980. //
  3981. // -1 on error.
  3982. //
  3983. static jlong slow_thread_cpu_time(Thread *thread, bool user_sys_cpu_time) {
  3984. static bool proc_pid_cpu_avail = true;
  3985. static bool proc_task_unchecked = true;
  3986. static const char *proc_stat_path = "/proc/%d/stat";
  3987. pid_t tid = thread->osthread()->thread_id();
  3988. int i;
  3989. char *s;
  3990. char stat[2048];
  3991. int statlen;
  3992. char proc_name[64];
  3993. int count;
  3994. long sys_time, user_time;
  3995. char string[64];
  3996. char cdummy;
  3997. int idummy;
  3998. long ldummy;
  3999. FILE *fp;
  4000. // We first try accessing /proc/<pid>/cpu since this is faster to
  4001. // process. If this file is not present (linux kernels 2.5 and above)
  4002. // then we open /proc/<pid>/stat.
  4003. if ( proc_pid_cpu_avail ) {
  4004. sprintf(proc_name, "/proc/%d/cpu", tid);
  4005. fp = fopen(proc_name, "r");
  4006. if ( fp != NULL ) {
  4007. count = fscanf( fp, "%s %lu %lu\n", string, &user_time, &sys_time);
  4008. fclose(fp);
  4009. if ( count != 3 ) return -1;
  4010. if (user_sys_cpu_time) {
  4011. return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
  4012. } else {
  4013. return (jlong)user_time * (1000000000 / clock_tics_per_sec);
  4014. }
  4015. }
  4016. else proc_pid_cpu_avail = false;
  4017. }
  4018. // The /proc/<tid>/stat aggregates per-process usage on
  4019. // new Linux kernels 2.6+ where NPTL is supported.
  4020. // The /proc/self/task/<tid>/stat still has the per-thread usage.
  4021. // See bug 6328462.
  4022. // There can be no directory /proc/self/task on kernels 2.4 with NPTL
  4023. // and possibly in some other cases, so we check its availability.
  4024. if (proc_task_unchecked && os::Linux::is_NPTL()) {
  4025. // This is executed only once
  4026. proc_task_unchecked = false;
  4027. fp = fopen("/proc/self/task", "r");
  4028. if (fp != NULL) {
  4029. proc_stat_path = "/proc/self/task/%d/stat";
  4030. fclose(fp);
  4031. }
  4032. }
  4033. sprintf(proc_name, proc_stat_path, tid);
  4034. fp = fopen(proc_name, "r");
  4035. if ( fp == NULL ) return -1;
  4036. statlen = fread(stat, 1, 2047, fp);
  4037. stat[statlen] = '\0';
  4038. fclose(fp);
  4039. // Skip pid and the command string. Note that we could be dealing with
  4040. // weird command names, e.g. user could decide to rename java launcher
  4041. // to "java 1.4.2 :)", then the stat file would look like
  4042. // 1234 (java 1.4.2 :)) R ... ...
  4043. // We don't really need to know the command string, just find the last
  4044. // occurrence of ")" and then start parsing from there. See bug 4726580.
  4045. s = strrchr(stat, ')');
  4046. i = 0;
  4047. if (s == NULL ) return -1;
  4048. // Skip blank chars
  4049. do s++; while (isspace(*s));
  4050. count = sscanf(s,"%c %d %d %d %d %d %lu %lu %lu %lu %lu %lu %lu",
  4051. &cdummy, &idummy, &idummy, &idummy, &idummy, &idummy,
  4052. &ldummy, &ldummy, &ldummy, &ldummy, &ldummy,
  4053. &user_time, &sys_time);
  4054. if ( count != 13 ) return -1;
  4055. if (user_sys_cpu_time) {
  4056. return ((jlong)sys_time + (jlong)user_time) * (1000000000 / clock_tics_per_sec);
  4057. } else {
  4058. return (jlong)user_time * (1000000000 / clock_tics_per_sec);
  4059. }
  4060. }
  4061. void os::current_thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
  4062. info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits
  4063. info_ptr->may_skip_backward = false; // elapsed time not wall time
  4064. info_ptr->may_skip_forward = false; // elapsed time not wall time
  4065. info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
  4066. }
  4067. void os::thread_cpu_time_info(jvmtiTimerInfo *info_ptr) {
  4068. info_ptr->max_value = ALL_64_BITS; // will not wrap in less than 64 bits
  4069. info_ptr->may_skip_backward = false; // elapsed time not wall time
  4070. info_ptr->may_skip_forward = false; // elapsed time not wall time
  4071. info_ptr->kind = JVMTI_TIMER_TOTAL_CPU; // user+system time is returned
  4072. }
  4073. bool os::is_thread_cpu_time_supported() {
  4074. return true;
  4075. }
  4076. // System loadavg support. Returns -1 if load average cannot be obtained.
  4077. // Linux doesn't yet have a (official) notion of processor sets,
  4078. // so just return the system wide load average.
  4079. int os::loadavg(double loadavg[], int nelem) {
  4080. return ::getloadavg(loadavg, nelem);
  4081. }
  4082. void os::pause() {
  4083. char filename[MAX_PATH];
  4084. if (PauseAtStartupFile && PauseAtStartupFile[0]) {
  4085. jio_snprintf(filename, MAX_PATH, PauseAtStartupFile);
  4086. } else {
  4087. jio_snprintf(filename, MAX_PATH, "./vm.paused.%d", current_process_id());
  4088. }
  4089. int fd = ::open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  4090. if (fd != -1) {
  4091. struct stat buf;
  4092. ::close(fd);
  4093. while (::stat(filename, &buf) == 0) {
  4094. (void)::poll(NULL, 0, 100);
  4095. }
  4096. } else {
  4097. jio_fprintf(stderr,
  4098. "Could not open pause file '%s', continuing immediately.\n", filename);
  4099. }
  4100. }
  4101. // Refer to the comments in os_solaris.cpp park-unpark.
  4102. //
  4103. // Beware -- Some versions of NPTL embody a flaw where pthread_cond_timedwait() can
  4104. // hang indefinitely. For instance NPTL 0.60 on 2.4.21-4ELsmp is vulnerable.
  4105. // For specifics regarding the bug see GLIBC BUGID 261237 :
  4106. // http://www.mail-archive.com/debian-glibc@lists.debian.org/msg10837.html.
  4107. // Briefly, pthread_cond_timedwait() calls with an expiry time that's not in the future
  4108. // will either hang or corrupt the condvar, resulting in subsequent hangs if the condvar
  4109. // is used. (The simple C test-case provided in the GLIBC bug report manifests the
  4110. // hang). The JVM is vulernable via sleep(), Object.wait(timo), LockSupport.parkNanos()
  4111. // and monitorenter when we're using 1-0 locking. All those operations may result in
  4112. // calls to pthread_cond_timedwait(). Using LD_ASSUME_KERNEL to use an older version
  4113. // of libpthread avoids the problem, but isn't practical.
  4114. //
  4115. // Possible remedies:
  4116. //
  4117. // 1. Establish a minimum relative wait time. 50 to 100 msecs seems to work.
  4118. // This is palliative and probabilistic, however. If the thread is preempted
  4119. // between the call to compute_abstime() and pthread_cond_timedwait(), more
  4120. // than the minimum period may have passed, and the abstime may be stale (in the
  4121. // past) resultin in a hang. Using this technique reduces the odds of a hang
  4122. // but the JVM is still vulnerable, particularly on heavily loaded systems.
  4123. //
  4124. // 2. Modify park-unpark to use per-thread (per ParkEvent) pipe-pairs instead
  4125. // of the usual flag-condvar-mutex idiom. The write side of the pipe is set
  4126. // NDELAY. unpark() reduces to write(), park() reduces to read() and park(timo)
  4127. // reduces to poll()+read(). This works well, but consumes 2 FDs per extant
  4128. // thread.
  4129. //
  4130. // 3. Embargo pthread_cond_timedwait() and implement a native "chron" thread
  4131. // that manages timeouts. We'd emulate pthread_cond_timedwait() by enqueuing
  4132. // a timeout request to the chron thread and then blocking via pthread_cond_wait().
  4133. // This also works well. In fact it avoids kernel-level scalability impediments
  4134. // on certain platforms that don't handle lots of active pthread_cond_timedwait()
  4135. // timers in a graceful fashion.
  4136. //
  4137. // 4. When the abstime value is in the past it appears that control returns
  4138. // correctly from pthread_cond_timedwait(), but the condvar is left corrupt.
  4139. // Subsequent timedwait/wait calls may hang indefinitely. Given that, we
  4140. // can avoid the problem by reinitializing the condvar -- by cond_destroy()
  4141. // followed by cond_init() -- after all calls to pthread_cond_timedwait().
  4142. // It may be possible to avoid reinitialization by checking the return
  4143. // value from pthread_cond_timedwait(). In addition to reinitializing the
  4144. // condvar we must establish the invariant that cond_signal() is only called
  4145. // within critical sections protected by the adjunct mutex. This prevents
  4146. // cond_signal() from "seeing" a condvar that's in the midst of being
  4147. // reinitialized or that is corrupt. Sadly, this invariant obviates the
  4148. // desirable signal-after-unlock optimization that avoids futile context switching.
  4149. //
  4150. // I'm also concerned that some versions of NTPL might allocate an auxilliary
  4151. // structure when a condvar is used or initialized. cond_destroy() would
  4152. // release the helper structure. Our reinitialize-after-timedwait fix
  4153. // put excessive stress on malloc/free and locks protecting the c-heap.
  4154. //
  4155. // We currently use (4). See the WorkAroundNTPLTimedWaitHang flag.
  4156. // It may be possible to refine (4) by checking the kernel and NTPL verisons
  4157. // and only enabling the work-around for vulnerable environments.
  4158. // utility to compute the abstime argument to timedwait:
  4159. // millis is the relative timeout time
  4160. // abstime will be the absolute timeout time
  4161. // TODO: replace compute_abstime() with unpackTime()
  4162. static struct timespec* compute_abstime(timespec* abstime, jlong millis) {
  4163. if (millis < 0) millis = 0;
  4164. struct timeval now;
  4165. int status = gettimeofday(&now, NULL);
  4166. assert(status == 0, "gettimeofday");
  4167. jlong seconds = millis / 1000;
  4168. millis %= 1000;
  4169. if (seconds > 50000000) { // see man cond_timedwait(3T)
  4170. seconds = 50000000;
  4171. }
  4172. abstime->tv_sec = now.tv_sec + seconds;
  4173. long usec = now.tv_usec + millis * 1000;
  4174. if (usec >= 1000000) {
  4175. abstime->tv_sec += 1;
  4176. usec -= 1000000;
  4177. }
  4178. abstime->tv_nsec = usec * 1000;
  4179. return abstime;
  4180. }
  4181. // Test-and-clear _Event, always leaves _Event set to 0, returns immediately.
  4182. // Conceptually TryPark() should be equivalent to park(0).
  4183. int os::PlatformEvent::TryPark() {
  4184. for (;;) {
  4185. const int v = _Event ;
  4186. guarantee ((v == 0) || (v == 1), "invariant") ;
  4187. if (Atomic::cmpxchg (0, &_Event, v) == v) return v ;
  4188. }
  4189. }
  4190. void os::PlatformEvent::park() { // AKA "down()"
  4191. // Invariant: Only the thread associated with the Event/PlatformEvent
  4192. // may call park().
  4193. // TODO: assert that _Assoc != NULL or _Assoc == Self
  4194. int v ;
  4195. for (;;) {
  4196. v = _Event ;
  4197. if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
  4198. }
  4199. guarantee (v >= 0, "invariant") ;
  4200. if (v == 0) {
  4201. // Do this the hard way by blocking ...
  4202. int status = pthread_mutex_lock(_mutex);
  4203. assert_status(status == 0, status, "mutex_lock");
  4204. guarantee (_nParked == 0, "invariant") ;
  4205. ++ _nParked ;
  4206. while (_Event < 0) {
  4207. status = pthread_cond_wait(_cond, _mutex);
  4208. // for some reason, under 2.7 lwp_cond_wait() may return ETIME ...
  4209. // Treat this the same as if the wait was interrupted
  4210. if (status == ETIME) { status = EINTR; }
  4211. assert_status(status == 0 || status == EINTR, status, "cond_wait");
  4212. }
  4213. -- _nParked ;
  4214. // In theory we could move the ST of 0 into _Event past the unlock(),
  4215. // but then we'd need a MEMBAR after the ST.
  4216. _Event = 0 ;
  4217. status = pthread_mutex_unlock(_mutex);
  4218. assert_status(status == 0, status, "mutex_unlock");
  4219. }
  4220. guarantee (_Event >= 0, "invariant") ;
  4221. }
  4222. int os::PlatformEvent::park(jlong millis) {
  4223. guarantee (_nParked == 0, "invariant") ;
  4224. int v ;
  4225. for (;;) {
  4226. v = _Event ;
  4227. if (Atomic::cmpxchg (v-1, &_Event, v) == v) break ;
  4228. }
  4229. guarantee (v >= 0, "invariant") ;
  4230. if (v != 0) return OS_OK ;
  4231. // We do this the hard way, by blocking the thread.
  4232. // Consider enforcing a minimum timeout value.
  4233. struct timespec abst;
  4234. compute_abstime(&abst, millis);
  4235. int ret = OS_TIMEOUT;
  4236. int status = pthread_mutex_lock(_mutex);
  4237. assert_status(status == 0, status, "mutex_lock");
  4238. guarantee (_nParked == 0, "invariant") ;
  4239. ++_nParked ;
  4240. // Object.wait(timo) will return because of
  4241. // (a) notification
  4242. // (b) timeout
  4243. // (c) thread.interrupt
  4244. //
  4245. // Thread.interrupt and object.notify{All} both call Event::set.
  4246. // That is, we treat thread.interrupt as a special case of notification.
  4247. // The underlying Solaris implementation, cond_timedwait, admits
  4248. // spurious/premature wakeups, but the JLS/JVM spec prevents the
  4249. // JVM from making those visible to Java code. As such, we must
  4250. // filter out spurious wakeups. We assume all ETIME returns are valid.
  4251. //
  4252. // TODO: properly differentiate simultaneous notify+interrupt.
  4253. // In that case, we should propagate the notify to another waiter.
  4254. while (_Event < 0) {
  4255. status = os::Linux::safe_cond_timedwait(_cond, _mutex, &abst);
  4256. if (status != 0 && WorkAroundNPTLTimedWaitHang) {
  4257. pthread_cond_destroy (_cond);
  4258. pthread_cond_init (_cond, NULL) ;
  4259. }
  4260. assert_status(status == 0 || status == EINTR ||
  4261. status == ETIME || status == ETIMEDOUT,
  4262. status, "cond_timedwait");
  4263. if (!FilterSpuriousWakeups) break ; // previous semantics
  4264. if (status == ETIME || status == ETIMEDOUT) break ;
  4265. // We consume and ignore EINTR and spurious wakeups.
  4266. }
  4267. --_nParked ;
  4268. if (_Event >= 0) {
  4269. ret = OS_OK;
  4270. }
  4271. _Event = 0 ;
  4272. status = pthread_mutex_unlock(_mutex);
  4273. assert_status(status == 0, status, "mutex_unlock");
  4274. assert (_nParked == 0, "invariant") ;
  4275. return ret;
  4276. }
  4277. void os::PlatformEvent::unpark() {
  4278. int v, AnyWaiters ;
  4279. for (;;) {
  4280. v = _Event ;
  4281. if (v > 0) {
  4282. // The LD of _Event could have reordered or be satisfied
  4283. // by a read-aside from this processor's write buffer.
  4284. // To avoid problems execute a barrier and then
  4285. // ratify the value.
  4286. OrderAccess::fence() ;
  4287. if (_Event == v) return ;
  4288. continue ;
  4289. }
  4290. if (Atomic::cmpxchg (v+1, &_Event, v) == v) break ;
  4291. }
  4292. if (v < 0) {
  4293. // Wait for the thread associated with the event to vacate
  4294. int status = pthread_mutex_lock(_mutex);
  4295. assert_status(status == 0, status, "mutex_lock");
  4296. AnyWaiters = _nParked ;
  4297. assert (AnyWaiters == 0 || AnyWaiters == 1, "invariant") ;
  4298. if (AnyWaiters != 0 && WorkAroundNPTLTimedWaitHang) {
  4299. AnyWaiters = 0 ;
  4300. pthread_cond_signal (_cond);
  4301. }
  4302. status = pthread_mutex_unlock(_mutex);
  4303. assert_status(status == 0, status, "mutex_unlock");
  4304. if (AnyWaiters != 0) {
  4305. status = pthread_cond_signal(_cond);
  4306. assert_status(status == 0, status, "cond_signal");
  4307. }
  4308. }
  4309. // Note that we signal() _after dropping the lock for "immortal" Events.
  4310. // This is safe and avoids a common class of futile wakeups. In rare
  4311. // circumstances this can cause a thread to return prematurely from
  4312. // cond_{timed}wait() but the spurious wakeup is benign and the victim will
  4313. // simply re-test the condition and re-park itself.
  4314. }
  4315. // JSR166
  4316. // -------------------------------------------------------
  4317. /*
  4318. * The solaris and linux implementations of park/unpark are fairly
  4319. * conservative for now, but can be improved. They currently use a
  4320. * mutex/condvar pair, plus a a count.
  4321. * Park decrements count if > 0, else does a condvar wait. Unpark
  4322. * sets count to 1 and signals condvar. Only one thread ever waits
  4323. * on the condvar. Contention seen when trying to park implies that someone
  4324. * is unparking you, so don't wait. And spurious returns are fine, so there
  4325. * is no need to track notifications.
  4326. */
  4327. #define NANOSECS_PER_SEC 1000000000
  4328. #define NANOSECS_PER_MILLISEC 1000000
  4329. #define MAX_SECS 100000000
  4330. /*
  4331. * This code is common to linux and solaris and will be moved to a
  4332. * common place in dolphin.
  4333. *
  4334. * The passed in time value is either a relative time in nanoseconds
  4335. * or an absolute time in milliseconds. Either way it has to be unpacked
  4336. * into suitable seconds and nanoseconds components and stored in the
  4337. * given timespec structure.
  4338. * Given time is a 64-bit value and the time_t used in the timespec is only
  4339. * a signed-32-bit value (except on 64-bit Linux) we have to watch for
  4340. * overflow if times way in the future are given. Further on Solaris versions
  4341. * prior to 10 there is a restriction (see cond_timedwait) that the specified
  4342. * number of seconds, in abstime, is less than current_time + 100,000,000.
  4343. * As it will be 28 years before "now + 100000000" will overflow we can
  4344. * ignore overflow and just impose a hard-limit on seconds using the value
  4345. * of "now + 100,000,000". This places a limit on the timeout of about 3.17
  4346. * years from "now".
  4347. */
  4348. static void unpackTime(timespec* absTime, bool isAbsolute, jlong time) {
  4349. assert (time > 0, "convertTime");
  4350. struct timeval now;
  4351. int status = gettimeofday(&now, NULL);
  4352. assert(status == 0, "gettimeofday");
  4353. time_t max_secs = now.tv_sec + MAX_SECS;
  4354. if (isAbsolute) {
  4355. jlong secs = time / 1000;
  4356. if (secs > max_secs) {
  4357. absTime->tv_sec = max_secs;
  4358. }
  4359. else {
  4360. absTime->tv_sec = secs;
  4361. }
  4362. absTime->tv_nsec = (time % 1000) * NANOSECS_PER_MILLISEC;
  4363. }
  4364. else {
  4365. jlong secs = time / NANOSECS_PER_SEC;
  4366. if (secs >= MAX_SECS) {
  4367. absTime->tv_sec = max_secs;
  4368. absTime->tv_nsec = 0;
  4369. }
  4370. else {
  4371. absTime->tv_sec = now.tv_sec + secs;
  4372. absTime->tv_nsec = (time % NANOSECS_PER_SEC) + now.tv_usec*1000;
  4373. if (absTime->tv_nsec >= NANOSECS_PER_SEC) {
  4374. absTime->tv_nsec -= NANOSECS_PER_SEC;
  4375. ++absTime->tv_sec; // note: this must be <= max_secs
  4376. }
  4377. }
  4378. }
  4379. assert(absTime->tv_sec >= 0, "tv_sec < 0");
  4380. assert(absTime->tv_sec <= max_secs, "tv_sec > max_secs");
  4381. assert(absTime->tv_nsec >= 0, "tv_nsec < 0");
  4382. assert(absTime->tv_nsec < NANOSECS_PER_SEC, "tv_nsec >= nanos_per_sec");
  4383. }
  4384. void Parker::park(bool isAbsolute, jlong time) {
  4385. // Optional fast-path check:
  4386. // Return immediately if a permit is available.
  4387. if (_counter > 0) {
  4388. _counter = 0 ;
  4389. OrderAccess::fence();
  4390. return ;
  4391. }
  4392. Thread* thread = Thread::current();
  4393. assert(thread->is_Java_thread(), "Must be JavaThread");
  4394. JavaThread *jt = (JavaThread *)thread;
  4395. // Optional optimization -- avoid state transitions if there's an interrupt pending.
  4396. // Check interrupt before trying to wait
  4397. if (Thread::is_interrupted(thread, false)) {
  4398. return;
  4399. }
  4400. // Next, demultiplex/decode time arguments
  4401. timespec absTime;
  4402. if (time < 0 || (isAbsolute && time == 0) ) { // don't wait at all
  4403. return;
  4404. }
  4405. if (time > 0) {
  4406. unpackTime(&absTime, isAbsolute, time);
  4407. }
  4408. // Enter safepoint region
  4409. // Beware of deadlocks such as 6317397.
  4410. // The per-thread Parker:: mutex is a classic leaf-lock.
  4411. // In particular a thread must never block on the Threads_lock while
  4412. // holding the Parker:: mutex. If safepoints are pending both the
  4413. // the ThreadBlockInVM() CTOR and DTOR may grab Threads_lock.
  4414. ThreadBlockInVM tbivm(jt);
  4415. // Don't wait if cannot get lock since interference arises from
  4416. // unblocking. Also. check interrupt before trying wait
  4417. if (Thread::is_interrupted(thread, false) || pthread_mutex_trylock(_mutex) != 0) {
  4418. return;
  4419. }
  4420. int status ;
  4421. if (_counter > 0) { // no wait needed
  4422. _counter = 0;
  4423. status = pthread_mutex_unlock(_mutex);
  4424. assert (status == 0, "invariant") ;
  4425. OrderAccess::fence();
  4426. return;
  4427. }
  4428. #ifdef ASSERT
  4429. // Don't catch signals while blocked; let the running threads have the signals.
  4430. // (This allows a debugger to break into the running thread.)
  4431. sigset_t oldsigs;
  4432. sigset_t* allowdebug_blocked = os::Linux::allowdebug_blocked_signals();
  4433. pthread_sigmask(SIG_BLOCK, allowdebug_blocked, &oldsigs);
  4434. #endif
  4435. OSThreadWaitState osts(thread->osthread(), false /* not Object.wait() */);
  4436. jt->set_suspend_equivalent();
  4437. // cleared by handle_special_suspend_equivalent_condition() or java_suspend_self()
  4438. if (time == 0) {
  4439. status = pthread_cond_wait (_cond, _mutex) ;
  4440. } else {
  4441. status = os::Linux::safe_cond_timedwait (_cond, _mutex, &absTime) ;
  4442. if (status != 0 && WorkAroundNPTLTimedWaitHang) {
  4443. pthread_cond_destroy (_cond) ;
  4444. pthread_cond_init (_cond, NULL);
  4445. }
  4446. }
  4447. assert_status(status == 0 || status == EINTR ||
  4448. status == ETIME || status == ETIMEDOUT,
  4449. status, "cond_timedwait");
  4450. #ifdef ASSERT
  4451. pthread_sigmask(SIG_SETMASK, &oldsigs, NULL);
  4452. #endif
  4453. _counter = 0 ;
  4454. status = pthread_mutex_unlock(_mutex) ;
  4455. assert_status(status == 0, status, "invariant") ;
  4456. // If externally suspended while waiting, re-suspend
  4457. if (jt->handle_special_suspend_equivalent_condition()) {
  4458. jt->java_suspend_self();
  4459. }
  4460. OrderAccess::fence();
  4461. }
  4462. void Parker::unpark() {
  4463. int s, status ;
  4464. status = pthread_mutex_lock(_mutex);
  4465. assert (status == 0, "invariant") ;
  4466. s = _counter;
  4467. _counter = 1;
  4468. if (s < 1) {
  4469. if (WorkAroundNPTLTimedWaitHang) {
  4470. status = pthread_cond_signal (_cond) ;
  4471. assert (status == 0, "invariant") ;
  4472. status = pthread_mutex_unlock(_mutex);
  4473. assert (status == 0, "invariant") ;
  4474. } else {
  4475. status = pthread_mutex_unlock(_mutex);
  4476. assert (status == 0, "invariant") ;
  4477. status = pthread_cond_signal (_cond) ;
  4478. assert (status == 0, "invariant") ;
  4479. }
  4480. } else {
  4481. pthread_mutex_unlock(_mutex);
  4482. assert (status == 0, "invariant") ;
  4483. }
  4484. }
  4485. extern char** environ;
  4486. #ifndef __NR_fork
  4487. #define __NR_fork IA32_ONLY(2) IA64_ONLY(not defined) AMD64_ONLY(57)
  4488. #endif
  4489. #ifndef __NR_execve
  4490. #define __NR_execve IA32_ONLY(11) IA64_ONLY(1033) AMD64_ONLY(59)
  4491. #endif
  4492. // Run the specified command in a separate process. Return its exit value,
  4493. // or -1 on failure (e.g. can't fork a new process).
  4494. // Unlike system(), this function can be called from signal handler. It
  4495. // doesn't block SIGINT et al.
  4496. int os::fork_and_exec(char* cmd) {
  4497. const char * argv[4] = {"sh", "-c", cmd, NULL};
  4498. // fork() in LinuxThreads/NPTL is not async-safe. It needs to run
  4499. // pthread_atfork handlers and reset pthread library. All we need is a
  4500. // separate process to execve. Make a direct syscall to fork process.
  4501. // On IA64 there's no fork syscall, we have to use fork() and hope for
  4502. // the best...
  4503. pid_t pid = NOT_IA64(syscall(__NR_fork);)
  4504. IA64_ONLY(fork();)
  4505. if (pid < 0) {
  4506. // fork failed
  4507. return -1;
  4508. } else if (pid == 0) {
  4509. // child process
  4510. // execve() in LinuxThreads will call pthread_kill_other_threads_np()
  4511. // first to kill every thread on the thread list. Because this list is
  4512. // not reset by fork() (see notes above), execve() will instead kill
  4513. // every thread in the parent process. We know this is the only thread
  4514. // in the new process, so make a system call directly.
  4515. // IA64 should use normal execve() from glibc to match the glibc fork()
  4516. // above.
  4517. NOT_IA64(syscall(__NR_execve, "/bin/sh", argv, environ);)
  4518. IA64_ONLY(execve("/bin/sh", (char* const*)argv, environ);)
  4519. // execve failed
  4520. _exit(-1);
  4521. } else {
  4522. // copied from J2SE ..._waitForProcessExit() in UNIXProcess_md.c; we don't
  4523. // care about the actual exit code, for now.
  4524. int status;
  4525. // Wait for the child process to exit. This returns immediately if
  4526. // the child has already exited. */
  4527. while (waitpid(pid, &status, 0) < 0) {
  4528. switch (errno) {
  4529. case ECHILD: return 0;
  4530. case EINTR: break;
  4531. default: return -1;
  4532. }
  4533. }
  4534. if (WIFEXITED(status)) {
  4535. // The child exited normally; get its exit code.
  4536. return WEXITSTATUS(status);
  4537. } else if (WIFSIGNALED(status)) {
  4538. // The child exited because of a signal
  4539. // The best value to return is 0x80 + signal number,
  4540. // because that is what all Unix shells do, and because
  4541. // it allows callers to distinguish between process exit and
  4542. // process death by signal.
  4543. return 0x80 + WTERMSIG(status);
  4544. } else {
  4545. // Unknown exit code; pass it through
  4546. return status;
  4547. }
  4548. }
  4549. }
  4550. // is_headless_jre()
  4551. //
  4552. // Test for the existence of libmawt in motif21 or xawt directories
  4553. // in order to report if we are running in a headless jre
  4554. //
  4555. bool os::is_headless_jre() {
  4556. struct stat statbuf;
  4557. char buf[MAXPATHLEN];
  4558. char libmawtpath[MAXPATHLEN];
  4559. const char *xawtstr = "/xawt/libmawt.so";
  4560. const char *motifstr = "/motif21/libmawt.so";
  4561. char *p;
  4562. // Get path to libjvm.so
  4563. os::jvm_path(buf, sizeof(buf));
  4564. // Get rid of libjvm.so
  4565. p = strrchr(buf, '/');
  4566. if (p == NULL) return false;
  4567. else *p = '\0';
  4568. // Get rid of client or server
  4569. p = strrchr(buf, '/');
  4570. if (p == NULL) return false;
  4571. else *p = '\0';
  4572. // check xawt/libmawt.so
  4573. strcpy(libmawtpath, buf);
  4574. strcat(libmawtpath, xawtstr);
  4575. if (::stat(libmawtpath, &statbuf) == 0) return false;
  4576. // check motif21/libmawt.so
  4577. strcpy(libmawtpath, buf);
  4578. strcat(libmawtpath, motifstr);
  4579. if (::stat(libmawtpath, &statbuf) == 0) return false;
  4580. return true;
  4581. }