src/share/vm/runtime/java.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6735
a45a4f5a9609
parent 1
2d8a650513c2
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@1 25 /*
aoqi@1 26 * This file has been modified by Loongson Technology in 2015. These
aoqi@1 27 * modifications are Copyright (c) 2015 Loongson Technology, and are made
aoqi@1 28 * available on the same license terms set forth above.
aoqi@1 29 */
aoqi@1 30
aoqi@0 31 #include "precompiled.hpp"
aoqi@0 32 #include "classfile/classLoader.hpp"
aoqi@0 33 #include "classfile/symbolTable.hpp"
aoqi@0 34 #include "classfile/systemDictionary.hpp"
aoqi@0 35 #include "code/codeCache.hpp"
aoqi@0 36 #include "compiler/compileBroker.hpp"
aoqi@0 37 #include "compiler/compilerOracle.hpp"
aoqi@0 38 #include "interpreter/bytecodeHistogram.hpp"
aoqi@0 39 #include "memory/genCollectedHeap.hpp"
aoqi@0 40 #include "memory/oopFactory.hpp"
aoqi@0 41 #include "memory/universe.hpp"
aoqi@0 42 #include "oops/constantPool.hpp"
aoqi@0 43 #include "oops/generateOopMap.hpp"
aoqi@0 44 #include "oops/instanceKlass.hpp"
aoqi@0 45 #include "oops/instanceOop.hpp"
aoqi@0 46 #include "oops/method.hpp"
aoqi@0 47 #include "oops/objArrayOop.hpp"
aoqi@0 48 #include "oops/oop.inline.hpp"
aoqi@0 49 #include "oops/symbol.hpp"
aoqi@0 50 #include "prims/jvmtiExport.hpp"
aoqi@0 51 #include "runtime/arguments.hpp"
aoqi@0 52 #include "runtime/biasedLocking.hpp"
aoqi@0 53 #include "runtime/compilationPolicy.hpp"
aoqi@0 54 #include "runtime/fprofiler.hpp"
aoqi@0 55 #include "runtime/init.hpp"
aoqi@0 56 #include "runtime/interfaceSupport.hpp"
aoqi@0 57 #include "runtime/java.hpp"
aoqi@0 58 #include "runtime/memprofiler.hpp"
aoqi@0 59 #include "runtime/sharedRuntime.hpp"
aoqi@0 60 #include "runtime/statSampler.hpp"
aoqi@0 61 #include "runtime/sweeper.hpp"
aoqi@0 62 #include "runtime/task.hpp"
aoqi@0 63 #include "runtime/thread.inline.hpp"
aoqi@0 64 #include "runtime/timer.hpp"
aoqi@0 65 #include "runtime/vm_operations.hpp"
aoqi@0 66 #include "services/memReporter.hpp"
aoqi@0 67 #include "services/memTracker.hpp"
aoqi@0 68 #include "trace/tracing.hpp"
aoqi@0 69 #include "utilities/dtrace.hpp"
aoqi@0 70 #include "utilities/globalDefinitions.hpp"
aoqi@0 71 #include "utilities/histogram.hpp"
aoqi@0 72 #include "utilities/macros.hpp"
aoqi@0 73 #include "utilities/vmError.hpp"
aoqi@0 74 #ifdef TARGET_ARCH_x86
aoqi@0 75 # include "vm_version_x86.hpp"
aoqi@0 76 #endif
aoqi@0 77 #ifdef TARGET_ARCH_sparc
aoqi@0 78 # include "vm_version_sparc.hpp"
aoqi@0 79 #endif
aoqi@0 80 #ifdef TARGET_ARCH_zero
aoqi@0 81 # include "vm_version_zero.hpp"
aoqi@0 82 #endif
aoqi@0 83 #ifdef TARGET_ARCH_arm
aoqi@0 84 # include "vm_version_arm.hpp"
aoqi@0 85 #endif
aoqi@0 86 #ifdef TARGET_ARCH_ppc
aoqi@0 87 # include "vm_version_ppc.hpp"
aoqi@0 88 #endif
aoqi@1 89 #ifdef TARGET_ARCH_mips
aoqi@1 90 # include "vm_version_mips.hpp"
aoqi@1 91 #endif
aoqi@0 92 #if INCLUDE_ALL_GCS
aoqi@0 93 #include "gc_implementation/concurrentMarkSweep/concurrentMarkSweepThread.hpp"
aoqi@0 94 #include "gc_implementation/parallelScavenge/psScavenge.hpp"
aoqi@0 95 #include "gc_implementation/parallelScavenge/psScavenge.inline.hpp"
aoqi@0 96 #endif // INCLUDE_ALL_GCS
aoqi@0 97 #ifdef COMPILER1
aoqi@0 98 #include "c1/c1_Compiler.hpp"
aoqi@0 99 #include "c1/c1_Runtime1.hpp"
aoqi@0 100 #endif
aoqi@0 101 #ifdef COMPILER2
aoqi@0 102 #include "code/compiledIC.hpp"
aoqi@0 103 #include "compiler/methodLiveness.hpp"
aoqi@0 104 #include "opto/compile.hpp"
aoqi@0 105 #include "opto/indexSet.hpp"
aoqi@0 106 #include "opto/runtime.hpp"
aoqi@0 107 #endif
aoqi@0 108
aoqi@0 109 #ifndef USDT2
aoqi@0 110 HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown);
aoqi@0 111 #endif /* !USDT2 */
aoqi@0 112
aoqi@0 113 #ifndef PRODUCT
aoqi@0 114
aoqi@0 115 // Statistics printing (method invocation histogram)
aoqi@0 116
aoqi@0 117 GrowableArray<Method*>* collected_invoked_methods;
aoqi@0 118
aoqi@0 119 void collect_invoked_methods(Method* m) {
aoqi@0 120 if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
aoqi@0 121 collected_invoked_methods->push(m);
aoqi@0 122 }
aoqi@0 123 }
aoqi@0 124
aoqi@0 125 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 126
aoqi@0 127 GrowableArray<Method*>* collected_profiled_methods;
aoqi@0 128
aoqi@0 129 void collect_profiled_methods(Method* m) {
aoqi@0 130 Thread* thread = Thread::current();
aoqi@0 131 // This HandleMark prevents a huge amount of handles from being added
aoqi@0 132 // to the metadata_handles() array on the thread.
aoqi@0 133 HandleMark hm(thread);
aoqi@0 134 methodHandle mh(thread, m);
aoqi@0 135 if ((m->method_data() != NULL) &&
aoqi@0 136 (PrintMethodData || CompilerOracle::should_print(mh))) {
aoqi@0 137 collected_profiled_methods->push(m);
aoqi@0 138 }
aoqi@0 139 }
aoqi@0 140
aoqi@0 141
aoqi@0 142 int compare_methods(Method** a, Method** b) {
aoqi@0 143 // %%% there can be 32-bit overflow here
aoqi@0 144 return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
aoqi@0 145 - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
aoqi@0 146 }
aoqi@0 147
aoqi@0 148
aoqi@0 149 void print_method_invocation_histogram() {
aoqi@0 150 ResourceMark rm;
aoqi@0 151 HandleMark hm;
aoqi@0 152 collected_invoked_methods = new GrowableArray<Method*>(1024);
aoqi@0 153 SystemDictionary::methods_do(collect_invoked_methods);
aoqi@0 154 collected_invoked_methods->sort(&compare_methods);
aoqi@0 155 //
aoqi@0 156 tty->cr();
aoqi@0 157 tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = %d):", MethodHistogramCutoff);
aoqi@0 158 tty->cr();
aoqi@0 159 tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
aoqi@0 160 unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
aoqi@0 161 synch_total = 0, nativ_total = 0, acces_total = 0;
aoqi@0 162 for (int index = 0; index < collected_invoked_methods->length(); index++) {
aoqi@0 163 Method* m = collected_invoked_methods->at(index);
aoqi@0 164 int c = m->invocation_count() + m->compiled_invocation_count();
aoqi@0 165 if (c >= MethodHistogramCutoff) m->print_invocation_count();
aoqi@0 166 int_total += m->invocation_count();
aoqi@0 167 comp_total += m->compiled_invocation_count();
aoqi@0 168 if (m->is_final()) final_total += c;
aoqi@0 169 if (m->is_static()) static_total += c;
aoqi@0 170 if (m->is_synchronized()) synch_total += c;
aoqi@0 171 if (m->is_native()) nativ_total += c;
aoqi@0 172 if (m->is_accessor()) acces_total += c;
aoqi@0 173 }
aoqi@0 174 tty->cr();
aoqi@0 175 total = int_total + comp_total;
aoqi@0 176 tty->print_cr("Invocations summary:");
aoqi@0 177 tty->print_cr("\t%9d (%4.1f%%) interpreted", int_total, 100.0 * int_total / total);
aoqi@0 178 tty->print_cr("\t%9d (%4.1f%%) compiled", comp_total, 100.0 * comp_total / total);
aoqi@0 179 tty->print_cr("\t%9d (100%%) total", total);
aoqi@0 180 tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total, 100.0 * synch_total / total);
aoqi@0 181 tty->print_cr("\t%9d (%4.1f%%) final", final_total, 100.0 * final_total / total);
aoqi@0 182 tty->print_cr("\t%9d (%4.1f%%) static", static_total, 100.0 * static_total / total);
aoqi@0 183 tty->print_cr("\t%9d (%4.1f%%) native", nativ_total, 100.0 * nativ_total / total);
aoqi@0 184 tty->print_cr("\t%9d (%4.1f%%) accessor", acces_total, 100.0 * acces_total / total);
aoqi@0 185 tty->cr();
aoqi@0 186 SharedRuntime::print_call_statistics(comp_total);
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 void print_method_profiling_data() {
aoqi@0 190 ResourceMark rm;
aoqi@0 191 HandleMark hm;
aoqi@0 192 collected_profiled_methods = new GrowableArray<Method*>(1024);
aoqi@0 193 SystemDictionary::methods_do(collect_profiled_methods);
aoqi@0 194 collected_profiled_methods->sort(&compare_methods);
aoqi@0 195
aoqi@0 196 int count = collected_profiled_methods->length();
aoqi@0 197 int total_size = 0;
aoqi@0 198 if (count > 0) {
aoqi@0 199 for (int index = 0; index < count; index++) {
aoqi@0 200 Method* m = collected_profiled_methods->at(index);
aoqi@0 201 ttyLocker ttyl;
aoqi@0 202 tty->print_cr("------------------------------------------------------------------------");
aoqi@0 203 //m->print_name(tty);
aoqi@0 204 m->print_invocation_count();
aoqi@0 205 tty->print_cr(" mdo size: %d bytes", m->method_data()->size_in_bytes());
aoqi@0 206 tty->cr();
aoqi@0 207 // Dump data on parameters if any
aoqi@0 208 if (m->method_data() != NULL && m->method_data()->parameters_type_data() != NULL) {
aoqi@0 209 tty->fill_to(2);
aoqi@0 210 m->method_data()->parameters_type_data()->print_data_on(tty);
aoqi@0 211 }
aoqi@0 212 m->print_codes();
aoqi@0 213 total_size += m->method_data()->size_in_bytes();
aoqi@0 214 }
aoqi@0 215 tty->print_cr("------------------------------------------------------------------------");
aoqi@0 216 tty->print_cr("Total MDO size: %d bytes", total_size);
aoqi@0 217 }
aoqi@0 218 }
aoqi@0 219
aoqi@0 220 void print_bytecode_count() {
aoqi@0 221 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
aoqi@0 222 tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
aoqi@0 223 }
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 AllocStats alloc_stats;
aoqi@0 227
aoqi@0 228
aoqi@0 229
aoqi@0 230 // General statistics printing (profiling ...)
aoqi@0 231 void print_statistics() {
aoqi@0 232 #ifdef ASSERT
aoqi@0 233
aoqi@0 234 if (CountRuntimeCalls) {
aoqi@0 235 extern Histogram *RuntimeHistogram;
aoqi@0 236 RuntimeHistogram->print();
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 if (CountJNICalls) {
aoqi@0 240 extern Histogram *JNIHistogram;
aoqi@0 241 JNIHistogram->print();
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 if (CountJVMCalls) {
aoqi@0 245 extern Histogram *JVMHistogram;
aoqi@0 246 JVMHistogram->print();
aoqi@0 247 }
aoqi@0 248
aoqi@0 249 #endif
aoqi@0 250
aoqi@0 251 if (MemProfiling) {
aoqi@0 252 MemProfiler::disengage();
aoqi@0 253 }
aoqi@0 254
aoqi@0 255 if (CITime) {
aoqi@0 256 CompileBroker::print_times();
aoqi@0 257 }
aoqi@0 258
aoqi@0 259 #ifdef COMPILER1
aoqi@0 260 if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
aoqi@0 261 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
aoqi@0 262 Runtime1::print_statistics();
aoqi@0 263 Deoptimization::print_statistics();
aoqi@0 264 SharedRuntime::print_statistics();
aoqi@0 265 nmethod::print_statistics();
aoqi@0 266 }
aoqi@0 267 #endif /* COMPILER1 */
aoqi@0 268
aoqi@0 269 #ifdef COMPILER2
aoqi@0 270 if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
aoqi@0 271 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
aoqi@0 272 Compile::print_statistics();
aoqi@0 273 #ifndef COMPILER1
aoqi@0 274 Deoptimization::print_statistics();
aoqi@0 275 nmethod::print_statistics();
aoqi@0 276 SharedRuntime::print_statistics();
aoqi@0 277 #endif //COMPILER1
aoqi@0 278 os::print_statistics();
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
aoqi@0 282 OptoRuntime::print_named_counters();
aoqi@0 283 }
aoqi@0 284
aoqi@0 285 if (TimeLivenessAnalysis) {
aoqi@0 286 MethodLiveness::print_times();
aoqi@0 287 }
aoqi@0 288 #ifdef ASSERT
aoqi@0 289 if (CollectIndexSetStatistics) {
aoqi@0 290 IndexSet::print_statistics();
aoqi@0 291 }
aoqi@0 292 #endif // ASSERT
aoqi@0 293 #endif // COMPILER2
aoqi@0 294 if (CountCompiledCalls) {
aoqi@0 295 print_method_invocation_histogram();
aoqi@0 296 }
aoqi@0 297 if (ProfileInterpreter COMPILER1_PRESENT(|| C1UpdateMethodData)) {
aoqi@0 298 print_method_profiling_data();
aoqi@0 299 }
aoqi@0 300 if (TimeCompiler) {
aoqi@0 301 COMPILER2_PRESENT(Compile::print_timers();)
aoqi@0 302 }
aoqi@0 303 if (TimeCompilationPolicy) {
aoqi@0 304 CompilationPolicy::policy()->print_time();
aoqi@0 305 }
aoqi@0 306 if (TimeOopMap) {
aoqi@0 307 GenerateOopMap::print_time();
aoqi@0 308 }
aoqi@0 309 if (ProfilerCheckIntervals) {
aoqi@0 310 PeriodicTask::print_intervals();
aoqi@0 311 }
aoqi@0 312 if (PrintSymbolTableSizeHistogram) {
aoqi@0 313 SymbolTable::print_histogram();
aoqi@0 314 }
aoqi@0 315 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
aoqi@0 316 BytecodeCounter::print();
aoqi@0 317 }
aoqi@0 318 if (PrintBytecodePairHistogram) {
aoqi@0 319 BytecodePairHistogram::print();
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 if (PrintCodeCache) {
aoqi@0 323 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 324 CodeCache::print();
aoqi@0 325 }
aoqi@0 326
aoqi@0 327 if (PrintMethodFlushingStatistics) {
aoqi@0 328 NMethodSweeper::print();
aoqi@0 329 }
aoqi@0 330
aoqi@0 331 if (PrintCodeCache2) {
aoqi@0 332 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 333 CodeCache::print_internals();
aoqi@0 334 }
aoqi@0 335
aoqi@0 336 if (PrintClassStatistics) {
aoqi@0 337 SystemDictionary::print_class_statistics();
aoqi@0 338 }
aoqi@0 339 if (PrintMethodStatistics) {
aoqi@0 340 SystemDictionary::print_method_statistics();
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 if (PrintVtableStats) {
aoqi@0 344 klassVtable::print_statistics();
aoqi@0 345 klassItable::print_statistics();
aoqi@0 346 }
aoqi@0 347 if (VerifyOops) {
aoqi@0 348 tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
aoqi@0 349 }
aoqi@0 350
aoqi@0 351 print_bytecode_count();
aoqi@0 352 if (PrintMallocStatistics) {
aoqi@0 353 tty->print("allocation stats: ");
aoqi@0 354 alloc_stats.print();
aoqi@0 355 tty->cr();
aoqi@0 356 }
aoqi@0 357
aoqi@0 358 if (PrintSystemDictionaryAtExit) {
aoqi@0 359 SystemDictionary::print();
aoqi@0 360 }
aoqi@0 361
aoqi@0 362 if (PrintBiasedLockingStatistics) {
aoqi@0 363 BiasedLocking::print_counters();
aoqi@0 364 }
aoqi@0 365
aoqi@0 366 #ifdef ENABLE_ZAP_DEAD_LOCALS
aoqi@0 367 #ifdef COMPILER2
aoqi@0 368 if (ZapDeadCompiledLocals) {
aoqi@0 369 tty->print_cr("Compile::CompiledZap_count = %d", Compile::CompiledZap_count);
aoqi@0 370 tty->print_cr("OptoRuntime::ZapDeadCompiledLocals_count = %d", OptoRuntime::ZapDeadCompiledLocals_count);
aoqi@0 371 }
aoqi@0 372 #endif // COMPILER2
aoqi@0 373 #endif // ENABLE_ZAP_DEAD_LOCALS
aoqi@0 374 // Native memory tracking data
aoqi@0 375 if (PrintNMTStatistics) {
aoqi@0 376 if (MemTracker::is_on()) {
aoqi@0 377 BaselineTTYOutputer outputer(tty);
aoqi@0 378 MemTracker::print_memory_usage(outputer, K, false);
aoqi@0 379 } else {
aoqi@0 380 tty->print_cr("%s", MemTracker::reason());
aoqi@0 381 }
aoqi@0 382 }
aoqi@0 383 }
aoqi@0 384
aoqi@0 385 #else // PRODUCT MODE STATISTICS
aoqi@0 386
aoqi@0 387 void print_statistics() {
aoqi@0 388
aoqi@0 389 if (CITime) {
aoqi@0 390 CompileBroker::print_times();
aoqi@0 391 }
aoqi@0 392
aoqi@0 393 if (PrintCodeCache) {
aoqi@0 394 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 395 CodeCache::print();
aoqi@0 396 }
aoqi@0 397
aoqi@0 398 if (PrintMethodFlushingStatistics) {
aoqi@0 399 NMethodSweeper::print();
aoqi@0 400 }
aoqi@0 401
aoqi@0 402 #ifdef COMPILER2
aoqi@0 403 if (PrintPreciseBiasedLockingStatistics || PrintPreciseRTMLockingStatistics) {
aoqi@0 404 OptoRuntime::print_named_counters();
aoqi@0 405 }
aoqi@0 406 #endif
aoqi@0 407 if (PrintBiasedLockingStatistics) {
aoqi@0 408 BiasedLocking::print_counters();
aoqi@0 409 }
aoqi@0 410
aoqi@0 411 // Native memory tracking data
aoqi@0 412 if (PrintNMTStatistics) {
aoqi@0 413 if (MemTracker::is_on()) {
aoqi@0 414 BaselineTTYOutputer outputer(tty);
aoqi@0 415 MemTracker::print_memory_usage(outputer, K, false);
aoqi@0 416 } else {
aoqi@0 417 tty->print_cr("%s", MemTracker::reason());
aoqi@0 418 }
aoqi@0 419 }
aoqi@0 420 }
aoqi@0 421
aoqi@0 422 #endif
aoqi@0 423
aoqi@0 424
aoqi@0 425 // Helper class for registering on_exit calls through JVM_OnExit
aoqi@0 426
aoqi@0 427 extern "C" {
aoqi@0 428 typedef void (*__exit_proc)(void);
aoqi@0 429 }
aoqi@0 430
aoqi@0 431 class ExitProc : public CHeapObj<mtInternal> {
aoqi@0 432 private:
aoqi@0 433 __exit_proc _proc;
aoqi@0 434 // void (*_proc)(void);
aoqi@0 435 ExitProc* _next;
aoqi@0 436 public:
aoqi@0 437 // ExitProc(void (*proc)(void)) {
aoqi@0 438 ExitProc(__exit_proc proc) {
aoqi@0 439 _proc = proc;
aoqi@0 440 _next = NULL;
aoqi@0 441 }
aoqi@0 442 void evaluate() { _proc(); }
aoqi@0 443 ExitProc* next() const { return _next; }
aoqi@0 444 void set_next(ExitProc* next) { _next = next; }
aoqi@0 445 };
aoqi@0 446
aoqi@0 447
aoqi@0 448 // Linked list of registered on_exit procedures
aoqi@0 449
aoqi@0 450 static ExitProc* exit_procs = NULL;
aoqi@0 451
aoqi@0 452
aoqi@0 453 extern "C" {
aoqi@0 454 void register_on_exit_function(void (*func)(void)) {
aoqi@0 455 ExitProc *entry = new ExitProc(func);
aoqi@0 456 // Classic vm does not throw an exception in case the allocation failed,
aoqi@0 457 if (entry != NULL) {
aoqi@0 458 entry->set_next(exit_procs);
aoqi@0 459 exit_procs = entry;
aoqi@0 460 }
aoqi@0 461 }
aoqi@0 462 }
aoqi@0 463
aoqi@0 464 // Note: before_exit() can be executed only once, if more than one threads
aoqi@0 465 // are trying to shutdown the VM at the same time, only one thread
aoqi@0 466 // can run before_exit() and all other threads must wait.
aoqi@0 467 void before_exit(JavaThread * thread) {
aoqi@0 468 #define BEFORE_EXIT_NOT_RUN 0
aoqi@0 469 #define BEFORE_EXIT_RUNNING 1
aoqi@0 470 #define BEFORE_EXIT_DONE 2
aoqi@0 471 static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
aoqi@0 472
aoqi@0 473 // Note: don't use a Mutex to guard the entire before_exit(), as
aoqi@0 474 // JVMTI post_thread_end_event and post_vm_death_event will run native code.
aoqi@0 475 // A CAS or OSMutex would work just fine but then we need to manipulate
aoqi@0 476 // thread state for Safepoint. Here we use Monitor wait() and notify_all()
aoqi@0 477 // for synchronization.
aoqi@0 478 { MutexLocker ml(BeforeExit_lock);
aoqi@0 479 switch (_before_exit_status) {
aoqi@0 480 case BEFORE_EXIT_NOT_RUN:
aoqi@0 481 _before_exit_status = BEFORE_EXIT_RUNNING;
aoqi@0 482 break;
aoqi@0 483 case BEFORE_EXIT_RUNNING:
aoqi@0 484 while (_before_exit_status == BEFORE_EXIT_RUNNING) {
aoqi@0 485 BeforeExit_lock->wait();
aoqi@0 486 }
aoqi@0 487 assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
aoqi@0 488 return;
aoqi@0 489 case BEFORE_EXIT_DONE:
aoqi@0 490 return;
aoqi@0 491 }
aoqi@0 492 }
aoqi@0 493
aoqi@0 494 // The only difference between this and Win32's _onexit procs is that
aoqi@0 495 // this version is invoked before any threads get killed.
aoqi@0 496 ExitProc* current = exit_procs;
aoqi@0 497 while (current != NULL) {
aoqi@0 498 ExitProc* next = current->next();
aoqi@0 499 current->evaluate();
aoqi@0 500 delete current;
aoqi@0 501 current = next;
aoqi@0 502 }
aoqi@0 503
aoqi@0 504 // Hang forever on exit if we're reporting an error.
aoqi@0 505 if (ShowMessageBoxOnError && is_error_reported()) {
aoqi@0 506 os::infinite_sleep();
aoqi@0 507 }
aoqi@0 508
aoqi@0 509 // Terminate watcher thread - must before disenrolling any periodic task
aoqi@0 510 if (PeriodicTask::num_tasks() > 0)
aoqi@0 511 WatcherThread::stop();
aoqi@0 512
aoqi@0 513 // Print statistics gathered (profiling ...)
aoqi@0 514 if (Arguments::has_profile()) {
aoqi@0 515 FlatProfiler::disengage();
aoqi@0 516 FlatProfiler::print(10);
aoqi@0 517 }
aoqi@0 518
aoqi@0 519 // shut down the StatSampler task
aoqi@0 520 StatSampler::disengage();
aoqi@0 521 StatSampler::destroy();
aoqi@0 522
aoqi@0 523 // Stop concurrent GC threads
aoqi@0 524 Universe::heap()->stop();
aoqi@0 525
aoqi@0 526 // Print GC/heap related information.
aoqi@0 527 if (PrintGCDetails) {
aoqi@0 528 Universe::print();
aoqi@0 529 AdaptiveSizePolicyOutput(0);
aoqi@0 530 if (Verbose) {
aoqi@0 531 ClassLoaderDataGraph::dump_on(gclog_or_tty);
aoqi@0 532 }
aoqi@0 533 }
aoqi@0 534
aoqi@0 535 if (PrintBytecodeHistogram) {
aoqi@0 536 BytecodeHistogram::print();
aoqi@0 537 }
aoqi@0 538
aoqi@0 539 if (JvmtiExport::should_post_thread_life()) {
aoqi@0 540 JvmtiExport::post_thread_end(thread);
aoqi@0 541 }
aoqi@0 542
aoqi@0 543
aoqi@0 544 EventThreadEnd event;
aoqi@0 545 if (event.should_commit()) {
aoqi@0 546 event.set_javalangthread(java_lang_Thread::thread_id(thread->threadObj()));
aoqi@0 547 event.commit();
aoqi@0 548 }
aoqi@0 549
aoqi@0 550 // Always call even when there are not JVMTI environments yet, since environments
aoqi@0 551 // may be attached late and JVMTI must track phases of VM execution
aoqi@0 552 JvmtiExport::post_vm_death();
aoqi@0 553 Threads::shutdown_vm_agents();
aoqi@0 554
aoqi@0 555 // Terminate the signal thread
aoqi@0 556 // Note: we don't wait until it actually dies.
aoqi@0 557 os::terminate_signal_thread();
aoqi@0 558
aoqi@0 559 print_statistics();
aoqi@0 560 Universe::heap()->print_tracing_info();
aoqi@0 561
aoqi@0 562 { MutexLocker ml(BeforeExit_lock);
aoqi@0 563 _before_exit_status = BEFORE_EXIT_DONE;
aoqi@0 564 BeforeExit_lock->notify_all();
aoqi@0 565 }
aoqi@0 566
aoqi@0 567 // Shutdown NMT before exit. Otherwise,
aoqi@0 568 // it will run into trouble when system destroys static variables.
aoqi@0 569 MemTracker::shutdown(MemTracker::NMT_normal);
aoqi@0 570
aoqi@0 571 if (VerifyStringTableAtExit) {
aoqi@0 572 int fail_cnt = 0;
aoqi@0 573 {
aoqi@0 574 MutexLocker ml(StringTable_lock);
aoqi@0 575 fail_cnt = StringTable::verify_and_compare_entries();
aoqi@0 576 }
aoqi@0 577
aoqi@0 578 if (fail_cnt != 0) {
aoqi@0 579 tty->print_cr("ERROR: fail_cnt=%d", fail_cnt);
aoqi@0 580 guarantee(fail_cnt == 0, "unexpected StringTable verification failures");
aoqi@0 581 }
aoqi@0 582 }
aoqi@0 583
aoqi@0 584 #undef BEFORE_EXIT_NOT_RUN
aoqi@0 585 #undef BEFORE_EXIT_RUNNING
aoqi@0 586 #undef BEFORE_EXIT_DONE
aoqi@0 587 }
aoqi@0 588
aoqi@0 589 void vm_exit(int code) {
aoqi@0 590 Thread* thread = ThreadLocalStorage::is_initialized() ?
aoqi@0 591 ThreadLocalStorage::get_thread_slow() : NULL;
aoqi@0 592 if (thread == NULL) {
aoqi@0 593 // we have serious problems -- just exit
aoqi@0 594 vm_direct_exit(code);
aoqi@0 595 }
aoqi@0 596
aoqi@0 597 if (VMThread::vm_thread() != NULL) {
aoqi@0 598 // Fire off a VM_Exit operation to bring VM to a safepoint and exit
aoqi@0 599 VM_Exit op(code);
aoqi@0 600 if (thread->is_Java_thread())
aoqi@0 601 ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
aoqi@0 602 VMThread::execute(&op);
aoqi@0 603 // should never reach here; but in case something wrong with VM Thread.
aoqi@0 604 vm_direct_exit(code);
aoqi@0 605 } else {
aoqi@0 606 // VM thread is gone, just exit
aoqi@0 607 vm_direct_exit(code);
aoqi@0 608 }
aoqi@0 609 ShouldNotReachHere();
aoqi@0 610 }
aoqi@0 611
aoqi@0 612 void notify_vm_shutdown() {
aoqi@0 613 // For now, just a dtrace probe.
aoqi@0 614 #ifndef USDT2
aoqi@0 615 HS_DTRACE_PROBE(hotspot, vm__shutdown);
aoqi@0 616 HS_DTRACE_WORKAROUND_TAIL_CALL_BUG();
aoqi@0 617 #else /* USDT2 */
aoqi@0 618 HOTSPOT_VM_SHUTDOWN();
aoqi@0 619 #endif /* USDT2 */
aoqi@0 620 }
aoqi@0 621
aoqi@0 622 void vm_direct_exit(int code) {
aoqi@0 623 notify_vm_shutdown();
aoqi@0 624 os::wait_for_keypress_at_exit();
aoqi@0 625 ::exit(code);
aoqi@0 626 }
aoqi@0 627
aoqi@0 628 void vm_perform_shutdown_actions() {
aoqi@0 629 // Warning: do not call 'exit_globals()' here. All threads are still running.
aoqi@0 630 // Calling 'exit_globals()' will disable thread-local-storage and cause all
aoqi@0 631 // kinds of assertions to trigger in debug mode.
aoqi@0 632 if (is_init_completed()) {
aoqi@0 633 Thread* thread = ThreadLocalStorage::is_initialized() ?
aoqi@0 634 ThreadLocalStorage::get_thread_slow() : NULL;
aoqi@0 635 if (thread != NULL && thread->is_Java_thread()) {
aoqi@0 636 // We are leaving the VM, set state to native (in case any OS exit
aoqi@0 637 // handlers call back to the VM)
aoqi@0 638 JavaThread* jt = (JavaThread*)thread;
aoqi@0 639 // Must always be walkable or have no last_Java_frame when in
aoqi@0 640 // thread_in_native
aoqi@0 641 jt->frame_anchor()->make_walkable(jt);
aoqi@0 642 jt->set_thread_state(_thread_in_native);
aoqi@0 643 }
aoqi@0 644 }
aoqi@0 645 notify_vm_shutdown();
aoqi@0 646 }
aoqi@0 647
aoqi@0 648 void vm_shutdown()
aoqi@0 649 {
aoqi@0 650 vm_perform_shutdown_actions();
aoqi@0 651 os::wait_for_keypress_at_exit();
aoqi@0 652 os::shutdown();
aoqi@0 653 }
aoqi@0 654
aoqi@0 655 void vm_abort(bool dump_core) {
aoqi@0 656 vm_perform_shutdown_actions();
aoqi@0 657 os::wait_for_keypress_at_exit();
aoqi@0 658 os::abort(dump_core);
aoqi@0 659 ShouldNotReachHere();
aoqi@0 660 }
aoqi@0 661
aoqi@0 662 void vm_notify_during_shutdown(const char* error, const char* message) {
aoqi@0 663 if (error != NULL) {
aoqi@0 664 tty->print_cr("Error occurred during initialization of VM");
aoqi@0 665 tty->print("%s", error);
aoqi@0 666 if (message != NULL) {
aoqi@0 667 tty->print_cr(": %s", message);
aoqi@0 668 }
aoqi@0 669 else {
aoqi@0 670 tty->cr();
aoqi@0 671 }
aoqi@0 672 }
aoqi@0 673 if (ShowMessageBoxOnError && WizardMode) {
aoqi@0 674 fatal("Error occurred during initialization of VM");
aoqi@0 675 }
aoqi@0 676 }
aoqi@0 677
aoqi@0 678 void vm_exit_during_initialization(Handle exception) {
aoqi@0 679 tty->print_cr("Error occurred during initialization of VM");
aoqi@0 680 // If there are exceptions on this thread it must be cleared
aoqi@0 681 // first and here. Any future calls to EXCEPTION_MARK requires
aoqi@0 682 // that no pending exceptions exist.
aoqi@0 683 Thread *THREAD = Thread::current();
aoqi@0 684 if (HAS_PENDING_EXCEPTION) {
aoqi@0 685 CLEAR_PENDING_EXCEPTION;
aoqi@0 686 }
aoqi@0 687 java_lang_Throwable::print(exception, tty);
aoqi@0 688 tty->cr();
aoqi@0 689 java_lang_Throwable::print_stack_trace(exception(), tty);
aoqi@0 690 tty->cr();
aoqi@0 691 vm_notify_during_shutdown(NULL, NULL);
aoqi@0 692
aoqi@0 693 // Failure during initialization, we don't want to dump core
aoqi@0 694 vm_abort(false);
aoqi@0 695 }
aoqi@0 696
aoqi@0 697 void vm_exit_during_initialization(Symbol* ex, const char* message) {
aoqi@0 698 ResourceMark rm;
aoqi@0 699 vm_notify_during_shutdown(ex->as_C_string(), message);
aoqi@0 700
aoqi@0 701 // Failure during initialization, we don't want to dump core
aoqi@0 702 vm_abort(false);
aoqi@0 703 }
aoqi@0 704
aoqi@0 705 void vm_exit_during_initialization(const char* error, const char* message) {
aoqi@0 706 vm_notify_during_shutdown(error, message);
aoqi@0 707
aoqi@0 708 // Failure during initialization, we don't want to dump core
aoqi@0 709 vm_abort(false);
aoqi@0 710 }
aoqi@0 711
aoqi@0 712 void vm_shutdown_during_initialization(const char* error, const char* message) {
aoqi@0 713 vm_notify_during_shutdown(error, message);
aoqi@0 714 vm_shutdown();
aoqi@0 715 }
aoqi@0 716
aoqi@0 717 JDK_Version JDK_Version::_current;
aoqi@0 718 const char* JDK_Version::_runtime_name;
aoqi@0 719 const char* JDK_Version::_runtime_version;
aoqi@0 720
aoqi@0 721 void JDK_Version::initialize() {
aoqi@0 722 jdk_version_info info;
aoqi@0 723 assert(!_current.is_valid(), "Don't initialize twice");
aoqi@0 724
aoqi@0 725 void *lib_handle = os::native_java_library();
aoqi@0 726 jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
aoqi@0 727 os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
aoqi@0 728
aoqi@0 729 if (func == NULL) {
aoqi@0 730 // JDK older than 1.6
aoqi@0 731 _current._partially_initialized = true;
aoqi@0 732 } else {
aoqi@0 733 (*func)(&info, sizeof(info));
aoqi@0 734
aoqi@0 735 int major = JDK_VERSION_MAJOR(info.jdk_version);
aoqi@0 736 int minor = JDK_VERSION_MINOR(info.jdk_version);
aoqi@0 737 int micro = JDK_VERSION_MICRO(info.jdk_version);
aoqi@0 738 int build = JDK_VERSION_BUILD(info.jdk_version);
aoqi@0 739 if (major == 1 && minor > 4) {
aoqi@0 740 // We represent "1.5.0" as "5.0", but 1.4.2 as itself.
aoqi@0 741 major = minor;
aoqi@0 742 minor = micro;
aoqi@0 743 micro = 0;
aoqi@0 744 }
aoqi@0 745 _current = JDK_Version(major, minor, micro, info.update_version,
aoqi@0 746 info.special_update_version, build,
aoqi@0 747 info.thread_park_blocker == 1,
aoqi@0 748 info.post_vm_init_hook_enabled == 1,
aoqi@0 749 info.pending_list_uses_discovered_field == 1);
aoqi@0 750 }
aoqi@0 751 }
aoqi@0 752
aoqi@0 753 void JDK_Version::fully_initialize(
aoqi@0 754 uint8_t major, uint8_t minor, uint8_t micro, uint8_t update) {
aoqi@0 755 // This is only called when current is less than 1.6 and we've gotten
aoqi@0 756 // far enough in the initialization to determine the exact version.
aoqi@0 757 assert(major < 6, "not needed for JDK version >= 6");
aoqi@0 758 assert(is_partially_initialized(), "must not initialize");
aoqi@0 759 if (major < 5) {
aoqi@0 760 // JDK verison sequence: 1.2.x, 1.3.x, 1.4.x, 5.0.x, 6.0.x, etc.
aoqi@0 761 micro = minor;
aoqi@0 762 minor = major;
aoqi@0 763 major = 1;
aoqi@0 764 }
aoqi@0 765 _current = JDK_Version(major, minor, micro, update);
aoqi@0 766 }
aoqi@0 767
aoqi@0 768 void JDK_Version_init() {
aoqi@0 769 JDK_Version::initialize();
aoqi@0 770 }
aoqi@0 771
aoqi@0 772 static int64_t encode_jdk_version(const JDK_Version& v) {
aoqi@0 773 return
aoqi@0 774 ((int64_t)v.major_version() << (BitsPerByte * 5)) |
aoqi@0 775 ((int64_t)v.minor_version() << (BitsPerByte * 4)) |
aoqi@0 776 ((int64_t)v.micro_version() << (BitsPerByte * 3)) |
aoqi@0 777 ((int64_t)v.update_version() << (BitsPerByte * 2)) |
aoqi@0 778 ((int64_t)v.special_update_version() << (BitsPerByte * 1)) |
aoqi@0 779 ((int64_t)v.build_number() << (BitsPerByte * 0));
aoqi@0 780 }
aoqi@0 781
aoqi@0 782 int JDK_Version::compare(const JDK_Version& other) const {
aoqi@0 783 assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
aoqi@0 784 if (!is_partially_initialized() && other.is_partially_initialized()) {
aoqi@0 785 return -(other.compare(*this)); // flip the comparators
aoqi@0 786 }
aoqi@0 787 assert(!other.is_partially_initialized(), "Not initialized yet");
aoqi@0 788 if (is_partially_initialized()) {
aoqi@0 789 assert(other.major_version() >= 6,
aoqi@0 790 "Invalid JDK version comparison during initialization");
aoqi@0 791 return -1;
aoqi@0 792 } else {
aoqi@0 793 uint64_t e = encode_jdk_version(*this);
aoqi@0 794 uint64_t o = encode_jdk_version(other);
aoqi@0 795 return (e > o) ? 1 : ((e == o) ? 0 : -1);
aoqi@0 796 }
aoqi@0 797 }
aoqi@0 798
aoqi@0 799 void JDK_Version::to_string(char* buffer, size_t buflen) const {
aoqi@0 800 size_t index = 0;
aoqi@0 801 if (!is_valid()) {
aoqi@0 802 jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
aoqi@0 803 } else if (is_partially_initialized()) {
aoqi@0 804 jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0");
aoqi@0 805 } else {
aoqi@0 806 index += jio_snprintf(
aoqi@0 807 &buffer[index], buflen - index, "%d.%d", _major, _minor);
aoqi@0 808 if (_micro > 0) {
aoqi@0 809 index += jio_snprintf(&buffer[index], buflen - index, ".%d", _micro);
aoqi@0 810 }
aoqi@0 811 if (_update > 0) {
aoqi@0 812 index += jio_snprintf(&buffer[index], buflen - index, "_%02d", _update);
aoqi@0 813 }
aoqi@0 814 if (_special > 0) {
aoqi@0 815 index += jio_snprintf(&buffer[index], buflen - index, "%c", _special);
aoqi@0 816 }
aoqi@0 817 if (_build > 0) {
aoqi@0 818 index += jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build);
aoqi@0 819 }
aoqi@0 820 }
aoqi@0 821 }

mercurial