1 /*
  2  * Copyright (c) 1997, 2024, 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 "cds/aotLinkedClassBulkLoader.hpp"
 27 #include "cds/cds_globals.hpp"
 28 #include "cds/cdsConfig.hpp"
 29 #include "cds/classListWriter.hpp"
 30 #include "cds/dynamicArchive.hpp"
 31 #include "cds/methodProfiler.hpp"
 32 #include "cds/metaspaceShared.hpp"
 33 #include "classfile/classLoader.hpp"
 34 #include "classfile/classLoaderDataGraph.hpp"
 35 #include "classfile/javaClasses.hpp"
 36 #include "classfile/stringTable.hpp"
 37 #include "classfile/symbolTable.hpp"
 38 #include "classfile/systemDictionary.hpp"
 39 #include "code/codeCache.hpp"
 40 #include "code/SCCache.hpp"
 41 #include "compiler/compilationMemoryStatistic.hpp"
 42 #include "compiler/compilationPolicy.hpp"
 43 #include "compiler/compileBroker.hpp"
 44 #include "compiler/compilerOracle.hpp"
 45 #include "gc/shared/collectedHeap.hpp"
 46 #include "gc/shared/stringdedup/stringDedup.hpp"
 47 #include "interpreter/bytecodeHistogram.hpp"
 48 #include "interpreter/interpreterRuntime.hpp"
 49 #include "jfr/jfrEvents.hpp"
 50 #include "jfr/support/jfrThreadId.hpp"
 51 #include "jvm.h"
 52 #include "logging/log.hpp"
 53 #include "logging/logStream.hpp"
 54 #include "memory/metaspaceUtils.hpp"
 55 #include "memory/oopFactory.hpp"
 56 #include "memory/resourceArea.hpp"
 57 #include "memory/universe.hpp"
 58 #include "nmt/memMapPrinter.hpp"
 59 #include "nmt/memTracker.hpp"
 60 #include "oops/constantPool.hpp"
 61 #include "oops/generateOopMap.hpp"
 62 #include "oops/instanceKlass.hpp"
 63 #include "oops/instanceOop.hpp"
 64 #include "oops/klassVtable.hpp"
 65 #include "oops/method.hpp"
 66 #include "oops/objArrayOop.hpp"
 67 #include "oops/oop.inline.hpp"
 68 #include "oops/symbol.hpp"
 69 #include "oops/trainingData.hpp"
 70 #include "prims/jvmtiAgentList.hpp"
 71 #include "prims/jvmtiExport.hpp"
 72 #include "prims/methodHandles.hpp"
 73 #include "runtime/continuation.hpp"
 74 #include "runtime/deoptimization.hpp"
 75 #include "runtime/flags/flagSetting.hpp"
 76 #include "runtime/globals_extension.hpp"
 77 #include "runtime/handles.inline.hpp"
 78 #include "runtime/init.hpp"
 79 #include "runtime/interfaceSupport.inline.hpp"
 80 #include "runtime/java.hpp"
 81 #include "runtime/javaThread.hpp"
 82 #include "runtime/sharedRuntime.hpp"
 83 #include "runtime/statSampler.hpp"
 84 #include "runtime/stubRoutines.hpp"
 85 #include "runtime/task.hpp"
 86 #include "runtime/threads.hpp"
 87 #include "runtime/timer.hpp"
 88 #include "runtime/trimNativeHeap.hpp"
 89 #include "runtime/vmOperations.hpp"
 90 #include "runtime/vmThread.hpp"
 91 #include "runtime/vm_version.hpp"
 92 #include "sanitizers/leak.hpp"
 93 #include "utilities/dtrace.hpp"
 94 #include "utilities/events.hpp"
 95 #include "utilities/globalDefinitions.hpp"
 96 #include "utilities/macros.hpp"
 97 #include "utilities/vmError.hpp"
 98 #ifdef COMPILER1
 99 #include "c1/c1_Compiler.hpp"
100 #include "c1/c1_Runtime1.hpp"
101 #endif
102 #ifdef COMPILER2
103 #include "code/compiledIC.hpp"
104 #include "opto/compile.hpp"
105 #include "opto/indexSet.hpp"
106 #include "opto/runtime.hpp"
107 #endif
108 #if INCLUDE_JFR
109 #include "jfr/jfr.hpp"
110 #endif
111 #if INCLUDE_JVMCI
112 #include "jvmci/jvmci.hpp"
113 #endif
114 
115 GrowableArray<Method*>* collected_profiled_methods;
116 
117 static int compare_methods(Method** a, Method** b) {
118   // compiled_invocation_count() returns int64_t, forcing the entire expression
119   // to be evaluated as int64_t. Overflow is not an issue.
120   int64_t diff = (((*b)->invocation_count() + (*b)->compiled_invocation_count())
121                 - ((*a)->invocation_count() + (*a)->compiled_invocation_count()));
122   return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
123 }
124 
125 static void collect_profiled_methods(Method* m) {
126   Thread* thread = Thread::current();
127   methodHandle mh(thread, m);
128   if ((m->method_data() != nullptr) &&
129       (PrintMethodData || CompilerOracle::should_print(mh))) {
130     collected_profiled_methods->push(m);
131   }
132 }
133 
134 static void print_method_profiling_data() {
135   if ((ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) &&
136      (PrintMethodData || CompilerOracle::should_print_methods())) {
137     ResourceMark rm;
138     collected_profiled_methods = new GrowableArray<Method*>(1024);
139     SystemDictionary::methods_do(collect_profiled_methods);
140     collected_profiled_methods->sort(&compare_methods);
141 
142     int count = collected_profiled_methods->length();
143     int total_size = 0;
144     if (count > 0) {
145       for (int index = 0; index < count; index++) {
146         Method* m = collected_profiled_methods->at(index);
147 
148         // Instead of taking tty lock, we collect all lines into a string stream
149         // and then print them all at once.
150         ResourceMark rm2;
151         stringStream ss;
152 
153         ss.print_cr("------------------------------------------------------------------------");
154         m->print_invocation_count(&ss);
155         ss.print_cr("  mdo size: %d bytes", m->method_data()->size_in_bytes());
156         ss.cr();
157         // Dump data on parameters if any
158         if (m->method_data() != nullptr && m->method_data()->parameters_type_data() != nullptr) {
159           ss.fill_to(2);
160           m->method_data()->parameters_type_data()->print_data_on(&ss);
161         }
162         m->print_codes_on(&ss);
163         tty->print("%s", ss.as_string()); // print all at once
164         total_size += m->method_data()->size_in_bytes();
165       }
166       tty->print_cr("------------------------------------------------------------------------");
167       tty->print_cr("Total MDO size: %d bytes", total_size);
168     }
169   }
170 }
171 
172 void perf_jvm_print_on(outputStream* st);
173 
174 void log_vm_init_stats(bool use_tty) {
175   LogStreamHandle(Info, init) log;
176   if (log.is_enabled()) {
177     outputStream* out = use_tty ? tty : &log;
178     SharedRuntime::print_counters_on(out);
179     LogStreamHandle(Info, perf, class, link) log2;
180     outputStream* out2 = use_tty ? tty : &log2;
181     ClassLoader::print_counters(out2);
182     AOTLinkedClassBulkLoader::print_counters();
183     out->cr();
184     // FIXME: intermittent crashes
185 //    if (CountBytecodesPerThread) {
186 //      log.print_cr("Thread info:");
187 //      class PrintThreadInfo : public ThreadClosure {
188 //        outputStream* _st;
189 //      public:
190 //        PrintThreadInfo(outputStream* st) : ThreadClosure(), _st(st) {}
191 //        void do_thread(Thread* thread) {
192 //          JavaThread* jt = JavaThread::cast(thread);
193 //          if (jt->bc_counter_value() > 0) {
194 //            _st->print_cr("  Thread " INTPTR_FORMAT ": %ld bytecodes executed (clinit: %ld)",
195 //                          p2i(jt), /*jt->name(),*/ jt->bc_counter_value(), jt->clinit_bc_counter_value());
196 //          }
197 //        }
198 //      };
199 //      PrintThreadInfo cl(&log);
200 //      Threads::java_threads_do(&cl);
201 //    }
202 //    log.cr();
203     out->print_cr("Deoptimization events: ");
204     Deoptimization::print_statistics_on(out);
205     out->cr();
206 
207     out->print("Compilation statistics: ");
208     CompileBroker::print_statistics_on(out);
209     out->cr();
210 
211     {
212       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
213       out->print("Code cache statistics: ");
214       CodeCache::print_nmethod_statistics_on(out);
215       out->cr();
216     }
217 
218     if (SCCache::is_on_for_read()) {
219       out->print_cr("Startup Code Cache: ");
220       SCCache::print_statistics_on(out);
221       out->cr();
222       SCCache::print_timers_on(out);
223     }
224 
225     VMThread::print_counters_on(out);
226     out->cr();
227     MutexLockerImpl::print_counters_on(out);
228     out->cr();
229     out->print("Runtime events for thread \"main\"");
230     if (ProfileRuntimeCalls) {
231       out->print_cr(" (%d nested events):", ProfileVMCallContext::nested_runtime_calls_count());
232 
233       InterpreterRuntime::print_counters_on(out);
234 #ifdef COMPILER1
235       Runtime1::print_counters_on(out);
236 #endif
237 #ifdef COMPILER2
238       OptoRuntime::print_counters_on(out);
239       Deoptimization::print_counters_on(out);
240 #endif
241     } else {
242       out->print_cr(": no info (%s is disabled)", (UsePerfData ? "ProfileRuntimeCalls" : "UsePerfData"));
243     }
244     out->cr();
245     perf_jvm_print_on(out);
246     out->cr();
247     MethodHandles::print_counters_on(out);
248   }
249 }
250 
251 void print_bytecode_count() {
252   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
253     tty->print_cr("[BytecodeCounter::counter_value = " JLONG_FORMAT "]", BytecodeCounter::counter_value());
254   }
255 }
256 
257 #ifndef PRODUCT
258 
259 // Statistics printing (method invocation histogram)
260 
261 GrowableArray<Method*>* collected_invoked_methods;
262 
263 static void collect_invoked_methods(Method* m) {
264   if (m->invocation_count() + m->compiled_invocation_count() >= 1) {
265     collected_invoked_methods->push(m);
266   }
267 }
268 
269 
270 // Invocation count accumulators should be unsigned long to shift the
271 // overflow border. Longer-running workloads tend to create invocation
272 // counts which already overflow 32-bit counters for individual methods.
273 static void print_method_invocation_histogram() {
274   ResourceMark rm;
275   collected_invoked_methods = new GrowableArray<Method*>(1024);
276   SystemDictionary::methods_do(collect_invoked_methods);
277   collected_invoked_methods->sort(&compare_methods);
278   //
279   tty->cr();
280   tty->print_cr("Histogram Over Method Invocation Counters (cutoff = " INTX_FORMAT "):", MethodHistogramCutoff);
281   tty->cr();
282   tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
283   uint64_t total        = 0,
284            int_total    = 0,
285            comp_total   = 0,
286            special_total= 0,
287            static_total = 0,
288            final_total  = 0,
289            synch_total  = 0,
290            native_total = 0,
291            access_total = 0;
292   for (int index = 0; index < collected_invoked_methods->length(); index++) {
293     // Counter values returned from getter methods are signed int.
294     // To shift the overflow border by a factor of two, we interpret
295     // them here as unsigned long. A counter can't be negative anyway.
296     Method* m = collected_invoked_methods->at(index);
297     uint64_t iic = (uint64_t)m->invocation_count();
298     uint64_t cic = (uint64_t)m->compiled_invocation_count();
299     if ((iic + cic) >= (uint64_t)MethodHistogramCutoff) m->print_invocation_count(tty);
300     int_total  += iic;
301     comp_total += cic;
302     if (m->is_final())        final_total  += iic + cic;
303     if (m->is_static())       static_total += iic + cic;
304     if (m->is_synchronized()) synch_total  += iic + cic;
305     if (m->is_native())       native_total += iic + cic;
306     if (m->is_accessor())     access_total += iic + cic;
307   }
308   tty->cr();
309   total = int_total + comp_total;
310   special_total = final_total + static_total +synch_total + native_total + access_total;
311   tty->print_cr("Invocations summary for %d methods:", collected_invoked_methods->length());
312   double total_div = (double)total;
313   tty->print_cr("\t" UINT64_FORMAT_W(12) " (100%%)  total",           total);
314   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- interpreted", int_total,     100.0 * (double)int_total    / total_div);
315   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- compiled",    comp_total,    100.0 * (double)comp_total   / total_div);
316   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%) |- special methods (interpreted and compiled)",
317                                                                          special_total, 100.0 * (double)special_total/ total_div);
318   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- synchronized",synch_total,   100.0 * (double)synch_total  / total_div);
319   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- final",       final_total,   100.0 * (double)final_total  / total_div);
320   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- static",      static_total,  100.0 * (double)static_total / total_div);
321   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- native",      native_total,  100.0 * (double)native_total / total_div);
322   tty->print_cr("\t" UINT64_FORMAT_W(12) " (%4.1f%%)    |- accessor",    access_total,  100.0 * (double)access_total / total_div);
323   tty->cr();
324   SharedRuntime::print_call_statistics_on(tty);
325 }
326 
327 #else
328 
329 static void print_method_invocation_histogram() {}
330 
331 #endif // PRODUCT
332 
333 
334 // General statistics printing (profiling ...)
335 void print_statistics() {
336 #if INCLUDE_CDS
337   if (ReplayTraining && PrintTrainingInfo) {
338     TrainingData::print_archived_training_data_on(tty);
339   }
340 #endif
341   if (CITime) {
342     CompileBroker::print_times();
343   }
344 
345 #ifdef COMPILER1
346   if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
347     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
348     Runtime1::print_statistics_on(tty);
349     SharedRuntime::print_statistics();
350   }
351 #endif /* COMPILER1 */
352 
353 #ifdef COMPILER2
354   if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
355     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
356     Compile::print_statistics();
357     Deoptimization::print_statistics();
358 #ifndef COMPILER1
359     SharedRuntime::print_statistics();
360 #endif //COMPILER1
361   }
362 
363   if (PrintLockStatistics) {
364     OptoRuntime::print_named_counters();
365   }
366 #ifdef ASSERT
367   if (CollectIndexSetStatistics) {
368     IndexSet::print_statistics();
369   }
370 #endif // ASSERT
371 #else // COMPILER2
372 #if INCLUDE_JVMCI
373 #ifndef COMPILER1
374   if ((TraceDeoptimization || LogVMOutput || LogCompilation) && UseCompiler) {
375     FlagSetting fs(DisplayVMOutput, DisplayVMOutput && TraceDeoptimization);
376     Deoptimization::print_statistics();
377     SharedRuntime::print_statistics();
378   }
379 #endif // COMPILER1
380 #endif // INCLUDE_JVMCI
381 #endif // COMPILER2
382 
383   if (PrintNMethodStatistics) {
384     nmethod::print_statistics();
385   }
386   if (CountCompiledCalls) {
387     print_method_invocation_histogram();
388   }
389 
390   print_method_profiling_data();
391 
392   if (TimeOopMap) {
393     GenerateOopMap::print_time();
394   }
395   if (PrintSymbolTableSizeHistogram) {
396     SymbolTable::print_histogram();
397   }
398   if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
399     BytecodeCounter::print();
400   }
401   if (PrintBytecodePairHistogram) {
402     BytecodePairHistogram::print();
403   }
404 
405   if (PrintCodeCache) {
406     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
407     CodeCache::print();
408   }
409 
410   // CodeHeap State Analytics.
411   if (PrintCodeHeapAnalytics) {
412     CompileBroker::print_heapinfo(nullptr, "all", 4096); // details
413   }
414 
415   LogStreamHandle(Debug, codecache, nmethod) log;
416   if (log.is_enabled()) {
417     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
418     CodeCache::print_nmethods_on(&log);
419   }
420 
421 #ifndef PRODUCT
422   if (PrintCodeCache2) {
423     MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
424     CodeCache::print_internals();
425   }
426 #endif
427 
428   if (VerifyOops && Verbose) {
429     tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
430   }
431 
432   print_bytecode_count();
433 
434   if (PrintSystemDictionaryAtExit) {
435     ResourceMark rm;
436     MutexLocker mcld(ClassLoaderDataGraph_lock);
437     SystemDictionary::print();
438   }
439 
440   if (PrintClassLoaderDataGraphAtExit) {
441     ResourceMark rm;
442     MutexLocker mcld(ClassLoaderDataGraph_lock);
443     ClassLoaderDataGraph::print();
444   }
445 
446   // Native memory tracking data
447   if (PrintNMTStatistics) {
448     MemTracker::final_report(tty);
449   }
450 
451   if (PrintMetaspaceStatisticsAtExit) {
452     MetaspaceUtils::print_basic_report(tty, 0);
453   }
454 
455   if (CompilerOracle::should_print_final_memstat_report()) {
456     CompilationMemoryStatistic::print_all_by_size(tty, false, 0);
457   }
458 
459   ThreadsSMRSupport::log_statistics();
460 
461   log_vm_init_stats(true /* use_tty */);
462 }
463 
464 // Note: before_exit() can be executed only once, if more than one threads
465 //       are trying to shutdown the VM at the same time, only one thread
466 //       can run before_exit() and all other threads must wait.
467 void before_exit(JavaThread* thread, bool halt) {
468   #define BEFORE_EXIT_NOT_RUN 0
469   #define BEFORE_EXIT_RUNNING 1
470   #define BEFORE_EXIT_DONE    2
471   static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
472 
473   Events::log(thread, "Before exit entered");
474 
475   // Note: don't use a Mutex to guard the entire before_exit(), as
476   // JVMTI post_thread_end_event and post_vm_death_event will run native code.
477   // A CAS or OSMutex would work just fine but then we need to manipulate
478   // thread state for Safepoint. Here we use Monitor wait() and notify_all()
479   // for synchronization.
480   { MonitorLocker ml(BeforeExit_lock);
481     switch (_before_exit_status) {
482     case BEFORE_EXIT_NOT_RUN:
483       _before_exit_status = BEFORE_EXIT_RUNNING;
484       break;
485     case BEFORE_EXIT_RUNNING:
486       while (_before_exit_status == BEFORE_EXIT_RUNNING) {
487         ml.wait();
488       }
489       assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
490       return;
491     case BEFORE_EXIT_DONE:
492       // need block to avoid SS compiler bug
493       {
494         return;
495       }
496     }
497   }
498 
499   // At this point only one thread is executing this logic. Any other threads
500   // attempting to invoke before_exit() will wait above and return early once
501   // this thread finishes before_exit().
502 
503   // Do not add any additional shutdown logic between the above mutex logic and
504   // leak sanitizer logic below. Any additional shutdown code which performs some
505   // cleanup should be added after the leak sanitizer logic below.
506 
507 #ifdef LEAK_SANITIZER
508   // If we are built with LSan, we need to perform leak checking. If we are
509   // terminating normally, not halting and no VM error, we perform a normal
510   // leak check which terminates if leaks are found. If we are not terminating
511   // normally, halting or VM error, we perform a recoverable leak check which
512   // prints leaks but will not terminate.
513   if (!halt && !VMError::is_error_reported()) {
514     LSAN_DO_LEAK_CHECK();
515   } else {
516     // Ignore the return value.
517     static_cast<void>(LSAN_DO_RECOVERABLE_LEAK_CHECK());
518   }
519 #endif
520 
521 #if INCLUDE_CDS
522   MethodProfiler::process_method_hotness();
523 #endif
524 
525   // Actual shutdown logic begins here.
526 
527 #if INCLUDE_JVMCI
528   if (EnableJVMCI) {
529     JVMCI::shutdown(thread);
530   }
531 #endif
532 
533 #if INCLUDE_CDS
534   ClassListWriter::write_resolved_constants();
535   ClassListWriter::write_reflection_data();
536   ClassListWriter::write_loader_negative_lookup_cache();
537   // Dynamic CDS dumping must happen whilst we can still reliably
538   // run Java code.
539   if (CDSConfig::is_dumping_preimage_static_archive()) {
540     // Creating the hotspot.cds.preimage file
541     MetaspaceShared::preload_and_dump(thread);
542   } else {
543     DynamicArchive::dump_at_exit(thread, ArchiveClassesAtExit);
544   }
545   assert(!thread->has_pending_exception(), "must be");
546 #endif
547 
548   SCCache::close(); // Write final data and close archive
549 
550   // Hang forever on exit if we're reporting an error.
551   if (ShowMessageBoxOnError && VMError::is_error_reported()) {
552     os::infinite_sleep();
553   }
554 
555   EventThreadEnd event;
556   if (event.should_commit()) {
557     event.set_thread(JFR_JVM_THREAD_ID(thread));
558     event.commit();
559   }
560 
561   JFR_ONLY(Jfr::on_vm_shutdown(false, halt);)
562 
563   // Stop the WatcherThread. We do this before disenrolling various
564   // PeriodicTasks to reduce the likelihood of races.
565   WatcherThread::stop();
566 
567   // shut down the StatSampler task
568   StatSampler::disengage();
569   StatSampler::destroy();
570 
571   NativeHeapTrimmer::cleanup();
572 
573   // Stop concurrent GC threads
574   Universe::heap()->stop();
575 
576   // Print GC/heap related information.
577   Log(gc, heap, exit) log;
578   if (log.is_info()) {
579     ResourceMark rm;
580     LogStream ls_info(log.info());
581     Universe::print_on(&ls_info);
582     if (log.is_trace()) {
583       LogStream ls_trace(log.trace());
584       MutexLocker mcld(ClassLoaderDataGraph_lock);
585       ClassLoaderDataGraph::print_on(&ls_trace);
586     }
587   }
588 
589   if (PrintBytecodeHistogram) {
590     BytecodeHistogram::print(PrintBytecodeHistogramCutoff);
591   }
592 
593 #ifdef LINUX
594   if (DumpPerfMapAtExit) {
595     CodeCache::write_perf_map(nullptr, tty);
596   }
597   if (PrintMemoryMapAtExit) {
598     MemMapPrinter::print_all_mappings(tty);
599   }
600 #endif
601 
602   if (JvmtiExport::should_post_thread_life()) {
603     JvmtiExport::post_thread_end(thread);
604   }
605 
606   // Always call even when there are not JVMTI environments yet, since environments
607   // may be attached late and JVMTI must track phases of VM execution
608   JvmtiExport::post_vm_death();
609   JvmtiAgentList::unload_agents();
610 
611   // Terminate the signal thread
612   // Note: we don't wait until it actually dies.
613   os::terminate_signal_thread();
614 
615   if (VerifyTrainingData) {
616     EXCEPTION_MARK;
617     CompilationPolicy::replay_training_at_init(true, THREAD); // implies TrainingData::verify()
618   }
619 
620   print_statistics();
621   Universe::heap()->print_tracing_info();
622 
623   { MutexLocker ml(BeforeExit_lock);
624     _before_exit_status = BEFORE_EXIT_DONE;
625     BeforeExit_lock->notify_all();
626   }
627 
628   if (VerifyStringTableAtExit) {
629     size_t fail_cnt = StringTable::verify_and_compare_entries();
630     if (fail_cnt != 0) {
631       tty->print_cr("ERROR: fail_cnt=" SIZE_FORMAT, fail_cnt);
632       guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
633     }
634   }
635 
636   #undef BEFORE_EXIT_NOT_RUN
637   #undef BEFORE_EXIT_RUNNING
638   #undef BEFORE_EXIT_DONE
639 }
640 
641 void vm_exit(int code) {
642   Thread* thread =
643       ThreadLocalStorage::is_initialized() ? Thread::current_or_null() : nullptr;
644   if (thread == nullptr) {
645     // very early initialization failure -- just exit
646     vm_direct_exit(code);
647   }
648 
649   // We'd like to add an entry to the XML log to show that the VM is
650   // terminating, but we can't safely do that here. The logic to make
651   // XML termination logging safe is tied to the termination of the
652   // VMThread, and it doesn't terminate on this exit path. See 8222534.
653 
654   if (VMThread::vm_thread() != nullptr) {
655     if (thread->is_Java_thread()) {
656       // We must be "in_vm" for the code below to work correctly.
657       // Historically there must have been some exit path for which
658       // that was not the case and so we set it explicitly - even
659       // though we no longer know what that path may be.
660       JavaThread::cast(thread)->set_thread_state(_thread_in_vm);
661     }
662 
663     // Fire off a VM_Exit operation to bring VM to a safepoint and exit
664     VM_Exit op(code);
665 
666     // 4945125 The vm thread comes to a safepoint during exit.
667     // GC vm_operations can get caught at the safepoint, and the
668     // heap is unparseable if they are caught. Grab the Heap_lock
669     // to prevent this. The GC vm_operations will not be able to
670     // queue until after we release it, but we never do that as we
671     // are terminating the VM process.
672     MutexLocker ml(Heap_lock);
673 
674     VMThread::execute(&op);
675     // should never reach here; but in case something wrong with VM Thread.
676     vm_direct_exit(code);
677   } else {
678     // VM thread is gone, just exit
679     vm_direct_exit(code);
680   }
681   ShouldNotReachHere();
682 }
683 
684 void notify_vm_shutdown() {
685   // For now, just a dtrace probe.
686   HOTSPOT_VM_SHUTDOWN();
687 }
688 
689 void vm_direct_exit(int code) {
690   notify_vm_shutdown();
691   os::wait_for_keypress_at_exit();
692   os::exit(code);
693 }
694 
695 void vm_direct_exit(int code, const char* message) {
696   if (message != nullptr) {
697     tty->print_cr("%s", message);
698   }
699   vm_direct_exit(code);
700 }
701 
702 static void vm_perform_shutdown_actions() {
703   if (is_init_completed()) {
704     Thread* thread = Thread::current_or_null();
705     if (thread != nullptr && thread->is_Java_thread()) {
706       // We are leaving the VM, set state to native (in case any OS exit
707       // handlers call back to the VM)
708       JavaThread* jt = JavaThread::cast(thread);
709       // Must always be walkable or have no last_Java_frame when in
710       // thread_in_native
711       jt->frame_anchor()->make_walkable();
712       jt->set_thread_state(_thread_in_native);
713     }
714   }
715   notify_vm_shutdown();
716 }
717 
718 void vm_shutdown()
719 {
720   vm_perform_shutdown_actions();
721   os::wait_for_keypress_at_exit();
722   os::shutdown();
723 }
724 
725 void vm_abort(bool dump_core) {
726   vm_perform_shutdown_actions();
727   os::wait_for_keypress_at_exit();
728 
729   // Flush stdout and stderr before abort.
730   fflush(stdout);
731   fflush(stderr);
732 
733   os::abort(dump_core);
734   ShouldNotReachHere();
735 }
736 
737 static void vm_notify_during_cds_dumping(const char* error, const char* message) {
738   if (error != nullptr) {
739     tty->print_cr("Error occurred during CDS dumping");
740     tty->print("%s", error);
741     if (message != nullptr) {
742       tty->print_cr(": %s", message);
743     }
744     else {
745       tty->cr();
746     }
747   }
748 }
749 
750 void vm_exit_during_cds_dumping(const char* error, const char* message) {
751   vm_notify_during_cds_dumping(error, message);
752 
753   // Failure during CDS dumping, we don't want to dump core
754   vm_abort(false);
755 }
756 
757 static void vm_notify_during_shutdown(const char* error, const char* message) {
758   if (error != nullptr) {
759     tty->print_cr("Error occurred during initialization of VM");
760     tty->print("%s", error);
761     if (message != nullptr) {
762       tty->print_cr(": %s", message);
763     }
764     else {
765       tty->cr();
766     }
767   }
768   if (ShowMessageBoxOnError && WizardMode) {
769     fatal("Error occurred during initialization of VM");
770   }
771 }
772 
773 void vm_exit_during_initialization() {
774   vm_notify_during_shutdown(nullptr, nullptr);
775 
776   // Failure during initialization, we don't want to dump core
777   vm_abort(false);
778 }
779 
780 void vm_exit_during_initialization(Handle exception) {
781   tty->print_cr("Error occurred during initialization of VM");
782   // If there are exceptions on this thread it must be cleared
783   // first and here. Any future calls to EXCEPTION_MARK requires
784   // that no pending exceptions exist.
785   JavaThread* THREAD = JavaThread::current(); // can't be null
786   if (HAS_PENDING_EXCEPTION) {
787     CLEAR_PENDING_EXCEPTION;
788   }
789   java_lang_Throwable::print_stack_trace(exception, tty);
790   tty->cr();
791   vm_notify_during_shutdown(nullptr, nullptr);
792 
793   // Failure during initialization, we don't want to dump core
794   vm_abort(false);
795 }
796 
797 void vm_exit_during_initialization(Symbol* ex, const char* message) {
798   ResourceMark rm;
799   vm_notify_during_shutdown(ex->as_C_string(), message);
800 
801   // Failure during initialization, we don't want to dump core
802   vm_abort(false);
803 }
804 
805 void vm_exit_during_initialization(const char* error, const char* message) {
806   vm_notify_during_shutdown(error, message);
807 
808   // Failure during initialization, we don't want to dump core
809   vm_abort(false);
810 }
811 
812 void vm_shutdown_during_initialization(const char* error, const char* message) {
813   vm_notify_during_shutdown(error, message);
814   vm_shutdown();
815 }
816 
817 JDK_Version JDK_Version::_current;
818 const char* JDK_Version::_java_version;
819 const char* JDK_Version::_runtime_name;
820 const char* JDK_Version::_runtime_version;
821 const char* JDK_Version::_runtime_vendor_version;
822 const char* JDK_Version::_runtime_vendor_vm_bug_url;
823 
824 void JDK_Version::initialize() {
825   assert(!_current.is_valid(), "Don't initialize twice");
826 
827   int major = VM_Version::vm_major_version();
828   int minor = VM_Version::vm_minor_version();
829   int security = VM_Version::vm_security_version();
830   int build = VM_Version::vm_build_number();
831   int patch = VM_Version::vm_patch_version();
832   _current = JDK_Version(major, minor, security, patch, build);
833 }
834 
835 void JDK_Version_init() {
836   JDK_Version::initialize();
837 }
838 
839 static int64_t encode_jdk_version(const JDK_Version& v) {
840   return
841     ((int64_t)v.major_version()          << (BitsPerByte * 4)) |
842     ((int64_t)v.minor_version()          << (BitsPerByte * 3)) |
843     ((int64_t)v.security_version()       << (BitsPerByte * 2)) |
844     ((int64_t)v.patch_version()          << (BitsPerByte * 1)) |
845     ((int64_t)v.build_number()           << (BitsPerByte * 0));
846 }
847 
848 int JDK_Version::compare(const JDK_Version& other) const {
849   assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
850   uint64_t e = encode_jdk_version(*this);
851   uint64_t o = encode_jdk_version(other);
852   return (e > o) ? 1 : ((e == o) ? 0 : -1);
853 }
854 
855 /* See JEP 223 */
856 void JDK_Version::to_string(char* buffer, size_t buflen) const {
857   assert(buffer && buflen > 0, "call with useful buffer");
858   size_t index = 0;
859 
860   if (!is_valid()) {
861     jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
862   } else {
863     int rc = jio_snprintf(
864         &buffer[index], buflen - index, "%d.%d", _major, _minor);
865     if (rc == -1) return;
866     index += rc;
867     if (_patch > 0) {
868       rc = jio_snprintf(&buffer[index], buflen - index, ".%d.%d", _security, _patch);
869       if (rc == -1) return;
870       index += rc;
871     } else if (_security > 0) {
872       rc = jio_snprintf(&buffer[index], buflen - index, ".%d", _security);
873       if (rc == -1) return;
874       index += rc;
875     }
876     if (_build > 0) {
877       rc = jio_snprintf(&buffer[index], buflen - index, "+%d", _build);
878       if (rc == -1) return;
879       index += rc;
880     }
881   }
882 }