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