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