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