duke@435: /* xdono@1383: * Copyright 2003-2009 Sun Microsystems, Inc. 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: * duke@435: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@435: * CA 95054 USA or visit www.sun.com if you need additional information or duke@435: * have any questions. duke@435: * duke@435: */ duke@435: duke@435: # include "incls/_precompiled.incl" duke@435: # include "incls/_threadService.cpp.incl" duke@435: duke@435: // TODO: we need to define a naming convention for perf counters duke@435: // to distinguish counters for: duke@435: // - standard JSR174 use duke@435: // - Hotspot extension (public and committed) duke@435: // - Hotspot extension (private/internal and uncommitted) duke@435: duke@435: // Default is disabled. duke@435: bool ThreadService::_thread_monitoring_contention_enabled = false; duke@435: bool ThreadService::_thread_cpu_time_enabled = false; duke@435: duke@435: PerfCounter* ThreadService::_total_threads_count = NULL; duke@435: PerfVariable* ThreadService::_live_threads_count = NULL; duke@435: PerfVariable* ThreadService::_peak_threads_count = NULL; duke@435: PerfVariable* ThreadService::_daemon_threads_count = NULL; duke@435: volatile int ThreadService::_exiting_threads_count = 0; duke@435: volatile int ThreadService::_exiting_daemon_threads_count = 0; duke@435: duke@435: ThreadDumpResult* ThreadService::_threaddump_list = NULL; duke@435: duke@435: static const int INITIAL_ARRAY_SIZE = 10; duke@435: duke@435: void ThreadService::init() { duke@435: EXCEPTION_MARK; duke@435: duke@435: // These counters are for java.lang.management API support. duke@435: // They are created even if -XX:-UsePerfData is set and in duke@435: // that case, they will be allocated on C heap. duke@435: duke@435: _total_threads_count = duke@435: PerfDataManager::create_counter(JAVA_THREADS, "started", duke@435: PerfData::U_Events, CHECK); duke@435: duke@435: _live_threads_count = duke@435: PerfDataManager::create_variable(JAVA_THREADS, "live", duke@435: PerfData::U_None, CHECK); duke@435: duke@435: _peak_threads_count = duke@435: PerfDataManager::create_variable(JAVA_THREADS, "livePeak", duke@435: PerfData::U_None, CHECK); duke@435: duke@435: _daemon_threads_count = duke@435: PerfDataManager::create_variable(JAVA_THREADS, "daemon", duke@435: PerfData::U_None, CHECK); duke@435: duke@435: if (os::is_thread_cpu_time_supported()) { duke@435: _thread_cpu_time_enabled = true; duke@435: } duke@435: } duke@435: duke@435: void ThreadService::reset_peak_thread_count() { duke@435: // Acquire the lock to update the peak thread count duke@435: // to synchronize with thread addition and removal. duke@435: MutexLockerEx mu(Threads_lock); duke@435: _peak_threads_count->set_value(get_live_thread_count()); duke@435: } duke@435: duke@435: void ThreadService::add_thread(JavaThread* thread, bool daemon) { duke@435: // Do not count VM internal or JVMTI agent threads duke@435: if (thread->is_hidden_from_external_view() || duke@435: thread->is_jvmti_agent_thread()) { duke@435: return; duke@435: } duke@435: duke@435: _total_threads_count->inc(); duke@435: _live_threads_count->inc(); duke@435: duke@435: if (_live_threads_count->get_value() > _peak_threads_count->get_value()) { duke@435: _peak_threads_count->set_value(_live_threads_count->get_value()); duke@435: } duke@435: duke@435: if (daemon) { duke@435: _daemon_threads_count->inc(); duke@435: } duke@435: } duke@435: duke@435: void ThreadService::remove_thread(JavaThread* thread, bool daemon) { duke@435: Atomic::dec((jint*) &_exiting_threads_count); duke@435: duke@435: if (thread->is_hidden_from_external_view() || duke@435: thread->is_jvmti_agent_thread()) { duke@435: return; duke@435: } duke@435: duke@435: _live_threads_count->set_value(_live_threads_count->get_value() - 1); duke@435: duke@435: if (daemon) { duke@435: _daemon_threads_count->set_value(_daemon_threads_count->get_value() - 1); duke@435: Atomic::dec((jint*) &_exiting_daemon_threads_count); duke@435: } duke@435: } duke@435: duke@435: void ThreadService::current_thread_exiting(JavaThread* jt) { duke@435: assert(jt == JavaThread::current(), "Called by current thread"); duke@435: Atomic::inc((jint*) &_exiting_threads_count); duke@435: duke@435: oop threadObj = jt->threadObj(); duke@435: if (threadObj != NULL && java_lang_Thread::is_daemon(threadObj)) { duke@435: Atomic::inc((jint*) &_exiting_daemon_threads_count); duke@435: } duke@435: } duke@435: duke@435: // FIXME: JVMTI should call this function duke@435: Handle ThreadService::get_current_contended_monitor(JavaThread* thread) { duke@435: assert(thread != NULL, "should be non-NULL"); duke@435: assert(Threads_lock->owned_by_self(), "must grab Threads_lock or be at safepoint"); duke@435: duke@435: ObjectMonitor *wait_obj = thread->current_waiting_monitor(); duke@435: duke@435: oop obj = NULL; duke@435: if (wait_obj != NULL) { duke@435: // thread is doing an Object.wait() call duke@435: obj = (oop) wait_obj->object(); duke@435: assert(obj != NULL, "Object.wait() should have an object"); duke@435: } else { duke@435: ObjectMonitor *enter_obj = thread->current_pending_monitor(); duke@435: if (enter_obj != NULL) { duke@435: // thread is trying to enter() or raw_enter() an ObjectMonitor. duke@435: obj = (oop) enter_obj->object(); duke@435: } duke@435: // If obj == NULL, then ObjectMonitor is raw which doesn't count. duke@435: } duke@435: duke@435: Handle h(obj); duke@435: return h; duke@435: } duke@435: duke@435: bool ThreadService::set_thread_monitoring_contention(bool flag) { duke@435: MutexLocker m(Management_lock); duke@435: duke@435: bool prev = _thread_monitoring_contention_enabled; duke@435: _thread_monitoring_contention_enabled = flag; duke@435: duke@435: return prev; duke@435: } duke@435: duke@435: bool ThreadService::set_thread_cpu_time_enabled(bool flag) { duke@435: MutexLocker m(Management_lock); duke@435: duke@435: bool prev = _thread_cpu_time_enabled; duke@435: _thread_cpu_time_enabled = flag; duke@435: duke@435: return prev; duke@435: } duke@435: duke@435: // GC support duke@435: void ThreadService::oops_do(OopClosure* f) { duke@435: for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) { duke@435: dump->oops_do(f); duke@435: } duke@435: } duke@435: duke@435: void ThreadService::add_thread_dump(ThreadDumpResult* dump) { duke@435: MutexLocker ml(Management_lock); duke@435: if (_threaddump_list == NULL) { duke@435: _threaddump_list = dump; duke@435: } else { duke@435: dump->set_next(_threaddump_list); duke@435: _threaddump_list = dump; duke@435: } duke@435: } duke@435: duke@435: void ThreadService::remove_thread_dump(ThreadDumpResult* dump) { duke@435: MutexLocker ml(Management_lock); duke@435: duke@435: ThreadDumpResult* prev = NULL; duke@435: bool found = false; duke@435: for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) { duke@435: if (d == dump) { duke@435: if (prev == NULL) { duke@435: _threaddump_list = dump->next(); duke@435: } else { duke@435: prev->set_next(dump->next()); duke@435: } duke@435: found = true; duke@435: break; duke@435: } duke@435: } duke@435: assert(found, "The threaddump result to be removed must exist."); duke@435: } duke@435: duke@435: // Dump stack trace of threads specified in the given threads array. duke@435: // Returns StackTraceElement[][] each element is the stack trace of a thread in duke@435: // the corresponding entry in the given threads array duke@435: Handle ThreadService::dump_stack_traces(GrowableArray* threads, duke@435: int num_threads, duke@435: TRAPS) { duke@435: assert(num_threads > 0, "just checking"); duke@435: duke@435: ThreadDumpResult dump_result; duke@435: VM_ThreadDump op(&dump_result, duke@435: threads, duke@435: num_threads, duke@435: -1, /* entire stack */ duke@435: false, /* with locked monitors */ duke@435: false /* with locked synchronizers */); duke@435: VMThread::execute(&op); duke@435: duke@435: // Allocate the resulting StackTraceElement[][] object duke@435: duke@435: ResourceMark rm(THREAD); duke@435: klassOop k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_StackTraceElement_array(), true, CHECK_NH); duke@435: objArrayKlassHandle ik (THREAD, k); duke@435: objArrayOop r = oopFactory::new_objArray(ik(), num_threads, CHECK_NH); duke@435: objArrayHandle result_obj(THREAD, r); duke@435: duke@435: int num_snapshots = dump_result.num_snapshots(); duke@435: assert(num_snapshots == num_threads, "Must have num_threads thread snapshots"); duke@435: int i = 0; duke@435: for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) { duke@435: ThreadStackTrace* stacktrace = ts->get_stack_trace(); duke@435: if (stacktrace == NULL) { duke@435: // No stack trace duke@435: result_obj->obj_at_put(i, NULL); duke@435: } else { duke@435: // Construct an array of java/lang/StackTraceElement object duke@435: Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH); duke@435: result_obj->obj_at_put(i, backtrace_h()); duke@435: } duke@435: } duke@435: duke@435: return result_obj; duke@435: } duke@435: duke@435: void ThreadService::reset_contention_count_stat(JavaThread* thread) { duke@435: ThreadStatistics* stat = thread->get_thread_stat(); duke@435: if (stat != NULL) { duke@435: stat->reset_count_stat(); duke@435: } duke@435: } duke@435: duke@435: void ThreadService::reset_contention_time_stat(JavaThread* thread) { duke@435: ThreadStatistics* stat = thread->get_thread_stat(); duke@435: if (stat != NULL) { duke@435: stat->reset_time_stat(); duke@435: } duke@435: } duke@435: duke@435: // Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true duke@435: DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(bool concurrent_locks) { duke@435: // This code was modified from the original Threads::find_deadlocks code. duke@435: int globalDfn = 0, thisDfn; duke@435: ObjectMonitor* waitingToLockMonitor = NULL; duke@435: oop waitingToLockBlocker = NULL; duke@435: bool blocked_on_monitor = false; duke@435: JavaThread *currentThread, *previousThread; duke@435: int num_deadlocks = 0; duke@435: duke@435: for (JavaThread* p = Threads::first(); p != NULL; p = p->next()) { duke@435: // Initialize the depth-first-number duke@435: p->set_depth_first_number(-1); duke@435: } duke@435: duke@435: DeadlockCycle* deadlocks = NULL; duke@435: DeadlockCycle* last = NULL; duke@435: DeadlockCycle* cycle = new DeadlockCycle(); duke@435: for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) { duke@435: if (jt->depth_first_number() >= 0) { duke@435: // this thread was already visited duke@435: continue; duke@435: } duke@435: duke@435: thisDfn = globalDfn; duke@435: jt->set_depth_first_number(globalDfn++); duke@435: previousThread = jt; duke@435: currentThread = jt; duke@435: duke@435: cycle->reset(); duke@435: duke@435: // When there is a deadlock, all the monitors involved in the dependency duke@435: // cycle must be contended and heavyweight. So we only care about the duke@435: // heavyweight monitor a thread is waiting to lock. duke@435: waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor(); duke@435: if (concurrent_locks) { duke@435: waitingToLockBlocker = jt->current_park_blocker(); duke@435: } duke@435: while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) { duke@435: cycle->add_thread(currentThread); duke@435: if (waitingToLockMonitor != NULL) { duke@435: currentThread = Threads::owning_thread_from_monitor_owner((address)waitingToLockMonitor->owner(), duke@435: false /* no locking needed */); duke@435: } else { duke@435: if (concurrent_locks) { duke@435: if (waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) { duke@435: oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker); duke@435: currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL; duke@435: } else { duke@435: currentThread = NULL; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (currentThread == NULL) { duke@435: // No dependency on another thread duke@435: break; duke@435: } duke@435: if (currentThread->depth_first_number() < 0) { duke@435: // First visit to this thread duke@435: currentThread->set_depth_first_number(globalDfn++); duke@435: } else if (currentThread->depth_first_number() < thisDfn) { duke@435: // Thread already visited, and not on a (new) cycle duke@435: break; duke@435: } else if (currentThread == previousThread) { duke@435: // Self-loop, ignore duke@435: break; duke@435: } else { duke@435: // We have a (new) cycle duke@435: num_deadlocks++; duke@435: duke@435: cycle->set_deadlock(true); duke@435: duke@435: // add this cycle to the deadlocks list duke@435: if (deadlocks == NULL) { duke@435: deadlocks = cycle; duke@435: } else { duke@435: last->set_next(cycle); duke@435: } duke@435: last = cycle; duke@435: cycle = new DeadlockCycle(); duke@435: break; duke@435: } duke@435: previousThread = currentThread; duke@435: waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(); duke@435: if (concurrent_locks) { duke@435: waitingToLockBlocker = currentThread->current_park_blocker(); duke@435: } duke@435: } duke@435: duke@435: } duke@435: duke@435: return deadlocks; duke@435: } duke@435: duke@435: ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) { duke@435: duke@435: // Create a new ThreadDumpResult object and append to the list. duke@435: // If GC happens before this function returns, methodOop duke@435: // in the stack trace will be visited. duke@435: ThreadService::add_thread_dump(this); duke@435: } duke@435: duke@435: ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) { duke@435: // Create a new ThreadDumpResult object and append to the list. duke@435: // If GC happens before this function returns, oops duke@435: // will be visited. duke@435: ThreadService::add_thread_dump(this); duke@435: } duke@435: duke@435: ThreadDumpResult::~ThreadDumpResult() { duke@435: ThreadService::remove_thread_dump(this); duke@435: duke@435: // free all the ThreadSnapshot objects created during duke@435: // the VM_ThreadDump operation duke@435: ThreadSnapshot* ts = _snapshots; duke@435: while (ts != NULL) { duke@435: ThreadSnapshot* p = ts; duke@435: ts = ts->next(); duke@435: delete p; duke@435: } duke@435: } duke@435: duke@435: duke@435: void ThreadDumpResult::add_thread_snapshot(ThreadSnapshot* ts) { duke@435: assert(_num_threads == 0 || _num_snapshots < _num_threads, duke@435: "_num_snapshots must be less than _num_threads"); duke@435: _num_snapshots++; duke@435: if (_snapshots == NULL) { duke@435: _snapshots = ts; duke@435: } else { duke@435: _last->set_next(ts); duke@435: } duke@435: _last = ts; duke@435: } duke@435: duke@435: void ThreadDumpResult::oops_do(OopClosure* f) { duke@435: for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) { duke@435: ts->oops_do(f); duke@435: } duke@435: } duke@435: duke@435: StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) { duke@435: _method = jvf->method(); duke@435: _bci = jvf->bci(); duke@435: _locked_monitors = NULL; duke@435: if (with_lock_info) { duke@435: ResourceMark rm; duke@435: GrowableArray* list = jvf->locked_monitors(); duke@435: int length = list->length(); duke@435: if (length > 0) { duke@435: _locked_monitors = new (ResourceObj::C_HEAP) GrowableArray(length, true); duke@435: for (int i = 0; i < length; i++) { duke@435: MonitorInfo* monitor = list->at(i); duke@435: assert(monitor->owner(), "This monitor must have an owning object"); duke@435: _locked_monitors->append(monitor->owner()); duke@435: } duke@435: } duke@435: } duke@435: } duke@435: duke@435: void StackFrameInfo::oops_do(OopClosure* f) { duke@435: f->do_oop((oop*) &_method); duke@435: if (_locked_monitors != NULL) { duke@435: int length = _locked_monitors->length(); duke@435: for (int i = 0; i < length; i++) { duke@435: f->do_oop((oop*) _locked_monitors->adr_at(i)); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void StackFrameInfo::print_on(outputStream* st) const { duke@435: ResourceMark rm; duke@435: java_lang_Throwable::print_stack_element(st, method(), bci()); duke@435: int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0); duke@435: for (int i = 0; i < len; i++) { duke@435: oop o = _locked_monitors->at(i); duke@435: instanceKlass* ik = instanceKlass::cast(o->klass()); duke@435: st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", (address)o, ik->external_name()); duke@435: } duke@435: duke@435: } duke@435: duke@435: // Iterate through monitor cache to find JNI locked monitors duke@435: class InflatedMonitorsClosure: public MonitorClosure { duke@435: private: duke@435: ThreadStackTrace* _stack_trace; duke@435: Thread* _thread; duke@435: public: duke@435: InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) { duke@435: _thread = t; duke@435: _stack_trace = st; duke@435: } duke@435: void do_monitor(ObjectMonitor* mid) { duke@435: if (mid->owner() == _thread) { duke@435: oop object = (oop) mid->object(); duke@435: if (!_stack_trace->is_owned_monitor_on_stack(object)) { duke@435: _stack_trace->add_jni_locked_monitor(object); duke@435: } duke@435: } duke@435: } duke@435: }; duke@435: duke@435: ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) { duke@435: _thread = t; duke@435: _frames = new (ResourceObj::C_HEAP) GrowableArray(INITIAL_ARRAY_SIZE, true); duke@435: _depth = 0; duke@435: _with_locked_monitors = with_locked_monitors; duke@435: if (_with_locked_monitors) { duke@435: _jni_locked_monitors = new (ResourceObj::C_HEAP) GrowableArray(INITIAL_ARRAY_SIZE, true); duke@435: } else { duke@435: _jni_locked_monitors = NULL; duke@435: } duke@435: } duke@435: duke@435: ThreadStackTrace::~ThreadStackTrace() { duke@435: for (int i = 0; i < _frames->length(); i++) { duke@435: delete _frames->at(i); duke@435: } duke@435: delete _frames; duke@435: if (_jni_locked_monitors != NULL) { duke@435: delete _jni_locked_monitors; duke@435: } duke@435: } duke@435: duke@435: void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); duke@435: duke@435: if (_thread->has_last_Java_frame()) { duke@435: RegisterMap reg_map(_thread); duke@435: vframe* start_vf = _thread->last_java_vframe(®_map); duke@435: int count = 0; duke@435: for (vframe* f = start_vf; f; f = f->sender() ) { duke@435: if (f->is_java_frame()) { duke@435: javaVFrame* jvf = javaVFrame::cast(f); duke@435: add_stack_frame(jvf); duke@435: count++; duke@435: } else { duke@435: // Ignore non-Java frames duke@435: } duke@435: if (maxDepth > 0 && count == maxDepth) { duke@435: // Skip frames if more than maxDepth duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: duke@435: if (_with_locked_monitors) { duke@435: // Iterate inflated monitors and find monitors locked by this thread duke@435: // not found in the stack duke@435: InflatedMonitorsClosure imc(_thread, this); duke@435: ObjectSynchronizer::monitors_iterate(&imc); duke@435: } duke@435: } duke@435: duke@435: duke@435: bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) { duke@435: assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); duke@435: duke@435: bool found = false; duke@435: int num_frames = get_stack_depth(); duke@435: for (int depth = 0; depth < num_frames; depth++) { duke@435: StackFrameInfo* frame = stack_frame_at(depth); duke@435: int len = frame->num_locked_monitors(); duke@435: GrowableArray* locked_monitors = frame->locked_monitors(); duke@435: for (int j = 0; j < len; j++) { duke@435: oop monitor = locked_monitors->at(j); duke@435: assert(monitor != NULL && monitor->is_instance(), "must be a Java object"); duke@435: if (monitor == object) { duke@435: found = true; duke@435: break; duke@435: } duke@435: } duke@435: } duke@435: return found; duke@435: } duke@435: duke@435: Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) { never@1577: klassOop k = SystemDictionary::StackTraceElement_klass(); jrose@567: assert(k != NULL, "must be loaded in 1.4+"); duke@435: instanceKlassHandle ik(THREAD, k); duke@435: duke@435: // Allocate an array of java/lang/StackTraceElement object duke@435: objArrayOop ste = oopFactory::new_objArray(ik(), _depth, CHECK_NH); duke@435: objArrayHandle backtrace(THREAD, ste); duke@435: for (int j = 0; j < _depth; j++) { duke@435: StackFrameInfo* frame = _frames->at(j); duke@435: methodHandle mh(THREAD, frame->method()); duke@435: oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH); duke@435: backtrace->obj_at_put(j, element); duke@435: } duke@435: return backtrace; duke@435: } duke@435: duke@435: void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) { duke@435: StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors); duke@435: _frames->append(frame); duke@435: _depth++; duke@435: } duke@435: duke@435: void ThreadStackTrace::oops_do(OopClosure* f) { duke@435: int length = _frames->length(); duke@435: for (int i = 0; i < length; i++) { duke@435: _frames->at(i)->oops_do(f); duke@435: } duke@435: duke@435: length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0); duke@435: for (int j = 0; j < length; j++) { duke@435: f->do_oop((oop*) _jni_locked_monitors->adr_at(j)); duke@435: } duke@435: } duke@435: duke@435: ConcurrentLocksDump::~ConcurrentLocksDump() { duke@435: if (_retain_map_on_free) { duke@435: return; duke@435: } duke@435: duke@435: for (ThreadConcurrentLocks* t = _map; t != NULL;) { duke@435: ThreadConcurrentLocks* tcl = t; duke@435: t = t->next(); duke@435: delete tcl; duke@435: } duke@435: } duke@435: duke@435: void ConcurrentLocksDump::dump_at_safepoint() { duke@435: // dump all locked concurrent locks duke@435: assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); duke@435: duke@435: if (JDK_Version::is_gte_jdk16x_version()) { duke@435: ResourceMark rm; duke@435: duke@435: GrowableArray* aos_objects = new GrowableArray(INITIAL_ARRAY_SIZE); duke@435: duke@435: // Find all instances of AbstractOwnableSynchronizer duke@435: HeapInspection::find_instances_at_safepoint(SystemDictionary::abstract_ownable_synchronizer_klass(), duke@435: aos_objects); duke@435: // Build a map of thread to its owned AQS locks duke@435: build_map(aos_objects); duke@435: } duke@435: } duke@435: duke@435: duke@435: // build a map of JavaThread to all its owned AbstractOwnableSynchronizer duke@435: void ConcurrentLocksDump::build_map(GrowableArray* aos_objects) { duke@435: int length = aos_objects->length(); duke@435: for (int i = 0; i < length; i++) { duke@435: oop o = aos_objects->at(i); duke@435: oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o); duke@435: if (owner_thread_obj != NULL) { duke@435: JavaThread* thread = java_lang_Thread::thread(owner_thread_obj); duke@435: assert(o->is_instance(), "Must be an instanceOop"); duke@435: add_lock(thread, (instanceOop) o); duke@435: } duke@435: } duke@435: } duke@435: duke@435: void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) { duke@435: ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread); duke@435: if (tcl != NULL) { duke@435: tcl->add_lock(o); duke@435: return; duke@435: } duke@435: duke@435: // First owned lock found for this thread duke@435: tcl = new ThreadConcurrentLocks(thread); duke@435: tcl->add_lock(o); duke@435: if (_map == NULL) { duke@435: _map = tcl; duke@435: } else { duke@435: _last->set_next(tcl); duke@435: } duke@435: _last = tcl; duke@435: } duke@435: duke@435: ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) { duke@435: for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) { duke@435: if (tcl->java_thread() == thread) { duke@435: return tcl; duke@435: } duke@435: } duke@435: return NULL; duke@435: } duke@435: duke@435: void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) { duke@435: st->print_cr(" Locked ownable synchronizers:"); duke@435: ThreadConcurrentLocks* tcl = thread_concurrent_locks(t); duke@435: GrowableArray* locks = (tcl != NULL ? tcl->owned_locks() : NULL); duke@435: if (locks == NULL || locks->is_empty()) { duke@435: st->print_cr("\t- None"); duke@435: st->cr(); duke@435: return; duke@435: } duke@435: duke@435: for (int i = 0; i < locks->length(); i++) { duke@435: instanceOop obj = locks->at(i); duke@435: instanceKlass* ik = instanceKlass::cast(obj->klass()); duke@435: st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", (address)obj, ik->external_name()); duke@435: } duke@435: st->cr(); duke@435: } duke@435: duke@435: ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) { duke@435: _thread = thread; duke@435: _owned_locks = new (ResourceObj::C_HEAP) GrowableArray(INITIAL_ARRAY_SIZE, true); duke@435: _next = NULL; duke@435: } duke@435: duke@435: ThreadConcurrentLocks::~ThreadConcurrentLocks() { duke@435: delete _owned_locks; duke@435: } duke@435: duke@435: void ThreadConcurrentLocks::add_lock(instanceOop o) { duke@435: _owned_locks->append(o); duke@435: } duke@435: duke@435: void ThreadConcurrentLocks::oops_do(OopClosure* f) { duke@435: int length = _owned_locks->length(); duke@435: for (int i = 0; i < length; i++) { duke@435: f->do_oop((oop*) _owned_locks->adr_at(i)); duke@435: } duke@435: } duke@435: duke@435: ThreadStatistics::ThreadStatistics() { duke@435: _contended_enter_count = 0; duke@435: _monitor_wait_count = 0; duke@435: _sleep_count = 0; duke@435: _count_pending_reset = false; duke@435: _timer_pending_reset = false; mchung@1310: memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts)); duke@435: } duke@435: duke@435: ThreadSnapshot::ThreadSnapshot(JavaThread* thread) { duke@435: _thread = thread; duke@435: _threadObj = thread->threadObj(); duke@435: _stack_trace = NULL; duke@435: _concurrent_locks = NULL; duke@435: _next = NULL; duke@435: duke@435: ThreadStatistics* stat = thread->get_thread_stat(); duke@435: _contended_enter_ticks = stat->contended_enter_ticks(); duke@435: _contended_enter_count = stat->contended_enter_count(); duke@435: _monitor_wait_ticks = stat->monitor_wait_ticks(); duke@435: _monitor_wait_count = stat->monitor_wait_count(); duke@435: _sleep_ticks = stat->sleep_ticks(); duke@435: _sleep_count = stat->sleep_count(); duke@435: duke@435: _blocker_object = NULL; duke@435: _blocker_object_owner = NULL; duke@435: duke@435: _thread_status = java_lang_Thread::get_thread_status(_threadObj); duke@435: _is_ext_suspended = thread->is_being_ext_suspended(); duke@435: _is_in_native = (thread->thread_state() == _thread_in_native); duke@435: duke@435: if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER || duke@435: _thread_status == java_lang_Thread::IN_OBJECT_WAIT || duke@435: _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) { duke@435: duke@435: Handle obj = ThreadService::get_current_contended_monitor(thread); duke@435: if (obj() == NULL) { duke@435: // monitor no longer exists; thread is not blocked duke@435: _thread_status = java_lang_Thread::RUNNABLE; duke@435: } else { duke@435: _blocker_object = obj(); duke@435: JavaThread* owner = ObjectSynchronizer::get_lock_owner(obj, false); duke@435: if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER) duke@435: || (owner != NULL && owner->is_attaching())) { duke@435: // ownership information of the monitor is not available duke@435: // (may no longer be owned or releasing to some other thread) duke@435: // make this thread in RUNNABLE state. duke@435: // And when the owner thread is in attaching state, the java thread duke@435: // is not completely initialized. For example thread name and id duke@435: // and may not be set, so hide the attaching thread. duke@435: _thread_status = java_lang_Thread::RUNNABLE; duke@435: _blocker_object = NULL; duke@435: } else if (owner != NULL) { duke@435: _blocker_object_owner = owner->threadObj(); duke@435: } duke@435: } duke@435: } duke@435: duke@435: // Support for JSR-166 locks kamg@677: if (JDK_Version::current().supports_thread_park_blocker() && duke@435: (_thread_status == java_lang_Thread::PARKED || duke@435: _thread_status == java_lang_Thread::PARKED_TIMED)) { duke@435: duke@435: _blocker_object = thread->current_park_blocker(); duke@435: if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) { duke@435: _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object); duke@435: } duke@435: } duke@435: } duke@435: duke@435: ThreadSnapshot::~ThreadSnapshot() { duke@435: delete _stack_trace; duke@435: delete _concurrent_locks; duke@435: } duke@435: duke@435: void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) { duke@435: _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors); duke@435: _stack_trace->dump_stack_at_safepoint(max_depth); duke@435: } duke@435: duke@435: duke@435: void ThreadSnapshot::oops_do(OopClosure* f) { duke@435: f->do_oop(&_threadObj); duke@435: f->do_oop(&_blocker_object); duke@435: f->do_oop(&_blocker_object_owner); duke@435: if (_stack_trace != NULL) { duke@435: _stack_trace->oops_do(f); duke@435: } duke@435: if (_concurrent_locks != NULL) { duke@435: _concurrent_locks->oops_do(f); duke@435: } duke@435: } duke@435: duke@435: DeadlockCycle::DeadlockCycle() { duke@435: _is_deadlock = false; duke@435: _threads = new (ResourceObj::C_HEAP) GrowableArray(INITIAL_ARRAY_SIZE, true); duke@435: _next = NULL; duke@435: } duke@435: duke@435: DeadlockCycle::~DeadlockCycle() { duke@435: delete _threads; duke@435: } duke@435: duke@435: void DeadlockCycle::print_on(outputStream* st) const { duke@435: st->cr(); duke@435: st->print_cr("Found one Java-level deadlock:"); duke@435: st->print("============================="); duke@435: duke@435: JavaThread* currentThread; duke@435: ObjectMonitor* waitingToLockMonitor; duke@435: oop waitingToLockBlocker; duke@435: int len = _threads->length(); duke@435: for (int i = 0; i < len; i++) { duke@435: currentThread = _threads->at(i); duke@435: waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(); duke@435: waitingToLockBlocker = currentThread->current_park_blocker(); duke@435: st->cr(); duke@435: st->print_cr("\"%s\":", currentThread->get_thread_name()); duke@435: const char* owner_desc = ",\n which is held by"; duke@435: if (waitingToLockMonitor != NULL) { duke@435: st->print(" waiting to lock monitor " INTPTR_FORMAT, waitingToLockMonitor); duke@435: oop obj = (oop)waitingToLockMonitor->object(); duke@435: if (obj != NULL) { duke@435: st->print(" (object "INTPTR_FORMAT ", a %s)", (address)obj, duke@435: (instanceKlass::cast(obj->klass()))->external_name()); duke@435: duke@435: if (!currentThread->current_pending_monitor_is_from_java()) { duke@435: owner_desc = "\n in JNI, which is held by"; duke@435: } duke@435: } else { duke@435: // No Java object associated - a JVMTI raw monitor duke@435: owner_desc = " (JVMTI raw monitor),\n which is held by"; duke@435: } duke@435: currentThread = Threads::owning_thread_from_monitor_owner( duke@435: (address)waitingToLockMonitor->owner(), false /* no locking needed */); duke@435: } else { duke@435: st->print(" waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)", duke@435: (address)waitingToLockBlocker, duke@435: (instanceKlass::cast(waitingToLockBlocker->klass()))->external_name()); duke@435: assert(waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass()), duke@435: "Must be an AbstractOwnableSynchronizer"); duke@435: oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker); duke@435: currentThread = java_lang_Thread::thread(ownerObj); duke@435: } duke@435: st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name()); duke@435: } duke@435: duke@435: st->cr(); duke@435: st->cr(); duke@435: duke@435: // Print stack traces duke@435: bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace; duke@435: JavaMonitorsInStackTrace = true; duke@435: st->print_cr("Java stack information for the threads listed above:"); duke@435: st->print_cr("==================================================="); duke@435: for (int j = 0; j < len; j++) { duke@435: currentThread = _threads->at(j); duke@435: st->print_cr("\"%s\":", currentThread->get_thread_name()); duke@435: currentThread->print_stack_on(st); duke@435: } duke@435: JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace; duke@435: } duke@435: duke@435: ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread, duke@435: bool include_jvmti_agent_threads, duke@435: bool include_jni_attaching_threads) { duke@435: assert(cur_thread == Thread::current(), "Check current thread"); duke@435: duke@435: int init_size = ThreadService::get_live_thread_count(); duke@435: _threads_array = new GrowableArray(init_size); duke@435: duke@435: MutexLockerEx ml(Threads_lock); duke@435: duke@435: for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) { duke@435: // skips JavaThreads in the process of exiting duke@435: // and also skips VM internal JavaThreads duke@435: // Threads in _thread_new or _thread_new_trans state are included. duke@435: // i.e. threads have been started but not yet running. duke@435: if (jt->threadObj() == NULL || duke@435: jt->is_exiting() || duke@435: !java_lang_Thread::is_alive(jt->threadObj()) || duke@435: jt->is_hidden_from_external_view()) { duke@435: continue; duke@435: } duke@435: duke@435: // skip agent threads duke@435: if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) { duke@435: continue; duke@435: } duke@435: duke@435: // skip jni threads in the process of attaching duke@435: if (!include_jni_attaching_threads && jt->is_attaching()) { duke@435: continue; duke@435: } duke@435: duke@435: instanceHandle h(cur_thread, (instanceOop) jt->threadObj()); duke@435: _threads_array->append(h); duke@435: } duke@435: }