java.cpp 24.0 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29 30 31 32 33 34 35
#include "precompiled.hpp"
#include "classfile/classLoader.hpp"
#include "classfile/symbolTable.hpp"
#include "classfile/systemDictionary.hpp"
#include "code/codeCache.hpp"
#include "compiler/compileBroker.hpp"
#include "compiler/compilerOracle.hpp"
#include "interpreter/bytecodeHistogram.hpp"
#include "memory/genCollectedHeap.hpp"
#include "memory/oopFactory.hpp"
#include "memory/universe.hpp"
36
#include "oops/constantPool.hpp"
37 38 39
#include "oops/generateOopMap.hpp"
#include "oops/instanceKlass.hpp"
#include "oops/instanceOop.hpp"
40
#include "oops/method.hpp"
41 42
#include "oops/objArrayOop.hpp"
#include "oops/oop.inline.hpp"
43
#include "oops/symbol.hpp"
44 45 46 47
#include "prims/jvmtiExport.hpp"
#include "runtime/arguments.hpp"
#include "runtime/biasedLocking.hpp"
#include "runtime/compilationPolicy.hpp"
48
#include "runtime/deoptimization.hpp"
49 50 51 52 53 54 55
#include "runtime/fprofiler.hpp"
#include "runtime/init.hpp"
#include "runtime/interfaceSupport.hpp"
#include "runtime/java.hpp"
#include "runtime/memprofiler.hpp"
#include "runtime/sharedRuntime.hpp"
#include "runtime/statSampler.hpp"
56
#include "runtime/sweeper.hpp"
57
#include "runtime/task.hpp"
58
#include "runtime/thread.inline.hpp"
59 60
#include "runtime/timer.hpp"
#include "runtime/vm_operations.hpp"
61
#include "services/memTracker.hpp"
62
#include "trace/tracing.hpp"
63 64 65
#include "utilities/dtrace.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/histogram.hpp"
66
#include "utilities/macros.hpp"
67 68 69 70 71 72 73 74 75 76
#include "utilities/vmError.hpp"
#ifdef TARGET_ARCH_x86
# include "vm_version_x86.hpp"
#endif
#ifdef TARGET_ARCH_sparc
# include "vm_version_sparc.hpp"
#endif
#ifdef TARGET_ARCH_zero
# include "vm_version_zero.hpp"
#endif
77 78 79 80 81 82
#ifdef TARGET_ARCH_arm
# include "vm_version_arm.hpp"
#endif
#ifdef TARGET_ARCH_ppc
# include "vm_version_ppc.hpp"
#endif
83
#if INCLUDE_ALL_GCS
84 85 86
#include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
#include "gc_implementation/parallelScavenge/psScavenge.hpp"
#include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
87
#endif // INCLUDE_ALL_GCS
88 89 90 91 92 93 94 95 96 97 98
#ifdef COMPILER1
#include "c1/c1_Compiler.hpp"
#include "c1/c1_Runtime1.hpp"
#endif
#ifdef COMPILER2
#include "code/compiledIC.hpp"
#include "compiler/methodLiveness.hpp"
#include "opto/compile.hpp"
#include "opto/indexSet.hpp"
#include "opto/runtime.hpp"
#endif
D
duke 已提交
99

D
dcubed 已提交
100
#ifndef USDT2
D
duke 已提交
101
HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown);
D
dcubed 已提交
102
#endif /* !USDT2 */
D
duke 已提交
103 104 105 106 107

#ifndef PRODUCT

// Statistics printing (method invocation histogram)

108
GrowableArray<Method*>* collected_invoked_methods;
D
duke 已提交
109

110
void collect_invoked_methods(Method* m) {
D
duke 已提交
111 112 113 114 115
  if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
    collected_invoked_methods->push(m);
  }
}

116
PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
D
duke 已提交
117

118
GrowableArray<Method*>* collected_profiled_methods;
D
duke 已提交
119

