191 (int)_daemon_threads_count->get_value(), daemon_count);
192 }
193
194 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
195 // Do not count hidden threads
196 if (is_hidden_thread(jt)) {
197 return;
198 }
199
200 assert(jt == JavaThread::current(), "Called by current thread");
201 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
202
203 decrement_thread_counts(jt, daemon);
204 }
205
206 // FIXME: JVMTI should call this function
207 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
208 assert(thread != NULL, "should be non-NULL");
209 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
210
211 ObjectMonitor *wait_obj = thread->current_waiting_monitor();
212
213 oop obj = NULL;
214 if (wait_obj != NULL) {
215 // thread is doing an Object.wait() call
216 obj = (oop) wait_obj->object();
217 assert(obj != NULL, "Object.wait() should have an object");
218 } else {
219 ObjectMonitor *enter_obj = thread->current_pending_monitor();
220 if (enter_obj != NULL) {
221 // thread is trying to enter() an ObjectMonitor.
222 obj = (oop) enter_obj->object();
223 assert(obj != NULL, "ObjectMonitor should have an associated object!");
224 }
225 }
226
227 Handle h(Thread::current(), obj);
228 return h;
229 }
230
231 bool ThreadService::set_thread_monitoring_contention(bool flag) {
232 MutexLocker m(Management_lock);
233
234 bool prev = _thread_monitoring_contention_enabled;
235 _thread_monitoring_contention_enabled = flag;
236
237 return prev;
238 }
239
345 ThreadStatistics* stat = thread->get_thread_stat();
346 if (stat != NULL) {
347 stat->reset_count_stat();
348 }
349 }
350
351 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
352 ThreadStatistics* stat = thread->get_thread_stat();
353 if (stat != NULL) {
354 stat->reset_time_stat();
355 }
356 }
357
358 // Find deadlocks involving raw monitors, object monitors and concurrent locks
359 // if concurrent_locks is true.
360 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
361 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
362
363 // This code was modified from the original Threads::find_deadlocks code.
364 int globalDfn = 0, thisDfn;
365 ObjectMonitor* waitingToLockMonitor = NULL;
366 JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
367 oop waitingToLockBlocker = NULL;
368 bool blocked_on_monitor = false;
369 JavaThread *currentThread, *previousThread;
370 int num_deadlocks = 0;
371
372 // Initialize the depth-first-number for each JavaThread.
373 JavaThreadIterator jti(t_list);
374 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
375 jt->set_depth_first_number(-1);
376 }
377
378 DeadlockCycle* deadlocks = NULL;
379 DeadlockCycle* last = NULL;
380 DeadlockCycle* cycle = new DeadlockCycle();
381 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
382 if (jt->depth_first_number() >= 0) {
383 // this thread was already visited
384 continue;
385 }
386
387 thisDfn = globalDfn;
388 jt->set_depth_first_number(globalDfn++);
389 previousThread = jt;
390 currentThread = jt;
391
392 cycle->reset();
393
394 // When there is a deadlock, all the monitors involved in the dependency
395 // cycle must be contended and heavyweight. So we only care about the
396 // heavyweight monitor a thread is waiting to lock.
397 waitingToLockMonitor = jt->current_pending_monitor();
398 // JVM TI raw monitors can also be involved in deadlocks, and we can be
399 // waiting to lock both a raw monitor and ObjectMonitor at the same time.
400 // It isn't clear how to make deadlock detection work correctly if that
401 // happens.
402 waitingToLockRawMonitor = jt->current_pending_raw_monitor();
403
404 if (concurrent_locks) {
405 waitingToLockBlocker = jt->current_park_blocker();
406 }
407
408 while (waitingToLockMonitor != NULL ||
409 waitingToLockRawMonitor != NULL ||
410 waitingToLockBlocker != NULL) {
411 cycle->add_thread(currentThread);
412 // Give preference to the raw monitor
413 if (waitingToLockRawMonitor != NULL) {
414 Thread* owner = waitingToLockRawMonitor->owner();
415 if (owner != NULL && // the raw monitor could be released at any time
416 owner->is_Java_thread()) {
417 // only JavaThreads can be reported here
468 } else if (currentThread == previousThread) {
469 // Self-loop, ignore
470 break;
471 } else {
472 // We have a (new) cycle
473 num_deadlocks++;
474
475 cycle->set_deadlock(true);
476
477 // add this cycle to the deadlocks list
478 if (deadlocks == NULL) {
479 deadlocks = cycle;
480 } else {
481 last->set_next(cycle);
482 }
483 last = cycle;
484 cycle = new DeadlockCycle();
485 break;
486 }
487 previousThread = currentThread;
488 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
489 if (concurrent_locks) {
490 waitingToLockBlocker = currentThread->current_park_blocker();
491 }
492 }
493
494 }
495 delete cycle;
496 return deadlocks;
497 }
498
499 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
500
501 // Create a new ThreadDumpResult object and append to the list.
502 // If GC happens before this function returns, Method*
503 // in the stack trace will be visited.
504 ThreadService::add_thread_dump(this);
505 }
506
507 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
508 // Create a new ThreadDumpResult object and append to the list.
950 }
951 }
952
953
954 DeadlockCycle::DeadlockCycle() {
955 _is_deadlock = false;
956 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
957 _next = NULL;
958 }
959
960 DeadlockCycle::~DeadlockCycle() {
961 delete _threads;
962 }
963
964 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
965 st->cr();
966 st->print_cr("Found one Java-level deadlock:");
967 st->print("=============================");
968
969 JavaThread* currentThread;
970 ObjectMonitor* waitingToLockMonitor;
971 JvmtiRawMonitor* waitingToLockRawMonitor;
972 oop waitingToLockBlocker;
973 int len = _threads->length();
974 for (int i = 0; i < len; i++) {
975 currentThread = _threads->at(i);
976 waitingToLockMonitor = currentThread->current_pending_monitor();
977 waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
978 waitingToLockBlocker = currentThread->current_park_blocker();
979 st->cr();
980 st->print_cr("\"%s\":", currentThread->get_thread_name());
981 const char* owner_desc = ",\n which is held by";
982
983 // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
984 // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
985 if (waitingToLockRawMonitor != NULL) {
986 st->print(" waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
987 Thread* owner = waitingToLockRawMonitor->owner();
988 // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
989 if (owner != NULL) {
990 if (owner->is_Java_thread()) {
991 currentThread = (JavaThread*) owner;
992 st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
993 } else {
994 st->print_cr(",\n which has now been released");
995 }
996 } else {
|
191 (int)_daemon_threads_count->get_value(), daemon_count);
192 }
193
194 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
195 // Do not count hidden threads
196 if (is_hidden_thread(jt)) {
197 return;
198 }
199
200 assert(jt == JavaThread::current(), "Called by current thread");
201 assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
202
203 decrement_thread_counts(jt, daemon);
204 }
205
206 // FIXME: JVMTI should call this function
207 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
208 assert(thread != NULL, "should be non-NULL");
209 debug_only(Thread::check_for_dangling_thread_pointer(thread);)
210
211 // This function can be called on a target JavaThread that is not
212 // the caller and we are not at a safepoint. This ObjectMonitorHandle
213 // keeps the ObjectMonitor from being async deflated so the object
214 // reference we fetch remains non-NULL.
215 ObjectMonitorHandle omh;
216 ObjectMonitor *wait_obj = thread->current_waiting_monitor(&omh);
217
218 oop obj = NULL;
219 if (wait_obj != NULL) {
220 // thread is doing an Object.wait() call
221 obj = (oop) wait_obj->object();
222 assert(obj != NULL, "Object.wait() should have an object");
223 } else {
224 ObjectMonitor *enter_obj = thread->current_pending_monitor(&omh);
225 if (enter_obj != NULL) {
226 // thread is trying to enter() an ObjectMonitor.
227 obj = (oop) enter_obj->object();
228 assert(obj != NULL, "ObjectMonitor should have an associated object!");
229 }
230 }
231
232 Handle h(Thread::current(), obj);
233 return h;
234 }
235
236 bool ThreadService::set_thread_monitoring_contention(bool flag) {
237 MutexLocker m(Management_lock);
238
239 bool prev = _thread_monitoring_contention_enabled;
240 _thread_monitoring_contention_enabled = flag;
241
242 return prev;
243 }
244
350 ThreadStatistics* stat = thread->get_thread_stat();
351 if (stat != NULL) {
352 stat->reset_count_stat();
353 }
354 }
355
356 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
357 ThreadStatistics* stat = thread->get_thread_stat();
358 if (stat != NULL) {
359 stat->reset_time_stat();
360 }
361 }
362
363 // Find deadlocks involving raw monitors, object monitors and concurrent locks
364 // if concurrent_locks is true.
365 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
366 assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
367
368 // This code was modified from the original Threads::find_deadlocks code.
369 int globalDfn = 0, thisDfn;
370 // This code is called at a safepoint so this ObjectMonitorHandle
371 // is not strictly necessary.
372 ObjectMonitorHandle omh;
373 ObjectMonitor* waitingToLockMonitor = NULL;
374 JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
375 oop waitingToLockBlocker = NULL;
376 bool blocked_on_monitor = false;
377 JavaThread *currentThread, *previousThread;
378 int num_deadlocks = 0;
379
380 // Initialize the depth-first-number for each JavaThread.
381 JavaThreadIterator jti(t_list);
382 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
383 jt->set_depth_first_number(-1);
384 }
385
386 DeadlockCycle* deadlocks = NULL;
387 DeadlockCycle* last = NULL;
388 DeadlockCycle* cycle = new DeadlockCycle();
389 for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
390 if (jt->depth_first_number() >= 0) {
391 // this thread was already visited
392 continue;
393 }
394
395 thisDfn = globalDfn;
396 jt->set_depth_first_number(globalDfn++);
397 previousThread = jt;
398 currentThread = jt;
399
400 cycle->reset();
401
402 if (waitingToLockMonitor != NULL) {
403 // Done with the current waitingToLockMonitor value so release
404 // the ObjectMonitorHandle manually before we use it again:
405 omh.unset_om_ptr();
406 }
407 // When there is a deadlock, all the monitors involved in the dependency
408 // cycle must be contended and heavyweight. So we only care about the
409 // heavyweight monitor a thread is waiting to lock.
410 waitingToLockMonitor = jt->current_pending_monitor(&omh);
411 // JVM TI raw monitors can also be involved in deadlocks, and we can be
412 // waiting to lock both a raw monitor and ObjectMonitor at the same time.
413 // It isn't clear how to make deadlock detection work correctly if that
414 // happens.
415 waitingToLockRawMonitor = jt->current_pending_raw_monitor();
416
417 if (concurrent_locks) {
418 waitingToLockBlocker = jt->current_park_blocker();
419 }
420
421 while (waitingToLockMonitor != NULL ||
422 waitingToLockRawMonitor != NULL ||
423 waitingToLockBlocker != NULL) {
424 cycle->add_thread(currentThread);
425 // Give preference to the raw monitor
426 if (waitingToLockRawMonitor != NULL) {
427 Thread* owner = waitingToLockRawMonitor->owner();
428 if (owner != NULL && // the raw monitor could be released at any time
429 owner->is_Java_thread()) {
430 // only JavaThreads can be reported here
481 } else if (currentThread == previousThread) {
482 // Self-loop, ignore
483 break;
484 } else {
485 // We have a (new) cycle
486 num_deadlocks++;
487
488 cycle->set_deadlock(true);
489
490 // add this cycle to the deadlocks list
491 if (deadlocks == NULL) {
492 deadlocks = cycle;
493 } else {
494 last->set_next(cycle);
495 }
496 last = cycle;
497 cycle = new DeadlockCycle();
498 break;
499 }
500 previousThread = currentThread;
501 if (waitingToLockMonitor != NULL) {
502 // Done with the current waitingToLockMonitor value so release
503 // the ObjectMonitorHandle manually before we use it again:
504 omh.unset_om_ptr();
505 }
506 waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor(&omh);
507 if (concurrent_locks) {
508 waitingToLockBlocker = currentThread->current_park_blocker();
509 }
510 }
511
512 }
513 delete cycle;
514 return deadlocks;
515 }
516
517 ThreadDumpResult::ThreadDumpResult() : _num_threads(0), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
518
519 // Create a new ThreadDumpResult object and append to the list.
520 // If GC happens before this function returns, Method*
521 // in the stack trace will be visited.
522 ThreadService::add_thread_dump(this);
523 }
524
525 ThreadDumpResult::ThreadDumpResult(int num_threads) : _num_threads(num_threads), _num_snapshots(0), _snapshots(NULL), _last(NULL), _next(NULL), _setter() {
526 // Create a new ThreadDumpResult object and append to the list.
968 }
969 }
970
971
972 DeadlockCycle::DeadlockCycle() {
973 _is_deadlock = false;
974 _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
975 _next = NULL;
976 }
977
978 DeadlockCycle::~DeadlockCycle() {
979 delete _threads;
980 }
981
982 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
983 st->cr();
984 st->print_cr("Found one Java-level deadlock:");
985 st->print("=============================");
986
987 JavaThread* currentThread;
988 JvmtiRawMonitor* waitingToLockRawMonitor;
989 oop waitingToLockBlocker;
990 int len = _threads->length();
991 for (int i = 0; i < len; i++) {
992 currentThread = _threads->at(i);
993 // This code is called at a safepoint so this ObjectMonitorHandle
994 // is not strictly necessary.
995 ObjectMonitorHandle omh;
996 ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor(&omh);
997 waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
998 waitingToLockBlocker = currentThread->current_park_blocker();
999 st->cr();
1000 st->print_cr("\"%s\":", currentThread->get_thread_name());
1001 const char* owner_desc = ",\n which is held by";
1002
1003 // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
1004 // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
1005 if (waitingToLockRawMonitor != NULL) {
1006 st->print(" waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
1007 Thread* owner = waitingToLockRawMonitor->owner();
1008 // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
1009 if (owner != NULL) {
1010 if (owner->is_Java_thread()) {
1011 currentThread = (JavaThread*) owner;
1012 st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1013 } else {
1014 st->print_cr(",\n which has now been released");
1015 }
1016 } else {
|