src/share/vm/services/threadService.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6911
ce8f6bb717c9
parent 6876
710a3c8b516e
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/systemDictionary.hpp"
aoqi@0 27 #include "memory/allocation.hpp"
aoqi@0 28 #include "memory/heapInspection.hpp"
aoqi@0 29 #include "memory/oopFactory.hpp"
aoqi@0 30 #include "oops/instanceKlass.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "runtime/handles.inline.hpp"
aoqi@0 33 #include "runtime/init.hpp"
aoqi@0 34 #include "runtime/thread.hpp"
aoqi@0 35 #include "runtime/vframe.hpp"
goetz@6911 36 #include "runtime/thread.inline.hpp"
aoqi@0 37 #include "runtime/vmThread.hpp"
aoqi@0 38 #include "runtime/vm_operations.hpp"
aoqi@0 39 #include "services/threadService.hpp"
aoqi@0 40
aoqi@0 41 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 42
aoqi@0 43 // TODO: we need to define a naming convention for perf counters
aoqi@0 44 // to distinguish counters for:
aoqi@0 45 // - standard JSR174 use
aoqi@0 46 // - Hotspot extension (public and committed)
aoqi@0 47 // - Hotspot extension (private/internal and uncommitted)
aoqi@0 48
aoqi@0 49 // Default is disabled.
aoqi@0 50 bool ThreadService::_thread_monitoring_contention_enabled = false;
aoqi@0 51 bool ThreadService::_thread_cpu_time_enabled = false;
aoqi@0 52 bool ThreadService::_thread_allocated_memory_enabled = false;
aoqi@0 53
aoqi@0 54 PerfCounter* ThreadService::_total_threads_count = NULL;
aoqi@0 55 PerfVariable* ThreadService::_live_threads_count = NULL;
aoqi@0 56 PerfVariable* ThreadService::_peak_threads_count = NULL;
aoqi@0 57 PerfVariable* ThreadService::_daemon_threads_count = NULL;
aoqi@0 58 volatile int ThreadService::_exiting_threads_count = 0;
aoqi@0 59 volatile int ThreadService::_exiting_daemon_threads_count = 0;
aoqi@0 60
aoqi@0 61 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
aoqi@0 62
aoqi@0 63 static const int INITIAL_ARRAY_SIZE = 10;
aoqi@0 64
aoqi@0 65 void ThreadService::init() {
aoqi@0 66 EXCEPTION_MARK;
aoqi@0 67
aoqi@0 68 // These counters are for java.lang.management API support.
aoqi@0 69 // They are created even if -XX:-UsePerfData is set and in
aoqi@0 70 // that case, they will be allocated on C heap.
aoqi@0 71
aoqi@0 72 _total_threads_count =
aoqi@0 73 PerfDataManager::create_counter(JAVA_THREADS, "started",
aoqi@0 74 PerfData::U_Events, CHECK);
aoqi@0 75
aoqi@0 76 _live_threads_count =
aoqi@0 77 PerfDataManager::create_variable(JAVA_THREADS, "live",
aoqi@0 78 PerfData::U_None, CHECK);
aoqi@0 79
aoqi@0 80 _peak_threads_count =
aoqi@0 81 PerfDataManager::create_variable(JAVA_THREADS, "livePeak",
aoqi@0 82 PerfData::U_None, CHECK);
aoqi@0 83
aoqi@0 84 _daemon_threads_count =
aoqi@0 85 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
aoqi@0 86 PerfData::U_None, CHECK);
aoqi@0 87
aoqi@0 88 if (os::is_thread_cpu_time_supported()) {
aoqi@0 89 _thread_cpu_time_enabled = true;
aoqi@0 90 }
aoqi@0 91
aoqi@0 92 _thread_allocated_memory_enabled = true; // Always on, so enable it
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 void ThreadService::reset_peak_thread_count() {
aoqi@0 96 // Acquire the lock to update the peak thread count
aoqi@0 97 // to synchronize with thread addition and removal.
aoqi@0 98 MutexLockerEx mu(Threads_lock);
aoqi@0 99 _peak_threads_count->set_value(get_live_thread_count());
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
aoqi@0 103 // Do not count VM internal or JVMTI agent threads
aoqi@0 104 if (thread->is_hidden_from_external_view() ||
aoqi@0 105 thread->is_jvmti_agent_thread()) {
aoqi@0 106 return;
aoqi@0 107 }
aoqi@0 108
aoqi@0 109 _total_threads_count->inc();
aoqi@0 110 _live_threads_count->inc();
aoqi@0 111
aoqi@0 112 if (_live_threads_count->get_value() > _peak_threads_count->get_value()) {
aoqi@0 113 _peak_threads_count->set_value(_live_threads_count->get_value());
aoqi@0 114 }
aoqi@0 115
aoqi@0 116 if (daemon) {
aoqi@0 117 _daemon_threads_count->inc();
aoqi@0 118 }
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
aoqi@0 122 Atomic::dec((jint*) &_exiting_threads_count);
aoqi@0 123
aoqi@0 124 if (thread->is_hidden_from_external_view() ||
aoqi@0 125 thread->is_jvmti_agent_thread()) {
aoqi@0 126 return;
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 _live_threads_count->set_value(_live_threads_count->get_value() - 1);
aoqi@0 130
aoqi@0 131 if (daemon) {
aoqi@0 132 _daemon_threads_count->set_value(_daemon_threads_count->get_value() - 1);
aoqi@0 133 Atomic::dec((jint*) &_exiting_daemon_threads_count);
aoqi@0 134 }
aoqi@0 135 }
aoqi@0 136
aoqi@0 137 void ThreadService::current_thread_exiting(JavaThread* jt) {
aoqi@0 138 assert(jt == JavaThread::current(), "Called by current thread");
aoqi@0 139 Atomic::inc((jint*) &_exiting_threads_count);
aoqi@0 140
aoqi@0 141 oop threadObj = jt->threadObj();
aoqi@0 142 if (threadObj != NULL && java_lang_Thread::is_daemon(threadObj)) {
aoqi@0 143 Atomic::inc((jint*) &_exiting_daemon_threads_count);
aoqi@0 144 }
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 // FIXME: JVMTI should call this function
aoqi@0 148 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
aoqi@0 149 assert(thread != NULL, "should be non-NULL");
aoqi@0 150 assert(Threads_lock->owned_by_self(), "must grab Threads_lock or be at safepoint");
aoqi@0 151
aoqi@0 152 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
aoqi@0 153
aoqi@0 154 oop obj = NULL;
aoqi@0 155 if (wait_obj != NULL) {
aoqi@0 156 // thread is doing an Object.wait() call
aoqi@0 157 obj = (oop) wait_obj->object();
aoqi@0 158 assert(obj != NULL, "Object.wait() should have an object");
aoqi@0 159 } else {
aoqi@0 160 ObjectMonitor *enter_obj = thread->current_pending_monitor();
aoqi@0 161 if (enter_obj != NULL) {
aoqi@0 162 // thread is trying to enter() or raw_enter() an ObjectMonitor.
aoqi@0 163 obj = (oop) enter_obj->object();
aoqi@0 164 }
aoqi@0 165 // If obj == NULL, then ObjectMonitor is raw which doesn't count.
aoqi@0 166 }
aoqi@0 167
aoqi@0 168 Handle h(obj);
aoqi@0 169 return h;
aoqi@0 170 }
aoqi@0 171
aoqi@0 172 bool ThreadService::set_thread_monitoring_contention(bool flag) {
aoqi@0 173 MutexLocker m(Management_lock);
aoqi@0 174
aoqi@0 175 bool prev = _thread_monitoring_contention_enabled;
aoqi@0 176 _thread_monitoring_contention_enabled = flag;
aoqi@0 177
aoqi@0 178 return prev;
aoqi@0 179 }
aoqi@0 180
aoqi@0 181 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
aoqi@0 182 MutexLocker m(Management_lock);
aoqi@0 183
aoqi@0 184 bool prev = _thread_cpu_time_enabled;
aoqi@0 185 _thread_cpu_time_enabled = flag;
aoqi@0 186
aoqi@0 187 return prev;
aoqi@0 188 }
aoqi@0 189
aoqi@0 190 bool ThreadService::set_thread_allocated_memory_enabled(bool flag) {
aoqi@0 191 MutexLocker m(Management_lock);
aoqi@0 192
aoqi@0 193 bool prev = _thread_allocated_memory_enabled;
aoqi@0 194 _thread_allocated_memory_enabled = flag;
aoqi@0 195
aoqi@0 196 return prev;
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 // GC support
aoqi@0 200 void ThreadService::oops_do(OopClosure* f) {
aoqi@0 201 for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
aoqi@0 202 dump->oops_do(f);
aoqi@0 203 }
aoqi@0 204 }
aoqi@0 205
aoqi@0 206 void ThreadService::metadata_do(void f(Metadata*)) {
aoqi@0 207 for (ThreadDumpResult* dump = _threaddump_list; dump != NULL; dump = dump->next()) {
aoqi@0 208 dump->metadata_do(f);
aoqi@0 209 }
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 void ThreadService::add_thread_dump(ThreadDumpResult* dump) {
aoqi@0 213 MutexLocker ml(Management_lock);
aoqi@0 214 if (_threaddump_list == NULL) {
aoqi@0 215 _threaddump_list = dump;
aoqi@0 216 } else {
aoqi@0 217 dump->set_next(_threaddump_list);
aoqi@0 218 _threaddump_list = dump;
aoqi@0 219 }
aoqi@0 220 }
aoqi@0 221
aoqi@0 222 void ThreadService::remove_thread_dump(ThreadDumpResult* dump) {
aoqi@0 223 MutexLocker ml(Management_lock);
aoqi@0 224
aoqi@0 225 ThreadDumpResult* prev = NULL;
aoqi@0 226 bool found = false;
aoqi@0 227 for (ThreadDumpResult* d = _threaddump_list; d != NULL; prev = d, d = d->next()) {
aoqi@0 228 if (d == dump) {
aoqi@0 229 if (prev == NULL) {
aoqi@0 230 _threaddump_list = dump->next();
aoqi@0 231 } else {
aoqi@0 232 prev->set_next(dump->next());
aoqi@0 233 }
aoqi@0 234 found = true;
aoqi@0 235 break;
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238 assert(found, "The threaddump result to be removed must exist.");
aoqi@0 239 }
aoqi@0 240
aoqi@0 241 // Dump stack trace of threads specified in the given threads array.
aoqi@0 242 // Returns StackTraceElement[][] each element is the stack trace of a thread in
aoqi@0 243 // the corresponding entry in the given threads array
aoqi@0 244 Handle ThreadService::dump_stack_traces(GrowableArray<instanceHandle>* threads,
aoqi@0 245 int num_threads,
aoqi@0 246 TRAPS) {
aoqi@0 247 assert(num_threads > 0, "just checking");
aoqi@0 248
aoqi@0 249 ThreadDumpResult dump_result;
aoqi@0 250 VM_ThreadDump op(&dump_result,
aoqi@0 251 threads,
aoqi@0 252 num_threads,
aoqi@0 253 -1, /* entire stack */
aoqi@0 254 false, /* with locked monitors */
aoqi@0 255 false /* with locked synchronizers */);
aoqi@0 256 VMThread::execute(&op);
aoqi@0 257
aoqi@0 258 // Allocate the resulting StackTraceElement[][] object
aoqi@0 259
aoqi@0 260 ResourceMark rm(THREAD);
aoqi@0 261 Klass* k = SystemDictionary::resolve_or_fail(vmSymbols::java_lang_StackTraceElement_array(), true, CHECK_NH);
aoqi@0 262 ObjArrayKlass* ik = ObjArrayKlass::cast(k);
aoqi@0 263 objArrayOop r = oopFactory::new_objArray(ik, num_threads, CHECK_NH);
aoqi@0 264 objArrayHandle result_obj(THREAD, r);
aoqi@0 265
aoqi@0 266 int num_snapshots = dump_result.num_snapshots();
aoqi@0 267 assert(num_snapshots == num_threads, "Must have num_threads thread snapshots");
aoqi@0 268 int i = 0;
aoqi@0 269 for (ThreadSnapshot* ts = dump_result.snapshots(); ts != NULL; i++, ts = ts->next()) {
aoqi@0 270 ThreadStackTrace* stacktrace = ts->get_stack_trace();
aoqi@0 271 if (stacktrace == NULL) {
aoqi@0 272 // No stack trace
aoqi@0 273 result_obj->obj_at_put(i, NULL);
aoqi@0 274 } else {
aoqi@0 275 // Construct an array of java/lang/StackTraceElement object
aoqi@0 276 Handle backtrace_h = stacktrace->allocate_fill_stack_trace_element_array(CHECK_NH);
aoqi@0 277 result_obj->obj_at_put(i, backtrace_h());
aoqi@0 278 }
aoqi@0 279 }
aoqi@0 280
aoqi@0 281 return result_obj;
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
aoqi@0 285 ThreadStatistics* stat = thread->get_thread_stat();
aoqi@0 286 if (stat != NULL) {
aoqi@0 287 stat->reset_count_stat();
aoqi@0 288 }
aoqi@0 289 }
aoqi@0 290
aoqi@0 291 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
aoqi@0 292 ThreadStatistics* stat = thread->get_thread_stat();
aoqi@0 293 if (stat != NULL) {
aoqi@0 294 stat->reset_time_stat();
aoqi@0 295 }
aoqi@0 296 }
aoqi@0 297
aoqi@0 298 // Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true
aoqi@0 299 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(bool concurrent_locks) {
aoqi@0 300 // This code was modified from the original Threads::find_deadlocks code.
aoqi@0 301 int globalDfn = 0, thisDfn;
aoqi@0 302 ObjectMonitor* waitingToLockMonitor = NULL;
aoqi@0 303 oop waitingToLockBlocker = NULL;
aoqi@0 304 bool blocked_on_monitor = false;
aoqi@0 305 JavaThread *currentThread, *previousThread;
aoqi@0 306 int num_deadlocks = 0;
aoqi@0 307
aoqi@0 308 for (JavaThread* p = Threads::first(); p != NULL; p = p->next()) {
aoqi@0 309 // Initialize the depth-first-number
aoqi@0 310 p->set_depth_first_number(-1);
aoqi@0 311 }
aoqi@0 312
aoqi@0 313 DeadlockCycle* deadlocks = NULL;
aoqi@0 314 DeadlockCycle* last = NULL;
aoqi@0 315 DeadlockCycle* cycle = new DeadlockCycle();
aoqi@0 316 for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) {
aoqi@0 317 if (jt->depth_first_number() >= 0) {
aoqi@0 318 // this thread was already visited
aoqi@0 319 continue;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 thisDfn = globalDfn;
aoqi@0 323 jt->set_depth_first_number(globalDfn++);
aoqi@0 324 previousThread = jt;
aoqi@0 325 currentThread = jt;
aoqi@0 326
aoqi@0 327 cycle->reset();
aoqi@0 328
aoqi@0 329 // When there is a deadlock, all the monitors involved in the dependency
aoqi@0 330 // cycle must be contended and heavyweight. So we only care about the
aoqi@0 331 // heavyweight monitor a thread is waiting to lock.
aoqi@0 332 waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor();
aoqi@0 333 if (concurrent_locks) {
aoqi@0 334 waitingToLockBlocker = jt->current_park_blocker();
aoqi@0 335 }
aoqi@0 336 while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) {
aoqi@0 337 cycle->add_thread(currentThread);
aoqi@0 338 if (waitingToLockMonitor != NULL) {
aoqi@0 339 address currentOwner = (address)waitingToLockMonitor->owner();
aoqi@0 340 if (currentOwner != NULL) {
aoqi@0 341 currentThread = Threads::owning_thread_from_monitor_owner(
aoqi@0 342 currentOwner,
aoqi@0 343 false /* no locking needed */);
aoqi@0 344 if (currentThread == NULL) {
aoqi@0 345 // This function is called at a safepoint so the JavaThread
aoqi@0 346 // that owns waitingToLockMonitor should be findable, but
aoqi@0 347 // if it is not findable, then the previous currentThread is
aoqi@0 348 // blocked permanently. We record this as a deadlock.
aoqi@0 349 num_deadlocks++;
aoqi@0 350
aoqi@0 351 cycle->set_deadlock(true);
aoqi@0 352
aoqi@0 353 // add this cycle to the deadlocks list
aoqi@0 354 if (deadlocks == NULL) {
aoqi@0 355 deadlocks = cycle;
aoqi@0 356 } else {
aoqi@0 357 last->set_next(cycle);
aoqi@0 358 }
aoqi@0 359 last = cycle;
aoqi@0 360 cycle = new DeadlockCycle();
aoqi@0 361 break;
aoqi@0 362 }
aoqi@0 363 }
aoqi@0 364 } else {
aoqi@0 365 if (concurrent_locks) {
aoqi@0 366 if (waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) {
aoqi@0 367 oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
aoqi@0 368 currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL;
aoqi@0 369 } else {
aoqi@0 370 currentThread = NULL;
aoqi@0 371 }
aoqi@0 372 }
aoqi@0 373 }
aoqi@0 374
aoqi@0 375 if (currentThread == NULL) {
aoqi@0 376 // No dependency on another thread
aoqi@0 377 break;
aoqi@0 378 }
aoqi@0 379 if (currentThread->depth_first_number() < 0) {
aoqi@0 380 // First visit to this thread
aoqi@0 381 currentThread->set_depth_first_number(globalDfn++);
aoqi@0 382 } else if (currentThread->depth_first_number() < thisDfn) {
aoqi@0 383 // Thread already visited, and not on a (new) cycle
aoqi@0 384 break;
aoqi@0 385 } else if (currentThread == previousThread) {
aoqi@0 386 // Self-loop, ignore
aoqi@0 387 break;
aoqi@0 388 } else {
aoqi@0 389 // We have a (new) cycle
aoqi@0 390 num_deadlocks++;
aoqi@0 391
aoqi@0 392 cycle->set_deadlock(true);
aoqi@0 393
aoqi@0 394 // add this cycle to the deadlocks list
aoqi@0 395 if (deadlocks == NULL) {
aoqi@0 396 deadlocks = cycle;
aoqi@0 397 } else {
aoqi@0 398 last->set_next(cycle);
aoqi@0 399 }
aoqi@0 400 last = cycle;
aoqi@0 401 cycle = new DeadlockCycle();
aoqi@0 402 break;
aoqi@0 403 }
aoqi@0 404 previousThread = currentThread;
aoqi@0 405 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
aoqi@0 406 if (concurrent_locks) {
aoqi@0 407 waitingToLockBlocker = currentThread->current_park_blocker();
aoqi@0 408 }
aoqi@0 409 }
aoqi@0 410
aoqi@0 411 }
aoqi@0 412 delete cycle;
aoqi@0 413 return deadlocks;
aoqi@0 414 }
aoqi@0 415
aoqi@0 416 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) {
aoqi@0 417
aoqi@0 418 // Create a new ThreadDumpResult object and append to the list.
aoqi@0 419 // If GC happens before this function returns, Method*
aoqi@0 420 // in the stack trace will be visited.
aoqi@0 421 ThreadService::add_thread_dump(this);
aoqi@0 422 }
aoqi@0 423
aoqi@0 424 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) {
aoqi@0 425 // Create a new ThreadDumpResult object and append to the list.
aoqi@0 426 // If GC happens before this function returns, oops
aoqi@0 427 // will be visited.
aoqi@0 428 ThreadService::add_thread_dump(this);
aoqi@0 429 }
aoqi@0 430
aoqi@0 431 ThreadDumpResult::~ThreadDumpResult() {
aoqi@0 432 ThreadService::remove_thread_dump(this);
aoqi@0 433
aoqi@0 434 // free all the ThreadSnapshot objects created during
aoqi@0 435 // the VM_ThreadDump operation
aoqi@0 436 ThreadSnapshot* ts = _snapshots;
aoqi@0 437 while (ts != NULL) {
aoqi@0 438 ThreadSnapshot* p = ts;
aoqi@0 439 ts = ts->next();
aoqi@0 440 delete p;
aoqi@0 441 }
aoqi@0 442 }
aoqi@0 443
aoqi@0 444
aoqi@0 445 void ThreadDumpResult::add_thread_snapshot(ThreadSnapshot* ts) {
aoqi@0 446 assert(_num_threads == 0 || _num_snapshots < _num_threads,
aoqi@0 447 "_num_snapshots must be less than _num_threads");
aoqi@0 448 _num_snapshots++;
aoqi@0 449 if (_snapshots == NULL) {
aoqi@0 450 _snapshots = ts;
aoqi@0 451 } else {
aoqi@0 452 _last->set_next(ts);
aoqi@0 453 }
aoqi@0 454 _last = ts;
aoqi@0 455 }
aoqi@0 456
aoqi@0 457 void ThreadDumpResult::oops_do(OopClosure* f) {
aoqi@0 458 for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
aoqi@0 459 ts->oops_do(f);
aoqi@0 460 }
aoqi@0 461 }
aoqi@0 462
aoqi@0 463 void ThreadDumpResult::metadata_do(void f(Metadata*)) {
aoqi@0 464 for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
aoqi@0 465 ts->metadata_do(f);
aoqi@0 466 }
aoqi@0 467 }
aoqi@0 468
aoqi@0 469 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
aoqi@0 470 _method = jvf->method();
aoqi@0 471 _bci = jvf->bci();
aoqi@0 472 _class_holder = _method->method_holder()->klass_holder();
aoqi@0 473 _locked_monitors = NULL;
aoqi@0 474 if (with_lock_info) {
aoqi@0 475 ResourceMark rm;
aoqi@0 476 GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
aoqi@0 477 int length = list->length();
aoqi@0 478 if (length > 0) {
aoqi@0 479 _locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(length, true);
aoqi@0 480 for (int i = 0; i < length; i++) {
aoqi@0 481 MonitorInfo* monitor = list->at(i);
aoqi@0 482 assert(monitor->owner(), "This monitor must have an owning object");
aoqi@0 483 _locked_monitors->append(monitor->owner());
aoqi@0 484 }
aoqi@0 485 }
aoqi@0 486 }
aoqi@0 487 }
aoqi@0 488
aoqi@0 489 void StackFrameInfo::oops_do(OopClosure* f) {
aoqi@0 490 if (_locked_monitors != NULL) {
aoqi@0 491 int length = _locked_monitors->length();
aoqi@0 492 for (int i = 0; i < length; i++) {
aoqi@0 493 f->do_oop((oop*) _locked_monitors->adr_at(i));
aoqi@0 494 }
aoqi@0 495 }
aoqi@0 496 f->do_oop(&_class_holder);
aoqi@0 497 }
aoqi@0 498
aoqi@0 499 void StackFrameInfo::metadata_do(void f(Metadata*)) {
aoqi@0 500 f(_method);
aoqi@0 501 }
aoqi@0 502
aoqi@0 503 void StackFrameInfo::print_on(outputStream* st) const {
aoqi@0 504 ResourceMark rm;
aoqi@0 505 java_lang_Throwable::print_stack_element(st, method(), bci());
aoqi@0 506 int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
aoqi@0 507 for (int i = 0; i < len; i++) {
aoqi@0 508 oop o = _locked_monitors->at(i);
aoqi@0 509 InstanceKlass* ik = InstanceKlass::cast(o->klass());
aoqi@0 510 st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", (address)o, ik->external_name());
aoqi@0 511 }
aoqi@0 512
aoqi@0 513 }
aoqi@0 514
aoqi@0 515 // Iterate through monitor cache to find JNI locked monitors
aoqi@0 516 class InflatedMonitorsClosure: public MonitorClosure {
aoqi@0 517 private:
aoqi@0 518 ThreadStackTrace* _stack_trace;
aoqi@0 519 Thread* _thread;
aoqi@0 520 public:
aoqi@0 521 InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) {
aoqi@0 522 _thread = t;
aoqi@0 523 _stack_trace = st;
aoqi@0 524 }
aoqi@0 525 void do_monitor(ObjectMonitor* mid) {
aoqi@0 526 if (mid->owner() == _thread) {
aoqi@0 527 oop object = (oop) mid->object();
aoqi@0 528 if (!_stack_trace->is_owned_monitor_on_stack(object)) {
aoqi@0 529 _stack_trace->add_jni_locked_monitor(object);
aoqi@0 530 }
aoqi@0 531 }
aoqi@0 532 }
aoqi@0 533 };
aoqi@0 534
aoqi@0 535 ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
aoqi@0 536 _thread = t;
aoqi@0 537 _frames = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, true);
aoqi@0 538 _depth = 0;
aoqi@0 539 _with_locked_monitors = with_locked_monitors;
aoqi@0 540 if (_with_locked_monitors) {
aoqi@0 541 _jni_locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true);
aoqi@0 542 } else {
aoqi@0 543 _jni_locked_monitors = NULL;
aoqi@0 544 }
aoqi@0 545 }
aoqi@0 546
aoqi@0 547 ThreadStackTrace::~ThreadStackTrace() {
aoqi@0 548 for (int i = 0; i < _frames->length(); i++) {
aoqi@0 549 delete _frames->at(i);
aoqi@0 550 }
aoqi@0 551 delete _frames;
aoqi@0 552 if (_jni_locked_monitors != NULL) {
aoqi@0 553 delete _jni_locked_monitors;
aoqi@0 554 }
aoqi@0 555 }
aoqi@0 556
aoqi@0 557 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) {
aoqi@0 558 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
aoqi@0 559
aoqi@0 560 if (_thread->has_last_Java_frame()) {
aoqi@0 561 RegisterMap reg_map(_thread);
aoqi@0 562 vframe* start_vf = _thread->last_java_vframe(&reg_map);
aoqi@0 563 int count = 0;
aoqi@0 564 for (vframe* f = start_vf; f; f = f->sender() ) {
aoqi@0 565 if (f->is_java_frame()) {
aoqi@0 566 javaVFrame* jvf = javaVFrame::cast(f);
aoqi@0 567 add_stack_frame(jvf);
aoqi@0 568 count++;
aoqi@0 569 } else {
aoqi@0 570 // Ignore non-Java frames
aoqi@0 571 }
aoqi@0 572 if (maxDepth > 0 && count == maxDepth) {
aoqi@0 573 // Skip frames if more than maxDepth
aoqi@0 574 break;
aoqi@0 575 }
aoqi@0 576 }
aoqi@0 577 }
aoqi@0 578
aoqi@0 579 if (_with_locked_monitors) {
aoqi@0 580 // Iterate inflated monitors and find monitors locked by this thread
aoqi@0 581 // not found in the stack
aoqi@0 582 InflatedMonitorsClosure imc(_thread, this);
aoqi@0 583 ObjectSynchronizer::monitors_iterate(&imc);
aoqi@0 584 }
aoqi@0 585 }
aoqi@0 586
aoqi@0 587
aoqi@0 588 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
aoqi@0 589 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
aoqi@0 590
aoqi@0 591 bool found = false;
aoqi@0 592 int num_frames = get_stack_depth();
aoqi@0 593 for (int depth = 0; depth < num_frames; depth++) {
aoqi@0 594 StackFrameInfo* frame = stack_frame_at(depth);
aoqi@0 595 int len = frame->num_locked_monitors();
aoqi@0 596 GrowableArray<oop>* locked_monitors = frame->locked_monitors();
aoqi@0 597 for (int j = 0; j < len; j++) {
aoqi@0 598 oop monitor = locked_monitors->at(j);
aoqi@0 599 assert(monitor != NULL && monitor->is_instance(), "must be a Java object");
aoqi@0 600 if (monitor == object) {
aoqi@0 601 found = true;
aoqi@0 602 break;
aoqi@0 603 }
aoqi@0 604 }
aoqi@0 605 }
aoqi@0 606 return found;
aoqi@0 607 }
aoqi@0 608
aoqi@0 609 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
aoqi@0 610 Klass* k = SystemDictionary::StackTraceElement_klass();
aoqi@0 611 assert(k != NULL, "must be loaded in 1.4+");
aoqi@0 612 instanceKlassHandle ik(THREAD, k);
aoqi@0 613
aoqi@0 614 // Allocate an array of java/lang/StackTraceElement object
aoqi@0 615 objArrayOop ste = oopFactory::new_objArray(ik(), _depth, CHECK_NH);
aoqi@0 616 objArrayHandle backtrace(THREAD, ste);
aoqi@0 617 for (int j = 0; j < _depth; j++) {
aoqi@0 618 StackFrameInfo* frame = _frames->at(j);
aoqi@0 619 methodHandle mh(THREAD, frame->method());
aoqi@0 620 oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
aoqi@0 621 backtrace->obj_at_put(j, element);
aoqi@0 622 }
aoqi@0 623 return backtrace;
aoqi@0 624 }
aoqi@0 625
aoqi@0 626 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
aoqi@0 627 StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
aoqi@0 628 _frames->append(frame);
aoqi@0 629 _depth++;
aoqi@0 630 }
aoqi@0 631
aoqi@0 632 void ThreadStackTrace::oops_do(OopClosure* f) {
aoqi@0 633 int length = _frames->length();
aoqi@0 634 for (int i = 0; i < length; i++) {
aoqi@0 635 _frames->at(i)->oops_do(f);
aoqi@0 636 }
aoqi@0 637
aoqi@0 638 length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0);
aoqi@0 639 for (int j = 0; j < length; j++) {
aoqi@0 640 f->do_oop((oop*) _jni_locked_monitors->adr_at(j));
aoqi@0 641 }
aoqi@0 642 }
aoqi@0 643
aoqi@0 644 void ThreadStackTrace::metadata_do(void f(Metadata*)) {
aoqi@0 645 int length = _frames->length();
aoqi@0 646 for (int i = 0; i < length; i++) {
aoqi@0 647 _frames->at(i)->metadata_do(f);
aoqi@0 648 }
aoqi@0 649 }
aoqi@0 650
aoqi@0 651
aoqi@0 652 ConcurrentLocksDump::~ConcurrentLocksDump() {
aoqi@0 653 if (_retain_map_on_free) {
aoqi@0 654 return;
aoqi@0 655 }
aoqi@0 656
aoqi@0 657 for (ThreadConcurrentLocks* t = _map; t != NULL;) {
aoqi@0 658 ThreadConcurrentLocks* tcl = t;
aoqi@0 659 t = t->next();
aoqi@0 660 delete tcl;
aoqi@0 661 }
aoqi@0 662 }
aoqi@0 663
aoqi@0 664 void ConcurrentLocksDump::dump_at_safepoint() {
aoqi@0 665 // dump all locked concurrent locks
aoqi@0 666 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
aoqi@0 667
aoqi@0 668 if (JDK_Version::is_gte_jdk16x_version()) {
aoqi@0 669 ResourceMark rm;
aoqi@0 670
aoqi@0 671 GrowableArray<oop>* aos_objects = new GrowableArray<oop>(INITIAL_ARRAY_SIZE);
aoqi@0 672
aoqi@0 673 // Find all instances of AbstractOwnableSynchronizer
aoqi@0 674 HeapInspection::find_instances_at_safepoint(SystemDictionary::abstract_ownable_synchronizer_klass(),
aoqi@0 675 aos_objects);
aoqi@0 676 // Build a map of thread to its owned AQS locks
aoqi@0 677 build_map(aos_objects);
aoqi@0 678 }
aoqi@0 679 }
aoqi@0 680
aoqi@0 681
aoqi@0 682 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
aoqi@0 683 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
aoqi@0 684 int length = aos_objects->length();
aoqi@0 685 for (int i = 0; i < length; i++) {
aoqi@0 686 oop o = aos_objects->at(i);
aoqi@0 687 oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
aoqi@0 688 if (owner_thread_obj != NULL) {
aoqi@0 689 JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
aoqi@0 690 assert(o->is_instance(), "Must be an instanceOop");
aoqi@0 691 add_lock(thread, (instanceOop) o);
aoqi@0 692 }
aoqi@0 693 }
aoqi@0 694 }
aoqi@0 695
aoqi@0 696 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
aoqi@0 697 ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
aoqi@0 698 if (tcl != NULL) {
aoqi@0 699 tcl->add_lock(o);
aoqi@0 700 return;
aoqi@0 701 }
aoqi@0 702
aoqi@0 703 // First owned lock found for this thread
aoqi@0 704 tcl = new ThreadConcurrentLocks(thread);
aoqi@0 705 tcl->add_lock(o);
aoqi@0 706 if (_map == NULL) {
aoqi@0 707 _map = tcl;
aoqi@0 708 } else {
aoqi@0 709 _last->set_next(tcl);
aoqi@0 710 }
aoqi@0 711 _last = tcl;
aoqi@0 712 }
aoqi@0 713
aoqi@0 714 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
aoqi@0 715 for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
aoqi@0 716 if (tcl->java_thread() == thread) {
aoqi@0 717 return tcl;
aoqi@0 718 }
aoqi@0 719 }
aoqi@0 720 return NULL;
aoqi@0 721 }
aoqi@0 722
aoqi@0 723 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
aoqi@0 724 st->print_cr(" Locked ownable synchronizers:");
aoqi@0 725 ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
aoqi@0 726 GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
aoqi@0 727 if (locks == NULL || locks->is_empty()) {
aoqi@0 728 st->print_cr("\t- None");
aoqi@0 729 st->cr();
aoqi@0 730 return;
aoqi@0 731 }
aoqi@0 732
aoqi@0 733 for (int i = 0; i < locks->length(); i++) {
aoqi@0 734 instanceOop obj = locks->at(i);
aoqi@0 735 InstanceKlass* ik = InstanceKlass::cast(obj->klass());
aoqi@0 736 st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", (address)obj, ik->external_name());
aoqi@0 737 }
aoqi@0 738 st->cr();
aoqi@0 739 }
aoqi@0 740
aoqi@0 741 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
aoqi@0 742 _thread = thread;
aoqi@0 743 _owned_locks = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceOop>(INITIAL_ARRAY_SIZE, true);
aoqi@0 744 _next = NULL;
aoqi@0 745 }
aoqi@0 746
aoqi@0 747 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
aoqi@0 748 delete _owned_locks;
aoqi@0 749 }
aoqi@0 750
aoqi@0 751 void ThreadConcurrentLocks::add_lock(instanceOop o) {
aoqi@0 752 _owned_locks->append(o);
aoqi@0 753 }
aoqi@0 754
aoqi@0 755 void ThreadConcurrentLocks::oops_do(OopClosure* f) {
aoqi@0 756 int length = _owned_locks->length();
aoqi@0 757 for (int i = 0; i < length; i++) {
aoqi@0 758 f->do_oop((oop*) _owned_locks->adr_at(i));
aoqi@0 759 }
aoqi@0 760 }
aoqi@0 761
aoqi@0 762 ThreadStatistics::ThreadStatistics() {
aoqi@0 763 _contended_enter_count = 0;
aoqi@0 764 _monitor_wait_count = 0;
aoqi@0 765 _sleep_count = 0;
aoqi@0 766 _count_pending_reset = false;
aoqi@0 767 _timer_pending_reset = false;
aoqi@0 768 memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
aoqi@0 769 }
aoqi@0 770
aoqi@0 771 ThreadSnapshot::ThreadSnapshot(JavaThread* thread) {
aoqi@0 772 _thread = thread;
aoqi@0 773 _threadObj = thread->threadObj();
aoqi@0 774 _stack_trace = NULL;
aoqi@0 775 _concurrent_locks = NULL;
aoqi@0 776 _next = NULL;
aoqi@0 777
aoqi@0 778 ThreadStatistics* stat = thread->get_thread_stat();
aoqi@0 779 _contended_enter_ticks = stat->contended_enter_ticks();
aoqi@0 780 _contended_enter_count = stat->contended_enter_count();
aoqi@0 781 _monitor_wait_ticks = stat->monitor_wait_ticks();
aoqi@0 782 _monitor_wait_count = stat->monitor_wait_count();
aoqi@0 783 _sleep_ticks = stat->sleep_ticks();
aoqi@0 784 _sleep_count = stat->sleep_count();
aoqi@0 785
aoqi@0 786 _blocker_object = NULL;
aoqi@0 787 _blocker_object_owner = NULL;
aoqi@0 788
aoqi@0 789 _thread_status = java_lang_Thread::get_thread_status(_threadObj);
aoqi@0 790 _is_ext_suspended = thread->is_being_ext_suspended();
aoqi@0 791 _is_in_native = (thread->thread_state() == _thread_in_native);
aoqi@0 792
aoqi@0 793 if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER ||
aoqi@0 794 _thread_status == java_lang_Thread::IN_OBJECT_WAIT ||
aoqi@0 795 _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) {
aoqi@0 796
aoqi@0 797 Handle obj = ThreadService::get_current_contended_monitor(thread);
aoqi@0 798 if (obj() == NULL) {
aoqi@0 799 // monitor no longer exists; thread is not blocked
aoqi@0 800 _thread_status = java_lang_Thread::RUNNABLE;
aoqi@0 801 } else {
aoqi@0 802 _blocker_object = obj();
aoqi@0 803 JavaThread* owner = ObjectSynchronizer::get_lock_owner(obj, false);
aoqi@0 804 if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER)
aoqi@0 805 || (owner != NULL && owner->is_attaching_via_jni())) {
aoqi@0 806 // ownership information of the monitor is not available
aoqi@0 807 // (may no longer be owned or releasing to some other thread)
aoqi@0 808 // make this thread in RUNNABLE state.
aoqi@0 809 // And when the owner thread is in attaching state, the java thread
aoqi@0 810 // is not completely initialized. For example thread name and id
aoqi@0 811 // and may not be set, so hide the attaching thread.
aoqi@0 812 _thread_status = java_lang_Thread::RUNNABLE;
aoqi@0 813 _blocker_object = NULL;
aoqi@0 814 } else if (owner != NULL) {
aoqi@0 815 _blocker_object_owner = owner->threadObj();
aoqi@0 816 }
aoqi@0 817 }
aoqi@0 818 }
aoqi@0 819
aoqi@0 820 // Support for JSR-166 locks
aoqi@0 821 if (JDK_Version::current().supports_thread_park_blocker() &&
aoqi@0 822 (_thread_status == java_lang_Thread::PARKED ||
aoqi@0 823 _thread_status == java_lang_Thread::PARKED_TIMED)) {
aoqi@0 824
aoqi@0 825 _blocker_object = thread->current_park_blocker();
aoqi@0 826 if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) {
aoqi@0 827 _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object);
aoqi@0 828 }
aoqi@0 829 }
aoqi@0 830 }
aoqi@0 831
aoqi@0 832 ThreadSnapshot::~ThreadSnapshot() {
aoqi@0 833 delete _stack_trace;
aoqi@0 834 delete _concurrent_locks;
aoqi@0 835 }
aoqi@0 836
aoqi@0 837 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) {
aoqi@0 838 _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
aoqi@0 839 _stack_trace->dump_stack_at_safepoint(max_depth);
aoqi@0 840 }
aoqi@0 841
aoqi@0 842
aoqi@0 843 void ThreadSnapshot::oops_do(OopClosure* f) {
aoqi@0 844 f->do_oop(&_threadObj);
aoqi@0 845 f->do_oop(&_blocker_object);
aoqi@0 846 f->do_oop(&_blocker_object_owner);
aoqi@0 847 if (_stack_trace != NULL) {
aoqi@0 848 _stack_trace->oops_do(f);
aoqi@0 849 }
aoqi@0 850 if (_concurrent_locks != NULL) {
aoqi@0 851 _concurrent_locks->oops_do(f);
aoqi@0 852 }
aoqi@0 853 }
aoqi@0 854
aoqi@0 855 void ThreadSnapshot::metadata_do(void f(Metadata*)) {
aoqi@0 856 if (_stack_trace != NULL) {
aoqi@0 857 _stack_trace->metadata_do(f);
aoqi@0 858 }
aoqi@0 859 }
aoqi@0 860
aoqi@0 861
aoqi@0 862 DeadlockCycle::DeadlockCycle() {
aoqi@0 863 _is_deadlock = false;
aoqi@0 864 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
aoqi@0 865 _next = NULL;
aoqi@0 866 }
aoqi@0 867
aoqi@0 868 DeadlockCycle::~DeadlockCycle() {
aoqi@0 869 delete _threads;
aoqi@0 870 }
aoqi@0 871
aoqi@0 872 void DeadlockCycle::print_on(outputStream* st) const {
aoqi@0 873 st->cr();
aoqi@0 874 st->print_cr("Found one Java-level deadlock:");
aoqi@0 875 st->print("=============================");
aoqi@0 876
aoqi@0 877 JavaThread* currentThread;
aoqi@0 878 ObjectMonitor* waitingToLockMonitor;
aoqi@0 879 oop waitingToLockBlocker;
aoqi@0 880 int len = _threads->length();
aoqi@0 881 for (int i = 0; i < len; i++) {
aoqi@0 882 currentThread = _threads->at(i);
aoqi@0 883 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
aoqi@0 884 waitingToLockBlocker = currentThread->current_park_blocker();
aoqi@0 885 st->cr();
aoqi@0 886 st->print_cr("\"%s\":", currentThread->get_thread_name());
aoqi@0 887 const char* owner_desc = ",\n which is held by";
aoqi@0 888 if (waitingToLockMonitor != NULL) {
aoqi@0 889 st->print(" waiting to lock monitor " INTPTR_FORMAT, waitingToLockMonitor);
aoqi@0 890 oop obj = (oop)waitingToLockMonitor->object();
aoqi@0 891 if (obj != NULL) {
aoqi@0 892 st->print(" (object "INTPTR_FORMAT ", a %s)", (address)obj,
aoqi@0 893 (InstanceKlass::cast(obj->klass()))->external_name());
aoqi@0 894
aoqi@0 895 if (!currentThread->current_pending_monitor_is_from_java()) {
aoqi@0 896 owner_desc = "\n in JNI, which is held by";
aoqi@0 897 }
aoqi@0 898 } else {
aoqi@0 899 // No Java object associated - a JVMTI raw monitor
aoqi@0 900 owner_desc = " (JVMTI raw monitor),\n which is held by";
aoqi@0 901 }
aoqi@0 902 currentThread = Threads::owning_thread_from_monitor_owner(
aoqi@0 903 (address)waitingToLockMonitor->owner(),
aoqi@0 904 false /* no locking needed */);
aoqi@0 905 if (currentThread == NULL) {
aoqi@0 906 // The deadlock was detected at a safepoint so the JavaThread
aoqi@0 907 // that owns waitingToLockMonitor should be findable, but
aoqi@0 908 // if it is not findable, then the previous currentThread is
aoqi@0 909 // blocked permanently.
aoqi@0 910 st->print("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
aoqi@0 911 (address)waitingToLockMonitor->owner());
aoqi@0 912 continue;
aoqi@0 913 }
aoqi@0 914 } else {
aoqi@0 915 st->print(" waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
aoqi@0 916 (address)waitingToLockBlocker,
aoqi@0 917 (InstanceKlass::cast(waitingToLockBlocker->klass()))->external_name());
aoqi@0 918 assert(waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass()),
aoqi@0 919 "Must be an AbstractOwnableSynchronizer");
aoqi@0 920 oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
aoqi@0 921 currentThread = java_lang_Thread::thread(ownerObj);
aoqi@0 922 }
aoqi@0 923 st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name());
aoqi@0 924 }
aoqi@0 925
aoqi@0 926 st->cr();
aoqi@0 927 st->cr();
aoqi@0 928
aoqi@0 929 // Print stack traces
aoqi@0 930 bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
aoqi@0 931 JavaMonitorsInStackTrace = true;
aoqi@0 932 st->print_cr("Java stack information for the threads listed above:");
aoqi@0 933 st->print_cr("===================================================");
aoqi@0 934 for (int j = 0; j < len; j++) {
aoqi@0 935 currentThread = _threads->at(j);
aoqi@0 936 st->print_cr("\"%s\":", currentThread->get_thread_name());
aoqi@0 937 currentThread->print_stack_on(st);
aoqi@0 938 }
aoqi@0 939 JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
aoqi@0 940 }
aoqi@0 941
aoqi@0 942 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
aoqi@0 943 bool include_jvmti_agent_threads,
aoqi@0 944 bool include_jni_attaching_threads) {
aoqi@0 945 assert(cur_thread == Thread::current(), "Check current thread");
aoqi@0 946
aoqi@0 947 int init_size = ThreadService::get_live_thread_count();
aoqi@0 948 _threads_array = new GrowableArray<instanceHandle>(init_size);
aoqi@0 949
aoqi@0 950 MutexLockerEx ml(Threads_lock);
aoqi@0 951
aoqi@0 952 for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) {
aoqi@0 953 // skips JavaThreads in the process of exiting
aoqi@0 954 // and also skips VM internal JavaThreads
aoqi@0 955 // Threads in _thread_new or _thread_new_trans state are included.
aoqi@0 956 // i.e. threads have been started but not yet running.
aoqi@0 957 if (jt->threadObj() == NULL ||
aoqi@0 958 jt->is_exiting() ||
aoqi@0 959 !java_lang_Thread::is_alive(jt->threadObj()) ||
aoqi@0 960 jt->is_hidden_from_external_view()) {
aoqi@0 961 continue;
aoqi@0 962 }
aoqi@0 963
aoqi@0 964 // skip agent threads
aoqi@0 965 if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
aoqi@0 966 continue;
aoqi@0 967 }
aoqi@0 968
aoqi@0 969 // skip jni threads in the process of attaching
aoqi@0 970 if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
aoqi@0 971 continue;
aoqi@0 972 }
aoqi@0 973
aoqi@0 974 instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
aoqi@0 975 _threads_array->append(h);
aoqi@0 976 }
aoqi@0 977 }

mercurial