120 121 122 123 124 125
void collect_profiled_methods(Method* m) {
  Thread* thread = Thread::current();
  // This HandleMark prevents a huge amount of handles from being added
  // to the metadata_handles() array on the thread.
  HandleMark hm(thread);
  methodHandle mh(thread, m);
D
duke 已提交
126 127 128 129 130 131 132
  if ((m->method_data() != NULL) &&
      (PrintMethodData || CompilerOracle::should_print(mh))) {
    collected_profiled_methods->push(m);
  }
}


133
int compare_methods(Method** a, Method** b) {
D
duke 已提交
134 135 136 137 138 139 140 141 142
  // %%% there can be 32-bit overflow here
  return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
       - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
}


void print_method_invocation_histogram() {
  ResourceMark rm;
  HandleMark hm;
143
  collected_invoked_methods = new GrowableArray<Method*>(1024);
D
duke 已提交
144 145 146 147 148 149 150 151 152 153
  SystemDictionary::methods_do(collect_invoked_methods);
  collected_invoked_methods->sort(&compare_methods);
  //
  tty->cr();
  tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = %d):", MethodHistogramCutoff);
  tty->cr();
  tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
  unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
      synch_total = 0, nativ_total = 0, acces_total = 0;
  for (int index = 0; index < collected_invoked_methods->length(); index++) {
154
    Method* m = collected_invoked_methods->at(index);
D
duke 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
    int c = m->invocation_count() + m->compiled_invocation_count();
    if (c >= MethodHistogramCutoff) m->print_invocation_count();
    int_total  += m->invocation_count();
    comp_total += m->compiled_invocation_count();
    if (m->is_final())        final_total  += c;
    if (m->is_static())       static_total += c;
    if (m->is_synchronized()) synch_total  += c;
    if (m->is_native())       nativ_total  += c;
    if (m->is_accessor())     acces_total  += c;
  }
  tty->cr();
  total = int_total + comp_total;
  tty->print_cr("Invocations summary:");
  tty->print_cr("\t%9d (%4.1f%%) interpreted",  int_total,    100.0 * int_total    / total);
  tty->print_cr("\t%9d (%4.1f%%) compiled",     comp_total,   100.0 * comp_total   / total);
  tty->print_cr("\t%9d (100%%)  total",         total);
  tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total,  100.0 * synch_total  / total);
  tty->print_cr("\t%9d (%4.1f%%) final",        final_total,  100.0 * final_total  / total);
  tty->print_cr("\t%9d (%4.1f%%) static",       static_total, 100.0 * static_total / total);
  tty->print_cr("\t%9d (%4.1f%%) native",       nativ_total,  100.0 * nativ_total  / total);
  tty->print_cr("\t%9d (%4.1f%%) accessor",     acces_total,  100.0 * acces_total  / total);
  tty->cr();
  SharedRuntime::print_call_statistics(comp_total);
}

void print_method_profiling_data() {
  ResourceMark rm;
  HandleMark hm;
183
  collected_profiled_methods = new GrowableArray<Method*>(1024);
D
duke 已提交
184 185 186 187
  SystemDictionary::methods_do(collect_profiled_methods);
  collected_profiled_methods->sort(&compare_methods);

  int count = collected_profiled_methods->length();
188
  int total_size = 0;
D
duke 已提交
189 190
  if (count > 0) {
    for (int index = 0; index < count; index++) {
191
      Method* m = collected_profiled_methods->at(index);
D
duke 已提交
192 193 194 195
      ttyLocker ttyl;
      tty->print_cr("------------------------------------------------------------------------");
      //m->print_name(tty);
      m->print_invocation_count();
196
      tty->print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
D
duke 已提交
197
      tty->cr();
198 199 200 201 202
      // Dump data on parameters if any
      if (m->method_data() != NULL && m->method_data()->parameters_type_data() != NULL) {
        tty->fill_to(2);
        m->method_data()->parameters_type_data()->print_data_on(tty);
      }
D
duke 已提交
203
      m->print_codes();
204
      total_size += m->method_data()->size_in_bytes();
D
duke 已提交
205 206
    }
    tty->print_cr("------------------------------------------------------------------------");
207
    tty->print_cr("Total MDO size: %d bytes", total_size);
D
duke 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  }
}

