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