< prev index next >

src/hotspot/share/services/threadService.cpp

Print this page
rev 52112 : [mq]: 8021335
rev 52113 : [mq]: 8021335.diff


  40 #include "runtime/vframe.hpp"
  41 #include "runtime/vmThread.hpp"
  42 #include "runtime/vm_operations.hpp"
  43 #include "services/threadService.hpp"
  44 
  45 // TODO: we need to define a naming convention for perf counters
  46 // to distinguish counters for:
  47 //   - standard JSR174 use
  48 //   - Hotspot extension (public and committed)
  49 //   - Hotspot extension (private/internal and uncommitted)
  50 
  51 // Default is disabled.
  52 bool ThreadService::_thread_monitoring_contention_enabled = false;
  53 bool ThreadService::_thread_cpu_time_enabled = false;
  54 bool ThreadService::_thread_allocated_memory_enabled = false;
  55 
  56 PerfCounter*  ThreadService::_total_threads_count = NULL;
  57 PerfVariable* ThreadService::_live_threads_count = NULL;
  58 PerfVariable* ThreadService::_peak_threads_count = NULL;
  59 PerfVariable* ThreadService::_daemon_threads_count = NULL;
  60 volatile int ThreadService::_exiting_threads_count = 0;
  61 volatile int ThreadService::_exiting_daemon_threads_count = 0;
  62 
  63 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
  64 
  65 static const int INITIAL_ARRAY_SIZE = 10;
  66 
  67 void ThreadService::init() {
  68   EXCEPTION_MARK;
  69 
  70   // These counters are for java.lang.management API support.
  71   // They are created even if -XX:-UsePerfData is set and in
  72   // that case, they will be allocated on C heap.
  73 
  74   _total_threads_count =
  75                 PerfDataManager::create_counter(JAVA_THREADS, "started",
  76                                                 PerfData::U_Events, CHECK);
  77 
  78   _live_threads_count =
  79                 PerfDataManager::create_variable(JAVA_THREADS, "live",
  80                                                  PerfData::U_None, CHECK);
  81 


  84                                                  PerfData::U_None, CHECK);
  85 
  86   _daemon_threads_count =
  87                 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
  88                                                  PerfData::U_None, CHECK);
  89 
  90   if (os::is_thread_cpu_time_supported()) {
  91     _thread_cpu_time_enabled = true;
  92   }
  93 
  94   _thread_allocated_memory_enabled = true; // Always on, so enable it
  95 }
  96 
  97 void ThreadService::reset_peak_thread_count() {
  98   // Acquire the lock to update the peak thread count
  99   // to synchronize with thread addition and removal.
 100   MutexLockerEx mu(Threads_lock);
 101   _peak_threads_count->set_value(get_live_thread_count());
 102 }
 103 





 104 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
 105   // Do not count VM internal or JVMTI agent threads
 106   if (thread->is_hidden_from_external_view() ||
 107       thread->is_jvmti_agent_thread()) {

 108     return;
 109   }
 110 
 111   _total_threads_count->inc();
 112   _live_threads_count->inc();


 113 
 114   if (_live_threads_count->get_value() > _peak_threads_count->get_value()) {
 115     _peak_threads_count->set_value(_live_threads_count->get_value());
 116   }
 117 
 118   if (daemon) {
 119     _daemon_threads_count->inc();

 120   }
 121 }
 122 
 123 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
 124   Atomic::dec(&_exiting_threads_count);


 125   if (daemon) {
 126     Atomic::dec(&_exiting_daemon_threads_count);

 127   }




 128 
 129   if (thread->is_hidden_from_external_view() ||
 130       thread->is_jvmti_agent_thread()) {
 131     return;
 132   }
 133 
 134   _live_threads_count->set_value(_live_threads_count->get_value() - 1);
 135   if (daemon) {
 136     _daemon_threads_count->set_value(_daemon_threads_count->get_value() - 1);

 137   }

 138 }
 139 
 140 void ThreadService::current_thread_exiting(JavaThread* jt) {





 141   assert(jt == JavaThread::current(), "Called by current thread");
 142   Atomic::inc(&_exiting_threads_count);
 143 
 144   oop threadObj = jt->threadObj();
 145   if (threadObj != NULL && java_lang_Thread::is_daemon(threadObj)) {
 146     Atomic::inc(&_exiting_daemon_threads_count);
 147   }
 148 }
 149 
 150 // FIXME: JVMTI should call this function
 151 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 152   assert(thread != NULL, "should be non-NULL");
 153   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 154 
 155   ObjectMonitor *wait_obj = thread->current_waiting_monitor();
 156 
 157   oop obj = NULL;
 158   if (wait_obj != NULL) {
 159     // thread is doing an Object.wait() call
 160     obj = (oop) wait_obj->object();
 161     assert(obj != NULL, "Object.wait() should have an object");
 162   } else {
 163     ObjectMonitor *enter_obj = thread->current_pending_monitor();
 164     if (enter_obj != NULL) {
 165       // thread is trying to enter() or raw_enter() an ObjectMonitor.
 166       obj = (oop) enter_obj->object();
 167     }




  40 #include "runtime/vframe.hpp"
  41 #include "runtime/vmThread.hpp"
  42 #include "runtime/vm_operations.hpp"
  43 #include "services/threadService.hpp"
  44 
  45 // TODO: we need to define a naming convention for perf counters
  46 // to distinguish counters for:
  47 //   - standard JSR174 use
  48 //   - Hotspot extension (public and committed)
  49 //   - Hotspot extension (private/internal and uncommitted)
  50 
  51 // Default is disabled.
  52 bool ThreadService::_thread_monitoring_contention_enabled = false;
  53 bool ThreadService::_thread_cpu_time_enabled = false;
  54 bool ThreadService::_thread_allocated_memory_enabled = false;
  55 
  56 PerfCounter*  ThreadService::_total_threads_count = NULL;
  57 PerfVariable* ThreadService::_live_threads_count = NULL;
  58 PerfVariable* ThreadService::_peak_threads_count = NULL;
  59 PerfVariable* ThreadService::_daemon_threads_count = NULL;
  60 volatile int ThreadService::_atomic_threads_count = 0;
  61 volatile int ThreadService::_atomic_daemon_threads_count = 0;
  62 
  63 ThreadDumpResult* ThreadService::_threaddump_list = NULL;
  64 
  65 static const int INITIAL_ARRAY_SIZE = 10;
  66 
  67 void ThreadService::init() {
  68   EXCEPTION_MARK;
  69 
  70   // These counters are for java.lang.management API support.
  71   // They are created even if -XX:-UsePerfData is set and in
  72   // that case, they will be allocated on C heap.
  73 
  74   _total_threads_count =
  75                 PerfDataManager::create_counter(JAVA_THREADS, "started",
  76                                                 PerfData::U_Events, CHECK);
  77 
  78   _live_threads_count =
  79                 PerfDataManager::create_variable(JAVA_THREADS, "live",
  80                                                  PerfData::U_None, CHECK);
  81 


  84                                                  PerfData::U_None, CHECK);
  85 
  86   _daemon_threads_count =
  87                 PerfDataManager::create_variable(JAVA_THREADS, "daemon",
  88                                                  PerfData::U_None, CHECK);
  89 
  90   if (os::is_thread_cpu_time_supported()) {
  91     _thread_cpu_time_enabled = true;
  92   }
  93 
  94   _thread_allocated_memory_enabled = true; // Always on, so enable it
  95 }
  96 
  97 void ThreadService::reset_peak_thread_count() {
  98   // Acquire the lock to update the peak thread count
  99   // to synchronize with thread addition and removal.
 100   MutexLockerEx mu(Threads_lock);
 101   _peak_threads_count->set_value(get_live_thread_count());
 102 }
 103 
 104 static bool is_hidden_thread(JavaThread *thread) {
 105   // hide VM internal or JVMTI agent threads
 106   return thread->is_hidden_from_external_view() || thread->is_jvmti_agent_thread();
 107 }
 108 
 109 void ThreadService::add_thread(JavaThread* thread, bool daemon) {
 110   assert(Threads_lock->owned_by_self(), "must have threads lock");
 111 
 112   // Do not count hidden threads
 113   if (is_hidden_thread(thread)) {
 114     return;
 115   }
 116 
 117   _total_threads_count->inc();
 118   Atomic::inc(&_atomic_threads_count);
 119   int count = _atomic_threads_count;
 120   _live_threads_count->set_value(count);
 121 
 122   if (count > _peak_threads_count->get_value()) {
 123     _peak_threads_count->set_value(count);
 124   }
 125 
 126   if (daemon) {
 127     Atomic::inc(&_atomic_daemon_threads_count);
 128     _daemon_threads_count->set_value(_atomic_daemon_threads_count);
 129   }
 130 }
 131 
 132 void ThreadService::decrement_thread_counts(JavaThread* jt, bool daemon) {
 133   Atomic::dec(&_atomic_threads_count);
 134   _live_threads_count->set_value(_atomic_threads_count);
 135 
 136   if (daemon) {
 137     Atomic::dec(&_atomic_daemon_threads_count);
 138     _daemon_threads_count->set_value(_atomic_daemon_threads_count);
 139   }
 140 }
 141 
 142 void ThreadService::remove_thread(JavaThread* thread, bool daemon) {
 143   assert(Threads_lock->owned_by_self(), "must have threads lock");
 144 
 145   // Do not count hidden threads
 146   if (is_hidden_thread(thread)) {
 147     return;
 148   }
 149 
 150   assert(!thread->is_terminated(), "must not be terminated");
 151   if (!thread->is_exiting()) {
 152     // JavaThread::exit() skipped calling current_thread_exiting()
 153     decrement_thread_counts(thread, daemon);
 154   }
 155 
 156 }
 157 
 158 void ThreadService::current_thread_exiting(JavaThread* jt, bool daemon) {
 159   // Do not count hidden threads
 160   if (is_hidden_thread(jt)) {
 161     return;
 162   }
 163 
 164   assert(jt == JavaThread::current(), "Called by current thread");
 165   assert(!jt->is_terminated() && jt->is_exiting(), "must be exiting");
 166 
 167   decrement_thread_counts(jt, daemon);



 168 }
 169 
 170 // FIXME: JVMTI should call this function
 171 Handle ThreadService::get_current_contended_monitor(JavaThread* thread) {
 172   assert(thread != NULL, "should be non-NULL");
 173   debug_only(Thread::check_for_dangling_thread_pointer(thread);)
 174 
 175   ObjectMonitor *wait_obj = thread->current_waiting_monitor();
 176 
 177   oop obj = NULL;
 178   if (wait_obj != NULL) {
 179     // thread is doing an Object.wait() call
 180     obj = (oop) wait_obj->object();
 181     assert(obj != NULL, "Object.wait() should have an object");
 182   } else {
 183     ObjectMonitor *enter_obj = thread->current_pending_monitor();
 184     if (enter_obj != NULL) {
 185       // thread is trying to enter() or raw_enter() an ObjectMonitor.
 186       obj = (oop) enter_obj->object();
 187     }


< prev index next >