< prev index next >

src/hotspot/share/services/threadService.cpp

Print this page




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/heapInspection.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/objArrayOop.inline.hpp"
  34 #include "oops/oop.inline.hpp"

  35 #include "runtime/atomic.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/init.hpp"
  38 #include "runtime/objectMonitor.inline.hpp"
  39 #include "runtime/thread.inline.hpp"
  40 #include "runtime/threadSMR.inline.hpp"
  41 #include "runtime/vframe.hpp"
  42 #include "runtime/vmThread.hpp"
  43 #include "runtime/vmOperations.hpp"
  44 #include "services/threadService.hpp"
  45 
  46 // TODO: we need to define a naming convention for perf counters
  47 // to distinguish counters for:
  48 //   - standard JSR174 use
  49 //   - Hotspot extension (public and committed)
  50 //   - Hotspot extension (private/internal and uncommitted)
  51 
  52 // Default is disabled.
  53 bool ThreadService::_thread_monitoring_contention_enabled = false;
  54 bool ThreadService::_thread_cpu_time_enabled = false;


 200   assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
 201 
 202   decrement_thread_counts(jt, daemon);
 203 }
 204 
 205 // FIXME: JVMTI should call this function
 206 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 207   assert(thread != NULL, "should be non-NULL");
 208   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 209 
 210   ObjectMonitor *wait_obj = thread->current_waiting_monitor();
 211 
 212   oop obj = NULL;
 213   if (wait_obj != NULL) {
 214     // thread is doing an Object.wait() call
 215     obj = (oop) wait_obj->object();
 216     assert(obj != NULL, "Object.wait() should have an object");
 217   } else {
 218     ObjectMonitor *enter_obj = thread->current_pending_monitor();
 219     if (enter_obj != NULL) {
 220       // thread is trying to enter() or raw_enter() an ObjectMonitor.
 221       obj = (oop) enter_obj->object();

 222     }
 223     // If obj == NULL, then ObjectMonitor is raw which doesn't count.
 224   }
 225 
 226   Handle h(Thread::current(), obj);
 227   return h;
 228 }
 229 
 230 bool ThreadService::set_thread_monitoring_contention(bool flag) {
 231   MutexLocker m(Management_lock);
 232 
 233   bool prev = _thread_monitoring_contention_enabled;
 234   _thread_monitoring_contention_enabled = flag;
 235 
 236   return prev;
 237 }
 238 
 239 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
 240   MutexLocker m(Management_lock);
 241 
 242   bool prev = _thread_cpu_time_enabled;
 243   _thread_cpu_time_enabled = flag;


 337     }
 338   }
 339 
 340   return result_obj;
 341 }
 342 
 343 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
 344   ThreadStatistics* stat = thread->get_thread_stat();
 345   if (stat != NULL) {
 346     stat->reset_count_stat();
 347   }
 348 }
 349 
 350 void ThreadService::reset_contention_time_stat(JavaThread* thread) {
 351   ThreadStatistics* stat = thread->get_thread_stat();
 352   if (stat != NULL) {
 353     stat->reset_time_stat();
 354   }
 355 }
 356 
 357 // Find deadlocks involving object monitors and concurrent locks if concurrent_locks is true

 358 DeadlockCycle* ThreadService::find_deadlocks_at_safepoint(ThreadsList * t_list, bool concurrent_locks) {
 359   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
 360 
 361   // This code was modified from the original Threads::find_deadlocks code.
 362   int globalDfn = 0, thisDfn;
 363   ObjectMonitor* waitingToLockMonitor = NULL;

 364   oop waitingToLockBlocker = NULL;
 365   bool blocked_on_monitor = false;
 366   JavaThread *currentThread, *previousThread;
 367   int num_deadlocks = 0;
 368 
 369   // Initialize the depth-first-number for each JavaThread.
 370   JavaThreadIterator jti(t_list);
 371   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 372     jt->set_depth_first_number(-1);
 373   }
 374 
 375   DeadlockCycle* deadlocks = NULL;
 376   DeadlockCycle* last = NULL;
 377   DeadlockCycle* cycle = new DeadlockCycle();
 378   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 379     if (jt->depth_first_number() >= 0) {
 380       // this thread was already visited
 381       continue;
 382     }
 383 
 384     thisDfn = globalDfn;
 385     jt->set_depth_first_number(globalDfn++);
 386     previousThread = jt;
 387     currentThread = jt;
 388 
 389     cycle->reset();
 390 
 391     // 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
 393     // heavyweight monitor a thread is waiting to lock.
 394     waitingToLockMonitor = (ObjectMonitor*)jt->current_pending_monitor();






 395     if (concurrent_locks) {
 396       waitingToLockBlocker = jt->current_park_blocker();
 397     }
 398     while (waitingToLockMonitor != NULL || waitingToLockBlocker != NULL) {



 399       cycle->add_thread(currentThread);
 400       if (waitingToLockMonitor != NULL) {








 401         address currentOwner = (address)waitingToLockMonitor->owner();
 402         if (currentOwner != NULL) {
 403           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
 404                                                                     currentOwner);
 405           if (currentThread == NULL) {
 406             // This function is called at a safepoint so the JavaThread
 407             // that owns waitingToLockMonitor should be findable, but
 408             // if it is not findable, then the previous currentThread is
 409             // blocked permanently. We record this as a deadlock.
 410             num_deadlocks++;
 411 
 412             cycle->set_deadlock(true);
 413 
 414             // add this cycle to the deadlocks list
 415             if (deadlocks == NULL) {
 416               deadlocks = cycle;
 417             } else {
 418               last->set_next(cycle);
 419             }
 420             last = cycle;


 931 }
 932 
 933 
 934 DeadlockCycle::DeadlockCycle() {
 935   _is_deadlock = false;
 936   _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
 937   _next = NULL;
 938 }
 939 
 940 DeadlockCycle::~DeadlockCycle() {
 941   delete _threads;
 942 }
 943 
 944 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
 945   st->cr();
 946   st->print_cr("Found one Java-level deadlock:");
 947   st->print("=============================");
 948 
 949   JavaThread* currentThread;
 950   ObjectMonitor* waitingToLockMonitor;

 951   oop waitingToLockBlocker;
 952   int len = _threads->length();
 953   for (int i = 0; i < len; i++) {
 954     currentThread = _threads->at(i);
 955     waitingToLockMonitor = (ObjectMonitor*)currentThread->current_pending_monitor();

 956     waitingToLockBlocker = currentThread->current_park_blocker();
 957     st->cr();
 958     st->print_cr("\"%s\":", currentThread->get_thread_name());
 959     const char* owner_desc = ",\n  which is held by";



















 960     if (waitingToLockMonitor != NULL) {
 961       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
 962       oop obj = (oop)waitingToLockMonitor->object();
 963       if (obj != NULL) {
 964         st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
 965                    obj->klass()->external_name());
 966 
 967         if (!currentThread->current_pending_monitor_is_from_java()) {
 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       }
 974       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
 975                                                                 (address)waitingToLockMonitor->owner());
 976       if (currentThread == NULL) {
 977         // The deadlock was detected at a safepoint so the JavaThread
 978         // that owns waitingToLockMonitor should be findable, but
 979         // if it is not findable, then the previous currentThread is
 980         // blocked permanently.
 981         st->print("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
 982                   p2i(waitingToLockMonitor->owner()));
 983         continue;
 984       }
 985     } else {
 986       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
 987                 p2i(waitingToLockBlocker),
 988                 waitingToLockBlocker->klass()->external_name());
 989       assert(waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
 990              "Must be an AbstractOwnableSynchronizer");
 991       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
 992       currentThread = java_lang_Thread::thread(ownerObj);
 993       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
 994     }
 995     st->print("%s \"%s\"", owner_desc, currentThread->get_thread_name());
 996   }
 997 
 998   st->cr();
 999   st->cr();
