src/share/vm/services/threadService.cpp

Wed, 06 Jan 2010 14:22:39 -0800

author
never
date
Wed, 06 Jan 2010 14:22:39 -0800
changeset 1577
4ce7240d622c
parent 1383
89e0543e1737
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6914300: ciEnv should export all well known classes
Reviewed-by: kvn, twisti

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

mercurial