< prev index next >

src/hotspot/share/services/threadService.cpp

Print this page
rev 59077 : 8153224.v2.09b.patch combined with 8153224.v2.10.patch; merge with jdk-15+21.
rev 59078 : eosterlund v2.10 CR: reorganize deflate_monitor_using_JT() to use "early exit" style; dcubed - clarify/fix/rearrange a few comments in deflate_monitor_using_JT(); eosterlund v2.10 CR: simplify install_displaced_markword_in_object() and save_om_ptr(); save_om_ptr()'s call to install_displaced_markword_in_object() can race with the deflater thread's clearing of the object field so handle that; fold 8153224.OMHandle_experiment into 8153224.v2.11.patch; merge with jdk-15+21.
   1 /*
   2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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;
  56 bool ThreadService::_thread_allocated_memory_enabled = false;
  57 
  58 PerfCounter*  ThreadService::_total_threads_count = NULL;
  59 PerfVariable* ThreadService::_live_threads_count = NULL;


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


 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) {


 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 {


   1 /*
   2  * Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  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/safepointVerifiers.hpp"
  41 #include "runtime/thread.inline.hpp"
  42 #include "runtime/threadSMR.inline.hpp"
  43 #include "runtime/vframe.hpp"
  44 #include "runtime/vmThread.hpp"
  45 #include "runtime/vmOperations.hpp"
  46 #include "services/threadService.hpp"
  47 
  48 // TODO: we need to define a naming convention for perf counters
  49 // to distinguish counters for:
  50 //   - standard JSR174 use
  51 //   - Hotspot extension (public and committed)
  52 //   - Hotspot extension (private/internal and uncommitted)
  53 
  54 // Default is disabled.
  55 bool ThreadService::_thread_monitoring_contention_enabled = false;
  56 bool ThreadService::_thread_cpu_time_enabled = false;
  57 bool ThreadService::_thread_allocated_memory_enabled = false;
  58 
  59 PerfCounter*  ThreadService::_total_threads_count = NULL;
  60 PerfVariable* ThreadService::_live_threads_count = NULL;


 192     (int)_daemon_threads_count->get_value(), daemon_count);
 193 }
 194 
 195 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
 196   // Do not count hidden threads
 197   if (is_hidden_thread(jt)) {
 198     return;
 199   }
 200 
 201   assert(jt == JavaThread::current(), "Called by current thread");
 202   assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
 203 
 204   decrement_thread_counts(jt, daemon);
 205 }
 206 
 207 // FIXME: JVMTI should call this function
 208 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 209   assert(thread != NULL, "should be non-NULL");
 210   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 211 
 212   // This function can be called on a target JavaThread that is not
 213   // the caller and we are not at a safepoint. So it is possible for
 214   // the waiting or pending condition to be over/stale and for the
 215   // first stage of async deflation to clear the object field in
 216   // the ObjectMonitor. It is also possible for the object to be
 217   // inflated again and to be associated with a completely different
 218   // ObjectMonitor by the time this object reference is processed
 219   // by the caller.
 220   ObjectMonitor *wait_obj = thread->current_waiting_monitor();
 221 
 222   oop obj = NULL;
 223   if (wait_obj != NULL) {
 224     // thread is doing an Object.wait() call
 225     obj = (oop) wait_obj->object();
 226     assert(AsyncDeflateIdleMonitors || obj != NULL, "Object.wait() should have an object");
 227   } else {
 228     ObjectMonitor *enter_obj = thread->current_pending_monitor();
 229     if (enter_obj != NULL) {
 230       // thread is trying to enter() an ObjectMonitor.
 231       obj = (oop) enter_obj->object();
 232       assert(AsyncDeflateIdleMonitors || obj != NULL, "ObjectMonitor should have an associated object!");
 233     }
 234   }
 235 
 236   Handle h(Thread::current(), obj);
 237   return h;
 238 }
 239 
 240 bool ThreadService::set_thread_monitoring_contention(bool flag) {
 241   MutexLocker m(Management_lock);
 242 
 243   bool prev = _thread_monitoring_contention_enabled;
 244   _thread_monitoring_contention_enabled = flag;
 245 
 246   return prev;
 247 }
 248 
 249 bool ThreadService::set_thread_cpu_time_enabled(bool flag) {
 250   MutexLocker m(Management_lock);
 251 
 252   bool prev = _thread_cpu_time_enabled;


 383   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 384     jt->set_depth_first_number(-1);
 385   }
 386 
 387   DeadlockCycle* deadlocks = NULL;
 388   DeadlockCycle* last = NULL;
 389   DeadlockCycle* cycle = new DeadlockCycle();
 390   for (JavaThread* jt = jti.first(); jt != NULL; jt = jti.next()) {
 391     if (jt->depth_first_number() >= 0) {
 392       // this thread was already visited
 393       continue;
 394     }
 395 
 396     thisDfn = globalDfn;
 397     jt->set_depth_first_number(globalDfn++);
 398     previousThread = jt;
 399     currentThread = jt;
 400 
 401     cycle->reset();
 402 
 403     // The ObjectMonitor* can't be async deflated since we are at a safepoint.
 404     // When there is a deadlock, all the monitors involved in the dependency
 405     // cycle must be contended and heavyweight. So we only care about the
 406     // heavyweight monitor a thread is waiting to lock.
 407     waitingToLockMonitor = jt->current_pending_monitor();
 408     // JVM TI raw monitors can also be involved in deadlocks, and we can be
 409     // waiting to lock both a raw monitor and ObjectMonitor at the same time.
 410     // It isn't clear how to make deadlock detection work correctly if that
 411     // happens.
 412     waitingToLockRawMonitor = jt->current_pending_raw_monitor();
 413 
 414     if (concurrent_locks) {
 415       waitingToLockBlocker = jt->current_park_blocker();
 416     }
 417 
 418     while (waitingToLockMonitor != NULL ||
 419            waitingToLockRawMonitor != NULL ||
 420            waitingToLockBlocker != NULL) {
 421       cycle->add_thread(currentThread);
 422       // Give preference to the raw monitor
 423       if (waitingToLockRawMonitor != NULL) {


 960   }
 961 }
 962 
 963 
 964 DeadlockCycle::DeadlockCycle() {
 965   _is_deadlock = false;
 966   _threads = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JavaThread*>(INITIAL_ARRAY_SIZE, true);
 967   _next = NULL;
 968 }
 969 
 970 DeadlockCycle::~DeadlockCycle() {
 971   delete _threads;
 972 }
 973 
 974 void DeadlockCycle::print_on_with(ThreadsList * t_list, outputStream* st) const {
 975   st->cr();
 976   st->print_cr("Found one Java-level deadlock:");
 977   st->print("=============================");
 978 
 979   JavaThread* currentThread;

 980   JvmtiRawMonitor* waitingToLockRawMonitor;
 981   oop waitingToLockBlocker;
 982   int len = _threads->length();
 983   for (int i = 0; i < len; i++) {
 984     currentThread = _threads->at(i);
 985     // The ObjectMonitor* can't be async deflated since we are at a safepoint.
 986     ObjectMonitor* waitingToLockMonitor = currentThread->current_pending_monitor();
 987     waitingToLockRawMonitor = currentThread->current_pending_raw_monitor();
 988     waitingToLockBlocker = currentThread->current_park_blocker();
 989     st->cr();
 990     st->print_cr("\"%s\":", currentThread->get_thread_name());
 991     const char* owner_desc = ",\n  which is held by";
 992 
 993     // Note: As the JVM TI "monitor contended enter" event callback is executed after ObjectMonitor
 994     // sets the current pending monitor, it is possible to then see a pending raw monitor as well.
 995     if (waitingToLockRawMonitor != NULL) {
 996       st->print("  waiting to lock JVM TI raw monitor " INTPTR_FORMAT, p2i(waitingToLockRawMonitor));
 997       Thread* owner = waitingToLockRawMonitor->owner();
 998       // Could be NULL as the raw monitor could be released at any time if held by non-JavaThread
 999       if (owner != NULL) {
1000         if (owner->is_Java_thread()) {
1001           currentThread = (JavaThread*) owner;
1002           st->print_cr("%s \"%s\"", owner_desc, currentThread->get_thread_name());
1003         } else {
1004           st->print_cr(",\n  which has now been released");
1005         }
1006       } else {


< prev index next >