src/share/vm/runtime/os.cpp

Tue, 29 May 2018 20:20:25 +0800

author
aoqi
date
Tue, 29 May 2018 20:20:25 +0800
changeset 9122
024be04bb151
parent 8856
ac27a9c85bea
child 9448
73d689add964
permissions
-rw-r--r--

Merge

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

mercurial