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