1 /* 2 * Copyright (c) 1997, 2010, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "incls/_precompiled.incl" 26 #include "incls/_java.cpp.incl" 27 28 HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown); 29 30 #ifndef PRODUCT 31 32 // Statistics printing (method invocation histogram) 33 34 GrowableArray<methodOop>* collected_invoked_methods; 35 36 void 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 43 GrowableArray<methodOop>* collected_profiled_methods; 44 45 void 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 54 int 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 61 void 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 101 void 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 123 void print_bytecode_count() { 124 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) { 125 tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value()); 126 } 127 } 128 129 AllocStats alloc_stats; 130 131 132 133 // General statistics printing (profiling ...) 134 135 void 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 || C1UpdateMethodData) { 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 278 void 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 298 extern "C" { 299 typedef void (*__exit_proc)(void); 300 } 301 302 class 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 321 static ExitProc* exit_procs = NULL; 322 323 324 extern "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. 338 void 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 if (PeriodicTask::num_tasks() > 0) 382 WatcherThread::stop(); 383 384 // Print statistics gathered (profiling ...) 385 if (Arguments::has_profile()) { 386 FlatProfiler::disengage(); 387 FlatProfiler::print(10); 388 } 389 390 // shut down the StatSampler task 391 StatSampler::disengage(); 392 StatSampler::destroy(); 393 394 #ifndef SERIALGC 395 // stop CMS threads 396 if (UseConcMarkSweepGC) { 397 ConcurrentMarkSweepThread::stop(); 398 } 399 #endif // SERIALGC 400 401 // Print GC/heap related information. 402 if (PrintGCDetails) { 403 Universe::print(); 404 AdaptiveSizePolicyOutput(0); 405 } 406 407 408 if (Arguments::has_alloc_profile()) { 409 HandleMark hm; 410 // Do one last collection to enumerate all the objects 411 // allocated since the last one. 412 Universe::heap()->collect(GCCause::_allocation_profiler); 413 AllocationProfiler::disengage(); 414 AllocationProfiler::print(0); 415 } 416 417 if (PrintBytecodeHistogram) { 418 BytecodeHistogram::print(); 419 } 420 421 if (JvmtiExport::should_post_thread_life()) { 422 JvmtiExport::post_thread_end(thread); 423 } 424 // Always call even when there are not JVMTI environments yet, since environments 425 // may be attached late and JVMTI must track phases of VM execution 426 JvmtiExport::post_vm_death(); 427 Threads::shutdown_vm_agents(); 428 429 // Terminate the signal thread 430 // Note: we don't wait until it actually dies. 431 os::terminate_signal_thread(); 432 433 print_statistics(); 434 Universe::heap()->print_tracing_info(); 435 436 { MutexLocker ml(BeforeExit_lock); 437 _before_exit_status = BEFORE_EXIT_DONE; 438 BeforeExit_lock->notify_all(); 439 } 440 441 #undef BEFORE_EXIT_NOT_RUN 442 #undef BEFORE_EXIT_RUNNING 443 #undef BEFORE_EXIT_DONE 444 } 445 446 void vm_exit(int code) { 447 Thread* thread = ThreadLocalStorage::thread_index() == -1 ? NULL 448 : ThreadLocalStorage::get_thread_slow(); 449 if (thread == NULL) { 450 // we have serious problems -- just exit 451 vm_direct_exit(code); 452 } 453 454 if (VMThread::vm_thread() != NULL) { 455 // Fire off a VM_Exit operation to bring VM to a safepoint and exit 456 VM_Exit op(code); 457 if (thread->is_Java_thread()) 458 ((JavaThread*)thread)->set_thread_state(_thread_in_vm); 459 VMThread::execute(&op); 460 // should never reach here; but in case something wrong with VM Thread. 461 vm_direct_exit(code); 462 } else { 463 // VM thread is gone, just exit 464 vm_direct_exit(code); 465 } 466 ShouldNotReachHere(); 467 } 468 469 void notify_vm_shutdown() { 470 // For now, just a dtrace probe. 471 HS_DTRACE_PROBE(hotspot, vm__shutdown); 472 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG(); 473 } 474 475 void vm_direct_exit(int code) { 476 notify_vm_shutdown(); 477 ::exit(code); 478 } 479 480 void vm_perform_shutdown_actions() { 481 // Warning: do not call 'exit_globals()' here. All threads are still running. 482 // Calling 'exit_globals()' will disable thread-local-storage and cause all 483 // kinds of assertions to trigger in debug mode. 484 if (is_init_completed()) { 485 Thread* thread = Thread::current(); 486 if (thread->is_Java_thread()) { 487 // We are leaving the VM, set state to native (in case any OS exit 488 // handlers call back to the VM) 489 JavaThread* jt = (JavaThread*)thread; 490 // Must always be walkable or have no last_Java_frame when in 491 // thread_in_native 492 jt->frame_anchor()->make_walkable(jt); 493 jt->set_thread_state(_thread_in_native); 494 } 495 } 496 notify_vm_shutdown(); 497 } 498 499 void vm_shutdown() 500 { 501 vm_perform_shutdown_actions(); 502 os::shutdown(); 503 } 504 505 void vm_abort(bool dump_core) { 506 vm_perform_shutdown_actions(); 507 os::abort(dump_core); 508 ShouldNotReachHere(); 509 } 510 511 void vm_notify_during_shutdown(const char* error, const char* message) { 512 if (error != NULL) { 513 tty->print_cr("Error occurred during initialization of VM"); 514 tty->print("%s", error); 515 if (message != NULL) { 516 tty->print_cr(": %s", message); 517 } 518 else { 519 tty->cr(); 520 } 521 } 522 if (ShowMessageBoxOnError && WizardMode) { 523 fatal("Error occurred during initialization of VM"); 524 } 525 } 526 527 void vm_exit_during_initialization(Handle exception) { 528 tty->print_cr("Error occurred during initialization of VM"); 529 // If there are exceptions on this thread it must be cleared 530 // first and here. Any future calls to EXCEPTION_MARK requires 531 // that no pending exceptions exist. 532 Thread *THREAD = Thread::current(); 533 if (HAS_PENDING_EXCEPTION) { 534 CLEAR_PENDING_EXCEPTION; 535 } 536 java_lang_Throwable::print(exception, tty); 537 tty->cr(); 538 java_lang_Throwable::print_stack_trace(exception(), tty); 539 tty->cr(); 540 vm_notify_during_shutdown(NULL, NULL); 541 542 // Failure during initialization, we don't want to dump core 543 vm_abort(false); 544 } 545 546 void vm_exit_during_initialization(symbolHandle ex, const char* message) { 547 ResourceMark rm; 548 vm_notify_during_shutdown(ex->as_C_string(), message); 549 550 // Failure during initialization, we don't want to dump core 551 vm_abort(false); 552 } 553 554 void vm_exit_during_initialization(const char* error, const char* message) { 555 vm_notify_during_shutdown(error, message); 556 557 // Failure during initialization, we don't want to dump core 558 vm_abort(false); 559 } 560 561 void vm_shutdown_during_initialization(const char* error, const char* message) { 562 vm_notify_during_shutdown(error, message); 563 vm_shutdown(); 564 } 565 566 JDK_Version JDK_Version::_current; 567 568 void JDK_Version::initialize() { 569 jdk_version_info info; 570 assert(!_current.is_valid(), "Don't initialize twice"); 571 572 void *lib_handle = os::native_java_library(); 573 jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t, 574 os::dll_lookup(lib_handle, "JDK_GetVersionInfo0")); 575 576 if (func == NULL) { 577 // JDK older than 1.6 578 _current._partially_initialized = true; 579 } else { 580 (*func)(&info, sizeof(info)); 581 582 int major = JDK_VERSION_MAJOR(info.jdk_version); 583 int minor = JDK_VERSION_MINOR(info.jdk_version); 584 int micro = JDK_VERSION_MICRO(info.jdk_version); 585 int build = JDK_VERSION_BUILD(info.jdk_version); 586 if (major == 1 && minor > 4) { 587 // We represent "1.5.0" as "5.0", but 1.4.2 as itself. 588 major = minor; 589 minor = micro; 590 micro = 0; 591 } 592 _current = JDK_Version(major, minor, micro, info.update_version, 593 info.special_update_version, build, 594 info.thread_park_blocker == 1); 595 } 596 } 597 598 void JDK_Version::fully_initialize( 599 uint8_t major, uint8_t minor, uint8_t micro, uint8_t update) { 600 // This is only called when current is less than 1.6 and we've gotten 601 // far enough in the initialization to determine the exact version. 602 assert(major < 6, "not needed for JDK version >= 6"); 603 assert(is_partially_initialized(), "must not initialize"); 604 if (major < 5) { 605 // JDK verison sequence: 1.2.x, 1.3.x, 1.4.x, 5.0.x, 6.0.x, etc. 606 micro = minor; 607 minor = major; 608 major = 1; 609 } 610 _current = JDK_Version(major, minor, micro, update); 611 } 612 613 void JDK_Version_init() { 614 JDK_Version::initialize(); 615 } 616 617 static int64_t encode_jdk_version(const JDK_Version& v) { 618 return 619 ((int64_t)v.major_version() << (BitsPerByte * 5)) | 620 ((int64_t)v.minor_version() << (BitsPerByte * 4)) | 621 ((int64_t)v.micro_version() << (BitsPerByte * 3)) | 622 ((int64_t)v.update_version() << (BitsPerByte * 2)) | 623 ((int64_t)v.special_update_version() << (BitsPerByte * 1)) | 624 ((int64_t)v.build_number() << (BitsPerByte * 0)); 625 } 626 627 int JDK_Version::compare(const JDK_Version& other) const { 628 assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)"); 629 if (!is_partially_initialized() && other.is_partially_initialized()) { 630 return -(other.compare(*this)); // flip the comparators 631 } 632 assert(!other.is_partially_initialized(), "Not initialized yet"); 633 if (is_partially_initialized()) { 634 assert(other.major_version() >= 6, 635 "Invalid JDK version comparison during initialization"); 636 return -1; 637 } else { 638 uint64_t e = encode_jdk_version(*this); 639 uint64_t o = encode_jdk_version(other); 640 return (e > o) ? 1 : ((e == o) ? 0 : -1); 641 } 642 } 643 644 void JDK_Version::to_string(char* buffer, size_t buflen) const { 645 size_t index = 0; 646 if (!is_valid()) { 647 jio_snprintf(buffer, buflen, "%s", "(uninitialized)"); 648 } else if (is_partially_initialized()) { 649 jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0"); 650 } else { 651 index += jio_snprintf( 652 &buffer[index], buflen - index, "%d.%d", _major, _minor); 653 if (_micro > 0) { 654 index += jio_snprintf(&buffer[index], buflen - index, ".%d", _micro); 655 } 656 if (_update > 0) { 657 index += jio_snprintf(&buffer[index], buflen - index, "_%02d", _update); 658 } 659 if (_special > 0) { 660 index += jio_snprintf(&buffer[index], buflen - index, "%c", _special); 661 } 662 if (_build > 0) { 663 index += jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build); 664 } 665 } 666 }