void print_bytecode_count() {
  if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
    tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
  }
}

AllocStats alloc_stats;



// General statistics printing (profiling ...)
void print_statistics() {
#ifdef ASSERT

  if (CountRuntimeCalls) {
    extern Histogram *RuntimeHistogram;
    RuntimeHistogram->print();
  }

  if (CountJNICalls) {
    extern Histogram *JNIHistogram;
    JNIHistogram->print();
  }

  if (CountJVMCalls) {
    extern Histogram *JVMHistogram;
    JVMHistogram->print();
  }

#endif

  if (MemProfiling) {
    MemProfiler::disengage();
  }

  if (CITime) {
    CompileBroker::print_times();
  }

#ifdef COMPILER1
  if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
    FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
    Runtime1::print_statistics();
    Deoptimization::print_statistics();
255
    SharedRuntime::print_statistics();
D
duke 已提交
256 257 258 259 260 261 262 263 264 265 266 267
    nmethod::print_statistics();
  }
#endif /* COMPILER1 */

#ifdef COMPILER2
  if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
    FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
    Compile::print_statistics();
#ifndef COMPILER1
    Deoptimization::print_statistics();
    nmethod::print_statistics();
    SharedRuntime::print_statistics();
268
#endif //COMPILER1
D
duke 已提交
269 270 271
    os::print_statistics();
  }

272
  if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
D
duke 已提交
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    OptoRuntime::print_named_counters();
  }

  if (TimeLivenessAnalysis) {
    MethodLiveness::print_times();
  }
#ifdef ASSERT
  if (CollectIndexSetStatistics) {
    IndexSet::print_statistics();
  }
#endif // ASSERT
#endif // COMPILER2
  if (CountCompiledCalls) {
    print_method_invocation_histogram();
  }
288
  if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) {
D
duke 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
    print_method_profiling_data();
  }
  if (TimeCompiler) {
    COMPILER2_PRESENT(Compile::print_timers();)
  }
  if (TimeCompilationPolicy) {
    CompilationPolicy::policy()->print_time();
  }
  if (TimeOopMap) {
    GenerateOopMap::print_time();
  }
  if (ProfilerCheckIntervals) {
    PeriodicTask::print_intervals();
  }
  if (PrintSymbolTableSizeHistogram) {
    SymbolTable::print_histogram();
  }
  if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
    BytecodeCounter::print();
  }
  if (PrintBytecodePairHistogram) {
    BytecodePairHistogram::print();
  }

  if (PrintCodeCache) {
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
    CodeCache::print();
  }

318 319 320 321
  if (PrintMethodFlushingStatistics) {
    NMethodSweeper::print();
  }

D
duke 已提交
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
  if (PrintCodeCache2) {
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
    CodeCache::print_internals();
  }

  if (PrintClassStatistics) {
    SystemDictionary::print_class_statistics();
  }
  if (PrintMethodStatistics) {
    SystemDictionary::print_method_statistics();
  }

  if (PrintVtableStats) {
    klassVtable::print_statistics();
    klassItable::print_statistics();
  }
  if (VerifyOops) {
    tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
  }

  print_bytecode_count();
343
  if (PrintMallocStatistics) {
D
duke 已提交
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
    tty->print("allocation stats: ");
    alloc_stats.print();
    tty->cr();
  }

  if (PrintSystemDictionaryAtExit) {
    SystemDictionary::print();
  }

  if (PrintBiasedLockingStatistics) {
    BiasedLocking::print_counters();
  }

#ifdef ENABLE_ZAP_DEAD_LOCALS
#ifdef COMPILER2
  if (ZapDeadCompiledLocals) {
    tty->print_cr("Compile::CompiledZap_count = %d", Compile::CompiledZap_count);
    tty->print_cr("OptoRuntime::ZapDeadCompiledLocals_count = %d", OptoRuntime::ZapDeadCompiledLocals_count);
  }
