src/share/vm/services/threadService.cpp

Mon, 12 Nov 2012 16:15:05 -0500

author
hseigel
date
Mon, 12 Nov 2012 16:15:05 -0500
changeset 4278
070d523b96a7
parent 4142
d8ce2825b193
child 4673
5ee250974db9
permissions
-rw-r--r--

8001471: Klass::cast() does nothing
Summary: Remove function Klass::cast() and calls to it.
Reviewed-by: dholmes, coleenp

duke@435 1 /*
coleenp@4037 2 * Copyright (c) 2003, 2012, 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) {
duke@435 330 currentThread = Threads::owning_thread_from_monitor_owner((address)waitingToLockMonitor->owner(),
duke@435 331 false /* no locking needed */);
duke@435 332 } else {
duke@435 333 if (concurrent_locks) {
duke@435 334 if (waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) {
duke@435 335 oop threadObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
duke@435 336 currentThread = threadObj != NULL ? java_lang_Thread::thread(threadObj) : NULL;
duke@435 337 } else {
duke@435 338 currentThread = NULL;
duke@435 339 }
duke@435 340 }
duke@435 341 }
duke@435 342
duke@435 343 if (currentThread == NULL) {
duke@435 344 // No dependency on another thread
duke@435 345 break;
duke@435 346 }
duke@435 347 if (currentThread->depth_first_number() < 0) {
duke@435 348 // First visit to this thread
duke@435 349 currentThread->set_depth_first_number(globalDfn++);
duke@435 350 } else if (currentThread->depth_first_number() < thisDfn) {
duke@435 351 // Thread already visited, and not on a (new) cycle
duke@435 352 break;
duke@435 353 } else if (currentThread == previousThread) {
duke@435 354 // Self-loop, ignore
duke@435 355 break;
duke@435 356 } else {
duke@435 357 // We have a (new) cycle
duke@435 358 num_deadlocks++;
duke@435 359
duke@435 360 cycle->set_deadlock(true);
duke@435 361
duke@435 362 // add this cycle to the deadlocks list
duke@435 363 if (deadlocks == NULL) {
duke@435 364 deadlocks = cycle;
duke@435 365 } else {
duke@435 366 last->set_next(cycle);
duke@435 367 }
duke@435 368 last = cycle;
duke@435 369 cycle = new DeadlockCycle();
duke@435 370 break;
duke@435 371 }
duke@435 372 previousThread = currentThread;
duke@435 373 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
duke@435 374 if (concurrent_locks) {
duke@435 375 waitingToLockBlocker = currentThread->current_park_blocker();
duke@435 376 }
duke@435 377 }
duke@435 378
duke@435 379 }
fparain@3381 380 delete cycle;
duke@435 381 return deadlocks;
duke@435 382 }
duke@435 383
duke@435 384 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) {
duke@435 385
duke@435 386 // Create a new ThreadDumpResult object and append to the list.
coleenp@4037 387 // If GC happens before this function returns, Method*
duke@435 388 // in the stack trace will be visited.
duke@435 389 ThreadService::add_thread_dump(this);
duke@435 390 }
duke@435 391
duke@435 392 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _next(NULL), _last(NULL) {
duke@435 393 // Create a new ThreadDumpResult object and append to the list.
duke@435 394 // If GC happens before this function returns, oops
duke@435 395 // will be visited.
duke@435 396 ThreadService::add_thread_dump(this);
duke@435 397 }
duke@435 398
duke@435 399 ThreadDumpResult::~ThreadDumpResult() {
duke@435 400 ThreadService::remove_thread_dump(this);
duke@435 401
duke@435 402 // free all the ThreadSnapshot objects created during
duke@435 403 // the VM_ThreadDump operation
duke@435 404 ThreadSnapshot* ts = _snapshots;
duke@435 405 while (ts != NULL) {
duke@435 406 ThreadSnapshot* p = ts;
duke@435 407 ts = ts->next();
duke@435 408 delete p;
duke@435 409 }
duke@435 410 }
duke@435 411
duke@435 412
duke@435 413 void ThreadDumpResult::add_thread_snapshot(ThreadSnapshot* ts) {
duke@435 414 assert(_num_threads == 0 || _num_snapshots < _num_threads,
duke@435 415 "_num_snapshots must be less than _num_threads");
duke@435 416 _num_snapshots++;
duke@435 417 if (_snapshots == NULL) {
duke@435 418 _snapshots = ts;
duke@435 419 } else {
duke@435 420 _last->set_next(ts);
duke@435 421 }
duke@435 422 _last = ts;
duke@435 423 }
duke@435 424
duke@435 425 void ThreadDumpResult::oops_do(OopClosure* f) {
duke@435 426 for (ThreadSnapshot* ts = _snapshots; ts != NULL; ts = ts->next()) {
duke@435 427 ts->oops_do(f);
duke@435 428 }
duke@435 429 }
duke@435 430
duke@435 431 StackFrameInfo::StackFrameInfo(javaVFrame* jvf, bool with_lock_info) {
duke@435 432 _method = jvf->method();
duke@435 433 _bci = jvf->bci();
duke@435 434 _locked_monitors = NULL;
duke@435 435 if (with_lock_info) {
duke@435 436 ResourceMark rm;
duke@435 437 GrowableArray<MonitorInfo*>* list = jvf->locked_monitors();
duke@435 438 int length = list->length();
duke@435 439 if (length > 0) {
zgu@3900 440 _locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(length, true);
duke@435 441 for (int i = 0; i < length; i++) {
duke@435 442 MonitorInfo* monitor = list->at(i);
duke@435 443 assert(monitor->owner(), "This monitor must have an owning object");
duke@435 444 _locked_monitors->append(monitor->owner());
duke@435 445 }
duke@435 446 }
duke@435 447 }
duke@435 448 }
duke@435 449
duke@435 450 void StackFrameInfo::oops_do(OopClosure* f) {
duke@435 451 if (_locked_monitors != NULL) {
duke@435 452 int length = _locked_monitors->length();
duke@435 453 for (int i = 0; i < length; i++) {
duke@435 454 f->do_oop((oop*) _locked_monitors->adr_at(i));
duke@435 455 }
duke@435 456 }
duke@435 457 }
duke@435 458
duke@435 459 void StackFrameInfo::print_on(outputStream* st) const {
duke@435 460 ResourceMark rm;
duke@435 461 java_lang_Throwable::print_stack_element(st, method(), bci());
duke@435 462 int len = (_locked_monitors != NULL ? _locked_monitors->length() : 0);
duke@435 463 for (int i = 0; i < len; i++) {
duke@435 464 oop o = _locked_monitors->at(i);
coleenp@4037 465 InstanceKlass* ik = InstanceKlass::cast(o->klass());
duke@435 466 st->print_cr("\t- locked <" INTPTR_FORMAT "> (a %s)", (address)o, ik->external_name());
duke@435 467 }
duke@435 468
duke@435 469 }
duke@435 470
duke@435 471 // Iterate through monitor cache to find JNI locked monitors
duke@435 472 class InflatedMonitorsClosure: public MonitorClosure {
duke@435 473 private:
duke@435 474 ThreadStackTrace* _stack_trace;
duke@435 475 Thread* _thread;
duke@435 476 public:
duke@435 477 InflatedMonitorsClosure(Thread* t, ThreadStackTrace* st) {
duke@435 478 _thread = t;
duke@435 479 _stack_trace = st;
duke@435 480 }
duke@435 481 void do_monitor(ObjectMonitor* mid) {
duke@435 482 if (mid->owner() == _thread) {
duke@435 483 oop object = (oop) mid->object();
duke@435 484 if (!_stack_trace->is_owned_monitor_on_stack(object)) {
duke@435 485 _stack_trace->add_jni_locked_monitor(object);
duke@435 486 }
duke@435 487 }
duke@435 488 }
duke@435 489 };
duke@435 490
duke@435 491 ThreadStackTrace::ThreadStackTrace(JavaThread* t, bool with_locked_monitors) {
duke@435 492 _thread = t;
zgu@3900 493 _frames = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<StackFrameInfo*>(INITIAL_ARRAY_SIZE, true);
duke@435 494 _depth = 0;
duke@435 495 _with_locked_monitors = with_locked_monitors;
duke@435 496 if (_with_locked_monitors) {
zgu@3900 497 _jni_locked_monitors = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<oop>(INITIAL_ARRAY_SIZE, true);
duke@435 498 } else {
duke@435 499 _jni_locked_monitors = NULL;
duke@435 500 }
duke@435 501 }
duke@435 502
duke@435 503 ThreadStackTrace::~ThreadStackTrace() {
duke@435 504 for (int i = 0; i < _frames->length(); i++) {
duke@435 505 delete _frames->at(i);
duke@435 506 }
duke@435 507 delete _frames;
duke@435 508 if (_jni_locked_monitors != NULL) {
duke@435 509 delete _jni_locked_monitors;
duke@435 510 }
duke@435 511 }
duke@435 512
duke@435 513 void ThreadStackTrace::dump_stack_at_safepoint(int maxDepth) {
duke@435 514 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
duke@435 515
duke@435 516 if (_thread->has_last_Java_frame()) {
duke@435 517 RegisterMap reg_map(_thread);
duke@435 518 vframe* start_vf = _thread->last_java_vframe(&reg_map);
duke@435 519 int count = 0;
duke@435 520 for (vframe* f = start_vf; f; f = f->sender() ) {
duke@435 521 if (f->is_java_frame()) {
duke@435 522 javaVFrame* jvf = javaVFrame::cast(f);
duke@435 523 add_stack_frame(jvf);
duke@435 524 count++;
duke@435 525 } else {
duke@435 526 // Ignore non-Java frames
duke@435 527 }
duke@435 528 if (maxDepth > 0 && count == maxDepth) {
duke@435 529 // Skip frames if more than maxDepth
duke@435 530 break;
duke@435 531 }
duke@435 532 }
duke@435 533 }
duke@435 534
duke@435 535 if (_with_locked_monitors) {
duke@435 536 // Iterate inflated monitors and find monitors locked by this thread
duke@435 537 // not found in the stack
duke@435 538 InflatedMonitorsClosure imc(_thread, this);
duke@435 539 ObjectSynchronizer::monitors_iterate(&imc);
duke@435 540 }
duke@435 541 }
duke@435 542
duke@435 543
duke@435 544 bool ThreadStackTrace::is_owned_monitor_on_stack(oop object) {
duke@435 545 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
duke@435 546
duke@435 547 bool found = false;
duke@435 548 int num_frames = get_stack_depth();
duke@435 549 for (int depth = 0; depth < num_frames; depth++) {
duke@435 550 StackFrameInfo* frame = stack_frame_at(depth);
duke@435 551 int len = frame->num_locked_monitors();
duke@435 552 GrowableArray<oop>* locked_monitors = frame->locked_monitors();
duke@435 553 for (int j = 0; j < len; j++) {
duke@435 554 oop monitor = locked_monitors->at(j);
duke@435 555 assert(monitor != NULL && monitor->is_instance(), "must be a Java object");
duke@435 556 if (monitor == object) {
duke@435 557 found = true;
duke@435 558 break;
duke@435 559 }
duke@435 560 }
duke@435 561 }
duke@435 562 return found;
duke@435 563 }
duke@435 564
duke@435 565 Handle ThreadStackTrace::allocate_fill_stack_trace_element_array(TRAPS) {
coleenp@4037 566 Klass* k = SystemDictionary::StackTraceElement_klass();
jrose@567 567 assert(k != NULL, "must be loaded in 1.4+");
duke@435 568 instanceKlassHandle ik(THREAD, k);
duke@435 569
duke@435 570 // Allocate an array of java/lang/StackTraceElement object
duke@435 571 objArrayOop ste = oopFactory::new_objArray(ik(), _depth, CHECK_NH);
duke@435 572 objArrayHandle backtrace(THREAD, ste);
duke@435 573 for (int j = 0; j < _depth; j++) {
duke@435 574 StackFrameInfo* frame = _frames->at(j);
duke@435 575 methodHandle mh(THREAD, frame->method());
duke@435 576 oop element = java_lang_StackTraceElement::create(mh, frame->bci(), CHECK_NH);
duke@435 577 backtrace->obj_at_put(j, element);
duke@435 578 }
duke@435 579 return backtrace;
duke@435 580 }
duke@435 581
duke@435 582 void ThreadStackTrace::add_stack_frame(javaVFrame* jvf) {
duke@435 583 StackFrameInfo* frame = new StackFrameInfo(jvf, _with_locked_monitors);
duke@435 584 _frames->append(frame);
duke@435 585 _depth++;
duke@435 586 }
duke@435 587
duke@435 588 void ThreadStackTrace::oops_do(OopClosure* f) {
duke@435 589 int length = _frames->length();
duke@435 590 for (int i = 0; i < length; i++) {
duke@435 591 _frames->at(i)->oops_do(f);
duke@435 592 }
duke@435 593
duke@435 594 length = (_jni_locked_monitors != NULL ? _jni_locked_monitors->length() : 0);
duke@435 595 for (int j = 0; j < length; j++) {
duke@435 596 f->do_oop((oop*) _jni_locked_monitors->adr_at(j));
duke@435 597 }
duke@435 598 }
duke@435 599
duke@435 600 ConcurrentLocksDump::~ConcurrentLocksDump() {
duke@435 601 if (_retain_map_on_free) {
duke@435 602 return;
duke@435 603 }
duke@435 604
duke@435 605 for (ThreadConcurrentLocks* t = _map; t != NULL;) {
duke@435 606 ThreadConcurrentLocks* tcl = t;
duke@435 607 t = t->next();
duke@435 608 delete tcl;
duke@435 609 }
duke@435 610 }
duke@435 611
duke@435 612 void ConcurrentLocksDump::dump_at_safepoint() {
duke@435 613 // dump all locked concurrent locks
duke@435 614 assert(SafepointSynchronize::is_at_safepoint(), "all threads are stopped");
duke@435 615
duke@435 616 if (JDK_Version::is_gte_jdk16x_version()) {
duke@435 617 ResourceMark rm;
duke@435 618
duke@435 619 GrowableArray<oop>* aos_objects = new GrowableArray<oop>(INITIAL_ARRAY_SIZE);
duke@435 620
duke@435 621 // Find all instances of AbstractOwnableSynchronizer
duke@435 622 HeapInspection::find_instances_at_safepoint(SystemDictionary::abstract_ownable_synchronizer_klass(),
duke@435 623 aos_objects);
duke@435 624 // Build a map of thread to its owned AQS locks
duke@435 625 build_map(aos_objects);
duke@435 626 }
duke@435 627 }
duke@435 628
duke@435 629
duke@435 630 // build a map of JavaThread to all its owned AbstractOwnableSynchronizer
duke@435 631 void ConcurrentLocksDump::build_map(GrowableArray<oop>* aos_objects) {
duke@435 632 int length = aos_objects->length();
duke@435 633 for (int i = 0; i < length; i++) {
duke@435 634 oop o = aos_objects->at(i);
duke@435 635 oop owner_thread_obj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(o);
duke@435 636 if (owner_thread_obj != NULL) {
duke@435 637 JavaThread* thread = java_lang_Thread::thread(owner_thread_obj);
duke@435 638 assert(o->is_instance(), "Must be an instanceOop");
duke@435 639 add_lock(thread, (instanceOop) o);
duke@435 640 }
duke@435 641 }
duke@435 642 }
duke@435 643
duke@435 644 void ConcurrentLocksDump::add_lock(JavaThread* thread, instanceOop o) {
duke@435 645 ThreadConcurrentLocks* tcl = thread_concurrent_locks(thread);
duke@435 646 if (tcl != NULL) {
duke@435 647 tcl->add_lock(o);
duke@435 648 return;
duke@435 649 }
duke@435 650
duke@435 651 // First owned lock found for this thread
duke@435 652 tcl = new ThreadConcurrentLocks(thread);
duke@435 653 tcl->add_lock(o);
duke@435 654 if (_map == NULL) {
duke@435 655 _map = tcl;
duke@435 656 } else {
duke@435 657 _last->set_next(tcl);
duke@435 658 }
duke@435 659 _last = tcl;
duke@435 660 }
duke@435 661
duke@435 662 ThreadConcurrentLocks* ConcurrentLocksDump::thread_concurrent_locks(JavaThread* thread) {
duke@435 663 for (ThreadConcurrentLocks* tcl = _map; tcl != NULL; tcl = tcl->next()) {
duke@435 664 if (tcl->java_thread() == thread) {
duke@435 665 return tcl;
duke@435 666 }
duke@435 667 }
duke@435 668 return NULL;
duke@435 669 }
duke@435 670
duke@435 671 void ConcurrentLocksDump::print_locks_on(JavaThread* t, outputStream* st) {
duke@435 672 st->print_cr(" Locked ownable synchronizers:");
duke@435 673 ThreadConcurrentLocks* tcl = thread_concurrent_locks(t);
duke@435 674 GrowableArray<instanceOop>* locks = (tcl != NULL ? tcl->owned_locks() : NULL);
duke@435 675 if (locks == NULL || locks->is_empty()) {
duke@435 676 st->print_cr("\t- None");
duke@435 677 st->cr();
duke@435 678 return;
duke@435 679 }
duke@435 680
duke@435 681 for (int i = 0; i < locks->length(); i++) {
duke@435 682 instanceOop obj = locks->at(i);
coleenp@4037 683 InstanceKlass* ik = InstanceKlass::cast(obj->klass());
duke@435 684 st->print_cr("\t- <" INTPTR_FORMAT "> (a %s)", (address)obj, ik->external_name());
duke@435 685 }
duke@435 686 st->cr();
duke@435 687 }
duke@435 688
duke@435 689 ThreadConcurrentLocks::ThreadConcurrentLocks(JavaThread* thread) {
duke@435 690 _thread = thread;
zgu@3900 691 _owned_locks = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<instanceOop>(INITIAL_ARRAY_SIZE, true);
duke@435 692 _next = NULL;
duke@435 693 }
duke@435 694
duke@435 695 ThreadConcurrentLocks::~ThreadConcurrentLocks() {
duke@435 696 delete _owned_locks;
duke@435 697 }
duke@435 698
duke@435 699 void ThreadConcurrentLocks::add_lock(instanceOop o) {
duke@435 700 _owned_locks->append(o);
duke@435 701 }
duke@435 702
duke@435 703 void ThreadConcurrentLocks::oops_do(OopClosure* f) {
duke@435 704 int length = _owned_locks->length();
duke@435 705 for (int i = 0; i < length; i++) {
duke@435 706 f->do_oop((oop*) _owned_locks->adr_at(i));
duke@435 707 }
duke@435 708 }
duke@435 709
duke@435 710 ThreadStatistics::ThreadStatistics() {
duke@435 711 _contended_enter_count = 0;
duke@435 712 _monitor_wait_count = 0;
duke@435 713 _sleep_count = 0;
duke@435 714 _count_pending_reset = false;
duke@435 715 _timer_pending_reset = false;
mchung@1310 716 memset((void*) _perf_recursion_counts, 0, sizeof(_perf_recursion_counts));
duke@435 717 }
duke@435 718
duke@435 719 ThreadSnapshot::ThreadSnapshot(JavaThread* thread) {
duke@435 720 _thread = thread;
duke@435 721 _threadObj = thread->threadObj();
duke@435 722 _stack_trace = NULL;
duke@435 723 _concurrent_locks = NULL;
duke@435 724 _next = NULL;
duke@435 725
duke@435 726 ThreadStatistics* stat = thread->get_thread_stat();
duke@435 727 _contended_enter_ticks = stat->contended_enter_ticks();
duke@435 728 _contended_enter_count = stat->contended_enter_count();
duke@435 729 _monitor_wait_ticks = stat->monitor_wait_ticks();
duke@435 730 _monitor_wait_count = stat->monitor_wait_count();
duke@435 731 _sleep_ticks = stat->sleep_ticks();
duke@435 732 _sleep_count = stat->sleep_count();
duke@435 733
duke@435 734 _blocker_object = NULL;
duke@435 735 _blocker_object_owner = NULL;
duke@435 736
duke@435 737 _thread_status = java_lang_Thread::get_thread_status(_threadObj);
duke@435 738 _is_ext_suspended = thread->is_being_ext_suspended();
duke@435 739 _is_in_native = (thread->thread_state() == _thread_in_native);
duke@435 740
duke@435 741 if (_thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER ||
duke@435 742 _thread_status == java_lang_Thread::IN_OBJECT_WAIT ||
duke@435 743 _thread_status == java_lang_Thread::IN_OBJECT_WAIT_TIMED) {
duke@435 744
duke@435 745 Handle obj = ThreadService::get_current_contended_monitor(thread);
duke@435 746 if (obj() == NULL) {
duke@435 747 // monitor no longer exists; thread is not blocked
duke@435 748 _thread_status = java_lang_Thread::RUNNABLE;
duke@435 749 } else {
duke@435 750 _blocker_object = obj();
duke@435 751 JavaThread* owner = ObjectSynchronizer::get_lock_owner(obj, false);
duke@435 752 if ((owner == NULL && _thread_status == java_lang_Thread::BLOCKED_ON_MONITOR_ENTER)
dcubed@3202 753 || (owner != NULL && owner->is_attaching_via_jni())) {
duke@435 754 // ownership information of the monitor is not available
duke@435 755 // (may no longer be owned or releasing to some other thread)
duke@435 756 // make this thread in RUNNABLE state.
duke@435 757 // And when the owner thread is in attaching state, the java thread
duke@435 758 // is not completely initialized. For example thread name and id
duke@435 759 // and may not be set, so hide the attaching thread.
duke@435 760 _thread_status = java_lang_Thread::RUNNABLE;
duke@435 761 _blocker_object = NULL;
duke@435 762 } else if (owner != NULL) {
duke@435 763 _blocker_object_owner = owner->threadObj();
duke@435 764 }
duke@435 765 }
duke@435 766 }
duke@435 767
duke@435 768 // Support for JSR-166 locks
kamg@677 769 if (JDK_Version::current().supports_thread_park_blocker() &&
duke@435 770 (_thread_status == java_lang_Thread::PARKED ||
duke@435 771 _thread_status == java_lang_Thread::PARKED_TIMED)) {
duke@435 772
duke@435 773 _blocker_object = thread->current_park_blocker();
duke@435 774 if (_blocker_object != NULL && _blocker_object->is_a(SystemDictionary::abstract_ownable_synchronizer_klass())) {
duke@435 775 _blocker_object_owner = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(_blocker_object);
duke@435 776 }
duke@435 777 }
duke@435 778 }
duke@435 779
duke@435 780 ThreadSnapshot::~ThreadSnapshot() {
duke@435 781 delete _stack_trace;
duke@435 782 delete _concurrent_locks;
duke@435 783 }
duke@435 784
duke@435 785 void ThreadSnapshot::dump_stack_at_safepoint(int max_depth, bool with_locked_monitors) {
duke@435 786 _stack_trace = new ThreadStackTrace(_thread, with_locked_monitors);
duke@435 787 _stack_trace->dump_stack_at_safepoint(max_depth);
duke@435 788 }
duke@435 789
duke@435 790
duke@435 791 void ThreadSnapshot::oops_do(OopClosure* f) {
duke@435 792 f->do_oop(&_threadObj);
duke@435 793 f->do_oop(&_blocker_object);
duke@435 794 f->do_oop(&_blocker_object_owner);
duke@435 795 if (_stack_trace != NULL) {
duke@435 796 _stack_trace->oops_do(f);
duke@435 797 }
duke@435 798 if (_concurrent_locks != NULL) {
duke@435 799 _concurrent_locks->oops_do(f);
duke@435 800 }
duke@435 801 }
duke@435 802
duke@435 803 DeadlockCycle::DeadlockCycle() {
duke@435 804 _is_deadlock = false;
zgu@3900 805 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
duke@435 806 _next = NULL;
duke@435 807 }
duke@435 808
duke@435 809 DeadlockCycle::~DeadlockCycle() {
duke@435 810 delete _threads;
duke@435 811 }
duke@435 812
duke@435 813 void DeadlockCycle::print_on(outputStream* st) const {
duke@435 814 st->cr();
duke@435 815 st->print_cr("Found one Java-level deadlock:");
duke@435 816 st->print("=============================");
duke@435 817
duke@435 818 JavaThread* currentThread;
duke@435 819 ObjectMonitor* waitingToLockMonitor;
duke@435 820 oop waitingToLockBlocker;
duke@435 821 int len = _threads->length();
duke@435 822 for (int i = 0; i < len; i++) {
duke@435 823 currentThread = _threads->at(i);
duke@435 824 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
duke@435 825 waitingToLockBlocker = currentThread->current_park_blocker();
duke@435 826 st->cr();
duke@435 827 st->print_cr("\"%s\":", currentThread->get_thread_name());
duke@435 828 const char* owner_desc = ",\n which is held by";
duke@435 829 if (waitingToLockMonitor != NULL) {
duke@435 830 st->print(" waiting to lock monitor " INTPTR_FORMAT, waitingToLockMonitor);
duke@435 831 oop obj = (oop)waitingToLockMonitor->object();
duke@435 832 if (obj != NULL) {
duke@435 833 st->print(" (object "INTPTR_FORMAT ", a %s)", (address)obj,
coleenp@4037 834 (InstanceKlass::cast(obj->klass()))->external_name());
duke@435 835
duke@435 836 if (!currentThread->current_pending_monitor_is_from_java()) {
duke@435 837 owner_desc = "\n in JNI, which is held by";
duke@435 838 }
duke@435 839 } else {
duke@435 840 // No Java object associated - a JVMTI raw monitor
duke@435 841 owner_desc = " (JVMTI raw monitor),\n which is held by";
duke@435 842 }
duke@435 843 currentThread = Threads::owning_thread_from_monitor_owner(
duke@435 844 (address)waitingToLockMonitor->owner(), false /* no locking needed */);
duke@435 845 } else {
duke@435 846 st->print(" waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
duke@435 847 (address)waitingToLockBlocker,
coleenp@4037 848 (InstanceKlass::cast(waitingToLockBlocker->klass()))->external_name());
duke@435 849 assert(waitingToLockBlocker->is_a(SystemDictionary::abstract_ownable_synchronizer_klass()),
duke@435 850 "Must be an AbstractOwnableSynchronizer");
duke@435 851 oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
duke@435 852 currentThread = java_lang_Thread::thread(ownerObj);
duke@435 853 }
duke@435 854 st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name());
duke@435 855 }
duke@435 856
duke@435 857 st->cr();
duke@435 858 st->cr();
duke@435 859
duke@435 860 // Print stack traces
duke@435 861 bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
duke@435 862 JavaMonitorsInStackTrace = true;
duke@435 863 st->print_cr("Java stack information for the threads listed above:");
duke@435 864 st->print_cr("===================================================");
duke@435 865 for (int j = 0; j < len; j++) {
duke@435 866 currentThread = _threads->at(j);
duke@435 867 st->print_cr("\"%s\":", currentThread->get_thread_name());
duke@435 868 currentThread->print_stack_on(st);
duke@435 869 }
duke@435 870 JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
duke@435 871 }
duke@435 872
duke@435 873 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
duke@435 874 bool include_jvmti_agent_threads,
duke@435 875 bool include_jni_attaching_threads) {
duke@435 876 assert(cur_thread == Thread::current(), "Check current thread");
duke@435 877
duke@435 878 int init_size = ThreadService::get_live_thread_count();
duke@435 879 _threads_array = new GrowableArray<instanceHandle>(init_size);
duke@435 880
duke@435 881 MutexLockerEx ml(Threads_lock);
duke@435 882
duke@435 883 for (JavaThread* jt = Threads::first(); jt != NULL; jt = jt->next()) {
duke@435 884 // skips JavaThreads in the process of exiting
duke@435 885 // and also skips VM internal JavaThreads
duke@435 886 // Threads in _thread_new or _thread_new_trans state are included.
duke@435 887 // i.e. threads have been started but not yet running.
duke@435 888 if (jt->threadObj() == NULL ||
duke@435 889 jt->is_exiting() ||
duke@435 890 !java_lang_Thread::is_alive(jt->threadObj()) ||
duke@435 891 jt->is_hidden_from_external_view()) {
duke@435 892 continue;
duke@435 893 }
duke@435 894
duke@435 895 // skip agent threads
duke@435 896 if (!include_jvmti_agent_threads && jt->is_jvmti_agent_thread()) {
duke@435 897 continue;
duke@435 898 }
duke@435 899
duke@435 900 // skip jni threads in the process of attaching
dcubed@3202 901 if (!include_jni_attaching_threads && jt->is_attaching_via_jni()) {
duke@435 902 continue;
duke@435 903 }
duke@435 904
duke@435 905 instanceHandle h(cur_thread, (instanceOop) jt->threadObj());
duke@435 906 _threads_array->append(h);
duke@435 907 }
duke@435 908 }

mercurial