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