#endif // COMPILER2
#endif // ENABLE_ZAP_DEAD_LOCALS
365 366
  // Native memory tracking data
  if (PrintNMTStatistics) {
367
    MemTracker::final_report(tty);
368
  }
D
duke 已提交
369 370 371 372 373 374 375 376 377
}

#else // PRODUCT MODE STATISTICS

void print_statistics() {

  if (CITime) {
    CompileBroker::print_times();
  }
378 379 380 381 382 383

  if (PrintCodeCache) {
    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
    CodeCache::print();
  }

384 385 386 387
  if (PrintMethodFlushingStatistics) {
    NMethodSweeper::print();
  }

D
duke 已提交
388
#ifdef COMPILER2
389
  if (PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
D
duke 已提交
390 391 392 393 394 395
    OptoRuntime::print_named_counters();
  }
#endif
  if (PrintBiasedLockingStatistics) {
    BiasedLocking::print_counters();
  }
396 397 398

  // Native memory tracking data
  if (PrintNMTStatistics) {
399
    MemTracker::final_report(tty);
400
  }
D
duke 已提交
401 402 403 404 405 406 407 408 409 410 411
}

#endif


// Helper class for registering on_exit calls through JVM_OnExit

extern "C" {
    typedef void (*__exit_proc)(void);
}

Z
zgu 已提交
412
class ExitProc : public CHeapObj<mtInternal> {
D
duke 已提交
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
 private:
  __exit_proc _proc;
  // void (*_proc)(void);
  ExitProc* _next;
 public:
  // ExitProc(void (*proc)(void)) {
  ExitProc(__exit_proc proc) {
    _proc = proc;
    _next = NULL;
  }
  void evaluate()               { _proc(); }
  ExitProc* next() const        { return _next; }
  void set_next(ExitProc* next) { _next = next; }
};


// Linked list of registered on_exit procedures

static ExitProc* exit_procs = NULL;


extern "C" {
  void register_on_exit_function(void (*func)(void)) {
    ExitProc *entry = new ExitProc(func);
    // Classic vm does not throw an exception in case the allocation failed,
    if (entry != NULL) {
      entry->set_next(exit_procs);
      exit_procs = entry;
    }
  }
}

