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