src/hotspot/share/services/threadService.cpp
changeset 58488 165b193b30dd
parent 58177 4932dce35882
child 58679 9c3209ff7550
equal deleted inserted replaced
58487:43f63f904bbc 58488:165b193b30dd
    30 #include "memory/resourceArea.hpp"
    30 #include "memory/resourceArea.hpp"
    31 #include "oops/instanceKlass.hpp"
    31 #include "oops/instanceKlass.hpp"
    32 #include "oops/objArrayKlass.hpp"
    32 #include "oops/objArrayKlass.hpp"
    33 #include "oops/objArrayOop.inline.hpp"
    33 #include "oops/objArrayOop.inline.hpp"
    34 #include "oops/oop.inline.hpp"
    34 #include "oops/oop.inline.hpp"
       
    35 #include "prims/jvmtiRawMonitor.hpp"
    35 #include "runtime/atomic.hpp"
    36 #include "runtime/atomic.hpp"
    36 #include "runtime/handles.inline.hpp"
    37 #include "runtime/handles.inline.hpp"
    37 #include "runtime/init.hpp"
    38 #include "runtime/init.hpp"
    38 #include "runtime/objectMonitor.inline.hpp"
    39 #include "runtime/objectMonitor.inline.hpp"
    39 #include "runtime/thread.inline.hpp"
    40 #include "runtime/thread.inline.hpp"
   215     obj = (oop) wait_obj->object();
   216     obj = (oop) wait_obj->object();
   216     assert(obj != NULL, "Object.wait() should have an object");
   217     assert(obj != NULL, "Object.wait() should have an object");
   217   } else {
   218   } else {
   218     ObjectMonitor *enter_obj = thread->current_pending_monitor();
   219     ObjectMonitor *enter_obj = thread->current_pending_monitor();
   219     if (enter_obj != NULL) {
   220     if (enter_obj != NULL) {
   220       // thread is trying to enter() or raw_enter() an ObjectMonitor.
   221       // thread is trying to enter() an ObjectMonitor.
   221       obj = (oop) enter_obj->object();
   222       obj = (oop) enter_obj->object();
   222     }
   223       assert(obj != NULL, "ObjectMonitor should have an associated object!");
   223     // If obj == NULL, then ObjectMonitor is raw which doesn't count.
   224     }
   224   }
   225   }
   225 
   226 
   226   Handle h(Thread::current(), obj);
   227   Handle h(Thread::current(), obj);
   227   return h;
   228   return h;
   228 }
   229 }
   352   if (stat != NULL) {
   353   if (stat != NULL) {
   353     stat->reset_time_stat();
   354     stat->reset_time_stat();
   354   }
   355   }
   355 }
   356 }
   356 
   357 
   357 // Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true
   358 // Find deadlocks involving raw monitors, object monitors and concurrent locks
       
   359 // if concurrent_locks is true.
   358 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
   360 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
   359   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
   361   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
   360 
   362 
   361   // This code was modified from the original Threads::find_deadlocks code.
   363   // This code was modified from the original Threads::find_deadlocks code.
   362   int globalDfn = 0, thisDfn;
   364   int globalDfn = 0, thisDfn;
   363   ObjectMonitor* waitingToLockMonitor = NULL;
   365   ObjectMonitor* waitingToLockMonitor = NULL;
       
   366   JvmtiRawMonitor* waitingToLockRawMonitor = NULL;
   364   oop waitingToLockBlocker = NULL;
   367   oop waitingToLockBlocker = NULL;
   365   bool blocked_on_monitor = false;
   368   bool blocked_on_monitor = false;
   366   JavaThread *currentThread, *previousThread;
   369   JavaThread *currentThread, *previousThread;
   367   int num_deadlocks = 0;
   370   int num_deadlocks = 0;
   368 
   371 
   389     cycle->reset();
   392     cycle->reset();
   390 
   393 
   391     // When there is a deadlock, all the monitors involved in the dependency
   394     // When there is a deadlock, all the monitors involved in the dependency
   392     // cycle must be contended and heavyweight. So we only care about the
   395     // cycle must be contended and heavyweight. So we only care about the
   393     // heavyweight monitor a thread is waiting to lock.
   396     // heavyweight monitor a thread is waiting to lock.
   394     waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor();
   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 
   395     if (concurrent_locks) {
   404     if (concurrent_locks) {
   396       waitingToLockBlocker = jt->current_park_blocker();
   405       waitingToLockBlocker = jt->current_park_blocker();
   397     }
   406     }
   398     while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) {
   407 
       
   408     while (waitingToLockMonitor != NULL ||
       
   409            waitingToLockRawMonitor != NULL ||
       
   410            waitingToLockBlocker != NULL) {
   399       cycle->add_thread(currentThread);
   411       cycle->add_thread(currentThread);
   400       if (waitingToLockMonitor != NULL) {
   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
       
   418           currentThread = (JavaThread*) owner;
       
   419         }
       
   420       } else if (waitingToLockMonitor != NULL) {
   401         address currentOwner = (address)waitingToLockMonitor->owner();
   421         address currentOwner = (address)waitingToLockMonitor->owner();
   402         if (currentOwner != NULL) {
   422         if (currentOwner != NULL) {
   403           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
   423           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
   404                                                                     currentOwner);
   424                                                                     currentOwner);
   405           if (currentThread == NULL) {
   425           if (currentThread == NULL) {
   946   st->print_cr("Found one Java-level deadlock:");
   966   st->print_cr("Found one Java-level deadlock:");
   947   st->print("=============================");
   967   st->print("=============================");
   948 
   968 
   949   JavaThread* currentThread;
   969   JavaThread* currentThread;
   950   ObjectMonitor* waitingToLockMonitor;
   970   ObjectMonitor* waitingToLockMonitor;
       
   971   JvmtiRawMonitor* waitingToLockRawMonitor;
   951   oop waitingToLockBlocker;
   972   oop waitingToLockBlocker;
   952   int len = _threads->length();
   973   int len = _threads->length();
   953   for (int i = 0; i < len; i++) {
   974   for (int i = 0; i < len; i++) {
   954     currentThread = _threads->at(i);
   975     currentThread = _threads->at(i);
   955     waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();
   976     waitingToLockMonitor = currentThread->current_pending_monitor();
       
   977     waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
   956     waitingToLockBlocker = currentThread->current_park_blocker();
   978     waitingToLockBlocker = currentThread->current_park_blocker();
   957     st->cr();
   979     st->cr();
   958     st->print_cr("\"%s\":", currentThread->get_thread_name());
   980     st->print_cr("\"%s\":", currentThread->get_thread_name());
   959     const char* owner_desc = ",\n  which is held by";
   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 {
       
   997         st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
       
   998       }
       
   999     }
       
  1000 
   960     if (waitingToLockMonitor != NULL) {
  1001     if (waitingToLockMonitor != NULL) {
   961       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
  1002       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
   962       oop obj = (oop)waitingToLockMonitor->object();
  1003       oop obj = (oop)waitingToLockMonitor->object();
   963       if (obj != NULL) {
  1004       st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
   964         st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
  1005                  obj->klass()->external_name());
   965                    obj->klass()->external_name());
  1006 
   966 
  1007       if (!currentThread->current_pending_monitor_is_from_java()) {
   967         if (!currentThread->current_pending_monitor_is_from_java()) {
  1008         owner_desc = "\n  in JNI, which is held by";
   968           owner_desc = "\n  in JNI, which is held by";
       
   969         }
       
   970       } else {
       
   971         // No Java object associated - a JVMTI raw monitor
       
   972         owner_desc = " (JVMTI raw monitor),\n  which is held by";
       
   973       }
  1009       }
   974       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
  1010       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
   975                                                                 (address)waitingToLockMonitor->owner());
  1011                                                                 (address)waitingToLockMonitor->owner());
   976       if (currentThread == NULL) {
  1012       if (currentThread == NULL) {
   977         // The deadlock was detected at a safepoint so the JavaThread
  1013         // The deadlock was detected at a safepoint so the JavaThread
   978         // that owns waitingToLockMonitor should be findable, but
  1014         // that owns waitingToLockMonitor should be findable, but
   979         // if it is not findable, then the previous currentThread is
  1015         // if it is not findable, then the previous currentThread is
   980         // blocked permanently.
  1016         // blocked permanently.
   981         st->print("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
  1017         st->print_cr("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
   982                   p2i(waitingToLockMonitor->owner()));
  1018                   p2i(waitingToLockMonitor->owner()));
   983         continue;
  1019         continue;
   984       }
  1020       }
   985     } else {
  1021     } else {
   986       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
  1022       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
   990              "Must be an AbstractOwnableSynchronizer");
  1026              "Must be an AbstractOwnableSynchronizer");
   991       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
  1027       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
   992       currentThread = java_lang_Thread::thread(ownerObj);
  1028       currentThread = java_lang_Thread::thread(ownerObj);
   993       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
  1029       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
   994     }
  1030     }
   995     st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name());
  1031     st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
   996   }
  1032   }
   997 
  1033 
   998   st->cr();
       
   999   st->cr();
  1034   st->cr();
  1000 
  1035 
  1001   // Print stack traces
  1036   // Print stack traces
  1002   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
  1037   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
  1003   JavaMonitorsInStackTrace = true;
  1038   JavaMonitorsInStackTrace = true;