// Note: before_exit() can be executed only once, if more than one threads
//       are trying to shutdown the VM at the same time, only one thread
//       can run before_exit() and all other threads must wait.
void before_exit(JavaThread * thread) {
  #define BEFORE_EXIT_NOT_RUN 0
  #define BEFORE_EXIT_RUNNING 1
  #define BEFORE_EXIT_DONE    2
  static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;

  // Note: don't use a Mutex to guard the entire before_exit(), as
  // JVMTI post_thread_end_event and post_vm_death_event will run native code.
  // A CAS or OSMutex would work just fine but then we need to manipulate
  // thread state for Safepoint. Here we use Monitor wait() and notify_all()
  // for synchronization.
  { MutexLocker ml(BeforeExit_lock);
    switch (_before_exit_status) {
    case BEFORE_EXIT_NOT_RUN:
      _before_exit_status = BEFORE_EXIT_RUNNING;
      break;
    case BEFORE_EXIT_RUNNING:
      while (_before_exit_status == BEFORE_EXIT_RUNNING) {
        BeforeExit_lock->wait();
      }
      assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
      return;
    case BEFORE_EXIT_DONE:
      return;
    }
  }

  // The only difference between this and Win32's _onexit procs is that
  // this version is invoked before any threads get killed.
  ExitProc* current = exit_procs;
  while (current != NULL) {
    ExitProc* next = current->next();
    current->evaluate();
    delete current;
    current = next;
  }

  // Hang forever on exit if we're reporting an error.
  if (ShowMessageBoxOnError && is_error_reported()) {
    os::infinite_sleep();
  }

  // Terminate watcher thread - must before disenrolling any periodic task
491 492
  if (PeriodicTask::num_tasks() > 0)
    WatcherThread::stop();
D
duke 已提交
493 494 495 496 497 498 499 500 501 502 503

  // Print statistics gathered (profiling ...)
  if (Arguments::has_profile()) {
    FlatProfiler::disengage();
    FlatProfiler::print(10);
  }

  // shut down the StatSampler task
  StatSampler::disengage();
  StatSampler::destroy();

504 505
  // Stop concurrent GC threads
  Universe::heap()->stop();
D
duke 已提交
506 507 508 509 510

  // Print GC/heap related information.
  if (PrintGCDetails) {
    Universe::print();
    AdaptiveSizePolicyOutput(0);
511 512 513
    if (Verbose) {
      ClassLoaderDataGraph::dump_on(gclog_or_tty);
    }
D
duke 已提交
514 515 516 517 518 519 520 521 522
  }

  if (PrintBytecodeHistogram) {
    BytecodeHistogram::print();
  }

  if (JvmtiExport::should_post_thread_life()) {
    JvmtiExport::post_thread_end(thread);
  }
523

S
sla 已提交
524 525 526 527 528 529

  EventThreadEnd event;
  if (event.should_commit()) {
      event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
      event.commit();
  }
530

D
duke 已提交
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547
  // Always call even when there are not JVMTI environments yet, since environments
  // may be attached late and JVMTI must track phases of VM execution
  JvmtiExport::post_vm_death();
  Threads::shutdown_vm_agents();

  // Terminate the signal thread
  // Note: we don't wait until it actually dies.
  os::terminate_signal_thread();

  print_statistics();
  Universe::heap()->print_tracing_info();

  { MutexLocker ml(BeforeExit_lock);
    _before_exit_status = BEFORE_EXIT_DONE;
    BeforeExit_lock->notify_all();
  }

548 549 550 551 552 553 554 555 556 557 558 559 560
  if (VerifyStringTableAtExit) {
    int fail_cnt = 0;
    {
      MutexLocker ml(StringTable_lock);
      fail_cnt = StringTable::verify_and_compare_entries();
    }

    if (fail_cnt != 0) {
      tty->print_cr("ERROR: fail_cnt=%d", fail_cnt);
      guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
    }
  }

D
duke 已提交
561 562 563 564 565 566
  #undef BEFORE_EXIT_NOT_RUN
  #undef BEFORE_EXIT_RUNNING
  #undef BEFORE_EXIT_DONE
}

void vm_exit(int code) {
567 568
  Thread* thread = ThreadLocalStorage::is_initialized() ?
    ThreadLocalStorage::get_thread_slow() : NULL;
D
duke 已提交
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590
  if (thread == NULL) {
    // we have serious problems -- just exit
    vm_direct_exit(code);
  }

  if (VMThread::vm_thread() != NULL) {
    // Fire off a VM_Exit operation to bring VM to a safepoint and exit
    VM_Exit op(code);
    if (thread->is_Java_thread())
      ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
    VMThread::execute(&op);
    // should never reach here; but in case something wrong with VM Thread.
    vm_direct_exit(code);
  } else {
    // VM thread is gone, just exit
    vm_direct_exit(code);
  }
  ShouldNotReachHere();
}

void notify_vm_shutdown() {
  // For now, just a dtrace probe.
D
dcubed 已提交
591
#ifndef USDT2
D
duke 已提交
592
  HS_DTRACE_PROBE(hotspot, vm__shutdown);
593
  HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
D
dcubed 已提交
594 595 596
#else /* USDT2 */
  HOTSPOT_VM_SHUTDOWN();
#endif /* USDT2 */
D
duke 已提交
597 598 599 600
}

void vm_direct_exit(int code) {
  notify_vm_shutdown();
601
  os::wait_for_keypress_at_exit();
D
duke 已提交
602 603 604 605 606 607 608 609
  ::exit(code);
}

