src/share/vm/services/threadService.cpp

Wed, 10 Apr 2013 08:55:50 -0400

author
zgu
date
Wed, 10 Apr 2013 08:55:50 -0400
changeset 4927
35f8765422b9
parent 4673
5ee250974db9
child 5271
ef748153ee8f
permissions
-rw-r--r--

8010151: nsk/regression/b6653214 fails "assert(snapshot != NULL) failed: Worker should not be started"
Summary: Fixed a racing condition when shutting down NMT while worker thread is being started, also fixed a few mis-declared volatile pointers.
Reviewed-by: dholmes, dlong

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

mercurial