src/share/vm/runtime/os.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/classLoader.hpp"
aoqi@0 27 #include "classfile/javaClasses.hpp"
aoqi@0 28 #include "classfile/systemDictionary.hpp"
aoqi@0 29 #include "classfile/vmSymbols.hpp"
aoqi@0 30 #include "code/icBuffer.hpp"
aoqi@0 31 #include "code/vtableStubs.hpp"
aoqi@0 32 #include "gc_implementation/shared/vmGCOperations.hpp"
aoqi@0 33 #include "interpreter/interpreter.hpp"
aoqi@0 34 #include "memory/allocation.inline.hpp"
aoqi@0 35 #include "oops/oop.inline.hpp"
aoqi@0 36 #include "prims/jvm.h"
aoqi@0 37 #include "prims/jvm_misc.hpp"
aoqi@0 38 #include "prims/privilegedStack.hpp"
aoqi@0 39 #include "runtime/arguments.hpp"
aoqi@0 40 #include "runtime/frame.inline.hpp"
aoqi@0 41 #include "runtime/interfaceSupport.hpp"
aoqi@0 42 #include "runtime/java.hpp"
aoqi@0 43 #include "runtime/javaCalls.hpp"
aoqi@0 44 #include "runtime/mutexLocker.hpp"
aoqi@0 45 #include "runtime/os.hpp"
aoqi@0 46 #include "runtime/stubRoutines.hpp"
aoqi@0 47 #include "runtime/thread.inline.hpp"
aoqi@0 48 #include "services/attachListener.hpp"
aoqi@0 49 #include "services/memTracker.hpp"
aoqi@0 50 #include "services/threadService.hpp"
aoqi@0 51 #include "utilities/defaultStream.hpp"
aoqi@0 52 #include "utilities/events.hpp"
aoqi@0 53 #ifdef TARGET_OS_FAMILY_linux
aoqi@0 54 # include "os_linux.inline.hpp"
aoqi@0 55 #endif
aoqi@0 56 #ifdef TARGET_OS_FAMILY_solaris
aoqi@0 57 # include "os_solaris.inline.hpp"
aoqi@0 58 #endif
aoqi@0 59 #ifdef TARGET_OS_FAMILY_windows
aoqi@0 60 # include "os_windows.inline.hpp"
aoqi@0 61 #endif
aoqi@0 62 #ifdef TARGET_OS_FAMILY_bsd
aoqi@0 63 # include "os_bsd.inline.hpp"
aoqi@0 64 #endif
aoqi@0 65
aoqi@0 66 # include <signal.h>
aoqi@0 67
aoqi@0 68 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 69
aoqi@0 70 OSThread* os::_starting_thread = NULL;
aoqi@0 71 address os::_polling_page = NULL;
aoqi@0 72 volatile int32_t* os::_mem_serialize_page = NULL;
aoqi@0 73 uintptr_t os::_serialize_page_mask = 0;
aoqi@0 74 long os::_rand_seed = 1;
aoqi@0 75 int os::_processor_count = 0;
aoqi@0 76 size_t os::_page_sizes[os::page_sizes_max];
aoqi@0 77
aoqi@0 78 #ifndef PRODUCT
aoqi@0 79 julong os::num_mallocs = 0; // # of calls to malloc/realloc
aoqi@0 80 julong os::alloc_bytes = 0; // # of bytes allocated
aoqi@0 81 julong os::num_frees = 0; // # of calls to free
aoqi@0 82 julong os::free_bytes = 0; // # of bytes freed
aoqi@0 83 #endif
aoqi@0 84
aoqi@0 85 static juint cur_malloc_words = 0; // current size for MallocMaxTestWords
aoqi@0 86
aoqi@0 87 void os_init_globals() {
aoqi@0 88 // Called from init_globals().
aoqi@0 89 // See Threads::create_vm() in thread.cpp, and init.cpp.
aoqi@0 90 os::init_globals();
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 // Fill in buffer with current local time as an ISO-8601 string.
aoqi@0 94 // E.g., yyyy-mm-ddThh:mm:ss-zzzz.
aoqi@0 95 // Returns buffer, or NULL if it failed.
aoqi@0 96 // This would mostly be a call to
aoqi@0 97 // strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
aoqi@0 98 // except that on Windows the %z behaves badly, so we do it ourselves.
aoqi@0 99 // Also, people wanted milliseconds on there,
aoqi@0 100 // and strftime doesn't do milliseconds.
aoqi@0 101 char* os::iso8601_time(char* buffer, size_t buffer_length) {
aoqi@0 102 // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"
aoqi@0 103 // 1 2
aoqi@0 104 // 12345678901234567890123456789
aoqi@0 105 static const char* iso8601_format =
aoqi@0 106 "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d";
aoqi@0 107 static const size_t needed_buffer = 29;
aoqi@0 108
aoqi@0 109 // Sanity check the arguments
aoqi@0 110 if (buffer == NULL) {
aoqi@0 111 assert(false, "NULL buffer");
aoqi@0 112 return NULL;
aoqi@0 113 }
aoqi@0 114 if (buffer_length < needed_buffer) {
aoqi@0 115 assert(false, "buffer_length too small");
aoqi@0 116 return NULL;
aoqi@0 117 }
aoqi@0 118 // Get the current time
aoqi@0 119 jlong milliseconds_since_19700101 = javaTimeMillis();
aoqi@0 120 const int milliseconds_per_microsecond = 1000;
aoqi@0 121 const time_t seconds_since_19700101 =
aoqi@0 122 milliseconds_since_19700101 / milliseconds_per_microsecond;
aoqi@0 123 const int milliseconds_after_second =
aoqi@0 124 milliseconds_since_19700101 % milliseconds_per_microsecond;
aoqi@0 125 // Convert the time value to a tm and timezone variable
aoqi@0 126 struct tm time_struct;
aoqi@0 127 if (localtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
aoqi@0 128 assert(false, "Failed localtime_pd");
aoqi@0 129 return NULL;
aoqi@0 130 }
aoqi@0 131 #if defined(_ALLBSD_SOURCE)
aoqi@0 132 const time_t zone = (time_t) time_struct.tm_gmtoff;
aoqi@0 133 #else
aoqi@0 134 const time_t zone = timezone;
aoqi@0 135 #endif
aoqi@0 136
aoqi@0 137 // If daylight savings time is in effect,
aoqi@0 138 // we are 1 hour East of our time zone
aoqi@0 139 const time_t seconds_per_minute = 60;
aoqi@0 140 const time_t minutes_per_hour = 60;
aoqi@0 141 const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
aoqi@0 142 time_t UTC_to_local = zone;
aoqi@0 143 if (time_struct.tm_isdst > 0) {
aoqi@0 144 UTC_to_local = UTC_to_local - seconds_per_hour;
aoqi@0 145 }
aoqi@0 146 // Compute the time zone offset.
aoqi@0 147 // localtime_pd() sets timezone to the difference (in seconds)
aoqi@0 148 // between UTC and and local time.
aoqi@0 149 // ISO 8601 says we need the difference between local time and UTC,
aoqi@0 150 // we change the sign of the localtime_pd() result.
aoqi@0 151 const time_t local_to_UTC = -(UTC_to_local);
aoqi@0 152 // Then we have to figure out if if we are ahead (+) or behind (-) UTC.
aoqi@0 153 char sign_local_to_UTC = '+';
aoqi@0 154 time_t abs_local_to_UTC = local_to_UTC;
aoqi@0 155 if (local_to_UTC < 0) {
aoqi@0 156 sign_local_to_UTC = '-';
aoqi@0 157 abs_local_to_UTC = -(abs_local_to_UTC);
aoqi@0 158 }
aoqi@0 159 // Convert time zone offset seconds to hours and minutes.
aoqi@0 160 const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour);
aoqi@0 161 const time_t zone_min =
aoqi@0 162 ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
aoqi@0 163
aoqi@0 164 // Print an ISO 8601 date and time stamp into the buffer
aoqi@0 165 const int year = 1900 + time_struct.tm_year;
aoqi@0 166 const int month = 1 + time_struct.tm_mon;
aoqi@0 167 const int printed = jio_snprintf(buffer, buffer_length, iso8601_format,
aoqi@0 168 year,
aoqi@0 169 month,
aoqi@0 170 time_struct.tm_mday,
aoqi@0 171 time_struct.tm_hour,
aoqi@0 172 time_struct.tm_min,
aoqi@0 173 time_struct.tm_sec,
aoqi@0 174 milliseconds_after_second,
aoqi@0 175 sign_local_to_UTC,
aoqi@0 176 zone_hours,
aoqi@0 177 zone_min);
aoqi@0 178 if (printed == 0) {
aoqi@0 179 assert(false, "Failed jio_printf");
aoqi@0 180 return NULL;
aoqi@0 181 }
aoqi@0 182 return buffer;
aoqi@0 183 }
aoqi@0 184
aoqi@0 185 OSReturn os::set_priority(Thread* thread, ThreadPriority p) {
aoqi@0 186 #ifdef ASSERT
aoqi@0 187 if (!(!thread->is_Java_thread() ||
aoqi@0 188 Thread::current() == thread ||
aoqi@0 189 Threads_lock->owned_by_self()
aoqi@0 190 || thread->is_Compiler_thread()
aoqi@0 191 )) {
aoqi@0 192 assert(false, "possibility of dangling Thread pointer");
aoqi@0 193 }
aoqi@0 194 #endif
aoqi@0 195
aoqi@0 196 if (p >= MinPriority && p <= MaxPriority) {
aoqi@0 197 int priority = java_to_os_priority[p];
aoqi@0 198 return set_native_priority(thread, priority);
aoqi@0 199 } else {
aoqi@0 200 assert(false, "Should not happen");
aoqi@0 201 return OS_ERR;
aoqi@0 202 }
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 // The mapping from OS priority back to Java priority may be inexact because
aoqi@0 206 // Java priorities can map M:1 with native priorities. If you want the definite
aoqi@0 207 // Java priority then use JavaThread::java_priority()
aoqi@0 208 OSReturn os::get_priority(const Thread* const thread, ThreadPriority& priority) {
aoqi@0 209 int p;
aoqi@0 210 int os_prio;
aoqi@0 211 OSReturn ret = get_native_priority(thread, &os_prio);
aoqi@0 212 if (ret != OS_OK) return ret;
aoqi@0 213
aoqi@0 214 if (java_to_os_priority[MaxPriority] > java_to_os_priority[MinPriority]) {
aoqi@0 215 for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] > os_prio; p--) ;
aoqi@0 216 } else {
aoqi@0 217 // niceness values are in reverse order
aoqi@0 218 for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] < os_prio; p--) ;
aoqi@0 219 }
aoqi@0 220 priority = (ThreadPriority)p;
aoqi@0 221 return OS_OK;
aoqi@0 222 }
aoqi@0 223
aoqi@0 224
aoqi@0 225 // --------------------- sun.misc.Signal (optional) ---------------------
aoqi@0 226
aoqi@0 227
aoqi@0 228 // SIGBREAK is sent by the keyboard to query the VM state
aoqi@0 229 #ifndef SIGBREAK
aoqi@0 230 #define SIGBREAK SIGQUIT
aoqi@0 231 #endif
aoqi@0 232
aoqi@0 233 // sigexitnum_pd is a platform-specific special signal used for terminating the Signal thread.
aoqi@0 234
aoqi@0 235
aoqi@0 236 static void signal_thread_entry(JavaThread* thread, TRAPS) {
aoqi@0 237 os::set_priority(thread, NearMaxPriority);
aoqi@0 238 while (true) {
aoqi@0 239 int sig;
aoqi@0 240 {
aoqi@0 241 // FIXME : Currently we have not decieded what should be the status
aoqi@0 242 // for this java thread blocked here. Once we decide about
aoqi@0 243 // that we should fix this.
aoqi@0 244 sig = os::signal_wait();
aoqi@0 245 }
aoqi@0 246 if (sig == os::sigexitnum_pd()) {
aoqi@0 247 // Terminate the signal thread
aoqi@0 248 return;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 switch (sig) {
aoqi@0 252 case SIGBREAK: {
aoqi@0 253 // Check if the signal is a trigger to start the Attach Listener - in that
aoqi@0 254 // case don't print stack traces.
aoqi@0 255 if (!DisableAttachMechanism && AttachListener::is_init_trigger()) {
aoqi@0 256 continue;
aoqi@0 257 }
aoqi@0 258 // Print stack traces
aoqi@0 259 // Any SIGBREAK operations added here should make sure to flush
aoqi@0 260 // the output stream (e.g. tty->flush()) after output. See 4803766.
aoqi@0 261 // Each module also prints an extra carriage return after its output.
aoqi@0 262 VM_PrintThreads op;
aoqi@0 263 VMThread::execute(&op);
aoqi@0 264 VM_PrintJNI jni_op;
aoqi@0 265 VMThread::execute(&jni_op);
aoqi@0 266 VM_FindDeadlocks op1(tty);
aoqi@0 267 VMThread::execute(&op1);
aoqi@0 268 Universe::print_heap_at_SIGBREAK();
aoqi@0 269 if (PrintClassHistogram) {
aoqi@0 270 VM_GC_HeapInspection op1(gclog_or_tty, true /* force full GC before heap inspection */);
aoqi@0 271 VMThread::execute(&op1);
aoqi@0 272 }
aoqi@0 273 if (JvmtiExport::should_post_data_dump()) {
aoqi@0 274 JvmtiExport::post_data_dump();
aoqi@0 275 }
aoqi@0 276 break;
aoqi@0 277 }
aoqi@0 278 default: {
aoqi@0 279 // Dispatch the signal to java
aoqi@0 280 HandleMark hm(THREAD);
aoqi@0 281 Klass* k = SystemDictionary::resolve_or_null(vmSymbols::sun_misc_Signal(), THREAD);
aoqi@0 282 KlassHandle klass (THREAD, k);
aoqi@0 283 if (klass.not_null()) {
aoqi@0 284 JavaValue result(T_VOID);
aoqi@0 285 JavaCallArguments args;
aoqi@0 286 args.push_int(sig);
aoqi@0 287 JavaCalls::call_static(
aoqi@0 288 &result,
aoqi@0 289 klass,
aoqi@0 290 vmSymbols::dispatch_name(),
aoqi@0 291 vmSymbols::int_void_signature(),
aoqi@0 292 &args,
aoqi@0 293 THREAD
aoqi@0 294 );
aoqi@0 295 }
aoqi@0 296 if (HAS_PENDING_EXCEPTION) {
aoqi@0 297 // tty is initialized early so we don't expect it to be null, but
aoqi@0 298 // if it is we can't risk doing an initialization that might
aoqi@0 299 // trigger additional out-of-memory conditions
aoqi@0 300 if (tty != NULL) {
aoqi@0 301 char klass_name[256];
aoqi@0 302 char tmp_sig_name[16];
aoqi@0 303 const char* sig_name = "UNKNOWN";
aoqi@0 304 InstanceKlass::cast(PENDING_EXCEPTION->klass())->
aoqi@0 305 name()->as_klass_external_name(klass_name, 256);
aoqi@0 306 if (os::exception_name(sig, tmp_sig_name, 16) != NULL)
aoqi@0 307 sig_name = tmp_sig_name;
aoqi@0 308 warning("Exception %s occurred dispatching signal %s to handler"
aoqi@0 309 "- the VM may need to be forcibly terminated",
aoqi@0 310 klass_name, sig_name );
aoqi@0 311 }
aoqi@0 312 CLEAR_PENDING_EXCEPTION;
aoqi@0 313 }
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316 }
aoqi@0 317 }
aoqi@0 318
aoqi@0 319 void os::init_before_ergo() {
aoqi@0 320 // We need to initialize large page support here because ergonomics takes some
aoqi@0 321 // decisions depending on large page support and the calculated large page size.
aoqi@0 322 large_page_init();
aoqi@0 323 }
aoqi@0 324
aoqi@0 325 void os::signal_init() {
aoqi@0 326 if (!ReduceSignalUsage) {
aoqi@0 327 // Setup JavaThread for processing signals
aoqi@0 328 EXCEPTION_MARK;
aoqi@0 329 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_Thread(), true, CHECK);
aoqi@0 330 instanceKlassHandle klass (THREAD, k);
aoqi@0 331 instanceHandle thread_oop = klass->allocate_instance_handle(CHECK);
aoqi@0 332
aoqi@0 333 const char thread_name[] = "Signal Dispatcher";
aoqi@0 334 Handle string = java_lang_String::create_from_str(thread_name, CHECK);
aoqi@0 335
aoqi@0 336 // Initialize thread_oop to put it into the system threadGroup
aoqi@0 337 Handle thread_group (THREAD, Universe::system_thread_group());
aoqi@0 338 JavaValue result(T_VOID);
aoqi@0 339 JavaCalls::call_special(&result, thread_oop,
aoqi@0 340 klass,
aoqi@0 341 vmSymbols::object_initializer_name(),
aoqi@0 342 vmSymbols::threadgroup_string_void_signature(),
aoqi@0 343 thread_group,
aoqi@0 344 string,
aoqi@0 345 CHECK);
aoqi@0 346
aoqi@0 347 KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass());
aoqi@0 348 JavaCalls::call_special(&result,
aoqi@0 349 thread_group,
aoqi@0 350 group,
aoqi@0 351 vmSymbols::add_method_name(),
aoqi@0 352 vmSymbols::thread_void_signature(),
aoqi@0 353 thread_oop, // ARG 1
aoqi@0 354 CHECK);
aoqi@0 355
aoqi@0 356 os::signal_init_pd();
aoqi@0 357
aoqi@0 358 { MutexLocker mu(Threads_lock);
aoqi@0 359 JavaThread* signal_thread = new JavaThread(&signal_thread_entry);
aoqi@0 360
aoqi@0 361 // At this point it may be possible that no osthread was created for the
aoqi@0 362 // JavaThread due to lack of memory. We would have to throw an exception
aoqi@0 363 // in that case. However, since this must work and we do not allow
aoqi@0 364 // exceptions anyway, check and abort if this fails.
aoqi@0 365 if (signal_thread == NULL || signal_thread->osthread() == NULL) {
aoqi@0 366 vm_exit_during_initialization("java.lang.OutOfMemoryError",
aoqi@0 367 "unable to create new native thread");
aoqi@0 368 }
aoqi@0 369
aoqi@0 370 java_lang_Thread::set_thread(thread_oop(), signal_thread);
aoqi@0 371 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
aoqi@0 372 java_lang_Thread::set_daemon(thread_oop());
aoqi@0 373
aoqi@0 374 signal_thread->set_threadObj(thread_oop());
aoqi@0 375 Threads::add(signal_thread);
aoqi@0 376 Thread::start(signal_thread);
aoqi@0 377 }
aoqi@0 378 // Handle ^BREAK
aoqi@0 379 os::signal(SIGBREAK, os::user_handler());
aoqi@0 380 }
aoqi@0 381 }
aoqi@0 382
aoqi@0 383
aoqi@0 384 void os::terminate_signal_thread() {
aoqi@0 385 if (!ReduceSignalUsage)
aoqi@0 386 signal_notify(sigexitnum_pd());
aoqi@0 387 }
aoqi@0 388
aoqi@0 389
aoqi@0 390 // --------------------- loading libraries ---------------------
aoqi@0 391
aoqi@0 392 typedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *);
aoqi@0 393 extern struct JavaVM_ main_vm;
aoqi@0 394
aoqi@0 395 static void* _native_java_library = NULL;
aoqi@0 396
aoqi@0 397 void* os::native_java_library() {
aoqi@0 398 if (_native_java_library == NULL) {
aoqi@0 399 char buffer[JVM_MAXPATHLEN];
aoqi@0 400 char ebuf[1024];
aoqi@0 401
aoqi@0 402 // Try to load verify dll first. In 1.3 java dll depends on it and is not
aoqi@0 403 // always able to find it when the loading executable is outside the JDK.
aoqi@0 404 // In order to keep working with 1.2 we ignore any loading errors.
aoqi@0 405 if (dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
aoqi@0 406 "verify")) {
aoqi@0 407 dll_load(buffer, ebuf, sizeof(ebuf));
aoqi@0 408 }
aoqi@0 409
aoqi@0 410 // Load java dll
aoqi@0 411 if (dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
aoqi@0 412 "java")) {
aoqi@0 413 _native_java_library = dll_load(buffer, ebuf, sizeof(ebuf));
aoqi@0 414 }
aoqi@0 415 if (_native_java_library == NULL) {
aoqi@0 416 vm_exit_during_initialization("Unable to load native library", ebuf);
aoqi@0 417 }
aoqi@0 418
aoqi@0 419 #if defined(__OpenBSD__)
aoqi@0 420 // Work-around OpenBSD's lack of $ORIGIN support by pre-loading libnet.so
aoqi@0 421 // ignore errors
aoqi@0 422 if (dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(),
aoqi@0 423 "net")) {
aoqi@0 424 dll_load(buffer, ebuf, sizeof(ebuf));
aoqi@0 425 }
aoqi@0 426 #endif
aoqi@0 427 }
aoqi@0 428 static jboolean onLoaded = JNI_FALSE;
aoqi@0 429 if (onLoaded) {
aoqi@0 430 // We may have to wait to fire OnLoad until TLS is initialized.
aoqi@0 431 if (ThreadLocalStorage::is_initialized()) {
aoqi@0 432 // The JNI_OnLoad handling is normally done by method load in
aoqi@0 433 // java.lang.ClassLoader$NativeLibrary, but the VM loads the base library
aoqi@0 434 // explicitly so we have to check for JNI_OnLoad as well
aoqi@0 435 const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS;
aoqi@0 436 JNI_OnLoad_t JNI_OnLoad = CAST_TO_FN_PTR(
aoqi@0 437 JNI_OnLoad_t, dll_lookup(_native_java_library, onLoadSymbols[0]));
aoqi@0 438 if (JNI_OnLoad != NULL) {
aoqi@0 439 JavaThread* thread = JavaThread::current();
aoqi@0 440 ThreadToNativeFromVM ttn(thread);
aoqi@0 441 HandleMark hm(thread);
aoqi@0 442 jint ver = (*JNI_OnLoad)(&main_vm, NULL);
aoqi@0 443 onLoaded = JNI_TRUE;
aoqi@0 444 if (!Threads::is_supported_jni_version_including_1_1(ver)) {
aoqi@0 445 vm_exit_during_initialization("Unsupported JNI version");
aoqi@0 446 }
aoqi@0 447 }
aoqi@0 448 }
aoqi@0 449 }
aoqi@0 450 return _native_java_library;
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 /*
aoqi@0 454 * Support for finding Agent_On(Un)Load/Attach<_lib_name> if it exists.
aoqi@0 455 * If check_lib == true then we are looking for an
aoqi@0 456 * Agent_OnLoad_lib_name or Agent_OnAttach_lib_name function to determine if
aoqi@0 457 * this library is statically linked into the image.
aoqi@0 458 * If check_lib == false then we will look for the appropriate symbol in the
aoqi@0 459 * executable if agent_lib->is_static_lib() == true or in the shared library
aoqi@0 460 * referenced by 'handle'.
aoqi@0 461 */
aoqi@0 462 void* os::find_agent_function(AgentLibrary *agent_lib, bool check_lib,
aoqi@0 463 const char *syms[], size_t syms_len) {
aoqi@0 464 assert(agent_lib != NULL, "sanity check");
aoqi@0 465 const char *lib_name;
aoqi@0 466 void *handle = agent_lib->os_lib();
aoqi@0 467 void *entryName = NULL;
aoqi@0 468 char *agent_function_name;
aoqi@0 469 size_t i;
aoqi@0 470
aoqi@0 471 // If checking then use the agent name otherwise test is_static_lib() to
aoqi@0 472 // see how to process this lookup
aoqi@0 473 lib_name = ((check_lib || agent_lib->is_static_lib()) ? agent_lib->name() : NULL);
aoqi@0 474 for (i = 0; i < syms_len; i++) {
aoqi@0 475 agent_function_name = build_agent_function_name(syms[i], lib_name, agent_lib->is_absolute_path());
aoqi@0 476 if (agent_function_name == NULL) {
aoqi@0 477 break;
aoqi@0 478 }
aoqi@0 479 entryName = dll_lookup(handle, agent_function_name);
aoqi@0 480 FREE_C_HEAP_ARRAY(char, agent_function_name, mtThread);
aoqi@0 481 if (entryName != NULL) {
aoqi@0 482 break;
aoqi@0 483 }
aoqi@0 484 }
aoqi@0 485 return entryName;
aoqi@0 486 }
aoqi@0 487
aoqi@0 488 // See if the passed in agent is statically linked into the VM image.
aoqi@0 489 bool os::find_builtin_agent(AgentLibrary *agent_lib, const char *syms[],
aoqi@0 490 size_t syms_len) {
aoqi@0 491 void *ret;
aoqi@0 492 void *proc_handle;
aoqi@0 493 void *save_handle;
aoqi@0 494
aoqi@0 495 assert(agent_lib != NULL, "sanity check");
aoqi@0 496 if (agent_lib->name() == NULL) {
aoqi@0 497 return false;
aoqi@0 498 }
aoqi@0 499 proc_handle = get_default_process_handle();
aoqi@0 500 // Check for Agent_OnLoad/Attach_lib_name function
aoqi@0 501 save_handle = agent_lib->os_lib();
aoqi@0 502 // We want to look in this process' symbol table.
aoqi@0 503 agent_lib->set_os_lib(proc_handle);
aoqi@0 504 ret = find_agent_function(agent_lib, true, syms, syms_len);
aoqi@0 505 if (ret != NULL) {
aoqi@0 506 // Found an entry point like Agent_OnLoad_lib_name so we have a static agent
aoqi@0 507 agent_lib->set_valid();
aoqi@0 508 agent_lib->set_static_lib(true);
aoqi@0 509 return true;
aoqi@0 510 }
aoqi@0 511 agent_lib->set_os_lib(save_handle);
aoqi@0 512 return false;
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 // --------------------- heap allocation utilities ---------------------
aoqi@0 516
aoqi@0 517 char *os::strdup(const char *str, MEMFLAGS flags) {
aoqi@0 518 size_t size = strlen(str);
aoqi@0 519 char *dup_str = (char *)malloc(size + 1, flags);
aoqi@0 520 if (dup_str == NULL) return NULL;
aoqi@0 521 strcpy(dup_str, str);
aoqi@0 522 return dup_str;
aoqi@0 523 }
aoqi@0 524
aoqi@0 525
aoqi@0 526
aoqi@0 527 #ifdef ASSERT
aoqi@0 528 #define space_before (MallocCushion + sizeof(double))
aoqi@0 529 #define space_after MallocCushion
aoqi@0 530 #define size_addr_from_base(p) (size_t*)(p + space_before - sizeof(size_t))
aoqi@0 531 #define size_addr_from_obj(p) ((size_t*)p - 1)
aoqi@0 532 // MallocCushion: size of extra cushion allocated around objects with +UseMallocOnly
aoqi@0 533 // NB: cannot be debug variable, because these aren't set from the command line until
aoqi@0 534 // *after* the first few allocs already happened
aoqi@0 535 #define MallocCushion 16
aoqi@0 536 #else
aoqi@0 537 #define space_before 0
aoqi@0 538 #define space_after 0
aoqi@0 539 #define size_addr_from_base(p) should not use w/o ASSERT
aoqi@0 540 #define size_addr_from_obj(p) should not use w/o ASSERT
aoqi@0 541 #define MallocCushion 0
aoqi@0 542 #endif
aoqi@0 543 #define paranoid 0 /* only set to 1 if you suspect checking code has bug */
aoqi@0 544
aoqi@0 545 #ifdef ASSERT
aoqi@0 546 inline size_t get_size(void* obj) {
aoqi@0 547 size_t size = *size_addr_from_obj(obj);
aoqi@0 548 if (size < 0) {
aoqi@0 549 fatal(err_msg("free: size field of object #" PTR_FORMAT " was overwritten ("
aoqi@0 550 SIZE_FORMAT ")", obj, size));
aoqi@0 551 }
aoqi@0 552 return size;
aoqi@0 553 }
aoqi@0 554
aoqi@0 555 u_char* find_cushion_backwards(u_char* start) {
aoqi@0 556 u_char* p = start;
aoqi@0 557 while (p[ 0] != badResourceValue || p[-1] != badResourceValue ||
aoqi@0 558 p[-2] != badResourceValue || p[-3] != badResourceValue) p--;
aoqi@0 559 // ok, we have four consecutive marker bytes; find start
aoqi@0 560 u_char* q = p - 4;
aoqi@0 561 while (*q == badResourceValue) q--;
aoqi@0 562 return q + 1;
aoqi@0 563 }
aoqi@0 564
aoqi@0 565 u_char* find_cushion_forwards(u_char* start) {
aoqi@0 566 u_char* p = start;
aoqi@0 567 while (p[0] != badResourceValue || p[1] != badResourceValue ||
aoqi@0 568 p[2] != badResourceValue || p[3] != badResourceValue) p++;
aoqi@0 569 // ok, we have four consecutive marker bytes; find end of cushion
aoqi@0 570 u_char* q = p + 4;
aoqi@0 571 while (*q == badResourceValue) q++;
aoqi@0 572 return q - MallocCushion;
aoqi@0 573 }
aoqi@0 574
aoqi@0 575 void print_neighbor_blocks(void* ptr) {
aoqi@0 576 // find block allocated before ptr (not entirely crash-proof)
aoqi@0 577 if (MallocCushion < 4) {
aoqi@0 578 tty->print_cr("### cannot find previous block (MallocCushion < 4)");
aoqi@0 579 return;
aoqi@0 580 }
aoqi@0 581 u_char* start_of_this_block = (u_char*)ptr - space_before;
aoqi@0 582 u_char* end_of_prev_block_data = start_of_this_block - space_after -1;
aoqi@0 583 // look for cushion in front of prev. block
aoqi@0 584 u_char* start_of_prev_block = find_cushion_backwards(end_of_prev_block_data);
aoqi@0 585 ptrdiff_t size = *size_addr_from_base(start_of_prev_block);
aoqi@0 586 u_char* obj = start_of_prev_block + space_before;
aoqi@0 587 if (size <= 0 ) {
aoqi@0 588 // start is bad; mayhave been confused by OS data inbetween objects
aoqi@0 589 // search one more backwards
aoqi@0 590 start_of_prev_block = find_cushion_backwards(start_of_prev_block);
aoqi@0 591 size = *size_addr_from_base(start_of_prev_block);
aoqi@0 592 obj = start_of_prev_block + space_before;
aoqi@0 593 }
aoqi@0 594
aoqi@0 595 if (start_of_prev_block + space_before + size + space_after == start_of_this_block) {
aoqi@0 596 tty->print_cr("### previous object: " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", obj, size);
aoqi@0 597 } else {
aoqi@0 598 tty->print_cr("### previous object (not sure if correct): " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", obj, size);
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 // now find successor block
aoqi@0 602 u_char* start_of_next_block = (u_char*)ptr + *size_addr_from_obj(ptr) + space_after;
aoqi@0 603 start_of_next_block = find_cushion_forwards(start_of_next_block);
aoqi@0 604 u_char* next_obj = start_of_next_block + space_before;
aoqi@0 605 ptrdiff_t next_size = *size_addr_from_base(start_of_next_block);
aoqi@0 606 if (start_of_next_block[0] == badResourceValue &&
aoqi@0 607 start_of_next_block[1] == badResourceValue &&
aoqi@0 608 start_of_next_block[2] == badResourceValue &&
aoqi@0 609 start_of_next_block[3] == badResourceValue) {
aoqi@0 610 tty->print_cr("### next object: " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", next_obj, next_size);
aoqi@0 611 } else {
aoqi@0 612 tty->print_cr("### next object (not sure if correct): " PTR_FORMAT " (" SSIZE_FORMAT " bytes)", next_obj, next_size);
aoqi@0 613 }
aoqi@0 614 }
aoqi@0 615
aoqi@0 616
aoqi@0 617 void report_heap_error(void* memblock, void* bad, const char* where) {
aoqi@0 618 tty->print_cr("## nof_mallocs = " UINT64_FORMAT ", nof_frees = " UINT64_FORMAT, os::num_mallocs, os::num_frees);
aoqi@0 619 tty->print_cr("## memory stomp: byte at " PTR_FORMAT " %s object " PTR_FORMAT, bad, where, memblock);
aoqi@0 620 print_neighbor_blocks(memblock);
aoqi@0 621 fatal("memory stomping error");
aoqi@0 622 }
aoqi@0 623
aoqi@0 624 void verify_block(void* memblock) {
aoqi@0 625 size_t size = get_size(memblock);
aoqi@0 626 if (MallocCushion) {
aoqi@0 627 u_char* ptr = (u_char*)memblock - space_before;
aoqi@0 628 for (int i = 0; i < MallocCushion; i++) {
aoqi@0 629 if (ptr[i] != badResourceValue) {
aoqi@0 630 report_heap_error(memblock, ptr+i, "in front of");
aoqi@0 631 }
aoqi@0 632 }
aoqi@0 633 u_char* end = (u_char*)memblock + size + space_after;
aoqi@0 634 for (int j = -MallocCushion; j < 0; j++) {
aoqi@0 635 if (end[j] != badResourceValue) {
aoqi@0 636 report_heap_error(memblock, end+j, "after");
aoqi@0 637 }
aoqi@0 638 }
aoqi@0 639 }
aoqi@0 640 }
aoqi@0 641 #endif
aoqi@0 642
aoqi@0 643 //
aoqi@0 644 // This function supports testing of the malloc out of memory
aoqi@0 645 // condition without really running the system out of memory.
aoqi@0 646 //
aoqi@0 647 static u_char* testMalloc(size_t alloc_size) {
aoqi@0 648 assert(MallocMaxTestWords > 0, "sanity check");
aoqi@0 649
aoqi@0 650 if ((cur_malloc_words + (alloc_size / BytesPerWord)) > MallocMaxTestWords) {
aoqi@0 651 return NULL;
aoqi@0 652 }
aoqi@0 653
aoqi@0 654 u_char* ptr = (u_char*)::malloc(alloc_size);
aoqi@0 655
aoqi@0 656 if (ptr != NULL) {
aoqi@0 657 Atomic::add(((jint) (alloc_size / BytesPerWord)),
aoqi@0 658 (volatile jint *) &cur_malloc_words);
aoqi@0 659 }
aoqi@0 660 return ptr;
aoqi@0 661 }
aoqi@0 662
aoqi@0 663 void* os::malloc(size_t size, MEMFLAGS memflags, address caller) {
aoqi@0 664 NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
aoqi@0 665 NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
aoqi@0 666
aoqi@0 667 #ifdef ASSERT
aoqi@0 668 // checking for the WatcherThread and crash_protection first
aoqi@0 669 // since os::malloc can be called when the libjvm.{dll,so} is
aoqi@0 670 // first loaded and we don't have a thread yet.
aoqi@0 671 // try to find the thread after we see that the watcher thread
aoqi@0 672 // exists and has crash protection.
aoqi@0 673 WatcherThread *wt = WatcherThread::watcher_thread();
aoqi@0 674 if (wt != NULL && wt->has_crash_protection()) {
aoqi@0 675 Thread* thread = ThreadLocalStorage::get_thread_slow();
aoqi@0 676 if (thread == wt) {
aoqi@0 677 assert(!wt->has_crash_protection(),
aoqi@0 678 "Can't malloc with crash protection from WatcherThread");
aoqi@0 679 }
aoqi@0 680 }
aoqi@0 681 #endif
aoqi@0 682
aoqi@0 683 if (size == 0) {
aoqi@0 684 // return a valid pointer if size is zero
aoqi@0 685 // if NULL is returned the calling functions assume out of memory.
aoqi@0 686 size = 1;
aoqi@0 687 }
aoqi@0 688
aoqi@0 689 const size_t alloc_size = size + space_before + space_after;
aoqi@0 690
aoqi@0 691 if (size > alloc_size) { // Check for rollover.
aoqi@0 692 return NULL;
aoqi@0 693 }
aoqi@0 694
aoqi@0 695 NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
aoqi@0 696
aoqi@0 697 u_char* ptr;
aoqi@0 698
aoqi@0 699 if (MallocMaxTestWords > 0) {
aoqi@0 700 ptr = testMalloc(alloc_size);
aoqi@0 701 } else {
aoqi@0 702 ptr = (u_char*)::malloc(alloc_size);
aoqi@0 703 }
aoqi@0 704
aoqi@0 705 #ifdef ASSERT
aoqi@0 706 if (ptr == NULL) return NULL;
aoqi@0 707 if (MallocCushion) {
aoqi@0 708 for (u_char* p = ptr; p < ptr + MallocCushion; p++) *p = (u_char)badResourceValue;
aoqi@0 709 u_char* end = ptr + space_before + size;
aoqi@0 710 for (u_char* pq = ptr+MallocCushion; pq < end; pq++) *pq = (u_char)uninitBlockPad;
aoqi@0 711 for (u_char* q = end; q < end + MallocCushion; q++) *q = (u_char)badResourceValue;
aoqi@0 712 }
aoqi@0 713 // put size just before data
aoqi@0 714 *size_addr_from_base(ptr) = size;
aoqi@0 715 #endif
aoqi@0 716 u_char* memblock = ptr + space_before;
aoqi@0 717 if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
aoqi@0 718 tty->print_cr("os::malloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, memblock);
aoqi@0 719 breakpoint();
aoqi@0 720 }
aoqi@0 721 debug_only(if (paranoid) verify_block(memblock));
aoqi@0 722 if (PrintMalloc && tty != NULL) tty->print_cr("os::malloc " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, memblock);
aoqi@0 723
aoqi@0 724 // we do not track MallocCushion memory
aoqi@0 725 MemTracker::record_malloc((address)memblock, size, memflags, caller == 0 ? CALLER_PC : caller);
aoqi@0 726
aoqi@0 727 return memblock;
aoqi@0 728 }
aoqi@0 729
aoqi@0 730
aoqi@0 731 void* os::realloc(void *memblock, size_t size, MEMFLAGS memflags, address caller) {
aoqi@0 732 #ifndef ASSERT
aoqi@0 733 NOT_PRODUCT(inc_stat_counter(&num_mallocs, 1));
aoqi@0 734 NOT_PRODUCT(inc_stat_counter(&alloc_bytes, size));
aoqi@0 735 MemTracker::Tracker tkr = MemTracker::get_realloc_tracker();
aoqi@0 736 void* ptr = ::realloc(memblock, size);
aoqi@0 737 if (ptr != NULL) {
aoqi@0 738 tkr.record((address)memblock, (address)ptr, size, memflags,
aoqi@0 739 caller == 0 ? CALLER_PC : caller);
aoqi@0 740 } else {
aoqi@0 741 tkr.discard();
aoqi@0 742 }
aoqi@0 743 return ptr;
aoqi@0 744 #else
aoqi@0 745 if (memblock == NULL) {
aoqi@0 746 return malloc(size, memflags, (caller == 0 ? CALLER_PC : caller));
aoqi@0 747 }
aoqi@0 748 if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
aoqi@0 749 tty->print_cr("os::realloc caught " PTR_FORMAT, memblock);
aoqi@0 750 breakpoint();
aoqi@0 751 }
aoqi@0 752 verify_block(memblock);
aoqi@0 753 NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
aoqi@0 754 if (size == 0) return NULL;
aoqi@0 755 // always move the block
aoqi@0 756 void* ptr = malloc(size, memflags, caller == 0 ? CALLER_PC : caller);
aoqi@0 757 if (PrintMalloc) tty->print_cr("os::remalloc " SIZE_FORMAT " bytes, " PTR_FORMAT " --> " PTR_FORMAT, size, memblock, ptr);
aoqi@0 758 // Copy to new memory if malloc didn't fail
aoqi@0 759 if ( ptr != NULL ) {
aoqi@0 760 memcpy(ptr, memblock, MIN2(size, get_size(memblock)));
aoqi@0 761 if (paranoid) verify_block(ptr);
aoqi@0 762 if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) {
aoqi@0 763 tty->print_cr("os::realloc caught, " SIZE_FORMAT " bytes --> " PTR_FORMAT, size, ptr);
aoqi@0 764 breakpoint();
aoqi@0 765 }
aoqi@0 766 free(memblock);
aoqi@0 767 }
aoqi@0 768 return ptr;
aoqi@0 769 #endif
aoqi@0 770 }
aoqi@0 771
aoqi@0 772
aoqi@0 773 void os::free(void *memblock, MEMFLAGS memflags) {
aoqi@0 774 NOT_PRODUCT(inc_stat_counter(&num_frees, 1));
aoqi@0 775 #ifdef ASSERT
aoqi@0 776 if (memblock == NULL) return;
aoqi@0 777 if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) {
aoqi@0 778 if (tty != NULL) tty->print_cr("os::free caught " PTR_FORMAT, memblock);
aoqi@0 779 breakpoint();
aoqi@0 780 }
aoqi@0 781 verify_block(memblock);
aoqi@0 782 NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap());
aoqi@0 783 // Added by detlefs.
aoqi@0 784 if (MallocCushion) {
aoqi@0 785 u_char* ptr = (u_char*)memblock - space_before;
aoqi@0 786 for (u_char* p = ptr; p < ptr + MallocCushion; p++) {
aoqi@0 787 guarantee(*p == badResourceValue,
aoqi@0 788 "Thing freed should be malloc result.");
aoqi@0 789 *p = (u_char)freeBlockPad;
aoqi@0 790 }
aoqi@0 791 size_t size = get_size(memblock);
aoqi@0 792 inc_stat_counter(&free_bytes, size);
aoqi@0 793 u_char* end = ptr + space_before + size;
aoqi@0 794 for (u_char* q = end; q < end + MallocCushion; q++) {
aoqi@0 795 guarantee(*q == badResourceValue,
aoqi@0 796 "Thing freed should be malloc result.");
aoqi@0 797 *q = (u_char)freeBlockPad;
aoqi@0 798 }
aoqi@0 799 if (PrintMalloc && tty != NULL)
aoqi@0 800 fprintf(stderr, "os::free " SIZE_FORMAT " bytes --> " PTR_FORMAT "\n", size, (uintptr_t)memblock);
aoqi@0 801 } else if (PrintMalloc && tty != NULL) {
aoqi@0 802 // tty->print_cr("os::free %p", memblock);
aoqi@0 803 fprintf(stderr, "os::free " PTR_FORMAT "\n", (uintptr_t)memblock);
aoqi@0 804 }
aoqi@0 805 #endif
aoqi@0 806 MemTracker::record_free((address)memblock, memflags);
aoqi@0 807
aoqi@0 808 ::free((char*)memblock - space_before);
aoqi@0 809 }
aoqi@0 810
aoqi@0 811 void os::init_random(long initval) {
aoqi@0 812 _rand_seed = initval;
aoqi@0 813 }
aoqi@0 814
aoqi@0 815
aoqi@0 816 long os::random() {
aoqi@0 817 /* standard, well-known linear congruential random generator with
aoqi@0 818 * next_rand = (16807*seed) mod (2**31-1)
aoqi@0 819 * see
aoqi@0 820 * (1) "Random Number Generators: Good Ones Are Hard to Find",
aoqi@0 821 * S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988),
aoqi@0 822 * (2) "Two Fast Implementations of the 'Minimal Standard' Random
aoqi@0 823 * Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88.
aoqi@0 824 */
aoqi@0 825 const long a = 16807;
aoqi@0 826 const unsigned long m = 2147483647;
aoqi@0 827 const long q = m / a; assert(q == 127773, "weird math");
aoqi@0 828 const long r = m % a; assert(r == 2836, "weird math");
aoqi@0 829
aoqi@0 830 // compute az=2^31p+q
aoqi@0 831 unsigned long lo = a * (long)(_rand_seed & 0xFFFF);
aoqi@0 832 unsigned long hi = a * (long)((unsigned long)_rand_seed >> 16);
aoqi@0 833 lo += (hi & 0x7FFF) << 16;
aoqi@0 834
aoqi@0 835 // if q overflowed, ignore the overflow and increment q
aoqi@0 836 if (lo > m) {
aoqi@0 837 lo &= m;
aoqi@0 838 ++lo;
aoqi@0 839 }
aoqi@0 840 lo += hi >> 15;
aoqi@0 841
aoqi@0 842 // if (p+q) overflowed, ignore the overflow and increment (p+q)
aoqi@0 843 if (lo > m) {
aoqi@0 844 lo &= m;
aoqi@0 845 ++lo;
aoqi@0 846 }
aoqi@0 847 return (_rand_seed = lo);
aoqi@0 848 }
aoqi@0 849
aoqi@0 850 // The INITIALIZED state is distinguished from the SUSPENDED state because the
aoqi@0 851 // conditions in which a thread is first started are different from those in which
aoqi@0 852 // a suspension is resumed. These differences make it hard for us to apply the
aoqi@0 853 // tougher checks when starting threads that we want to do when resuming them.
aoqi@0 854 // However, when start_thread is called as a result of Thread.start, on a Java
aoqi@0 855 // thread, the operation is synchronized on the Java Thread object. So there
aoqi@0 856 // cannot be a race to start the thread and hence for the thread to exit while
aoqi@0 857 // we are working on it. Non-Java threads that start Java threads either have
aoqi@0 858 // to do so in a context in which races are impossible, or should do appropriate
aoqi@0 859 // locking.
aoqi@0 860
aoqi@0 861 void os::start_thread(Thread* thread) {
aoqi@0 862 // guard suspend/resume
aoqi@0 863 MutexLockerEx ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag);
aoqi@0 864 OSThread* osthread = thread->osthread();
aoqi@0 865 osthread->set_state(RUNNABLE);
aoqi@0 866 pd_start_thread(thread);
aoqi@0 867 }
aoqi@0 868
aoqi@0 869 //---------------------------------------------------------------------------
aoqi@0 870 // Helper functions for fatal error handler
aoqi@0 871
aoqi@0 872 void os::print_hex_dump(outputStream* st, address start, address end, int unitsize) {
aoqi@0 873 assert(unitsize == 1 || unitsize == 2 || unitsize == 4 || unitsize == 8, "just checking");
aoqi@0 874
aoqi@0 875 int cols = 0;
aoqi@0 876 int cols_per_line = 0;
aoqi@0 877 switch (unitsize) {
aoqi@0 878 case 1: cols_per_line = 16; break;
aoqi@0 879 case 2: cols_per_line = 8; break;
aoqi@0 880 case 4: cols_per_line = 4; break;
aoqi@0 881 case 8: cols_per_line = 2; break;
aoqi@0 882 default: return;
aoqi@0 883 }
aoqi@0 884
aoqi@0 885 address p = start;
aoqi@0 886 st->print(PTR_FORMAT ": ", start);
aoqi@0 887 while (p < end) {
aoqi@0 888 switch (unitsize) {
aoqi@0 889 case 1: st->print("%02x", *(u1*)p); break;
aoqi@0 890 case 2: st->print("%04x", *(u2*)p); break;
aoqi@0 891 case 4: st->print("%08x", *(u4*)p); break;
aoqi@0 892 case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break;
aoqi@0 893 }
aoqi@0 894 p += unitsize;
aoqi@0 895 cols++;
aoqi@0 896 if (cols >= cols_per_line && p < end) {
aoqi@0 897 cols = 0;
aoqi@0 898 st->cr();
aoqi@0 899 st->print(PTR_FORMAT ": ", p);
aoqi@0 900 } else {
aoqi@0 901 st->print(" ");
aoqi@0 902 }
aoqi@0 903 }
aoqi@0 904 st->cr();
aoqi@0 905 }
aoqi@0 906
aoqi@0 907 void os::print_environment_variables(outputStream* st, const char** env_list,
aoqi@0 908 char* buffer, int len) {
aoqi@0 909 if (env_list) {
aoqi@0 910 st->print_cr("Environment Variables:");
aoqi@0 911
aoqi@0 912 for (int i = 0; env_list[i] != NULL; i++) {
aoqi@0 913 if (getenv(env_list[i], buffer, len)) {
aoqi@0 914 st->print("%s", env_list[i]);
aoqi@0 915 st->print("=");
aoqi@0 916 st->print_cr("%s", buffer);
aoqi@0 917 }
aoqi@0 918 }
aoqi@0 919 }
aoqi@0 920 }
aoqi@0 921
aoqi@0 922 void os::print_cpu_info(outputStream* st) {
aoqi@0 923 // cpu
aoqi@0 924 st->print("CPU:");
aoqi@0 925 st->print("total %d", os::processor_count());
aoqi@0 926 // It's not safe to query number of active processors after crash
aoqi@0 927 // st->print("(active %d)", os::active_processor_count());
aoqi@0 928 st->print(" %s", VM_Version::cpu_features());
aoqi@0 929 st->cr();
aoqi@0 930 pd_print_cpu_info(st);
aoqi@0 931 }
aoqi@0 932
aoqi@0 933 void os::print_date_and_time(outputStream *st) {
aoqi@0 934 const int secs_per_day = 86400;
aoqi@0 935 const int secs_per_hour = 3600;
aoqi@0 936 const int secs_per_min = 60;
aoqi@0 937
aoqi@0 938 time_t tloc;
aoqi@0 939 (void)time(&tloc);
aoqi@0 940 st->print("time: %s", ctime(&tloc)); // ctime adds newline.
aoqi@0 941
aoqi@0 942 double t = os::elapsedTime();
aoqi@0 943 // NOTE: It tends to crash after a SEGV if we want to printf("%f",...) in
aoqi@0 944 // Linux. Must be a bug in glibc ? Workaround is to round "t" to int
aoqi@0 945 // before printf. We lost some precision, but who cares?
aoqi@0 946 int eltime = (int)t; // elapsed time in seconds
aoqi@0 947
aoqi@0 948 // print elapsed time in a human-readable format:
aoqi@0 949 int eldays = eltime / secs_per_day;
aoqi@0 950 int day_secs = eldays * secs_per_day;
aoqi@0 951 int elhours = (eltime - day_secs) / secs_per_hour;
aoqi@0 952 int hour_secs = elhours * secs_per_hour;
aoqi@0 953 int elmins = (eltime - day_secs - hour_secs) / secs_per_min;
aoqi@0 954 int minute_secs = elmins * secs_per_min;
aoqi@0 955 int elsecs = (eltime - day_secs - hour_secs - minute_secs);
aoqi@0 956 st->print_cr("elapsed time: %d seconds (%dd %dh %dm %ds)", eltime, eldays, elhours, elmins, elsecs);
aoqi@0 957 }
aoqi@0 958
aoqi@0 959 // moved from debug.cpp (used to be find()) but still called from there
aoqi@0 960 // The verbose parameter is only set by the debug code in one case
aoqi@0 961 void os::print_location(outputStream* st, intptr_t x, bool verbose) {
aoqi@0 962 address addr = (address)x;
aoqi@0 963 CodeBlob* b = CodeCache::find_blob_unsafe(addr);
aoqi@0 964 if (b != NULL) {
aoqi@0 965 if (b->is_buffer_blob()) {
aoqi@0 966 // the interpreter is generated into a buffer blob
aoqi@0 967 InterpreterCodelet* i = Interpreter::codelet_containing(addr);
aoqi@0 968 if (i != NULL) {
aoqi@0 969 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an Interpreter codelet", addr, (int)(addr - i->code_begin()));
aoqi@0 970 i->print_on(st);
aoqi@0 971 return;
aoqi@0 972 }
aoqi@0 973 if (Interpreter::contains(addr)) {
aoqi@0 974 st->print_cr(INTPTR_FORMAT " is pointing into interpreter code"
aoqi@0 975 " (not bytecode specific)", addr);
aoqi@0 976 return;
aoqi@0 977 }
aoqi@0 978 //
aoqi@0 979 if (AdapterHandlerLibrary::contains(b)) {
aoqi@0 980 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in an AdapterHandler", addr, (int)(addr - b->code_begin()));
aoqi@0 981 AdapterHandlerLibrary::print_handler_on(st, b);
aoqi@0 982 }
aoqi@0 983 // the stubroutines are generated into a buffer blob
aoqi@0 984 StubCodeDesc* d = StubCodeDesc::desc_for(addr);
aoqi@0 985 if (d != NULL) {
aoqi@0 986 st->print_cr(INTPTR_FORMAT " is at begin+%d in a stub", addr, (int)(addr - d->begin()));
aoqi@0 987 d->print_on(st);
aoqi@0 988 st->cr();
aoqi@0 989 return;
aoqi@0 990 }
aoqi@0 991 if (StubRoutines::contains(addr)) {
aoqi@0 992 st->print_cr(INTPTR_FORMAT " is pointing to an (unnamed) "
aoqi@0 993 "stub routine", addr);
aoqi@0 994 return;
aoqi@0 995 }
aoqi@0 996 // the InlineCacheBuffer is using stubs generated into a buffer blob
aoqi@0 997 if (InlineCacheBuffer::contains(addr)) {
aoqi@0 998 st->print_cr(INTPTR_FORMAT " is pointing into InlineCacheBuffer", addr);
aoqi@0 999 return;
aoqi@0 1000 }
aoqi@0 1001 VtableStub* v = VtableStubs::stub_containing(addr);
aoqi@0 1002 if (v != NULL) {
aoqi@0 1003 st->print_cr(INTPTR_FORMAT " is at entry_point+%d in a vtable stub", addr, (int)(addr - v->entry_point()));
aoqi@0 1004 v->print_on(st);
aoqi@0 1005 st->cr();
aoqi@0 1006 return;
aoqi@0 1007 }
aoqi@0 1008 }
aoqi@0 1009 nmethod* nm = b->as_nmethod_or_null();
aoqi@0 1010 if (nm != NULL) {
aoqi@0 1011 ResourceMark rm;
aoqi@0 1012 st->print(INTPTR_FORMAT " is at entry_point+%d in (nmethod*)" INTPTR_FORMAT,
aoqi@0 1013 addr, (int)(addr - nm->entry_point()), nm);
aoqi@0 1014 if (verbose) {
aoqi@0 1015 st->print(" for ");
aoqi@0 1016 nm->method()->print_value_on(st);
aoqi@0 1017 }
aoqi@0 1018 st->cr();
aoqi@0 1019 nm->print_nmethod(verbose);
aoqi@0 1020 return;
aoqi@0 1021 }
aoqi@0 1022 st->print_cr(INTPTR_FORMAT " is at code_begin+%d in ", addr, (int)(addr - b->code_begin()));
aoqi@0 1023 b->print_on(st);
aoqi@0 1024 return;
aoqi@0 1025 }
aoqi@0 1026
aoqi@0 1027 if (Universe::heap()->is_in(addr)) {
aoqi@0 1028 HeapWord* p = Universe::heap()->block_start(addr);
aoqi@0 1029 bool print = false;
aoqi@0 1030 // If we couldn't find it it just may mean that heap wasn't parseable
aoqi@0 1031 // See if we were just given an oop directly
aoqi@0 1032 if (p != NULL && Universe::heap()->block_is_obj(p)) {
aoqi@0 1033 print = true;
aoqi@0 1034 } else if (p == NULL && ((oopDesc*)addr)->is_oop()) {
aoqi@0 1035 p = (HeapWord*) addr;
aoqi@0 1036 print = true;
aoqi@0 1037 }
aoqi@0 1038 if (print) {
aoqi@0 1039 if (p == (HeapWord*) addr) {
aoqi@0 1040 st->print_cr(INTPTR_FORMAT " is an oop", addr);
aoqi@0 1041 } else {
aoqi@0 1042 st->print_cr(INTPTR_FORMAT " is pointing into object: " INTPTR_FORMAT, addr, p);
aoqi@0 1043 }
aoqi@0 1044 oop(p)->print_on(st);
aoqi@0 1045 return;
aoqi@0 1046 }
aoqi@0 1047 } else {
aoqi@0 1048 if (Universe::heap()->is_in_reserved(addr)) {
aoqi@0 1049 st->print_cr(INTPTR_FORMAT " is an unallocated location "
aoqi@0 1050 "in the heap", addr);
aoqi@0 1051 return;
aoqi@0 1052 }
aoqi@0 1053 }
aoqi@0 1054 if (JNIHandles::is_global_handle((jobject) addr)) {
aoqi@0 1055 st->print_cr(INTPTR_FORMAT " is a global jni handle", addr);
aoqi@0 1056 return;
aoqi@0 1057 }
aoqi@0 1058 if (JNIHandles::is_weak_global_handle((jobject) addr)) {
aoqi@0 1059 st->print_cr(INTPTR_FORMAT " is a weak global jni handle", addr);
aoqi@0 1060 return;
aoqi@0 1061 }
aoqi@0 1062 #ifndef PRODUCT
aoqi@0 1063 // we don't keep the block list in product mode
aoqi@0 1064 if (JNIHandleBlock::any_contains((jobject) addr)) {
aoqi@0 1065 st->print_cr(INTPTR_FORMAT " is a local jni handle", addr);
aoqi@0 1066 return;
aoqi@0 1067 }
aoqi@0 1068 #endif
aoqi@0 1069
aoqi@0 1070 for(JavaThread *thread = Threads::first(); thread; thread = thread->next()) {
aoqi@0 1071 // Check for privilege stack
aoqi@0 1072 if (thread->privileged_stack_top() != NULL &&
aoqi@0 1073 thread->privileged_stack_top()->contains(addr)) {
aoqi@0 1074 st->print_cr(INTPTR_FORMAT " is pointing into the privilege stack "
aoqi@0 1075 "for thread: " INTPTR_FORMAT, addr, thread);
aoqi@0 1076 if (verbose) thread->print_on(st);
aoqi@0 1077 return;
aoqi@0 1078 }
aoqi@0 1079 // If the addr is a java thread print information about that.
aoqi@0 1080 if (addr == (address)thread) {
aoqi@0 1081 if (verbose) {
aoqi@0 1082 thread->print_on(st);
aoqi@0 1083 } else {
aoqi@0 1084 st->print_cr(INTPTR_FORMAT " is a thread", addr);
aoqi@0 1085 }
aoqi@0 1086 return;
aoqi@0 1087 }
aoqi@0 1088 // If the addr is in the stack region for this thread then report that
aoqi@0 1089 // and print thread info
aoqi@0 1090 if (thread->stack_base() >= addr &&
aoqi@0 1091 addr > (thread->stack_base() - thread->stack_size())) {
aoqi@0 1092 st->print_cr(INTPTR_FORMAT " is pointing into the stack for thread: "
aoqi@0 1093 INTPTR_FORMAT, addr, thread);
aoqi@0 1094 if (verbose) thread->print_on(st);
aoqi@0 1095 return;
aoqi@0 1096 }
aoqi@0 1097
aoqi@0 1098 }
aoqi@0 1099
aoqi@0 1100 // Check if in metaspace and print types that have vptrs (only method now)
aoqi@0 1101 if (Metaspace::contains(addr)) {
aoqi@0 1102 if (Method::has_method_vptr((const void*)addr)) {
aoqi@0 1103 ((Method*)addr)->print_value_on(st);
aoqi@0 1104 st->cr();
aoqi@0 1105 } else {
aoqi@0 1106 // Use addr->print() from the debugger instead (not here)
aoqi@0 1107 st->print_cr(INTPTR_FORMAT " is pointing into metadata", addr);
aoqi@0 1108 }
aoqi@0 1109 return;
aoqi@0 1110 }
aoqi@0 1111
aoqi@0 1112 // Try an OS specific find
aoqi@0 1113 if (os::find(addr, st)) {
aoqi@0 1114 return;
aoqi@0 1115 }
aoqi@0 1116
aoqi@0 1117 st->print_cr(INTPTR_FORMAT " is an unknown value", addr);
aoqi@0 1118 }
aoqi@0 1119
aoqi@0 1120 // Looks like all platforms except IA64 can use the same function to check
aoqi@0 1121 // if C stack is walkable beyond current frame. The check for fp() is not
aoqi@0 1122 // necessary on Sparc, but it's harmless.
aoqi@0 1123 bool os::is_first_C_frame(frame* fr) {
aoqi@0 1124 #if (defined(IA64) && !defined(AIX)) && !defined(_WIN32)
aoqi@0 1125 // On IA64 we have to check if the callers bsp is still valid
aoqi@0 1126 // (i.e. within the register stack bounds).
aoqi@0 1127 // Notice: this only works for threads created by the VM and only if
aoqi@0 1128 // we walk the current stack!!! If we want to be able to walk
aoqi@0 1129 // arbitrary other threads, we'll have to somehow store the thread
aoqi@0 1130 // object in the frame.
aoqi@0 1131 Thread *thread = Thread::current();
aoqi@0 1132 if ((address)fr->fp() <=
aoqi@0 1133 thread->register_stack_base() HPUX_ONLY(+ 0x0) LINUX_ONLY(+ 0x50)) {
aoqi@0 1134 // This check is a little hacky, because on Linux the first C
aoqi@0 1135 // frame's ('start_thread') register stack frame starts at
aoqi@0 1136 // "register_stack_base + 0x48" while on HPUX, the first C frame's
aoqi@0 1137 // ('__pthread_bound_body') register stack frame seems to really
aoqi@0 1138 // start at "register_stack_base".
aoqi@0 1139 return true;
aoqi@0 1140 } else {
aoqi@0 1141 return false;
aoqi@0 1142 }
aoqi@0 1143 #elif defined(IA64) && defined(_WIN32)
aoqi@0 1144 return true;
aoqi@0 1145 #else
aoqi@0 1146 // Load up sp, fp, sender sp and sender fp, check for reasonable values.
aoqi@0 1147 // Check usp first, because if that's bad the other accessors may fault
aoqi@0 1148 // on some architectures. Ditto ufp second, etc.
aoqi@0 1149 uintptr_t fp_align_mask = (uintptr_t)(sizeof(address)-1);
aoqi@0 1150 // sp on amd can be 32 bit aligned.
aoqi@0 1151 uintptr_t sp_align_mask = (uintptr_t)(sizeof(int)-1);
aoqi@0 1152
aoqi@0 1153 uintptr_t usp = (uintptr_t)fr->sp();
aoqi@0 1154 if ((usp & sp_align_mask) != 0) return true;
aoqi@0 1155
aoqi@0 1156 uintptr_t ufp = (uintptr_t)fr->fp();
aoqi@0 1157 if ((ufp & fp_align_mask) != 0) return true;
aoqi@0 1158
aoqi@0 1159 uintptr_t old_sp = (uintptr_t)fr->sender_sp();
aoqi@0 1160 if ((old_sp & sp_align_mask) != 0) return true;
aoqi@0 1161 if (old_sp == 0 || old_sp == (uintptr_t)-1) return true;
aoqi@0 1162
aoqi@0 1163 uintptr_t old_fp = (uintptr_t)fr->link();
aoqi@0 1164 if ((old_fp & fp_align_mask) != 0) return true;
aoqi@0 1165 if (old_fp == 0 || old_fp == (uintptr_t)-1 || old_fp == ufp) return true;
aoqi@0 1166
aoqi@0 1167 // stack grows downwards; if old_fp is below current fp or if the stack
aoqi@0 1168 // frame is too large, either the stack is corrupted or fp is not saved
aoqi@0 1169 // on stack (i.e. on x86, ebp may be used as general register). The stack
aoqi@0 1170 // is not walkable beyond current frame.
aoqi@0 1171 if (old_fp < ufp) return true;
aoqi@0 1172 if (old_fp - ufp > 64 * K) return true;
aoqi@0 1173
aoqi@0 1174 return false;
aoqi@0 1175 #endif
aoqi@0 1176 }
aoqi@0 1177
aoqi@0 1178 #ifdef ASSERT
aoqi@0 1179 extern "C" void test_random() {
aoqi@0 1180 const double m = 2147483647;
aoqi@0 1181 double mean = 0.0, variance = 0.0, t;
aoqi@0 1182 long reps = 10000;
aoqi@0 1183 unsigned long seed = 1;
aoqi@0 1184
aoqi@0 1185 tty->print_cr("seed %ld for %ld repeats...", seed, reps);
aoqi@0 1186 os::init_random(seed);
aoqi@0 1187 long num;
aoqi@0 1188 for (int k = 0; k < reps; k++) {
aoqi@0 1189 num = os::random();
aoqi@0 1190 double u = (double)num / m;
aoqi@0 1191 assert(u >= 0.0 && u <= 1.0, "bad random number!");
aoqi@0 1192
aoqi@0 1193 // calculate mean and variance of the random sequence
aoqi@0 1194 mean += u;
aoqi@0 1195 variance += (u*u);
aoqi@0 1196 }
aoqi@0 1197 mean /= reps;
aoqi@0 1198 variance /= (reps - 1);
aoqi@0 1199
aoqi@0 1200 assert(num == 1043618065, "bad seed");
aoqi@0 1201 tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
aoqi@0 1202 tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
aoqi@0 1203 const double eps = 0.0001;
aoqi@0 1204 t = fabsd(mean - 0.5018);
aoqi@0 1205 assert(t < eps, "bad mean");
aoqi@0 1206 t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
aoqi@0 1207 assert(t < eps, "bad variance");
aoqi@0 1208 }
aoqi@0 1209 #endif
aoqi@0 1210
aoqi@0 1211
aoqi@0 1212 // Set up the boot classpath.
aoqi@0 1213
aoqi@0 1214 char* os::format_boot_path(const char* format_string,
aoqi@0 1215 const char* home,
aoqi@0 1216 int home_len,
aoqi@0 1217 char fileSep,
aoqi@0 1218 char pathSep) {
aoqi@0 1219 assert((fileSep == '/' && pathSep == ':') ||
aoqi@0 1220 (fileSep == '\\' && pathSep == ';'), "unexpected seperator chars");
aoqi@0 1221
aoqi@0 1222 // Scan the format string to determine the length of the actual
aoqi@0 1223 // boot classpath, and handle platform dependencies as well.
aoqi@0 1224 int formatted_path_len = 0;
aoqi@0 1225 const char* p;
aoqi@0 1226 for (p = format_string; *p != 0; ++p) {
aoqi@0 1227 if (*p == '%') formatted_path_len += home_len - 1;
aoqi@0 1228 ++formatted_path_len;
aoqi@0 1229 }
aoqi@0 1230
aoqi@0 1231 char* formatted_path = NEW_C_HEAP_ARRAY(char, formatted_path_len + 1, mtInternal);
aoqi@0 1232 if (formatted_path == NULL) {
aoqi@0 1233 return NULL;
aoqi@0 1234 }
aoqi@0 1235
aoqi@0 1236 // Create boot classpath from format, substituting separator chars and
aoqi@0 1237 // java home directory.
aoqi@0 1238 char* q = formatted_path;
aoqi@0 1239 for (p = format_string; *p != 0; ++p) {
aoqi@0 1240 switch (*p) {
aoqi@0 1241 case '%':
aoqi@0 1242 strcpy(q, home);
aoqi@0 1243 q += home_len;
aoqi@0 1244 break;
aoqi@0 1245 case '/':
aoqi@0 1246 *q++ = fileSep;
aoqi@0 1247 break;
aoqi@0 1248 case ':':
aoqi@0 1249 *q++ = pathSep;
aoqi@0 1250 break;
aoqi@0 1251 default:
aoqi@0 1252 *q++ = *p;
aoqi@0 1253 }
aoqi@0 1254 }
aoqi@0 1255 *q = '\0';
aoqi@0 1256
aoqi@0 1257 assert((q - formatted_path) == formatted_path_len, "formatted_path size botched");
aoqi@0 1258 return formatted_path;
aoqi@0 1259 }
aoqi@0 1260
aoqi@0 1261
aoqi@0 1262 bool os::set_boot_path(char fileSep, char pathSep) {
aoqi@0 1263 const char* home = Arguments::get_java_home();
aoqi@0 1264 int home_len = (int)strlen(home);
aoqi@0 1265
aoqi@0 1266 static const char* meta_index_dir_format = "%/lib/";
aoqi@0 1267 static const char* meta_index_format = "%/lib/meta-index";
aoqi@0 1268 char* meta_index = format_boot_path(meta_index_format, home, home_len, fileSep, pathSep);
aoqi@0 1269 if (meta_index == NULL) return false;
aoqi@0 1270 char* meta_index_dir = format_boot_path(meta_index_dir_format, home, home_len, fileSep, pathSep);
aoqi@0 1271 if (meta_index_dir == NULL) return false;
aoqi@0 1272 Arguments::set_meta_index_path(meta_index, meta_index_dir);
aoqi@0 1273
aoqi@0 1274 // Any modification to the JAR-file list, for the boot classpath must be
aoqi@0 1275 // aligned with install/install/make/common/Pack.gmk. Note: boot class
aoqi@0 1276 // path class JARs, are stripped for StackMapTable to reduce download size.
aoqi@0 1277 static const char classpath_format[] =
aoqi@0 1278 "%/lib/resources.jar:"
aoqi@0 1279 "%/lib/rt.jar:"
aoqi@0 1280 "%/lib/sunrsasign.jar:"
aoqi@0 1281 "%/lib/jsse.jar:"
aoqi@0 1282 "%/lib/jce.jar:"
aoqi@0 1283 "%/lib/charsets.jar:"
aoqi@0 1284 "%/lib/jfr.jar:"
aoqi@0 1285 "%/classes";
aoqi@0 1286 char* sysclasspath = format_boot_path(classpath_format, home, home_len, fileSep, pathSep);
aoqi@0 1287 if (sysclasspath == NULL) return false;
aoqi@0 1288 Arguments::set_sysclasspath(sysclasspath);
aoqi@0 1289
aoqi@0 1290 return true;
aoqi@0 1291 }
aoqi@0 1292
aoqi@0 1293 /*
aoqi@0 1294 * Splits a path, based on its separator, the number of
aoqi@0 1295 * elements is returned back in n.
aoqi@0 1296 * It is the callers responsibility to:
aoqi@0 1297 * a> check the value of n, and n may be 0.
aoqi@0 1298 * b> ignore any empty path elements
aoqi@0 1299 * c> free up the data.
aoqi@0 1300 */
aoqi@0 1301 char** os::split_path(const char* path, int* n) {
aoqi@0 1302 *n = 0;
aoqi@0 1303 if (path == NULL || strlen(path) == 0) {
aoqi@0 1304 return NULL;
aoqi@0 1305 }
aoqi@0 1306 const char psepchar = *os::path_separator();
aoqi@0 1307 char* inpath = (char*)NEW_C_HEAP_ARRAY(char, strlen(path) + 1, mtInternal);
aoqi@0 1308 if (inpath == NULL) {
aoqi@0 1309 return NULL;
aoqi@0 1310 }
aoqi@0 1311 strcpy(inpath, path);
aoqi@0 1312 int count = 1;
aoqi@0 1313 char* p = strchr(inpath, psepchar);
aoqi@0 1314 // Get a count of elements to allocate memory
aoqi@0 1315 while (p != NULL) {
aoqi@0 1316 count++;
aoqi@0 1317 p++;
aoqi@0 1318 p = strchr(p, psepchar);
aoqi@0 1319 }
aoqi@0 1320 char** opath = (char**) NEW_C_HEAP_ARRAY(char*, count, mtInternal);
aoqi@0 1321 if (opath == NULL) {
aoqi@0 1322 return NULL;
aoqi@0 1323 }
aoqi@0 1324
aoqi@0 1325 // do the actual splitting
aoqi@0 1326 p = inpath;
aoqi@0 1327 for (int i = 0 ; i < count ; i++) {
aoqi@0 1328 size_t len = strcspn(p, os::path_separator());
aoqi@0 1329 if (len > JVM_MAXPATHLEN) {
aoqi@0 1330 return NULL;
aoqi@0 1331 }
aoqi@0 1332 // allocate the string and add terminator storage
aoqi@0 1333 char* s = (char*)NEW_C_HEAP_ARRAY(char, len + 1, mtInternal);
aoqi@0 1334 if (s == NULL) {
aoqi@0 1335 return NULL;
aoqi@0 1336 }
aoqi@0 1337 strncpy(s, p, len);
aoqi@0 1338 s[len] = '\0';
aoqi@0 1339 opath[i] = s;
aoqi@0 1340 p += len + 1;
aoqi@0 1341 }
aoqi@0 1342 FREE_C_HEAP_ARRAY(char, inpath, mtInternal);
aoqi@0 1343 *n = count;
aoqi@0 1344 return opath;
aoqi@0 1345 }
aoqi@0 1346
aoqi@0 1347 void os::set_memory_serialize_page(address page) {
aoqi@0 1348 int count = log2_intptr(sizeof(class JavaThread)) - log2_intptr(64);
aoqi@0 1349 _mem_serialize_page = (volatile int32_t *)page;
aoqi@0 1350 // We initialize the serialization page shift count here
aoqi@0 1351 // We assume a cache line size of 64 bytes
aoqi@0 1352 assert(SerializePageShiftCount == count,
aoqi@0 1353 "thread size changed, fix SerializePageShiftCount constant");
aoqi@0 1354 set_serialize_page_mask((uintptr_t)(vm_page_size() - sizeof(int32_t)));
aoqi@0 1355 }
aoqi@0 1356
aoqi@0 1357 static volatile intptr_t SerializePageLock = 0;
aoqi@0 1358
aoqi@0 1359 // This method is called from signal handler when SIGSEGV occurs while the current
aoqi@0 1360 // thread tries to store to the "read-only" memory serialize page during state
aoqi@0 1361 // transition.
aoqi@0 1362 void os::block_on_serialize_page_trap() {
aoqi@0 1363 if (TraceSafepoint) {
aoqi@0 1364 tty->print_cr("Block until the serialize page permission restored");
aoqi@0 1365 }
aoqi@0 1366 // When VMThread is holding the SerializePageLock during modifying the
aoqi@0 1367 // access permission of the memory serialize page, the following call
aoqi@0 1368 // will block until the permission of that page is restored to rw.
aoqi@0 1369 // Generally, it is unsafe to manipulate locks in signal handlers, but in
aoqi@0 1370 // this case, it's OK as the signal is synchronous and we know precisely when
aoqi@0 1371 // it can occur.
aoqi@0 1372 Thread::muxAcquire(&SerializePageLock, "set_memory_serialize_page");
aoqi@0 1373 Thread::muxRelease(&SerializePageLock);
aoqi@0 1374 }
aoqi@0 1375
aoqi@0 1376 // Serialize all thread state variables
aoqi@0 1377 void os::serialize_thread_states() {
aoqi@0 1378 // On some platforms such as Solaris & Linux, the time duration of the page
aoqi@0 1379 // permission restoration is observed to be much longer than expected due to
aoqi@0 1380 // scheduler starvation problem etc. To avoid the long synchronization
aoqi@0 1381 // time and expensive page trap spinning, 'SerializePageLock' is used to block
aoqi@0 1382 // the mutator thread if such case is encountered. See bug 6546278 for details.
aoqi@0 1383 Thread::muxAcquire(&SerializePageLock, "serialize_thread_states");
aoqi@0 1384 os::protect_memory((char *)os::get_memory_serialize_page(),
aoqi@0 1385 os::vm_page_size(), MEM_PROT_READ);
aoqi@0 1386 os::protect_memory((char *)os::get_memory_serialize_page(),
aoqi@0 1387 os::vm_page_size(), MEM_PROT_RW);
aoqi@0 1388 Thread::muxRelease(&SerializePageLock);
aoqi@0 1389 }
aoqi@0 1390
aoqi@0 1391 // Returns true if the current stack pointer is above the stack shadow
aoqi@0 1392 // pages, false otherwise.
aoqi@0 1393
aoqi@0 1394 bool os::stack_shadow_pages_available(Thread *thread, methodHandle method) {
aoqi@0 1395 assert(StackRedPages > 0 && StackYellowPages > 0,"Sanity check");
aoqi@0 1396 address sp = current_stack_pointer();
aoqi@0 1397 // Check if we have StackShadowPages above the yellow zone. This parameter
aoqi@0 1398 // is dependent on the depth of the maximum VM call stack possible from
aoqi@0 1399 // the handler for stack overflow. 'instanceof' in the stack overflow
aoqi@0 1400 // handler or a println uses at least 8k stack of VM and native code
aoqi@0 1401 // respectively.
aoqi@0 1402 const int framesize_in_bytes =
aoqi@0 1403 Interpreter::size_top_interpreter_activation(method()) * wordSize;
aoqi@0 1404 int reserved_area = ((StackShadowPages + StackRedPages + StackYellowPages)
aoqi@0 1405 * vm_page_size()) + framesize_in_bytes;
aoqi@0 1406 // The very lower end of the stack
aoqi@0 1407 address stack_limit = thread->stack_base() - thread->stack_size();
aoqi@0 1408 return (sp > (stack_limit + reserved_area));
aoqi@0 1409 }
aoqi@0 1410
aoqi@0 1411 size_t os::page_size_for_region(size_t region_min_size, size_t region_max_size,
aoqi@0 1412 uint min_pages)
aoqi@0 1413 {
aoqi@0 1414 assert(min_pages > 0, "sanity");
aoqi@0 1415 if (UseLargePages) {
aoqi@0 1416 const size_t max_page_size = region_max_size / min_pages;
aoqi@0 1417
aoqi@0 1418 for (unsigned int i = 0; _page_sizes[i] != 0; ++i) {
aoqi@0 1419 const size_t sz = _page_sizes[i];
aoqi@0 1420 const size_t mask = sz - 1;
aoqi@0 1421 if ((region_min_size & mask) == 0 && (region_max_size & mask) == 0) {
aoqi@0 1422 // The largest page size with no fragmentation.
aoqi@0 1423 return sz;
aoqi@0 1424 }
aoqi@0 1425
aoqi@0 1426 if (sz <= max_page_size) {
aoqi@0 1427 // The largest page size that satisfies the min_pages requirement.
aoqi@0 1428 return sz;
aoqi@0 1429 }
aoqi@0 1430 }
aoqi@0 1431 }
aoqi@0 1432
aoqi@0 1433 return vm_page_size();
aoqi@0 1434 }
aoqi@0 1435
aoqi@0 1436 #ifndef PRODUCT
aoqi@0 1437 void os::trace_page_sizes(const char* str, const size_t* page_sizes, int count)
aoqi@0 1438 {
aoqi@0 1439 if (TracePageSizes) {
aoqi@0 1440 tty->print("%s: ", str);
aoqi@0 1441 for (int i = 0; i < count; ++i) {
aoqi@0 1442 tty->print(" " SIZE_FORMAT, page_sizes[i]);
aoqi@0 1443 }
aoqi@0 1444 tty->cr();
aoqi@0 1445 }
aoqi@0 1446 }
aoqi@0 1447
aoqi@0 1448 void os::trace_page_sizes(const char* str, const size_t region_min_size,
aoqi@0 1449 const size_t region_max_size, const size_t page_size,
aoqi@0 1450 const char* base, const size_t size)
aoqi@0 1451 {
aoqi@0 1452 if (TracePageSizes) {
aoqi@0 1453 tty->print_cr("%s: min=" SIZE_FORMAT " max=" SIZE_FORMAT
aoqi@0 1454 " pg_sz=" SIZE_FORMAT " base=" PTR_FORMAT
aoqi@0 1455 " size=" SIZE_FORMAT,
aoqi@0 1456 str, region_min_size, region_max_size,
aoqi@0 1457 page_size, base, size);
aoqi@0 1458 }
aoqi@0 1459 }
aoqi@0 1460 #endif // #ifndef PRODUCT
aoqi@0 1461
aoqi@0 1462 // This is the working definition of a server class machine:
aoqi@0 1463 // >= 2 physical CPU's and >=2GB of memory, with some fuzz
aoqi@0 1464 // because the graphics memory (?) sometimes masks physical memory.
aoqi@0 1465 // If you want to change the definition of a server class machine
aoqi@0 1466 // on some OS or platform, e.g., >=4GB on Windohs platforms,
aoqi@0 1467 // then you'll have to parameterize this method based on that state,
aoqi@0 1468 // as was done for logical processors here, or replicate and
aoqi@0 1469 // specialize this method for each platform. (Or fix os to have
aoqi@0 1470 // some inheritance structure and use subclassing. Sigh.)
aoqi@0 1471 // If you want some platform to always or never behave as a server
aoqi@0 1472 // class machine, change the setting of AlwaysActAsServerClassMachine
aoqi@0 1473 // and NeverActAsServerClassMachine in globals*.hpp.
aoqi@0 1474 bool os::is_server_class_machine() {
aoqi@0 1475 // First check for the early returns
aoqi@0 1476 if (NeverActAsServerClassMachine) {
aoqi@0 1477 return false;
aoqi@0 1478 }
aoqi@0 1479 if (AlwaysActAsServerClassMachine) {
aoqi@0 1480 return true;
aoqi@0 1481 }
aoqi@0 1482 // Then actually look at the machine
aoqi@0 1483 bool result = false;
aoqi@0 1484 const unsigned int server_processors = 2;
aoqi@0 1485 const julong server_memory = 2UL * G;
aoqi@0 1486 // We seem not to get our full complement of memory.
aoqi@0 1487 // We allow some part (1/8?) of the memory to be "missing",
aoqi@0 1488 // based on the sizes of DIMMs, and maybe graphics cards.
aoqi@0 1489 const julong missing_memory = 256UL * M;
aoqi@0 1490
aoqi@0 1491 /* Is this a server class machine? */
aoqi@0 1492 if ((os::active_processor_count() >= (int)server_processors) &&
aoqi@0 1493 (os::physical_memory() >= (server_memory - missing_memory))) {
aoqi@0 1494 const unsigned int logical_processors =
aoqi@0 1495 VM_Version::logical_processors_per_package();
aoqi@0 1496 if (logical_processors > 1) {
aoqi@0 1497 const unsigned int physical_packages =
aoqi@0 1498 os::active_processor_count() / logical_processors;
aoqi@0 1499 if (physical_packages > server_processors) {
aoqi@0 1500 result = true;
aoqi@0 1501 }
aoqi@0 1502 } else {
aoqi@0 1503 result = true;
aoqi@0 1504 }
aoqi@0 1505 }
aoqi@0 1506 return result;
aoqi@0 1507 }
aoqi@0 1508
aoqi@0 1509 void os::SuspendedThreadTask::run() {
aoqi@0 1510 assert(Threads_lock->owned_by_self() || (_thread == VMThread::vm_thread()), "must have threads lock to call this");
aoqi@0 1511 internal_do_task();
aoqi@0 1512 _done = true;
aoqi@0 1513 }
aoqi@0 1514
aoqi@0 1515 bool os::create_stack_guard_pages(char* addr, size_t bytes) {
aoqi@0 1516 return os::pd_create_stack_guard_pages(addr, bytes);
aoqi@0 1517 }
aoqi@0 1518
aoqi@0 1519 char* os::reserve_memory(size_t bytes, char* addr, size_t alignment_hint) {
aoqi@0 1520 char* result = pd_reserve_memory(bytes, addr, alignment_hint);
aoqi@0 1521 if (result != NULL) {
aoqi@0 1522 MemTracker::record_virtual_memory_reserve((address)result, bytes, mtNone, CALLER_PC);
aoqi@0 1523 }
aoqi@0 1524
aoqi@0 1525 return result;
aoqi@0 1526 }
aoqi@0 1527
aoqi@0 1528 char* os::reserve_memory(size_t bytes, char* addr, size_t alignment_hint,
aoqi@0 1529 MEMFLAGS flags) {
aoqi@0 1530 char* result = pd_reserve_memory(bytes, addr, alignment_hint);
aoqi@0 1531 if (result != NULL) {
aoqi@0 1532 MemTracker::record_virtual_memory_reserve((address)result, bytes, mtNone, CALLER_PC);
aoqi@0 1533 MemTracker::record_virtual_memory_type((address)result, flags);
aoqi@0 1534 }
aoqi@0 1535
aoqi@0 1536 return result;
aoqi@0 1537 }
aoqi@0 1538
aoqi@0 1539 char* os::attempt_reserve_memory_at(size_t bytes, char* addr) {
aoqi@0 1540 char* result = pd_attempt_reserve_memory_at(bytes, addr);
aoqi@0 1541 if (result != NULL) {
aoqi@0 1542 MemTracker::record_virtual_memory_reserve((address)result, bytes, mtNone, CALLER_PC);
aoqi@0 1543 }
aoqi@0 1544 return result;
aoqi@0 1545 }
aoqi@0 1546
aoqi@0 1547 void os::split_reserved_memory(char *base, size_t size,
aoqi@0 1548 size_t split, bool realloc) {
aoqi@0 1549 pd_split_reserved_memory(base, size, split, realloc);
aoqi@0 1550 }
aoqi@0 1551
aoqi@0 1552 bool os::commit_memory(char* addr, size_t bytes, bool executable) {
aoqi@0 1553 bool res = pd_commit_memory(addr, bytes, executable);
aoqi@0 1554 if (res) {
aoqi@0 1555 MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
aoqi@0 1556 }
aoqi@0 1557 return res;
aoqi@0 1558 }
aoqi@0 1559
aoqi@0 1560 bool os::commit_memory(char* addr, size_t size, size_t alignment_hint,
aoqi@0 1561 bool executable) {
aoqi@0 1562 bool res = os::pd_commit_memory(addr, size, alignment_hint, executable);
aoqi@0 1563 if (res) {
aoqi@0 1564 MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
aoqi@0 1565 }
aoqi@0 1566 return res;
aoqi@0 1567 }
aoqi@0 1568
aoqi@0 1569 void os::commit_memory_or_exit(char* addr, size_t bytes, bool executable,
aoqi@0 1570 const char* mesg) {
aoqi@0 1571 pd_commit_memory_or_exit(addr, bytes, executable, mesg);
aoqi@0 1572 MemTracker::record_virtual_memory_commit((address)addr, bytes, CALLER_PC);
aoqi@0 1573 }
aoqi@0 1574
aoqi@0 1575 void os::commit_memory_or_exit(char* addr, size_t size, size_t alignment_hint,
aoqi@0 1576 bool executable, const char* mesg) {
aoqi@0 1577 os::pd_commit_memory_or_exit(addr, size, alignment_hint, executable, mesg);
aoqi@0 1578 MemTracker::record_virtual_memory_commit((address)addr, size, CALLER_PC);
aoqi@0 1579 }
aoqi@0 1580
aoqi@0 1581 bool os::uncommit_memory(char* addr, size_t bytes) {
aoqi@0 1582 MemTracker::Tracker tkr = MemTracker::get_virtual_memory_uncommit_tracker();
aoqi@0 1583 bool res = pd_uncommit_memory(addr, bytes);
aoqi@0 1584 if (res) {
aoqi@0 1585 tkr.record((address)addr, bytes);
aoqi@0 1586 } else {
aoqi@0 1587 tkr.discard();
aoqi@0 1588 }
aoqi@0 1589 return res;
aoqi@0 1590 }
aoqi@0 1591
aoqi@0 1592 bool os::release_memory(char* addr, size_t bytes) {
aoqi@0 1593 MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
aoqi@0 1594 bool res = pd_release_memory(addr, bytes);
aoqi@0 1595 if (res) {
aoqi@0 1596 tkr.record((address)addr, bytes);
aoqi@0 1597 } else {
aoqi@0 1598 tkr.discard();
aoqi@0 1599 }
aoqi@0 1600 return res;
aoqi@0 1601 }
aoqi@0 1602
aoqi@0 1603
aoqi@0 1604 char* os::map_memory(int fd, const char* file_name, size_t file_offset,
aoqi@0 1605 char *addr, size_t bytes, bool read_only,
aoqi@0 1606 bool allow_exec) {
aoqi@0 1607 char* result = pd_map_memory(fd, file_name, file_offset, addr, bytes, read_only, allow_exec);
aoqi@0 1608 if (result != NULL) {
aoqi@0 1609 MemTracker::record_virtual_memory_reserve_and_commit((address)result, bytes, mtNone, CALLER_PC);
aoqi@0 1610 }
aoqi@0 1611 return result;
aoqi@0 1612 }
aoqi@0 1613
aoqi@0 1614 char* os::remap_memory(int fd, const char* file_name, size_t file_offset,
aoqi@0 1615 char *addr, size_t bytes, bool read_only,
aoqi@0 1616 bool allow_exec) {
aoqi@0 1617 return pd_remap_memory(fd, file_name, file_offset, addr, bytes,
aoqi@0 1618 read_only, allow_exec);
aoqi@0 1619 }
aoqi@0 1620
aoqi@0 1621 bool os::unmap_memory(char *addr, size_t bytes) {
aoqi@0 1622 MemTracker::Tracker tkr = MemTracker::get_virtual_memory_release_tracker();
aoqi@0 1623 bool result = pd_unmap_memory(addr, bytes);
aoqi@0 1624 if (result) {
aoqi@0 1625 tkr.record((address)addr, bytes);
aoqi@0 1626 } else {
aoqi@0 1627 tkr.discard();
aoqi@0 1628 }
aoqi@0 1629 return result;
aoqi@0 1630 }
aoqi@0 1631
aoqi@0 1632 void os::free_memory(char *addr, size_t bytes, size_t alignment_hint) {
aoqi@0 1633 pd_free_memory(addr, bytes, alignment_hint);
aoqi@0 1634 }
aoqi@0 1635
aoqi@0 1636 void os::realign_memory(char *addr, size_t bytes, size_t alignment_hint) {
aoqi@0 1637 pd_realign_memory(addr, bytes, alignment_hint);
aoqi@0 1638 }
aoqi@0 1639
aoqi@0 1640 #ifndef TARGET_OS_FAMILY_windows
aoqi@0 1641 /* try to switch state from state "from" to state "to"
aoqi@0 1642 * returns the state set after the method is complete
aoqi@0 1643 */
aoqi@0 1644 os::SuspendResume::State os::SuspendResume::switch_state(os::SuspendResume::State from,
aoqi@0 1645 os::SuspendResume::State to)
aoqi@0 1646 {
aoqi@0 1647 os::SuspendResume::State result =
aoqi@0 1648 (os::SuspendResume::State) Atomic::cmpxchg((jint) to, (jint *) &_state, (jint) from);
aoqi@0 1649 if (result == from) {
aoqi@0 1650 // success
aoqi@0 1651 return to;
aoqi@0 1652 }
aoqi@0 1653 return result;
aoqi@0 1654 }
aoqi@0 1655 #endif

mercurial