void vm_perform_shutdown_actions() {
  // Warning: do not call 'exit_globals()' here. All threads are still running.
  // Calling 'exit_globals()' will disable thread-local-storage and cause all
  // kinds of assertions to trigger in debug mode.
  if (is_init_completed()) {
610 611 612
    Thread* thread = ThreadLocalStorage::is_initialized() ?
                     ThreadLocalStorage::get_thread_slow() : NULL;
    if (thread != NULL && thread->is_Java_thread()) {
D
duke 已提交
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
      // We are leaving the VM, set state to native (in case any OS exit
      // handlers call back to the VM)
      JavaThread* jt = (JavaThread*)thread;
      // Must always be walkable or have no last_Java_frame when in
      // thread_in_native
      jt->frame_anchor()->make_walkable(jt);
      jt->set_thread_state(_thread_in_native);
    }
  }
  notify_vm_shutdown();
}

void vm_shutdown()
{
  vm_perform_shutdown_actions();
628
  os::wait_for_keypress_at_exit();
D
duke 已提交
629 630 631
  os::shutdown();
}

632
void vm_abort(bool dump_core) {
D
duke 已提交
633
  vm_perform_shutdown_actions();
634
  os::wait_for_keypress_at_exit();
635
  os::abort(dump_core);
D
duke 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
  ShouldNotReachHere();
}

void vm_notify_during_shutdown(const char* error, const char* message) {
  if (error != NULL) {
    tty->print_cr("Error occurred during initialization of VM");
    tty->print("%s", error);
    if (message != NULL) {
      tty->print_cr(": %s", message);
    }
    else {
      tty->cr();
    }
  }
  if (ShowMessageBoxOnError && WizardMode) {
    fatal("Error occurred during initialization of VM");
  }
}

void vm_exit_during_initialization(Handle exception) {
  tty->print_cr("Error occurred during initialization of VM");
  // If there are exceptions on this thread it must be cleared
  // first and here. Any future calls to EXCEPTION_MARK requires
  // that no pending exceptions exist.
  Thread *THREAD = Thread::current();
  if (HAS_PENDING_EXCEPTION) {
    CLEAR_PENDING_EXCEPTION;
  }
  java_lang_Throwable::print(exception, tty);
  tty->cr();
  java_lang_Throwable::print_stack_trace(exception(), tty);
  tty->cr();
  vm_notify_during_shutdown(NULL, NULL);
669 670 671

  // Failure during initialization, we don't want to dump core
  vm_abort(false);
D
duke 已提交
672 673
}

674
void vm_exit_during_initialization(Symbol* ex, const char* message) {
D
duke 已提交
675 676
  ResourceMark rm;
  vm_notify_during_shutdown(ex->as_C_string(), message);
677 678 679

  // Failure during initialization, we don't want to dump core
  vm_abort(false);
D
duke 已提交
680 681 682 683
}

void vm_exit_during_initialization(const char* error, const char* message) {
  vm_notify_during_shutdown(error, message);
684 685 686

  // Failure during initialization, we don't want to dump core
  vm_abort(false);
D
duke 已提交
687 688 689 690 691 692 693
}

void vm_shutdown_during_initialization(const char* error, const char* message) {
  vm_notify_during_shutdown(error, message);
  vm_shutdown();
}

K
kamg 已提交
694
JDK_Version JDK_Version::_current;
695
const char* JDK_Version::_runtime_name;
696
const char* JDK_Version::_runtime_version;
D
duke 已提交
697 698

void JDK_Version::initialize() {
K
kamg 已提交
699 700 701
  jdk_version_info info;
  assert(!_current.is_valid(), "Don't initialize twice");

D
duke 已提交
702
  void *lib_handle = os::native_java_library();
K
kamg 已提交
703 704
  jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
     os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
D
duke 已提交
705 706 707

  if (func == NULL) {
    // JDK older than 1.6
K
kamg 已提交
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723
    _current._partially_initialized = true;
  } else {
    (*func)(&info, sizeof(info));

    int major = JDK_VERSION_MAJOR(info.jdk_version);
    int minor = JDK_VERSION_MINOR(info.jdk_version);
    int micro = JDK_VERSION_MICRO(info.jdk_version);
    int build = JDK_VERSION_BUILD(info.jdk_version);
    if (major == 1 && minor > 4) {
      // We represent "1.5.0" as "5.0", but 1.4.2 as itself.
      major = minor;
      minor = micro;
      micro = 0;
    }
    _current = JDK_Version(major, minor, micro, info.update_version,
                           info.special_update_version, build,
724
                           info.thread_park_blocker == 1,
725 726
                           info.post_vm_init_hook_enabled == 1,
                           info.pending_list_uses_discovered_field == 1);
D
duke 已提交
727
  }
K
kamg 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
}

