Wed, 17 Sep 2008 19:59:35 +0400
Merge
1 /*
2 * Copyright 1997-2008 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
25 #include "incls/_precompiled.incl"
26 #include "incls/_java.cpp.incl"
28 HS_DTRACE_PROBE_DECL(hotspot, vm__shutdown);
30 #ifndef PRODUCT
32 // Statistics printing (method invocation histogram)
34 GrowableArray<methodOop>* collected_invoked_methods;
36 void collect_invoked_methods(methodOop m) {
37 if (m->invocation_count() + m->compiled_invocation_count() >= 1 ) {
38 collected_invoked_methods->push(m);
39 }
40 }
43 GrowableArray<methodOop>* collected_profiled_methods;
45 void collect_profiled_methods(methodOop m) {
46 methodHandle mh(Thread::current(), m);
47 if ((m->method_data() != NULL) &&
48 (PrintMethodData || CompilerOracle::should_print(mh))) {
49 collected_profiled_methods->push(m);
50 }
51 }
54 int compare_methods(methodOop* a, methodOop* b) {
55 // %%% there can be 32-bit overflow here
56 return ((*b)->invocation_count() + (*b)->compiled_invocation_count())
57 - ((*a)->invocation_count() + (*a)->compiled_invocation_count());
58 }
61 void print_method_invocation_histogram() {
62 ResourceMark rm;
63 HandleMark hm;
64 collected_invoked_methods = new GrowableArray<methodOop>(1024);
65 SystemDictionary::methods_do(collect_invoked_methods);
66 collected_invoked_methods->sort(&compare_methods);
67 //
68 tty->cr();
69 tty->print_cr("Histogram Over MethodOop Invocation Counters (cutoff = %d):", MethodHistogramCutoff);
70 tty->cr();
71 tty->print_cr("____Count_(I+C)____Method________________________Module_________________");
72 unsigned total = 0, int_total = 0, comp_total = 0, static_total = 0, final_total = 0,
73 synch_total = 0, nativ_total = 0, acces_total = 0;
74 for (int index = 0; index < collected_invoked_methods->length(); index++) {
75 methodOop m = collected_invoked_methods->at(index);
76 int c = m->invocation_count() + m->compiled_invocation_count();
77 if (c >= MethodHistogramCutoff) m->print_invocation_count();
78 int_total += m->invocation_count();
79 comp_total += m->compiled_invocation_count();
80 if (m->is_final()) final_total += c;
81 if (m->is_static()) static_total += c;
82 if (m->is_synchronized()) synch_total += c;
83 if (m->is_native()) nativ_total += c;
84 if (m->is_accessor()) acces_total += c;
85 }
86 tty->cr();
87 total = int_total + comp_total;
88 tty->print_cr("Invocations summary:");
89 tty->print_cr("\t%9d (%4.1f%%) interpreted", int_total, 100.0 * int_total / total);
90 tty->print_cr("\t%9d (%4.1f%%) compiled", comp_total, 100.0 * comp_total / total);
91 tty->print_cr("\t%9d (100%%) total", total);
92 tty->print_cr("\t%9d (%4.1f%%) synchronized", synch_total, 100.0 * synch_total / total);
93 tty->print_cr("\t%9d (%4.1f%%) final", final_total, 100.0 * final_total / total);
94 tty->print_cr("\t%9d (%4.1f%%) static", static_total, 100.0 * static_total / total);
95 tty->print_cr("\t%9d (%4.1f%%) native", nativ_total, 100.0 * nativ_total / total);
96 tty->print_cr("\t%9d (%4.1f%%) accessor", acces_total, 100.0 * acces_total / total);
97 tty->cr();
98 SharedRuntime::print_call_statistics(comp_total);
99 }
101 void print_method_profiling_data() {
102 ResourceMark rm;
103 HandleMark hm;
104 collected_profiled_methods = new GrowableArray<methodOop>(1024);
105 SystemDictionary::methods_do(collect_profiled_methods);
106 collected_profiled_methods->sort(&compare_methods);
108 int count = collected_profiled_methods->length();
109 if (count > 0) {
110 for (int index = 0; index < count; index++) {
111 methodOop m = collected_profiled_methods->at(index);
112 ttyLocker ttyl;
113 tty->print_cr("------------------------------------------------------------------------");
114 //m->print_name(tty);
115 m->print_invocation_count();
116 tty->cr();
117 m->print_codes();
118 }
119 tty->print_cr("------------------------------------------------------------------------");
120 }
121 }
123 void print_bytecode_count() {
124 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
125 tty->print_cr("[BytecodeCounter::counter_value = %d]", BytecodeCounter::counter_value());
126 }
127 }
129 AllocStats alloc_stats;
133 // General statistics printing (profiling ...)
135 void print_statistics() {
137 #ifdef ASSERT
139 if (CountRuntimeCalls) {
140 extern Histogram *RuntimeHistogram;
141 RuntimeHistogram->print();
142 }
144 if (CountJNICalls) {
145 extern Histogram *JNIHistogram;
146 JNIHistogram->print();
147 }
149 if (CountJVMCalls) {
150 extern Histogram *JVMHistogram;
151 JVMHistogram->print();
152 }
154 #endif
156 if (MemProfiling) {
157 MemProfiler::disengage();
158 }
160 if (CITime) {
161 CompileBroker::print_times();
162 }
164 #ifdef COMPILER1
165 if ((PrintC1Statistics || LogVMOutput || LogCompilation) && UseCompiler) {
166 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintC1Statistics);
167 Runtime1::print_statistics();
168 Deoptimization::print_statistics();
169 nmethod::print_statistics();
170 }
171 #endif /* COMPILER1 */
173 #ifdef COMPILER2
174 if ((PrintOptoStatistics || LogVMOutput || LogCompilation) && UseCompiler) {
175 FlagSetting fs(DisplayVMOutput, DisplayVMOutput && PrintOptoStatistics);
176 Compile::print_statistics();
177 #ifndef COMPILER1
178 Deoptimization::print_statistics();
179 nmethod::print_statistics();
180 #endif //COMPILER1
181 SharedRuntime::print_statistics();
182 os::print_statistics();
183 }
185 if (PrintLockStatistics || PrintPreciseBiasedLockingStatistics) {
186 OptoRuntime::print_named_counters();
187 }
189 if (TimeLivenessAnalysis) {
190 MethodLiveness::print_times();
191 }
192 #ifdef ASSERT
193 if (CollectIndexSetStatistics) {
194 IndexSet::print_statistics();
195 }
196 #endif // ASSERT
197 #endif // COMPILER2
198 if (CountCompiledCalls) {
199 print_method_invocation_histogram();
200 }
201 if (ProfileInterpreter || Tier1UpdateMethodData) {
202 print_method_profiling_data();
203 }
204 if (TimeCompiler) {
205 COMPILER2_PRESENT(Compile::print_timers();)
206 }
207 if (TimeCompilationPolicy) {
208 CompilationPolicy::policy()->print_time();
209 }
210 if (TimeOopMap) {
211 GenerateOopMap::print_time();
212 }
213 if (ProfilerCheckIntervals) {
214 PeriodicTask::print_intervals();
215 }
216 if (PrintSymbolTableSizeHistogram) {
217 SymbolTable::print_histogram();
218 }
219 if (CountBytecodes || TraceBytecodes || StopInterpreterAt) {
220 BytecodeCounter::print();
221 }
222 if (PrintBytecodePairHistogram) {
223 BytecodePairHistogram::print();
224 }
226 if (PrintCodeCache) {
227 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
228 CodeCache::print();
229 }
231 if (PrintCodeCache2) {
232 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
233 CodeCache::print_internals();
234 }
236 if (PrintClassStatistics) {
237 SystemDictionary::print_class_statistics();
238 }
239 if (PrintMethodStatistics) {
240 SystemDictionary::print_method_statistics();
241 }
243 if (PrintVtableStats) {
244 klassVtable::print_statistics();
245 klassItable::print_statistics();
246 }
247 if (VerifyOops) {
248 tty->print_cr("+VerifyOops count: %d", StubRoutines::verify_oop_count());
249 }
251 print_bytecode_count();
252 if (WizardMode) {
253 tty->print("allocation stats: ");
254 alloc_stats.print();
255 tty->cr();
256 }
258 if (PrintSystemDictionaryAtExit) {
259 SystemDictionary::print();
260 }
262 if (PrintBiasedLockingStatistics) {
263 BiasedLocking::print_counters();
264 }
266 #ifdef ENABLE_ZAP_DEAD_LOCALS
267 #ifdef COMPILER2
268 if (ZapDeadCompiledLocals) {
269 tty->print_cr("Compile::CompiledZap_count = %d", Compile::CompiledZap_count);
270 tty->print_cr("OptoRuntime::ZapDeadCompiledLocals_count = %d", OptoRuntime::ZapDeadCompiledLocals_count);
271 }
272 #endif // COMPILER2
273 #endif // ENABLE_ZAP_DEAD_LOCALS
274 }
276 #else // PRODUCT MODE STATISTICS
278 void print_statistics() {
280 if (CITime) {
281 CompileBroker::print_times();
282 }
283 #ifdef COMPILER2
284 if (PrintPreciseBiasedLockingStatistics) {
285 OptoRuntime::print_named_counters();
286 }
287 #endif
288 if (PrintBiasedLockingStatistics) {
289 BiasedLocking::print_counters();
290 }
291 }
293 #endif
296 // Helper class for registering on_exit calls through JVM_OnExit
298 extern "C" {
299 typedef void (*__exit_proc)(void);
300 }
302 class ExitProc : public CHeapObj {
303 private:
304 __exit_proc _proc;
305 // void (*_proc)(void);
306 ExitProc* _next;
307 public:
308 // ExitProc(void (*proc)(void)) {
309 ExitProc(__exit_proc proc) {
310 _proc = proc;
311 _next = NULL;
312 }
313 void evaluate() { _proc(); }
314 ExitProc* next() const { return _next; }
315 void set_next(ExitProc* next) { _next = next; }
316 };
319 // Linked list of registered on_exit procedures
321 static ExitProc* exit_procs = NULL;
324 extern "C" {
325 void register_on_exit_function(void (*func)(void)) {
326 ExitProc *entry = new ExitProc(func);
327 // Classic vm does not throw an exception in case the allocation failed,
328 if (entry != NULL) {
329 entry->set_next(exit_procs);
330 exit_procs = entry;
331 }
332 }
333 }
335 // Note: before_exit() can be executed only once, if more than one threads
336 // are trying to shutdown the VM at the same time, only one thread
337 // can run before_exit() and all other threads must wait.
338 void before_exit(JavaThread * thread) {
339 #define BEFORE_EXIT_NOT_RUN 0
340 #define BEFORE_EXIT_RUNNING 1
341 #define BEFORE_EXIT_DONE 2
342 static jint volatile _before_exit_status = BEFORE_EXIT_NOT_RUN;
344 // Note: don't use a Mutex to guard the entire before_exit(), as
345 // JVMTI post_thread_end_event and post_vm_death_event will run native code.
346 // A CAS or OSMutex would work just fine but then we need to manipulate
347 // thread state for Safepoint. Here we use Monitor wait() and notify_all()
348 // for synchronization.
349 { MutexLocker ml(BeforeExit_lock);
350 switch (_before_exit_status) {
351 case BEFORE_EXIT_NOT_RUN:
352 _before_exit_status = BEFORE_EXIT_RUNNING;
353 break;
354 case BEFORE_EXIT_RUNNING:
355 while (_before_exit_status == BEFORE_EXIT_RUNNING) {
356 BeforeExit_lock->wait();
357 }
358 assert(_before_exit_status == BEFORE_EXIT_DONE, "invalid state");
359 return;
360 case BEFORE_EXIT_DONE:
361 return;
362 }
363 }
365 // The only difference between this and Win32's _onexit procs is that
366 // this version is invoked before any threads get killed.
367 ExitProc* current = exit_procs;
368 while (current != NULL) {
369 ExitProc* next = current->next();
370 current->evaluate();
371 delete current;
372 current = next;
373 }
375 // Hang forever on exit if we're reporting an error.
376 if (ShowMessageBoxOnError && is_error_reported()) {
377 os::infinite_sleep();
378 }
380 // Terminate watcher thread - must before disenrolling any periodic task
381 WatcherThread::stop();
383 // Print statistics gathered (profiling ...)
384 if (Arguments::has_profile()) {
385 FlatProfiler::disengage();
386 FlatProfiler::print(10);
387 }
389 // shut down the StatSampler task
390 StatSampler::disengage();
391 StatSampler::destroy();
393 #ifndef SERIALGC
394 // stop CMS threads
395 if (UseConcMarkSweepGC) {
396 ConcurrentMarkSweepThread::stop();
397 }
398 #endif // SERIALGC
400 // Print GC/heap related information.
401 if (PrintGCDetails) {
402 Universe::print();
403 AdaptiveSizePolicyOutput(0);
404 }
407 if (Arguments::has_alloc_profile()) {
408 HandleMark hm;
409 // Do one last collection to enumerate all the objects
410 // allocated since the last one.
411 Universe::heap()->collect(GCCause::_allocation_profiler);
412 AllocationProfiler::disengage();
413 AllocationProfiler::print(0);
414 }
416 if (PrintBytecodeHistogram) {
417 BytecodeHistogram::print();
418 }
420 if (JvmtiExport::should_post_thread_life()) {
421 JvmtiExport::post_thread_end(thread);
422 }
423 // Always call even when there are not JVMTI environments yet, since environments
424 // may be attached late and JVMTI must track phases of VM execution
425 JvmtiExport::post_vm_death();
426 Threads::shutdown_vm_agents();
428 // Terminate the signal thread
429 // Note: we don't wait until it actually dies.
430 os::terminate_signal_thread();
432 print_statistics();
433 Universe::heap()->print_tracing_info();
435 VTune::exit();
437 { MutexLocker ml(BeforeExit_lock);
438 _before_exit_status = BEFORE_EXIT_DONE;
439 BeforeExit_lock->notify_all();
440 }
442 #undef BEFORE_EXIT_NOT_RUN
443 #undef BEFORE_EXIT_RUNNING
444 #undef BEFORE_EXIT_DONE
445 }
447 void vm_exit(int code) {
448 Thread* thread = ThreadLocalStorage::thread_index() == -1 ? NULL
449 : ThreadLocalStorage::get_thread_slow();
450 if (thread == NULL) {
451 // we have serious problems -- just exit
452 vm_direct_exit(code);
453 }
455 if (VMThread::vm_thread() != NULL) {
456 // Fire off a VM_Exit operation to bring VM to a safepoint and exit
457 VM_Exit op(code);
458 if (thread->is_Java_thread())
459 ((JavaThread*)thread)->set_thread_state(_thread_in_vm);
460 VMThread::execute(&op);
461 // should never reach here; but in case something wrong with VM Thread.
462 vm_direct_exit(code);
463 } else {
464 // VM thread is gone, just exit
465 vm_direct_exit(code);
466 }
467 ShouldNotReachHere();
468 }
470 void notify_vm_shutdown() {
471 // For now, just a dtrace probe.
472 HS_DTRACE_PROBE(hotspot, vm__shutdown);
473 }
475 void vm_direct_exit(int code) {
476 notify_vm_shutdown();
477 ::exit(code);
478 }
480 void vm_perform_shutdown_actions() {
481 // Warning: do not call 'exit_globals()' here. All threads are still running.
482 // Calling 'exit_globals()' will disable thread-local-storage and cause all
483 // kinds of assertions to trigger in debug mode.
484 if (is_init_completed()) {
485 Thread* thread = Thread::current();
486 if (thread->is_Java_thread()) {
487 // We are leaving the VM, set state to native (in case any OS exit
488 // handlers call back to the VM)
489 JavaThread* jt = (JavaThread*)thread;
490 // Must always be walkable or have no last_Java_frame when in
491 // thread_in_native
492 jt->frame_anchor()->make_walkable(jt);
493 jt->set_thread_state(_thread_in_native);
494 }
495 }
496 notify_vm_shutdown();
497 }
499 void vm_shutdown()
500 {
501 vm_perform_shutdown_actions();
502 os::shutdown();
503 }
505 void vm_abort(bool dump_core) {
506 vm_perform_shutdown_actions();
507 os::abort(dump_core);
508 ShouldNotReachHere();
509 }
511 void vm_notify_during_shutdown(const char* error, const char* message) {
512 if (error != NULL) {
513 tty->print_cr("Error occurred during initialization of VM");
514 tty->print("%s", error);
515 if (message != NULL) {
516 tty->print_cr(": %s", message);
517 }
518 else {
519 tty->cr();
520 }
521 }
522 if (ShowMessageBoxOnError && WizardMode) {
523 fatal("Error occurred during initialization of VM");
524 }
525 }
527 void vm_exit_during_initialization(Handle exception) {
528 tty->print_cr("Error occurred during initialization of VM");
529 // If there are exceptions on this thread it must be cleared
530 // first and here. Any future calls to EXCEPTION_MARK requires
531 // that no pending exceptions exist.
532 Thread *THREAD = Thread::current();
533 if (HAS_PENDING_EXCEPTION) {
534 CLEAR_PENDING_EXCEPTION;
535 }
536 java_lang_Throwable::print(exception, tty);
537 tty->cr();
538 java_lang_Throwable::print_stack_trace(exception(), tty);
539 tty->cr();
540 vm_notify_during_shutdown(NULL, NULL);
542 // Failure during initialization, we don't want to dump core
543 vm_abort(false);
544 }
546 void vm_exit_during_initialization(symbolHandle ex, const char* message) {
547 ResourceMark rm;
548 vm_notify_during_shutdown(ex->as_C_string(), message);
550 // Failure during initialization, we don't want to dump core
551 vm_abort(false);
552 }
554 void vm_exit_during_initialization(const char* error, const char* message) {
555 vm_notify_during_shutdown(error, message);
557 // Failure during initialization, we don't want to dump core
558 vm_abort(false);
559 }
561 void vm_shutdown_during_initialization(const char* error, const char* message) {
562 vm_notify_during_shutdown(error, message);
563 vm_shutdown();
564 }
566 JDK_Version JDK_Version::_current;
568 void JDK_Version::initialize() {
569 jdk_version_info info;
570 assert(!_current.is_valid(), "Don't initialize twice");
572 void *lib_handle = os::native_java_library();
573 jdk_version_info_fn_t func = CAST_TO_FN_PTR(jdk_version_info_fn_t,
574 os::dll_lookup(lib_handle, "JDK_GetVersionInfo0"));
576 if (func == NULL) {
577 // JDK older than 1.6
578 _current._partially_initialized = true;
579 } else {
580 (*func)(&info, sizeof(info));
582 int major = JDK_VERSION_MAJOR(info.jdk_version);
583 int minor = JDK_VERSION_MINOR(info.jdk_version);
584 int micro = JDK_VERSION_MICRO(info.jdk_version);
585 int build = JDK_VERSION_BUILD(info.jdk_version);
586 if (major == 1 && minor > 4) {
587 // We represent "1.5.0" as "5.0", but 1.4.2 as itself.
588 major = minor;
589 minor = micro;
590 micro = 0;
591 }
592 _current = JDK_Version(major, minor, micro, info.update_version,
593 info.special_update_version, build,
594 info.thread_park_blocker == 1);
595 }
596 }
598 void JDK_Version::fully_initialize(
599 uint8_t major, uint8_t minor, uint8_t micro, uint8_t update) {
600 // This is only called when current is less than 1.6 and we've gotten
601 // far enough in the initialization to determine the exact version.
602 assert(major < 6, "not needed for JDK version >= 6");
603 assert(is_partially_initialized(), "must not initialize");
604 if (major < 5) {
605 // JDK verison sequence: 1.2.x, 1.3.x, 1.4.x, 5.0.x, 6.0.x, etc.
606 micro = minor;
607 minor = major;
608 major = 1;
609 }
610 _current = JDK_Version(major, minor, micro, update);
611 }
613 void JDK_Version_init() {
614 JDK_Version::initialize();
615 }
617 static int64_t encode_jdk_version(const JDK_Version& v) {
618 return
619 ((int64_t)v.major_version() << (BitsPerByte * 5)) |
620 ((int64_t)v.minor_version() << (BitsPerByte * 4)) |
621 ((int64_t)v.micro_version() << (BitsPerByte * 3)) |
622 ((int64_t)v.update_version() << (BitsPerByte * 2)) |
623 ((int64_t)v.special_update_version() << (BitsPerByte * 1)) |
624 ((int64_t)v.build_number() << (BitsPerByte * 0));
625 }
627 int JDK_Version::compare(const JDK_Version& other) const {
628 assert(is_valid() && other.is_valid(), "Invalid version (uninitialized?)");
629 if (!is_partially_initialized() && other.is_partially_initialized()) {
630 return -(other.compare(*this)); // flip the comparators
631 }
632 assert(!other.is_partially_initialized(), "Not initialized yet");
633 if (is_partially_initialized()) {
634 assert(other.major_version() >= 6,
635 "Invalid JDK version comparison during initialization");
636 return -1;
637 } else {
638 uint64_t e = encode_jdk_version(*this);
639 uint64_t o = encode_jdk_version(other);
640 return (e > o) ? 1 : ((e == o) ? 0 : -1);
641 }
642 }
644 void JDK_Version::to_string(char* buffer, size_t buflen) const {
645 size_t index = 0;
646 if (!is_valid()) {
647 jio_snprintf(buffer, buflen, "%s", "(uninitialized)");
648 } else if (is_partially_initialized()) {
649 jio_snprintf(buffer, buflen, "%s", "(uninitialized) pre-1.6.0");
650 } else {
651 index += jio_snprintf(
652 &buffer[index], buflen - index, "%d.%d", _major, _minor);
653 if (_micro > 0) {
654 index += jio_snprintf(&buffer[index], buflen - index, ".%d", _micro);
655 }
656 if (_update > 0) {
657 index += jio_snprintf(&buffer[index], buflen - index, "_%02d", _update);
658 }
659 if (_special > 0) {
660 index += jio_snprintf(&buffer[index], buflen - index, "%c", _special);
661 }
662 if (_build > 0) {
663 index += jio_snprintf(&buffer[index], buflen - index, "-b%02d", _build);
664 }
665 }
666 }