os.cpp revision 1562:0e7d2a08b605
1243789Sdim/* 2243789Sdim * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved. 3243789Sdim * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4243789Sdim * 5243789Sdim * This code is free software; you can redistribute it and/or modify it 6243789Sdim * under the terms of the GNU General Public License version 2 only, as 7243789Sdim * published by the Free Software Foundation. 8243789Sdim * 9243789Sdim * This code is distributed in the hope that it will be useful, but WITHOUT 10243789Sdim * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11243789Sdim * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12243789Sdim * version 2 for more details (a copy is included in the LICENSE file that 13243789Sdim * accompanied this code). 14243789Sdim * 15243789Sdim * You should have received a copy of the GNU General Public License version 16243789Sdim * 2 along with this work; if not, write to the Free Software Foundation, 17243789Sdim * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18243789Sdim * 19243789Sdim * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20243789Sdim * or visit www.oracle.com if you need additional information or have any 21243789Sdim * questions. 22243789Sdim * 23243789Sdim */ 24243789Sdim 25243789Sdim# include "incls/_precompiled.incl" 26243789Sdim# include "incls/_os.cpp.incl" 27243789Sdim 28249423Sdim# include <signal.h> 29243789Sdim 30243789SdimOSThread* os::_starting_thread = NULL; 31243789Sdimaddress os::_polling_page = NULL; 32243789Sdimvolatile int32_t* os::_mem_serialize_page = NULL; 33243789Sdimuintptr_t os::_serialize_page_mask = 0; 34249423Sdimlong os::_rand_seed = 1; 35243789Sdimint os::_processor_count = 0; 36249423Sdimsize_t os::_page_sizes[os::page_sizes_max]; 37249423Sdim 38249423Sdim#ifndef PRODUCT 39249423Sdimint os::num_mallocs = 0; // # of calls to malloc/realloc 40249423Sdimsize_t os::alloc_bytes = 0; // # of bytes allocated 41249423Sdimint os::num_frees = 0; // # of calls to free 42249423Sdim#endif 43249423Sdim 44249423Sdim// Fill in buffer with current local time as an ISO-8601 string. 45249423Sdim// E.g., yyyy-mm-ddThh:mm:ss-zzzz. 46249423Sdim// Returns buffer, or NULL if it failed. 47249423Sdim// This would mostly be a call to 48249423Sdim// strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....) 49243789Sdim// except that on Windows the %z behaves badly, so we do it ourselves. 50243789Sdim// Also, people wanted milliseconds on there, 51243789Sdim// and strftime doesn't do milliseconds. 52243789Sdimchar* os::iso8601_time(char* buffer, size_t buffer_length) { 53243789Sdim // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0" 54243789Sdim // 1 2 55243789Sdim // 12345678901234567890123456789 56243789Sdim static const char* iso8601_format = 57243789Sdim "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d"; 58243789Sdim static const size_t needed_buffer = 29; 59243789Sdim 60249423Sdim // Sanity check the arguments 61249423Sdim if (buffer == NULL) { 62249423Sdim assert(false, "NULL buffer"); 63249423Sdim return NULL; 64249423Sdim } 65249423Sdim if (buffer_length < needed_buffer) { 66243789Sdim assert(false, "buffer_length too small"); 67249423Sdim return NULL; 68249423Sdim } 69243789Sdim // Get the current time 70243789Sdim jlong milliseconds_since_19700101 = javaTimeMillis(); 71243789Sdim const int milliseconds_per_microsecond = 1000; 72243789Sdim const time_t seconds_since_19700101 = 73243789Sdim milliseconds_since_19700101 / milliseconds_per_microsecond; 74243789Sdim const int milliseconds_after_second = 75243789Sdim milliseconds_since_19700101 % milliseconds_per_microsecond; 76249423Sdim // Convert the time value to a tm and timezone variable 77249423Sdim struct tm time_struct; 78249423Sdim if (localtime_pd(&seconds_since_19700101, &time_struct) == NULL) { 79249423Sdim assert(false, "Failed localtime_pd"); 80249423Sdim return NULL; 81249423Sdim } 82249423Sdim const time_t zone = timezone; 83243789Sdim 84249423Sdim // If daylight savings time is in effect, 85243789Sdim // we are 1 hour East of our time zone 86249423Sdim const time_t seconds_per_minute = 60; 87249423Sdim const time_t minutes_per_hour = 60; 88249423Sdim const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour; 89249423Sdim time_t UTC_to_local = zone; 90249423Sdim if (time_struct.tm_isdst > 0) { 91249423Sdim UTC_to_local = UTC_to_local - seconds_per_hour; 92249423Sdim } 93243789Sdim // Compute the time zone offset. 94249423Sdim // localtime_pd() sets timezone to the difference (in seconds) 95249423Sdim // between UTC and and local time. 96249423Sdim // ISO 8601 says we need the difference between local time and UTC, 97249423Sdim // we change the sign of the localtime_pd() result. 98249423Sdim const time_t local_to_UTC = -(UTC_to_local); 99249423Sdim // Then we have to figure out if if we are ahead (+) or behind (-) UTC. 100249423Sdim char sign_local_to_UTC = '+'; 101243789Sdim time_t abs_local_to_UTC = local_to_UTC; 102249423Sdim if (local_to_UTC < 0) { 103249423Sdim sign_local_to_UTC = '-'; 104249423Sdim abs_local_to_UTC = -(abs_local_to_UTC); 105249423Sdim } 106249423Sdim // Convert time zone offset seconds to hours and minutes. 107249423Sdim const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour); 108249423Sdim const time_t zone_min = 109249423Sdim ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute); 110249423Sdim 111243789Sdim // Print an ISO 8601 date and time stamp into the buffer 112249423Sdim const int year = 1900 + time_struct.tm_year; 113249423Sdim const int month = 1 + time_struct.tm_mon; 114249423Sdim const int printed = jio_snprintf(buffer, buffer_length, iso8601_format, 115249423Sdim year, 116249423Sdim month, 117243789Sdim time_struct.tm_mday, 118249423Sdim time_struct.tm_hour, 119249423Sdim time_struct.tm_min, 120243789Sdim time_struct.tm_sec, 121249423Sdim milliseconds_after_second, 122249423Sdim sign_local_to_UTC, 123249423Sdim zone_hours, 124243789Sdim zone_min); 125249423Sdim if (printed == 0) { 126243789Sdim assert(false, "Failed jio_printf"); 127249423Sdim return NULL; 128249423Sdim } 129249423Sdim return buffer; 130249423Sdim} 131249423Sdim 132249423SdimOSReturn os::set_priority(Thread* thread, ThreadPriority p) { 133249423Sdim#ifdef ASSERT 134249423Sdim if (!(!thread->is_Java_thread() || 135249423Sdim Thread::current() == thread || 136249423Sdim Threads_lock->owned_by_self() 137243789Sdim || thread->is_Compiler_thread() 138249423Sdim )) { 139249423Sdim assert(false, "possibility of dangling Thread pointer"); 140249423Sdim } 141249423Sdim#endif 142243789Sdim 143249423Sdim if (p >= MinPriority && p <= MaxPriority) { 144249423Sdim int priority = java_to_os_priority[p]; 145249423Sdim return set_native_priority(thread, priority); 146249423Sdim } else { 147249423Sdim assert(false, "Should not happen"); 148249423Sdim return OS_ERR; 149249423Sdim } 150249423Sdim} 151249423Sdim 152249423Sdim 153249423SdimOSReturn os::get_priority(const Thread* const thread, ThreadPriority& priority) { 154249423Sdim int p; 155249423Sdim int os_prio; 156249423Sdim OSReturn ret = get_native_priority(thread, &os_prio); 157249423Sdim if (ret != OS_OK) return ret; 158249423Sdim 159249423Sdim for (p = MaxPriority; p > MinPriority && java_to_os_priority[p] > os_prio; p--) ; 160249423Sdim priority = (ThreadPriority)p; 161249423Sdim return OS_OK; 162249423Sdim} 163249423Sdim 164249423Sdim 165249423Sdim// --------------------- sun.misc.Signal (optional) --------------------- 166249423Sdim 167249423Sdim 168249423Sdim// SIGBREAK is sent by the keyboard to query the VM state 169249423Sdim#ifndef SIGBREAK 170249423Sdim#define SIGBREAK SIGQUIT 171249423Sdim#endif 172243789Sdim 173249423Sdim// sigexitnum_pd is a platform-specific special signal used for terminating the Signal thread. 174249423Sdim 175243789Sdim 176249423Sdimstatic void signal_thread_entry(JavaThread* thread, TRAPS) { 177249423Sdim os::set_priority(thread, NearMaxPriority); 178249423Sdim while (true) { 179249423Sdim int sig; 180249423Sdim { 181249423Sdim // FIXME : Currently we have not decieded what should be the status 182249423Sdim // for this java thread blocked here. Once we decide about 183243789Sdim // that we should fix this. 184249423Sdim sig = os::signal_wait(); 185249423Sdim } 186249423Sdim if (sig == os::sigexitnum_pd()) { 187249423Sdim // Terminate the signal thread 188249423Sdim return; 189249423Sdim } 190249423Sdim 191249423Sdim switch (sig) { 192249423Sdim case SIGBREAK: { 193249423Sdim // Check if the signal is a trigger to start the Attach Listener - in that 194249423Sdim // case don't print stack traces. 195249423Sdim if (!DisableAttachMechanism && AttachListener::is_init_trigger()) { 196249423Sdim continue; 197249423Sdim } 198249423Sdim // Print stack traces 199249423Sdim // Any SIGBREAK operations added here should make sure to flush 200249423Sdim // the output stream (e.g. tty->flush()) after output. See 4803766. 201249423Sdim // Each module also prints an extra carriage return after its output. 202249423Sdim VM_PrintThreads op; 203249423Sdim VMThread::execute(&op); 204249423Sdim VM_PrintJNI jni_op; 205249423Sdim VMThread::execute(&jni_op); 206249423Sdim VM_FindDeadlocks op1(tty); 207249423Sdim VMThread::execute(&op1); 208243789Sdim Universe::print_heap_at_SIGBREAK(); 209249423Sdim if (PrintClassHistogram) { 210249423Sdim VM_GC_HeapInspection op1(gclog_or_tty, true /* force full GC before heap inspection */, 211249423Sdim true /* need_prologue */); 212243789Sdim VMThread::execute(&op1); 213249423Sdim } 214249423Sdim if (JvmtiExport::should_post_data_dump()) { 215243789Sdim JvmtiExport::post_data_dump(); 216249423Sdim } 217249423Sdim break; 218249423Sdim } 219249423Sdim default: { 220249423Sdim // Dispatch the signal to java 221249423Sdim HandleMark hm(THREAD); 222249423Sdim klassOop k = SystemDictionary::resolve_or_null(vmSymbolHandles::sun_misc_Signal(), THREAD); 223249423Sdim KlassHandle klass (THREAD, k); 224249423Sdim if (klass.not_null()) { 225249423Sdim JavaValue result(T_VOID); 226249423Sdim JavaCallArguments args; 227249423Sdim args.push_int(sig); 228249423Sdim JavaCalls::call_static( 229249423Sdim &result, 230249423Sdim klass, 231249423Sdim vmSymbolHandles::dispatch_name(), 232249423Sdim vmSymbolHandles::int_void_signature(), 233249423Sdim &args, 234249423Sdim THREAD 235249423Sdim ); 236249423Sdim } 237243789Sdim if (HAS_PENDING_EXCEPTION) { 238243789Sdim // tty is initialized early so we don't expect it to be null, but 239243789Sdim // if it is we can't risk doing an initialization that might 240243789Sdim // trigger additional out-of-memory conditions 241243789Sdim if (tty != NULL) { 242243789Sdim char klass_name[256]; 243243789Sdim char tmp_sig_name[16]; 244243789Sdim const char* sig_name = "UNKNOWN"; 245243789Sdim instanceKlass::cast(PENDING_EXCEPTION->klass())-> 246243789Sdim name()->as_klass_external_name(klass_name, 256); 247243789Sdim if (os::exception_name(sig, tmp_sig_name, 16) != NULL) 248243789Sdim sig_name = tmp_sig_name; 249243789Sdim warning("Exception %s occurred dispatching signal %s to handler" 250243789Sdim "- the VM may need to be forcibly terminated", 251243789Sdim klass_name, sig_name ); 252243789Sdim } 253243789Sdim CLEAR_PENDING_EXCEPTION; 254243789Sdim } 255243789Sdim } 256243789Sdim } 257243789Sdim } 258243789Sdim} 259243789Sdim 260243789Sdim 261243789Sdimvoid os::signal_init() { 262243789Sdim if (!ReduceSignalUsage) { 263243789Sdim // Setup JavaThread for processing signals 264243789Sdim EXCEPTION_MARK; 265243789Sdim klassOop k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_Thread(), true, CHECK); 266243789Sdim instanceKlassHandle klass (THREAD, k); 267243789Sdim instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); 268243789Sdim 269243789Sdim const char thread_name[] = "Signal Dispatcher"; 270243789Sdim Handle string = java_lang_String::create_from_str(thread_name, CHECK); 271243789Sdim 272243789Sdim // Initialize thread_oop to put it into the system threadGroup 273243789Sdim Handle thread_group (THREAD, Universe::system_thread_group()); 274243789Sdim JavaValue result(T_VOID); 275243789Sdim JavaCalls::call_special(&result, thread_oop, 276243789Sdim klass, 277243789Sdim vmSymbolHandles::object_initializer_name(), 278243789Sdim vmSymbolHandles::threadgroup_string_void_signature(), 279243789Sdim thread_group, 280243789Sdim string, 281243789Sdim CHECK); 282243789Sdim 283243789Sdim KlassHandle group(THREAD, SystemDictionary::ThreadGroup_klass()); 284243789Sdim JavaCalls::call_special(&result, 285243789Sdim thread_group, 286243789Sdim group, 287243789Sdim vmSymbolHandles::add_method_name(), 288243789Sdim vmSymbolHandles::thread_void_signature(), 289243789Sdim thread_oop, // ARG 1 290243789Sdim CHECK); 291243789Sdim 292243789Sdim os::signal_init_pd(); 293243789Sdim 294243789Sdim { MutexLocker mu(Threads_lock); 295243789Sdim JavaThread* signal_thread = new JavaThread(&signal_thread_entry); 296243789Sdim 297243789Sdim // At this point it may be possible that no osthread was created for the 298243789Sdim // JavaThread due to lack of memory. We would have to throw an exception 299243789Sdim // in that case. However, since this must work and we do not allow 300243789Sdim // exceptions anyway, check and abort if this fails. 301243789Sdim if (signal_thread == NULL || signal_thread->osthread() == NULL) { 302243789Sdim vm_exit_during_initialization("java.lang.OutOfMemoryError", 303243789Sdim "unable to create new native thread"); 304243789Sdim } 305243789Sdim 306243789Sdim java_lang_Thread::set_thread(thread_oop(), signal_thread); 307243789Sdim java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); 308243789Sdim java_lang_Thread::set_daemon(thread_oop()); 309243789Sdim 310243789Sdim signal_thread->set_threadObj(thread_oop()); 311243789Sdim Threads::add(signal_thread); 312243789Sdim Thread::start(signal_thread); 313243789Sdim } 314243789Sdim // Handle ^BREAK 315243789Sdim os::signal(SIGBREAK, os::user_handler()); 316243789Sdim } 317243789Sdim} 318243789Sdim 319243789Sdim 320243789Sdimvoid os::terminate_signal_thread() { 321243789Sdim if (!ReduceSignalUsage) 322243789Sdim signal_notify(sigexitnum_pd()); 323243789Sdim} 324243789Sdim 325243789Sdim 326243789Sdim// --------------------- loading libraries --------------------- 327243789Sdim 328243789Sdimtypedef jint (JNICALL *JNI_OnLoad_t)(JavaVM *, void *); 329243789Sdimextern struct JavaVM_ main_vm; 330243789Sdim 331243789Sdimstatic void* _native_java_library = NULL; 332243789Sdim 333243789Sdimvoid* os::native_java_library() { 334243789Sdim if (_native_java_library == NULL) { 335243789Sdim char buffer[JVM_MAXPATHLEN]; 336243789Sdim char ebuf[1024]; 337243789Sdim 338243789Sdim // Try to load verify dll first. In 1.3 java dll depends on it and is not 339243789Sdim // always able to find it when the loading executable is outside the JDK. 340243789Sdim // In order to keep working with 1.2 we ignore any loading errors. 341243789Sdim dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), "verify"); 342243789Sdim dll_load(buffer, ebuf, sizeof(ebuf)); 343243789Sdim 344243789Sdim // Load java dll 345243789Sdim dll_build_name(buffer, sizeof(buffer), Arguments::get_dll_dir(), "java"); 346243789Sdim _native_java_library = dll_load(buffer, ebuf, sizeof(ebuf)); 347243789Sdim if (_native_java_library == NULL) { 348243789Sdim vm_exit_during_initialization("Unable to load native library", ebuf); 349243789Sdim } 350243789Sdim } 351243789Sdim static jboolean onLoaded = JNI_FALSE; 352243789Sdim if (onLoaded) { 353243789Sdim // We may have to wait to fire OnLoad until TLS is initialized. 354243789Sdim if (ThreadLocalStorage::is_initialized()) { 355243789Sdim // The JNI_OnLoad handling is normally done by method load in 356243789Sdim // java.lang.ClassLoader$NativeLibrary, but the VM loads the base library 357243789Sdim // explicitly so we have to check for JNI_OnLoad as well 358243789Sdim const char *onLoadSymbols[] = JNI_ONLOAD_SYMBOLS; 359243789Sdim JNI_OnLoad_t JNI_OnLoad = CAST_TO_FN_PTR( 360243789Sdim JNI_OnLoad_t, dll_lookup(_native_java_library, onLoadSymbols[0])); 361243789Sdim if (JNI_OnLoad != NULL) { 362243789Sdim JavaThread* thread = JavaThread::current(); 363243789Sdim ThreadToNativeFromVM ttn(thread); 364243789Sdim HandleMark hm(thread); 365243789Sdim jint ver = (*JNI_OnLoad)(&main_vm, NULL); 366243789Sdim onLoaded = JNI_TRUE; 367243789Sdim if (!Threads::is_supported_jni_version_including_1_1(ver)) { 368243789Sdim vm_exit_during_initialization("Unsupported JNI version"); 369243789Sdim } 370243789Sdim } 371243789Sdim } 372243789Sdim } 373243789Sdim return _native_java_library; 374243789Sdim} 375243789Sdim 376243789Sdim// --------------------- heap allocation utilities --------------------- 377243789Sdim 378243789Sdimchar *os::strdup(const char *str) { 379243789Sdim size_t size = strlen(str); 380243789Sdim char *dup_str = (char *)malloc(size + 1); 381243789Sdim if (dup_str == NULL) return NULL; 382243789Sdim strcpy(dup_str, str); 383243789Sdim return dup_str; 384243789Sdim} 385243789Sdim 386243789Sdim 387243789Sdim 388243789Sdim#ifdef ASSERT 389243789Sdim#define space_before (MallocCushion + sizeof(double)) 390243789Sdim#define space_after MallocCushion 391243789Sdim#define size_addr_from_base(p) (size_t*)(p + space_before - sizeof(size_t)) 392243789Sdim#define size_addr_from_obj(p) ((size_t*)p - 1) 393243789Sdim// MallocCushion: size of extra cushion allocated around objects with +UseMallocOnly 394243789Sdim// NB: cannot be debug variable, because these aren't set from the command line until 395249423Sdim// *after* the first few allocs already happened 396243789Sdim#define MallocCushion 16 397243789Sdim#else 398243789Sdim#define space_before 0 399243789Sdim#define space_after 0 400243789Sdim#define size_addr_from_base(p) should not use w/o ASSERT 401243789Sdim#define size_addr_from_obj(p) should not use w/o ASSERT 402243789Sdim#define MallocCushion 0 403243789Sdim#endif 404243789Sdim#define paranoid 0 /* only set to 1 if you suspect checking code has bug */ 405243789Sdim 406243789Sdim#ifdef ASSERT 407243789Sdiminline size_t get_size(void* obj) { 408243789Sdim size_t size = *size_addr_from_obj(obj); 409243789Sdim if (size < 0) { 410243789Sdim fatal(err_msg("free: size field of object #" PTR_FORMAT " was overwritten (" 411243789Sdim SIZE_FORMAT ")", obj, size)); 412243789Sdim } 413243789Sdim return size; 414243789Sdim} 415243789Sdim 416243789Sdimu_char* find_cushion_backwards(u_char* start) { 417243789Sdim u_char* p = start; 418243789Sdim while (p[ 0] != badResourceValue || p[-1] != badResourceValue || 419243789Sdim p[-2] != badResourceValue || p[-3] != badResourceValue) p--; 420243789Sdim // ok, we have four consecutive marker bytes; find start 421243789Sdim u_char* q = p - 4; 422243789Sdim while (*q == badResourceValue) q--; 423243789Sdim return q + 1; 424243789Sdim} 425243789Sdim 426243789Sdimu_char* find_cushion_forwards(u_char* start) { 427243789Sdim u_char* p = start; 428243789Sdim while (p[0] != badResourceValue || p[1] != badResourceValue || 429243789Sdim p[2] != badResourceValue || p[3] != badResourceValue) p++; 430243789Sdim // ok, we have four consecutive marker bytes; find end of cushion 431243789Sdim u_char* q = p + 4; 432243789Sdim while (*q == badResourceValue) q++; 433243789Sdim return q - MallocCushion; 434243789Sdim} 435243789Sdim 436243789Sdimvoid print_neighbor_blocks(void* ptr) { 437243789Sdim // find block allocated before ptr (not entirely crash-proof) 438243789Sdim if (MallocCushion < 4) { 439243789Sdim tty->print_cr("### cannot find previous block (MallocCushion < 4)"); 440243789Sdim return; 441243789Sdim } 442243789Sdim u_char* start_of_this_block = (u_char*)ptr - space_before; 443243789Sdim u_char* end_of_prev_block_data = start_of_this_block - space_after -1; 444243789Sdim // look for cushion in front of prev. block 445243789Sdim u_char* start_of_prev_block = find_cushion_backwards(end_of_prev_block_data); 446243789Sdim ptrdiff_t size = *size_addr_from_base(start_of_prev_block); 447243789Sdim u_char* obj = start_of_prev_block + space_before; 448243789Sdim if (size <= 0 ) { 449243789Sdim // start is bad; mayhave been confused by OS data inbetween objects 450243789Sdim // search one more backwards 451243789Sdim start_of_prev_block = find_cushion_backwards(start_of_prev_block); 452243789Sdim size = *size_addr_from_base(start_of_prev_block); 453243789Sdim obj = start_of_prev_block + space_before; 454243789Sdim } 455243789Sdim 456243789Sdim if (start_of_prev_block + space_before + size + space_after == start_of_this_block) { 457243789Sdim tty->print_cr("### previous object: %p (%ld bytes)", obj, size); 458243789Sdim } else { 459243789Sdim tty->print_cr("### previous object (not sure if correct): %p (%ld bytes)", obj, size); 460243789Sdim } 461243789Sdim 462243789Sdim // now find successor block 463243789Sdim u_char* start_of_next_block = (u_char*)ptr + *size_addr_from_obj(ptr) + space_after; 464243789Sdim start_of_next_block = find_cushion_forwards(start_of_next_block); 465249423Sdim u_char* next_obj = start_of_next_block + space_before; 466249423Sdim ptrdiff_t next_size = *size_addr_from_base(start_of_next_block); 467249423Sdim if (start_of_next_block[0] == badResourceValue && 468249423Sdim start_of_next_block[1] == badResourceValue && 469249423Sdim start_of_next_block[2] == badResourceValue && 470249423Sdim start_of_next_block[3] == badResourceValue) { 471249423Sdim tty->print_cr("### next object: %p (%ld bytes)", next_obj, next_size); 472249423Sdim } else { 473243789Sdim tty->print_cr("### next object (not sure if correct): %p (%ld bytes)", next_obj, next_size); 474249423Sdim } 475249423Sdim} 476243789Sdim 477243789Sdim 478243789Sdimvoid report_heap_error(void* memblock, void* bad, const char* where) { 479243789Sdim tty->print_cr("## nof_mallocs = %d, nof_frees = %d", os::num_mallocs, os::num_frees); 480243789Sdim tty->print_cr("## memory stomp: byte at %p %s object %p", bad, where, memblock); 481243789Sdim print_neighbor_blocks(memblock); 482243789Sdim fatal("memory stomping error"); 483249423Sdim} 484249423Sdim 485249423Sdimvoid verify_block(void* memblock) { 486249423Sdim size_t size = get_size(memblock); 487243789Sdim if (MallocCushion) { 488249423Sdim u_char* ptr = (u_char*)memblock - space_before; 489249423Sdim for (int i = 0; i < MallocCushion; i++) { 490249423Sdim if (ptr[i] != badResourceValue) { 491243789Sdim report_heap_error(memblock, ptr+i, "in front of"); 492243789Sdim } 493243789Sdim } 494249423Sdim u_char* end = (u_char*)memblock + size + space_after; 495249423Sdim for (int j = -MallocCushion; j < 0; j++) { 496249423Sdim if (end[j] != badResourceValue) { 497249423Sdim report_heap_error(memblock, end+j, "after"); 498243789Sdim } 499243789Sdim } 500249423Sdim } 501243789Sdim} 502249423Sdim#endif 503249423Sdim 504249423Sdimvoid* os::malloc(size_t size) { 505243789Sdim NOT_PRODUCT(num_mallocs++); 506249423Sdim NOT_PRODUCT(alloc_bytes += size); 507249423Sdim 508243789Sdim if (size == 0) { 509243789Sdim // return a valid pointer if size is zero 510243789Sdim // if NULL is returned the calling functions assume out of memory. 511243789Sdim size = 1; 512243789Sdim } 513249423Sdim 514249423Sdim NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap()); 515243789Sdim u_char* ptr = (u_char*)::malloc(size + space_before + space_after); 516243789Sdim#ifdef ASSERT 517243789Sdim if (ptr == NULL) return NULL; 518249423Sdim if (MallocCushion) { 519249423Sdim for (u_char* p = ptr; p < ptr + MallocCushion; p++) *p = (u_char)badResourceValue; 520249423Sdim u_char* end = ptr + space_before + size; 521249423Sdim for (u_char* pq = ptr+MallocCushion; pq < end; pq++) *pq = (u_char)uninitBlockPad; 522243789Sdim for (u_char* q = end; q < end + MallocCushion; q++) *q = (u_char)badResourceValue; 523243789Sdim } 524243789Sdim // put size just before data 525243789Sdim *size_addr_from_base(ptr) = size; 526243789Sdim#endif 527243789Sdim u_char* memblock = ptr + space_before; 528243789Sdim if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) { 529243789Sdim tty->print_cr("os::malloc caught, %lu bytes --> %p", size, memblock); 530243789Sdim breakpoint(); 531243789Sdim } 532243789Sdim debug_only(if (paranoid) verify_block(memblock)); 533243789Sdim if (PrintMalloc && tty != NULL) tty->print_cr("os::malloc %lu bytes --> %p", size, memblock); 534243789Sdim return memblock; 535249423Sdim} 536249423Sdim 537249423Sdim 538249423Sdimvoid* os::realloc(void *memblock, size_t size) { 539249423Sdim NOT_PRODUCT(num_mallocs++); 540249423Sdim NOT_PRODUCT(alloc_bytes += size); 541249423Sdim#ifndef ASSERT 542249423Sdim return ::realloc(memblock, size); 543249423Sdim#else 544249423Sdim if (memblock == NULL) { 545249423Sdim return os::malloc(size); 546249423Sdim } 547243789Sdim if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) { 548249423Sdim tty->print_cr("os::realloc caught %p", memblock); 549249423Sdim breakpoint(); 550249423Sdim } 551249423Sdim verify_block(memblock); 552249423Sdim NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap()); 553249423Sdim if (size == 0) return NULL; 554249423Sdim // always move the block 555249423Sdim void* ptr = malloc(size); 556249423Sdim if (PrintMalloc) tty->print_cr("os::remalloc %lu bytes, %p --> %p", size, memblock, ptr); 557249423Sdim // Copy to new memory if malloc didn't fail 558249423Sdim if ( ptr != NULL ) { 559249423Sdim memcpy(ptr, memblock, MIN2(size, get_size(memblock))); 560249423Sdim if (paranoid) verify_block(ptr); 561249423Sdim if ((intptr_t)ptr == (intptr_t)MallocCatchPtr) { 562249423Sdim tty->print_cr("os::realloc caught, %lu bytes --> %p", size, ptr); 563249423Sdim breakpoint(); 564249423Sdim } 565249423Sdim free(memblock); 566249423Sdim } 567249423Sdim return ptr; 568249423Sdim#endif 569249423Sdim} 570249423Sdim 571243789Sdim 572243789Sdimvoid os::free(void *memblock) { 573243789Sdim NOT_PRODUCT(num_frees++); 574243789Sdim#ifdef ASSERT 575243789Sdim if (memblock == NULL) return; 576243789Sdim if ((intptr_t)memblock == (intptr_t)MallocCatchPtr) { 577243789Sdim if (tty != NULL) tty->print_cr("os::free caught %p", memblock); 578249423Sdim breakpoint(); 579249423Sdim } 580249423Sdim verify_block(memblock); 581243789Sdim if (PrintMalloc && tty != NULL) 582243789Sdim // tty->print_cr("os::free %p", memblock); 583243789Sdim fprintf(stderr, "os::free %p\n", memblock); 584249423Sdim NOT_PRODUCT(if (MallocVerifyInterval > 0) check_heap()); 585249423Sdim // Added by detlefs. 586243789Sdim if (MallocCushion) { 587243789Sdim u_char* ptr = (u_char*)memblock - space_before; 588243789Sdim for (u_char* p = ptr; p < ptr + MallocCushion; p++) { 589243789Sdim guarantee(*p == badResourceValue, 590249423Sdim "Thing freed should be malloc result."); 591243789Sdim *p = (u_char)freeBlockPad; 592243789Sdim } 593243789Sdim size_t size = get_size(memblock); 594249423Sdim u_char* end = ptr + space_before + size; 595243789Sdim for (u_char* q = end; q < end + MallocCushion; q++) { 596243789Sdim guarantee(*q == badResourceValue, 597249423Sdim "Thing freed should be malloc result."); 598249423Sdim *q = (u_char)freeBlockPad; 599249423Sdim } 600249423Sdim } 601249423Sdim#endif 602249423Sdim ::free((char*)memblock - space_before); 603249423Sdim} 604249423Sdim 605249423Sdimvoid os::init_random(long initval) { 606249423Sdim _rand_seed = initval; 607249423Sdim} 608249423Sdim 609243789Sdim 610243789Sdimlong os::random() { 611249423Sdim /* standard, well-known linear congruential random generator with 612243789Sdim * next_rand = (16807*seed) mod (2**31-1) 613249423Sdim * see 614249423Sdim * (1) "Random Number Generators: Good Ones Are Hard to Find", 615243789Sdim * S.K. Park and K.W. Miller, Communications of the ACM 31:10 (Oct 1988), 616249423Sdim * (2) "Two Fast Implementations of the 'Minimal Standard' Random 617243789Sdim * Number Generator", David G. Carta, Comm. ACM 33, 1 (Jan 1990), pp. 87-88. 618249423Sdim */ 619249423Sdim const long a = 16807; 620249423Sdim const unsigned long m = 2147483647; 621249423Sdim const long q = m / a; assert(q == 127773, "weird math"); 622249423Sdim const long r = m % a; assert(r == 2836, "weird math"); 623249423Sdim 624249423Sdim // compute az=2^31p+q 625243789Sdim unsigned long lo = a * (long)(_rand_seed & 0xFFFF); 626243789Sdim unsigned long hi = a * (long)((unsigned long)_rand_seed >> 16); 627243789Sdim lo += (hi & 0x7FFF) << 16; 628243789Sdim 629243789Sdim // if q overflowed, ignore the overflow and increment q 630243789Sdim if (lo > m) { 631249423Sdim lo &= m; 632249423Sdim ++lo; 633243789Sdim } 634243789Sdim lo += hi >> 15; 635249423Sdim 636249423Sdim // if (p+q) overflowed, ignore the overflow and increment (p+q) 637243789Sdim if (lo > m) { 638243789Sdim lo &= m; 639243789Sdim ++lo; 640243789Sdim } 641243789Sdim return (_rand_seed = lo); 642243789Sdim} 643243789Sdim 644243789Sdim// The INITIALIZED state is distinguished from the SUSPENDED state because the 645243789Sdim// conditions in which a thread is first started are different from those in which 646243789Sdim// a suspension is resumed. These differences make it hard for us to apply the 647243789Sdim// tougher checks when starting threads that we want to do when resuming them. 648243789Sdim// However, when start_thread is called as a result of Thread.start, on a Java 649249423Sdim// thread, the operation is synchronized on the Java Thread object. So there 650243789Sdim// cannot be a race to start the thread and hence for the thread to exit while 651243789Sdim// we are working on it. Non-Java threads that start Java threads either have 652243789Sdim// to do so in a context in which races are impossible, or should do appropriate 653243789Sdim// locking. 654243789Sdim 655243789Sdimvoid os::start_thread(Thread* thread) { 656243789Sdim // guard suspend/resume 657243789Sdim MutexLockerEx ml(thread->SR_lock(), Mutex::_no_safepoint_check_flag); 658243789Sdim OSThread* osthread = thread->osthread(); 659243789Sdim osthread->set_state(RUNNABLE); 660243789Sdim pd_start_thread(thread); 661243789Sdim} 662249423Sdim 663243789Sdim//--------------------------------------------------------------------------- 664243789Sdim// Helper functions for fatal error handler 665243789Sdim 666243789Sdimvoid os::print_hex_dump(outputStream* st, address start, address end, int unitsize) { 667243789Sdim assert(unitsize == 1 || unitsize == 2 || unitsize == 4 || unitsize == 8, "just checking"); 668243789Sdim 669243789Sdim int cols = 0; 670243789Sdim int cols_per_line = 0; 671243789Sdim switch (unitsize) { 672243789Sdim case 1: cols_per_line = 16; break; 673243789Sdim case 2: cols_per_line = 8; break; 674243789Sdim case 4: cols_per_line = 4; break; 675243789Sdim case 8: cols_per_line = 2; break; 676243789Sdim default: return; 677243789Sdim } 678243789Sdim 679243789Sdim address p = start; 680243789Sdim st->print(PTR_FORMAT ": ", start); 681243789Sdim while (p < end) { 682243789Sdim switch (unitsize) { 683243789Sdim case 1: st->print("%02x", *(u1*)p); break; 684243789Sdim case 2: st->print("%04x", *(u2*)p); break; 685249423Sdim case 4: st->print("%08x", *(u4*)p); break; 686243789Sdim case 8: st->print("%016" FORMAT64_MODIFIER "x", *(u8*)p); break; 687249423Sdim } 688249423Sdim p += unitsize; 689249423Sdim cols++; 690249423Sdim if (cols >= cols_per_line && p < end) { 691243789Sdim cols = 0; 692243789Sdim st->cr(); 693243789Sdim st->print(PTR_FORMAT ": ", p); 694249423Sdim } else { 695249423Sdim st->print(" "); 696243789Sdim } 697249423Sdim } 698243789Sdim st->cr(); 699243789Sdim} 700249423Sdim 701243789Sdimvoid os::print_environment_variables(outputStream* st, const char** env_list, 702243789Sdim char* buffer, int len) { 703243789Sdim if (env_list) { 704243789Sdim st->print_cr("Environment Variables:"); 705243789Sdim 706243789Sdim for (int i = 0; env_list[i] != NULL; i++) { 707243789Sdim if (getenv(env_list[i], buffer, len)) { 708243789Sdim st->print(env_list[i]); 709243789Sdim st->print("="); 710243789Sdim st->print_cr(buffer); 711243789Sdim } 712243789Sdim } 713243789Sdim } 714243789Sdim} 715243789Sdim 716243789Sdimvoid os::print_cpu_info(outputStream* st) { 717243789Sdim // cpu 718243789Sdim st->print("CPU:"); 719243789Sdim st->print("total %d", os::processor_count()); 720249423Sdim // It's not safe to query number of active processors after crash 721243789Sdim // st->print("(active %d)", os::active_processor_count()); 722243789Sdim st->print(" %s", VM_Version::cpu_features()); 723243789Sdim st->cr(); 724243789Sdim} 725243789Sdim 726243789Sdimvoid os::print_date_and_time(outputStream *st) { 727249423Sdim time_t tloc; 728243789Sdim (void)time(&tloc); 729243789Sdim st->print("time: %s", ctime(&tloc)); // ctime adds newline. 730243789Sdim 731243789Sdim double t = os::elapsedTime(); 732243789Sdim // NOTE: It tends to crash after a SEGV if we want to printf("%f",...) in 733243789Sdim // Linux. Must be a bug in glibc ? Workaround is to round "t" to int 734243789Sdim // before printf. We lost some precision, but who cares? 735243789Sdim st->print_cr("elapsed time: %d seconds", (int)t); 736243789Sdim} 737243789Sdim 738243789Sdim 739243789Sdim// Looks like all platforms except IA64 can use the same function to check 740243789Sdim// if C stack is walkable beyond current frame. The check for fp() is not 741243789Sdim// necessary on Sparc, but it's harmless. 742243789Sdimbool os::is_first_C_frame(frame* fr) { 743243789Sdim#ifdef IA64 744243789Sdim // In order to walk native frames on Itanium, we need to access the unwind 745243789Sdim // table, which is inside ELF. We don't want to parse ELF after fatal error, 746243789Sdim // so return true for IA64. If we need to support C stack walking on IA64, 747243789Sdim // this function needs to be moved to CPU specific files, as fp() on IA64 748249423Sdim // is register stack, which grows towards higher memory address. 749249423Sdim return true; 750249423Sdim#endif 751249423Sdim 752249423Sdim // Load up sp, fp, sender sp and sender fp, check for reasonable values. 753249423Sdim // Check usp first, because if that's bad the other accessors may fault 754243789Sdim // on some architectures. Ditto ufp second, etc. 755243789Sdim uintptr_t fp_align_mask = (uintptr_t)(sizeof(address)-1); 756243789Sdim // sp on amd can be 32 bit aligned. 757243789Sdim uintptr_t sp_align_mask = (uintptr_t)(sizeof(int)-1); 758243789Sdim 759249423Sdim uintptr_t usp = (uintptr_t)fr->sp(); 760243789Sdim if ((usp & sp_align_mask) != 0) return true; 761243789Sdim 762243789Sdim uintptr_t ufp = (uintptr_t)fr->fp(); 763249423Sdim if ((ufp & fp_align_mask) != 0) return true; 764249423Sdim 765243789Sdim uintptr_t old_sp = (uintptr_t)fr->sender_sp(); 766243789Sdim if ((old_sp & sp_align_mask) != 0) return true; 767243789Sdim if (old_sp == 0 || old_sp == (uintptr_t)-1) return true; 768243789Sdim 769249423Sdim uintptr_t old_fp = (uintptr_t)fr->link(); 770249423Sdim if ((old_fp & fp_align_mask) != 0) return true; 771249423Sdim if (old_fp == 0 || old_fp == (uintptr_t)-1 || old_fp == ufp) return true; 772243789Sdim 773243789Sdim // stack grows downwards; if old_fp is below current fp or if the stack 774243789Sdim // frame is too large, either the stack is corrupted or fp is not saved 775243789Sdim // on stack (i.e. on x86, ebp may be used as general register). The stack 776249423Sdim // is not walkable beyond current frame. 777243789Sdim if (old_fp < ufp) return true; 778249423Sdim if (old_fp - ufp > 64 * K) return true; 779243789Sdim 780249423Sdim return false; 781249423Sdim} 782243789Sdim 783243789Sdim#ifdef ASSERT 784243789Sdimextern "C" void test_random() { 785243789Sdim const double m = 2147483647; 786243789Sdim double mean = 0.0, variance = 0.0, t; 787243789Sdim long reps = 10000; 788249423Sdim unsigned long seed = 1; 789243789Sdim 790243789Sdim tty->print_cr("seed %ld for %ld repeats...", seed, reps); 791243789Sdim os::init_random(seed); 792249423Sdim long num; 793249423Sdim for (int k = 0; k < reps; k++) { 794243789Sdim num = os::random(); 795243789Sdim double u = (double)num / m; 796243789Sdim assert(u >= 0.0 && u <= 1.0, "bad random number!"); 797243789Sdim 798243789Sdim // calculate mean and variance of the random sequence 799249423Sdim mean += u; 800249423Sdim variance += (u*u); 801249423Sdim } 802243789Sdim mean /= reps; 803243789Sdim variance /= (reps - 1); 804243789Sdim 805243789Sdim assert(num == 1043618065, "bad seed"); 806243789Sdim tty->print_cr("mean of the 1st 10000 numbers: %f", mean); 807243789Sdim tty->print_cr("variance of the 1st 10000 numbers: %f", variance); 808243789Sdim const double eps = 0.0001; 809243789Sdim t = fabsd(mean - 0.5018); 810243789Sdim assert(t < eps, "bad mean"); 811243789Sdim t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355; 812243789Sdim assert(t < eps, "bad variance"); 813243789Sdim} 814243789Sdim#endif 815243789Sdim 816243789Sdim 817243789Sdim// Set up the boot classpath. 818243789Sdim 819243789Sdimchar* os::format_boot_path(const char* format_string, 820243789Sdim const char* home, 821243789Sdim int home_len, 822249423Sdim char fileSep, 823249423Sdim char pathSep) { 824243789Sdim assert((fileSep == '/' && pathSep == ':') || 825249423Sdim (fileSep == '\\' && pathSep == ';'), "unexpected seperator chars"); 826243789Sdim 827249423Sdim // Scan the format string to determine the length of the actual 828249423Sdim // boot classpath, and handle platform dependencies as well. 829249423Sdim int formatted_path_len = 0; 830243789Sdim const char* p; 831243789Sdim for (p = format_string; *p != 0; ++p) { 832243789Sdim if (*p == '%') formatted_path_len += home_len - 1; 833243789Sdim ++formatted_path_len; 834243789Sdim } 835249423Sdim 836249423Sdim char* formatted_path = NEW_C_HEAP_ARRAY(char, formatted_path_len + 1); 837249423Sdim if (formatted_path == NULL) { 838243789Sdim return NULL; 839243789Sdim } 840243789Sdim 841243789Sdim // Create boot classpath from format, substituting separator chars and 842243789Sdim // java home directory. 843243789Sdim char* q = formatted_path; 844243789Sdim for (p = format_string; *p != 0; ++p) { 845249423Sdim switch (*p) { 846243789Sdim case '%': 847243789Sdim strcpy(q, home); 848249423Sdim q += home_len; 849243789Sdim break; 850243789Sdim case '/': 851249423Sdim *q++ = fileSep; 852249423Sdim break; 853243789Sdim case ':': 854243789Sdim *q++ = pathSep; 855243789Sdim break; 856243789Sdim default: 857243789Sdim *q++ = *p; 858243789Sdim } 859243789Sdim } 860243789Sdim *q = '\0'; 861249423Sdim 862249423Sdim assert((q - formatted_path) == formatted_path_len, "formatted_path size botched"); 863249423Sdim return formatted_path; 864249423Sdim} 865249423Sdim 866249423Sdim 867243789Sdimbool os::set_boot_path(char fileSep, char pathSep) { 868249423Sdim const char* home = Arguments::get_java_home(); 869243789Sdim int home_len = (int)strlen(home); 870243789Sdim 871243789Sdim static const char* meta_index_dir_format = "%/lib/"; 872243789Sdim static const char* meta_index_format = "%/lib/meta-index"; 873243789Sdim char* meta_index = format_boot_path(meta_index_format, home, home_len, fileSep, pathSep); 874243789Sdim if (meta_index == NULL) return false; 875243789Sdim char* meta_index_dir = format_boot_path(meta_index_dir_format, home, home_len, fileSep, pathSep); 876243789Sdim if (meta_index_dir == NULL) return false; 877243789Sdim Arguments::set_meta_index_path(meta_index, meta_index_dir); 878243789Sdim 879243789Sdim // Any modification to the JAR-file list, for the boot classpath must be 880249423Sdim // aligned with install/install/make/common/Pack.gmk. Note: boot class 881243789Sdim // path class JARs, are stripped for StackMapTable to reduce download size. 882243789Sdim static const char classpath_format[] = 883243789Sdim "%/lib/resources.jar:" 884243789Sdim "%/lib/rt.jar:" 885243789Sdim "%/lib/sunrsasign.jar:" 886243789Sdim "%/lib/jsse.jar:" 887249423Sdim "%/lib/jce.jar:" 888243789Sdim "%/lib/charsets.jar:" 889243789Sdim 890243789Sdim // ## TEMPORARY hack to keep the legacy launcher working when 891249423Sdim // ## only the boot module is installed (cf. j.l.ClassLoader) 892249423Sdim "%/lib/modules/jdk.boot.jar:" 893249423Sdim 894243789Sdim "%/classes"; 895243789Sdim char* sysclasspath = format_boot_path(classpath_format, home, home_len, fileSep, pathSep); 896243789Sdim if (sysclasspath == NULL) return false; 897249423Sdim Arguments::set_sysclasspath(sysclasspath); 898249423Sdim 899249423Sdim return true; 900249423Sdim} 901249423Sdim 902249423Sdim/* 903249423Sdim * Splits a path, based on its separator, the number of 904249423Sdim * elements is returned back in n. 905249423Sdim * It is the callers responsibility to: 906249423Sdim * a> check the value of n, and n may be 0. 907249423Sdim * b> ignore any empty path elements 908249423Sdim * c> free up the data. 909243789Sdim */ 910243789Sdimchar** os::split_path(const char* path, int* n) { 911243789Sdim *n = 0; 912243789Sdim if (path == NULL || strlen(path) == 0) { 913249423Sdim return NULL; 914249423Sdim } 915249423Sdim const char psepchar = *os::path_separator(); 916249423Sdim char* inpath = (char*)NEW_C_HEAP_ARRAY(char, strlen(path) + 1); 917249423Sdim if (inpath == NULL) { 918249423Sdim return NULL; 919249423Sdim } 920243789Sdim strncpy(inpath, path, strlen(path)); 921243789Sdim int count = 1; 922243789Sdim char* p = strchr(inpath, psepchar); 923243789Sdim // Get a count of elements to allocate memory 924249423Sdim while (p != NULL) { 925249423Sdim count++; 926243789Sdim p++; 927243789Sdim p = strchr(p, psepchar); 928249423Sdim } 929249423Sdim char** opath = (char**) NEW_C_HEAP_ARRAY(char*, count); 930249423Sdim if (opath == NULL) { 931249423Sdim return NULL; 932249423Sdim } 933243789Sdim 934243789Sdim // do the actual splitting 935243789Sdim p = inpath; 936243789Sdim for (int i = 0 ; i < count ; i++) { 937243789Sdim size_t len = strcspn(p, os::path_separator()); 938243789Sdim if (len > JVM_MAXPATHLEN) { 939243789Sdim return NULL; 940243789Sdim } 941249423Sdim // allocate the string and add terminator storage 942243789Sdim char* s = (char*)NEW_C_HEAP_ARRAY(char, len + 1); 943243789Sdim if (s == NULL) { 944243789Sdim return NULL; 945243789Sdim } 946249423Sdim strncpy(s, p, len); 947249423Sdim s[len] = '\0'; 948243789Sdim opath[i] = s; 949243789Sdim p += len + 1; 950249423Sdim } 951243789Sdim FREE_C_HEAP_ARRAY(char, inpath); 952243789Sdim *n = count; 953243789Sdim return opath; 954243789Sdim} 955243789Sdim 956243789Sdimvoid os::set_memory_serialize_page(address page) { 957249423Sdim int count = log2_intptr(sizeof(class JavaThread)) - log2_intptr(64); 958249423Sdim _mem_serialize_page = (volatile int32_t *)page; 959243789Sdim // We initialize the serialization page shift count here 960243789Sdim // We assume a cache line size of 64 bytes 961243789Sdim assert(SerializePageShiftCount == count, 962243789Sdim "thread size changed, fix SerializePageShiftCount constant"); 963243789Sdim set_serialize_page_mask((uintptr_t)(vm_page_size() - sizeof(int32_t))); 964243789Sdim} 965249423Sdim 966243789Sdimstatic volatile intptr_t SerializePageLock = 0; 967243789Sdim 968243789Sdim// This method is called from signal handler when SIGSEGV occurs while the current 969243789Sdim// thread tries to store to the "read-only" memory serialize page during state 970249423Sdim// transition. 971243789Sdimvoid os::block_on_serialize_page_trap() { 972243789Sdim if (TraceSafepoint) { 973249423Sdim tty->print_cr("Block until the serialize page permission restored"); 974243789Sdim } 975243789Sdim // When VMThread is holding the SerializePageLock during modifying the 976243789Sdim // access permission of the memory serialize page, the following call 977243789Sdim // will block until the permission of that page is restored to rw. 978243789Sdim // Generally, it is unsafe to manipulate locks in signal handlers, but in 979243789Sdim // this case, it's OK as the signal is synchronous and we know precisely when 980243789Sdim // it can occur. 981243789Sdim Thread::muxAcquire(&SerializePageLock, "set_memory_serialize_page"); 982249423Sdim Thread::muxRelease(&SerializePageLock); 983243789Sdim} 984243789Sdim 985243789Sdim// Serialize all thread state variables 986243789Sdimvoid os::serialize_thread_states() { 987243789Sdim // On some platforms such as Solaris & Linux, the time duration of the page 988243789Sdim // permission restoration is observed to be much longer than expected due to 989243789Sdim // scheduler starvation problem etc. To avoid the long synchronization 990243789Sdim // time and expensive page trap spinning, 'SerializePageLock' is used to block 991249423Sdim // the mutator thread if such case is encountered. See bug 6546278 for details. 992243789Sdim Thread::muxAcquire(&SerializePageLock, "serialize_thread_states"); 993243789Sdim os::protect_memory((char *)os::get_memory_serialize_page(), 994243789Sdim os::vm_page_size(), MEM_PROT_READ); 995243789Sdim os::protect_memory((char *)os::get_memory_serialize_page(), 996243789Sdim os::vm_page_size(), MEM_PROT_RW); 997243789Sdim Thread::muxRelease(&SerializePageLock); 998243789Sdim} 999243789Sdim 1000243789Sdim// Returns true if the current stack pointer is above the stack shadow 1001243789Sdim// pages, false otherwise. 1002243789Sdim 1003243789Sdimbool os::stack_shadow_pages_available(Thread *thread, methodHandle method) { 1004243789Sdim assert(StackRedPages > 0 && StackYellowPages > 0,"Sanity check"); 1005243789Sdim address sp = current_stack_pointer(); 1006243789Sdim // Check if we have StackShadowPages above the yellow zone. This parameter 1007243789Sdim // is dependent on the depth of the maximum VM call stack possible from 1008243789Sdim // the handler for stack overflow. 'instanceof' in the stack overflow 1009243789Sdim // handler or a println uses at least 8k stack of VM and native code 1010243789Sdim // respectively. 1011243789Sdim const int framesize_in_bytes = 1012243789Sdim Interpreter::size_top_interpreter_activation(method()) * wordSize; 1013243789Sdim int reserved_area = ((StackShadowPages + StackRedPages + StackYellowPages) 1014243789Sdim * vm_page_size()) + framesize_in_bytes; 1015243789Sdim // The very lower end of the stack 1016243789Sdim address stack_limit = thread->stack_base() - thread->stack_size(); 1017243789Sdim return (sp > (stack_limit + reserved_area)); 1018243789Sdim} 1019243789Sdim 1020243789Sdimsize_t os::page_size_for_region(size_t region_min_size, size_t region_max_size, 1021243789Sdim uint min_pages) 1022243789Sdim{ 1023243789Sdim assert(min_pages > 0, "sanity"); 1024243789Sdim if (UseLargePages) { 1025243789Sdim const size_t max_page_size = region_max_size / min_pages; 1026243789Sdim 1027243789Sdim for (unsigned int i = 0; _page_sizes[i] != 0; ++i) { 1028243789Sdim const size_t sz = _page_sizes[i]; 1029243789Sdim const size_t mask = sz - 1; 1030243789Sdim if ((region_min_size & mask) == 0 && (region_max_size & mask) == 0) { 1031243789Sdim // The largest page size with no fragmentation. 1032243789Sdim return sz; 1033243789Sdim } 1034243789Sdim 1035243789Sdim if (sz <= max_page_size) { 1036243789Sdim // The largest page size that satisfies the min_pages requirement. 1037243789Sdim return sz; 1038243789Sdim } 1039243789Sdim } 1040243789Sdim } 1041243789Sdim 1042243789Sdim return vm_page_size(); 1043243789Sdim} 1044243789Sdim 1045243789Sdim#ifndef PRODUCT 1046243789Sdimvoid os::trace_page_sizes(const char* str, const size_t region_min_size, 1047243789Sdim const size_t region_max_size, const size_t page_size, 1048243789Sdim const char* base, const size_t size) 1049243789Sdim{ 1050243789Sdim if (TracePageSizes) { 1051243789Sdim tty->print_cr("%s: min=" SIZE_FORMAT " max=" SIZE_FORMAT 1052243789Sdim " pg_sz=" SIZE_FORMAT " base=" PTR_FORMAT 1053243789Sdim " size=" SIZE_FORMAT, 1054243789Sdim str, region_min_size, region_max_size, 1055243789Sdim page_size, base, size); 1056243789Sdim } 1057243789Sdim} 1058243789Sdim#endif // #ifndef PRODUCT 1059243789Sdim 1060243789Sdim// This is the working definition of a server class machine: 1061243789Sdim// >= 2 physical CPU's and >=2GB of memory, with some fuzz 1062243789Sdim// because the graphics memory (?) sometimes masks physical memory. 1063243789Sdim// If you want to change the definition of a server class machine 1064243789Sdim// on some OS or platform, e.g., >=4GB on Windohs platforms, 1065243789Sdim// then you'll have to parameterize this method based on that state, 1066243789Sdim// as was done for logical processors here, or replicate and 1067243789Sdim// specialize this method for each platform. (Or fix os to have 1068243789Sdim// some inheritance structure and use subclassing. Sigh.) 1069243789Sdim// If you want some platform to always or never behave as a server 1070243789Sdim// class machine, change the setting of AlwaysActAsServerClassMachine 1071243789Sdim// and NeverActAsServerClassMachine in globals*.hpp. 1072243789Sdimbool os::is_server_class_machine() { 1073243789Sdim // First check for the early returns 1074243789Sdim if (NeverActAsServerClassMachine) { 1075243789Sdim return false; 1076243789Sdim } 1077243789Sdim if (AlwaysActAsServerClassMachine) { 1078243789Sdim return true; 1079243789Sdim } 1080243789Sdim // Then actually look at the machine 1081243789Sdim bool result = false; 1082243789Sdim const unsigned int server_processors = 2; 1083243789Sdim const julong server_memory = 2UL * G; 1084243789Sdim // We seem not to get our full complement of memory. 1085243789Sdim // We allow some part (1/8?) of the memory to be "missing", 1086243789Sdim // based on the sizes of DIMMs, and maybe graphics cards. 1087243789Sdim const julong missing_memory = 256UL * M; 1088243789Sdim 1089243789Sdim /* Is this a server class machine? */ 1090243789Sdim if ((os::active_processor_count() >= (int)server_processors) && 1091243789Sdim (os::physical_memory() >= (server_memory - missing_memory))) { 1092243789Sdim const unsigned int logical_processors = 1093243789Sdim VM_Version::logical_processors_per_package(); 1094243789Sdim if (logical_processors > 1) { 1095243789Sdim const unsigned int physical_packages = 1096243789Sdim os::active_processor_count() / logical_processors; 1097243789Sdim if (physical_packages > server_processors) { 1098249423Sdim result = true; 1099243789Sdim } 1100243789Sdim } else { 1101243789Sdim result = true; 1102243789Sdim } 1103249423Sdim } 1104249423Sdim return result; 1105249423Sdim} 1106249423Sdim