void JDK_Version::fully_initialize(
    uint8_t major, uint8_t minor, uint8_t micro, uint8_t update) {
  // This is only called when current is less than 1.6 and we've gotten
  // far enough in the initialization to determine the exact version.
  assert(major < 6, "not needed for JDK version >= 6");
  assert(is_partially_initialized(), "must not initialize");
  if (major < 5) {
    // JDK verison sequence: 1.2.x, 1.3.x, 1.4.x, 5.0.x, 6.0.x, etc.
    micro = minor;
    minor = major;
    major = 1;
  }
  _current = JDK_Version(major, minor, micro, update);
}

void JDK_Version_init() {
  JDK_Version::initialize();
}
D
duke 已提交
748

K
kamg 已提交
749 750 751 752 753 754 755 756 757 758 759 760 761 762
static int64_t encode_jdk_version(const JDK_Version& v) {
  return
    ((int64_t)v.major_version()          << (BitsPerByte * 5)) |
    ((int64_t)v.minor_version()          << (BitsPerByte * 4)) |
    ((int64_t)v.micro_version()          << (BitsPerByte * 3)) |
    ((int64_t)v.update_version()         << (BitsPerByte * 2)) |
    ((int64_t)v.special_update_version() << (BitsPerByte * 1)) |
    ((int64_t)v.build_number()           << (BitsPerByte * 0));
}

int JDK_Version::compare(const JDK_Version& other) const {
  assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
  if (!is_partially_initialized() && other.is_partially_initialized()) {
    return -(other.compare(*this)); // flip the comparators
D
duke 已提交
763
  }
K
kamg 已提交
764 765 766 767 768
  assert(!other.is_partially_initialized(), "Not initialized yet");
  if (is_partially_initialized()) {
    assert(other.major_version() >= 6,
           "Invalid JDK version comparison during initialization");
    return -1;
D
duke 已提交
769
  } else {
K
kamg 已提交
770 771 772
    uint64_t e = encode_jdk_version(*this);
    uint64_t o = encode_jdk_version(other);
    return (e > o) ? 1 : ((e == o) ? 0 : -1);
D
duke 已提交
773 774 775
  }
}

K
kamg 已提交
776
void JDK_Version::to_string(char* buffer, size_t buflen) const {
777
  assert(buffer && buflen > 0, "call with useful buffer");
K
kamg 已提交
778 779 780 781 782 783
  size_t index = 0;
  if (!is_valid()) {
    jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
  } else if (is_partially_initialized()) {
    jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0");
  } else {
784
    int rc = jio_snprintf(
K
kamg 已提交
785
        &buffer[index], buflen - index, "%d.%d", _major, _minor);
786 787
    if (rc == -1) return;
    index += rc;
K
kamg 已提交
788
    if (_micro > 0) {
789 790 791
      rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _micro);
      if (rc == -1) return;
      index += rc;
K
kamg 已提交
792 793
    }
    if (_update > 0) {
794 795 796
      rc = jio_snprintf(&buffer[index], buflen - index, "_%02d", _update);
      if (rc == -1) return;
      index += rc;
K
kamg 已提交
797 798
    }
    if (_special > 0) {
799 800 801
      rc = jio_snprintf(&buffer[index], buflen - index, "%c", _special);
      if (rc == -1) return;
      index += rc;
K
kamg 已提交
802 803
    }
    if (_build > 0) {
804 805 806
      rc = jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build);
      if (rc == -1) return;
      index += rc;
K
kamg 已提交
807 808
    }
  }
D
duke 已提交
809
}