java.cpp revision 0:a61af66fc99e
1/* 2 * Copyright 1997-2007 Sun Microsystems, Inc. All Rights Reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 * 23 */ 24 25#include "incls/_precompiled.incl" 26#include "incls/_java.cpp.incl" 27 28HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown); 29 30#ifndef PRODUCT 31 32// Statistics printing (method invocation histogram) 33 34GrowableArray<methodOop>* collected_invoked_methods; 35 36void collect_invoked_methods(methodOop m) { 37 if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) { 38 collected_invoked_methods->push(m); 39 } 40} 41 42 43GrowableArray<methodOop>* collected_profiled_methods; 44 45void collect_profiled_methods(methodOop m) { 46 methodHandle mh(Thread::current(), m); 47 if ((m->method_data() != NULL) && 48 (PrintMethodData || CompilerOracle::should_print(mh))) { 49 collected_profiled_methods->push(m); 50 } 51} 52 53 54int compare_methods(methodOop* a, methodOop* b) { 55 // %%% there can be 32-bit overflow here 56 return ((*b)->invocation_count() + (*b)->compiled_invocation_count()) 57 - ((*a)->invocation_count() + (*a)->compiled_invocation_count()); 58} 59 60 61void print_method_invocation_histogram() { 62 ResourceMark rm; 63 HandleMark hm; 64 collected_invoked_methods = new GrowableArray<methodOop>(1024); 65 SystemDictionary::methods_do(collect_invoked_methods); 66 collected_invoked_methods->sort(&compare_methods); 67 // 68 tty->cr(); 69 tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = %d):", MethodHistogramCutoff); 70 tty->cr(); 71 tty->print_cr("____Count_(I+C)____Method________________________Module_________________"); 72 unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0, 73 synch_total = 0, nativ_total = 0, acces_total = 0; 74 for (int index = 0; index < collected_invoked_methods->length(); index++) { 75 methodOop m = collected_invoked_methods->at(index); 76 int c = m->invocation_count() + m->compiled_invocation_count(); 77 if (c >= MethodHistogramCutoff) m->print_invocation_count(); 78 int_total += m->invocation_count(); 79 comp_total += m->compiled_invocation_count(); 80 if (m->is_final()) final_total += c; 81 if (m->is_static()) static_total += c; 82 if (m->is_synchronized()) synch_total += c; 83 if (m->is_native()) nativ_total += c; 84 if (m->is_accessor()) acces_total += c; 85 } 86 tty->cr(); 87 total = int_total + comp_total; 88 tty->print_cr("Invocations summary:"); 89 tty->print_cr("\t%9d (%4.1f%%) interpreted", int_total, 100.0 * int_total / total); 90 tty->print_cr("\t%9d (%4.1f%%) compiled", comp_total, 100.0 * comp_total / total); 91 tty->print_cr("\t%9d (100%%) total", total); 92 tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total, 100.0 * synch_total / total); 93 tty->print_cr("\t%9d (%4.1f%%) final", final_total, 100.0 * final_total / total); 94 tty->print_cr("\t%9d (%4.1f%%) static", static_total, 100.0 * static_total / total); 95 tty->print_cr("\t%9d (%4.1f%%) native", nativ_total, 100.0 * nativ_total / total); 96 tty->print_cr("\t%9d (%4.1f%%) accessor", acces_total, 100.0 * acces_total / total); 97 tty->cr(); 98 SharedRuntime::print_call_statistics(comp_total); 99} 100 101void print_method_profiling_data() { 102 ResourceMark rm; 103 HandleMark hm; 104 collected_profiled_methods = new GrowableArray<methodOop>(1024); 105 SystemDictionary::methods_do(collect_profiled_methods); 106 collected_profiled_methods->sort(&compare_methods); 107 108 int count = collected_profiled_methods->length(); 109 if (count > 0) { 110 for (int index = 0; index < count; index++) { 111 methodOop m = collected_profiled_methods->at(index); 112 ttyLocker ttyl; 113 tty->print_cr("------------------------------------------------------------------------"); 114 //m->print_name(tty); 115 m->print_invocation_count(); 116 tty->cr(); 117 m->print_codes(); 118 } 119 tty->print_cr("------------------------------------------------------------------------"); 120 } 121} 122 123void print_bytecode_count() { 124 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) { 125 tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value()); 126 } 127} 128 129AllocStats alloc_stats; 130 131 132 133// General statistics printing (profiling ...) 134 135void print_statistics() { 136 137#ifdef ASSERT 138 139 if (CountRuntimeCalls) { 140 extern Histogram *RuntimeHistogram; 141 RuntimeHistogram->print(); 142 } 143 144 if (CountJNICalls) { 145 extern Histogram *JNIHistogram; 146 JNIHistogram->print(); 147 } 148 149 if (CountJVMCalls) { 150 extern Histogram *JVMHistogram; 151 JVMHistogram->print(); 152 } 153 154#endif 155 156 if (MemProfiling) { 157 MemProfiler::disengage(); 158 } 159 160 if (CITime) { 161 CompileBroker::print_times(); 162 } 163 164#ifdef COMPILER1 165 if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) { 166 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics); 167 Runtime1::print_statistics(); 168 Deoptimization::print_statistics(); 169 nmethod::print_statistics(); 170 } 171#endif /* COMPILER1 */ 172 173#ifdef COMPILER2 174 if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) { 175 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics); 176 Compile::print_statistics(); 177#ifndef COMPILER1 178 Deoptimization::print_statistics(); 179 nmethod::print_statistics(); 180#endif //COMPILER1 181 SharedRuntime::print_statistics(); 182 os::print_statistics(); 183 } 184 185 if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics) { 186 OptoRuntime::print_named_counters(); 187 } 188 189 if (TimeLivenessAnalysis) { 190 MethodLiveness::print_times(); 191 } 192#ifdef ASSERT 193 if (CollectIndexSetStatistics) { 194 IndexSet::print_statistics(); 195 } 196#endif // ASSERT 197#endif // COMPILER2 198 if (CountCompiledCalls) { 199 print_method_invocation_histogram(); 200 } 201 if (ProfileInterpreter || Tier1UpdateMethodData) { 202 print_method_profiling_data(); 203 } 204 if (TimeCompiler) { 205 COMPILER2_PRESENT(Compile::print_timers();) 206 } 207 if (TimeCompilationPolicy) { 208 CompilationPolicy::policy()->print_time(); 209 } 210 if (TimeOopMap) { 211 GenerateOopMap::print_time(); 212 } 213 if (ProfilerCheckIntervals) { 214 PeriodicTask::print_intervals(); 215 } 216 if (PrintSymbolTableSizeHistogram) { 217 SymbolTable::print_histogram(); 218 } 219 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) { 220 BytecodeCounter::print(); 221 } 222 if (PrintBytecodePairHistogram) { 223 BytecodePairHistogram::print(); 224 } 225 226 if (PrintCodeCache) { 227 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 228 CodeCache::print(); 229 } 230 231 if (PrintCodeCache2) { 232 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 233 CodeCache::print_internals(); 234 } 235 236 if (PrintClassStatistics) { 237 SystemDictionary::print_class_statistics(); 238 } 239 if (PrintMethodStatistics) { 240 SystemDictionary::print_method_statistics(); 241 } 242 243 if (PrintVtableStats) { 244 klassVtable::print_statistics(); 245 klassItable::print_statistics(); 246 } 247 if (VerifyOops) { 248 tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count()); 249 } 250 251 print_bytecode_count(); 252 if (WizardMode) { 253 tty->print("allocation stats: "); 254 alloc_stats.print(); 255 tty->cr(); 256 } 257 258 if (PrintSystemDictionaryAtExit) { 259 SystemDictionary::print(); 260 } 261 262 if (PrintBiasedLockingStatistics) { 263 BiasedLocking::print_counters(); 264 } 265 266#ifdef ENABLE_ZAP_DEAD_LOCALS 267#ifdef COMPILER2 268 if (ZapDeadCompiledLocals) { 269 tty->print_cr("Compile::CompiledZap_count = %d", Compile::CompiledZap_count); 270 tty->print_cr("OptoRuntime::ZapDeadCompiledLocals_count = %d", OptoRuntime::ZapDeadCompiledLocals_count); 271 } 272#endif // COMPILER2 273#endif // ENABLE_ZAP_DEAD_LOCALS 274} 275 276#else // PRODUCT MODE STATISTICS 277 278void print_statistics() { 279 280 if (CITime) { 281 CompileBroker::print_times(); 282 } 283#ifdef COMPILER2 284 if (PrintPreciseBiasedLockingStatistics) { 285 OptoRuntime::print_named_counters(); 286 } 287#endif 288 if (PrintBiasedLockingStatistics) { 289 BiasedLocking::print_counters(); 290 } 291} 292 293#endif 294 295 296// Helper class for registering on_exit calls through JVM_OnExit 297 298extern "C" { 299 typedef void (*__exit_proc)(void); 300} 301 302class ExitProc : public CHeapObj { 303 private: 304 __exit_proc _proc; 305 // void (*_proc)(void); 306 ExitProc* _next; 307 public: 308 // ExitProc(void (*proc)(void)) { 309 ExitProc(__exit_proc proc) { 310 _proc = proc; 311 _next = NULL; 312 } 313 void evaluate() { _proc(); } 314 ExitProc* next() const { return _next; } 315 void set_next(ExitProc* next) { _next = next; } 316}; 317 318 319// Linked list of registered on_exit procedures 320 321static ExitProc* exit_procs = NULL; 322 323 324extern "C" { 325 void register_on_exit_function(void (*func)(void)) { 326 ExitProc *entry = new ExitProc(func); 327 // Classic vm does not throw an exception in case the allocation failed, 328 if (entry != NULL) { 329 entry->set_next(exit_procs); 330 exit_procs = entry; 331 } 332 } 333} 334 335// Note: before_exit() can be executed only once, if more than one threads 336// are trying to shutdown the VM at the same time, only one thread 337// can run before_exit() and all other threads must wait. 338void before_exit(JavaThread * thread) { 339 #define BEFORE_EXIT_NOT_RUN 0 340 #define BEFORE_EXIT_RUNNING 1 341 #define BEFORE_EXIT_DONE 2 342 static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN; 343 344 // Note: don't use a Mutex to guard the entire before_exit(), as 345 // JVMTI post_thread_end_event and post_vm_death_event will run native code. 346 // A CAS or OSMutex would work just fine but then we need to manipulate 347 // thread state for Safepoint. Here we use Monitor wait() and notify_all() 348 // for synchronization. 349 { MutexLocker ml(BeforeExit_lock); 350 switch (_before_exit_status) { 351 case BEFORE_EXIT_NOT_RUN: 352 _before_exit_status = BEFORE_EXIT_RUNNING; 353 break; 354 case BEFORE_EXIT_RUNNING: 355 while (_before_exit_status == BEFORE_EXIT_RUNNING) { 356 BeforeExit_lock->wait(); 357 } 358 assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state"); 359 return; 360 case BEFORE_EXIT_DONE: 361 return; 362 } 363 } 364 365 // The only difference between this and Win32's _onexit procs is that 366 // this version is invoked before any threads get killed. 367 ExitProc* current = exit_procs; 368 while (current != NULL) { 369 ExitProc* next = current->next(); 370 current->evaluate(); 371 delete current; 372 current = next; 373 } 374 375 // Hang forever on exit if we're reporting an error. 376 if (ShowMessageBoxOnError && is_error_reported()) { 377 os::infinite_sleep(); 378 } 379 380 // Terminate watcher thread - must before disenrolling any periodic task 381 WatcherThread::stop(); 382 383 // Print statistics gathered (profiling ...) 384 if (Arguments::has_profile()) { 385 FlatProfiler::disengage(); 386 FlatProfiler::print(10); 387 } 388 389 // shut down the StatSampler task 390 StatSampler::disengage(); 391 StatSampler::destroy(); 392 393 // shut down the TimeMillisUpdateTask 394 if (CacheTimeMillis) { 395 TimeMillisUpdateTask::disengage(); 396 } 397 398#ifndef SERIALGC 399 // stop CMS threads 400 if (UseConcMarkSweepGC) { 401 ConcurrentMarkSweepThread::stop(); 402 } 403#endif // SERIALGC 404 405 // Print GC/heap related information. 406 if (PrintGCDetails) { 407 Universe::print(); 408 AdaptiveSizePolicyOutput(0); 409 } 410 411 412 if (Arguments::has_alloc_profile()) { 413 HandleMark hm; 414 // Do one last collection to enumerate all the objects 415 // allocated since the last one. 416 Universe::heap()->collect(GCCause::_allocation_profiler); 417 AllocationProfiler::disengage(); 418 AllocationProfiler::print(0); 419 } 420 421 if (PrintBytecodeHistogram) { 422 BytecodeHistogram::print(); 423 } 424 425 if (JvmtiExport::should_post_thread_life()) { 426 JvmtiExport::post_thread_end(thread); 427 } 428 // Always call even when there are not JVMTI environments yet, since environments 429 // may be attached late and JVMTI must track phases of VM execution 430 JvmtiExport::post_vm_death(); 431 Threads::shutdown_vm_agents(); 432 433 // Terminate the signal thread 434 // Note: we don't wait until it actually dies. 435 os::terminate_signal_thread(); 436 437 print_statistics(); 438 Universe::heap()->print_tracing_info(); 439 440 VTune::exit(); 441 442 { MutexLocker ml(BeforeExit_lock); 443 _before_exit_status = BEFORE_EXIT_DONE; 444 BeforeExit_lock->notify_all(); 445 } 446 447 #undef BEFORE_EXIT_NOT_RUN 448 #undef BEFORE_EXIT_RUNNING 449 #undef BEFORE_EXIT_DONE 450} 451 452void vm_exit(int code) { 453 Thread* thread = ThreadLocalStorage::thread_index() == -1 ? NULL 454 : ThreadLocalStorage::get_thread_slow(); 455 if (thread == NULL) { 456 // we have serious problems -- just exit 457 vm_direct_exit(code); 458 } 459 460 if (VMThread::vm_thread() != NULL) { 461 // Fire off a VM_Exit operation to bring VM to a safepoint and exit 462 VM_Exit op(code); 463 if (thread->is_Java_thread()) 464 ((JavaThread*)thread)->set_thread_state(_thread_in_vm); 465 VMThread::execute(&op); 466 // should never reach here; but in case something wrong with VM Thread. 467 vm_direct_exit(code); 468 } else { 469 // VM thread is gone, just exit 470 vm_direct_exit(code); 471 } 472 ShouldNotReachHere(); 473} 474 475void notify_vm_shutdown() { 476 // For now, just a dtrace probe. 477 HS_DTRACE_PROBE(hotspot, vm__shutdown); 478} 479 480void vm_direct_exit(int code) { 481 notify_vm_shutdown(); 482 ::exit(code); 483} 484 485void vm_perform_shutdown_actions() { 486 // Warning: do not call 'exit_globals()' here. All threads are still running. 487 // Calling 'exit_globals()' will disable thread-local-storage and cause all 488 // kinds of assertions to trigger in debug mode. 489 if (is_init_completed()) { 490 Thread* thread = Thread::current(); 491 if (thread->is_Java_thread()) { 492 // We are leaving the VM, set state to native (in case any OS exit 493 // handlers call back to the VM) 494 JavaThread* jt = (JavaThread*)thread; 495 // Must always be walkable or have no last_Java_frame when in 496 // thread_in_native 497 jt->frame_anchor()->make_walkable(jt); 498 jt->set_thread_state(_thread_in_native); 499 } 500 } 501 notify_vm_shutdown(); 502} 503 504void vm_shutdown() 505{ 506 vm_perform_shutdown_actions(); 507 os::shutdown(); 508} 509 510void vm_abort() { 511 vm_perform_shutdown_actions(); 512 os::abort(PRODUCT_ONLY(false)); 513 ShouldNotReachHere(); 514} 515 516void vm_notify_during_shutdown(const char* error, const char* message) { 517 if (error != NULL) { 518 tty->print_cr("Error occurred during initialization of VM"); 519 tty->print("%s", error); 520 if (message != NULL) { 521 tty->print_cr(": %s", message); 522 } 523 else { 524 tty->cr(); 525 } 526 } 527 if (ShowMessageBoxOnError && WizardMode) { 528 fatal("Error occurred during initialization of VM"); 529 } 530} 531 532void vm_exit_during_initialization(Handle exception) { 533 tty->print_cr("Error occurred during initialization of VM"); 534 // If there are exceptions on this thread it must be cleared 535 // first and here. Any future calls to EXCEPTION_MARK requires 536 // that no pending exceptions exist. 537 Thread *THREAD = Thread::current(); 538 if (HAS_PENDING_EXCEPTION) { 539 CLEAR_PENDING_EXCEPTION; 540 } 541 java_lang_Throwable::print(exception, tty); 542 tty->cr(); 543 java_lang_Throwable::print_stack_trace(exception(), tty); 544 tty->cr(); 545 vm_notify_during_shutdown(NULL, NULL); 546 vm_abort(); 547} 548 549void vm_exit_during_initialization(symbolHandle ex, const char* message) { 550 ResourceMark rm; 551 vm_notify_during_shutdown(ex->as_C_string(), message); 552 vm_abort(); 553} 554 555void vm_exit_during_initialization(const char* error, const char* message) { 556 vm_notify_during_shutdown(error, message); 557 vm_abort(); 558} 559 560void vm_shutdown_during_initialization(const char* error, const char* message) { 561 vm_notify_during_shutdown(error, message); 562 vm_shutdown(); 563} 564 565jdk_version_info JDK_Version::_version_info = {0}; 566bool JDK_Version::_pre_jdk16_version = false; 567int JDK_Version::_jdk_version = 0; 568 569void JDK_Version::initialize() { 570 void *lib_handle = os::native_java_library(); 571 jdk_version_info_fn_t func = 572 CAST_TO_FN_PTR(jdk_version_info_fn_t, hpi::dll_lookup(lib_handle, "JDK_GetVersionInfo0")); 573 574 if (func == NULL) { 575 // JDK older than 1.6 576 _pre_jdk16_version = true; 577 return; 578 } 579 580 if (func != NULL) { 581 (*func)(&_version_info, sizeof(_version_info)); 582 } 583 if (jdk_major_version() == 1) { 584 _jdk_version = jdk_minor_version(); 585 } else { 586 // If the release version string is changed to n.x.x (e.g. 7.0.0) in a future release 587 _jdk_version = jdk_major_version(); 588 } 589} 590 591void JDK_Version_init() { 592 JDK_Version::initialize(); 593} 594