1000 
1001   // Print stack traces
1002   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1003   JavaMonitorsInStackTrace = true;
1004   st->print_cr("Java stack information for the threads listed above:");
1005   st->print_cr("===================================================");
1006   for (int j = 0; j < len; j++) {
1007     currentThread = _threads->at(j);
1008     st->print_cr("\"%s\":", currentThread->get_thread_name());
1009     currentThread->print_stack_on(st);
1010   }
1011   JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1012 }
1013 
1014 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1015                                              bool include_jvmti_agent_threads,
1016                                              bool include_jni_attaching_threads) {
1017   assert(cur_thread == Thread::current(), "Check current thread");
1018 




  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/systemDictionary.hpp"
  27 #include "memory/allocation.hpp"
  28 #include "memory/heapInspection.hpp"
  29 #include "memory/oopFactory.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/objArrayKlass.hpp"
  33 #include "oops/objArrayOop.inline.hpp"
  34 #include "oops/oop.inline.hpp"
  35 #include "prims/jvmtiRawMonitor.hpp"
  36 #include "runtime/atomic.hpp"
  37 #include "runtime/handles.inline.hpp"
  38 #include "runtime/init.hpp"
  39 #include "runtime/objectMonitor.inline.hpp"
  40 #include "runtime/thread.inline.hpp"
  41 #include "runtime/threadSMR.inline.hpp"
  42 #include "runtime/vframe.hpp"
  43 #include "runtime/vmThread.hpp"
  44 #include "runtime/vmOperations.hpp"
  45 #include "services/threadService.hpp"
  46 
  47 // TODO: we need to define a naming convention for perf counters
  48 // to distinguish counters for:
  49 //   - standard JSR174 use
  50 //   - Hotspot extension (public and committed)
  51 //   - Hotspot extension (private/internal and uncommitted)
  52 
  53 // Default is disabled.
  54 bool ThreadService::_thread_monitoring_contention_enabled = false;
  55 bool ThreadService::_thread_cpu_time_enabled = false;


 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 
 240 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
 241   MutexLocker m(Management_lock);
 242 
 243   bool prev = _thread_cpu_time_enabled;
 244   _thread_cpu_time_enabled = flag;


 338     }
 339   }
 340 
 341   return result_obj;
 342 }
 343 
 344 void ThreadService::reset_contention_count_stat(JavaThread* thread) {
 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 on both a raw monitor and ObjectMonitor at the same time. It
 400     // 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
 418           currentThread = (JavaThread*) owner;
 419         }
 420       } else if (waitingToLockMonitor != NULL) {
 421         address currentOwner = (address)waitingToLockMonitor->owner();
 422         if (currentOwner != NULL) {
 423           currentThread = Threads::owning_thread_from_monitor_owner(t_list,
 424                                                                     currentOwner);
 425           if (currentThread == NULL) {
 426             // This function is called at a safepoint so the JavaThread
 427             // that owns waitingToLockMonitor should be findable, but
 428             // if it is not findable, then the previous currentThread is
 429             // blocked permanently. We record this as a deadlock.
 430             num_deadlocks++;
 431 
 432             cycle->set_deadlock(true);
 433 
 434             // add this cycle to the deadlocks list
 435             if (deadlocks == NULL) {
 436               deadlocks = cycle;
 437             } else {
 438               last->set_next(cycle);
 439             }
 440             last = cycle;


 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 {
 997         st->print_cr("%s non-Java thread=" PTR_FORMAT, owner_desc, p2i(owner));
 998       }
 999     }
1000 
1001     if (waitingToLockMonitor != NULL) {
1002       st->print("  waiting to lock monitor " INTPTR_FORMAT, p2i(waitingToLockMonitor));
1003       oop obj = (oop)waitingToLockMonitor->object();

1004       st->print(" (object " INTPTR_FORMAT ", a %s)", p2i(obj),
1005                  obj->klass()->external_name());
1006 
1007       if (!currentThread->current_pending_monitor_is_from_java()) {
1008         owner_desc = "\n  in JNI, which is held by";
1009       }




1010       currentThread = Threads::owning_thread_from_monitor_owner(t_list,
1011                                                                 (address)waitingToLockMonitor->owner());
1012       if (currentThread == NULL) {
1013         // The deadlock was detected at a safepoint so the JavaThread
1014         // that owns waitingToLockMonitor should be findable, but
1015         // if it is not findable, then the previous currentThread is
1016         // blocked permanently.
1017         st->print_cr("%s UNKNOWN_owner_addr=" PTR_FORMAT, owner_desc,
1018                   p2i(waitingToLockMonitor->owner()));
1019         continue;
1020       }
1021     } else {
1022       st->print("  waiting for ownable synchronizer " INTPTR_FORMAT ", (a %s)",
1023                 p2i(waitingToLockBlocker),
1024                 waitingToLockBlocker->klass()->external_name());
1025       assert(waitingToLockBlocker->is_a(SystemDictionary::java_util_concurrent_locks_AbstractOwnableSynchronizer_klass()),
1026              "Must be an AbstractOwnableSynchronizer");
1027       oop ownerObj = java_util_concurrent_locks_AbstractOwnableSynchronizer::get_owner_threadObj(waitingToLockBlocker);
1028       currentThread = java_lang_Thread::thread(ownerObj);
1029       assert(currentThread != NULL, "AbstractOwnableSynchronizer owning thread is unexpectedly NULL");
1030     }
1031     st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1032   }
1033 

1034   st->cr();
1035 
1036   // Print stack traces
1037   bool oldJavaMonitorsInStackTrace = JavaMonitorsInStackTrace;
1038   JavaMonitorsInStackTrace = true;
1039   st->print_cr("Java stack information for the threads listed above:");
1040   st->print_cr("===================================================");
1041   for (int j = 0; j < len; j++) {
1042     currentThread = _threads->at(j);
1043     st->print_cr("\"%s\":", currentThread->get_thread_name());
1044     currentThread->print_stack_on(st);
1045   }
1046   JavaMonitorsInStackTrace = oldJavaMonitorsInStackTrace;
1047 }
1048 
1049 ThreadsListEnumerator::ThreadsListEnumerator(Thread* cur_thread,
1050                                              bool include_jvmti_agent_threads,
1051                                              bool include_jni_attaching_threads) {
1052   assert(cur_thread == Thread::current(), "Check current thread");
1053 


< prev index next >