1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/services/threadService.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,885 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_threadService.cpp.incl" 1.30 + 1.31 +// TODO: we need to define a naming convention for perf counters 1.32 +// to distinguish counters for: 1.33 +// - standard JSR174 use 1.34 +// - Hotspot extension (public and committed) 1.35 +// - Hotspot extension (private/internal and uncommitted) 1.36 + 1.37 +// Default is disabled. 1.38 +bool ThreadService::_thread_monitoring_contention_enabled = false; 1.39 +bool ThreadService::_thread_cpu_time_enabled = false; 1.40 + 1.41 +PerfCounter* ThreadService::_total_threads_count = NULL; 1.42 +PerfVariable* ThreadService::_live_threads_count = NULL; 1.43 +PerfVariable* ThreadService::_peak_threads_count = NULL; 1.44 +PerfVariable* ThreadService::_daemon_threads_count = NULL; 1.45 +volatile int ThreadService::_exiting_threads_count = 0; 1.46 +volatile int ThreadService::_exiting_daemon_threads_count = 0; 1.47 + 1.48 +ThreadDumpResult* ThreadService::_threaddump_list = NULL; 1.49 + 1.50 +static const int INITIAL_ARRAY_SIZE = 10; 1.51 + 1.52 +void ThreadService::init() { 1.53 + EXCEPTION_MARK; 1.54 + 1.55 + // These counters are for java.lang.management API support. 1.56 + // They are created even if -XX:-UsePerfData is set and in 1.57 + // that case, they will be allocated on C heap. 1.58 + 1.59 + _total_threads_count = 1.60 + PerfDataManager::create_counter(JAVA_THREADS, "started", 1.61 + PerfData::U_Events, CHECK); 1.62 + 1.63 + _live_threads_count = 1.64 + PerfDataManager::create_variable(JAVA_THREADS, "live", 1.65 + PerfData::U_None, CHECK); 1.66 + 1.67 + _peak_threads_count = 1.68 + PerfDataManager::create_variable(JAVA_THREADS, "livePeak", 1.69 + PerfData::U_None, CHECK); 1.70 + 1.71 + _daemon_threads_count = 1.72 + PerfDataManager::create_variable(JAVA_THREADS, "daemon", 1.73 + PerfData::U_None, CHECK); 1.74 + 1.75 + if (os::is_thread_cpu_time_supported()) { 1.76 + _thread_cpu_time_enabled = true; 1.77 + } 1.78 +} 1.79 + 1.80 +void ThreadService::reset_peak_thread_count() { 1.81 + // Acquire the lock to update the peak thread count 1.82 + // to synchronize with thread addition and removal. 1.83 + MutexLockerEx mu(Threads_lock); 1.84 + _peak_threads_count->set_value(get_live_thread_count()); 1.85 +} 1.86 + 1.87 +void ThreadService::add_thread(JavaThread* thread, bool daemon) { 1.88 + // Do not count VM internal or JVMTI agent threads 1.89 + if (thread->is_hidden_from_external_view() || 1.90 + thread->is_jvmti_agent_thread()) { 1.91 + return; 1.92 + } 1.93 + 1.94 + _total_threads_count->inc(); 1.95 + _live_threads_count->inc(); 1.96 + 1.97 + if (_live_threads_count->get_value() > _peak_threads_count->get_value()) { 1.98 + _peak_threads_count->set_value(_live_threads_count->get_value()); 1.99 + } 1.100 + 1.101 + if (daemon) { 1.102 + _daemon_threads_count->inc(); 1.103 + } 1.104 +} 1.105 + 1.106 +void ThreadService::remove_thread(JavaThread* thread, bool daemon) { 1.107 + Atomic::dec((jint*) &_exiting_threads_count); 1.108 + 1.109 + if (thread->is_hidden_from_external_view() || 1.110 + thread->is_jvmti_agent_thread()) { 1.111 + return; 1.112 + } 1.113 + 1.114 + _live_threads_count->set_value(_live_threads_count->get_value() - 1); 1.115 + 1.116 + if (daemon) { 1.117 + _daemon_threads_count->set_value(_daemon_threads_count->get_value() - 1); 1.118 + Atomic::dec((jint*) &_exiting_daemon_threads_count); 1.119 + } 1.120 +} 1.121 + 1.122 +void ThreadService::current_thread_exiting(JavaThread* jt) { 1.123 + assert(jt == JavaThread::current(), "Called by current thread"); 1.124 + Atomic::inc((jint*) &_exiting_threads_count); 1.125 + 1.126 + oop threadObj = jt->threadObj(); 1.127 + if (threadObj != NULL && java_lang_Thread::is_daemon(threadObj)) { 1.128 + Atomic::inc((jint*) &_exiting_daemon_threads_count); 1.129 + } 1.130 +} 1.131 + 1.132 +// FIXME: JVMTI should call this function 1.133 +Handle ThreadService::get_current_contended_monitor(JavaThread* thread) { 1.134 + assert(thread != NULL, "should be non-NULL"); 1.135 + assert(Threads_lock->owned_by_self(), "must grab Threads_lock or be at safepoint"); 1.136 + 1.137 + ObjectMonitor *wait_obj = thread->current_waiting_monitor(); 1.138 + 1.139 + oop obj = NULL; 1.140 + if (wait_obj != NULL) { 1.141 + // thread is doing an Object.wait() call 1.142 + obj = (oop) wait_obj->object(); 1.143 + assert(obj != NULL, "Object.wait() should have an object"); 1.144 + } else { 1.145 + ObjectMonitor *enter_obj = thread->current_pending_monitor(); 1.146 + if (enter_obj != NULL) { 1.147 + // thread is trying to enter() or raw_enter() an ObjectMonitor. 1.148 + obj = (oop) enter_obj->object(); 1.149 + } 1.150 + // If obj == NULL, then ObjectMonitor is raw which doesn't count. 1.151 + } 1.152 + 1.153 + Handle h(obj); 1.154 + return h; 1.155 +} 1.156 + 1.157 +bool ThreadService::set_thread_monitoring_contention(bool flag) { 1.158 + MutexLocker m(Management_lock); 1.159 + 1.160 + bool prev = _thread_monitoring_contention_enabled; 1.161 + _thread_monitoring_contention_enabled = flag; 1.162 + 1.163 + return prev; 1.164 +} 1.165 + 1.166 +bool ThreadService::set_thread_cpu_time_enabled(bool flag) { 1.167 + MutexLocker m(Management_lock); 1.168 + 1.169 + bool prev = _thread_cpu_time_enabled; 1.170 + _thread_cpu_time_enabled = flag; 1.171 + 1.172 + return prev; 1.173 +} 1.174 + 1.175 +// GC support 1.176 +void ThreadService::oops_do(OopClosure* f) { 1.177 + for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) { 1.178 + dump->oops_do(f); 1.179 + } 1.180 +} 1.181 + 1.182 +void ThreadService::add_thread_dump(ThreadDumpResult* dump) { 1.183 + MutexLocker ml(Management_lock); 1.184 + if (_threaddump_list == NULL) { 1.185 + _threaddump_list = dump; 1.186 + } else { 1.187 + dump->set_next(_threaddump_list); 1.188 + _threaddump_list = dump; 1.189 + } 1.190 +} 1.191 + 1.192 +void ThreadService::remove_thread_dump(ThreadDumpResult* dump) { 1.193 + MutexLocker ml(Management_lock); 1.194 + 1.195 + ThreadDumpResult* prev = NULL; 1.196 + bool found = false; 1.197 + for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) { 1.198 + if (d == dump) { 1.199 + if (prev == NULL) { 1.200 + _threaddump_list = dump->next(); 1.201 + } else { 1.202 + prev->set_next(dump->next()); 1.203 + } 1.204 + found = true; 1.205 + break; 1.206 + } 1.207 + } 1.208 + assert(found, "The threaddump result to be removed must exist."); 1.209 +} 1.210 + 1.211 +// Dump stack trace of threads specified in the given threads array. 1.212 +// Returns StackTraceElement[][] each element is the stack trace of a thread in 1.213 +// the corresponding entry in the given threads array 1.214 +Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads, 1.215 + int num_threads, 1.216 + TRAPS) { 1.217 + assert(num_threads > 0, "just checking"); 1.218 + 1.219 + ThreadDumpResult dump_result; 1.220 + VM_ThreadDump op(&dump_result, 1.221 + threads, 1.222 + num_threads, 1.223 + -1, /* entire stack */ 1.224 + false, /* with locked monitors */ 1.225 + false /* with locked synchronizers */); 1.226 + VMThread::execute(&op); 1.227 + 1.228 + // Allocate the resulting StackTraceElement[][] object 1.229 + 1.230 + ResourceMark rm(THREAD); 1.231 + klassOop k = SystemDictionary::resolve_or_fail(vmSymbolHandles::java_lang_StackTraceElement_array(), true, CHECK_NH); 1.232 + objArrayKlassHandle ik (THREAD, k); 1.233 + objArrayOop r = oopFactory::new_objArray(ik(), num_threads, CHECK_NH); 1.234 + objArrayHandle result_obj(THREAD, r); 1.235 + 1.236 + int num_snapshots = dump_result.num_snapshots(); 1.237 + assert(num_snapshots == num_threads, "Must have num_threads thread snapshots"); 1.238 + int i = 0; 1.239 + for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) { 1.240 + ThreadStackTrace* stacktrace = ts->get_stack_trace(); 1.241 + if (stacktrace == NULL) { 1.242 + // No stack trace 1.243 + result_obj->obj_at_put(i, NULL); 1.244 + } else { 1.245 + // Construct an array of java/lang/StackTraceElement object 1.246 + Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH); 1.247 + result_obj->obj_at_put(i, backtrace_h()); 1.248 + } 1.249 + } 1.250 + 1.251 + return result_obj; 1.252 +} 1.253 + 1.254 +void ThreadService::reset_contention_count_stat(JavaThread* thread) { 1.255 + ThreadStatistics* stat = thread->get_thread_stat(); 1.256 + if (stat != NULL) { 1.257 + stat->reset_count_stat(); 1.258 + } 1.259 +} 1.260 + 1.261 +void ThreadService::reset_contention_time_stat(JavaThread* thread) { 1.262 + ThreadStatistics* stat = thread->get_thread_stat(); 1.263 + if (stat != NULL) { 1.264 + stat->reset_time_stat(); 1.265 + } 1.266 +} 1.267 + 1.268 +// Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true 1.269 +DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(bool concurrent_locks) { 1.270 + // This code was modified from the original Threads::find_deadlocks code. 1.271 + int globalDfn = 0, thisDfn; 1.272 + ObjectMonitor* waitingToLockMonitor = NULL; 1.273 + oop waitingToLockBlocker = NULL; 1.274 + bool blocked_on_monitor = false; 1.275 + JavaThread *currentThread, *previousThread; 1.276 + int num_deadlocks = 0; 1.277 + 1.278 + for (JavaThread* p = Threads::first(); p != NULL; p = p->next()) { 1.279 + // Initialize the depth-first-number 1.280 + p->set_depth_first_number(-1); 1.281 + } 1.282 + 1.283 + DeadlockCycle* deadlocks = NULL; 1.284 + DeadlockCycle* last = NULL; 1.285 + DeadlockCycle* cycle = new DeadlockCycle(); 1.286 + for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) { 1.287 + if (jt->depth_first_number() >= 0) { 1.288 + // this thread was already visited 1.289 + continue; 1.290 + } 1.291 + 1.292 + thisDfn = globalDfn; 1.293 + jt->set_depth_first_number(globalDfn++); 1.294 + previousThread = jt; 1.295 + currentThread = jt; 1.296 + 1.297 + cycle->reset(); 1.298 + 1.299 + // When there is a deadlock, all the monitors involved in the dependency 1.300 + // cycle must be contended and heavyweight. So we only care about the 1.301 + // heavyweight monitor a thread is waiting to lock. 1.302 + waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor(); 1.303 + if (concurrent_locks) { 1.304 + waitingToLockBlocker = jt->current_park_blocker(); 1.305 + } 1.306 + while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) { 1.307 + cycle->add_thread(currentThread); 1.308 + if (waitingToLockMonitor != NULL) { 1.309 + currentThread = Threads::owning_thread_from_monitor_owner((address)waitingToLockMonitor->owner(), 1.310 + false /* no locking needed */); 1.311 + } else { 1.312 + if (concurrent_locks) { 1.313 + if (waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) { 1.314 + oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker); 1.315 + currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL; 1.316 + } else { 1.317 + currentThread = NULL; 1.318 + } 1.319 + } 1.320 + } 1.321 + 1.322 + if (currentThread == NULL) { 1.323 + // No dependency on another thread 1.324 + break; 1.325 + } 1.326 + if (currentThread->depth_first_number() < 0) { 1.327 + // First visit to this thread 1.328 + currentThread->set_depth_first_number(globalDfn++); 1.329 + } else if (currentThread->depth_first_number() < thisDfn) { 1.330 + // Thread already visited, and not on a (new) cycle 1.331 + break; 1.332 + } else if (currentThread == previousThread) { 1.333 + // Self-loop, ignore 1.334 + break; 1.335 + } else { 1.336 + // We have a (new) cycle 1.337 + num_deadlocks++; 1.338 + 1.339 + cycle->set_deadlock(true); 1.340 + 1.341 + // add this cycle to the deadlocks list 1.342 + if (deadlocks == NULL) { 1.343 + deadlocks = cycle; 1.344 + } else { 1.345 + last->set_next(cycle); 1.346 + } 1.347 + last = cycle; 1.348 + cycle = new DeadlockCycle(); 1.349 + break; 1.350 + } 1.351 + previousThread = currentThread; 1.352 + waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(); 1.353 + if (concurrent_locks) { 1.354 + waitingToLockBlocker = currentThread->current_park_blocker(); 1.355 + } 1.356 + } 1.357 + 1.358 + } 1.359 + 1.360 + return deadlocks; 1.361 +} 1.362 + 1.363 +ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) { 1.364 + 1.365 + // Create a new ThreadDumpResult object and append to the list. 1.366 + // If GC happens before this function returns, methodOop 1.367 + // in the stack trace will be visited. 1.368 + ThreadService::add_thread_dump(this); 1.369 +} 1.370 + 1.371 +ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) { 1.372 + // Create a new ThreadDumpResult object and append to the list. 1.373 + // If GC happens before this function returns, oops 1.374 + // will be visited. 1.375 + ThreadService::add_thread_dump(this); 1.376 +} 1.377 + 1.378 +ThreadDumpResult::~ThreadDumpResult() { 1.379 + ThreadService::remove_thread_dump(this); 1.380 + 1.381 + // free all the ThreadSnapshot objects created during 1.382 + // the VM_ThreadDump operation 1.383 + ThreadSnapshot* ts = _snapshots; 1.384 + while (ts != NULL) { 1.385 + ThreadSnapshot* p = ts; 1.386 + ts = ts->next(); 1.387 + delete p; 1.388 + } 1.389 +} 1.390 + 1.391 + 1.392 +void ThreadDumpResult::add_thread_snapshot(ThreadSnapshot* ts) { 1.393 + assert(_num_threads == 0 || _num_snapshots < _num_threads, 1.394 + "_num_snapshots must be less than _num_threads"); 1.395 + _num_snapshots++; 1.396 + if (_snapshots == NULL) { 1.397 + _snapshots = ts; 1.398 + } else { 1.399 + _last->set_next(ts); 1.400 + } 1.401 + _last = ts; 1.402 +} 1.403 + 1.404 +void ThreadDumpResult::oops_do(OopClosure* f) { 1.405 + for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) { 1.406 + ts->oops_do(f); 1.407 + } 1.408 +} 1.409 + 1.410 +StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) { 1.411 + _method = jvf->method(); 1.412 + _bci = jvf->bci(); 1.413 + _locked_monitors = NULL; 1.414 + if (with_lock_info) { 1.415 + ResourceMark rm; 1.416 + GrowableArray<MonitorInfo*>* list = jvf->locked_monitors(); 1.417 + int length = list->length(); 1.418 + if (length > 0) { 1.419 + _locked_monitors = new (ResourceObj::C_HEAP) GrowableArray<oop>(length, true); 1.420 + for (int i = 0; i < length; i++) { 1.421 + MonitorInfo* monitor = list->at(i); 1.422 + assert(monitor->owner(), "This monitor must have an owning object"); 1.423 + _locked_monitors->append(monitor->owner()); 1.424 + } 1.425 + } 1.426 + } 1.427 +} 1.428 + 1.429 +void StackFrameInfo::oops_do(OopClosure* f) { 1.430 + f->do_oop((oop*) &_method); 1.431 + if (_locked_monitors != NULL) { 1.432 + int length = _locked_monitors->length(); 1.433 + for (int i = 0; i < length; i++) { 1.434 + f->do_oop((oop*) _locked_monitors->adr_at(i)); 1.435 + } 1.436 + } 1.437 +} 1.438 + 1.439 +void StackFrameInfo::print_on(outputStream* st) const { 1.440 + ResourceMark rm; 1.441 + java_lang_Throwable::print_stack_element(st, method(), bci()); 1.442 + int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0); 1.443 + for (int i = 0; i < len; i++) { 1.444 + oop o = _locked_monitors->at(i); 1.445 + instanceKlass* ik = instanceKlass::cast(o->klass()); 1.446 + st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", (address)o, ik->external_name()); 1.447 + } 1.448 + 1.449 +} 1.450 + 1.451 +// Iterate through monitor cache to find JNI locked monitors 1.452 +class InflatedMonitorsClosure: public MonitorClosure { 1.453 +private: 1.454 + ThreadStackTrace* _stack_trace; 1.455 + Thread* _thread; 1.456 +public: 1.457 + InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) { 1.458 + _thread = t; 1.459 + _stack_trace = st; 1.460 + } 1.461 + void do_monitor(ObjectMonitor* mid) { 1.462 + if (mid->owner() == _thread) { 1.463 + oop object = (oop) mid->object(); 1.464 + if (!_stack_trace->is_owned_monitor_on_stack(object)) { 1.465 + _stack_trace->add_jni_locked_monitor(object); 1.466 + } 1.467 + } 1.468 + } 1.469 +}; 1.470 + 1.471 +ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) { 1.472 + _thread = t; 1.473 + _frames = new (ResourceObj::C_HEAP) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, true); 1.474 + _depth = 0; 1.475 + _with_locked_monitors = with_locked_monitors; 1.476 + if (_with_locked_monitors) { 1.477 + _jni_locked_monitors = new (ResourceObj::C_HEAP) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true); 1.478 + } else { 1.479 + _jni_locked_monitors = NULL; 1.480 + } 1.481 +} 1.482 + 1.483 +ThreadStackTrace::~ThreadStackTrace() { 1.484 + for (int i = 0; i < _frames->length(); i++) { 1.485 + delete _frames->at(i); 1.486 + } 1.487 + delete _frames; 1.488 + if (_jni_locked_monitors != NULL) { 1.489 + delete _jni_locked_monitors; 1.490 + } 1.491 +} 1.492 + 1.493 +void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) { 1.494 + assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); 1.495 + 1.496 + if (_thread->has_last_Java_frame()) { 1.497 + RegisterMap reg_map(_thread); 1.498 + vframe* start_vf = _thread->last_java_vframe(®_map); 1.499 + int count = 0; 1.500 + for (vframe* f = start_vf; f; f = f->sender() ) { 1.501 + if (f->is_java_frame()) { 1.502 + javaVFrame* jvf = javaVFrame::cast(f); 1.503 + add_stack_frame(jvf); 1.504 + count++; 1.505 + } else { 1.506 + // Ignore non-Java frames 1.507 + } 1.508 + if (maxDepth > 0 && count == maxDepth) { 1.509 + // Skip frames if more than maxDepth 1.510 + break; 1.511 + } 1.512 + } 1.513 + } 1.514 + 1.515 + if (_with_locked_monitors) { 1.516 + // Iterate inflated monitors and find monitors locked by this thread 1.517 + // not found in the stack 1.518 + InflatedMonitorsClosure imc(_thread, this); 1.519 + ObjectSynchronizer::monitors_iterate(&imc); 1.520 + } 1.521 +} 1.522 + 1.523 + 1.524 +bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) { 1.525 + assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); 1.526 + 1.527 + bool found = false; 1.528 + int num_frames = get_stack_depth(); 1.529 + for (int depth = 0; depth < num_frames; depth++) { 1.530 + StackFrameInfo* frame = stack_frame_at(depth); 1.531 + int len = frame->num_locked_monitors(); 1.532 + GrowableArray<oop>* locked_monitors = frame->locked_monitors(); 1.533 + for (int j = 0; j < len; j++) { 1.534 + oop monitor = locked_monitors->at(j); 1.535 + assert(monitor != NULL && monitor->is_instance(), "must be a Java object"); 1.536 + if (monitor == object) { 1.537 + found = true; 1.538 + break; 1.539 + } 1.540 + } 1.541 + } 1.542 + return found; 1.543 +} 1.544 + 1.545 +Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) { 1.546 + klassOop k = SystemDictionary::stackTraceElement_klass(); 1.547 + instanceKlassHandle ik(THREAD, k); 1.548 + 1.549 + // Allocate an array of java/lang/StackTraceElement object 1.550 + objArrayOop ste = oopFactory::new_objArray(ik(), _depth, CHECK_NH); 1.551 + objArrayHandle backtrace(THREAD, ste); 1.552 + for (int j = 0; j < _depth; j++) { 1.553 + StackFrameInfo* frame = _frames->at(j); 1.554 + methodHandle mh(THREAD, frame->method()); 1.555 + oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH); 1.556 + backtrace->obj_at_put(j, element); 1.557 + } 1.558 + return backtrace; 1.559 +} 1.560 + 1.561 +void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) { 1.562 + StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors); 1.563 + _frames->append(frame); 1.564 + _depth++; 1.565 +} 1.566 + 1.567 +void ThreadStackTrace::oops_do(OopClosure* f) { 1.568 + int length = _frames->length(); 1.569 + for (int i = 0; i < length; i++) { 1.570 + _frames->at(i)->oops_do(f); 1.571 + } 1.572 + 1.573 + length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0); 1.574 + for (int j = 0; j < length; j++) { 1.575 + f->do_oop((oop*) _jni_locked_monitors->adr_at(j)); 1.576 + } 1.577 +} 1.578 + 1.579 +ConcurrentLocksDump::~ConcurrentLocksDump() { 1.580 + if (_retain_map_on_free) { 1.581 + return; 1.582 + } 1.583 + 1.584 + for (ThreadConcurrentLocks* t = _map; t != NULL;) { 1.585 + ThreadConcurrentLocks* tcl = t; 1.586 + t = t->next(); 1.587 + delete tcl; 1.588 + } 1.589 +} 1.590 + 1.591 +void ConcurrentLocksDump::dump_at_safepoint() { 1.592 + // dump all locked concurrent locks 1.593 + assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped"); 1.594 + 1.595 + if (JDK_Version::is_gte_jdk16x_version()) { 1.596 + ResourceMark rm; 1.597 + 1.598 + GrowableArray<oop>* aos_objects = new GrowableArray<oop>(INITIAL_ARRAY_SIZE); 1.599 + 1.600 + // Find all instances of AbstractOwnableSynchronizer 1.601 + HeapInspection::find_instances_at_safepoint(SystemDictionary::abstract_ownable_synchronizer_klass(), 1.602 + aos_objects); 1.603 + // Build a map of thread to its owned AQS locks 1.604 + build_map(aos_objects); 1.605 + } 1.606 +} 1.607 + 1.608 + 1.609 +// build a map of JavaThread to all its owned AbstractOwnableSynchronizer 1.610 +void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) { 1.611 + int length = aos_objects->length(); 1.612 + for (int i = 0; i < length; i++) { 1.613 + oop o = aos_objects->at(i); 1.614 + oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o); 1.615 + if (owner_thread_obj != NULL) { 1.616 + JavaThread* thread = java_lang_Thread::thread(owner_thread_obj); 1.617 + assert(o->is_instance(), "Must be an instanceOop"); 1.618 + add_lock(thread, (instanceOop) o); 1.619 + } 1.620 + } 1.621 +} 1.622 + 1.623 +void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) { 1.624 + ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread); 1.625 + if (tcl != NULL) { 1.626 + tcl->add_lock(o); 1.627 + return; 1.628 + } 1.629 + 1.630 + // First owned lock found for this thread 1.631 + tcl = new ThreadConcurrentLocks(thread); 1.632 + tcl->add_lock(o); 1.633 + if (_map == NULL) { 1.634 + _map = tcl; 1.635 + } else { 1.636 + _last->set_next(tcl); 1.637 + } 1.638 + _last = tcl; 1.639 +} 1.640 + 1.641 +ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) { 1.642 + for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) { 1.643 + if (tcl->java_thread() == thread) { 1.644 + return tcl; 1.645 + } 1.646 + } 1.647 + return NULL; 1.648 +} 1.649 + 1.650 +void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) { 1.651 + st->print_cr(" Locked ownable synchronizers:"); 1.652 + ThreadConcurrentLocks* tcl = thread_concurrent_locks(t); 1.653 + GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL); 1.654 + if (locks == NULL || locks->is_empty()) { 1.655 + st->print_cr("\t- None"); 1.656 + st->cr(); 1.657 + return; 1.658 + } 1.659 + 1.660 + for (int i = 0; i < locks->length(); i++) { 1.661 + instanceOop obj = locks->at(i); 1.662 + instanceKlass* ik = instanceKlass::cast(obj->klass()); 1.663 + st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", (address)obj, ik->external_name()); 1.664 + } 1.665 + st->cr(); 1.666 +} 1.667 + 1.668 +ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) { 1.669 + _thread = thread; 1.670 + _owned_locks = new (ResourceObj::C_HEAP) GrowableArray<instanceOop>(INITIAL_ARRAY_SIZE, true); 1.671 + _next = NULL; 1.672 +} 1.673 + 1.674 +ThreadConcurrentLocks::~ThreadConcurrentLocks() { 1.675 + delete _owned_locks; 1.676 +} 1.677 + 1.678 +void ThreadConcurrentLocks::add_lock(instanceOop o) { 1.679 + _owned_locks->append(o); 1.680 +} 1.681 + 1.682 +void ThreadConcurrentLocks::oops_do(OopClosure* f) { 1.683 + int length = _owned_locks->length(); 1.684 + for (int i = 0; i < length; i++) { 1.685 + f->do_oop((oop*) _owned_locks->adr_at(i)); 1.686 + } 1.687 +} 1.688 + 1.689 +ThreadStatistics::ThreadStatistics() { 1.690 + _contended_enter_count = 0; 1.691 + _monitor_wait_count = 0; 1.692 + _sleep_count = 0; 1.693 + _class_init_recursion_count = 0; 1.694 + _class_verify_recursion_count = 0; 1.695 + _count_pending_reset = false; 1.696 + _timer_pending_reset = false; 1.697 +} 1.698 + 1.699 +ThreadSnapshot::ThreadSnapshot(JavaThread* thread) { 1.700 + _thread = thread; 1.701 + _threadObj = thread->threadObj(); 1.702 + _stack_trace = NULL; 1.703 + _concurrent_locks = NULL; 1.704 + _next = NULL; 1.705 + 1.706 + ThreadStatistics* stat = thread->get_thread_stat(); 1.707 + _contended_enter_ticks = stat->contended_enter_ticks(); 1.708 + _contended_enter_count = stat->contended_enter_count(); 1.709 + _monitor_wait_ticks = stat->monitor_wait_ticks(); 1.710 + _monitor_wait_count = stat->monitor_wait_count(); 1.711 + _sleep_ticks = stat->sleep_ticks(); 1.712 + _sleep_count = stat->sleep_count(); 1.713 + 1.714 + _blocker_object = NULL; 1.715 + _blocker_object_owner = NULL; 1.716 + 1.717 + _thread_status = java_lang_Thread::get_thread_status(_threadObj); 1.718 + _is_ext_suspended = thread->is_being_ext_suspended(); 1.719 + _is_in_native = (thread->thread_state() == _thread_in_native); 1.720 + 1.721 + if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER || 1.722 + _thread_status == java_lang_Thread::IN_OBJECT_WAIT || 1.723 + _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) { 1.724 + 1.725 + Handle obj = ThreadService::get_current_contended_monitor(thread); 1.726 + if (obj() == NULL) { 1.727 + // monitor no longer exists; thread is not blocked 1.728 + _thread_status = java_lang_Thread::RUNNABLE; 1.729 + } else { 1.730 + _blocker_object = obj(); 1.731 + JavaThread* owner = ObjectSynchronizer::get_lock_owner(obj, false); 1.732 + if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER) 1.733 + || (owner != NULL && owner->is_attaching())) { 1.734 + // ownership information of the monitor is not available 1.735 + // (may no longer be owned or releasing to some other thread) 1.736 + // make this thread in RUNNABLE state. 1.737 + // And when the owner thread is in attaching state, the java thread 1.738 + // is not completely initialized. For example thread name and id 1.739 + // and may not be set, so hide the attaching thread. 1.740 + _thread_status = java_lang_Thread::RUNNABLE; 1.741 + _blocker_object = NULL; 1.742 + } else if (owner != NULL) { 1.743 + _blocker_object_owner = owner->threadObj(); 1.744 + } 1.745 + } 1.746 + } 1.747 + 1.748 + // Support for JSR-166 locks 1.749 + if (JDK_Version::supports_thread_park_blocker() && 1.750 + (_thread_status == java_lang_Thread::PARKED || 1.751 + _thread_status == java_lang_Thread::PARKED_TIMED)) { 1.752 + 1.753 + _blocker_object = thread->current_park_blocker(); 1.754 + if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) { 1.755 + _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object); 1.756 + } 1.757 + } 1.758 +} 1.759 + 1.760 +ThreadSnapshot::~ThreadSnapshot() { 1.761 + delete _stack_trace; 1.762 + delete _concurrent_locks; 1.763 +} 1.764 + 1.765 +void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) { 1.766 + _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors); 1.767 + _stack_trace->dump_stack_at_safepoint(max_depth); 1.768 +} 1.769 + 1.770 + 1.771 +void ThreadSnapshot::oops_do(OopClosure* f) { 1.772 + f->do_oop(&_threadObj); 1.773 + f->do_oop(&_blocker_object); 1.774 + f->do_oop(&_blocker_object_owner); 1.775 + if (_stack_trace != NULL) { 1.776 + _stack_trace->oops_do(f); 1.777 + } 1.778 + if (_concurrent_locks != NULL) { 1.779 + _concurrent_locks->oops_do(f); 1.780 + } 1.781 +} 1.782 + 1.783 +DeadlockCycle::DeadlockCycle() { 1.784 + _is_deadlock = false; 1.785 + _threads = new (ResourceObj::C_HEAP) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true); 1.786 + _next = NULL; 1.787 +} 1.788 + 1.789 +DeadlockCycle::~DeadlockCycle() { 1.790 + delete _threads; 1.791 +} 1.792 + 1.793 +void DeadlockCycle::print_on(outputStream* st) const { 1.794 + st->cr(); 1.795 + st->print_cr("Found one Java-level deadlock:"); 1.796 + st->print("============================="); 1.797 + 1.798 + JavaThread* currentThread; 1.799 + ObjectMonitor* waitingToLockMonitor; 1.800 + oop waitingToLockBlocker; 1.801 + int len = _threads->length(); 1.802 + for (int i = 0; i < len; i++) { 1.803 + currentThread = _threads->at(i); 1.804 + waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(); 1.805 + waitingToLockBlocker = currentThread->current_park_blocker(); 1.806 + st->cr(); 1.807 + st->print_cr("\"%s\":", currentThread->get_thread_name()); 1.808 + const char* owner_desc = ",\n which is held by"; 1.809 + if (waitingToLockMonitor != NULL) { 1.810 + st->print(" waiting to lock monitor " INTPTR_FORMAT, waitingToLockMonitor); 1.811 + oop obj = (oop)waitingToLockMonitor->object(); 1.812 + if (obj != NULL) { 1.813 + st->print(" (object "INTPTR_FORMAT ", a %s)", (address)obj, 1.814 + (instanceKlass::cast(obj->klass()))->external_name()); 1.815 + 1.816 + if (!currentThread->current_pending_monitor_is_from_java()) { 1.817 + owner_desc = "\n in JNI, which is held by"; 1.818 + } 1.819 + } else { 1.820 + // No Java object associated - a JVMTI raw monitor 1.821 + owner_desc = " (JVMTI raw monitor),\n which is held by"; 1.822 + } 1.823 + currentThread = Threads::owning_thread_from_monitor_owner( 1.824 + (address)waitingToLockMonitor->owner(), false /* no locking needed */); 1.825 + } else { 1.826 + st->print(" waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)", 1.827 + (address)waitingToLockBlocker, 1.828 + (instanceKlass::cast(waitingToLockBlocker->klass()))->external_name()); 1.829 + assert(waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass()), 1.830 + "Must be an AbstractOwnableSynchronizer"); 1.831 + oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker); 1.832 + currentThread = java_lang_Thread::thread(ownerObj); 1.833 + } 1.834 + st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name()); 1.835 + } 1.836 + 1.837 + st->cr(); 1.838 + st->cr(); 1.839 + 1.840 + // Print stack traces 1.841 + bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace; 1.842 + JavaMonitorsInStackTrace = true; 1.843 + st->print_cr("Java stack information for the threads listed above:"); 1.844 + st->print_cr("==================================================="); 1.845 + for (int j = 0; j < len; j++) { 1.846 + currentThread = _threads->at(j); 1.847 + st->print_cr("\"%s\":", currentThread->get_thread_name()); 1.848 + currentThread->print_stack_on(st); 1.849 + } 1.850 + JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace; 1.851 +} 1.852 + 1.853 +ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread, 1.854 + bool include_jvmti_agent_threads, 1.855 + bool include_jni_attaching_threads) { 1.856 + assert(cur_thread == Thread::current(), "Check current thread"); 1.857 + 1.858 + int init_size = ThreadService::get_live_thread_count(); 1.859 + _threads_array = new GrowableArray<instanceHandle>(init_size); 1.860 + 1.861 + MutexLockerEx ml(Threads_lock); 1.862 + 1.863 + for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) { 1.864 + // skips JavaThreads in the process of exiting 1.865 + // and also skips VM internal JavaThreads 1.866 + // Threads in _thread_new or _thread_new_trans state are included. 1.867 + // i.e. threads have been started but not yet running. 1.868 + if (jt->threadObj() == NULL || 1.869 + jt->is_exiting() || 1.870 + !java_lang_Thread::is_alive(jt->threadObj()) || 1.871 + jt->is_hidden_from_external_view()) { 1.872 + continue; 1.873 + } 1.874 + 1.875 + // skip agent threads 1.876 + if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) { 1.877 + continue; 1.878 + } 1.879 + 1.880 + // skip jni threads in the process of attaching 1.881 + if (!include_jni_attaching_threads && jt->is_attaching()) { 1.882 + continue; 1.883 + } 1.884 + 1.885 + instanceHandle h(cur_thread, (instanceOop) jt->threadObj()); 1.886 + _threads_array->append(h); 1.887 + } 1.888 +}