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