java.cpp revision 13213:4358b7205556
1183593Sjkoshy/*
2183593Sjkoshy * Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
3183593Sjkoshy * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4183593Sjkoshy *
5183593Sjkoshy * This code is free software; you can redistribute it and/or modify it
6183593Sjkoshy * under the terms of the GNU General Public License version 2 only, as
7183593Sjkoshy * published by the Free Software Foundation.
8183593Sjkoshy *
9183593Sjkoshy * This code is distributed in the hope that it will be useful, but WITHOUT
10183593Sjkoshy * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11183593Sjkoshy * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12231871Sbrueffer * version 2 for more details (a copy is included in the LICENSE file that
13231871Sbrueffer * accompanied this code).
14231871Sbrueffer *
15231871Sbrueffer * You should have received a copy of the GNU General Public License version
16231871Sbrueffer * 2 along with this work; if not, write to the Free Software Foundation,
17231871Sbrueffer * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18231871Sbrueffer *
19231871Sbrueffer * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20231871Sbrueffer * or visit www.oracle.com if you need additional information or have any
21231871Sbrueffer * questions.
22231871Sbrueffer *
23183593Sjkoshy */
24183593Sjkoshy
25183593Sjkoshy#include "precompiled.hpp"
26266911Shiren#include "aot/aotLoader.hpp"
27206622Suqs#include "classfile/classLoader.hpp"
28183593Sjkoshy#include "classfile/stringTable.hpp"
29183593Sjkoshy#include "classfile/systemDictionary.hpp"
30183593Sjkoshy#include "code/codeCache.hpp"
31183593Sjkoshy#include "compiler/compileBroker.hpp"
32183593Sjkoshy#include "compiler/compilerOracle.hpp"
33183593Sjkoshy#include "gc/shared/genCollectedHeap.hpp"
34183593Sjkoshy#include "interpreter/bytecodeHistogram.hpp"
35183593Sjkoshy#if INCLUDE_JVMCI
36183593Sjkoshy#include "jvmci/jvmciCompiler.hpp"
37183593Sjkoshy#include "jvmci/jvmciRuntime.hpp"
38183593Sjkoshy#endif
39183593Sjkoshy#include "logging/log.hpp"
40183593Sjkoshy#include "memory/oopFactory.hpp"
41183593Sjkoshy#include "memory/resourceArea.hpp"
42183593Sjkoshy#include "memory/universe.hpp"
43183593Sjkoshy#include "oops/constantPool.hpp"
44183593Sjkoshy#include "oops/generateOopMap.hpp"
45183593Sjkoshy#include "oops/instanceKlass.hpp"
46183593Sjkoshy#include "oops/instanceOop.hpp"
47183593Sjkoshy#include "oops/method.hpp"
48183593Sjkoshy#include "oops/objArrayOop.hpp"
49183593Sjkoshy#include "oops/oop.inline.hpp"
50183593Sjkoshy#include "oops/symbol.hpp"
51183593Sjkoshy#include "prims/jvm.h"
52183593Sjkoshy#include "prims/jvmtiExport.hpp"
53183593Sjkoshy#include "runtime/arguments.hpp"
54183593Sjkoshy#include "runtime/biasedLocking.hpp"
55183593Sjkoshy#include "runtime/compilationPolicy.hpp"
56183593Sjkoshy#include "runtime/deoptimization.hpp"
57183593Sjkoshy#include "runtime/fprofiler.hpp"
58183593Sjkoshy#include "runtime/init.hpp"
59183593Sjkoshy#include "runtime/interfaceSupport.hpp"
60183593Sjkoshy#include "runtime/java.hpp"
61183593Sjkoshy#include "runtime/memprofiler.hpp"
62183593Sjkoshy#include "runtime/sharedRuntime.hpp"
63183593Sjkoshy#include "runtime/statSampler.hpp"
64183593Sjkoshy#include "runtime/sweeper.hpp"
65183593Sjkoshy#include "runtime/task.hpp"
66183593Sjkoshy#include "runtime/thread.inline.hpp"
67183593Sjkoshy#include "runtime/timer.hpp"
68183593Sjkoshy#include "runtime/vm_operations.hpp"
69183593Sjkoshy#include "services/memTracker.hpp"
70183593Sjkoshy#include "trace/traceMacros.hpp"
71183593Sjkoshy#include "trace/tracing.hpp"
72183593Sjkoshy#include "utilities/dtrace.hpp"
73183593Sjkoshy#include "utilities/globalDefinitions.hpp"
74183593Sjkoshy#include "utilities/histogram.hpp"
75183593Sjkoshy#include "utilities/macros.hpp"
76183593Sjkoshy#include "utilities/vmError.hpp"
77183593Sjkoshy#if INCLUDE_ALL_GCS
78183593Sjkoshy#include "gc/cms/concurrentMarkSweepThread.hpp"
79183593Sjkoshy#include "gc/parallel/psScavenge.hpp"
80183593Sjkoshy#endif // INCLUDE_ALL_GCS
81183593Sjkoshy#ifdef COMPILER1
82183593Sjkoshy#include "c1/c1_Compiler.hpp"
83183593Sjkoshy#include "c1/c1_Runtime1.hpp"
84183593Sjkoshy#endif
85183593Sjkoshy#ifdef COMPILER2
86183593Sjkoshy#include "code/compiledIC.hpp"
87183593Sjkoshy#include "compiler/methodLiveness.hpp"
88183593Sjkoshy#include "opto/compile.hpp"
89183593Sjkoshy#include "opto/indexSet.hpp"
90183593Sjkoshy#include "opto/runtime.hpp"
91183593Sjkoshy#endif
92183593Sjkoshy
93183593SjkoshyGrowableArray<Method*>* collected_profiled_methods;
94183593Sjkoshy
95183593Sjkoshyint compare_methods(Method** a, Method** b) {
96196449Sjkoshy  // %%% there can be 32-bit overflow here
97183593Sjkoshy  return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
98183593Sjkoshy       - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
99183593Sjkoshy}
100183593Sjkoshy
101183593Sjkoshyvoid collect_profiled_methods(Method* m) {
102196449Sjkoshy  Thread* thread = Thread::current();
103183593Sjkoshy  methodHandle mh(thread, m);
104183593Sjkoshy  if ((m->method_data() != NULL) &&
105183593Sjkoshy      (PrintMethodData || CompilerOracle::should_print(mh))) {
106183593Sjkoshy    collected_profiled_methods->push(m);
107183593Sjkoshy  }
108183593Sjkoshy}
109183593Sjkoshy
110183593Sjkoshyvoid print_method_profiling_data() {
111183593Sjkoshy  if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData) &&
112183593Sjkoshy     (PrintMethodData || CompilerOracle::should_print_methods())) {
113183593Sjkoshy    ResourceMark rm;
114183593Sjkoshy    HandleMark hm;
115183593Sjkoshy    collected_profiled_methods = new GrowableArray<Method*>(1024);
116183593Sjkoshy    SystemDictionary::methods_do(collect_profiled_methods);
117183593Sjkoshy    collected_profiled_methods->sort(&compare_methods);
118183593Sjkoshy
119183593Sjkoshy    int count = collected_profiled_methods->length();
120183593Sjkoshy    int total_size = 0;
121183593Sjkoshy    if (count > 0) {
122183593Sjkoshy      for (int index = 0; index < count; index++) {
123183593Sjkoshy        Method* m = collected_profiled_methods->at(index);
124183593Sjkoshy        ttyLocker ttyl;
125183593Sjkoshy        tty->print_cr("------------------------------------------------------------------------");
126183593Sjkoshy        m->print_invocation_count();
127183593Sjkoshy        tty->print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
128183593Sjkoshy        tty->cr();
129183593Sjkoshy        // Dump data on parameters if any
130183593Sjkoshy        if (m->method_data() != NULL && m->method_data()->parameters_type_data() != NULL) {
131183593Sjkoshy          tty->fill_to(2);
132183593Sjkoshy          m->method_data()->parameters_type_data()->print_data_on(tty);
133183593Sjkoshy        }
134183593Sjkoshy        m->print_codes();
135183593Sjkoshy        total_size += m->method_data()->size_in_bytes();
136183593Sjkoshy      }
137183593Sjkoshy      tty->print_cr("------------------------------------------------------------------------");
138183593Sjkoshy      tty->print_cr("Total MDO size: %d bytes", total_size);
139183593Sjkoshy    }
140183593Sjkoshy  }
141183593Sjkoshy}
142183593Sjkoshy
143183593Sjkoshy
144183593Sjkoshy#ifndef PRODUCT
145183593Sjkoshy
146183593Sjkoshy// Statistics printing (method invocation histogram)
147183593Sjkoshy
148183593SjkoshyGrowableArray<Method*>* collected_invoked_methods;
149183593Sjkoshy
150183593Sjkoshyvoid collect_invoked_methods(Method* m) {
151183593Sjkoshy  if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
152183593Sjkoshy    collected_invoked_methods->push(m);
153183593Sjkoshy  }
154183593Sjkoshy}
155183593Sjkoshy
156183593Sjkoshy
157183593Sjkoshy
158183593Sjkoshy
159183593Sjkoshyvoid print_method_invocation_histogram() {
160183593Sjkoshy  ResourceMark rm;
161183593Sjkoshy  HandleMark hm;
162183593Sjkoshy  collected_invoked_methods = new GrowableArray<Method*>(1024);
163183593Sjkoshy  SystemDictionary::methods_do(collect_invoked_methods);
164183593Sjkoshy  collected_invoked_methods->sort(&compare_methods);
165183593Sjkoshy  //
166183593Sjkoshy  tty->cr();
167183593Sjkoshy  tty->print_cr("Histogram Over Method Invocation Counters (cutoff = " INTX_FORMAT "):", MethodHistogramCutoff);
168183593Sjkoshy  tty->cr();
169183593Sjkoshy  tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
170183593Sjkoshy  unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
171183593Sjkoshy      synch_total = 0, nativ_total = 0, acces_total = 0;
172183593Sjkoshy  for (int index = 0; index < collected_invoked_methods->length(); index++) {
173196449Sjkoshy    Method* m = collected_invoked_methods->at(index);
174183593Sjkoshy    int c = m->invocation_count() + m->compiled_invocation_count();
175183593Sjkoshy    if (c >= MethodHistogramCutoff) m->print_invocation_count();
176183593Sjkoshy    int_total  += m->invocation_count();
177183593Sjkoshy    comp_total += m->compiled_invocation_count();
178183593Sjkoshy    if (m->is_final())        final_total  += c;
179183593Sjkoshy    if (m->is_static())       static_total += c;
180183593Sjkoshy    if (m->is_synchronized()) synch_total  += c;
181183593Sjkoshy    if (m->is_native())       nativ_total  += c;
182183593Sjkoshy    if (m->is_accessor())     acces_total  += c;
183183593Sjkoshy  }
184183593Sjkoshy  tty->cr();
185183593Sjkoshy  total = int_total + comp_total;
186183593Sjkoshy  tty->print_cr("Invocations summary:");
187183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) interpreted",  int_total,    100.0 * int_total    / total);
188183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) compiled",     comp_total,   100.0 * comp_total   / total);
189183593Sjkoshy  tty->print_cr("\t%9d (100%%)  total",         total);
190183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total,  100.0 * synch_total  / total);
191183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) final",        final_total,  100.0 * final_total  / total);
192183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) static",       static_total, 100.0 * static_total / total);
193183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) native",       nativ_total,  100.0 * nativ_total  / total);
194183593Sjkoshy  tty->print_cr("\t%9d (%4.1f%%) accessor",     acces_total,  100.0 * acces_total  / total);
195183593Sjkoshy  tty->cr();
196183593Sjkoshy  SharedRuntime::print_call_statistics(comp_total);
197183593Sjkoshy}
198183593Sjkoshy
199183593Sjkoshyvoid print_bytecode_count() {
200183593Sjkoshy  if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
201183593Sjkoshy    tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
202183593Sjkoshy  }
203183593Sjkoshy}
204183593Sjkoshy
205183593SjkoshyAllocStats alloc_stats;
206183593Sjkoshy
207183593Sjkoshy
208183593Sjkoshy
209183593Sjkoshy// General statistics printing (profiling ...)
210183593Sjkoshyvoid print_statistics() {
211183593Sjkoshy#ifdef ASSERT
212183593Sjkoshy
213183593Sjkoshy  if (CountRuntimeCalls) {
214183593Sjkoshy    extern Histogram *RuntimeHistogram;
215183593Sjkoshy    RuntimeHistogram->print();
216183593Sjkoshy  }
217183593Sjkoshy
218183593Sjkoshy  if (CountJNICalls) {
219183593Sjkoshy    extern Histogram *JNIHistogram;
220183593Sjkoshy    JNIHistogram->print();
221183593Sjkoshy  }
222183593Sjkoshy
223183593Sjkoshy  if (CountJVMCalls) {
224183593Sjkoshy    extern Histogram *JVMHistogram;
225196437Sjkoshy    JVMHistogram->print();
226183593Sjkoshy  }
227183593Sjkoshy
228183593Sjkoshy#endif
229183593Sjkoshy
230183593Sjkoshy  if (MemProfiling) {
231184914Sjkoshy    MemProfiler::disengage();
232183593Sjkoshy  }
233183593Sjkoshy
234183593Sjkoshy  if (CITime) {
235184914Sjkoshy    CompileBroker::print_times();
236183593Sjkoshy  }
237183593Sjkoshy
238183593Sjkoshy#ifdef COMPILER1
239184914Sjkoshy  if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
240183593Sjkoshy    FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
241183593Sjkoshy    Runtime1::print_statistics();
242183593Sjkoshy    Deoptimization::print_statistics();
243183593Sjkoshy    SharedRuntime::print_statistics();
244184914Sjkoshy  }
245183593Sjkoshy#endif /* COMPILER1 */
246183593Sjkoshy
247183593Sjkoshy#ifdef COMPILER2
248183593Sjkoshy  if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
249184914Sjkoshy    FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
250183593Sjkoshy    Compile::print_statistics();
251183593Sjkoshy#ifndef COMPILER1
252184914Sjkoshy    Deoptimization::print_statistics();
253183593Sjkoshy    SharedRuntime::print_statistics();
254183593Sjkoshy#endif //COMPILER1
255184914Sjkoshy    os::print_statistics();
256183593Sjkoshy  }
257183593Sjkoshy
258183593Sjkoshy  if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
259183593Sjkoshy    OptoRuntime::print_named_counters();
260184914Sjkoshy  }
261183593Sjkoshy
262183593Sjkoshy  if (TimeLivenessAnalysis) {
263184914Sjkoshy    MethodLiveness::print_times();
264183593Sjkoshy  }
265183593Sjkoshy#ifdef ASSERT
266183593Sjkoshy  if (CollectIndexSetStatistics) {
267183593Sjkoshy    IndexSet::print_statistics();
268183593Sjkoshy  }
269184914Sjkoshy#endif // ASSERT
270183593Sjkoshy#else
271183593Sjkoshy#ifdef INCLUDE_JVMCI
272183593Sjkoshy#ifndef COMPILER1
273184892Sjkoshy  if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
274183593Sjkoshy    FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
275184892Sjkoshy    Deoptimization::print_statistics();
276183593Sjkoshy    SharedRuntime::print_statistics();
277183593Sjkoshy  }
278183593Sjkoshy#endif
279183593Sjkoshy#endif
280184914Sjkoshy#endif
281184892Sjkoshy
282183593Sjkoshy  if (PrintAOTStatistics) {
283184892Sjkoshy    AOTLoader::print_statistics();
284183593Sjkoshy  }
285183593Sjkoshy
286183593Sjkoshy  if (PrintNMethodStatistics) {
287183593Sjkoshy    nmethod::print_statistics();
288183593Sjkoshy  }
289183593Sjkoshy  if (CountCompiledCalls) {
290183593Sjkoshy    print_method_invocation_histogram();
291183593Sjkoshy  }
292183593Sjkoshy
293183593Sjkoshy  print_method_profiling_data();
294183593Sjkoshy
295183593Sjkoshy  if (TimeCompilationPolicy) {
296183593Sjkoshy    CompilationPolicy::policy()->print_time();
297183593Sjkoshy  }
298183593Sjkoshy  if (TimeOopMap) {
299183593Sjkoshy    GenerateOopMap::print_time();
300183593Sjkoshy  }
301183593Sjkoshy  if (ProfilerCheckIntervals) {
302183593Sjkoshy    PeriodicTask::print_intervals();
303184914Sjkoshy  }
304183593Sjkoshy  if (PrintSymbolTableSizeHistogram) {
305183593Sjkoshy    SymbolTable::print_histogram();
306184914Sjkoshy  }
307183593Sjkoshy  if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
308183593Sjkoshy    BytecodeCounter::print();
309183593Sjkoshy  }
310183593Sjkoshy  if (PrintBytecodePairHistogram) {
311184914Sjkoshy    BytecodePairHistogram::print();
312183593Sjkoshy  }
313183593Sjkoshy
314183593Sjkoshy  if (PrintCodeCache) {
315183593Sjkoshy    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
316184914Sjkoshy    CodeCache::print();
317183593Sjkoshy  }
318183593Sjkoshy
319183593Sjkoshy  if (PrintMethodFlushingStatistics) {
320183593Sjkoshy    NMethodSweeper::print();
321184914Sjkoshy  }
322183593Sjkoshy
323183593Sjkoshy  if (PrintCodeCache2) {
324184914Sjkoshy    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
325183593Sjkoshy    CodeCache::print_internals();
326183593Sjkoshy  }
327183593Sjkoshy
328183593Sjkoshy  if (PrintVtableStats) {
329183593Sjkoshy    klassVtable::print_statistics();
330183593Sjkoshy    klassItable::print_statistics();
331183593Sjkoshy  }
332183593Sjkoshy  if (VerifyOops) {
333183593Sjkoshy    tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
334183593Sjkoshy  }
335183593Sjkoshy
336183593Sjkoshy  print_bytecode_count();
337183593Sjkoshy  if (PrintMallocStatistics) {
338183593Sjkoshy    tty->print("allocation stats: ");
339183593Sjkoshy    alloc_stats.print();
340183593Sjkoshy    tty->cr();
341183593Sjkoshy  }
342183593Sjkoshy
343183593Sjkoshy  if (PrintSystemDictionaryAtExit) {
344183593Sjkoshy    SystemDictionary::print();
345183593Sjkoshy  }
346183593Sjkoshy
347183593Sjkoshy  if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
348183593Sjkoshy    Method::print_touched_methods(tty);
349183593Sjkoshy  }
350183593Sjkoshy
351183593Sjkoshy  if (PrintBiasedLockingStatistics) {
352183593Sjkoshy    BiasedLocking::print_counters();
353183593Sjkoshy  }
354183593Sjkoshy
355183593Sjkoshy  // Native memory tracking data
356183593Sjkoshy  if (PrintNMTStatistics) {
357183593Sjkoshy    MemTracker::final_report(tty);
358183593Sjkoshy  }
359183593Sjkoshy}
360183593Sjkoshy
361183593Sjkoshy#else // PRODUCT MODE STATISTICS
362183593Sjkoshy
363183593Sjkoshyvoid print_statistics() {
364183593Sjkoshy
365183593Sjkoshy  if (PrintMethodData) {
366183593Sjkoshy    print_method_profiling_data();
367183593Sjkoshy  }
368183593Sjkoshy
369183593Sjkoshy  if (CITime) {
370183593Sjkoshy    CompileBroker::print_times();
371183593Sjkoshy  }
372183593Sjkoshy
373183593Sjkoshy  if (PrintCodeCache) {
374196449Sjkoshy    MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
375183593Sjkoshy    CodeCache::print();
376183593Sjkoshy  }
377183593Sjkoshy
378183593Sjkoshy  if (PrintMethodFlushingStatistics) {
379183593Sjkoshy    NMethodSweeper::print();
380183593Sjkoshy  }
381183593Sjkoshy
382183593Sjkoshy#ifdef COMPILER2
383183593Sjkoshy  if (PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
384183593Sjkoshy    OptoRuntime::print_named_counters();
385183593Sjkoshy  }
386183593Sjkoshy#endif
387183593Sjkoshy  if (PrintBiasedLockingStatistics) {
388183593Sjkoshy    BiasedLocking::print_counters();
389183593Sjkoshy  }
390183593Sjkoshy
391183593Sjkoshy  // Native memory tracking data
392183593Sjkoshy  if (PrintNMTStatistics) {
393183593Sjkoshy    MemTracker::final_report(tty);
394183593Sjkoshy  }
395183593Sjkoshy
396183593Sjkoshy  if (LogTouchedMethods && PrintTouchedMethodsAtExit) {
397183593Sjkoshy    Method::print_touched_methods(tty);
398183593Sjkoshy  }
399183593Sjkoshy}
400183593Sjkoshy
401183593Sjkoshy#endif
402183593Sjkoshy
403183593Sjkoshy// Note: before_exit() can be executed only once, if more than one threads
404183593Sjkoshy//       are trying to shutdown the VM at the same time, only one thread
405183593Sjkoshy//       can run before_exit() and all other threads must wait.
406183593Sjkoshyvoid before_exit(JavaThread* thread) {
407183593Sjkoshy  #define BEFORE_EXIT_NOT_RUN 0
408183593Sjkoshy  #define BEFORE_EXIT_RUNNING 1
409183593Sjkoshy  #define BEFORE_EXIT_DONE    2
410183593Sjkoshy  static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
411183593Sjkoshy
412183593Sjkoshy  // Note: don't use a Mutex to guard the entire before_exit(), as
413183593Sjkoshy  // JVMTI post_thread_end_event and post_vm_death_event will run native code.
414183593Sjkoshy  // A CAS or OSMutex would work just fine but then we need to manipulate
415183593Sjkoshy  // thread state for Safepoint. Here we use Monitor wait() and notify_all()
416183593Sjkoshy  // for synchronization.
417183593Sjkoshy  { MutexLocker ml(BeforeExit_lock);
418183593Sjkoshy    switch (_before_exit_status) {
419183593Sjkoshy    case BEFORE_EXIT_NOT_RUN:
420183593Sjkoshy      _before_exit_status = BEFORE_EXIT_RUNNING;
421183593Sjkoshy      break;
422183593Sjkoshy    case BEFORE_EXIT_RUNNING:
423183593Sjkoshy      while (_before_exit_status == BEFORE_EXIT_RUNNING) {
424183593Sjkoshy        BeforeExit_lock->wait();
425183593Sjkoshy      }
426183593Sjkoshy      assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
427183593Sjkoshy      return;
428183593Sjkoshy    case BEFORE_EXIT_DONE:
429183593Sjkoshy      // need block to avoid SS compiler bug
430183593Sjkoshy      {
431183593Sjkoshy        return;
432183593Sjkoshy      }
433183593Sjkoshy    }
434183593Sjkoshy  }
435196449Sjkoshy
436183593Sjkoshy#if INCLUDE_JVMCI
437183593Sjkoshy  // We are not using CATCH here because we want the exit to continue normally.
438183593Sjkoshy  Thread* THREAD = thread;
439183593Sjkoshy  JVMCIRuntime::shutdown(THREAD);
440183593Sjkoshy  if (HAS_PENDING_EXCEPTION) {
441183593Sjkoshy    HandleMark hm(THREAD);
442183593Sjkoshy    Handle exception(THREAD, PENDING_EXCEPTION);
443183593Sjkoshy    CLEAR_PENDING_EXCEPTION;
444183593Sjkoshy    java_lang_Throwable::java_printStackTrace(exception, THREAD);
445183593Sjkoshy  }
446183593Sjkoshy#endif
447183593Sjkoshy
448183593Sjkoshy  // Hang forever on exit if we're reporting an error.
449183593Sjkoshy  if (ShowMessageBoxOnError && VMError::is_error_reported()) {
450183593Sjkoshy    os::infinite_sleep();
451196449Sjkoshy  }
452183593Sjkoshy
453183593Sjkoshy  EventThreadEnd event;
454183593Sjkoshy  if (event.should_commit()) {
455183593Sjkoshy    event.set_thread(THREAD_TRACE_ID(thread));
456183593Sjkoshy    event.commit();
457183593Sjkoshy  }
458183593Sjkoshy
459183593Sjkoshy  TRACE_VM_EXIT();
460183593Sjkoshy
461183593Sjkoshy  // Stop the WatcherThread. We do this before disenrolling various
462184892Sjkoshy  // PeriodicTasks to reduce the likelihood of races.
463183593Sjkoshy  if (PeriodicTask::num_tasks() > 0) {
464184892Sjkoshy    WatcherThread::stop();
465183593Sjkoshy  }
466183593Sjkoshy
467184892Sjkoshy  // Print statistics gathered (profiling ...)
468183593Sjkoshy  if (Arguments::has_profile()) {
469184892Sjkoshy    FlatProfiler::disengage();
470183593Sjkoshy    FlatProfiler::print(10);
471183593Sjkoshy  }
472183593Sjkoshy
473183593Sjkoshy  // shut down the StatSampler task
474183593Sjkoshy  StatSampler::disengage();
475183593Sjkoshy  StatSampler::destroy();
476183593Sjkoshy
477183593Sjkoshy  // Stop concurrent GC threads
478183593Sjkoshy  Universe::heap()->stop();
479183593Sjkoshy
480183593Sjkoshy  // Print GC/heap related information.
481183593Sjkoshy  Log(gc, heap, exit) log;
482183593Sjkoshy  if (log.is_info()) {
483183593Sjkoshy    ResourceMark rm;
484183593Sjkoshy    Universe::print_on(log.info_stream());
485184914Sjkoshy    if (log.is_trace()) {
486183593Sjkoshy      ClassLoaderDataGraph::dump_on(log.trace_stream());
487183593Sjkoshy    }
488183593Sjkoshy  }
489183593Sjkoshy  AdaptiveSizePolicyOutput::print();
490183593Sjkoshy
491183593Sjkoshy  if (PrintBytecodeHistogram) {
492183593Sjkoshy    BytecodeHistogram::print();
493183593Sjkoshy  }
494183593Sjkoshy
495183593Sjkoshy  if (JvmtiExport::should_post_thread_life()) {
496183593Sjkoshy    JvmtiExport::post_thread_end(thread);
497183593Sjkoshy  }
498183593Sjkoshy
499183593Sjkoshy  // Always call even when there are not JVMTI environments yet, since environments
500183593Sjkoshy  // may be attached late and JVMTI must track phases of VM execution
501183593Sjkoshy  JvmtiExport::post_vm_death();
502183593Sjkoshy  Threads::shutdown_vm_agents();
503183593Sjkoshy
504183593Sjkoshy  // Terminate the signal thread
505183593Sjkoshy  // Note: we don't wait until it actually dies.
506183593Sjkoshy  os::terminate_signal_thread();
507183593Sjkoshy
508183593Sjkoshy  print_statistics();
509183593Sjkoshy  Universe::heap()->print_tracing_info();
510183593Sjkoshy
511183593Sjkoshy  { MutexLocker ml(BeforeExit_lock);
512183593Sjkoshy    _before_exit_status = BEFORE_EXIT_DONE;
513183593Sjkoshy    BeforeExit_lock->notify_all();
514183593Sjkoshy  }
515183593Sjkoshy
516183593Sjkoshy  if (VerifyStringTableAtExit) {
517183593Sjkoshy    int fail_cnt = 0;
518183593Sjkoshy    {
519183593Sjkoshy      MutexLocker ml(StringTable_lock);
520183593Sjkoshy      fail_cnt = StringTable::verify_and_compare_entries();
521183593Sjkoshy    }
522183593Sjkoshy
523183593Sjkoshy    if (fail_cnt != 0) {
524183593Sjkoshy      tty->print_cr("ERROR: fail_cnt=%d", fail_cnt);
525183593Sjkoshy      guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
526183593Sjkoshy    }
527183593Sjkoshy  }
528183593Sjkoshy
529183593Sjkoshy  #undef BEFORE_EXIT_NOT_RUN
530183593Sjkoshy  #undef BEFORE_EXIT_RUNNING
531183593Sjkoshy  #undef BEFORE_EXIT_DONE
532183593Sjkoshy}
533183593Sjkoshy
534184914Sjkoshyvoid vm_exit(int code) {
535183593Sjkoshy  Thread* thread =
536183593Sjkoshy      ThreadLocalStorage::is_initialized() ? Thread::current_or_null() : NULL;
537183593Sjkoshy  if (thread == NULL) {
538183593Sjkoshy    // very early initialization failure -- just exit
539183593Sjkoshy    vm_direct_exit(code);
540183593Sjkoshy  }
541183593Sjkoshy
542183593Sjkoshy  if (VMThread::vm_thread() != NULL) {
543183593Sjkoshy    // Fire off a VM_Exit operation to bring VM to a safepoint and exit
544183593Sjkoshy    VM_Exit op(code);
545183593Sjkoshy    if (thread->is_Java_thread())
546183593Sjkoshy      ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
547183593Sjkoshy    VMThread::execute(&op);
548183593Sjkoshy    // should never reach here; but in case something wrong with VM Thread.
549183593Sjkoshy    vm_direct_exit(code);
550183593Sjkoshy  } else {
551183593Sjkoshy    // VM thread is gone, just exit
552183593Sjkoshy    vm_direct_exit(code);
553183593Sjkoshy  }
554183593Sjkoshy  ShouldNotReachHere();
555183593Sjkoshy}
556183593Sjkoshy
557183593Sjkoshyvoid notify_vm_shutdown() {
558183593Sjkoshy  // For now, just a dtrace probe.
559183593Sjkoshy  HOTSPOT_VM_SHUTDOWN();
560185364Sjkoshy  HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
561183593Sjkoshy}
562184914Sjkoshy
563183593Sjkoshyvoid vm_direct_exit(int code) {
564183593Sjkoshy  notify_vm_shutdown();
565183593Sjkoshy  os::wait_for_keypress_at_exit();
566183593Sjkoshy  os::exit(code);
567183593Sjkoshy}
568183593Sjkoshy
569183593Sjkoshyvoid vm_perform_shutdown_actions() {
570183593Sjkoshy  // Warning: do not call 'exit_globals()' here. All threads are still running.
571183593Sjkoshy  // Calling 'exit_globals()' will disable thread-local-storage and cause all
572183593Sjkoshy  // kinds of assertions to trigger in debug mode.
573183593Sjkoshy  if (is_init_completed()) {
574183593Sjkoshy    Thread* thread = Thread::current_or_null();
575183593Sjkoshy    if (thread != NULL && thread->is_Java_thread()) {
576183593Sjkoshy      // We are leaving the VM, set state to native (in case any OS exit
577183593Sjkoshy      // handlers call back to the VM)
578184914Sjkoshy      JavaThread* jt = (JavaThread*)thread;
579196437Sjkoshy      // Must always be walkable or have no last_Java_frame when in
580183593Sjkoshy      // thread_in_native
581183593Sjkoshy      jt->frame_anchor()->make_walkable(jt);
582183593Sjkoshy      jt->set_thread_state(_thread_in_native);
583183593Sjkoshy    }
584183593Sjkoshy  }
585183593Sjkoshy  notify_vm_shutdown();
586183593Sjkoshy}
587184914Sjkoshy
588183593Sjkoshyvoid vm_shutdown()
589183593Sjkoshy{
590183593Sjkoshy  vm_perform_shutdown_actions();
591183593Sjkoshy  os::wait_for_keypress_at_exit();
592184914Sjkoshy  os::shutdown();
593183593Sjkoshy}
594183593Sjkoshy
595183593Sjkoshyvoid vm_abort(bool dump_core) {
596184914Sjkoshy  vm_perform_shutdown_actions();
597183593Sjkoshy  os::wait_for_keypress_at_exit();
598183593Sjkoshy
599183593Sjkoshy  // Flush stdout and stderr before abort.
600184892Sjkoshy  fflush(stdout);
601183593Sjkoshy  fflush(stderr);
602184892Sjkoshy
603183593Sjkoshy  os::abort(dump_core);
604183593Sjkoshy  ShouldNotReachHere();
605183593Sjkoshy}
606183593Sjkoshy
607184914Sjkoshyvoid vm_notify_during_shutdown(const char* error, const char* message) {
608183593Sjkoshy  if (error != NULL) {
609183593Sjkoshy    tty->print_cr("Error occurred during initialization of VM");
610183593Sjkoshy    tty->print("%s", error);
611184914Sjkoshy    if (message != NULL) {
612183593Sjkoshy      tty->print_cr(": %s", message);
613183593Sjkoshy    }
614183593Sjkoshy    else {
615183593Sjkoshy      tty->cr();
616183593Sjkoshy    }
617183593Sjkoshy  }
618183593Sjkoshy  if (ShowMessageBoxOnError && WizardMode) {
619183593Sjkoshy    fatal("Error occurred during initialization of VM");
620183593Sjkoshy  }
621183593Sjkoshy}
622183593Sjkoshy
623183593Sjkoshyvoid vm_exit_during_initialization() {
624183593Sjkoshy  vm_notify_during_shutdown(NULL, NULL);
625183593Sjkoshy
626183593Sjkoshy  // Failure during initialization, we don't want to dump core
627183593Sjkoshy  vm_abort(false);
628184914Sjkoshy}
629183593Sjkoshy
630183593Sjkoshyvoid vm_exit_during_initialization(Handle exception) {
631183593Sjkoshy  tty->print_cr("Error occurred during initialization of VM");
632183593Sjkoshy  // If there are exceptions on this thread it must be cleared
633183593Sjkoshy  // first and here. Any future calls to EXCEPTION_MARK requires
634183593Sjkoshy  // that no pending exceptions exist.
635183593Sjkoshy  Thread *THREAD = Thread::current(); // can't be NULL
636183593Sjkoshy  if (HAS_PENDING_EXCEPTION) {
637183593Sjkoshy    CLEAR_PENDING_EXCEPTION;
638183593Sjkoshy  }
639183593Sjkoshy  java_lang_Throwable::print_stack_trace(exception, tty);
640183593Sjkoshy  tty->cr();
641183593Sjkoshy  vm_notify_during_shutdown(NULL, NULL);
642184914Sjkoshy
643183593Sjkoshy  // Failure during initialization, we don't want to dump core
644183593Sjkoshy  vm_abort(false);
645183593Sjkoshy}
646183593Sjkoshy
647183593Sjkoshyvoid vm_exit_during_initialization(Symbol* ex, const char* message) {
648183593Sjkoshy  ResourceMark rm;
649183593Sjkoshy  vm_notify_during_shutdown(ex->as_C_string(), message);
650183593Sjkoshy
651183593Sjkoshy  // Failure during initialization, we don't want to dump core
652184914Sjkoshy  vm_abort(false);
653183593Sjkoshy}
654183593Sjkoshy
655184914Sjkoshyvoid vm_exit_during_initialization(const char* error, const char* message) {
656183593Sjkoshy  vm_notify_during_shutdown(error, message);
657183593Sjkoshy
658184914Sjkoshy  // Failure during initialization, we don't want to dump core
659183593Sjkoshy  vm_abort(false);
660184914Sjkoshy}
661184914Sjkoshy
662183593Sjkoshyvoid vm_shutdown_during_initialization(const char* error, const char* message) {
663183593Sjkoshy  vm_notify_during_shutdown(error, message);
664183593Sjkoshy  vm_shutdown();
665184914Sjkoshy}
666183593Sjkoshy
667183593SjkoshyJDK_Version JDK_Version::_current;
668183593Sjkoshyconst char* JDK_Version::_runtime_name;
669183593Sjkoshyconst char* JDK_Version::_runtime_version;
670183593Sjkoshy
671183593Sjkoshyvoid JDK_Version::initialize() {
672183593Sjkoshy  jdk_version_info info;
673183593Sjkoshy  assert(!_current.is_valid(), "Don't initialize twice");
674184914Sjkoshy
675183593Sjkoshy  void *lib_handle = os::native_java_library();
676183593Sjkoshy  jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
677184914Sjkoshy     os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
678183593Sjkoshy
679183593Sjkoshy  assert(func != NULL, "Support for JDK 1.5 or older has been removed after JEP-223");
680183593Sjkoshy
681183593Sjkoshy  (*func)(&info, sizeof(info));
682183593Sjkoshy
683183593Sjkoshy  int major = JDK_VERSION_MAJOR(info.jdk_version);
684183593Sjkoshy  int minor = JDK_VERSION_MINOR(info.jdk_version);
685183593Sjkoshy  int security = JDK_VERSION_SECURITY(info.jdk_version);
686183593Sjkoshy  int build = JDK_VERSION_BUILD(info.jdk_version);
687183593Sjkoshy
688183593Sjkoshy  // Incompatible with pre-4243978 JDK.
689183593Sjkoshy  if (info.pending_list_uses_discovered_field == 0) {
690183593Sjkoshy    vm_exit_during_initialization(
691183593Sjkoshy      "Incompatible JDK is not using Reference.discovered field for pending list");
692183593Sjkoshy  }
693183593Sjkoshy  _current = JDK_Version(major, minor, security, info.patch_version, build,
694183593Sjkoshy                         info.thread_park_blocker == 1,
695183593Sjkoshy                         info.post_vm_init_hook_enabled == 1);
696196449Sjkoshy}
697183593Sjkoshy
698183593Sjkoshyvoid JDK_Version_init() {
699183593Sjkoshy  JDK_Version::initialize();
700183593Sjkoshy}
701183593Sjkoshy
702183593Sjkoshystatic int64_t encode_jdk_version(const JDK_Version& v) {
703183593Sjkoshy  return
704183593Sjkoshy    ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
705183593Sjkoshy    ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
706183593Sjkoshy    ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
707183593Sjkoshy    ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
708183593Sjkoshy    ((int64_t)v.build_number()           << (BitsPerByte * 0));
709183593Sjkoshy}
710183593Sjkoshy
711183593Sjkoshyint JDK_Version::compare(const JDK_Version& other) const {
712183593Sjkoshy  assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
713183593Sjkoshy  uint64_t e = encode_jdk_version(*this);
714183593Sjkoshy  uint64_t o = encode_jdk_version(other);
715183593Sjkoshy  return (e > o) ? 1 : ((e == o) ? 0 : -1);
716183593Sjkoshy}
717183593Sjkoshy
718183593Sjkoshyvoid JDK_Version::to_string(char* buffer, size_t buflen) const {
719183593Sjkoshy  assert(buffer && buflen > 0, "call with useful buffer");
720183593Sjkoshy  size_t index = 0;
721183593Sjkoshy
722183593Sjkoshy  if (!is_valid()) {
723183593Sjkoshy    jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
724183593Sjkoshy  } else {
725183593Sjkoshy    int rc = jio_snprintf(
726183593Sjkoshy        &buffer[index], buflen - index, "%d.%d", _major, _minor);
727183593Sjkoshy    if (rc == -1) return;
728183593Sjkoshy    index += rc;
729183593Sjkoshy    if (_security > 0) {
730183593Sjkoshy      rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
731183593Sjkoshy      if (rc == -1) return;
732183593Sjkoshy      index += rc;
733183593Sjkoshy    }
734183593Sjkoshy    if (_patch > 0) {
735183593Sjkoshy      rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _patch);
736183593Sjkoshy      if (rc == -1) return;
737183593Sjkoshy      index += rc;
738183593Sjkoshy    }
739183593Sjkoshy    if (_build > 0) {
740183593Sjkoshy      rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
741183593Sjkoshy      if (rc == -1) return;
742183593Sjkoshy      index += rc;
743183593Sjkoshy    }
744183593Sjkoshy  }
745183593Sjkoshy}
746183593Sjkoshy