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   JVMCIRuntime::shutdown();
 429 #endif
 430 
 431   // Hang forever on exit if we're reporting an error.
 432   if (ShowMessageBoxOnError && is_error_reported()) {
 433     os::infinite_sleep();
 434   }
 435 
 436   // Stop the WatcherThread. We do this before disenrolling various
 437   // PeriodicTasks to reduce the likelihood of races.
 438   if (PeriodicTask::num_tasks() > 0) {
 439     WatcherThread::stop();
 440   }
 441 
 442   // Print statistics gathered (profiling ...)
 443   if (Arguments::has_profile()) {
 444     FlatProfiler::disengage();
 445     FlatProfiler::print(10);
 446   }
 447 
 448   // shut down the StatSampler task
 449   StatSampler::disengage();
 450   StatSampler::destroy();
 451 
 452   // Stop concurrent GC threads
 453   Universe::heap()->stop();
 454 
 455   // Print GC/heap related information.
 456   if (PrintGCDetails) {
 457     Universe::print();
 458     AdaptiveSizePolicyOutput(0);
 459     if (Verbose) {
 460       ClassLoaderDataGraph::dump_on(gclog_or_tty);
 461     }
 462   }
 463 
 464   if (PrintBytecodeHistogram) {
 465     BytecodeHistogram::print();
 466   }
 467 
 468   if (JvmtiExport::should_post_thread_life()) {
 469     JvmtiExport::post_thread_end(thread);
 470   }
 471 
 472 
 473   EventThreadEnd event;
 474   if (event.should_commit()) {
 475       event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
 476       event.commit();
 477   }
 478 
 479   // Always call even when there are not JVMTI environments yet, since environments
 480   // may be attached late and JVMTI must track phases of VM execution
 481   JvmtiExport::post_vm_death();
 482   Threads::shutdown_vm_agents();
 483 
 484   // Terminate the signal thread
 485   // Note: we don't wait until it actually dies.
 486   os::terminate_signal_thread();
 487 
 488   print_statistics();
 489   Universe::heap()->print_tracing_info();
 490 
 491   { MutexLocker ml(BeforeExit_lock);
 492     _before_exit_status = BEFORE_EXIT_DONE;
 493     BeforeExit_lock->notify_all();
 494   }
 495 
 496   if (VerifyStringTableAtExit) {
 497     int fail_cnt = 0;
 498     {
 499       MutexLocker ml(StringTable_lock);
 500       fail_cnt = StringTable::verify_and_compare_entries();
 501     }
 502 
 503     if (fail_cnt != 0) {
 504       tty->print_cr("ERROR: fail_cnt=%d", fail_cnt);
 505       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
 506     }
 507   }
 508 
 509   #undef BEFORE_EXIT_NOT_RUN
 510   #undef BEFORE_EXIT_RUNNING
 511   #undef BEFORE_EXIT_DONE
 512 }
 513 
 514 void vm_exit(int code) {
 515   Thread* thread = ThreadLocalStorage::is_initialized() ?
 516     ThreadLocalStorage::get_thread_slow() : NULL;
 517   if (thread == NULL) {
 518     // we have serious problems -- just exit
 519     vm_direct_exit(code);
 520   }
 521 
 522   if (VMThread::vm_thread() != NULL) {
 523     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
 524     VM_Exit op(code);
 525     if (thread->is_Java_thread())
 526       ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
 527     VMThread::execute(&op);
 528     // should never reach here; but in case something wrong with VM Thread.
 529     vm_direct_exit(code);
 530   } else {
 531     // VM thread is gone, just exit
 532     vm_direct_exit(code);
 533   }
 534   ShouldNotReachHere();
 535 }
 536 
 537 void notify_vm_shutdown() {
 538   // For now, just a dtrace probe.
 539   HOTSPOT_VM_SHUTDOWN();
 540   HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
 541 }
 542 
 543 void vm_direct_exit(int code) {
 544   notify_vm_shutdown();
 545   os::wait_for_keypress_at_exit();
 546   os::exit(code);
 547 }
 548 
 549 void vm_perform_shutdown_actions() {
 550   // Warning: do not call 'exit_globals()' here. All threads are still running.
 551   // Calling 'exit_globals()' will disable thread-local-storage and cause all
 552   // kinds of assertions to trigger in debug mode.
 553   if (is_init_completed()) {
 554     Thread* thread = ThreadLocalStorage::is_initialized() ?
 555                      ThreadLocalStorage::get_thread_slow() : NULL;
 556     if (thread != NULL && thread->is_Java_thread()) {
 557       // We are leaving the VM, set state to native (in case any OS exit
 558       // handlers call back to the VM)
 559       JavaThread* jt = (JavaThread*)thread;
 560       // Must always be walkable or have no last_Java_frame when in
 561       // thread_in_native
 562       jt->frame_anchor()->make_walkable(jt);
 563       jt->set_thread_state(_thread_in_native);
 564     }
 565   }
 566   notify_vm_shutdown();
 567 }
 568 
 569 void vm_shutdown()
 570 {
 571   vm_perform_shutdown_actions();
 572   os::wait_for_keypress_at_exit();
 573   os::shutdown();
 574 }
 575 
 576 void vm_abort(bool dump_core) {
 577   vm_perform_shutdown_actions();
 578   os::wait_for_keypress_at_exit();
 579 
 580   // Flush stdout and stderr before abort.
 581   fflush(stdout);
 582   fflush(stderr);
 583 
 584   os::abort(dump_core);
 585   ShouldNotReachHere();
 586 }
 587 
 588 void vm_notify_during_shutdown(const char* error, const char* message) {
 589   if (error != NULL) {
 590     tty->print_cr("Error occurred during initialization of VM");
 591     tty->print("%s", error);
 592     if (message != NULL) {
 593       tty->print_cr(": %s", message);
 594     }
 595     else {
 596       tty->cr();
 597     }
 598   }
 599   if (ShowMessageBoxOnError && WizardMode) {
 600     fatal("Error occurred during initialization of VM");
 601   }
 602 }
 603 
 604 void vm_exit_during_initialization(Handle exception) {
 605   tty->print_cr("Error occurred during initialization of VM");
 606   // If there are exceptions on this thread it must be cleared
 607   // first and here. Any future calls to EXCEPTION_MARK requires
 608   // that no pending exceptions exist.
 609   Thread *THREAD = Thread::current();
 610   if (HAS_PENDING_EXCEPTION) {
 611     CLEAR_PENDING_EXCEPTION;
 612   }
 613   java_lang_Throwable::print(exception, tty);
 614   tty->cr();
 615   java_lang_Throwable::print_stack_trace(exception(), tty);
 616   tty->cr();
 617   vm_notify_during_shutdown(NULL, NULL);
 618 
 619   // Failure during initialization, we don't want to dump core
 620   vm_abort(false);
 621 }
 622 
 623 void vm_exit_during_initialization(Symbol* ex, const char* message) {
 624   ResourceMark rm;
 625   vm_notify_during_shutdown(ex->as_C_string(), message);
 626 
 627   // Failure during initialization, we don't want to dump core
 628   vm_abort(false);
 629 }
 630 
 631 void vm_exit_during_initialization(const char* error, const char* message) {
 632   vm_notify_during_shutdown(error, message);
 633 
 634   // Failure during initialization, we don't want to dump core
 635   vm_abort(false);
 636 }
 637 
 638 void vm_shutdown_during_initialization(const char* error, const char* message) {
 639   vm_notify_during_shutdown(error, message);
 640   vm_shutdown();
 641 }
 642 
 643 JDK_Version JDK_Version::_current;
 644 const char* JDK_Version::_runtime_name;
 645 const char* JDK_Version::_runtime_version;
 646 
 647 void JDK_Version::initialize() {
 648   jdk_version_info info;
 649   assert(!_current.is_valid(), "Don't initialize twice");
 650 
 651   void *lib_handle = os::native_java_library();
 652   jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
 653      os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
 654 
 655   assert(func != NULL, "Support for JDK 1.5 or older has been removed after JEP-223");
 656 
 657   (*func)(&info, sizeof(info));
 658 
 659   int major = JDK_VERSION_MAJOR(info.jdk_version);
 660   int minor = JDK_VERSION_MINOR(info.jdk_version);
 661   int security = JDK_VERSION_SECURITY(info.jdk_version);
 662   int build = JDK_VERSION_BUILD(info.jdk_version);
 663 
 664   // Incompatible with pre-4243978 JDK.
 665   if (info.pending_list_uses_discovered_field == 0) {
 666     vm_exit_during_initialization(
 667       "Incompatible JDK is not using Reference.discovered field for pending list");
 668   }
 669   _current = JDK_Version(major, minor, security, info.patch_version, build,
 670                          info.thread_park_blocker == 1,
 671                          info.post_vm_init_hook_enabled == 1);
 672 }
 673 
 674 void JDK_Version_init() {
 675   JDK_Version::initialize();
 676 }
 677 
 678 static int64_t encode_jdk_version(const JDK_Version& v) {
 679   return
 680     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
 681     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
 682     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
 683     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
 684     ((int64_t)v.build_number()           << (BitsPerByte * 0));
 685 }
 686 
 687 int JDK_Version::compare(const JDK_Version& other) const {
 688   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
 689   uint64_t e = encode_jdk_version(*this);
 690   uint64_t o = encode_jdk_version(other);
 691   return (e > o) ? 1 : ((e == o) ? 0 : -1);
 692 }
 693 
 694 void JDK_Version::to_string(char* buffer, size_t buflen) const {
 695   assert(buffer && buflen > 0, "call with useful buffer");
 696   size_t index = 0;
 697 
 698   if (!is_valid()) {
 699     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
 700   } else {
 701     int rc = jio_snprintf(
 702         &buffer[index], buflen - index, "%d.%d", _major, _minor);
 703     if (rc == -1) return;
 704     index += rc;
 705     if (_security > 0) {
 706       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
 707     }
 708     if (_patch > 0) {
 709       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _patch);
 710       if (rc == -1) return;
 711       index += rc;
 712     }
 713     if (_build > 0) {
 714       rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
 715       if (rc == -1) return;
 716       index += rc;
 717     }
 718   }
 719 }