1 /*
   2  * Copyright (c) 1997, 2015, 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 "precompiled.hpp"
  26 #include "classfile/classLoader.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "classfile/systemDictionary.hpp"
  29 #include "code/codeCache.hpp"
  30 #include "compiler/compileBroker.hpp"
  31 #include "compiler/compilerOracle.hpp"
  32 #include "gc/shared/genCollectedHeap.hpp"
  33 #include "interpreter/bytecodeHistogram.hpp"
  34 #if INCLUDE_JVMCI
  35 #include "jvmci/jvmciCompiler.hpp"
  36 #include "jvmci/jvmciRuntime.hpp"
  37 #endif
  38 #include "memory/oopFactory.hpp"
  39 #include "memory/universe.hpp"
  40 #include "oops/constantPool.hpp"
  41 #include "oops/generateOopMap.hpp"
  42 #include "oops/instanceKlass.hpp"
  43 #include "oops/instanceOop.hpp"
  44 #include "oops/method.hpp"
  45 #include "oops/objArrayOop.hpp"
  46 #include "oops/oop.inline.hpp"
  47 #include "oops/symbol.hpp"
  48 #include "prims/jvmtiExport.hpp"
  49 #include "runtime/arguments.hpp"
  50 #include "runtime/biasedLocking.hpp"
  51 #include "runtime/compilationPolicy.hpp"
  52 #include "runtime/deoptimization.hpp"
  53 #include "runtime/fprofiler.hpp"
  54 #include "runtime/init.hpp"
  55 #include "runtime/interfaceSupport.hpp"
  56 #include "runtime/java.hpp"
  57 #include "runtime/memprofiler.hpp"
  58 #include "runtime/sharedRuntime.hpp"
  59 #include "runtime/statSampler.hpp"
  60 #include "runtime/sweeper.hpp"
  61 #include "runtime/task.hpp"
  62 #include "runtime/thread.inline.hpp"
  63 #include "runtime/timer.hpp"
  64 #include "runtime/vm_operations.hpp"
  65 #include "services/memTracker.hpp"
  66 #include "trace/tracing.hpp"
  67 #include "utilities/dtrace.hpp"
  68 #include "utilities/globalDefinitions.hpp"
  69 #include "utilities/histogram.hpp"
  70 #include "utilities/macros.hpp"
  71 #include "utilities/vmError.hpp"
  72 #if INCLUDE_ALL_GCS
  73 #include "gc/cms/concurrentMarkSweepThread.hpp"
  74 #include "gc/parallel/psScavenge.hpp"
  75 #endif // INCLUDE_ALL_GCS
  76 #ifdef COMPILER1
  77 #include "c1/c1_Compiler.hpp"
  78 #include "c1/c1_Runtime1.hpp"
  79 #endif
  80 #ifdef COMPILER2
  81 #include "code/compiledIC.hpp"
  82 #include "compiler/methodLiveness.hpp"
  83 #include "opto/compile.hpp"
  84 #include "opto/indexSet.hpp"
  85 #include "opto/runtime.hpp"
  86 #endif
  87 
  88 GrowableArray<Method*>* collected_profiled_methods;
  89 
  90 int compare_methods(Method** a, Method** b) {
  91   // %%% there can be 32-bit overflow here
  92   return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
  93        - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
  94 }
  95 
  96 void collect_profiled_methods(Method* m) {
  97   Thread* thread = Thread::current();
  98   // This HandleMark prevents a huge amount of handles from being added
  99   // to the metadata_handles() array on the thread.
 100   HandleMark hm(thread);
 101   methodHandle mh(thread, m);
 102   if ((m->method_data() != NULL) &&
 103       (PrintMethodData || CompilerOracle::should_print(mh))) {
 104     collected_profiled_methods->push(m);
 105   }
 106 }
 107 
 108 void print_method_profiling_data() {
 109   if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData) &&
 110      (PrintMethodData || CompilerOracle::should_print_methods())) {
 111     ResourceMark rm;
 112     HandleMark hm;
 113     collected_profiled_methods = new GrowableArray<Method*>(1024);
 114     ClassLoaderDataGraph::methods_do(collect_profiled_methods);
 115     collected_profiled_methods->sort(&compare_methods);
 116 
 117     int count = collected_profiled_methods->length();
 118     int total_size = 0;
 119     if (count > 0) {
 120       for (int index = 0; index < count; index++) {
 121         Method* m = collected_profiled_methods->at(index);
 122         ttyLocker ttyl;
 123         tty->print_cr("------------------------------------------------------------------------");
 124         m->print_invocation_count();
 125         tty->print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
 126         tty->cr();
 127         // Dump data on parameters if any
 128         if (m->method_data() != NULL && m->method_data()->parameters_type_data() != NULL) {
 129           tty->fill_to(2);
 130           m->method_data()->parameters_type_data()->print_data_on(tty);
 131         }
 132         m->print_codes();
 133         total_size += m->method_data()->size_in_bytes();
 134       }
 135       tty->print_cr("------------------------------------------------------------------------");
 136       tty->print_cr("Total MDO size: %d bytes", total_size);
 137     }
 138   }
 139 }
 140 
 141 
 142 #ifndef PRODUCT
 143 
 144 // Statistics printing (method invocation histogram)
 145 
 146 GrowableArray<Method*>* collected_invoked_methods;
 147 
 148 void collect_invoked_methods(Method* m) {
 149   if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
 150     collected_invoked_methods->push(m);
 151   }
 152 }
 153 
 154 
 155 
 156 
 157 void print_method_invocation_histogram() {
 158   ResourceMark rm;
 159   HandleMark hm;
 160   collected_invoked_methods = new GrowableArray<Method*>(1024);
 161   SystemDictionary::methods_do(collect_invoked_methods);
 162   collected_invoked_methods->sort(&compare_methods);
 163   //
 164   tty->cr();
 165   tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = " INTX_FORMAT "):", MethodHistogramCutoff);
 166   tty->cr();
 167   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
 168   unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
 169       synch_total = 0, nativ_total = 0, acces_total = 0;
 170   for (int index = 0; index < collected_invoked_methods->length(); index++) {
 171     Method* m = collected_invoked_methods->at(index);
 172     int c = m->invocation_count() + m->compiled_invocation_count();
 173     if (c >= MethodHistogramCutoff) m->print_invocation_count();
 174     int_total  += m->invocation_count();
 175     comp_total += m->compiled_invocation_count();
 176     if (m->is_final())        final_total  += c;
 177     if (m->is_static())       static_total += c;
 178     if (m->is_synchronized()) synch_total  += c;
 179     if (m->is_native())       nativ_total  += c;
 180     if (m->is_accessor())     acces_total  += c;
 181   }
 182   tty->cr();
 183   total = int_total + comp_total;
 184   tty->print_cr("Invocations summary:");
 185   tty->print_cr("\t%9d (%4.1f%%) interpreted",  int_total,    100.0 * int_total    / total);
 186   tty->print_cr("\t%9d (%4.1f%%) compiled",     comp_total,   100.0 * comp_total   / total);
 187   tty->print_cr("\t%9d (100%%)  total",         total);
 188   tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total,  100.0 * synch_total  / total);
 189   tty->print_cr("\t%9d (%4.1f%%) final",        final_total,  100.0 * final_total  / total);
 190   tty->print_cr("\t%9d (%4.1f%%) static",       static_total, 100.0 * static_total / total);
 191   tty->print_cr("\t%9d (%4.1f%%) native",       nativ_total,  100.0 * nativ_total  / total);
 192   tty->print_cr("\t%9d (%4.1f%%) accessor",     acces_total,  100.0 * acces_total  / total);
 193   tty->cr();
 194   SharedRuntime::print_call_statistics(comp_total);
 195 }
 196 
 197 void print_bytecode_count() {
 198   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
 199     tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
 200   }
 201 }
 202 
 203 AllocStats alloc_stats;
 204 
 205 
 206 
 207 // General statistics printing (profiling ...)
 208 void print_statistics() {
 209 #ifdef ASSERT
 210 
 211   if (CountRuntimeCalls) {
 212     extern Histogram *RuntimeHistogram;
 213     RuntimeHistogram->print();
 214   }
 215 
 216   if (CountJNICalls) {
 217     extern Histogram *JNIHistogram;
 218     JNIHistogram->print();
 219   }
 220 
 221   if (CountJVMCalls) {
 222     extern Histogram *JVMHistogram;
 223     JVMHistogram->print();
 224   }
 225 
 226 #endif
 227 
 228   if (MemProfiling) {
 229     MemProfiler::disengage();
 230   }
 231 
 232   if (CITime) {
 233     CompileBroker::print_times();
 234   }
 235 
 236 #ifdef COMPILER1
 237   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
 238     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
 239     Runtime1::print_statistics();
 240     Deoptimization::print_statistics();
 241     SharedRuntime::print_statistics();
 242   }
 243 #endif /* COMPILER1 */
 244 
 245 #ifdef COMPILER2
 246   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
 247     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
 248     Compile::print_statistics();
 249 #ifndef COMPILER1
 250     Deoptimization::print_statistics();
 251     SharedRuntime::print_statistics();
 252 #endif //COMPILER1
 253     os::print_statistics();
 254   }
 255 
 256   if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
 257     OptoRuntime::print_named_counters();
 258   }
 259 
 260   if (TimeLivenessAnalysis) {
 261     MethodLiveness::print_times();
 262   }
 263 #ifdef ASSERT
 264   if (CollectIndexSetStatistics) {
 265     IndexSet::print_statistics();
 266   }
 267 #endif // ASSERT
 268 #else
 269 #ifdef INCLUDE_JVMCI
 270 #ifndef COMPILER1
 271   if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
 272     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
 273     Deoptimization::print_statistics();
 274     SharedRuntime::print_statistics();
 275   }
 276 #endif
 277 #endif
 278 #endif
 279 
 280   if (PrintNMethodStatistics) {
 281     nmethod::print_statistics();
 282   }
 283   if (CountCompiledCalls) {
 284     print_method_invocation_histogram();
 285   }
 286 
 287   print_method_profiling_data();
 288 
 289   if (TimeCompilationPolicy) {
 290     CompilationPolicy::policy()->print_time();
 291   }
 292   if (TimeOopMap) {
 293     GenerateOopMap::print_time();
 294   }
 295   if (ProfilerCheckIntervals) {
 296     PeriodicTask::print_intervals();
 297   }
 298   if (PrintSymbolTableSizeHistogram) {
 299     SymbolTable::print_histogram();
 300   }
 301   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
 302     BytecodeCounter::print();
 303   }
 304   if (PrintBytecodePairHistogram) {
 305     BytecodePairHistogram::print();
 306   }
 307 
 308   if (PrintCodeCache) {
 309     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 310     CodeCache::print();
 311   }
 312 
 313   if (PrintMethodFlushingStatistics) {
 314     NMethodSweeper::print();
 315   }
 316 
 317   if (PrintCodeCache2) {
 318     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 319     CodeCache::print_internals();
 320   }
 321 
 322   if (PrintVtableStats) {
 323     klassVtable::print_statistics();
 324     klassItable::print_statistics();
 325   }
 326   if (VerifyOops) {
 327     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
 328   }
 329 
 330   print_bytecode_count();
 331   if (PrintMallocStatistics) {
 332     tty->print("allocation stats: ");
 333     alloc_stats.print();
 334     tty->cr();
 335   }
 336 
 337   if (PrintSystemDictionaryAtExit) {
 338     SystemDictionary::print();
 339   }
 340 
 341   if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
 342     Method::print_touched_methods(tty);
 343   }
 344 
 345   if (PrintBiasedLockingStatistics) {
 346     BiasedLocking::print_counters();
 347   }
 348 
 349   // Native memory tracking data
 350   if (PrintNMTStatistics) {
 351     MemTracker::final_report(tty);
 352   }
 353 }
 354 
 355 #else // PRODUCT MODE STATISTICS
 356 
 357 void print_statistics() {
 358 
 359   if (PrintMethodData) {
 360     print_method_profiling_data();
 361   }
 362 
 363   if (CITime) {
 364     CompileBroker::print_times();
 365   }
 366 
 367   if (PrintCodeCache) {
 368     MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 369     CodeCache::print();
 370   }
 371 
 372   if (PrintMethodFlushingStatistics) {
 373     NMethodSweeper::print();
 374   }
 375 
 376 #ifdef COMPILER2
 377   if (PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
 378     OptoRuntime::print_named_counters();
 379   }
 380 #endif
 381   if (PrintBiasedLockingStatistics) {
 382     BiasedLocking::print_counters();
 383   }
 384 
 385   // Native memory tracking data
 386   if (PrintNMTStatistics) {
 387     MemTracker::final_report(tty);
 388   }
 389 
 390   if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
 391     Method::print_touched_methods(tty);
 392   }
 393 }
 394 
 395 #endif
 396 
 397 // Note: before_exit() can be executed only once, if more than one threads
 398 //       are trying to shutdown the VM at the same time, only one thread
 399 //       can run before_exit() and all other threads must wait.
 400 void before_exit(JavaThread* thread) {
 401   #define BEFORE_EXIT_NOT_RUN 0
 402   #define BEFORE_EXIT_RUNNING 1
 403   #define BEFORE_EXIT_DONE    2
 404   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
 405 
 406   // Note: don't use a Mutex to guard the entire before_exit(), as
 407   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
 408   // A CAS or OSMutex would work just fine but then we need to manipulate
 409   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
 410   // for synchronization.
 411   { MutexLocker ml(BeforeExit_lock);
 412     switch (_before_exit_status) {
 413     case BEFORE_EXIT_NOT_RUN:
 414       _before_exit_status = BEFORE_EXIT_RUNNING;
 415       break;
 416     case BEFORE_EXIT_RUNNING:
 417       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
 418         BeforeExit_lock->wait();
 419       }
 420       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
 421       return;
 422     case BEFORE_EXIT_DONE:
 423       return;
 424     }
 425   }
 426 
 427 #if INCLUDE_JVMCI
 428   // We are not using CATCH here because we want the exit to continue normally.
 429   Thread* THREAD = thread;
 430   JVMCIRuntime::shutdown(THREAD);
 431   if (HAS_PENDING_EXCEPTION) {
 432     Handle exception(THREAD, PENDING_EXCEPTION);
 433     CLEAR_PENDING_EXCEPTION;
 434     ttyLocker ttyl;
 435     java_lang_Throwable::print_stack_trace(exception, tty);
 436   }
 437 #endif
 438 
 439   // Hang forever on exit if we're reporting an error.
 440   if (ShowMessageBoxOnError && is_error_reported()) {
 441     os::infinite_sleep();
 442   }
 443 
 444   // Stop the WatcherThread. We do this before disenrolling various
 445   // PeriodicTasks to reduce the likelihood of races.
 446   if (PeriodicTask::num_tasks() > 0) {
 447     WatcherThread::stop();
 448   }
 449 
 450   // Print statistics gathered (profiling ...)
 451   if (Arguments::has_profile()) {
 452     FlatProfiler::disengage();
 453     FlatProfiler::print(10);
 454   }
 455 
 456   // shut down the StatSampler task
 457   StatSampler::disengage();
 458   StatSampler::destroy();
 459 
 460   // Stop concurrent GC threads
 461   Universe::heap()->stop();
 462 
 463   // Print GC/heap related information.
 464   if (PrintGCDetails) {
 465     Universe::print();
 466     AdaptiveSizePolicyOutput(0);
 467     if (Verbose) {
 468       ClassLoaderDataGraph::dump_on(gclog_or_tty);
 469     }
 470   }
 471 
 472   if (PrintBytecodeHistogram) {
 473     BytecodeHistogram::print();
 474   }
 475 
 476   if (JvmtiExport::should_post_thread_life()) {
 477     JvmtiExport::post_thread_end(thread);
 478   }
 479 
 480 
 481   EventThreadEnd event;
 482   if (event.should_commit()) {
 483       event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
 484       event.commit();
 485   }
 486 
 487   // Always call even when there are not JVMTI environments yet, since environments
 488   // may be attached late and JVMTI must track phases of VM execution
 489   JvmtiExport::post_vm_death();
 490   Threads::shutdown_vm_agents();
 491 
 492   // Terminate the signal thread
 493   // Note: we don't wait until it actually dies.
 494   os::terminate_signal_thread();
 495 
 496   print_statistics();
 497   Universe::heap()->print_tracing_info();
 498 
 499   { MutexLocker ml(BeforeExit_lock);
 500     _before_exit_status = BEFORE_EXIT_DONE;
 501     BeforeExit_lock->notify_all();
 502   }
 503 
 504   if (VerifyStringTableAtExit) {
 505     int fail_cnt = 0;
 506     {
 507       MutexLocker ml(StringTable_lock);
 508       fail_cnt = StringTable::verify_and_compare_entries();
 509     }
 510 
 511     if (fail_cnt != 0) {
 512       tty->print_cr("ERROR: fail_cnt=%d", fail_cnt);
 513       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
 514     }
 515   }
 516 
 517   #undef BEFORE_EXIT_NOT_RUN
 518   #undef BEFORE_EXIT_RUNNING
 519   #undef BEFORE_EXIT_DONE
 520 }
 521 
 522 void vm_exit(int code) {
 523   Thread* thread = ThreadLocalStorage::is_initialized() ?
 524     ThreadLocalStorage::get_thread_slow() : NULL;
 525   if (thread == NULL) {
 526     // we have serious problems -- just exit
 527     vm_direct_exit(code);
 528   }
 529 
 530   if (VMThread::vm_thread() != NULL) {
 531     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
 532     VM_Exit op(code);
 533     if (thread->is_Java_thread())
 534       ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
 535     VMThread::execute(&op);
 536     // should never reach here; but in case something wrong with VM Thread.
 537     vm_direct_exit(code);
 538   } else {
 539     // VM thread is gone, just exit
 540     vm_direct_exit(code);
 541   }
 542   ShouldNotReachHere();
 543 }
 544 
 545 void notify_vm_shutdown() {
 546   // For now, just a dtrace probe.
 547   HOTSPOT_VM_SHUTDOWN();
 548   HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
 549 }
 550 
 551 void vm_direct_exit(int code) {
 552   notify_vm_shutdown();
 553   os::wait_for_keypress_at_exit();
 554   os::exit(code);
 555 }
 556 
 557 void vm_perform_shutdown_actions() {
 558   // Warning: do not call 'exit_globals()' here. All threads are still running.
 559   // Calling 'exit_globals()' will disable thread-local-storage and cause all
 560   // kinds of assertions to trigger in debug mode.
 561   if (is_init_completed()) {
 562     Thread* thread = ThreadLocalStorage::is_initialized() ?
 563                      ThreadLocalStorage::get_thread_slow() : NULL;
 564     if (thread != NULL && thread->is_Java_thread()) {
 565       // We are leaving the VM, set state to native (in case any OS exit
 566       // handlers call back to the VM)
 567       JavaThread* jt = (JavaThread*)thread;
 568       // Must always be walkable or have no last_Java_frame when in
 569       // thread_in_native
 570       jt->frame_anchor()->make_walkable(jt);
 571       jt->set_thread_state(_thread_in_native);
 572     }
 573   }
 574   notify_vm_shutdown();
 575 }
 576 
 577 void vm_shutdown()
 578 {
 579   vm_perform_shutdown_actions();
 580   os::wait_for_keypress_at_exit();
 581   os::shutdown();
 582 }
 583 
 584 void vm_abort(bool dump_core) {
 585   vm_perform_shutdown_actions();
 586   os::wait_for_keypress_at_exit();
 587 
 588   // Flush stdout and stderr before abort.
 589   fflush(stdout);
 590   fflush(stderr);
 591 
 592   os::abort(dump_core);
 593   ShouldNotReachHere();
 594 }
 595 
 596 void vm_notify_during_shutdown(const char* error, const char* message) {
 597   if (error != NULL) {
 598     tty->print_cr("Error occurred during initialization of VM");
 599     tty->print("%s", error);
 600     if (message != NULL) {
 601       tty->print_cr(": %s", message);
 602     }
 603     else {
 604       tty->cr();
 605     }
 606   }
 607   if (ShowMessageBoxOnError && WizardMode) {
 608     fatal("Error occurred during initialization of VM");
 609   }
 610 }
 611 
 612 void vm_exit_during_initialization(Handle exception) {
 613   tty->print_cr("Error occurred during initialization of VM");
 614   // If there are exceptions on this thread it must be cleared
 615   // first and here. Any future calls to EXCEPTION_MARK requires
 616   // that no pending exceptions exist.
 617   Thread *THREAD = Thread::current();
 618   if (HAS_PENDING_EXCEPTION) {
 619     CLEAR_PENDING_EXCEPTION;
 620   }
 621   java_lang_Throwable::print_stack_trace(exception, tty);
 622   tty->cr();
 623   vm_notify_during_shutdown(NULL, NULL);
 624 
 625   // Failure during initialization, we don't want to dump core
 626   vm_abort(false);
 627 }
 628 
 629 void vm_exit_during_initialization(Symbol* ex, const char* message) {
 630   ResourceMark rm;
 631   vm_notify_during_shutdown(ex->as_C_string(), message);
 632 
 633   // Failure during initialization, we don't want to dump core
 634   vm_abort(false);
 635 }
 636 
 637 void vm_exit_during_initialization(const char* error, const char* message) {
 638   vm_notify_during_shutdown(error, message);
 639 
 640   // Failure during initialization, we don't want to dump core
 641   vm_abort(false);
 642 }
 643 
 644 void vm_shutdown_during_initialization(const char* error, const char* message) {
 645   vm_notify_during_shutdown(error, message);
 646   vm_shutdown();
 647 }
 648 
 649 JDK_Version JDK_Version::_current;
 650 const char* JDK_Version::_runtime_name;
 651 const char* JDK_Version::_runtime_version;
 652 
 653 void JDK_Version::initialize() {
 654   jdk_version_info info;
 655   assert(!_current.is_valid(), "Don't initialize twice");
 656 
 657   void *lib_handle = os::native_java_library();
 658   jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
 659      os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
 660 
 661   assert(func != NULL, "Support for JDK 1.5 or older has been removed after JEP-223");
 662 
 663   (*func)(&info, sizeof(info));
 664 
 665   int major = JDK_VERSION_MAJOR(info.jdk_version);
 666   int minor = JDK_VERSION_MINOR(info.jdk_version);
 667   int security = JDK_VERSION_SECURITY(info.jdk_version);
 668   int build = JDK_VERSION_BUILD(info.jdk_version);
 669 
 670   // Incompatible with pre-4243978 JDK.
 671   if (info.pending_list_uses_discovered_field == 0) {
 672     vm_exit_during_initialization(
 673       "Incompatible JDK is not using Reference.discovered field for pending list");
 674   }
 675   _current = JDK_Version(major, minor, security, info.patch_version, build,
 676                          info.thread_park_blocker == 1,
 677                          info.post_vm_init_hook_enabled == 1);
 678 }
 679 
 680 void JDK_Version_init() {
 681   JDK_Version::initialize();
 682 }
 683 
 684 static int64_t encode_jdk_version(const JDK_Version& v) {
 685   return
 686     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
 687     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
 688     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
 689     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
 690     ((int64_t)v.build_number()           << (BitsPerByte * 0));
 691 }
 692 
 693 int JDK_Version::compare(const JDK_Version& other) const {
 694   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
 695   uint64_t e = encode_jdk_version(*this);
 696   uint64_t o = encode_jdk_version(other);
 697   return (e > o) ? 1 : ((e == o) ? 0 : -1);
 698 }
 699 
 700 void JDK_Version::to_string(char* buffer, size_t buflen) const {
 701   assert(buffer && buflen > 0, "call with useful buffer");
 702   size_t index = 0;
 703 
 704   if (!is_valid()) {
 705     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
 706   } else {
 707     int rc = jio_snprintf(
 708         &buffer[index], buflen - index, "%d.%d", _major, _minor);
 709     if (rc == -1) return;
 710     index += rc;
 711     if (_security > 0) {
 712       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
 713     }
 714     if (_patch > 0) {
 715       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _patch);
 716       if (rc == -1) return;
 717       index += rc;
 718     }
 719     if (_build > 0) {
 720       rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
 721       if (rc == -1) return;
 722       index += rc;
 723     }
 724   }
 725 }