aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "classfile/symbolTable.hpp" aoqi@0: #include "classfile/vmSymbols.hpp" aoqi@0: #include "compiler/compileBroker.hpp" aoqi@0: #include "compiler/compilerOracle.hpp" aoqi@0: #include "gc_implementation/shared/isGCActiveMark.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/symbol.hpp" aoqi@0: #include "runtime/arguments.hpp" aoqi@0: #include "runtime/deoptimization.hpp" aoqi@0: #include "runtime/interfaceSupport.hpp" aoqi@0: #include "runtime/sweeper.hpp" aoqi@0: #include "runtime/thread.inline.hpp" aoqi@0: #include "runtime/vm_operations.hpp" aoqi@0: #include "services/threadService.hpp" aoqi@0: #include "trace/tracing.hpp" aoqi@0: aoqi@0: PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC aoqi@0: aoqi@0: #define VM_OP_NAME_INITIALIZE(name) #name, aoqi@0: aoqi@0: const char* VM_Operation::_names[VM_Operation::VMOp_Terminating] = \ aoqi@0: { VM_OPS_DO(VM_OP_NAME_INITIALIZE) }; aoqi@0: aoqi@0: void VM_Operation::set_calling_thread(Thread* thread, ThreadPriority priority) { aoqi@0: _calling_thread = thread; aoqi@0: assert(MinPriority <= priority && priority <= MaxPriority, "sanity check"); aoqi@0: _priority = priority; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VM_Operation::evaluate() { aoqi@0: ResourceMark rm; aoqi@0: if (TraceVMOperation) { aoqi@0: tty->print("["); aoqi@0: NOT_PRODUCT(print();) aoqi@0: } aoqi@0: doit(); aoqi@0: if (TraceVMOperation) { aoqi@0: tty->print_cr("]"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: const char* VM_Operation::mode_to_string(Mode mode) { aoqi@0: switch(mode) { aoqi@0: case _safepoint : return "safepoint"; aoqi@0: case _no_safepoint : return "no safepoint"; aoqi@0: case _concurrent : return "concurrent"; aoqi@0: case _async_safepoint: return "async safepoint"; aoqi@0: default : return "unknown"; aoqi@0: } aoqi@0: } aoqi@0: // Called by fatal error handler. aoqi@0: void VM_Operation::print_on_error(outputStream* st) const { aoqi@0: st->print("VM_Operation (" PTR_FORMAT "): ", this); aoqi@0: st->print("%s", name()); aoqi@0: aoqi@0: const char* mode = mode_to_string(evaluation_mode()); aoqi@0: st->print(", mode: %s", mode); aoqi@0: aoqi@0: if (calling_thread()) { aoqi@0: st->print(", requested by thread " PTR_FORMAT, calling_thread()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void VM_ThreadStop::doit() { aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint"); aoqi@0: JavaThread* target = java_lang_Thread::thread(target_thread()); aoqi@0: // Note that this now allows multiple ThreadDeath exceptions to be aoqi@0: // thrown at a thread. aoqi@0: if (target != NULL) { aoqi@0: // the thread has run and is not already in the process of exiting aoqi@0: target->send_thread_stop(throwable()); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void VM_Deoptimize::doit() { aoqi@0: // We do not want any GCs to happen while we are in the middle of this VM operation aoqi@0: ResourceMark rm; aoqi@0: DeoptimizationMarker dm; aoqi@0: aoqi@0: // Deoptimize all activations depending on marked nmethods aoqi@0: Deoptimization::deoptimize_dependents(); aoqi@0: thartmann@8074: // Make the dependent methods not entrant thartmann@8074: CodeCache::make_marked_nmethods_not_entrant(); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: VM_DeoptimizeFrame::VM_DeoptimizeFrame(JavaThread* thread, intptr_t* id) { aoqi@0: _thread = thread; aoqi@0: _id = id; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VM_DeoptimizeFrame::doit() { aoqi@0: Deoptimization::deoptimize_frame_internal(_thread, _id); aoqi@0: } aoqi@0: aoqi@0: aoqi@0: #ifndef PRODUCT aoqi@0: aoqi@0: void VM_DeoptimizeAll::doit() { aoqi@0: DeoptimizationMarker dm; aoqi@0: // deoptimize all java threads in the system aoqi@0: if (DeoptimizeALot) { aoqi@0: for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) { aoqi@0: if (thread->has_last_Java_frame()) { aoqi@0: thread->deoptimize(); aoqi@0: } aoqi@0: } aoqi@0: } else if (DeoptimizeRandom) { aoqi@0: aoqi@0: // Deoptimize some selected threads and frames aoqi@0: int tnum = os::random() & 0x3; aoqi@0: int fnum = os::random() & 0x3; aoqi@0: int tcount = 0; aoqi@0: for (JavaThread* thread = Threads::first(); thread != NULL; thread = thread->next()) { aoqi@0: if (thread->has_last_Java_frame()) { aoqi@0: if (tcount++ == tnum) { aoqi@0: tcount = 0; aoqi@0: int fcount = 0; aoqi@0: // Deoptimize some selected frames. aoqi@0: // Biased llocking wants a updated register map aoqi@0: for(StackFrameStream fst(thread, UseBiasedLocking); !fst.is_done(); fst.next()) { aoqi@0: if (fst.current()->can_be_deoptimized()) { aoqi@0: if (fcount++ == fnum) { aoqi@0: fcount = 0; aoqi@0: Deoptimization::deoptimize(thread, *fst.current(), fst.register_map()); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VM_ZombieAll::doit() { aoqi@0: JavaThread *thread = (JavaThread *)calling_thread(); aoqi@0: assert(thread->is_Java_thread(), "must be a Java thread"); aoqi@0: thread->make_zombies(); aoqi@0: } aoqi@0: aoqi@0: #endif // !PRODUCT aoqi@0: aoqi@0: void VM_UnlinkSymbols::doit() { aoqi@0: JavaThread *thread = (JavaThread *)calling_thread(); aoqi@0: assert(thread->is_Java_thread(), "must be a Java thread"); aoqi@0: SymbolTable::unlink(); aoqi@0: } aoqi@0: aoqi@0: void VM_Verify::doit() { aoqi@0: Universe::heap()->prepare_for_verify(); aoqi@0: Universe::verify(_silent); aoqi@0: } aoqi@0: aoqi@0: bool VM_PrintThreads::doit_prologue() { aoqi@0: assert(Thread::current()->is_Java_thread(), "just checking"); aoqi@0: aoqi@0: // Make sure AbstractOwnableSynchronizer is loaded aoqi@0: if (JDK_Version::is_gte_jdk16x_version()) { aoqi@0: java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(JavaThread::current()); aoqi@0: } aoqi@0: aoqi@0: // Get Heap_lock if concurrent locks will be dumped aoqi@0: if (_print_concurrent_locks) { aoqi@0: Heap_lock->lock(); aoqi@0: } aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void VM_PrintThreads::doit() { aoqi@0: Threads::print_on(_out, true, false, _print_concurrent_locks); aoqi@0: } aoqi@0: aoqi@0: void VM_PrintThreads::doit_epilogue() { aoqi@0: if (_print_concurrent_locks) { aoqi@0: // Release Heap_lock aoqi@0: Heap_lock->unlock(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void VM_PrintJNI::doit() { aoqi@0: JNIHandles::print_on(_out); aoqi@0: } aoqi@0: aoqi@0: VM_FindDeadlocks::~VM_FindDeadlocks() { aoqi@0: if (_deadlocks != NULL) { aoqi@0: DeadlockCycle* cycle = _deadlocks; aoqi@0: while (cycle != NULL) { aoqi@0: DeadlockCycle* d = cycle; aoqi@0: cycle = cycle->next(); aoqi@0: delete d; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: bool VM_FindDeadlocks::doit_prologue() { aoqi@0: assert(Thread::current()->is_Java_thread(), "just checking"); aoqi@0: aoqi@0: // Load AbstractOwnableSynchronizer class aoqi@0: if (_concurrent_locks && JDK_Version::is_gte_jdk16x_version()) { aoqi@0: java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(JavaThread::current()); aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void VM_FindDeadlocks::doit() { aoqi@0: _deadlocks = ThreadService::find_deadlocks_at_safepoint(_concurrent_locks); aoqi@0: if (_out != NULL) { aoqi@0: int num_deadlocks = 0; aoqi@0: for (DeadlockCycle* cycle = _deadlocks; cycle != NULL; cycle = cycle->next()) { aoqi@0: num_deadlocks++; aoqi@0: cycle->print_on(_out); aoqi@0: } aoqi@0: aoqi@0: if (num_deadlocks == 1) { aoqi@0: _out->print_cr("\nFound 1 deadlock.\n"); aoqi@0: _out->flush(); aoqi@0: } else if (num_deadlocks > 1) { aoqi@0: _out->print_cr("\nFound %d deadlocks.\n", num_deadlocks); aoqi@0: _out->flush(); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result, aoqi@0: int max_depth, aoqi@0: bool with_locked_monitors, aoqi@0: bool with_locked_synchronizers) { aoqi@0: _result = result; aoqi@0: _num_threads = 0; // 0 indicates all threads aoqi@0: _threads = NULL; aoqi@0: _result = result; aoqi@0: _max_depth = max_depth; aoqi@0: _with_locked_monitors = with_locked_monitors; aoqi@0: _with_locked_synchronizers = with_locked_synchronizers; aoqi@0: } aoqi@0: aoqi@0: VM_ThreadDump::VM_ThreadDump(ThreadDumpResult* result, aoqi@0: GrowableArray* threads, aoqi@0: int num_threads, aoqi@0: int max_depth, aoqi@0: bool with_locked_monitors, aoqi@0: bool with_locked_synchronizers) { aoqi@0: _result = result; aoqi@0: _num_threads = num_threads; aoqi@0: _threads = threads; aoqi@0: _result = result; aoqi@0: _max_depth = max_depth; aoqi@0: _with_locked_monitors = with_locked_monitors; aoqi@0: _with_locked_synchronizers = with_locked_synchronizers; aoqi@0: } aoqi@0: aoqi@0: bool VM_ThreadDump::doit_prologue() { aoqi@0: assert(Thread::current()->is_Java_thread(), "just checking"); aoqi@0: aoqi@0: // Load AbstractOwnableSynchronizer class before taking thread snapshots aoqi@0: if (JDK_Version::is_gte_jdk16x_version()) { aoqi@0: java_util_concurrent_locks_AbstractOwnableSynchronizer::initialize(JavaThread::current()); aoqi@0: } aoqi@0: aoqi@0: if (_with_locked_synchronizers) { aoqi@0: // Acquire Heap_lock to dump concurrent locks aoqi@0: Heap_lock->lock(); aoqi@0: } aoqi@0: aoqi@0: return true; aoqi@0: } aoqi@0: aoqi@0: void VM_ThreadDump::doit_epilogue() { aoqi@0: if (_with_locked_synchronizers) { aoqi@0: // Release Heap_lock aoqi@0: Heap_lock->unlock(); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void VM_ThreadDump::doit() { aoqi@0: ResourceMark rm; aoqi@0: aoqi@0: ConcurrentLocksDump concurrent_locks(true); aoqi@0: if (_with_locked_synchronizers) { aoqi@0: concurrent_locks.dump_at_safepoint(); aoqi@0: } aoqi@0: aoqi@0: if (_num_threads == 0) { aoqi@0: // Snapshot all live threads aoqi@0: for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) { aoqi@0: if (jt->is_exiting() || aoqi@0: jt->is_hidden_from_external_view()) { aoqi@0: // skip terminating threads and hidden threads aoqi@0: continue; aoqi@0: } aoqi@0: ThreadConcurrentLocks* tcl = NULL; aoqi@0: if (_with_locked_synchronizers) { aoqi@0: tcl = concurrent_locks.thread_concurrent_locks(jt); aoqi@0: } aoqi@0: ThreadSnapshot* ts = snapshot_thread(jt, tcl); aoqi@0: _result->add_thread_snapshot(ts); aoqi@0: } aoqi@0: } else { aoqi@0: // Snapshot threads in the given _threads array aoqi@0: // A dummy snapshot is created if a thread doesn't exist aoqi@0: for (int i = 0; i < _num_threads; i++) { aoqi@0: instanceHandle th = _threads->at(i); aoqi@0: if (th() == NULL) { aoqi@0: // skip if the thread doesn't exist aoqi@0: // Add a dummy snapshot aoqi@0: _result->add_thread_snapshot(new ThreadSnapshot()); aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: // Dump thread stack only if the thread is alive and not exiting aoqi@0: // and not VM internal thread. aoqi@0: JavaThread* jt = java_lang_Thread::thread(th()); aoqi@0: if (jt == NULL || /* thread not alive */ aoqi@0: jt->is_exiting() || aoqi@0: jt->is_hidden_from_external_view()) { aoqi@0: // add a NULL snapshot if skipped aoqi@0: _result->add_thread_snapshot(new ThreadSnapshot()); aoqi@0: continue; aoqi@0: } aoqi@0: ThreadConcurrentLocks* tcl = NULL; aoqi@0: if (_with_locked_synchronizers) { aoqi@0: tcl = concurrent_locks.thread_concurrent_locks(jt); aoqi@0: } aoqi@0: ThreadSnapshot* ts = snapshot_thread(jt, tcl); aoqi@0: _result->add_thread_snapshot(ts); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: ThreadSnapshot* VM_ThreadDump::snapshot_thread(JavaThread* java_thread, ThreadConcurrentLocks* tcl) { aoqi@0: ThreadSnapshot* snapshot = new ThreadSnapshot(java_thread); aoqi@0: snapshot->dump_stack_at_safepoint(_max_depth, _with_locked_monitors); aoqi@0: snapshot->set_concurrent_locks(tcl); aoqi@0: return snapshot; aoqi@0: } aoqi@0: aoqi@0: volatile bool VM_Exit::_vm_exited = false; aoqi@0: Thread * VM_Exit::_shutdown_thread = NULL; aoqi@0: aoqi@0: int VM_Exit::set_vm_exited() { aoqi@0: Thread * thr_cur = ThreadLocalStorage::get_thread_slow(); aoqi@0: aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already"); aoqi@0: aoqi@0: int num_active = 0; aoqi@0: aoqi@0: _shutdown_thread = thr_cur; aoqi@0: _vm_exited = true; // global flag aoqi@0: for(JavaThread *thr = Threads::first(); thr != NULL; thr = thr->next()) aoqi@0: if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { aoqi@0: ++num_active; aoqi@0: thr->set_terminated(JavaThread::_vm_exited); // per-thread flag aoqi@0: } aoqi@0: aoqi@0: return num_active; aoqi@0: } aoqi@0: aoqi@0: int VM_Exit::wait_for_threads_in_native_to_block() { aoqi@0: // VM exits at safepoint. This function must be called at the final safepoint aoqi@0: // to wait for threads in _thread_in_native state to be quiescent. aoqi@0: assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint already"); aoqi@0: aoqi@0: Thread * thr_cur = ThreadLocalStorage::get_thread_slow(); aoqi@0: Monitor timer(Mutex::leaf, "VM_Exit timer", true); aoqi@0: aoqi@0: // Compiler threads need longer wait because they can access VM data directly aoqi@0: // while in native. If they are active and some structures being used are aoqi@0: // deleted by the shutdown sequence, they will crash. On the other hand, user aoqi@0: // threads must go through native=>Java/VM transitions first to access VM aoqi@0: // data, and they will be stopped during state transition. In theory, we aoqi@0: // don't have to wait for user threads to be quiescent, but it's always aoqi@0: // better to terminate VM when current thread is the only active thread, so aoqi@0: // wait for user threads too. Numbers are in 10 milliseconds. aoqi@0: int max_wait_user_thread = 30; // at least 300 milliseconds aoqi@0: int max_wait_compiler_thread = 1000; // at least 10 seconds aoqi@0: aoqi@0: int max_wait = max_wait_compiler_thread; aoqi@0: aoqi@0: int attempts = 0; aoqi@0: while (true) { aoqi@0: int num_active = 0; aoqi@0: int num_active_compiler_thread = 0; aoqi@0: aoqi@0: for(JavaThread *thr = Threads::first(); thr != NULL; thr = thr->next()) { aoqi@0: if (thr!=thr_cur && thr->thread_state() == _thread_in_native) { aoqi@0: num_active++; aoqi@0: if (thr->is_Compiler_thread()) { aoqi@0: num_active_compiler_thread++; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: if (num_active == 0) { aoqi@0: return 0; aoqi@0: } else if (attempts > max_wait) { aoqi@0: return num_active; aoqi@0: } else if (num_active_compiler_thread == 0 && attempts > max_wait_user_thread) { aoqi@0: return num_active; aoqi@0: } aoqi@0: aoqi@0: attempts++; aoqi@0: aoqi@0: MutexLockerEx ml(&timer, Mutex::_no_safepoint_check_flag); aoqi@0: timer.wait(Mutex::_no_safepoint_check_flag, 10); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: void VM_Exit::doit() { aoqi@0: CompileBroker::set_should_block(); aoqi@0: aoqi@0: // Wait for a short period for threads in native to block. Any thread aoqi@0: // still executing native code after the wait will be stopped at aoqi@0: // native==>Java/VM barriers. aoqi@0: // Among 16276 JCK tests, 94% of them come here without any threads still aoqi@0: // running in native; the other 6% are quiescent within 250ms (Ultra 80). aoqi@0: wait_for_threads_in_native_to_block(); aoqi@0: aoqi@0: set_vm_exited(); aoqi@0: aoqi@0: // cleanup globals resources before exiting. exit_globals() currently aoqi@0: // cleans up outputStream resources and PerfMemory resources. aoqi@0: exit_globals(); aoqi@0: aoqi@0: // Check for exit hook aoqi@0: exit_hook_t exit_hook = Arguments::exit_hook(); aoqi@0: if (exit_hook != NULL) { aoqi@0: // exit hook should exit. aoqi@0: exit_hook(_exit_code); aoqi@0: // ... but if it didn't, we must do it here aoqi@0: vm_direct_exit(_exit_code); aoqi@0: } else { aoqi@0: vm_direct_exit(_exit_code); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: void VM_Exit::wait_if_vm_exited() { aoqi@0: if (_vm_exited && aoqi@0: ThreadLocalStorage::get_thread_slow() != _shutdown_thread) { aoqi@0: // _vm_exited is set at safepoint, and the Threads_lock is never released aoqi@0: // we will block here until the process dies aoqi@0: Threads_lock->lock_without_safepoint_check(); aoqi@0: ShouldNotReachHere(); aoqi@0: } aoqi@0: }