src/share/vm/services/threadService.cpp

Tue, 08 Aug 2017 15:57:29 +0800

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

mercurial