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