annotate src/share/vm/services/threadService.cpp @ 1995:b1a2afa37ec4

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